| 1 | package memoriatest |
| 2 | |
| 3 | import ( |
| 4 | "html" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "net/url" |
| 8 | "regexp" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | // The fact box, end to end: through the real handler, the real template |
| 14 | // and the real escaping. |
| 15 | // |
| 16 | // **Through HTTP and not against the struct, deliberately, and gate 34 is |
| 17 | // the opposite deliberately.** Gate 34 is about what the box may CONTAIN, |
| 18 | // where two dispositions render identical pages and only the struct can |
| 19 | // tell them apart. These are about what reaches the reader, where a |
| 20 | // template that escapes a span, drops a label or renders the box outside |
| 21 | // the swapped region is exactly the defect — and a struct assertion |
| 22 | // cannot see any of it. AGENTS.md: a template test proves markup exists, |
| 23 | // not that it renders. |
| 24 | |
| 25 | // An ANSWER row's value. A row marked rst-fact-guess is §8.10's best |
| 26 | // guess, a separate surface; these gates are about the box. |
| 27 | var factValueRe = regexp.MustCompile(`(?s)<p rst-fact(?: rst-fact-model)?>\s*<span rst-fact-label>[^<]*</span>\s*<span rst-fact-value[^>]*>([^<]*)</span>`) |
| 28 | var guessValueRe = regexp.MustCompile(`(?s)<p rst-fact[^>]* rst-fact-guess>.*?<span rst-fact-value[^>]*>([^<]*)</span>`) |
| 29 | var factLabelRe = regexp.MustCompile(`<span rst-fact-label>([^<]*)</span>`) |
| 30 | |
| 31 | func factValues(body string) []string { return capture(factValueRe, body) } |
| 32 | func factLabels(body string) []string { return capture(factLabelRe, body) } |
| 33 | func hasFactBox(body string) bool { return len(factValues(body)) > 0 } |
| 34 | func guessValues(body string) []string { return capture(guessValueRe, body) } |
| 35 | |
| 36 | // capture pulls the rendered text out of the box and UNESCAPES it. |
| 37 | // |
| 38 | // html/template escapes "+" to "+" in text content, which is correct |
| 39 | // and which the reader never sees. Gate 36 is about the bytes that reach |
| 40 | // the READER — "byte-identical to the document" is a claim about what is |
| 41 | // displayed, not about the wire — so comparing raw markup would fail the |
| 42 | // gate on the escaping working properly, and "fixing" that would mean |
| 43 | // unescaping the span into the page, which is an injection. |
| 44 | func capture(re *regexp.Regexp, s string) []string { |
| 45 | var out []string |
| 46 | for _, m := range re.FindAllStringSubmatch(s, -1) { |
| 47 | out = append(out, html.UnescapeString(m[1])) |
| 48 | } |
| 49 | return out |
| 50 | } |
| 51 | |
| 52 | func search(t *testing.T, h http.Handler, q string) string { |
| 53 | t.Helper() |
| 54 | return get(t, h, "/?q="+url.QueryEscape(q)).Body.String() |
| 55 | } |
| 56 | |
| 57 | // GATE 35 — candidates are canonical values, ambiguity is enumerated |
| 58 | // rather than collapsed, and the cap is three (§8.8, §10). |
| 59 | // |
| 60 | // One test rather than five, because the rules share a definition and a |
| 61 | // gate that splits them cannot see the interactions: the same number in |
| 62 | // five documents is one candidate AND therefore does not pass the cap. |
| 63 | // |
| 64 | // Mutations run, each verified red and reverted, 2026-09-14: |
| 65 | // - returning found[k][:1] when there is more than one — "two distinct" |
| 66 | // goes red. |
| 67 | // - keying dedupe on Span instead of Canonical — "two spellings" red. |
| 68 | // - keying dedupe on the passage rather than the value — "five |
| 69 | // documents" red. |
| 70 | // - cap written len(cs) >= MaxFacts — "three still renders" red, and |
| 71 | // nothing else, which is the point of having it. |
| 72 | // - cap and collision counted across kinds rather than per kind — the |
| 73 | // two-kind case red. |
| 74 | func TestGate35CandidatesAmbiguityAndTheCap(t *testing.T) { |
| 75 | t.Run("presence: one number, one box, one value", func(t *testing.T) { |
| 76 | h := newApp(t) |
| 77 | store(t, h, "Tito office", "## Contact\n\nOffice phone: +353 (0)1 547 7854") |
| 78 | body := search(t, h, "phone number") |
| 79 | if got := factValues(body); len(got) != 1 || got[0] != "+353 (0)1 547 7854" { |
| 80 | t.Fatalf("values = %q, want the one number", got) |
| 81 | } |
| 82 | }) |
| 83 | |
| 84 | // Two right answers to an underspecified question. §8.7 refuses in |
| 85 | // this position; §8.8 enumerates, because two phone numbers are not |
| 86 | // a wrong average — and the labels are what let the reader choose. |
| 87 | t.Run("two distinct numbers are both named, neither picked", func(t *testing.T) { |
| 88 | h := newApp(t) |
| 89 | store(t, h, "Tito office", "## Contact\n\nOffice phone: +353 (0)1 547 7854") |
| 90 | store(t, h, "Support", "## Contact\n\nSupport phone: +353 (0)1 598 1234") |
| 91 | got := factValues(search(t, h, "phone number")) |
| 92 | if len(got) != 2 { |
| 93 | t.Fatalf("values = %q, want both", got) |
| 94 | } |
| 95 | }) |
| 96 | |
| 97 | // A dedupe keyed on the span passes any fixture that spells the |
| 98 | // number the same way twice, which is why this one does not. |
| 99 | t.Run("two spellings of one number are one candidate", func(t *testing.T) { |
| 100 | h := newApp(t) |
| 101 | store(t, h, "Tito office", "## Contact\n\nOffice phone: +353 (0)1 547 7854") |
| 102 | store(t, h, "Handbook", "## Contact\n\nOffice phone: 01 547 7854") |
| 103 | if got := factValues(search(t, h, "phone number")); len(got) != 1 { |
| 104 | t.Fatalf("values = %q, want one: the same number written two ways", got) |
| 105 | } |
| 106 | }) |
| 107 | |
| 108 | // The commonest real shape — a signature repeated through forwarded |
| 109 | // mail. An implementation counting passages rather than values shows |
| 110 | // five rows here, or passes the cap and shows none. |
| 111 | t.Run("the same number in five documents is one candidate", func(t *testing.T) { |
| 112 | h := newApp(t) |
| 113 | for _, title := range []string{"One", "Two", "Three", "Four", "Five"} { |
| 114 | store(t, h, title, "## Contact\n\nOffice phone: +353 (0)1 547 7854") |
| 115 | } |
| 116 | if got := factValues(search(t, h, "phone number")); len(got) != 1 { |
| 117 | t.Fatalf("values = %q, want one", got) |
| 118 | } |
| 119 | }) |
| 120 | |
| 121 | // index/rank_test.go's own paragraph, which came off the live |
| 122 | // instance. An implementation treating the CHUNK as the unit hides |
| 123 | // the second number — the one the document calls unused. |
| 124 | t.Run("two numbers in one passage are two candidates", func(t *testing.T) { |
| 125 | h := newApp(t) |
| 126 | store(t, h, "Office address", "## Office Address\n\nTurn the knob. "+ |
| 127 | "The office phone number is: +353 (0)1 547 7854. "+ |
| 128 | "We also have the following unused phone numbers: +353 (0)1 598 1234.") |
| 129 | body := search(t, h, "phone number") |
| 130 | if got := factValues(body); len(got) != 2 { |
| 131 | t.Fatalf("values = %q, want both", got) |
| 132 | } |
| 133 | // And the labels are what make the enumeration honest: the |
| 134 | // document said which is unused, so the page does. |
| 135 | labels := factLabels(body) |
| 136 | if len(labels) != 2 || !strings.Contains(labels[1], "unused") { |
| 137 | t.Fatalf("labels = %q; the document's own word for the second number is missing", labels) |
| 138 | } |
| 139 | }) |
| 140 | |
| 141 | // **Three AND four.** A cap written `>= MaxFacts` passes a suite that |
| 142 | // tests only one, two and four. |
| 143 | t.Run("three renders, four renders nothing", func(t *testing.T) { |
| 144 | nums := []string{"+353 (0)1 547 7854", "+353 (0)1 598 1234", "+353 (0)1 555 0101", "+353 (0)1 555 0202"} |
| 145 | for _, n := range []int{3, 4} { |
| 146 | h := newApp(t) |
| 147 | for i := 0; i < n; i++ { |
| 148 | store(t, h, "Office "+string(rune('A'+i)), "## Contact\n\nOffice phone: "+nums[i]) |
| 149 | } |
| 150 | body := search(t, h, "phone number") |
| 151 | switch { |
| 152 | case n == 3 && len(factValues(body)) != 3: |
| 153 | t.Errorf("three candidates rendered %q, want three", factValues(body)) |
| 154 | case n == 4 && hasFactBox(body): |
| 155 | t.Errorf("four candidates rendered a box: %q", factValues(body)) |
| 156 | } |
| 157 | } |
| 158 | }) |
| 159 | |
| 160 | // Every rule is per kind. Counted across kinds, two of each passes |
| 161 | // the cap and both boxes vanish. |
| 162 | t.Run("a two-kind query renders both kinds", func(t *testing.T) { |
| 163 | h := newApp(t) |
| 164 | store(t, h, "Tito office", "## Contact\n\nOffice phone: +353 (0)1 547 7854\n\nEircode: D02 X285") |
| 165 | store(t, h, "Support", "## Contact\n\nSupport phone: +353 (0)1 598 1234\n\nEircode: A65 F4E2") |
| 166 | got := factValues(search(t, h, "phone number and eircode")) |
| 167 | if len(got) != 4 { |
| 168 | t.Fatalf("values = %q, want two of each kind", got) |
| 169 | } |
| 170 | }) |
| 171 | |
| 172 | // The whole citation line collides, so the page would print one |
| 173 | // identical row twice against two different values. |
| 174 | t.Run("colliding rows render no box", func(t *testing.T) { |
| 175 | h := newApp(t) |
| 176 | store(t, h, "Suppliers", "## Phone\n\n- +353 (0)1 555 0101\n- +353 (0)1 555 0202") |
| 177 | if body := search(t, h, "phone number"); hasFactBox(body) { |
| 178 | t.Errorf("rendered a box with two identical rows: %q / %q", factLabels(body), factValues(body)) |
| 179 | } |
| 180 | }) |
| 181 | |
| 182 | // §10 says this gate asserts first-seen order, and for a while it |
| 183 | // asserted only the count. A box sorted by canonical value is |
| 184 | // deterministic and passes every length check, and the two-values-in- |
| 185 | // one-passage fixture happens to sort the same way it was seen — so |
| 186 | // nothing here could see it. Found by review, 2026-09-14. |
| 187 | t.Run("rows are in first-seen order", func(t *testing.T) { |
| 188 | h := newApp(t) |
| 189 | // Both orderings a wrong implementation would pick disagree with |
| 190 | // document order here: sorted by VALUE the 547 number comes |
| 191 | // first, and sorted by LABEL "Alternate" sorts before "Work". |
| 192 | // Only first-seen gives 598, so this one fixture holds against |
| 193 | // both — and the version it replaces held against neither, |
| 194 | // because its two labels happened to sort as they were seen. |
| 195 | store(t, h, "Tito office", "## Contact\n\n"+ |
| 196 | "Work phone is: +353 (0)1 598 1234. "+ |
| 197 | "Alternate phone is: +353 (0)1 547 7854.") |
| 198 | got := factValues(search(t, h, "phone number")) |
| 199 | if len(got) != 2 { |
| 200 | t.Fatalf("values = %q, want two", got) |
| 201 | } |
| 202 | if got[0] != "+353 (0)1 598 1234" { |
| 203 | t.Errorf("first row = %q; the box is not in the order the document states them", got[0]) |
| 204 | } |
| 205 | }) |
| 206 | |
| 207 | // Every collision fixture above is a PAIR, so a check comparing only |
| 208 | // neighbours passes all of them. Three rows whose first and third are |
| 209 | // identical is the shape that sees it. |
| 210 | t.Run("a collision between non-adjacent rows still suppresses", func(t *testing.T) { |
| 211 | h := newApp(t) |
| 212 | store(t, h, "Tito office", "## Contact\n\n"+ |
| 213 | "Ring the office phone on 01 547 7854 any weekday. "+ |
| 214 | "Support phone: 01 598 1234. "+ |
| 215 | "Or dial the phone on 01 555 0101 instead.") |
| 216 | if body := search(t, h, "phone number"); hasFactBox(body) { |
| 217 | t.Errorf("rendered a box whose first and third rows are identical: %q / %q", |
| 218 | factLabels(body), factValues(body)) |
| 219 | } |
| 220 | }) |
| 221 | |
| 222 | // The mirror of the title case below, for the trail. Same title, same |
| 223 | // label, different headings — the trail is what tells them apart, and |
| 224 | // a collision check ignoring it loses a real answer. |
| 225 | t.Run("the same label under different headings still renders", func(t *testing.T) { |
| 226 | h := newApp(t) |
| 227 | store(t, h, "Tito office", "## Office\n\nPhone: +353 (0)1 547 7854\n\n## Support\n\nPhone: +353 (0)1 598 1234") |
| 228 | if got := factValues(search(t, h, "phone number")); len(got) != 2 { |
| 229 | t.Fatalf("values = %q, want both: the headings tell them apart", got) |
| 230 | } |
| 231 | }) |
| 232 | |
| 233 | // A different title disambiguates perfectly, so this one MUST box. |
| 234 | // Keying collision on the label alone loses a real answer here. |
| 235 | t.Run("the same label under different titles still renders", func(t *testing.T) { |
| 236 | h := newApp(t) |
| 237 | store(t, h, "Tito office", "## Contact\n\nOffice phone: +353 (0)1 547 7854") |
| 238 | store(t, h, "Acme supplier", "## Contact\n\nOffice phone: +353 (0)1 598 1234") |
| 239 | if got := factValues(search(t, h, "phone number")); len(got) != 2 { |
| 240 | t.Fatalf("values = %q, want both: the titles tell them apart", got) |
| 241 | } |
| 242 | }) |
| 243 | } |
| 244 | |
| 245 | // GATE 36 — the displayed value is byte-identical to the document's |
| 246 | // (§8.8, §10). The canonical form exists for matching and must never |
| 247 | // reach the page. |
| 248 | // |
| 249 | // Mutation: canonicalise on the display path — one line's difference from |
| 250 | // what the code already does for identity — and every case goes red. |
| 251 | func TestGate36TheDisplayedValueIsTheDocumentsBytes(t *testing.T) { |
| 252 | for _, c := range []struct{ name, body, want string }{ |
| 253 | {"as written", "## Contact\n\nOffice phone: +353 (0)1 547 7854", "+353 (0)1 547 7854"}, |
| 254 | {"with an extension", "## Contact\n\nOffice phone: +353 (0)1 547 7854 ext. 12", "+353 (0)1 547 7854 ext. 12"}, |
| 255 | // Pasted from Word or Notion. Go's \s does not match U+00A0, so |
| 256 | // a recogniser written with it finds nothing here and this case |
| 257 | // fails on PRESENCE rather than on the bytes — which is the |
| 258 | // design demand and not an accident. |
| 259 | {"pasted with non-breaking spaces", "## Contact\n\nOffice phone: +353 (0)1 547 7854", "+353 (0)1 547 7854"}, |
| 260 | } { |
| 261 | t.Run(c.name, func(t *testing.T) { |
| 262 | h := newApp(t) |
| 263 | store(t, h, "Tito office", c.body) |
| 264 | got := factValues(search(t, h, "phone number")) |
| 265 | if len(got) != 1 { |
| 266 | t.Fatalf("values = %q, want one", got) |
| 267 | } |
| 268 | if got[0] != c.want { |
| 269 | t.Errorf("value = %q, want %q", got[0], c.want) |
| 270 | } |
| 271 | }) |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // GATE 37 — the label is the document's, and it reaches the page. |
| 276 | // |
| 277 | // The exhaustive matrix — bullets, adjacent values, abbreviations, the |
| 278 | // cap boundary, the no-heading fallback — is unit-tested in |
| 279 | // internal/memoria/fact, where each rule can be driven directly. What |
| 280 | // this gate adds is the half a unit test cannot see: that the label |
| 281 | // survives the store, the template and the escaping, and that a value the |
| 282 | // document labelled too long to show renders NO BOX rather than a vaguer |
| 283 | // substitute. |
| 284 | func TestGate37TheLabelReachesThePage(t *testing.T) { |
| 285 | t.Run("the document's own label is what is shown", func(t *testing.T) { |
| 286 | h := newApp(t) |
| 287 | store(t, h, "Tito office", "## Contact\n\nOffice phone: +353 (0)1 547 7854") |
| 288 | if got := factLabels(search(t, h, "phone number")); len(got) != 1 || got[0] != "Office phone" { |
| 289 | t.Fatalf("labels = %q, want [Office phone]", got) |
| 290 | } |
| 291 | }) |
| 292 | |
| 293 | t.Run("no label in the line falls to the heading trail", func(t *testing.T) { |
| 294 | h := newApp(t) |
| 295 | store(t, h, "Tito office", "## Contact\n\nRing the office phone on +353 (0)1 547 7854 any weekday.") |
| 296 | if got := factLabels(search(t, h, "phone number")); len(got) != 1 || got[0] != "Contact" { |
| 297 | t.Fatalf("labels = %q, want [Contact]", got) |
| 298 | } |
| 299 | }) |
| 300 | |
| 301 | // Substituting the trail would replace the document's own warning |
| 302 | // with a vaguer word, beside a number the document did not warn |
| 303 | // about. Dropping just this candidate would turn a two-candidate |
| 304 | // ambiguity into a confident single answer. Both are worse than |
| 305 | // silence. |
| 306 | t.Run("a label too long to show renders no box at all", func(t *testing.T) { |
| 307 | h := newApp(t) |
| 308 | store(t, h, "Tito office", "## Contact\n\nReception phone: +353 (0)1 547 7854\n\n"+ |
| 309 | "The number you must never give to any customer because it rings the chief executive directly is: +353 (0)1 598 1234") |
| 310 | if body := search(t, h, "phone number"); hasFactBox(body) { |
| 311 | t.Errorf("rendered a box: %q / %q", factLabels(body), factValues(body)) |
| 312 | } |
| 313 | }) |
| 314 | } |
| 315 | |
| 316 | // The box lives INSIDE the results define, so the fragment search.js |
| 317 | // swaps carries it. Outside, query N-1's answer would sit over query N's |
| 318 | // results on every keystroke — §8.4 inverted, several times a second. |
| 319 | // |
| 320 | // This is the page-and-fragment agreement that TestALookupPrimesTheFirstResult |
| 321 | // makes for priming, and it is needed for the same reason: two renderings |
| 322 | // of one region that can disagree are two regions. |
| 323 | func TestTheFactBoxIsInsideTheSwappedRegion(t *testing.T) { |
| 324 | h := newApp(t) |
| 325 | store(t, h, "Tito office", "## Contact\n\nOffice phone: +353 (0)1 547 7854") |
| 326 | page := search(t, h, "phone number") |
| 327 | fragment := get(t, h, "/search?q="+url.QueryEscape("phone number")).Body.String() |
| 328 | if !hasFactBox(page) { |
| 329 | t.Fatal("no box on the page") |
| 330 | } |
| 331 | if !hasFactBox(fragment) { |
| 332 | t.Error("no box in the fragment: it would not survive a keystroke") |
| 333 | } |
| 334 | if a, b := factValues(page), factValues(fragment); len(a) != len(b) || a[0] != b[0] { |
| 335 | t.Errorf("page %q and fragment %q disagree", a, b) |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | // Enter goes to the first RESULT, never to the box's citation. §8.8: |
| 340 | // changing what Enter means depending on whether a regular expression |
| 341 | // matched is the unreliable-Enter failure PrimesFirstResult exists to |
| 342 | // avoid. search.js targets rst-result-title, so the guard is that the |
| 343 | // box's link never wears it. |
| 344 | func TestTheFactBoxCitationIsNotAnEnterTarget(t *testing.T) { |
| 345 | h := newApp(t) |
| 346 | store(t, h, "Tito office", "## Contact\n\nOffice phone: +353 (0)1 547 7854") |
| 347 | body := search(t, h, "phone number") |
| 348 | if !hasFactBox(body) { |
| 349 | t.Fatal("no box to check: this assertion needs one to be wrong about") |
| 350 | } |
| 351 | box := body[regexp.MustCompile(`<div rst-fact-box[ >]`).FindStringIndex(body)[0]:] |
| 352 | box = box[:strings.Index(box, "</div>")] |
| 353 | if strings.Contains(box, "rst-result-title") { |
| 354 | t.Errorf("the box carries search.js's Enter target:\n%s", box) |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | // **The reachability limit, pinned rather than discovered.** |
| 359 | // |
| 360 | // §8.12 (Paul, 2026-10-27) built the lift this test used to wait for. |
| 361 | // Until then it pinned §8.8's limit — a contact page that writes |
| 362 | // "Office: +353 …" and never the word "phone" had no box for "phone |
| 363 | // number" — and said it would go red, and be rewritten on purpose, the |
| 364 | // day the lift was built. It went red on 2026-10-27 and this is the |
| 365 | // rewrite: the same document now answers the query that names its kind, |
| 366 | // and still answers the query that shares a word with it. |
| 367 | // |
| 368 | // The box still sees only what retrieval returned; §8.12 changes what |
| 369 | // retrieval returns, not where the box looks. |
| 370 | func TestTheBoxReachesAKindTheQueryNames(t *testing.T) { |
| 371 | const body = "## Contact\n\nOffice: +353 (0)1 547 7854" |
| 372 | |
| 373 | h := newApp(t) |
| 374 | store(t, h, "Tito office", body) |
| 375 | |
| 376 | // "phone number" appears nowhere in that document; it holds a phone |
| 377 | // number, and §8.12's kind token reaches it. |
| 378 | if got := factValues(search(t, h, "phone number")); len(got) != 1 { |
| 379 | t.Errorf("values = %q, want the number: the document holds a phone number", got) |
| 380 | } |
| 381 | // One shared word — "office" — and the same fact, as before. |
| 382 | if got := factValues(search(t, h, "office phone number")); len(got) != 1 { |
| 383 | t.Fatalf("values = %q, want the number: the query shares a word with the document", got) |
| 384 | } |
| 385 | // And a query naming no kind and sharing no word still finds nothing. |
| 386 | if got := search(t, h, "zebrafinch"); hasFactBox(got) { |
| 387 | t.Errorf("a box appeared for a query that reaches nothing: %q", factValues(got)) |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | var factNoteRe = regexp.MustCompile(`<p rst-fact-note>([^<]*)</p>`) |
| 392 | |
| 393 | // The page that motivated §8.8's narrowing (Paul, 2026-09-29), through |
| 394 | // the real handler and template, on the page and in the fragment |
| 395 | // search.js swaps. Gate 40 holds the rule at store level; this holds that |
| 396 | // the row and its line reach the reader, and that the bullet is gone. |
| 397 | func TestTheNarrowedBoxReachesThePageWithItsLine(t *testing.T) { |
| 398 | h := newApp(t) |
| 399 | store(t, h, "Office", "## Office Address\n\n"+ |
| 400 | "- Turn the knob\n"+ |
| 401 | "- The office phone number is: +353 (0)1 547 7854\n"+ |
| 402 | "- We also have the following unused phone numbers: +353 (0)1 598 1234\n"+ |
| 403 | " - +353 (0)1 598 1235\n"+ |
| 404 | " - +353 (0)1 598 1236\n") |
| 405 | q := "what is the office phone number?" |
| 406 | for name, body := range map[string]string{ |
| 407 | "page": search(t, h, q), |
| 408 | "fragment": get(t, h, "/search?q="+url.QueryEscape(q)).Body.String(), |
| 409 | } { |
| 410 | if got := factValues(body); len(got) != 1 || got[0] != "+353 (0)1 547 7854" { |
| 411 | t.Fatalf("%s: values = %q, want the office number alone", name, got) |
| 412 | } |
| 413 | if got := factLabels(body); len(got) != 1 || got[0] != "The office phone number is" { |
| 414 | t.Errorf("%s: labels = %q, want the document's own without its bullet", name, got) |
| 415 | } |
| 416 | want := "Chosen because its label says “office”. 3 other phone numbers found." |
| 417 | if got := capture(factNoteRe, body); len(got) != 1 || got[0] != want { |
| 418 | t.Errorf("%s: note = %q, want %q", name, got, want) |
| 419 | } |
| 420 | // The citation survives narrowing: title, trail and a link to the |
| 421 | // passage, not just the value. |
| 422 | if !regexp.MustCompile(`<a href="/documents/\d+#p\d+" rst-fact-cite>Office › Office Address</a>`).MatchString(body) { |
| 423 | t.Errorf("%s: the narrowed row's citation is missing or wrong:\n%s", name, body) |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // The note sits under the row it describes, not under whichever kind |
| 429 | // renders last. Phone renders before VAT, so a note placed after every |
| 430 | // row would sit under the VAT number and describe the wrong thing. |
| 431 | // |
| 432 | // MUTATION-TESTED 2026-09-29, each reverted: the note rendered after all |
| 433 | // rows (Astra's finding) → "order is phone 2432, note 2818, vat 2668"; |
| 434 | // the note rendered above its row → "phone 2525, note 2362". |
| 435 | func TestTheNarrowingLineSitsUnderItsOwnRow(t *testing.T) { |
| 436 | h := newApp(t) |
| 437 | store(t, h, "Office", "## Office\n\nOffice phone: +353 (0)1 547 0001\n\nReception: +353 (0)1 547 0002\n\n"+ |
| 438 | "Sales: +353 (0)1 547 0003\n\nFax: +353 (0)1 547 0004\n\nOffice VAT: IE3384527RH") |
| 439 | body := search(t, h, "office phone and vat") |
| 440 | if got := factValues(body); len(got) != 2 { |
| 441 | t.Fatalf("values = %q, want the narrowed phone and the VAT number", got) |
| 442 | } |
| 443 | phone := strings.Index(body, "<span rst-fact-value rst-nowrap>+353 (0)1 547 0001</span>") |
| 444 | note := strings.Index(body, "<p rst-fact-note>") |
| 445 | vat := strings.Index(body, "<span rst-fact-value rst-nowrap>IE3384527RH</span>") |
| 446 | if phone < 0 || note < 0 || vat < 0 { |
| 447 | t.Fatalf("phone %d, note %d, vat %d: something is missing:\n%s", phone, note, vat, body) |
| 448 | } |
| 449 | if !(phone < note && note < vat) { |
| 450 | t.Errorf("order is phone %d, note %d, vat %d; the note must sit between its row and the next", phone, note, vat) |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | // ?explain=facts renders the box's account only when the URL asks, on |
| 455 | // the page and in the fragment, and never otherwise. |
| 456 | // |
| 457 | // MUTATION-TESTED 2026-10-26 (GPT-6 Astra's, which passed before these |
| 458 | // assertions): the candidate table deleted from the template → "the |
| 459 | // disclosure does not carry its table"; any non-empty explain value |
| 460 | // accepted → "explain=1 shows the explanation". |
| 461 | func TestExplainRendersOnlyWhenAsked(t *testing.T) { |
| 462 | h := newApp(t) |
| 463 | store(t, h, "Tito office", "## Contact\n\nOffice phone: +353 (0)1 547 7854") |
| 464 | q := url.QueryEscape("phone number") |
| 465 | disclosure := regexp.MustCompile(`(?s)<details rst-fact-explain open>.*?</details>`) |
| 466 | for _, path := range []string{"/?q=" + q, "/search?q=" + q} { |
| 467 | for _, extra := range []string{"", "&explain=1", "&explain=fact"} { |
| 468 | if body := get(t, h, path+extra).Body.String(); strings.Contains(body, "rst-fact-explain") { |
| 469 | t.Errorf("%s%s shows the explanation without being asked", path, extra) |
| 470 | } |
| 471 | } |
| 472 | d := disclosure.FindString(get(t, h, path+"&explain=facts").Body.String()) |
| 473 | if d == "" { |
| 474 | t.Fatalf("%s&explain=facts has no explanation", path) |
| 475 | } |
| 476 | for _, want := range []string{ |
| 477 | "<strong>phone</strong>: Rendered 1.", |
| 478 | "<td>+353 (0)1 547 7854</td>", |
| 479 | "<td>Office phone</td>", |
| 480 | "<td>yes</td>", |
| 481 | "Tito office › Contact", |
| 482 | } { |
| 483 | if !strings.Contains(d, want) { |
| 484 | t.Errorf("%s: the disclosure does not carry its table: missing %q in\n%s", path, want, d) |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | // GATE 41/42 (page half) — §8.10's card, link, correction page and |
| 491 | // reports, through the real routes, templates and the CSRF check. |
| 492 | func TestTheAnswerCardAndCorrectionsOnThePage(t *testing.T) { |
| 493 | h := newApp(t) |
| 494 | store(t, h, "Office", "## Office Address\n\nThe office phone number is:\n+353 (0)1 547 7854\n"+ |
| 495 | "We also have the following unused phone numbers:\n+353 (0)1 598 5125\n+353 (0)1 515 7175\n+353 (0)1 534 6069") |
| 496 | q := "office phone number" |
| 497 | body := search(t, h, q) |
| 498 | if !strings.Contains(body, "<div rst-fact-box rst-fact-one>") { |
| 499 | t.Errorf("one answer is not the large card:\n%s", body) |
| 500 | } |
| 501 | if !strings.Contains(body, `<p rst-fact-report><a href="/facts/correct?q=office%20phone%20number">Is this wrong?</a></p>`) { |
| 502 | t.Errorf("no \"Is this wrong?\" link for the query:\n%s", body) |
| 503 | } |
| 504 | |
| 505 | page := get(t, h, "/facts/correct?q="+url.QueryEscape(q)).Body.String() |
| 506 | handles := regexp.MustCompile(`name="choice" value="([^"]*)"`).FindAllStringSubmatch(page, -1) |
| 507 | if len(handles) != 5 { |
| 508 | t.Fatalf("%d choices on the correction page, want four numbers and none of these:\n%s", len(handles), page) |
| 509 | } |
| 510 | // Each value is an opaque handle — 64 hex characters, an HMAC — or |
| 511 | // "none". A digit test would flag a legitimate handle that happens to |
| 512 | // contain "353"; the shape is what rules out a carried value. |
| 513 | opaque := regexp.MustCompile(`^(?:[0-9a-f]{64}|none)$`) |
| 514 | for _, m := range handles { |
| 515 | if !opaque.MatchString(m[1]) { |
| 516 | t.Errorf("a radio value is not an opaque handle: %q", m[1]) |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | // A POST without the same-origin header is refused and stores nothing. |
| 521 | form := url.Values{"q": {q}, "kind": {"phone"}, "choice": {handles[1][1]}, "note": {"unused"}} |
| 522 | req := httptest.NewRequest(http.MethodPost, "/facts/correct", strings.NewReader(form.Encode())) |
| 523 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 524 | rec := httptest.NewRecorder() |
| 525 | h.ServeHTTP(rec, req) |
| 526 | if rec.Code < 400 { |
| 527 | t.Errorf("a cross-origin POST answered %d", rec.Code) |
| 528 | } |
| 529 | if strings.Contains(get(t, h, "/reports").Body.String(), "unused") { |
| 530 | t.Fatal("a cross-origin POST stored a report") |
| 531 | } |
| 532 | |
| 533 | // The real POST: stored, redirected with thanks, listed. |
| 534 | res := post(t, h, "/facts/correct", form) |
| 535 | if res.Code != http.StatusSeeOther { |
| 536 | t.Fatalf("POST answered %d, want 303:\n%s", res.Code, res.Body.String()) |
| 537 | } |
| 538 | reports := get(t, h, "/reports").Body.String() |
| 539 | if !strings.Contains(reports, "unused") || !strings.Contains(reports, "What you picked") { |
| 540 | t.Errorf("/reports does not list the correction:\n%s", reports) |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | // A card holding only a model row has no "Is this wrong?": model kinds |
| 545 | // are not correctable (§8.10). |
| 546 | func TestAModelOnlyCardHasNoCorrectionLink(t *testing.T) { |
| 547 | h, _ := appWithModel(t, &countingModel{}) |
| 548 | body := search(t, h, "office address") |
| 549 | if !strings.Contains(body, "rst-fact-model") { |
| 550 | t.Fatalf("no model row, so the absence below proves nothing:\n%s", body) |
| 551 | } |
| 552 | if strings.Contains(body, "/facts/correct") { |
| 553 | t.Error("a model-only card links to the correction page") |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | // §8.10's routes are inside the session gate. TestNoUndeclaredRoutes |
| 558 | // checks a route is declared, not where it is mounted. The production app |
| 559 | // here has no sign-in wired, and requireSession fails closed on that with |
| 560 | // its own answer ("sign-in is not configured", 500) — the same answer "/" |
| 561 | // gets — so seeing that answer proves the gate ran. A route mounted |
| 562 | // outside it reaches the handler instead, which answers 404. |
| 563 | // |
| 564 | // MUTATION-TESTED 2026-10-26, reverted: POST /facts/correct mounted on the |
| 565 | // outer router beside /appearance → "POST /facts/correct answered 404 |
| 566 | // signed out; requireSession did not run". |
| 567 | func TestCorrectionRoutesNeedASession(t *testing.T) { |
| 568 | h := newProductionApp(t) |
| 569 | for _, c := range []struct{ method, path string }{ |
| 570 | {http.MethodGet, "/"}, |
| 571 | {http.MethodGet, "/facts/correct?q=office"}, |
| 572 | {http.MethodPost, "/facts/correct"}, |
| 573 | {http.MethodGet, "/reports"}, |
| 574 | {http.MethodPost, "/reports/1/delete"}, |
| 575 | } { |
| 576 | req := httptest.NewRequest(c.method, c.path, strings.NewReader("q=office&kind=phone&choice=none")) |
| 577 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 578 | req.Header.Set("Origin", testOrigin) |
| 579 | rec := httptest.NewRecorder() |
| 580 | h.ServeHTTP(rec, req) |
| 581 | if rec.Code != http.StatusInternalServerError || !strings.Contains(rec.Body.String(), "sign-in is not configured") { |
| 582 | t.Errorf("%s %s answered %d signed out; requireSession did not run", c.method, c.path, rec.Code) |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |