Phase 1, part 3: Reddit
- Sign in with a Reddit web app; access tokens refresh automatically, and an app-only token reads rules before sign-in. - Reddit drafts: a title and text for one subreddit, chosen when writing. The subreddit's guidelines and rules go into the prompt, which asks for a community member's post that discloses the affiliation and avoids marketing language. - The Inbox shows the subreddit's rules (cached for a day, with Refresh) and warns, without blocking, about promotion rules, restricted subreddits, link-only subreddits, posting in the same subreddit within a week, more than 1 in 10 recent posts linking to the company's sites, and new accounts. - Publishing makes a text post with the source link at the end, or a link post where the subreddit takes only links. - Migration 4 adds the draft's title and subreddit, and the rules cache. Publishers now take a Post, which carries the title and subreddit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
package content
|
||||
|
||||
import (
|
||||
"context"
|
||||
"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"}}}
|
||||
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", "Flair your post.", "Rule 1: No spam"} {
|
||||
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")
|
||||
}
|
||||
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())
|
||||
}
|
||||
|
||||
pub := &fakePub{}
|
||||
if _, err := Publish(context.Background(), d, Publishers{ChannelReddit: pub}, utm.NewTagger([]string{"acme.example"}), dr.ID); 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 s, err := GetSubreddit(d, "selfhosted"); s != nil || err != nil {
|
||||
t.Fatalf("empty cache: %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 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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"}}}
|
||||
|
||||
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, "restricted") || !has(w, "has no link") || !has(w, "“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", Info: &reddit.Subreddit{}, History: []reddit.Submission{}, Now: now}); !has(w, "hasn't posted before") {
|
||||
t.Errorf("new account = %q", w)
|
||||
}
|
||||
|
||||
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", 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 {
|
||||
t.Errorf("clean post = %q", w)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user