oficina / memoria Public

Clone
git clone https://amadan.net/oficina/memoria

Plain git — no account needed to clone.

Download

Download this file

1package memoria
2
3import (
4 "context"
5 "testing"
6
7 "amadan.net/oficina/suite"
8
9 "amadan.net/oficina/memoria/internal/memoria/sibling"
10)
11
12// perItemCustody rules each candidate on its own item id.
13//
14// **fixedCustody cannot do this**, and that is why this type exists
15// rather than a field being added to it: it returns sibling.Decide(give)
16// for every candidate from ONE field, so every fixture it builds has all
17// its passages visible or none of them. Gate 34 is entirely about pages
18// where some are and some are not, and drafts 1 and 2 of §8.8 both said
19// the gate could run against "memoriatest's sibling stub, which exists".
20// No such stub exists. §10 opens by naming that failure — a gate naming a
21// test that was renamed away — and the section citing it committed it
22// twice.
23type perItemCustody struct {
24 apps map[string]sibling.Sibling
25 // give is keyed by item id. A candidate whose item id is absent is
26 // VISIBLE, so a fixture names only the exceptions it cares about.
27 give map[string]suite.Authorization
28}
29
30func (p *perItemCustody) Sibling(source string) (sibling.Sibling, bool) {
31 s, ok := p.apps[source]
32 return s, ok
33}
34
35func (p *perItemCustody) Rule(_ context.Context, _ suite.Subject, cands []sibling.Candidate) []sibling.Disposition {
36 out := make([]sibling.Disposition, len(cands))
37 for i, c := range cands {
38 a, ok := p.give[c.Item.ItemID]
39 if !ok {
40 a = suite.VISIBLE
41 }
42 out[i] = sibling.Decide(a)
43 }
44 return out
45}
46
47// eircodeCorpus stores one sibling document per (title, eircode) pair,
48// each under its own item id so the fake above can rule them apart.
49func eircodeCorpus(t *testing.T, cus Custody, docs [][3]string) *Store {
50 t.Helper()
51 s := storeForTest(t)
52 s.custody = cus
53 for _, d := range docs {
54 if _, err := s.Ingest(context.Background(), IngestSpec{
55 Source: "Docs", Key: d[0], Title: d[1],
56 Body: "## Contact\n\nEircode: " + d[2],
57 ItemID: d[0],
58 SiblingRevision: 1,
59 }); err != nil {
60 t.Fatalf("ingest %s: %v", d[0], err)
61 }
62 }
63 return s
64}
65
66func searchFacts(t *testing.T, s *Store, q string) Page {
67 t.Helper()
68 p, err := s.Search(context.Background(),
69 suite.Subject{Kind: suite.SubjectAddress, Value: "paul@tito.io"}, q, 25)
70 if err != nil {
71 t.Fatalf("search: %v", err)
72 }
73 return p
74}
75
76// spans is the box: the answer rows. §8.10's best guess is a separate
77// surface and is not counted, so gates 34–40 keep meaning what they say.
78func spans(p Page) []string {
79 var out []string
80 for _, f := range p.Facts {
81 if f.Guess == nil {
82 out = append(out, f.Span)
83 }
84 }
85 return out
86}
87
88// guesses is §8.10's best-guess rows.
89func guesses(p Page) []Fact {
90 var out []Fact
91 for _, f := range p.Facts {
92 if f.Guess != nil {
93 out = append(out, f)
94 }
95 }
96 return out
97}
98
99// GATE 34 — the box never speaks a withheld passage, and every count
100// happens after the ruling (§8.8, §10).
101//
102// **Asserts the struct, never the rendered copy.** Two dispositions that
103// render an identical page is how Outcome.Record filed silent rulings
104// undetected (§1.9); a page assertion here could not tell case 2 from
105// case 3 at all.
106//
107// Mutations run, each verified red and reverted, 2026-09-14:
108// - addFacts ranging the pre-ruling hits instead of p.Results — cases
109// 2, 4, 5 and 6 go red.
110// - the cap counted before the ruling — case 4 goes red alone.
111// - the collision check computed over the unruled set — case 6 alone.
112// - citing r.DocumentID of the first hit rather than the first RESULT —
113// case 5 alone, and no other fixture can see it because every other
114// one uses distinct values per document.
115func TestGate34TheFactBoxNeverSpeaksAWithheldPassage(t *testing.T) {
116 roster := docsRoster()
117
118 // 1. PRESENCE. Both visible, two distinct eircodes, two candidates.
119 // Without this every absence below is an assertion about an empty
120 // world — §10's second rule, and gate 4's lesson.
121 t.Run("presence: two visible passages give two candidates", func(t *testing.T) {
122 s := eircodeCorpus(t, &perItemCustody{apps: roster}, [][3]string{
123 {"a", "Office A", "D02 X285"},
124 {"b", "Office B", "A65 F4E2"},
125 })
126 if got := spans(searchFacts(t, s, "eircode")); len(got) != 2 {
127 t.Fatalf("spans = %q, want two", got)
128 }
129 })
130
131 // 2. NOT_VISIBLE IS SILENT. §4.1's table makes OmitSilently the only
132 // disposition that may be silent, and Outcome.Record implements
133 // exactly that. Draft 1 of §8.8 demanded a notice here, which is
134 // either unreachable against correct code or an instruction to build
135 // the existence oracle §4.1 exists to deny. **The empty-notice
136 // assertion is the load-bearing half of this case.**
137 t.Run("not_visible: the survivor is boxed and nothing is said", func(t *testing.T) {
138 s := eircodeCorpus(t, &perItemCustody{
139 apps: roster,
140 give: map[string]suite.Authorization{"b": suite.NOT_VISIBLE},
141 }, [][3]string{
142 {"a", "Office A", "D02 X285"},
143 {"b", "Office B", "A65 F4E2"},
144 })
145 p := searchFacts(t, s, "eircode")
146 if got := spans(p); len(got) != 1 || got[0] != "D02 X285" {
147 t.Fatalf("spans = %q, want just the visible one", got)
148 }
149 if len(p.Notices) != 0 {
150 t.Errorf("notices = %q, want none: a NOT_VISIBLE is the one value that may be silent", p.Notices)
151 }
152 })
153
154 // 3. UNKNOWN IS LOUD. The same fixture, the other disposition. This
155 // is what draft 1 meant and attached to the wrong one.
156 t.Run("unknown: the survivor is boxed and the page says so", func(t *testing.T) {
157 s := eircodeCorpus(t, &perItemCustody{
158 apps: roster,
159 give: map[string]suite.Authorization{"b": suite.UNKNOWN},
160 }, [][3]string{
161 {"a", "Office A", "D02 X285"},
162 {"b", "Office B", "A65 F4E2"},
163 })
164 p := searchFacts(t, s, "eircode")
165 if got := spans(p); len(got) != 1 || got[0] != "D02 X285" {
166 t.Fatalf("spans = %q, want just the visible one", got)
167 }
168 if len(p.Notices) == 0 {
169 t.Error("want a notice: an UNKNOWN is withheld loudly (§6.5)")
170 }
171 })
172
173 // 4. THE CAP COUNTS AFTER THE RULING. Four values, one withheld,
174 // three left — a box. Counted before the ruling there are four, the
175 // cap fires, and the reader loses the box because of a document they
176 // may not see. **No two-document fixture can tell those apart**,
177 // which is why this one has four.
178 t.Run("the cap counts the ruled set", func(t *testing.T) {
179 s := eircodeCorpus(t, &perItemCustody{
180 apps: roster,
181 give: map[string]suite.Authorization{"d": suite.NOT_VISIBLE},
182 }, [][3]string{
183 {"a", "Office A", "D02 X285"},
184 {"b", "Office B", "A65 F4E2"},
185 {"c", "Office C", "D6W 1234"},
186 {"d", "Office D", "T12 XY45"},
187 })
188 if got := spans(searchFacts(t, s, "eircode")); len(got) != 3 {
189 t.Fatalf("spans = %q, want three", got)
190 }
191 })
192
193 // 5. EVERY FIELD OF THE CITATION IS THE SURVIVOR'S. The SAME eircode
194 // in a visible and a withheld document: one candidate, and all five
195 // of its fields from the visible one.
196 //
197 // **Asserting Title alone was not enough**, and a review mutation
198 // proved it: taking Title from the ruled result and DocumentID,
199 // Ordinal, Label and Trail from the first UNRULED occurrence passed
200 // this case, and would have rendered an href to a document the
201 // reader may not open, under the withheld document's own heading and
202 // label text. The two bodies were identical, so nothing could tell
203 // the fields apart. They differ now, and §10 says assert the tuple.
204 t.Run("every field of the citation is the visible passage's", func(t *testing.T) {
205 cus := &perItemCustody{
206 apps: roster,
207 give: map[string]suite.Authorization{"hidden": suite.NOT_VISIBLE},
208 }
209 s := storeForTest(t)
210 s.custody = cus
211 ctx := context.Background()
212 // The withheld one is ingested first AND carries the query's
213 // words three times, so it ranks above the visible one: if any
214 // field came from the hits rather than the results, it would be
215 // this document's.
216 hidden, err := s.Ingest(ctx, IngestSpec{
217 Source: "Docs", Key: "hidden", Title: "Hidden office",
218 Body: "## Hidden trail\n\nHidden eircode eircode eircode label: D02 X285",
219 ItemID: "hidden", SiblingRevision: 1,
220 })
221 if err != nil {
222 t.Fatalf("ingest hidden: %v", err)
223 }
224 shown, err := s.Ingest(ctx, IngestSpec{
225 Source: "Docs", Key: "shown", Title: "Visible office",
226 Body: "## Contact\n\nEircode: D02 X285",
227 ItemID: "shown", SiblingRevision: 1,
228 })
229 if err != nil {
230 t.Fatalf("ingest shown: %v", err)
231 }
232 if hidden == shown {
233 t.Fatal("the two documents share an id; this fixture cannot be wrong about anything")
234 }
235 p := searchFacts(t, s, "eircode")
236 if len(p.Facts) != 1 {
237 t.Fatalf("facts = %+v, want one", p.Facts)
238 }
239 got := p.Facts[0]
240 for _, c := range []struct{ name, got, want string }{
241 {"title", got.Title, "Visible office"},
242 {"trail", got.Trail, "Contact"},
243 {"label", got.Label, "Eircode"},
244 } {
245 if c.got != c.want {
246 t.Errorf("%s = %q, want %q: the box took a field from the passage this reader may not read", c.name, c.got, c.want)
247 }
248 }
249 if got.DocumentID != shown {
250 t.Errorf("citation links to document %d, want %d: the href points at the withheld document", got.DocumentID, shown)
251 }
252 // The fifth field, which the comment above claimed and the
253 // assertions did not make. Both documents here are single-chunk
254 // so it cannot currently differ — asserted anyway, because the
255 // claim should be true and the next fixture may have two.
256 if got.Ordinal != 0 {
257 t.Errorf("ordinal = %d, want 0", got.Ordinal)
258 }
259 })
260
261 // 7. A WITHHELD PASSAGE CANNOT SILENCE A KIND. §8.8's rule 4 — a
262 // document-authored label too long to show renders no box for that
263 // kind — is a count like the cap and the collision check, and it has
264 // to happen after the ruling for the same reason. Computed over the
265 // unruled set, a document the reader may not see takes the box away
266 // from them, which reports that document's existence.
267 //
268 // **No other fixture here has a NoBox candidate at all**, so a
269 // mutation OR-ing `silenced` from the unruled set passed all six of
270 // them. Found by review, 2026-09-14.
271 t.Run("a withheld over-long label does not silence the kind", func(t *testing.T) {
272 cus := &perItemCustody{
273 apps: roster,
274 give: map[string]suite.Authorization{"hidden": suite.NOT_VISIBLE},
275 }
276 s := storeForTest(t)
277 s.custody = cus
278 ctx := context.Background()
279 for _, d := range [][3]string{
280 // Over sixty characters, and carrying the query's word so it
281 // is actually retrieved — the first version of this fixture
282 // was green because nothing ever reached the box.
283 {"hidden", "Hidden office", "The eircode you must never give to any customer or supplier is: T12 XY45"},
284 {"shown", "Visible office", "Eircode: D02 X285"},
285 } {
286 if _, err := s.Ingest(ctx, IngestSpec{
287 Source: "Docs", Key: d[0], Title: d[1],
288 Body: "## Contact\n\n" + d[2],
289 ItemID: d[0], SiblingRevision: 1,
290 }); err != nil {
291 t.Fatalf("ingest %s: %v", d[0], err)
292 }
293 }
294 if got := spans(searchFacts(t, s, "eircode")); len(got) != 1 || got[0] != "D02 X285" {
295 t.Fatalf("spans = %q, want the visible one boxed", got)
296 }
297 })
298
299 // 6. COLLISION COUNTS AFTER THE RULING TOO. Two documents sharing a
300 // title and a trail, both falling to the trail for a label: over the
301 // unruled set they collide and the box vanishes, which is the same
302 // leak as case 4 wearing the collision rule's coat.
303 t.Run("collision counts the ruled set", func(t *testing.T) {
304 cus := &perItemCustody{
305 apps: roster,
306 give: map[string]suite.Authorization{"twin-b": suite.NOT_VISIBLE},
307 }
308 s := storeForTest(t)
309 s.custody = cus
310 ctx := context.Background()
311 for i, d := range [][2]string{{"twin-a", "D02 X285"}, {"twin-b", "A65 F4E2"}} {
312 if _, err := s.Ingest(ctx, IngestSpec{
313 Source: "Docs", Key: d[0], Title: "Offices",
314 // Mid-sentence and with no separator before it, so the
315 // label falls to the heading trail and the two rows
316 // would be identical but for their values.
317 Body: "## Contact\n\nThe eircode " + d[1] + " reaches us.",
318 ItemID: d[0],
319 SiblingRevision: int64(i) + 1,
320 }); err != nil {
321 t.Fatalf("ingest %s: %v", d[0], err)
322 }
323 }
324 if got := spans(searchFacts(t, s, "eircode")); len(got) != 1 {
325 t.Fatalf("spans = %q, want the one survivor boxed", got)
326 }
327 })
328}
329