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 "strings"
5 "testing"
6
7 "amadan.net/oficina/suite"
8)
9
10// GATE 41 (store half) — §8.10's best guess: only when the box renders
11// nothing for a kind with candidates; the scoring tiers in order; the
12// safety refusals kept; counted after the ruling.
13//
14// MUTATION-TESTED 2026-10-26, each built and seen red, each reverted:
15// the two tiers swapped; the title-and-trail tier removed; the guess
16// always the first candidate; the NoBox refusal removed; the collision
17// refusal removed; a guess made even when the box renders; a fallback
18// label counted as an own label; and Astra's, green before: title and
19// trail counted separately (a word in both scoring twice). The title
20// tier's fixture first passed
21// with the tier removed — the titled page also ranked first — and now
22// checks the rank order it depends on.
23func TestGate41TheBestGuess(t *testing.T) {
24 roster := docsRoster()
25 visible := func() Custody { return &perItemCustody{apps: roster} }
26 page := func(t *testing.T, body string) *Store {
27 return factCorpus(t, visible(), [][3]string{{"a", "Contacts", body}})
28 }
29 guessOf := func(t *testing.T, p Page) (string, int) {
30 t.Helper()
31 g := guesses(p)
32 if len(g) != 1 {
33 t.Fatalf("guesses = %+v, want one", g)
34 }
35 return g[0].Span, g[0].Guess.Of
36 }
37 noGuess := func(t *testing.T, p Page) {
38 t.Helper()
39 if g := guesses(p); len(g) != 0 {
40 t.Errorf("guessed %q, want no guess", g[0].Span)
41 }
42 }
43
44 t.Run("no qualifiers: the first-seen candidate", func(t *testing.T) {
45 s := page(t, "## Phones\n\nReception: +353 (0)1 547 0001\n\nSales: +353 (0)1 547 0002\n\nFax: +353 (0)1 547 0003\n\nSupport: +353 (0)1 547 0004")
46 p := searchFacts(t, s, "phone number")
47 if got := spans(p); len(got) != 0 {
48 t.Fatalf("the box rendered %q; this fixture needs it empty", got)
49 }
50 span, of := guessOf(t, p)
51 if span != "+353 (0)1 547 0001" || of != 4 {
52 t.Errorf("guess = %q of %d, want the first of 4", span, of)
53 }
54 })
55
56 t.Run("the box renders, so no guess", func(t *testing.T) {
57 s := page(t, "## Phones\n\nOffice phone: +353 (0)1 547 0001")
58 p := searchFacts(t, s, "phone")
59 if got := spans(p); len(got) != 1 {
60 t.Fatalf("the box = %q, want one row", got)
61 }
62 noGuess(t, p)
63 })
64
65 t.Run("no candidates, so no guess", func(t *testing.T) {
66 noGuess(t, searchFacts(t, page(t, "## Phones\n\nNo numbers, only the word phone."), "phone"))
67 })
68
69 // Tier 1 beats tier 2: B's title and trail carry both words, A's own
70 // label carries one. Neither label has both, so narrowing finds
71 // nothing and the box is empty.
72 t.Run("own-label coverage beats title and trail", func(t *testing.T) {
73 s := factCorpus(t, visible(), [][3]string{
74 {"b", "Dublin office", "## Dublin office phones\n\nReception: +353 (0)1 547 0002"},
75 {"a", "Kitchen", "## Phones\n\nOffice: +353 (0)1 547 0001"},
76 {"c", "Cork", "## Cork phones\n\nSales: +353 (0)21 547 0003"},
77 {"d", "Galway", "## Galway phones\n\nFax: +353 (0)91 547 0004"},
78 })
79 p := searchFacts(t, s, "dublin office phone")
80 if span, _ := guessOf(t, p); span != "+353 (0)1 547 0001" {
81 t.Errorf("guess = %q, want the own-label match over the title match", span)
82 }
83 })
84
85 // Only the document's OWN label counts in tier 1. X falls to its
86 // heading "Office" for a label; counted as its own it would score 1
87 // and beat Z, whose title and trail carry both words.
88 t.Run("a fallback label is not an own label", func(t *testing.T) {
89 s := factCorpus(t, visible(), [][3]string{
90 {"x", "Kitchen", "## Office\n\n- +353 (0)1 547 0001"},
91 {"z", "Dublin office", "## Dublin office phones\n\nReception: +353 (0)1 547 0002"},
92 {"c", "Cork", "## Cork phones\n\nSales: +353 (0)21 547 0003"},
93 {"d", "Galway", "## Galway phones\n\nFax: +353 (0)91 547 0004"},
94 })
95 p := searchFacts(t, s, "dublin office phone")
96 if span, _ := guessOf(t, p); span != "+353 (0)1 547 0002" {
97 t.Errorf("guess = %q, want Z, whose title and trail carry both words", span)
98 }
99 })
100
101 // Tier 2 beats tier 3. The front desk's passage says "office" five
102 // times in its body, so it ranks FIRST, but its title and trail do
103 // not; the later Office page's title does. The fixture checks that
104 // order first, because if the titled page ranked first anyway this
105 // would pass with the tier removed.
106 t.Run("title and trail coverage beats first-seen", func(t *testing.T) {
107 s := factCorpus(t, visible(), [][3]string{
108 {"f", "Front desk", "## Phones\n\nThe office office office office office building. Reception: +353 (0)1 547 0001"},
109 {"o", "Office", "## Phones\n\nFax: +353 (0)1 547 0003"},
110 {"c", "Cork", "## Cork phones\n\nSales: +353 (0)21 547 0005"},
111 {"g", "Galway", "## Galway phones\n\nSupport: +353 (0)91 547 0007"},
112 })
113 p := searchFacts(t, s, "office phone")
114 if len(p.Results) == 0 || p.Results[0].Title != "Front desk" {
115 t.Fatalf("the front desk must rank first for this fixture to mean anything; results start %+v", p.Results[:1])
116 }
117 if span, _ := guessOf(t, p); span != "+353 (0)1 547 0003" {
118 t.Errorf("guess = %q, want the Office page's, whose title says office", span)
119 }
120 })
121
122 // Title and trail are counted together, each word once. A's title AND
123 // trail both say "office" — one word, counted once, 1 — while B's
124 // carry "dublin" and "office", 2. Counted per field, A also scores 2,
125 // ties, and wins on rank; the fixture checks A ranks first.
126 t.Run("a word in both title and trail counts once", func(t *testing.T) {
127 s := factCorpus(t, visible(), [][3]string{
128 {"a", "Office", "## Office\n\nDublin office phone. Dublin office phone. Dublin office phone. Dublin office phone. Reception: +353 (0)1 547 0001"},
129 {"b", "Dublin office", "## Phones\n\nFax: +353 (0)1 547 0002"},
130 {"c", "Cork", "## Cork phones\n\nSales: +353 (0)21 547 0003"},
131 {"d", "Galway", "## Galway phones\n\nSupport: +353 (0)91 547 0004"},
132 })
133 p := searchFacts(t, s, "dublin office phone")
134 if len(p.Results) == 0 || p.Results[0].Title != "Office" {
135 t.Fatalf("A must rank first for this fixture to mean anything; results start %+v", p.Results[:1])
136 }
137 if span, _ := guessOf(t, p); span != "+353 (0)1 547 0002" {
138 t.Errorf("guess = %q, want B, whose title carries both words", span)
139 }
140 })
141
142 t.Run("any over-long label means no guess", func(t *testing.T) {
143 s := page(t, "## Phones\n\nReception: +353 (0)1 547 0001\n\nSales: +353 (0)1 547 0002\n\nFax: +353 (0)1 547 0003\n\n"+
144 "The number you must never give to anyone at all "+strings.Repeat("x", 40)+": +353 (0)1 547 0004")
145 noGuess(t, searchFacts(t, s, "phone number"))
146 })
147
148 t.Run("a guess whose citation line collides means no guess", func(t *testing.T) {
149 s := page(t, "## Phones\n\nOffice: +353 (0)1 547 0001\n\nOffice: +353 (0)1 547 0002\n\nFax: +353 (0)1 547 0003\n\nSales: +353 (0)1 547 0004")
150 noGuess(t, searchFacts(t, s, "phone number"))
151 })
152
153 // Counted after the ruling: the withheld document is first-seen, so a
154 // guess over the unruled set would pick it.
155 t.Run("a withheld candidate is never the guess and is not counted", func(t *testing.T) {
156 s := factCorpus(t, &perItemCustody{apps: roster, give: map[string]suite.Authorization{"a": suite.NOT_VISIBLE}}, [][3]string{
157 {"a", "Alpha", "## Alpha phones\n\nOffice phone: +353 (0)1 547 0001"},
158 {"b", "Beta", "## Beta phones\n\nReception: +353 (0)1 547 0002"},
159 {"c", "Gamma", "## Gamma phones\n\nSales: +353 (0)1 547 0003"},
160 {"d", "Delta", "## Delta phones\n\nFax: +353 (0)1 547 0004"},
161 {"e", "Epsilon", "## Epsilon phones\n\nSupport: +353 (0)1 547 0005"},
162 })
163 p := searchFacts(t, s, "phone number")
164 span, of := guessOf(t, p)
165 if span == "+353 (0)1 547 0001" {
166 t.Error("the guess is the withheld document's value")
167 }
168 if of != 4 {
169 t.Errorf("guess is 1 of %d, want 1 of 4: the withheld one is not counted", of)
170 }
171 })
172}
173
174// bestGuess refuses on NoBox itself, not on how long the label looks. A
175// label line beyond the lookback is NoBox with a SHORT visible tail
176// (§8.8); a guess that checked length instead would pick around it. Called
177// directly: a page fixture producing exactly that, with no other refusal
178// in play, needs sibling lines arranged to the byte.
179//
180// MUTATION-TESTED 2026-10-26, reverted: the refusal made to depend on the
181// label being longer than MaxLabel → "guessed around a short-labelled
182// NoBox candidate".
183func TestBestGuessRefusesOnNoBoxNotLength(t *testing.T) {
184 cand := func(span, label string, own, noBox bool) factCandidate {
185 return factCandidate{fact: Fact{Kind: "phone", Span: span, Label: label, Title: "T" + span}, own: own, noBox: noBox}
186 }
187 cs := []factCandidate{
188 cand("+353 (0)1 547 0001", "Reception", true, false),
189 cand("+353 (0)1 547 0002", "ice", true, true), // the visible tail of a label beyond the lookback
190 cand("+353 (0)1 547 0003", "Sales", true, false),
191 }
192 if g, ok := bestGuess(cs, nil); ok {
193 t.Errorf("guessed around a short-labelled NoBox candidate: %q", g.fact.Span)
194 }
195 cs[1].noBox = false
196 if _, ok := bestGuess(cs, nil); !ok {
197 t.Error("no guess with no NoBox candidate; the refusal above proves nothing")
198 }
199}
200