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>
158 lines
6.0 KiB
Go
158 lines
6.0 KiB
Go
package content
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"supermarket/internal/inventory"
|
|
)
|
|
|
|
func TestGenerateReddit(t *testing.T) {
|
|
entry := &inventory.Entry{Name: "Jitsi", Relationship: inventory.Partner}
|
|
items := []Item{{ContentItem: item("1", "Jitsi", time.Now())}}
|
|
info := &Subreddit{Name: "selfhosted", Rules: "1. No spam: the 10% rule\n2. Flair your post."}
|
|
f := &fakeAI{replies: []string{`{"x": "Short", "reddit": {"title": " Jitsi 3 is out ", "body": "I work at Acme. Details."}}`}}
|
|
drafts, err := Generate(context.Background(), f, Request{Profile: testProfile(), Entry: entry, Items: items,
|
|
Channels: []string{ChannelX, ChannelReddit}, Subreddit: "r/selfhosted", SubredditInfo: info})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := drafts[1]
|
|
if r.Channel != ChannelReddit || r.Title != "Jitsi 3 is out" || r.Target != "selfhosted" || r.Content != "I work at Acme. Details." {
|
|
t.Fatalf("reddit draft = %+v", r)
|
|
}
|
|
prompt := f.reqs[0].Prompt
|
|
for _, want := range []string{"r/selfhosted", "I work at Acme", "No hashtags", "2. Flair your post.", "1. No spam: the 10% rule"} {
|
|
if !strings.Contains(prompt, want) {
|
|
t.Errorf("prompt lacks %q", want)
|
|
}
|
|
}
|
|
// Without rules, the model is told they weren't given.
|
|
f = &fakeAI{replies: []string{`{"reddit": {"title": "T", "body": "B"}}`}}
|
|
if _, err := Generate(context.Background(), f, Request{Profile: testProfile(), Entry: entry, Items: items,
|
|
Channels: []string{ChannelReddit}, Subreddit: "selfhosted"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(f.reqs[0].Prompt, "rules weren't given") {
|
|
t.Error("no note about missing rules")
|
|
}
|
|
props := f.reqs[0].Schema["properties"].(map[string]any)
|
|
if props["reddit"].(map[string]any)["type"] != "object" {
|
|
t.Errorf("reddit schema = %v", props["reddit"])
|
|
}
|
|
// A Reddit draft needs a subreddit.
|
|
if _, err := Generate(context.Background(), f, Request{Profile: testProfile(), Entry: entry, Items: items, Channels: []string{ChannelReddit}}); err == nil {
|
|
t.Error("expected an error without a subreddit")
|
|
}
|
|
}
|
|
|
|
func TestRedditDraftsAndCache(t *testing.T) {
|
|
d := openDB(t)
|
|
_, err := CreateDrafts(d, []Draft{{Channel: ChannelReddit, Title: "Hi", Target: "selfhosted", Content: "Body", SourceURL: "https://acme.example/a"}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ds, _ := ListDrafts(d, 1, StatusDraft)
|
|
dr := ds[0]
|
|
if dr.Title != "Hi" || dr.Target != "selfhosted" || dr.Where() != "Reddit r/selfhosted" || dr.Problem() != "" {
|
|
t.Fatalf("draft = %+v (%s)", dr, dr.Problem())
|
|
}
|
|
if err := UpdateTitle(d, dr.ID, " New title "); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := UpdateTitle(d, dr.ID, " "); err == nil {
|
|
t.Error("empty title accepted")
|
|
}
|
|
got, _ := GetDraft(d, dr.ID)
|
|
if got.Title != "New title" {
|
|
t.Errorf("title = %q", got.Title)
|
|
}
|
|
got.Title = strings.Repeat("t", 301)
|
|
if !strings.Contains(got.Problem(), "title is 301") {
|
|
t.Errorf("problem = %q", got.Problem())
|
|
}
|
|
|
|
// Reddit drafts are posted by hand: never published or scheduled.
|
|
pub := &fakePub{}
|
|
if _, err := Publish(context.Background(), d, ChannelPublishers{ChannelReddit: pub}, nil, dr.ID); !errors.Is(err, ErrByHand) || len(pub.calls) != 0 {
|
|
t.Errorf("publish: %v, %d calls", err, len(pub.calls))
|
|
}
|
|
if err := Schedule(d, dr.ID, time.Now().Add(time.Hour)); !errors.Is(err, ErrByHand) {
|
|
t.Errorf("schedule: %v", err)
|
|
}
|
|
if err := MarkPosted(d, dr.ID, "https://www.reddit.com/r/selfhosted/comments/abc"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if rec, _ := RecordedRedditPosts(d, time.Now().Add(-time.Hour)); len(rec) != 1 || rec[0].Subreddit != "selfhosted" {
|
|
t.Errorf("recorded = %+v", rec)
|
|
}
|
|
|
|
if s, err := GetSubreddit(d, "selfhosted"); s != nil || err != nil {
|
|
t.Fatalf("no rules yet: %v %v", s, err)
|
|
}
|
|
// A row from before, read through Reddit's API, counts as no rules.
|
|
if _, err := d.Exec(`INSERT INTO subreddits (name, data, fetched_at) VALUES ('selfhosted', '{"name":"selfhosted","rules":[{"name":"R"}]}', '')`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s, err := GetSubreddit(d, "selfhosted"); s != nil || err != nil {
|
|
t.Fatalf("old row: %v %v", s, err)
|
|
}
|
|
at := time.Now().UTC().Truncate(time.Second)
|
|
if err := SaveRules(d, "r/selfhosted", " 1. Be nice\r\n2. No ads ", at); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s, err := GetSubreddit(d, "https://www.reddit.com/r/selfhosted/about/rules")
|
|
if err != nil || s == nil || s.Rules != "1. Be nice\n2. No ads" || !s.SavedAt.Equal(at) {
|
|
t.Errorf("saved = %+v, %v", s, err)
|
|
}
|
|
if err := SaveRules(d, "selfhosted", strings.Repeat("x", MaxRules+1), at); err == nil {
|
|
t.Error("too long accepted")
|
|
}
|
|
if err := SaveRules(d, "selfhosted", " ", at); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s, _ := GetSubreddit(d, "selfhosted"); s != nil {
|
|
t.Error("emptied rules kept")
|
|
}
|
|
if RulesURL("r/selfhosted") != "https://www.reddit.com/r/selfhosted/about/rules" {
|
|
t.Error(RulesURL("r/selfhosted"))
|
|
}
|
|
}
|
|
|
|
func TestRedditWarnings(t *testing.T) {
|
|
now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC)
|
|
info := &Subreddit{Name: "selfhosted", Rules: "1. No self-promotion\n2. Be nice"}
|
|
|
|
has := func(ws []string, sub string) bool {
|
|
for _, w := range ws {
|
|
if strings.Contains(w, sub) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
w := RedditWarnings(RedditCheck{Target: "selfhosted", Info: info, Now: now})
|
|
if !has(w, "“1. No self-promotion”") || has(w, "Be nice") || has(w, "past week") {
|
|
t.Errorf("rules warnings = %q", w)
|
|
}
|
|
if w := RedditWarnings(RedditCheck{Target: "x", Promo: true, Now: now}); !has(w, "promotes your own work") {
|
|
t.Errorf("promo = %q", w)
|
|
}
|
|
recorded := []RedditPost{
|
|
{Subreddit: "SelfHosted", Created: now.Add(-2 * 24 * time.Hour)},
|
|
{Subreddit: "selfhosted", Created: now.Add(-20 * 24 * time.Hour)},
|
|
{Subreddit: "homelab", Created: now.Add(-1 * 24 * time.Hour)},
|
|
}
|
|
w = RedditWarnings(RedditCheck{Target: "selfhosted", Recorded: recorded, Now: now})
|
|
if !has(w, "posted in r/selfhosted 1 time in the past week") {
|
|
t.Errorf("recorded warnings = %q", w)
|
|
}
|
|
if w := RedditWarnings(RedditCheck{Target: "golang", Recorded: recorded, Now: now}); len(w) != 0 {
|
|
t.Errorf("clean post = %q", w)
|
|
}
|
|
}
|