| index ee09879..c9a6299 100644 |
| --- a/internal/docs/suite_test.go |
| +++ b/internal/docs/suite_test.go |
| @@ -9,6 +9,7 @@ import ( |
| "io/fs" |
| "net/http" |
| "net/http/httptest" |
| + "net/url" |
| "os" |
| "path/filepath" |
| "sort" |
| @@ -45,18 +46,21 @@ func suiteVector(t *testing.T, id string) []byte { |
| return b |
| } |
| |
| -// setRevision puts a document at the fixture's revision, with the revision |
| -// before it in its history: both are announced, as two bumps would be. The |
| -// fixture numbers its revisions 2 to 11 and real bumps are random |
| +// setRevision puts a document at the fixture's revision, with the case's |
| +// prior revision before it in its history: both are announced, as two bumps |
| +// would be. The fixture names its revisions and real bumps are random |
| // (newRevision), so the world is seeded at the fixture's numbers directly; |
| // the producer reads documents.revision and suite_feed and does not care how |
| -// they got there. revision - 1 being in the log is what makes the harness's |
| -// "one behind" STALE (Docs decides STALE from its log), and revision + 1 |
| -// never being announced is what makes "one ahead" UNKNOWN. |
| -func setRevision(t *testing.T, s *Store, docID, revision int64) { |
| +// they got there. prior being in the log is what makes the harness's |
| +// prior_revision STALE (Docs decides STALE from its log), and the case's |
| +// unknown_revision never being announced is what makes it UNKNOWN. Suite |
| +// revision 11 (91affa1) names prior_revision in the fixture rather than |
| +// letting the harness compute "one behind"; it is revision - 1 in every case |
| +// today, and is read from the fixture here all the same. |
| +func setRevision(t *testing.T, s *Store, docID, prior, revision int64) { |
| t.Helper() |
| err := s.db.Transaction(func(tx *gorm.DB) error { |
| - for _, r := range []int64{revision - 1, revision} { |
| + for _, r := range []int64{prior, revision} { |
| if err := tx.Model(&Document{}).Where("id = ?", docID).UpdateColumn("revision", r).Error; err != nil { |
| return err |
| } |
| @@ -91,11 +95,20 @@ type custodyParty struct { |
| // seedCustody builds the custody fixture's world in a Docs store, through |
| // Docs' own store paths: each case's rung, its owner (the asker or the other |
| // party), a grant to the asker or to a guest, a published link, a deletion, |
| -// and the case's revision (setRevision). The fixture's ids are not |
| +// and the case's revisions (setRevision). The fixture's ids are not |
| // sqids; each document's sqid is set to the fixture id straight after it is |
| // created (the column is a plain unique TEXT), so the producer under test is |
| // the real one with no id-mapping in between. Documents are untitled, so S3's |
| // body is the text alone and equals the fixture's s3_body exactly. |
| +// |
| +// MUTATION-TESTED 2026-11-02, each reverted: the "other" owner arm renamed |
| +// so the switch no longer knows it → both conformance tests fatal on |
| +// "custody case doc-team: owner "other" is not one this seeding knows"; |
| +// setRevision seeding revision + 1000 in place of the fixture's |
| +// prior_revision → both conformance tests "at a prior revision the log |
| +// holds: got UNKNOWN, want STALE" for the four cases the asker may open. |
| +// (A first try seeded revision - 2 and went red on the column's CHECK, not |
| +// on the rule, so it proved nothing and was redone.) |
| func seedCustody(t *testing.T, s *Store, asker, other custodyParty) { |
| t.Helper() |
| ctx := context.Background() |
| @@ -104,9 +117,20 @@ func seedCustody(t *testing.T, s *Store, asker, other custodyParty) { |
| t.Fatal(err) |
| } |
| for _, c := range f.Cases { |
| - owner := other |
| - if c.Owner == "asker" { |
| + // "asker" and "other" are the only owners a binding case names. |
| + // Revision 11 briefly added "stranger" (doc-owner-left: an owner |
| + // who has lost team standing), withdrawn at 91affa1 until Paul rules |
| + // (suite §14); Docs' Rule keeps gate 5's reading meanwhile. A case |
| + // naming any other owner is refused here rather than seeded as |
| + // owned by "other", which would pass it for the wrong reason. |
| + var owner custodyParty |
| + switch c.Owner { |
| + case "asker": |
| owner = asker |
| + case "other": |
| + owner = other |
| + default: |
| + t.Fatalf("custody case %s: owner %q is not one this seeding knows (asker, other)", c.ItemID, c.Owner) |
| } |
| vis := VisibilityPrivate |
| if c.Rung == suite.RungTeam || c.ItemID == "doc-was-team" { |
| @@ -155,12 +179,12 @@ func seedCustody(t *testing.T, s *Store, asker, other custodyParty) { |
| // Deleted through the real path; its last revision is a |
| // random one, which nothing asks about: a deleted item is |
| // NOT_VISIBLE and refused at every revision. |
| - setRevision(t, s, doc.ID, c.Revision-1) |
| + setRevision(t, s, doc.ID, c.PriorRevision, c.Revision) |
| if err := s.deleteDocument(ctx, doc.ID); err != nil { |
| t.Fatal(err) |
| } |
| } else { |
| - setRevision(t, s, doc.ID, c.Revision) |
| + setRevision(t, s, doc.ID, c.PriorRevision, c.Revision) |
| } |
| } |
| } |
| @@ -193,6 +217,20 @@ func loneCustodyApp(t *testing.T) *App { |
| // addresses here, and the harness also asks under person_ref, which must be |
| // UNKNOWN for every pair. |
| // |
| +// Run through conformance.RunProducer (suite revision 11, 91affa1): the |
| +// harness mounts the producer behind server.Handler with a token it mints, |
| +// requires the 401 without it, and audits Cache-Control: no-store and Vary: |
| +// Authorization on every response. It replaces conformance.RunAs over |
| +// server.Routes, the unauthenticated routes the module has deprecated. It |
| +// covers Handler and the producer only; what Docs mounts in front of |
| +// Handler is TestSuiteS1ResetIsNotAFailure's and |
| +// TestSuiteRoutesAreMountedExactlyOutsideCSRF's. |
| +// |
| +// MUTATION-TESTED 2026-11-02, reverted: Feed answering an unparseable |
| +// cursor with a plain error (revision 10's 503) → "S1 with a cursor the log |
| +// never issued: 503, want 409 {"code":"CURSOR_RESET"}" here and in team |
| +// mode. |
| +// |
| // MUTATION-TESTED 2026-11-01, each reverted: the link table read as a grant |
| // in permissionScope (an EXISTS on links) → doc-link, doc-link-added-late |
| // VISIBLE; teamReader returning true → the stranger VISIBLE on doc-team; |
| @@ -207,7 +245,7 @@ func loneCustodyApp(t *testing.T) *App { |
| // the test blind, and was replaced by the one above.) |
| func TestSuiteConformanceLoneShape(t *testing.T) { |
| app := loneCustodyApp(t) |
| - conformance.RunAs(t, server.Routes(app.suite, nil), suite.SubjectAddress) |
| + conformance.RunProducer(t, app.suite, suite.SubjectAddress) |
| } |
| |
| // renamedAskers maps the fixture's person refs onto roster-shaped subjects |
| @@ -269,8 +307,7 @@ func teamCustodyStore(t *testing.T, members ...string) (*App, *suiteProducer, *t |
| // stranger answer UNKNOWN (a 503 for the whole call). |
| func TestSuiteConformanceTeamMode(t *testing.T) { |
| _, p, _, _, _ := teamCustodyStore(t) |
| - h := server.Routes(renamedAskers{Producer: p, names: map[string]string{"pers_ana": anaSubject, "pers_bo": strangerSubject}}, nil) |
| - conformance.Run(t, h) |
| + conformance.RunProducer(t, renamedAskers{Producer: p, names: map[string]string{"pers_ana": anaSubject, "pers_bo": strangerSubject}}, suite.SubjectPersonRef) |
| } |
| |
| // The two halves of Paul's 2026-11-01 ruling, "New docs Team, old private": |
| @@ -519,6 +556,13 @@ func TestSuiteTeamRungTeamMode(t *testing.T) { |
| // gone=false → "(gone)"; Feed returning "" at an empty tip → "empty tip"; |
| // Feed ignoring the cursor → "the cursor came back unchanged with rows"; |
| // Feed not checking the stamp → cursor "2.1" answered a page. |
| +// |
| +// MUTATION-TESTED 2026-11-02 (suite revision 11, the cursor reset), each |
| +// reverted: Feed answering a stamp mismatch or a missing row with a plain |
| +// error (revision 10's 503) → "2.1", "999.<stamp>" and the conformance |
| +// fixture's never-issued cursor "want server.ErrCursorReset"; Feed |
| +// answering an unparseable cursor with a plain error → "not-a-cursor", "-1" |
| +// and the four respellings, same message. |
| func TestSuiteFeed(t *testing.T) { |
| app, err := NewForTest(t) |
| if err != nil { |
| @@ -613,13 +657,14 @@ func TestSuiteFeed(t *testing.T) { |
| if tip, _ := p.Feed(ctx, cursor, 2); tip.Cursor != cursor || len(tip.Rows) != 0 { |
| t.Errorf("tip: %+v, want cursor %q and no rows", tip, cursor) |
| } |
| - // A cursor Docs did not issue is an error (the suite package's 503), |
| - // never a page: not a bare id, not a stamp that does not match the row, |
| - // not another spelling of a real one. |
| + // A cursor Docs did not issue is the cursor reset (suite revision 11 |
| + // §6.6; the suite package's 409 CURSOR_RESET), never a page and never |
| + // another error: not a bare id, not a stamp that does not match the |
| + // row, not another spelling of a real one, not a row past the log's end. |
| id, stamp, _ := strings.Cut(cursors[0], ".") |
| - for _, bad := range []string{"not-a-cursor", id, id + ".1", "0" + cursors[0], id + "." + stamp + "0", "-1", "+" + cursors[0]} { |
| - if f, err := p.Feed(ctx, bad, 100); err == nil { |
| - t.Errorf("cursor %q: a page of %d rows, want an error", bad, len(f.Rows)) |
| + for _, bad := range []string{"not-a-cursor", id, id + ".1", "0" + cursors[0], id + "." + stamp + "0", "-1", "+" + cursors[0], "999." + stamp, suite.NeverIssuedCursor} { |
| + if f, err := p.Feed(ctx, bad, 100); !errors.Is(err, server.ErrCursorReset) { |
| + t.Errorf("cursor %q: %d rows, error %v; want server.ErrCursorReset", bad, len(f.Rows), err) |
| } |
| } |
| if f, err := p.Feed(ctx, cursors[0], 100); err != nil || len(f.Rows) != len(want)-2 { |
| @@ -919,6 +964,116 @@ func TestSuiteRoutesAreMountedExactlyOutsideCSRF(t *testing.T) { |
| } |
| } |
| |
| +// S1 over the wire as memoria meets it — the app's own mux and rastrillo's |
| +// production stack in front of it, as cmd/docs serves — for the two answers |
| +// suite revision 11 (§6.6, 91affa1) keeps apart. A cursor this log does not |
| +// recognise is the fixed 409 {"code":"CURSOR_RESET"}, the one way a |
| +// consumer is told to set aside what it holds from Docs and read from "". |
| +// A log that cannot be read is a 503, because it has lost nothing and a |
| +// reset would destroy the consumer's whole copy. Both carry Cache-Control: |
| +// no-store and Vary: Authorization through every layer Docs puts in front |
| +// of server.Handler (conformance.RunProducer drives Handler alone and cannot |
| +// see those layers, doctrine §6.5). |
| +// |
| +// MUTATION-TESTED 2026-11-02, each reverted: Feed answering a failed stamp |
| +// read with server.ErrCursorReset (a database failure read as a reset) → |
| +// "a log that cannot be read, from a cursor it issued: 409, want 503"; |
| +// Feed answering a stamp mismatch with a plain error (revision 10) → "a |
| +// stamp this log never wrote: 503"; Feed answering an unparseable cursor |
| +// with a plain error → "a cursor that does not parse: 503"; mountSuite |
| +// wrapping Handler in a layer whose ResponseWriter sets Cache-Control: |
| +// private at WriteHeader → every "Cache-Control" line, on both layers. (A |
| +// first try set the header before calling Handler and stayed green: |
| +// Handler sets no-store over it, so that layer was harmless, not the test |
| +// blind.) |
| +func TestSuiteS1ResetIsNotAFailure(t *testing.T) { |
| + app, err := NewForTestWith(t, Config{SuiteConsumers: suiteConsumersJSON()}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + ctx := context.Background() |
| + alice := app.TestPerson(t, "alice@example.com") |
| + if _, err := app.store.Create(ctx, alice, "One"); err != nil { |
| + t.Fatal(err) |
| + } |
| + served, done, err := rastrillo.Handler(ServeOptions(rastrillo.Options{}, app)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if done != nil { |
| + defer done() |
| + } |
| + layers := map[string]http.Handler{"the app's mux": app.Handler(), "rastrillo's production stack": served} |
| + s1 := func(h http.Handler, cursor string) *httptest.ResponseRecorder { |
| + return httptestServe(h, suiteRequest("GET", suite.RouteS1+"?cursor="+url.QueryEscape(cursor), suiteTestToken, nil)) |
| + } |
| + headers := func(t *testing.T, what string, rec *httptest.ResponseRecorder) { |
| + t.Helper() |
| + if got := rec.Header().Get("Cache-Control"); got != "no-store" { |
| + t.Errorf("%s: Cache-Control %q, want no-store", what, got) |
| + } |
| + if got := rec.Header().Values("Vary"); len(got) != 1 || got[0] != "Authorization" { |
| + t.Errorf("%s: Vary %q, want Authorization", what, got) |
| + } |
| + } |
| + first := s1(app.Handler(), "") |
| + feed, err := suite.DecodeS1(first.Code, first.Body.Bytes()) |
| + if err != nil || len(feed.Rows) == 0 { |
| + t.Fatalf("S1 from the beginning: %d %v", first.Code, err) |
| + } |
| + issued := feed.Cursor |
| + id, stamp, _ := strings.Cut(issued, ".") |
| + wrongStamp := "1" |
| + if stamp == wrongStamp { |
| + wrongStamp = "2" |
| + } |
| + |
| + for name, h := range layers { |
| + t.Run(name, func(t *testing.T) { |
| + for what, cursor := range map[string]string{ |
| + "a stamp this log never wrote": id + "." + wrongStamp, |
| + "a row this log does not hold": "999999." + stamp, |
| + "a cursor that does not parse": "not-a-cursor", |
| + "the fixture's never-issued one": suite.NeverIssuedCursor, |
| + } { |
| + rec := s1(h, cursor) |
| + if rec.Code != suite.CursorResetStatus || rec.Body.String() != suite.CursorResetBody { |
| + t.Errorf("%s: %d %q, want %d %s", what, rec.Code, rec.Body.String(), suite.CursorResetStatus, suite.CursorResetBody) |
| + } |
| + if _, err := suite.DecodeS1(rec.Code, rec.Body.Bytes()); !errors.Is(err, suite.ErrCursorReset) { |
| + t.Errorf("%s: memoria's decoder reads %v, want suite.ErrCursorReset", what, err) |
| + } |
| + headers(t, what, rec) |
| + } |
| + if rec := s1(h, issued); rec.Code != http.StatusOK { |
| + t.Errorf("a cursor Docs issued: %d, want 200", rec.Code) |
| + } else { |
| + headers(t, "a cursor Docs issued", rec) |
| + } |
| + }) |
| + } |
| + |
| + // The log cannot be read. The table is moved aside, not dropped, so |
| + // nothing about the log's content changes: only whether it can be read. |
| + if err := app.store.db.Exec("ALTER TABLE suite_feed RENAME TO suite_feed_away").Error; err != nil { |
| + t.Fatal(err) |
| + } |
| + for name, h := range layers { |
| + t.Run(name+", log unreadable", func(t *testing.T) { |
| + for what, cursor := range map[string]string{ |
| + "a log that cannot be read, from a cursor it issued": issued, |
| + "a log that cannot be read, from the beginning": "", |
| + } { |
| + rec := s1(h, cursor) |
| + if rec.Code != http.StatusServiceUnavailable { |
| + t.Errorf("%s: %d %q, want 503", what, rec.Code, rec.Body.String()) |
| + } |
| + headers(t, what, rec) |
| + } |
| + }) |
| + } |
| +} |
| + |
| func httptestServe(h http.Handler, r *http.Request) *httptest.ResponseRecorder { |
| rec := httptest.NewRecorder() |
| h.ServeHTTP(rec, r) |
| @@ -954,7 +1109,10 @@ func TestSuiteConsumersMalformedRefusesBoot(t *testing.T) { |
| // (newRevision and randomPositive reading suiteClock) → "clock set back" |
| // VISIBLE and its old cursor answered a page, while "real clock" stayed |
| // green, which is why both subtests exist; Feed without the nonce check → |
| -// both subtests' old cursor answered a page. |
| +// both subtests' old cursor answered a page. MUTATION-TESTED 2026-11-02 |
| +// (suite revision 11 §6.6), reverted: Feed answering the stamp mismatch with |
| +// a plain error, revision 10's 503 → both subtests "want |
| +// server.ErrCursorReset". |
| func TestSuiteRestoreNeverReissuesARevisionOrACursor(t *testing.T) { |
| for _, tc := range []struct { |
| name string |
| @@ -1023,8 +1181,8 @@ func TestSuiteRestoreNeverReissuesARevisionOrACursor(t *testing.T) { |
| if current := revisionOf(t, restored.store, doc.ID); current == held { |
| t.Errorf("the restore reissued revision %d", held) |
| } |
| - if f, err := restored.suite.Feed(ctx, cursor, 100); err == nil { |
| - t.Errorf("the cursor from before the restore (%q) answered a page: cursor %q, %d rows; want an error", cursor, f.Cursor, len(f.Rows)) |
| + if f, err := restored.suite.Feed(ctx, cursor, 100); !errors.Is(err, server.ErrCursorReset) { |
| + t.Errorf("the cursor from before the restore (%q): cursor %q, %d rows, error %v; want server.ErrCursorReset (suite revision 11 §6.6)", cursor, f.Cursor, len(f.Rows), err) |
| } |
| }) |
| } |
| @@ -1340,6 +1498,12 @@ func (brokenRandom) Read([]byte) (int, error) { return 0, errors.New("no entropy |
| // 0; newRevision returning its first draw → "current, then the next" gets |
| // 5; randomPositive answering a failed read with a time-based value instead |
| // of an error → "a broken source" commits a document. |
| +// |
| +// newRevision now takes what is taken rather than the current revision |
| +// (suite revision 11 §6.6, 91affa1: unique against the whole log; the log |
| +// half is TestSuiteARevisionIsNeverOneTheLogHolds). MUTATION-TESTED |
| +// 2026-11-02, reverted: newRevision ignoring taken's error → "a log that |
| +// cannot be read: a revision was drawn anyway". |
| func TestSuiteRevisionsRedrawAndNeverFallBack(t *testing.T) { |
| t.Cleanup(func() { randomSource = crand.Reader }) |
| |
| @@ -1351,13 +1515,27 @@ func TestSuiteRevisionsRedrawAndNeverFallBack(t *testing.T) { |
| if n, err := randomPositive(); err != nil || n != 11 { |
| t.Errorf("the sign bit alone (zero once cleared), then 11: %d %v", n, err) |
| } |
| + only := func(held ...int64) func(int64) (bool, error) { |
| + return func(r int64) (bool, error) { |
| + for _, h := range held { |
| + if r == h { |
| + return true, nil |
| + } |
| + } |
| + return false, nil |
| + } |
| + } |
| randomSource = draws(5, 9) |
| - if n, err := newRevision(5); err != nil || n != 9 { |
| + if n, err := newRevision(only(5)); err != nil || n != 9 { |
| t.Errorf("current, then the next: %d %v", n, err) |
| } |
| randomSource = draws(5) |
| - if n, err := newRevision(4); err != nil || n != 5 { |
| - t.Errorf("a draw that is not current is kept: %d %v", n, err) |
| + if n, err := newRevision(only(4)); err != nil || n != 5 { |
| + t.Errorf("a draw that is not taken is kept: %d %v", n, err) |
| + } |
| + randomSource = draws(5) |
| + if _, err := newRevision(func(int64) (bool, error) { return false, errors.New("log unreadable") }); err == nil { |
| + t.Error("a log that cannot be read: a revision was drawn anyway") |
| } |
| |
| app, err := NewForTest(t) |
| @@ -1390,6 +1568,78 @@ func TestSuiteRevisionsRedrawAndNeverFallBack(t *testing.T) { |
| } |
| } |
| |
| +// A revision is never one the document's log holds (suite revision 11 §6.6, |
| +// 91affa1, answering Fable's review of 0a59e7d, L4): a draw equal to ANY |
| +// earlier revision of the document is drawn again, not only a draw equal to |
| +// the current one. A draw equal to a prior revision would give an old name |
| +// to new bytes, and a consumer still holding the old copy would be answered |
| +// VISIBLE over text the document no longer has. The draws are forced: the |
| +// document's first revision, its second, its current one, then a fresh |
| +// value, which is the one the rename must take. |
| +// |
| +// MUTATION-TESTED 2026-11-02, each reverted: logRevisionTaken checking only |
| +// the current revision (the revision-10 rule) → "the rename took revision |
| +// <first>, one the log already held"; 0012 deleted → "the log probe's plan |
| +// is [{Detail:SCAN suite_feed}]"; logRevisionTaken returning false, |
| +// nil on a failed read → "a log that cannot be read: taken false and no |
| +// error". |
| +func TestSuiteARevisionIsNeverOneTheLogHolds(t *testing.T) { |
| + t.Cleanup(func() { randomSource = crand.Reader }) |
| + app, err := NewForTest(t) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + ctx := context.Background() |
| + alice := app.TestPerson(t, "alice@example.com") |
| + actor := app.TestActorForPerson(t, alice) |
| + doc, err := app.store.Create(ctx, alice, "One") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + history := []int64{revisionOf(t, app.store, doc.ID)} |
| + for _, title := range []string{"One, again", "One, once more"} { |
| + if err := app.store.Rename(ctx, actor, doc, title); err != nil { |
| + t.Fatal(err) |
| + } |
| + history = append(history, revisionOf(t, app.store, doc.ID)) |
| + } |
| + const fresh, nonce = 42, 4242 |
| + for _, h := range history { |
| + if h == fresh { |
| + t.Skip("a random revision happened to be the fresh value") |
| + } |
| + } |
| + // Bump: history[0], history[1], history[2] (current), then fresh; then |
| + // announce's nonce. |
| + randomSource = draws(uint64(history[0]), uint64(history[1]), uint64(history[2]), fresh, nonce) |
| + if err := app.store.Rename(ctx, actor, doc, "Different text"); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := revisionOf(t, app.store, doc.ID); got != fresh { |
| + t.Errorf("the rename took revision %d, one the log already held (history %v); want the fresh draw %d", got, history, fresh) |
| + } |
| + |
| + // The probe is one index lookup (0012), not a scan of a log that grows |
| + // with every applied update. |
| + var plan []struct{ Detail string } |
| + if err := app.store.db.Raw("EXPLAIN QUERY PLAN SELECT revision FROM suite_feed WHERE document_id = ? AND revision = ? LIMIT 1", doc.ID, fresh).Scan(&plan).Error; err != nil { |
| + t.Fatal(err) |
| + } |
| + if len(plan) != 1 || !strings.Contains(plan[0].Detail, "suite_feed_document_revision") { |
| + t.Errorf("the log probe's plan is %+v, want a search on suite_feed_document_revision", plan) |
| + } |
| + |
| + // A log that cannot be read is an error, never "not taken". Asked of |
| + // the probe directly: through a bump, announce's insert into the same |
| + // missing table would fail anyway and hide whether the probe did. |
| + if err := app.store.db.Exec("ALTER TABLE suite_feed RENAME TO suite_feed_away").Error; err != nil { |
| + t.Fatal(err) |
| + } |
| + if taken, err := logRevisionTaken(app.store.db, doc.ID, fresh)(7); err == nil { |
| + t.Errorf("a log that cannot be read: taken %v and no error", taken) |
| + } |
| +} |
| + |
| // An orphan — a document 0002_owners.sql left at owner_id 0, readable by |
| // nobody — is not live, even if a feed row names it. Nothing in Docs writes |
| // one (announce runs only for documents with an owner), so the row is made |