Phase 1, part 1: fetch, write and publish posts from the web app

Brings back posting, which Phase 0 removed with the terminal interface.

- Content page: per inventory entry, fetch its X account, X hashtag
  search, YouTube channel and RSS/Atom feeds (new), then select, dismiss
  or restore the fetched items, which are kept in the database.
- Writing: one AI call returns a draft per channel (X and LinkedIn),
  prompted from the company profile, the entry's relationship, talking
  points and mention policy, and each channel's rules. Fetched text is
  quoted as untrusted. An over-long X draft gets one request to shorten.
- Drafts page: edit with a live length counter that counts like X, then
  publish each draft on its own. A draft is claimed before posting so it
  can't go out twice; failures are kept on the draft for a retry; X
  tokens are refreshed and saved.
- Migration 2 adds content_items and drafts, and turns queued posts into
  drafts for the platforms they hadn't reached.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-20 12:58:24 -07:00
co-authored by Claude Opus 5.5
parent abf410d097
commit 13bf18facc
29 changed files with 2484 additions and 95 deletions
+129
View File
@@ -0,0 +1,129 @@
package web
import (
"net/http"
"net/url"
"strconv"
"strings"
"testing"
"time"
"supermarket/internal/content"
"supermarket/internal/inventory"
"supermarket/internal/models"
"supermarket/internal/profile"
)
func TestContentPages(t *testing.T) {
s, h := newTestServer(t, nil)
if err := profile.Save(s.DB, &profile.Profile{Name: "Acme", Description: "Widgets."}); err != nil {
t.Fatal(err)
}
e := &inventory.Entry{Name: "Jitsi", Relationship: inventory.Partner, RSS: []string{"https://jitsi.example/feed"}}
if err := inventory.Save(s.DB, e); err != nil {
t.Fatal(err)
}
id := strconv.FormatInt(e.ID, 10)
now := time.Now()
if _, err := content.SaveItems(s.DB, []models.ContentItem{
{ID: "a", Source: models.SourceRSS, Title: "Jitsi 3 is out", Body: "Faster calls.", URL: "https://jitsi.example/3", PublishedAt: now, AppName: "Jitsi"},
{ID: "b", Source: models.SourceTwitterAccount, AuthorHandle: "jitsinews", Body: "Hello", PublishedAt: now.Add(-time.Hour), AppName: "Jitsi"},
}); err != nil {
t.Fatal(err)
}
rec := do(h, http.MethodGet, "/content", nil, nil)
if body := rec.Body.String(); rec.Code != 200 || !strings.Contains(body, "1 RSS feed") || !strings.Contains(body, ">2<") {
t.Errorf("content index: %d\n%s", rec.Code, body)
}
rec = do(h, http.MethodGet, "/content/"+id, nil, nil)
if body := rec.Body.String(); !strings.Contains(body, "Jitsi 3 is out") || !strings.Contains(body, "@jitsinews") ||
!strings.Contains(body, "not connected") {
t.Errorf("entry page:\n%s", body)
}
// Nothing selected, or no channel: an error on the page, no drafts.
rec = postForm(h, "/content/"+id+"/items", url.Values{"action": {"write"}})
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "Select at least one item") {
t.Errorf("write with nothing selected: %d", rec.Code)
}
rec = postForm(h, "/content/"+id+"/items", url.Values{"action": {"write"}, "item": {"a"}})
if !strings.Contains(rec.Body.String(), "choose at least one channel") {
t.Errorf("write without channels:\n%s", rec.Body.String())
}
// No AI key: the provider's error is shown.
rec = postForm(h, "/content/"+id+"/items", url.Values{"action": {"write"}, "item": {"a"}, "channel": {"x"}})
if !strings.Contains(rec.Body.String(), "Anthropic API key") {
t.Errorf("write without an AI key:\n%s", rec.Body.String())
}
// Dismiss and restore.
rec = postForm(h, "/content/"+id+"/items", url.Values{"action": {"dismiss"}, "item": {"b"}})
if rec.Code != http.StatusSeeOther {
t.Fatalf("dismiss: %d", rec.Code)
}
if items, _ := content.ListItems(s.DB, "Jitsi", true, 10); len(items) != 1 || items[0].ID != "b" {
t.Errorf("dismissed: %+v", items)
}
postForm(h, "/content/"+id+"/items", url.Values{"action": {"restore"}, "item": {"b"}, "dismissed": {"1"}})
if items, _ := content.ListItems(s.DB, "Jitsi", true, 10); len(items) != 0 {
t.Errorf("still dismissed: %+v", items)
}
if rec := do(h, http.MethodGet, "/content/999", nil, nil); rec.Code != http.StatusNotFound {
t.Errorf("missing entry: %d", rec.Code)
}
}
func TestDraftsPage(t *testing.T) {
s, h := newTestServer(t, nil)
if _, err := content.CreateDrafts(s.DB, []content.Draft{
{InventoryName: "Jitsi", Kind: "partner", Channel: content.ChannelLinkedIn, Content: "LinkedIn text", SourceURL: "https://jitsi.example/3"},
{InventoryName: "Jitsi", Kind: "partner", Channel: content.ChannelX, Content: "X text", SourceURL: "https://jitsi.example/3"},
}); err != nil {
t.Fatal(err)
}
drafts, _ := content.ListDrafts(s.DB, 10, content.StatusDraft)
ids := map[string]string{}
for _, d := range drafts {
ids[d.Channel] = strconv.FormatInt(d.ID, 10)
}
rec := do(h, http.MethodGet, "/drafts", nil, nil)
body := rec.Body.String()
if rec.Code != 200 || strings.Index(body, "X text") > strings.Index(body, "LinkedIn text") ||
!strings.Contains(body, "31 / 280 including the link") || !strings.Contains(body, `data-reserve="25"`) {
t.Errorf("drafts page (X should come first):\n%s", body)
}
// Save an edit.
if rec := postForm(h, "/drafts/"+ids["x"]+"/save", url.Values{"content": {"Edited X"}}); rec.Code != http.StatusSeeOther {
t.Fatalf("save: %d %s", rec.Code, rec.Body.String())
}
if d, _ := content.GetDraft(s.DB, mustID(ids["x"])); d.Content != "Edited X" {
t.Errorf("content = %q", d.Content)
}
// X isn't connected: the error is shown and the edit is still saved.
rec = postForm(h, "/drafts/"+ids["x"]+"/publish", url.Values{"content": {"Edited again"}})
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "X isn&#39;t connected") {
t.Errorf("publish without X: %d\n%s", rec.Code, rec.Body.String())
}
if d, _ := content.GetDraft(s.DB, mustID(ids["x"])); d.Content != "Edited again" || d.Status != content.StatusDraft {
t.Errorf("draft after refused publish: %+v", d)
}
// Discard.
if rec := postForm(h, "/drafts/"+ids["linkedin"]+"/discard", nil); rec.Code != http.StatusSeeOther {
t.Fatalf("discard: %d", rec.Code)
}
if rec := postForm(h, "/drafts/"+ids["linkedin"]+"/discard", nil); !strings.Contains(rec.Body.String(), "can&#39;t be changed") {
t.Errorf("discarding twice: %s", rec.Body.String())
}
if rec := postForm(h, "/drafts/999/save", url.Values{"content": {"x"}}); !strings.Contains(rec.Body.String(), "can&#39;t be changed") {
t.Errorf("unknown draft: %d %s", rec.Code, rec.Body.String())
}
}
func mustID(s string) int64 {
n, _ := strconv.ParseInt(s, 10, 64)
return n
}