package store import ( "context" "errors" "fmt" "strings" "testing" "github.com/google/uuid" ) func periodicalBackendFixture(t *testing.T, b storeSQLBackend) (*Store, string, string) { t.Helper() f := newPublicationBackend(t, b) f.s.DocumentRoot = t.TempDir() for i := 0; i < 2; i++ { if e := f.s.EnsurePeriodicalSchema(); e != nil { t.Fatal(e) } } on := true if e := f.s.ChangePeriodical(context.Background(), f.domain, f.actor, "blog", &on, nil, "/blog"); e != nil { t.Fatal(e) } return f.s, f.domain, f.actor } func syntheticPost() PeriodicalPost { return PeriodicalPost{Title: "日本語の記事", Date: "2026-11-15", Keyword: "日記", Body: "
synthetic
", RequestID: uuid.NewString(), Timezone: "Asia/Tokyo"} } func TestPeriodicalsBackend(t *testing.T) { for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} { t.Run(fmt.Sprint(b), func(t *testing.T) { s, d, a := periodicalBackendFixture(t, b) ctx := context.Background() p := syntheticPost() if on, e := s.PeriodicalEnabled(ctx, d, "blog"); e != nil || !on { t.Fatal(on, e) } if prefix, e := s.PeriodicalPrefix(ctx, d, "blog"); e != nil || prefix != "/blog" { t.Fatal(prefix, e) } if e := s.CreatePeriodical(ctx, d, a, "blog", &p); e != nil { t.Fatal(e) } if p.Path != "/blog/2026/11/15/日記" || p.PageUUID == "" { t.Fatal(p) } replay := p if e := s.CreatePeriodical(ctx, d, a, "blog", &replay); e != nil || replay.PageUUID != p.PageUUID { t.Fatal(replay, e) } changed := p changed.Body += "different
" if e := s.CreatePeriodical(ctx, d, a, "blog", &changed); !errors.Is(e, ErrPeriodicalConflict) { t.Fatal(e) } if r, e := s.GetLatestRevision(p.PageUUID); e != nil || r.Body != p.Body { t.Fatal(r, e) } entries, e := s.ListPeriodicalEntries(ctx, d, "blog") if e != nil || len(entries) != 1 { t.Fatal(entries, e) } entry := entries[0] stale := entry entry.Title = "Changed" if e = s.ChangePeriodical(ctx, d, a, "blog", nil, &entry); e != nil { t.Fatal(e) } if e = s.ChangePeriodical(ctx, d, a, "blog", nil, &stale); !errors.Is(e, ErrPeriodicalConflict) { t.Fatal(e) } entries, e = s.ListPeriodicalEntries(ctx, d, "blog") if e != nil || entries[0].Version != 2 { t.Fatal(entries, e) } entry = entries[0] entry.Remove = true if e = s.ChangePeriodical(ctx, d, a, "blog", nil, &entry); e != nil { t.Fatal(e) } if id, e := s.ResolvePathInDomain(d, p.Path); e != nil || id != p.PageUUID { t.Fatal("unregister removed page", id, e) } entry.Remove = false entry.Version = 0 if e = s.ChangePeriodical(ctx, d, a, "blog", nil, &entry); e != nil { t.Fatal(e) } entries, e = s.ListPeriodicalEntries(ctx, d, "blog") if e != nil || entries[0].Version != 4 { t.Fatal(entries, e) } s.ReadOnly = true off := false if e = s.ChangePeriodical(ctx, d, a, "blog", &off, nil); !errors.Is(e, ErrReadOnly) { t.Fatal(e) } }) } } func TestPostgreSQLPeriodicalAtomicity(t *testing.T) { s, d, a := periodicalBackendFixture(t, storePostgreSQL) ctx := context.Background() p := syntheticPost() if _, e := s.DB.Exec(`ALTER TABLE periodical_events RENAME COLUMN operation TO unavailable_operation`); e != nil { t.Fatal(e) } if e := s.CreatePeriodical(ctx, d, a, "blog", &p); e == nil { t.Fatal("fault ignored") } var n int if e := s.DB.QueryRow(`SELECT count(*) FROM periodical_posts`).Scan(&n); e != nil || n != 0 { t.Fatal(n, e) } if e := s.DB.QueryRow(`SELECT count(*) FROM paths WHERE domain_uuid=$1 AND path=$2`, d, p.Path).Scan(&n); e != nil || n != 0 { t.Fatal(n, e) } if _, e := s.DB.Exec(`ALTER TABLE periodical_events RENAME COLUMN unavailable_operation TO operation`); e != nil { t.Fatal(e) } peer := &Store{DB: s.DB, sqlBackend: storePostgreSQL, DocumentRoot: s.DocumentRoot} ch := make(chan error, 2) for _, w := range []*Store{s, peer} { go func(w *Store) { local := p; ch <- w.CreatePeriodical(ctx, d, a, "blog", &local) }(w) } for i := 0; i < 2; i++ { if e := <-ch; e != nil { t.Fatal(e) } } if e := s.DB.QueryRow(`SELECT count(*) FROM periodical_posts`).Scan(&n); e != nil || n != 1 { t.Fatal(n, e) } if _, e := s.DB.Exec(`UPDATE campuses SET enabled=0`); e != nil { t.Fatal(e) } other := syntheticPost() other.Keyword = "denied" if e := s.CreatePeriodical(ctx, d, a, "blog", &other); !errors.Is(e, ErrAccessDenied) { t.Fatal(e) } } func TestPeriodicalsBackendAssetsAndProjection(t *testing.T) { for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} { t.Run(fmt.Sprint(b), func(t *testing.T) { s, domain, actor := periodicalBackendFixture(t, b) ctx := context.Background() if err := s.EnsureAssetsSchema(); err != nil { t.Fatal(err) } asset, _, err := s.PutAsset(domain, "asset:image:cover", []byte("synthetic cover"), "image/png", "cover.png", "", actor) if err != nil { t.Fatal(err) } p := syntheticPost() p.Body = `