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:
@@ -9,10 +9,12 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/api/reddit"
|
||||
"supermarket/internal/content"
|
||||
"supermarket/internal/inventory"
|
||||
"supermarket/internal/models"
|
||||
"supermarket/internal/profile"
|
||||
"supermarket/internal/secrets"
|
||||
)
|
||||
|
||||
func TestContentPages(t *testing.T) {
|
||||
@@ -197,3 +199,77 @@ func mustID(s string) int64 {
|
||||
n, _ := strconv.ParseInt(s, 10, 64)
|
||||
return n
|
||||
}
|
||||
|
||||
func TestRedditInInbox(t *testing.T) {
|
||||
s, h := newTestServer(t, nil)
|
||||
if _, err := content.CreateDrafts(s.DB, []content.Draft{
|
||||
{InventoryName: "Jitsi", Kind: "partner", Channel: content.ChannelReddit, Title: "Jitsi 3", Target: "selfhosted", Content: "I work at Acme.", SourceURL: "https://jitsi.example/3"},
|
||||
{InventoryName: "Jitsi", Kind: "partner", Channel: content.ChannelReddit, Title: "Other", Target: "homelab", Content: "Body"},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := content.SaveSubreddit(s.DB, &reddit.Subreddit{Name: "selfhosted", SubmitText: "Flair it.", SubmissionType: "self",
|
||||
Rules: []reddit.Rule{{Name: "No self-promotion", Description: "Limit it."}}, FetchedAt: time.Now()}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body := do(h, http.MethodGet, "/inbox", nil, nil).Body.String()
|
||||
for _, want := range []string{"Reddit r/selfhosted", `name="title" value="Jitsi 3"`, "Flair it.", "No self-promotion",
|
||||
"has a rule about this", "Couldn't read r/homelab's rules", `action="/reddit/rules/homelab"`, "Read rules", "text only"} {
|
||||
if !strings.Contains(body, want) {
|
||||
t.Errorf("inbox lacks %q", want)
|
||||
}
|
||||
}
|
||||
|
||||
ds, _ := content.ListDrafts(s.DB, 10, content.StatusDraft)
|
||||
var id string
|
||||
for _, d := range ds {
|
||||
if d.Target == "selfhosted" {
|
||||
id = strconv.FormatInt(d.ID, 10)
|
||||
}
|
||||
}
|
||||
if rec := postForm(h, "/drafts/"+id+"/save", url.Values{"title": {"Jitsi 3 is out"}, "content": {"Edited"}}); rec.Code != http.StatusSeeOther {
|
||||
t.Fatalf("save: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if d, _ := content.GetDraft(s.DB, mustID(id)); d.Title != "Jitsi 3 is out" || d.Content != "Edited" {
|
||||
t.Errorf("saved: %+v", d)
|
||||
}
|
||||
if rec := postForm(h, "/drafts/"+id+"/save", url.Values{"title": {""}}); !strings.Contains(rec.Body.String(), "the title is empty") {
|
||||
t.Errorf("empty title: %s", rec.Body.String())
|
||||
}
|
||||
if rec := postForm(h, "/drafts/"+id+"/publish", nil); !strings.Contains(rec.Body.String(), "Reddit isn't connected") {
|
||||
t.Errorf("publish without Reddit: %s", rec.Body.String())
|
||||
}
|
||||
// Reading rules needs Reddit credentials.
|
||||
if rec := postForm(h, "/reddit/rules/homelab", nil); !strings.Contains(rec.Body.String(), "client ID and secret") {
|
||||
t.Errorf("refresh without credentials: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
// Signing in needs the client ID and secret first.
|
||||
if rec := postForm(h, "/auth/reddit/start", nil); rec.Code != http.StatusBadRequest {
|
||||
t.Errorf("reddit start without credentials: %d", rec.Code)
|
||||
}
|
||||
_ = s.Secrets.Set(map[string]string{secrets.RedditClientID: "id", secrets.RedditClientSecret: "sec"})
|
||||
rec := postForm(h, "/auth/reddit/start", nil)
|
||||
if loc := rec.Header().Get("Location"); rec.Code != http.StatusSeeOther || !strings.HasPrefix(loc, "https://www.reddit.com/api/v1/authorize?") ||
|
||||
!strings.Contains(loc, "duration=permanent") || !strings.Contains(loc, "submit") {
|
||||
t.Errorf("reddit start: %d %s", rec.Code, loc)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteRedditNeedsSubreddit(t *testing.T) {
|
||||
s, h := newTestServer(t, nil)
|
||||
_ = profile.Save(s.DB, &profile.Profile{Name: "Acme", Description: "Widgets.", Accounts: profile.Accounts{Subreddits: []string{"selfhosted"}}})
|
||||
e := &inventory.Entry{Name: "Jitsi", Relationship: inventory.Partner, Subreddits: []string{"r/jitsi", "selfhosted"}}
|
||||
_ = inventory.Save(s.DB, e)
|
||||
id := strconv.FormatInt(e.ID, 10)
|
||||
_, _ = content.SaveItems(s.DB, []models.ContentItem{{ID: "a", Source: models.SourceRSS, Body: "x", PublishedAt: time.Now(), AppName: "Jitsi"}})
|
||||
|
||||
body := do(h, http.MethodGet, "/content/"+id, nil, nil).Body.String()
|
||||
if !strings.Contains(body, `<option value="jitsi">`) || strings.Count(body, `<option value="selfhosted">`) != 1 {
|
||||
t.Errorf("subreddit suggestions:\n%s", body)
|
||||
}
|
||||
_ = s.Secrets.Set(map[string]string{secrets.AnthropicAPIKey: "k"})
|
||||
rec := postForm(h, "/content/"+id+"/items", url.Values{"action": {"write"}, "item": {"a"}, "channel": {"reddit"}, "subreddit": {" "}})
|
||||
if !strings.Contains(rec.Body.String(), "choose a subreddit") {
|
||||
t.Errorf("write without subreddit: %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user