Reddit's API isn't open to apps like this one, so Supermarket no longer signs in to Reddit, publishes or schedules there, or reads subreddits and the account's posts. The internal/api/reddit client, Reddit's OAuth, its keys and its Social accounts card are gone. Supermarket still writes the post. Each Reddit draft in Posts has a box for its subreddit's rules, with a link to them on Reddit: pasted once, they're kept (subreddits.data, as pasted_rules) and given to the model for later drafts. Open in Reddit saves the edits and opens the submit page filled in; Copy title and Copy text (with the tagged source link) cover a box that comes up empty; Mark as posted records it. Warnings come from the pasted rules and the posts marked as posted. Fill's subreddit suggestions are kept unchecked, and say so. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
123 lines
3.9 KiB
Go
123 lines
3.9 KiB
Go
package inventory
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"supermarket/internal/ai"
|
|
"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)
|
|
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); 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 TestFillSuggestsSubreddits(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": "not a name!", "why": "junk"},
|
|
{"name": "https://www.reddit.com/r/homelab/", "why": "home servers"},
|
|
{"name": "one", "why": ""}, {"name": "two", "why": ""}, {"name": "three", "why": ""}, {"name": "four", "why": ""},
|
|
},
|
|
}}
|
|
e := &Entry{Name: "Rival", Relationship: Partner}
|
|
res, err := Fill(context.Background(), fake, e, srv.URL, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := strings.Join(e.Subreddits, ","); got != "selfhosted,homelab,one,two,three" {
|
|
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 homelab:true one:true two:true three:true four:false" {
|
|
t.Errorf("suggestions = %s", got)
|
|
}
|
|
}
|