If the Subreddits box is empty, the AI suggests up to eight, by the relationship, each with who is there. Each is looked up on Reddit (with the app's client ID and secret) and left out if it doesn't exist, is private, 18+, restricted or archived; the first five kept fill the box. The form lists every suggestion with members, post types and why any was left out. Lookups are cached for drafting. Without Reddit keys the suggestions are kept and the form says they weren't checked. The Reddit client now returns ErrNotFound and ErrForbidden, and treats an empty about listing as not found. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
152 lines
5.1 KiB
Go
152 lines
5.1 KiB
Go
package inventory
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"supermarket/internal/ai"
|
|
"supermarket/internal/api/reddit"
|
|
"supermarket/internal/profile"
|
|
)
|
|
|
|
type fakeAI struct {
|
|
calls int
|
|
prompt string
|
|
reply any
|
|
}
|
|
|
|
func (f *fakeAI) Generate(_ context.Context, req ai.Request) (string, error) {
|
|
f.calls++
|
|
f.prompt = req.Prompt
|
|
raw, _ := json.Marshal(f.reply)
|
|
return string(raw), nil
|
|
}
|
|
|
|
func rivalSite(t *testing.T) *httptest.Server {
|
|
t.Helper()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.Write([]byte(`<title>Rival</title><link rel="alternate" type="application/rss+xml" href="/feed">
|
|
<p>Rival makes hosted widgets.</p>
|
|
<a href="https://x.com/rivalhq">X</a> <a href="https://www.linkedin.com/company/rival">LinkedIn</a>`))
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
return srv
|
|
}
|
|
|
|
func TestFillKeepsWhatWasTyped(t *testing.T) {
|
|
srv := rivalSite(t)
|
|
fake := &fakeAI{reply: map[string]any{
|
|
"name": "Rival Inc", "description": "Hosted widgets.", "talking_points": "We self-host; they don't.",
|
|
"keywords": []string{"widgets"}, "hashtags": []string{"Rival"},
|
|
}}
|
|
e := &Entry{Name: "Rival", Relationship: Competitor, TwitterHandle: "typed"}
|
|
ours := &profile.Profile{Name: "Acme", Description: "Self-hosted widgets."}
|
|
res, err := Fill(context.Background(), fake, e, srv.URL, ours, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
filled := res.Filled
|
|
if e.Name != "Rival" || e.TwitterHandle != "typed" {
|
|
t.Errorf("typed values changed: %+v", e)
|
|
}
|
|
if e.LinkedIn != "https://www.linkedin.com/company/rival" || len(e.RSS) != 1 || e.RSS[0] != srv.URL+"/feed" ||
|
|
len(e.Websites) != 1 || e.Description != "Hosted widgets." || e.TalkingPoints == "" {
|
|
t.Errorf("not filled: %+v", e)
|
|
}
|
|
want := "website, LinkedIn page, feeds, description, talking points, keywords, hashtags"
|
|
if strings.Join(filled, ", ") != want {
|
|
t.Errorf("filled = %q", strings.Join(filled, ", "))
|
|
}
|
|
for _, s := range []string{"Relationship: competitor", "Name: Acme", "Rival makes hosted widgets."} {
|
|
if !strings.Contains(fake.prompt, s) {
|
|
t.Errorf("prompt lacks %q", s)
|
|
}
|
|
}
|
|
if err := e.Normalize(); err != nil || e.Hashtags[0] != "#Rival" {
|
|
t.Errorf("normalize: %v %q", err, e.Hashtags)
|
|
}
|
|
}
|
|
|
|
func TestFillSkipsTheModelWhenNothingIsLeft(t *testing.T) {
|
|
srv := rivalSite(t)
|
|
fake := &fakeAI{}
|
|
e := &Entry{Name: "Rival", Relationship: Partner, Description: "d", TalkingPoints: "t",
|
|
Keywords: []string{"k"}, Hashtags: []string{"#h"}, Subreddits: []string{"selfhosted"}}
|
|
if _, err := Fill(context.Background(), fake, e, srv.URL, nil, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if fake.calls != 0 {
|
|
t.Errorf("model called %d times", fake.calls)
|
|
}
|
|
if e.TwitterHandle != "rivalhq" {
|
|
t.Errorf("links not read: %+v", e)
|
|
}
|
|
}
|
|
|
|
func TestFillChecksSuggestedSubreddits(t *testing.T) {
|
|
srv := rivalSite(t)
|
|
fake := &fakeAI{reply: map[string]any{
|
|
"name": "", "description": "", "talking_points": "", "keywords": []string{}, "hashtags": []string{},
|
|
"subreddits": []map[string]string{
|
|
{"name": "r/selfhosted", "why": "self-hosters"},
|
|
{"name": "SelfHosted", "why": "again"},
|
|
{"name": "madeupwidgets", "why": "invented"},
|
|
{"name": "secretclub", "why": "private"},
|
|
{"name": "announcements", "why": "restricted"},
|
|
{"name": "homelab", "why": "home servers"},
|
|
{"name": "slowreddit", "why": "rate limited"},
|
|
},
|
|
}}
|
|
check := func(_ context.Context, name string) (*reddit.Subreddit, error) {
|
|
switch name {
|
|
case "selfhosted":
|
|
return &reddit.Subreddit{Name: "selfhosted", Type: "public", Subscribers: 500000}, nil
|
|
case "homelab":
|
|
return &reddit.Subreddit{Name: "homelab", Type: "public"}, nil
|
|
case "secretclub":
|
|
return nil, reddit.ErrForbidden
|
|
case "announcements":
|
|
return &reddit.Subreddit{Name: "announcements", Type: "restricted"}, nil
|
|
case "slowreddit":
|
|
return nil, errors.New("rate limited")
|
|
}
|
|
return nil, reddit.ErrNotFound
|
|
}
|
|
e := &Entry{Name: "Rival", Relationship: Partner}
|
|
res, err := Fill(context.Background(), fake, e, srv.URL, nil, check)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := strings.Join(e.Subreddits, ","); got != "selfhosted,homelab,slowreddit" {
|
|
t.Errorf("subreddits = %q", got)
|
|
}
|
|
var notes []string
|
|
for _, sg := range res.Suggestions {
|
|
notes = append(notes, fmt.Sprintf("%s:%v", sg.Name, sg.Kept))
|
|
}
|
|
if got := strings.Join(notes, " "); got != "selfhosted:true madeupwidgets:false secretclub:false announcements:false homelab:true slowreddit:true" {
|
|
t.Errorf("suggestions = %s", got)
|
|
}
|
|
if res.Suggestions[5].Problem == "" || res.Suggestions[0].Info == nil {
|
|
t.Errorf("unchecked one should say so, checked one should carry its info: %+v", res.Suggestions)
|
|
}
|
|
|
|
// Without a check, suggestions are kept, marked unchecked.
|
|
e = &Entry{Name: "Rival", Relationship: Partner}
|
|
res, _ = Fill(context.Background(), fake, e, srv.URL, nil, nil)
|
|
if len(e.Subreddits) != 5 || !strings.Contains(res.Suggestions[0].Problem, "not checked") {
|
|
t.Errorf("unchecked: %q %+v", e.Subreddits, res.Suggestions)
|
|
}
|
|
}
|