| 1 | package fichas |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "slices" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/suite" |
| 10 | "amadan.net/oficina/suite/conformance" |
| 11 | "amadan.net/oficina/suite/server" |
| 12 | ) |
| 13 | |
| 14 | // fixtureNames lets the suite's custody fixture address a real Fichas |
| 15 | // world. It RENAMES and decides nothing: the fixture's item ids become the |
| 16 | // card ids seeded for them, and back again in feed rows (spec §6.7 — a |
| 17 | // fixture id is a name, not an id Fichas could allocate). The askers need |
| 18 | // no renaming: conformance.CheckProducer sends them as addresses. Every answer is |
| 19 | // the real producer's, over a real database. |
| 20 | type fixtureNames struct { |
| 21 | inner server.Producer |
| 22 | ids map[string]string |
| 23 | } |
| 24 | |
| 25 | // Feed renames each row's card id back to the fixture id it was seeded |
| 26 | // for, so the harness can find its items in the feed. Rows for cards the |
| 27 | // fixture did not seed pass through as themselves. |
| 28 | func (n fixtureNames) Feed(ctx context.Context, cursor string, limit int) (suite.Feed, error) { |
| 29 | f, err := n.inner.Feed(ctx, cursor, limit) |
| 30 | for i, row := range f.Rows { |
| 31 | for fixtureID, cardID := range n.ids { |
| 32 | if row.ItemID == cardID { |
| 33 | f.Rows[i].ItemID = fixtureID |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | return f, err |
| 38 | } |
| 39 | |
| 40 | func (n fixtureNames) Rule(ctx context.Context, asker suite.Subject, pairs []suite.ItemRevision) ([]suite.Authorization, error) { |
| 41 | renamed := make([]suite.ItemRevision, len(pairs)) |
| 42 | for i, pr := range pairs { |
| 43 | renamed[i] = suite.ItemRevision{ItemID: n.name(pr.ItemID), Revision: pr.Revision} |
| 44 | } |
| 45 | return n.inner.Rule(ctx, asker, renamed) |
| 46 | } |
| 47 | |
| 48 | func (n fixtureNames) Content(ctx context.Context, itemID string, revision int64) ([]byte, error) { |
| 49 | return n.inner.Content(ctx, n.name(itemID), revision) |
| 50 | } |
| 51 | |
| 52 | // name maps a fixture id to its card, and passes anything else through |
| 53 | // untouched — so the harness's never-existed id reaches Fichas as itself. |
| 54 | func (n fixtureNames) name(id string) string { |
| 55 | if c, ok := n.ids[id]; ok { |
| 56 | return c |
| 57 | } |
| 58 | return id |
| 59 | } |
| 60 | |
| 61 | // The suite's own conformance harness — conformance.CheckProducer, revision |
| 62 | // 11's entry point for an app built on suite/server — run as an |
| 63 | // address-keyed app (suite.SubjectAddress) against Fichas' real producer |
| 64 | // over a world seeded from the custody fixture. CheckProducer mounts the |
| 65 | // producer through server.Handler behind a token it mints, exactly as |
| 66 | // app.go mounts it, and requires the 401 without it; nothing here calls |
| 67 | // the deprecated, unauthenticated server.Routes. It covers Handler and the |
| 68 | // producer only: the middleware in front of Handler in production |
| 69 | // (rastrillo's recovery and security headers, and app.go's outer mux) is |
| 70 | // fichastest/suite_test.go's to test (doctrine §6.5). |
| 71 | // |
| 72 | // The world: one Customer card per case, named with the case's s3_body (so |
| 73 | // S3's exact-body check is a real check of the text shape: a card with |
| 74 | // only a title is exactly its title), the deleted case deleted, and each |
| 75 | // card's random revisions then set to the case's prior_revision and |
| 76 | // revision (see the seeding below); its unknown_revision is never issued. |
| 77 | // The harness also asks under person_ref, a kind Fichas does not key on, |
| 78 | // and requires UNKNOWN; asks S2 about an id Fichas does not know; follows |
| 79 | // S1 to its tip and asks it from a cursor no log issued (the reset, 409); |
| 80 | // refuses S3 at both other revisions, with no revision, a malformed one, 0 |
| 81 | // and on POST; and holds every response to no-store and Vary: |
| 82 | // Authorization. |
| 83 | // |
| 84 | // The fixture describes a sharing ladder Fichas does not have (spec §6.7): |
| 85 | // every admitted person sees every card. So four cases — guest-only, |
| 86 | // link-shared private, moved to private, link added late — expect |
| 87 | // NOT_VISIBLE for the fixture's asker where Fichas, correctly for Fichas, |
| 88 | // answers VISIBLE at the current revision, STALE at the prior revision and |
| 89 | // UNKNOWN at the never-issued one (a member may open the card, so a |
| 90 | // revision the log never issued is UNKNOWN for them, §4.3). This test |
| 91 | // asserts the failures are EXACTLY those twelve, in the harness's own words |
| 92 | // at 91affa1: any other failure, or one of these passing, is red. And it |
| 93 | // pins the fixture's nine case ids, so a case added or withdrawn on the |
| 94 | // suite side (doc-owner-left was withdrawn at 91affa1 pending Paul's |
| 95 | // ruling; were it restored, Fichas could not express it either: a private |
| 96 | // card owned by someone no longer admitted is still every member's) is |
| 97 | // noticed here and the seeding re-read, rather than absorbed. |
| 98 | // |
| 99 | // MUTATION-TESTED 2026-11-01 against 983114a, through CheckAs over |
| 100 | // server.Routes, by how many failures the harness then reported (12 |
| 101 | // expected): both of Content's revision checks removed → 28; a trailing |
| 102 | // newline on the text → 20; admission skipped → 36; the person_ref branch |
| 103 | // removed → 21; gone cards answered in Rule → 15; the title repeated as a |
| 104 | // "Name:" line → 20; feed rows never gone → 13; feed rows at revision 1 → |
| 105 | // 20; a revision ahead answered STALE → 16. |
| 106 | // |
| 107 | // MUTATION-TESTED 2026-11-02 against 91affa1, through CheckProducer, each |
| 108 | // reverted, same measure (12 expected): both of Content's revision checks |
| 109 | // removed → 28; a trailing newline on the text → 20; admission skipped → |
| 110 | // 36; the person_ref branch removed → 21; gone cards answered in Rule → |
| 111 | // 15; the title repeated as a "Name:" line → 20; feed rows never gone → |
| 112 | // 13; feed rows at revision 1 → 20; every revision that is not current |
| 113 | // answered STALE → 16; STALE read off the numbers (1 <= asked < current) |
| 114 | // instead of the log → 15 (the three openable cases whose unknown_revision |
| 115 | // sits below the current one; revision 10's harness could not see this); |
| 116 | // an id Rule cannot find answered VISIBLE (failing open) → 16; an |
| 117 | // unparseable cursor answered with a plain error, revision 10's 503, |
| 118 | // instead of the reset → 13. |
| 119 | func TestSuiteConformanceFailsOnlyWhereFichasCannotExpress(t *testing.T) { |
| 120 | f, err := suite.LoadCustodyFixture() |
| 121 | if err != nil { |
| 122 | t.Fatal(err) |
| 123 | } |
| 124 | var names []string |
| 125 | for _, c := range f.Cases { |
| 126 | names = append(names, c.ItemID) |
| 127 | } |
| 128 | if want := []string{"doc-team", "doc-private", "doc-granted", "doc-guest", "doc-link", |
| 129 | "doc-team-link", "doc-deleted", "doc-was-team", "doc-link-added-late"}; !slices.Equal(names, want) { |
| 130 | t.Fatalf("the custody fixture's cases changed (spec §6.7 and the seeding below were written for nine):\n got %v\nwant %v", names, want) |
| 131 | } |
| 132 | |
| 133 | g, a, p := suiteWorld(t) |
| 134 | // The real, random revisions (suiteWorld pins a count for readability). |
| 135 | pinRevisions(t, randomRevision) |
| 136 | |
| 137 | ids := map[string]string{} |
| 138 | for _, c := range f.Cases { |
| 139 | gone := c.Rung == suite.RungDeleted |
| 140 | // Two real changes, so the card has a history: created, then |
| 141 | // renamed to its s3_body (or deleted). Random revisions cannot be |
| 142 | // edited into the fixture's names, so they are then set directly, |
| 143 | // the card and its two events together: the first event at the |
| 144 | // case's prior_revision and the last at its revision — the |
| 145 | // fixture's own names, never computed (doctrine §6.6) — so the |
| 146 | // prior revision is in this card's log (STALE) and the unknown one |
| 147 | // is not (UNKNOWN). |
| 148 | if c.PriorRevision == c.Revision || c.UnknownRevision == c.Revision || c.UnknownRevision == c.PriorRevision { |
| 149 | t.Fatalf("seeding %s: the fixture's revisions are not three different names", c.ItemID) |
| 150 | } |
| 151 | card := mustCard(t, g, a, map[string]string{FieldName: "draft"}) |
| 152 | if gone { |
| 153 | if err := DeleteCard(ctx(), g, a.sealer, card.ID, Person(1)); err != nil { |
| 154 | t.Fatal(err) |
| 155 | } |
| 156 | } else if err := UpdateCard(ctx(), g, a.sealer, card.ID, Person(1), map[string]string{FieldName: c.S3Body}); err != nil { |
| 157 | t.Fatal(err) |
| 158 | } |
| 159 | evs := events(t, g, ObjectCard, card.ID) |
| 160 | if len(evs) != 2 { |
| 161 | t.Fatalf("seeding %s: %d card events, want 2", c.ItemID, len(evs)) |
| 162 | } |
| 163 | for _, set := range []struct { |
| 164 | table string |
| 165 | id int64 |
| 166 | rev int64 |
| 167 | }{{"events", evs[0].ID, c.PriorRevision}, {"events", evs[1].ID, c.Revision}, {"cards", card.ID, c.Revision}} { |
| 168 | if err := g.Exec("UPDATE "+set.table+" SET revision = ? WHERE id = ?", set.rev, set.id).Error; err != nil { |
| 169 | t.Fatal(err) |
| 170 | } |
| 171 | } |
| 172 | var now Card |
| 173 | if err := g.First(&now, card.ID).Error; err != nil || now.Revision != c.Revision || now.Gone != gone { |
| 174 | t.Fatalf("seeding %s: card is %+v (%v), want revision %d gone %v", c.ItemID, now, err, c.Revision, gone) |
| 175 | } |
| 176 | ids[c.ItemID] = id(now) |
| 177 | } |
| 178 | |
| 179 | // CheckProducer sends the fixture's asker and stranger as addresses |
| 180 | // with their values unchanged: pers_ana is put on the owners list, as |
| 181 | // FICHAS_OWNERS would, and pers_bo is not. |
| 182 | a.owners = ParseOwners(f.Asker.Value) |
| 183 | report := conformance.CheckProducer(fixtureNames{inner: p, ids: ids}, suite.SubjectAddress) |
| 184 | if report.Checked == 0 { |
| 185 | t.Fatal("conformance checked nothing") |
| 186 | } |
| 187 | |
| 188 | var want []string |
| 189 | for _, item := range []string{"doc-guest", "doc-link", "doc-was-team", "doc-link-added-late"} { |
| 190 | want = append(want, |
| 191 | fmt.Sprintf("S2 %s asked for the fixture's asker, at the current revision: got VISIBLE, want NOT_VISIBLE", item), |
| 192 | fmt.Sprintf("S2 %s asked for the fixture's asker, at a prior revision the log holds: got STALE, want NOT_VISIBLE", item), |
| 193 | fmt.Sprintf("S2 %s asked for the fixture's asker, at a revision the log never issued: got UNKNOWN, want NOT_VISIBLE", item)) |
| 194 | } |
| 195 | got := slices.Clone(report.Failures) |
| 196 | slices.Sort(got) |
| 197 | slices.Sort(want) |
| 198 | if !slices.Equal(got, want) { |
| 199 | t.Errorf("conformance failures are not exactly the inexpressible cases (spec §6.7).\n got (%d):", len(got)) |
| 200 | for _, s := range got { |
| 201 | t.Errorf(" %s", s) |
| 202 | } |
| 203 | t.Errorf("want (%d):", len(want)) |
| 204 | for _, s := range want { |
| 205 | t.Errorf(" %s", s) |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |