| 1 | package memoria |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/suite" |
| 11 | |
| 12 | "amadan.net/oficina/memoria/internal/memoria/fact" |
| 13 | ) |
| 14 | |
| 15 | // GATE 44 — §8.12, finding a kind by its name (Paul, 2026-10-27: "No |
| 16 | // matches for postcode or eircode … need to be smarter about that"). |
| 17 | // |
| 18 | // One passage per kind, none of which says the kind's name, "address" or |
| 19 | // "office": a passage reached here was reached by its token and by |
| 20 | // nothing else. |
| 21 | // |
| 22 | // MUTATION-TESTED 2026-10-27, each built and seen red, each reverted: |
| 23 | // Kinds left out of the index entry, and the fill removed from the |
| 24 | // store's search → "Deliveries holds one"; the eircode→address token removed |
| 25 | // → "address finds []"; kind matches put before word matches → "outranks" (in |
| 26 | // the first build, KindsWeight raised to HeadingWeight did the same, and |
| 27 | // was green until Front desk, which says "phone" and holds no number, |
| 28 | // was added; the weight is gone now — see index.go); snippet() pointed at the kinds column → "snippet"; the |
| 29 | // validator check in IndexTokens removed → "look-alike" (green until the |
| 30 | // failed-checksum IBAN was added: an eircode's pattern IS its check, so |
| 31 | // the eircode look-alike never reached a validator). Tokens written as |
| 32 | // the word "phone" → TestKindTokensLeaveTheWordsWeight. |
| 33 | func TestGate44AKindIsFoundByItsName(t *testing.T) { |
| 34 | paul := suite.Subject{Kind: suite.SubjectAddress, Value: "paul@tito.io"} |
| 35 | ctx := context.Background() |
| 36 | s := factCorpus(t, &perItemCustody{apps: docsRoster()}, [][3]string{ |
| 37 | {"e", "Where we are", "## Contact\n\nLocation: 1 Grand Canal Square, Dublin 2, D02 X285"}, |
| 38 | {"x", "Tickets", "## Closed\n\nTicket D02 XB85 was closed on Friday."}, |
| 39 | {"p", "Reception", "## Reception\n\nThe phone at reception is 01 547 7854."}, |
| 40 | {"n", "Deliveries", "## Deliveries\n\nCall 01 547 7000 for deliveries."}, |
| 41 | {"d", "Front desk", "## Desk\n\nRing the phone at the front desk if nobody answers the door."}, |
| 42 | // The pattern matches, the mod-97 check refuses: the last digit |
| 43 | // is one off. |
| 44 | {"r", "Old reference", "## Reference\n\nReference IE29AIBK93115212345679 is on file."}, |
| 45 | {"i", "Paying us", "## Paying\n\nPay into IE29 AIBK 9311 5212 3456 78 by Friday."}, |
| 46 | {"v", "Invoices", "## Invoices\n\nOurs is IE3384527RH on every invoice."}, |
| 47 | {"m", "Writing to us", "## Writing\n\nWrite to hello@tito.io with questions."}, |
| 48 | }) |
| 49 | titles := func(q string) []string { |
| 50 | t.Helper() |
| 51 | page, err := s.Search(ctx, paul, q, 25) |
| 52 | if err != nil { |
| 53 | t.Fatalf("search %q: %v", q, err) |
| 54 | } |
| 55 | var out []string |
| 56 | for _, r := range page.Results { |
| 57 | out = append(out, r.Title) |
| 58 | if strings.Contains(r.Snippet, "xkind") { |
| 59 | t.Errorf("snippet: %q shows a kind token: %q", q, r.Snippet) |
| 60 | } |
| 61 | } |
| 62 | return out |
| 63 | } |
| 64 | has := func(ts []string, title string) bool { |
| 65 | for _, x := range ts { |
| 66 | if x == title { |
| 67 | return true |
| 68 | } |
| 69 | } |
| 70 | return false |
| 71 | } |
| 72 | |
| 73 | // PRESENCE, for every kind and every word the lexicon gives it. |
| 74 | for title, qs := range map[string][]string{ |
| 75 | "Where we are": {"eircode", "Eircode", "postcode", "post code", "address", "office address"}, |
| 76 | "Deliveries": {"phone", "telephone", "mobile", "tel", "phone number"}, |
| 77 | "Paying us": {"iban", "bank details"}, |
| 78 | "Invoices": {"vat", "vat number"}, |
| 79 | "Writing to us": {"email", "e-mail", "email address"}, |
| 80 | } { |
| 81 | for _, q := range qs { |
| 82 | if got := titles(q); !has(got, title) { |
| 83 | t.Errorf("%q finds %v; %q holds one", q, got, title) |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | // A look-alike adds nothing: one outside the eircode pattern, and an |
| 88 | // IBAN the pattern matches and the checksum refuses. |
| 89 | if got := titles("eircode"); has(got, "Tickets") { |
| 90 | t.Errorf("look-alike: %q found the passage with an invalid eircode: %v", "eircode", got) |
| 91 | } |
| 92 | if got := titles("iban"); has(got, "Old reference") { |
| 93 | t.Errorf("look-alike: %q found the passage whose IBAN fails its checksum: %v", "iban", got) |
| 94 | } |
| 95 | // Found, not reordered: a passage that SAYS phone outranks one that |
| 96 | // only holds a number — with a number too (Reception) or without one |
| 97 | // (Front desk). |
| 98 | got := titles("phone") |
| 99 | pos := map[string]int{} |
| 100 | for i, x := range got { |
| 101 | pos[x] = i |
| 102 | } |
| 103 | if len(got) == 0 || got[0] != "Reception" || pos["Front desk"] > pos["Deliveries"] { |
| 104 | t.Errorf("outranks: %q ranks %v; the passages that say \"phone\" must come before the one that only holds a number", "phone", got) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Narrowing survives the extra recall: gate 40's office page, among a |
| 109 | // dozen passages that hold other numbers and never say "phone" or |
| 110 | // "office", still answers with the office number alone. |
| 111 | func TestGate44NarrowingSurvivesTheTokens(t *testing.T) { |
| 112 | paul := suite.Subject{Kind: suite.SubjectAddress, Value: "paul@tito.io"} |
| 113 | docs := [][3]string{{"o", "Office", officePage}} |
| 114 | for i := 0; i < 12; i++ { |
| 115 | docs = append(docs, [3]string{"n" + strconv.Itoa(i), "Supplier " + strconv.Itoa(i), |
| 116 | "## Supplier\n\nCall 01 547 70" + strconv.Itoa(10+i) + " for orders."}) |
| 117 | } |
| 118 | s := factCorpus(t, &perItemCustody{apps: docsRoster()}, docs) |
| 119 | page, err := s.Search(context.Background(), paul, "what is the office phone number?", 25) |
| 120 | if err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | if len(page.Facts) != 1 || page.Facts[0].Span != "+353 (0)1 547 7854" { |
| 124 | var got []string |
| 125 | for _, f := range page.Facts { |
| 126 | got = append(got, f.Span) |
| 127 | } |
| 128 | t.Errorf("facts = %q, want the office number alone", got) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // §8.9's new reach, stated and tested: "address" now finds the eircode |
| 133 | // passage, so the model is sent it; "eircode" names no model kind and |
| 134 | // sends nothing. |
| 135 | func TestGate44AddressReachesTheModel(t *testing.T) { |
| 136 | paul := suite.Subject{Kind: suite.SubjectAddress, Value: "paul@tito.io"} |
| 137 | ctx := context.Background() |
| 138 | s := factCorpus(t, &perItemCustody{apps: docsRoster()}, [][3]string{ |
| 139 | {"e", "Where we are", "## Contact\n\nLocation: 1 Grand Canal Square, Dublin 2, D02 X285"}, |
| 140 | }) |
| 141 | rec := &recorder{} |
| 142 | s.extractor = rec |
| 143 | if _, err := s.Search(ctx, paul, "address", 25); err != nil { |
| 144 | t.Fatal(err) |
| 145 | } |
| 146 | if len(rec.calls) != 1 || len(rec.calls[0]) != 1 || !strings.Contains(rec.calls[0][0], "D02 X285") { |
| 147 | t.Errorf("address: model calls %q, want one carrying the eircode passage", rec.calls) |
| 148 | } |
| 149 | rec.calls = nil |
| 150 | if _, err := s.Search(ctx, paul, "eircode", 25); err != nil { |
| 151 | t.Fatal(err) |
| 152 | } |
| 153 | if len(rec.calls) != 0 { |
| 154 | t.Errorf("eircode: %d model calls, want none", len(rec.calls)) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func TestIndexTokens(t *testing.T) { |
| 159 | for _, c := range []struct{ in, want string }{ |
| 160 | {"Location: Dublin 2, D02 X285", "xkindeircode xkindaddress"}, |
| 161 | {"Reception 01 547 7854", "xkindphone"}, |
| 162 | {"Ticket D02 XB85 closed", ""}, |
| 163 | {"Nothing to see here.", ""}, |
| 164 | } { |
| 165 | if got := fact.IndexTokens(c.in); got != c.want { |
| 166 | t.Errorf("IndexTokens(%q) = %q, want %q", c.in, got, c.want) |
| 167 | } |
| 168 | } |
| 169 | // Boot runs this on every passage. A generous bound, not a |
| 170 | // benchmark: one regex pass per recogniser, and no labels derived. |
| 171 | big := strings.Repeat("Ticket D02 XB85 and 12345 and words words words. ", 4000) |
| 172 | start := time.Now() |
| 173 | fact.IndexTokens(big) |
| 174 | if took := time.Since(start); took > 3*time.Second { |
| 175 | t.Errorf("IndexTokens on %d KB took %v", len(big)/1024, took) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // §8.12's reason for tokens rather than words, measured as §8.8 measured |
| 180 | // the objection: twenty passages that each hold a phone number, one of |
| 181 | // which says "phone". With the words injected, "phone" was in every row |
| 182 | // and the passage that says it scored 0.0000; with tokens, the word keeps |
| 183 | // its weight and that passage stays far ahead of the nineteen. |
| 184 | func TestKindTokensLeaveTheWordsWeight(t *testing.T) { |
| 185 | var docs [][3]string |
| 186 | for i := 0; i < 20; i++ { |
| 187 | body := "## Line\n\nCall 01 547 70" + strconv.Itoa(10+i) + " today." |
| 188 | if i == 7 { |
| 189 | body = "## Line\n\nThe phone is 01 547 7854 today." |
| 190 | } |
| 191 | docs = append(docs, [3]string{"d" + strconv.Itoa(i), "Line " + strconv.Itoa(i), body}) |
| 192 | } |
| 193 | s := factCorpus(t, &perItemCustody{apps: docsRoster()}, docs) |
| 194 | paul := suite.Subject{Kind: suite.SubjectAddress, Value: "paul@tito.io"} |
| 195 | page, err := s.Search(context.Background(), paul, "phone", 25) |
| 196 | if err != nil { |
| 197 | t.Fatal(err) |
| 198 | } |
| 199 | hits := page.Results |
| 200 | if len(hits) != 20 { |
| 201 | t.Fatalf("%d results, want all twenty: each holds a phone number", len(hits)) |
| 202 | } |
| 203 | if hits[0].Title != "Line 7" { |
| 204 | t.Fatalf("the passage that says \"phone\" is not first: %q", hits[0].Title) |
| 205 | } |
| 206 | if hits[0].Score < 1.0 || hits[0].Score < 3*hits[1].Score { |
| 207 | t.Errorf("the word lost its weight: %.4f for the passage saying it, %.4f for the next", hits[0].Score, hits[1].Score) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // Astra's corpus, 2026-10-27: the office page, twenty-five suppliers that |
| 212 | // only hold numbers, and a hundred longer passages that say "phone" and |
| 213 | // hold none. ORing the kind token into the query let the suppliers' rare |
| 214 | // token outrank every word match: the office page fell out of the top 25 |
| 215 | // and the box guessed a supplier's number. The words come first now. |
| 216 | // |
| 217 | // MUTATION-TESTED 2026-10-27, reverted: the tokens ORed into the words' |
| 218 | // query again → red ("facts = [01 547 7010]"). |
| 219 | func TestGate44TokensNeverDisplaceAWordMatch(t *testing.T) { |
| 220 | paul := suite.Subject{Kind: suite.SubjectAddress, Value: "paul@tito.io"} |
| 221 | docs := [][3]string{{"o", "Office", officePage}} |
| 222 | for i := 0; i < 25; i++ { |
| 223 | docs = append(docs, [3]string{"s" + strconv.Itoa(i), "Supplier " + strconv.Itoa(i), |
| 224 | "## Supplier\n\n01 547 70" + strconv.Itoa(10+i)}) |
| 225 | } |
| 226 | for i := 0; i < 100; i++ { |
| 227 | docs = append(docs, [3]string{"p" + strconv.Itoa(i), "Policy " + strconv.Itoa(i), |
| 228 | "## Policy\n\nPhone calls with customers are logged in the CRM within a working day, and a phone call that runs long is summarised for the account owner before the end of the week."}) |
| 229 | } |
| 230 | s := factCorpus(t, &perItemCustody{apps: docsRoster()}, docs) |
| 231 | page, err := s.Search(context.Background(), paul, "what is the office phone number?", 25) |
| 232 | if err != nil { |
| 233 | t.Fatal(err) |
| 234 | } |
| 235 | var got []string |
| 236 | for _, f := range page.Facts { |
| 237 | got = append(got, f.Span) |
| 238 | } |
| 239 | if len(got) != 1 || got[0] != "+353 (0)1 547 7854" { |
| 240 | t.Errorf("facts = %q, want the office number alone", got) |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // The fill reaches the typing path, and every kind a query names. |
| 245 | // Mutations: the fill skipped while typing → "typing"; only the first |
| 246 | // kind's token used → "both kinds". |
| 247 | func TestGate44FillReachesTypingAndEveryKind(t *testing.T) { |
| 248 | paul := suite.Subject{Kind: suite.SubjectAddress, Value: "paul@tito.io"} |
| 249 | ctx := context.Background() |
| 250 | s := factCorpus(t, &perItemCustody{apps: docsRoster()}, [][3]string{ |
| 251 | {"e", "Where we are", "## Contact\n\nLocation: 1 Grand Canal Square, Dublin 2, D02 X285"}, |
| 252 | {"i", "Paying us", "## Paying\n\nPay into IE29 AIBK 9311 5212 3456 78 by Friday."}, |
| 253 | }) |
| 254 | titles := func(p Page) map[string]bool { |
| 255 | out := map[string]bool{} |
| 256 | for _, r := range p.Results { |
| 257 | out[r.Title] = true |
| 258 | } |
| 259 | return out |
| 260 | } |
| 261 | typed, err := s.SearchWhileTyping(ctx, paul, "eircode", 25) |
| 262 | if err != nil || !titles(typed)["Where we are"] { |
| 263 | t.Errorf("typing: %q while typing found %v, %v", "eircode", titles(typed), err) |
| 264 | } |
| 265 | both, err := s.Search(ctx, paul, "eircode and iban", 25) |
| 266 | if err != nil || !titles(both)["Where we are"] || !titles(both)["Paying us"] { |
| 267 | t.Errorf("both kinds: %q found %v, %v", "eircode and iban", titles(both), err) |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // A value whose label is too long for a box (NoBox) is still a value the |
| 272 | // passage holds. Mutation: IndexTokens skipping NoBox values → red. |
| 273 | func TestIndexTokensCountANoBoxValue(t *testing.T) { |
| 274 | text := strings.Repeat("this sentence keeps going on about where the calls are routed ", 4) + "is: 01 547 7854" |
| 275 | if f := fact.Scan(text, "Contact", "Tito", []fact.Kind{fact.Phone}); len(f) != 1 || !f[0].NoBox { |
| 276 | t.Fatalf("fixture: the number should be NoBox, got %+v", f) |
| 277 | } |
| 278 | if got := fact.IndexTokens(text); got != "xkindphone" { |
| 279 | t.Errorf("IndexTokens = %q, want xkindphone", got) |
| 280 | } |
| 281 | } |
| 282 | |