| 1 | package memoria |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/suite" |
| 9 | ) |
| 10 | |
| 11 | // ?explain=facts (§8.8, 2026-10-26): the box's account of itself, so the |
| 12 | // owner can see why it did or did not appear on data nobody else reads. |
| 13 | // Asserts the struct: the outcome sentence and each candidate in full. |
| 14 | // |
| 15 | // MUTATION-TESTED 2026-10-26, GPT-6 Astra's, both green before the full |
| 16 | // tuples and the kinds case: Label, Title and Trail left blank → |
| 17 | // "candidates = …"; every kind explained, asked for or not → "explained |
| 18 | // kinds = [eircode phone iban vat email]". |
| 19 | func TestExplainSaysWhichRuleDecided(t *testing.T) { |
| 20 | roster := docsRoster() |
| 21 | visible := func() Custody { return &perItemCustody{apps: roster} } |
| 22 | page := func(t *testing.T, body string) *Store { |
| 23 | return factCorpus(t, visible(), [][3]string{{"a", "Contacts", body}}) |
| 24 | } |
| 25 | phoneExplained := func(t *testing.T, p Page) FactExplanation { |
| 26 | t.Helper() |
| 27 | for _, e := range p.Explained { |
| 28 | if e.Kind == "phone" { |
| 29 | return e |
| 30 | } |
| 31 | } |
| 32 | t.Fatalf("no phone explanation in %+v", p.Explained) |
| 33 | return FactExplanation{} |
| 34 | } |
| 35 | starts := func(t *testing.T, e FactExplanation, prefix string) { |
| 36 | t.Helper() |
| 37 | if !strings.HasPrefix(e.Outcome, prefix) { |
| 38 | t.Errorf("outcome = %q, want it to start %q", e.Outcome, prefix) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | t.Run("over the cap with no narrowing words", func(t *testing.T) { |
| 43 | s := factCorpus(t, visible(), [][3]string{{"o", "Office", officePage}}) |
| 44 | e := phoneExplained(t, searchFacts(t, s, "phone number")) |
| 45 | starts(t, e, "4 candidates is over the cap of 3, and the query has no other words") |
| 46 | // Every field of every candidate: the diagnostic is only as good |
| 47 | // as what it shows, and blank labels or citations would pass a |
| 48 | // test of the flags alone. |
| 49 | at := func(span, label string, own bool) ExplainedCandidate { |
| 50 | return ExplainedCandidate{Span: span, Label: label, Title: "Office", Trail: "Office Address", Own: own} |
| 51 | } |
| 52 | want := []ExplainedCandidate{ |
| 53 | at("+353 (0)1 547 7854", "The office phone number is", true), |
| 54 | at("+353 (0)1 598 1234", "We also have the following unused phone numbers", true), |
| 55 | at("+353 (0)1 598 1235", "Office Address", false), |
| 56 | at("+353 (0)1 598 1236", "Office Address", false), |
| 57 | } |
| 58 | if !reflect.DeepEqual(e.Candidates, want) { |
| 59 | t.Errorf("candidates =\n%+v\nwant\n%+v", e.Candidates, want) |
| 60 | } |
| 61 | }) |
| 62 | |
| 63 | t.Run("narrowed", func(t *testing.T) { |
| 64 | s := factCorpus(t, visible(), [][3]string{{"o", "Office", officePage}}) |
| 65 | p := searchFacts(t, s, "what is the office phone number?") |
| 66 | e := phoneExplained(t, p) |
| 67 | starts(t, e, "Narrowed to one row.") |
| 68 | if !reflect.DeepEqual(e.Qualifiers, []string{"office"}) { |
| 69 | t.Errorf("qualifiers = %q, want [office]", e.Qualifiers) |
| 70 | } |
| 71 | var matches []bool |
| 72 | for _, c := range e.Candidates { |
| 73 | matches = append(matches, c.Matches) |
| 74 | } |
| 75 | if !reflect.DeepEqual(matches, []bool{true, false, false, false}) { |
| 76 | t.Errorf("matches = %v, want only the office number", matches) |
| 77 | } |
| 78 | }) |
| 79 | |
| 80 | t.Run("two matches", func(t *testing.T) { |
| 81 | s := page(t, "## Office\n\nOffice phone: +353 (0)1 547 0001\n\nOffice line: +353 (0)1 547 0002\n\n"+ |
| 82 | "Reception: +353 (0)1 547 0003\n\nSales: +353 (0)1 547 0004") |
| 83 | starts(t, phoneExplained(t, searchFacts(t, s, "office phone")), "4 candidates is over the cap of 3, and 2 own labels match") |
| 84 | }) |
| 85 | |
| 86 | t.Run("no match", func(t *testing.T) { |
| 87 | s := page(t, "## Office\n\nReception: +353 (0)1 547 0001\n\nSales: +353 (0)1 547 0002\n\n"+ |
| 88 | "Fax: +353 (0)1 547 0003\n\nSupport: +353 (0)1 547 0004") |
| 89 | starts(t, phoneExplained(t, searchFacts(t, s, "office phone")), "4 candidates is over the cap of 3, and no candidate's own label") |
| 90 | }) |
| 91 | |
| 92 | t.Run("collision", func(t *testing.T) { |
| 93 | s := page(t, "## Office phones\n\nOffice: +353 (0)1 547 0001\n\nOffice: +353 (0)1 547 0002") |
| 94 | starts(t, phoneExplained(t, searchFacts(t, s, "phone")), "Two candidates share a title, trail and label") |
| 95 | }) |
| 96 | |
| 97 | t.Run("over-long", func(t *testing.T) { |
| 98 | s := page(t, "## Office phones\n\nThe number you must never give to anyone at all "+strings.Repeat("x", 40)+": +353 (0)1 547 0001") |
| 99 | e := phoneExplained(t, searchFacts(t, s, "phone")) |
| 100 | starts(t, e, "A candidate's label is too long to show") |
| 101 | want := []ExplainedCandidate{{ |
| 102 | Span: "+353 (0)1 547 0001", Label: "The number you must never give to anyone at all " + strings.Repeat("x", 40), |
| 103 | Title: "Contacts", Trail: "Office phones", Own: true, OverLong: true, |
| 104 | }} |
| 105 | if !reflect.DeepEqual(e.Candidates, want) { |
| 106 | t.Errorf("candidates = %+v, want %+v", e.Candidates, want) |
| 107 | } |
| 108 | }) |
| 109 | |
| 110 | t.Run("rendered", func(t *testing.T) { |
| 111 | s := page(t, "## Office\n\nOffice phone: +353 (0)1 547 0001") |
| 112 | starts(t, phoneExplained(t, searchFacts(t, s, "phone")), "Rendered 1.") |
| 113 | }) |
| 114 | |
| 115 | t.Run("no candidates", func(t *testing.T) { |
| 116 | s := page(t, "## Office\n\nNo number here, only a phone mentioned.") |
| 117 | starts(t, phoneExplained(t, searchFacts(t, s, "phone")), "No candidates in these results.") |
| 118 | }) |
| 119 | |
| 120 | // Only the kinds the query named are explained. A page carrying an |
| 121 | // email address, searched for a phone, says nothing about email. |
| 122 | t.Run("only the kinds asked for", func(t *testing.T) { |
| 123 | s := page(t, "## Office phones\n\nOffice phone: +353 (0)1 547 0001\n\nOffice email: office@tito.io") |
| 124 | var kinds []string |
| 125 | for _, e := range searchFacts(t, s, "office phone").Explained { |
| 126 | kinds = append(kinds, e.Kind) |
| 127 | } |
| 128 | if !reflect.DeepEqual(kinds, []string{"phone"}) { |
| 129 | t.Errorf("explained kinds = %q, want [phone]", kinds) |
| 130 | } |
| 131 | }) |
| 132 | |
| 133 | // The explanation is built from the ruled results only, like the box. |
| 134 | // A withheld document's value and label must not appear in it, and |
| 135 | // must not be counted in its sentence. |
| 136 | t.Run("a withheld document is not explained", func(t *testing.T) { |
| 137 | s := factCorpus(t, &perItemCustody{ |
| 138 | apps: roster, |
| 139 | give: map[string]suite.Authorization{"b": suite.NOT_VISIBLE}, |
| 140 | }, [][3]string{ |
| 141 | {"a", "Dublin", "## Dublin office\n\nOffice phone: +353 (0)1 547 0001"}, |
| 142 | {"b", "Cork", "## Cork office\n\nSecret line: +353 (0)21 547 0002"}, |
| 143 | }) |
| 144 | e := phoneExplained(t, searchFacts(t, s, "office phone")) |
| 145 | if len(e.Candidates) != 1 || e.Candidates[0].Span != "+353 (0)1 547 0001" { |
| 146 | t.Errorf("candidates = %+v, want only the visible document's", e.Candidates) |
| 147 | } |
| 148 | starts(t, e, "Rendered 1.") |
| 149 | }) |
| 150 | } |
| 151 | |