Files
pages/internal/server/feeds_actions_test.go
Anya Tokugawa 131a7ca01f
Build and publish container images / images (push) Failing after 12m40s
feat(feeds): queue explicit FreshRSS state changes with uncertain-outcome recovery
2026-11-08 05:51:47 +09:00

405 lines
12 KiB
Go

package server
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"strings"
"testing"
"github.com/google/uuid"
"pageswiki/internal/store"
)
func feedActionFixture(t *testing.T) (*Server, string, string, string, store.FeedSource) {
t.Helper()
s, token, domain, actor, _ := feedFixture(t)
ctx := context.Background()
v := store.FeedSource{UUID: uuid.NewString(), Provider: "freshrss", Title: "Remote", URL: "https://example.org/api/greader.php", Enabled: true, WriteEnabled: true, Interval: 300}
v.Credential, _ = s.store.SealFeedCredential(domain, actor, v.UUID, "reader", "secret")
if _, e := s.store.SaveFeedSource(ctx, domain, actor, v); e != nil {
t.Fatal(e)
}
all, e := s.store.FeedSources(ctx, domain, actor)
if e != nil {
t.Fatal(e)
}
for _, x := range all {
if x.UUID == v.UUID {
v = x
}
}
c, e := s.store.ClaimFeed(ctx, v)
if e != nil {
t.Fatal(e)
}
if e = s.store.CompleteFeed(ctx, c, []store.FeedItem{{ID: "one", Title: "Article", Starred: true}}, "", "", "", false); e != nil {
t.Fatal(e)
}
return s, token, domain, actor, v
}
func newFeedAction(v store.FeedSource) store.FeedAction {
return store.FeedAction{RequestID: uuid.NewString(), Source: v.UUID, ID: "one", Field: "read", Desired: true, SourceVersion: v.Version}
}
type feedRemoteFake struct {
id string
read, star, timeout bool
edits, lookups, requests int
beforeEdit func()
afterEdit func()
}
func (f *feedRemoteFake) client(t *testing.T) *http.Client {
return &http.Client{Transport: feedTransportFunc(func(r *http.Request) (*http.Response, error) {
id := f.id
if id == "" {
id = "one"
}
f.requests++
body := ""
switch {
case strings.HasSuffix(r.URL.Path, "/ClientLogin"):
body = "Auth=test-auth"
case strings.HasSuffix(r.URL.Path, "/stream/items/contents"):
if r.Method != "POST" {
t.Fatal("read lookup requires POST")
}
r.ParseForm()
if r.Form.Get("i") != id {
t.Fatal("wrong item")
}
f.lookups++
cats := []string{}
if f.read {
cats = append(cats, "user/reader/state/com.google/read")
}
if f.star {
cats = append(cats, "user/reader/state/com.google/starred")
}
raw, _ := json.Marshal(map[string]any{"items": []map[string]any{{"id": id, "title": "Article", "categories": cats}}})
body = string(raw)
case strings.HasSuffix(r.URL.Path, "/token"):
body = "write-token"
if f.beforeEdit != nil {
f.beforeEdit()
}
case strings.HasSuffix(r.URL.Path, "/edit-tag"):
if r.Method != "POST" {
t.Fatal("mutation method")
}
r.ParseForm()
if len(r.Form) != 3 || r.Form.Get("T") != "write-token" || r.Form.Get("i") != id {
t.Fatal("unexpected mutation fields", r.Form)
}
if tag := r.Form.Get("a"); tag != "" {
switch tag {
case "user/-/state/com.google/read":
f.read = true
case "user/-/state/com.google/starred":
f.star = true
default:
t.Fatal("wrong tag", tag)
}
} else {
switch r.Form.Get("r") {
case "user/-/state/com.google/read":
f.read = false
case "user/-/state/com.google/starred":
f.star = false
default:
t.Fatal("wrong tag")
}
}
f.edits++
if f.afterEdit != nil {
f.afterEdit()
}
if f.timeout {
return nil, errors.New("synthetic response lost after apply")
}
body = "OK"
default:
t.Fatal("unexpected path", r.URL.Path)
}
return &http.Response{StatusCode: 200, Header: http.Header{}, Body: io.NopCloser(strings.NewReader(body))}, nil
})}
}
func TestFeedActionWritebackReceiptAndIsolation(t *testing.T) {
s, token, domain, actor, v := feedActionFixture(t)
ctx := context.Background()
a := newFeedAction(v)
w := feedRequestTest(s, token, "POST", "remote-state", a)
if w.Code != 202 {
t.Fatal(w.Code, w.Body)
}
if w = feedRequestTest(s, token, "POST", "remote-state", a); w.Code != 202 {
t.Fatal("replay", w.Code, w.Body)
}
changed := a
changed.Desired = false
if w = feedRequestTest(s, token, "POST", "remote-state", changed); w.Code != 412 {
t.Fatal("changed replay", w.Code)
}
changed.RequestID = uuid.NewString()
if w = feedRequestTest(s, token, "POST", "remote-state", changed); w.Code != 412 {
t.Fatal("parallel intent", w.Code)
}
item, _ := s.store.FeedItem(ctx, domain, actor, v.UUID, "one")
if item.Read {
t.Fatal("optimistic cache mutation")
}
f := &feedRemoteFake{star: true}
if e := s.syncFeedActions(ctx, f.client(t)); e != nil {
t.Fatal(e)
}
got, e := s.store.FeedAction(ctx, domain, actor, v.UUID, "one")
if e != nil || got.Status != "confirmed" || f.edits != 1 {
t.Fatal(got, e, f.edits)
}
item, _ = s.store.FeedItem(ctx, domain, actor, v.UUID, "one")
if !item.Read || !item.Starred {
t.Fatal("unrelated star overwritten", item)
}
if w = feedRequestTest(s, token, "POST", "remote-state", a); w.Code != 202 {
t.Fatal("receipt replay", w.Code)
}
if e = s.syncFeedActions(ctx, f.client(t)); e != nil || f.edits != 1 {
t.Fatal("replayed write", e, f.edits)
}
// A second explicit intent already satisfied remotely causes no edit-tag.
a.RequestID = uuid.NewString()
if _, e = s.store.QueueFeedAction(ctx, domain, actor, a); e != nil {
t.Fatal(e)
}
if e = s.syncFeedActions(ctx, f.client(t)); e != nil || f.edits != 1 {
t.Fatal("redundant write", e)
}
other, e := s.store.CreatePasswordAccount(domain, "actionother", "long test password", "Other", "")
if e != nil {
t.Fatal(e)
}
if got, e = s.store.FeedAction(ctx, domain, other.UUID, v.UUID, "one"); e != nil || got != nil {
t.Fatal("other owner receipt", got, e)
}
a.RequestID = uuid.NewString()
if _, e = s.store.QueueFeedAction(ctx, domain, other.UUID, a); e == nil {
t.Fatal("other owner mutation")
}
v.Credential = ""
v.WriteEnabled = false
if _, e = s.store.SaveFeedSource(ctx, domain, actor, v); e != nil {
t.Fatal(e)
}
a.SourceVersion++
if _, e = s.store.QueueFeedAction(ctx, domain, actor, a); e == nil {
t.Fatal("opt-out ignored")
}
}
func TestFeedActionUnknownNeverResendsAndReadOnlyRecheck(t *testing.T) {
s, _, domain, actor, v := feedActionFixture(t)
ctx := context.Background()
a := newFeedAction(v)
if _, e := s.store.QueueFeedAction(ctx, domain, actor, a); e != nil {
t.Fatal(e)
}
f := &feedRemoteFake{star: true, timeout: true}
client := f.client(t)
if e := s.syncFeedActions(ctx, client); e != nil {
t.Fatal(e)
}
got, e := s.store.FeedAction(ctx, domain, actor, v.UUID, "one")
if e != nil || got.Status != "unknown" {
t.Fatal(got, e)
}
item, _ := s.store.FeedItem(ctx, domain, actor, v.UUID, "one")
if item.Read {
t.Fatal("unconfirmed cache")
}
requests := f.requests
if e = s.syncFeedActions(ctx, client); e != nil || f.requests != requests {
t.Fatal("automatic retry", e)
}
if e = s.store.RecheckFeedAction(ctx, domain, actor, a.RequestID); e == nil {
t.Fatal("must wait for claim expiry")
}
// Simulate the uncertainty cooldown without sleeping.
v.Credential = ""
v.WriteEnabled = false
if _, e = s.store.SaveFeedSource(ctx, domain, actor, v); e != nil {
t.Fatal(e)
}
s.store.DB.Exec(`UPDATE feed_actions SET until_ms=0 WHERE request_id=?`, a.RequestID)
if e = s.store.RecheckFeedAction(ctx, domain, actor, a.RequestID); e != nil {
t.Fatal(e)
}
if e = s.syncFeedActions(ctx, client); e != nil {
t.Fatal(e)
}
got, e = s.store.FeedAction(ctx, domain, actor, v.UUID, "one")
if e != nil || got.Status != "confirmed" || f.edits != 1 {
t.Fatal("recheck sent a write", got, e, f.edits)
}
}
func TestFeedActionConflictAndPostSendFence(t *testing.T) {
for _, changed := range []bool{false, true} {
t.Run(map[bool]string{false: "remote_conflict", true: "source_changed"}[changed], func(t *testing.T) {
s, _, domain, actor, v := feedActionFixture(t)
ctx := context.Background()
a := newFeedAction(v)
if _, e := s.store.QueueFeedAction(ctx, domain, actor, a); e != nil {
t.Fatal(e)
}
f := &feedRemoteFake{star: true}
f.afterEdit = func() {
if changed {
v.Credential = ""
v.WriteEnabled = false
if _, e := s.store.SaveFeedSource(ctx, domain, actor, v); e != nil {
t.Fatal(e)
}
} else {
f.read = false
}
}
if e := s.syncFeedActions(ctx, f.client(t)); e != nil {
t.Fatal(e)
}
got, e := s.store.FeedAction(ctx, domain, actor, v.UUID, "one")
want := "conflict"
if changed {
want = "unknown"
}
if e != nil || got.Status != want || f.edits != 1 {
t.Fatal(got, e)
}
item, e := s.store.FeedItem(ctx, domain, actor, v.UUID, "one")
if e != nil || item.Read {
t.Fatal("unconfirmed/fenced value stored", item, e)
}
})
}
}
func TestFeedActionFencesAndRecovery(t *testing.T) {
for _, scenario := range []string{"config", "delete", "readonly", "extension", "crash"} {
t.Run(scenario, func(t *testing.T) {
s, _, domain, actor, v := feedActionFixture(t)
ctx := context.Background()
a := newFeedAction(v)
if _, e := s.store.QueueFeedAction(ctx, domain, actor, a); e != nil {
t.Fatal(e)
}
if scenario == "crash" {
due, e := s.store.DueFeedActions(ctx)
if e != nil || len(due) != 1 {
t.Fatal(e)
}
a, source, e := s.store.ClaimFeedAction(ctx, due[0])
if e != nil {
t.Fatal(e)
}
if _, e = s.store.ClaimFeed(ctx, v); !errors.Is(e, store.ErrTaskConflict) {
t.Fatal("fetch overlapped mutation", e)
}
if e = s.store.MarkFeedActionSending(ctx, a, source); e != nil {
t.Fatal(e)
}
s.store.DB.Exec(`UPDATE feed_actions SET until_ms=0 WHERE request_id=?`, a.RequestID)
due, e = s.store.DueFeedActions(ctx)
if e != nil || len(due) != 0 {
t.Fatal("crash requeued", due, e)
}
got, _ := s.store.FeedAction(ctx, domain, actor, v.UUID, "one")
if got.Status != "unknown" {
t.Fatal(got)
}
return
}
f := &feedRemoteFake{star: true}
f.beforeEdit = func() {
var e error
switch scenario {
case "config":
v.Credential = ""
v.WriteEnabled = false
_, e = s.store.SaveFeedSource(ctx, domain, actor, v)
case "delete":
e = s.store.FeedSourceAction(ctx, domain, actor, v.UUID, "delete", v.Version)
case "readonly":
e = s.store.SetPrincipalState(domain, actor, "readonly")
case "extension":
e = s.setSystemExtensionEnabled(domain, "organizer.feeds", false)
}
if e != nil {
t.Fatal(e)
}
}
if e := s.syncFeedActions(ctx, f.client(t)); e != nil {
t.Fatal(e)
}
if f.edits != 0 {
t.Fatal("sent after revocation")
}
})
}
}
func TestFeedActionSingleFieldAndConcurrentIntent(t *testing.T) {
for _, field := range []string{"read", "starred"} {
for _, desired := range []bool{false, true} {
t.Run(field+map[bool]string{false: "_remove", true: "_add"}[desired], func(t *testing.T) {
s, _, domain, actor, v := feedActionFixture(t)
ctx := context.Background()
a := newFeedAction(v)
a.Field = field
a.Desired = desired
results := make(chan error, 2)
for range 2 {
go func() {
copy := a
copy.RequestID = uuid.NewString()
_, e := s.store.QueueFeedAction(ctx, domain, actor, copy)
results <- e
}()
}
success := 0
for range 2 {
e := <-results
if e == nil {
success++
} else if !errors.Is(e, store.ErrTaskConflict) {
t.Fatal(e)
}
}
if success != 1 {
t.Fatal("concurrent intents accepted", success)
}
f := &feedRemoteFake{read: true, star: true}
if field == "read" {
f.read = !desired
} else {
f.star = !desired
}
if e := s.syncFeedActions(ctx, f.client(t)); e != nil {
t.Fatal(e)
}
if f.edits != 1 {
t.Fatal("wrong edit count", f.edits)
}
if field == "read" {
if f.read != desired || !f.star {
t.Fatal("wrong field changed")
}
} else {
if f.star != desired || !f.read {
t.Fatal("wrong field changed")
}
}
})
}
}
}