Reddit: posted by hand, with the rules pasted
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>
This commit is contained in:
@@ -2,20 +2,18 @@ package content
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/api/reddit"
|
||||
"supermarket/internal/inventory"
|
||||
"supermarket/internal/utm"
|
||||
)
|
||||
|
||||
func TestGenerateReddit(t *testing.T) {
|
||||
entry := &inventory.Entry{Name: "Jitsi", Relationship: inventory.Partner}
|
||||
items := []Item{{ContentItem: item("1", "Jitsi", time.Now())}}
|
||||
info := &reddit.Subreddit{Name: "selfhosted", Description: "Self-hosting", SubmitText: "Flair your post.",
|
||||
Rules: []reddit.Rule{{Name: "No spam", Description: "10% rule", Kind: "link"}, {Name: "Be civil", Kind: "comment"}}}
|
||||
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})
|
||||
@@ -27,13 +25,19 @@ func TestGenerateReddit(t *testing.T) {
|
||||
t.Fatalf("reddit draft = %+v", r)
|
||||
}
|
||||
prompt := f.reqs[0].Prompt
|
||||
for _, want := range []string{"r/selfhosted", "I work at Acme", "No hashtags", "Flair your post.", "Rule 1: No spam"} {
|
||||
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)
|
||||
}
|
||||
}
|
||||
if strings.Contains(prompt, "Be civil") {
|
||||
t.Error("comment-only rules needn't go in the prompt")
|
||||
// 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" {
|
||||
@@ -71,33 +75,56 @@ func TestRedditDraftsAndCache(t *testing.T) {
|
||||
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}, utm.NewTagger([]string{"acme.example"}), dr.ID); err != nil {
|
||||
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 !strings.HasPrefix(pub.calls[0], "r/selfhosted|New title|Body|https://acme.example/a?utm_") ||
|
||||
!strings.Contains(pub.calls[0], "utm_source=reddit") {
|
||||
t.Errorf("published %q", pub.calls[0])
|
||||
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("empty cache: %v %v", s, err)
|
||||
t.Fatalf("no rules yet: %v %v", s, err)
|
||||
}
|
||||
at := time.Now().UTC().Truncate(time.Second)
|
||||
if err := SaveSubreddit(d, &reddit.Subreddit{Name: "selfhosted", SubmissionType: "self", Rules: []reddit.Rule{{Name: "R"}}, FetchedAt: at}); err != nil {
|
||||
// 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)
|
||||
}
|
||||
s, err := GetSubreddit(d, "r/SelfHosted")
|
||||
if err != nil || s == nil || len(s.Rules) != 1 || !s.FetchedAt.Equal(at) || s.AllowsLinks() {
|
||||
t.Errorf("cached = %+v, %v", s, 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)
|
||||
own := utm.NewTagger([]string{"acme.example"}).Own
|
||||
info := &reddit.Subreddit{Name: "selfhosted", SubmissionType: "link", Type: "restricted",
|
||||
Rules: []reddit.Rule{{Name: "No self-promotion"}, {Name: "Be nice"}}}
|
||||
info := &Subreddit{Name: "selfhosted", Rules: "1. No self-promotion\n2. Be nice"}
|
||||
|
||||
has := func(ws []string, sub string) bool {
|
||||
for _, w := range ws {
|
||||
@@ -109,29 +136,22 @@ func TestRedditWarnings(t *testing.T) {
|
||||
}
|
||||
|
||||
w := RedditWarnings(RedditCheck{Target: "selfhosted", Info: info, Now: now})
|
||||
if !has(w, "restricted") || !has(w, "has no link") || !has(w, "“No self-promotion”") || has(w, "Be nice") || has(w, "past week") {
|
||||
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", Now: now}); !has(w, "Couldn't read r/x's rules") {
|
||||
t.Errorf("no info = %q", w)
|
||||
if w := RedditWarnings(RedditCheck{Target: "x", Promo: true, Now: now}); !has(w, "promotes your own work") {
|
||||
t.Errorf("promo = %q", w)
|
||||
}
|
||||
if w := RedditWarnings(RedditCheck{Target: "x", Info: &reddit.Subreddit{}, History: []reddit.Submission{}, Now: now}); !has(w, "hasn't posted before") {
|
||||
t.Errorf("new account = %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)},
|
||||
}
|
||||
|
||||
history := []reddit.Submission{
|
||||
{Subreddit: "SelfHosted", URL: "https://acme.example/1", Created: now.Add(-2 * 24 * time.Hour)},
|
||||
{Subreddit: "homelab", Text: "see https://acme.example/2)", IsSelf: true, Created: now.Add(-10 * 24 * time.Hour)},
|
||||
{Subreddit: "golang", Created: now.Add(-20 * 24 * time.Hour)},
|
||||
{Subreddit: "selfhosted", URL: "https://acme.example/old", Created: now.Add(-60 * 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)
|
||||
}
|
||||
w = RedditWarnings(RedditCheck{Target: "selfhosted", Info: &reddit.Subreddit{}, History: history, Own: own, OwnLink: true, HasLink: true, Now: now})
|
||||
if !has(w, "posted in r/selfhosted 1 time in the past week") || !has(w, "3 of your 4 posts") {
|
||||
t.Errorf("history warnings = %q", w)
|
||||
}
|
||||
// Without a link of our own, the share isn't the issue.
|
||||
w = RedditWarnings(RedditCheck{Target: "golang", Info: &reddit.Subreddit{}, History: history, Own: own, Now: now})
|
||||
if len(w) != 0 {
|
||||
if w := RedditWarnings(RedditCheck{Target: "golang", Recorded: recorded, Now: now}); len(w) != 0 {
|
||||
t.Errorf("clean post = %q", w)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user