- 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>
86 lines
3.4 KiB
Go
86 lines
3.4 KiB
Go
package reddit
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestClient(t *testing.T) {
|
|
var submitted string
|
|
refreshed := 0
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Header.Get("User-Agent") != "web:supermarket:test (by /u/pat)" {
|
|
t.Errorf("User-Agent = %q", r.Header.Get("User-Agent"))
|
|
}
|
|
if r.Header.Get("Authorization") == "Bearer old" {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
switch r.URL.Path {
|
|
case "/api/v1/me":
|
|
w.Write([]byte(`{"name":"pat"}`))
|
|
case "/api/submit":
|
|
r.ParseForm()
|
|
submitted = r.PostForm.Encode()
|
|
if r.PostForm.Get("sr") == "strict" {
|
|
w.Write([]byte(`{"json":{"errors":[["SUBREDDIT_NOTALLOWED","you aren't allowed to post there.","sr"]]}}`))
|
|
return
|
|
}
|
|
w.Write([]byte(`{"json":{"errors":[],"data":{"url":"https://www.reddit.com/r/selfhosted/comments/abc/hi/"}}}`))
|
|
case "/r/selfhosted/about":
|
|
w.Write([]byte(`{"data":{"display_name":"selfhosted","public_description":"Self-hosting","submit_text":"No spam.","submission_type":"self","subreddit_type":"public","subscribers":500000}}`))
|
|
case "/r/selfhosted/about/rules":
|
|
w.Write([]byte(`{"rules":[{"short_name":"No self-promotion","description":"Limit it to 10%.","kind":"link"}]}`))
|
|
case "/user/pat/submitted":
|
|
w.Write([]byte(`{"data":{"children":[{"data":{"subreddit":"selfhosted","title":"T","url":"https://acme.example/x","is_self":false,"created_utc":1794000000}}]}}`))
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := New("old", UserAgent("test", "pat"), func(context.Context) (string, error) {
|
|
refreshed++
|
|
return "new", nil
|
|
})
|
|
c.base = srv.URL
|
|
ctx := context.Background()
|
|
|
|
if name, err := c.Me(ctx); err != nil || name != "pat" || refreshed != 1 {
|
|
t.Fatalf("Me = %q, %v (refreshed %d)", name, err, refreshed)
|
|
}
|
|
u, err := c.Submit(ctx, Post{Subreddit: "selfhosted", Title: "Hi", Text: "Body"})
|
|
if err != nil || !strings.Contains(u, "/comments/abc/") || !strings.Contains(submitted, "kind=self") || !strings.Contains(submitted, "text=Body") {
|
|
t.Errorf("Submit = %q, %v; sent %s", u, err, submitted)
|
|
}
|
|
if _, err := c.Submit(ctx, Post{Subreddit: "selfhosted", Title: "Hi", URL: "https://acme.example"}); err != nil || !strings.Contains(submitted, "kind=link") {
|
|
t.Errorf("link post sent %s (%v)", submitted, err)
|
|
}
|
|
if _, err := c.Submit(ctx, Post{Subreddit: "strict", Title: "Hi", Text: "x"}); err == nil || !strings.Contains(err.Error(), "aren't allowed") {
|
|
t.Errorf("refused submit: %v", err)
|
|
}
|
|
s, err := c.SubredditInfo(ctx, "/r/selfhosted/")
|
|
if err != nil || s.SubmitText != "No spam." || len(s.Rules) != 1 || s.Rules[0].Name != "No self-promotion" || s.AllowsLinks() || !s.AllowsText() {
|
|
t.Errorf("SubredditInfo = %+v, %v", s, err)
|
|
}
|
|
subs, err := c.Submitted(ctx, "pat")
|
|
if err != nil || len(subs) != 1 || subs[0].Subreddit != "selfhosted" || subs[0].Created.Year() != 2026 {
|
|
t.Errorf("Submitted = %+v, %v", subs, err)
|
|
}
|
|
if _, err := c.SubredditInfo(ctx, "nope"); err == nil {
|
|
t.Error("expected 404")
|
|
}
|
|
}
|
|
|
|
func TestCleanName(t *testing.T) {
|
|
for in, want := range map[string]string{"selfhosted": "selfhosted", "r/selfhosted": "selfhosted", "/r/selfhosted/": "selfhosted",
|
|
"https://www.reddit.com/r/homelab/top?t=week": "homelab", " r/Go ": "Go"} {
|
|
if got := CleanName(in); got != want {
|
|
t.Errorf("CleanName(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|