| 1 | package memoria |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/sha256" |
| 6 | "encoding/hex" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "reflect" |
| 10 | "sort" |
| 11 | "strconv" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "amadan.net/oficina/suite" |
| 16 | |
| 17 | "amadan.net/oficina/memoria/internal/memoria/seal" |
| 18 | ) |
| 19 | |
| 20 | // GATE 42 (store half) — §8.10's corrections. |
| 21 | // |
| 22 | // MUTATION-TESTED 2026-10-26, each built and seen red, each reverted: a |
| 23 | // span added to the stored coordinate; the reporter print an unkeyed |
| 24 | // SHA-256; the handle check skipped; the revision left out of the |
| 25 | // handle; the correction search unruled; Reports and DeleteReport each |
| 26 | // ignoring the print; the correction search run with the model step. |
| 27 | // Then GPT-6 Astra's, all green before the assertions written for them: |
| 28 | // zeroed span offsets; "shown" decided without the span; the query left |
| 29 | // out of the handle; DeleteReport deleting all of a person's reports; |
| 30 | // links to the next passage; the notices dropped. |
| 31 | func TestGate42Corrections(t *testing.T) { |
| 32 | roster := docsRoster() |
| 33 | paul := suite.Subject{Kind: suite.SubjectAddress, Value: "paul@tito.io"} |
| 34 | other := suite.Subject{Kind: suite.SubjectAddress, Value: "someone@tito.io"} |
| 35 | ctx := context.Background() |
| 36 | const q = "office phone number" |
| 37 | corpus := func(t *testing.T, cus Custody) *Store { |
| 38 | return factCorpus(t, cus, [][3]string{ |
| 39 | {"a", "Office", "## Office Address\n\nThe office phone number is:\n+353 (0)1 547 7854\n" + |
| 40 | "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"}, |
| 41 | }) |
| 42 | } |
| 43 | visible := func() Custody { return &perItemCustody{apps: roster} } |
| 44 | phones := func(t *testing.T, s *Store, who suite.Subject) CorrectionKind { |
| 45 | t.Helper() |
| 46 | kinds, _, err := s.SearchForCorrection(ctx, who, q) |
| 47 | if err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | for _, k := range kinds { |
| 51 | if k.Kind == "phone" { |
| 52 | return k |
| 53 | } |
| 54 | } |
| 55 | t.Fatalf("no phone candidates in %+v", kinds) |
| 56 | return CorrectionKind{} |
| 57 | } |
| 58 | rows := func(t *testing.T, s *Store) []FactReport { |
| 59 | t.Helper() |
| 60 | var out []FactReport |
| 61 | if err := s.db.G.Find(&out).Error; err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | return out |
| 65 | } |
| 66 | |
| 67 | // PRESENCE: a correction is stored, listed, and its payload holds |
| 68 | // coordinates and the person's own words and nothing else. |
| 69 | // Mutation: a span copied into the payload → "payload fields". |
| 70 | t.Run("a correction is stored, listed and sealed as coordinates", func(t *testing.T) { |
| 71 | s := corpus(t, visible()) |
| 72 | k := phones(t, s, paul) |
| 73 | if len(k.Items) != 4 { |
| 74 | t.Fatalf("%d phone candidates, want 4", len(k.Items)) |
| 75 | } |
| 76 | var pick CorrectionItem |
| 77 | shown := 0 |
| 78 | for _, it := range k.Items { |
| 79 | if strings.Contains(it.Handle, it.Span) || strings.Contains(it.Handle, "+") || strings.Contains(it.Handle, " ") { |
| 80 | t.Errorf("a handle carries the value: %q", it.Handle) |
| 81 | } |
| 82 | if it.Shown { |
| 83 | shown++ |
| 84 | if it.Span != "+353 (0)1 547 7854" { |
| 85 | t.Errorf("%q is marked shown; the card showed the office number", it.Span) |
| 86 | } |
| 87 | } |
| 88 | if it.Span == "+353 (0)1 598 5125" { |
| 89 | pick = it |
| 90 | } |
| 91 | } |
| 92 | // All four sit in one passage, so "shown" decided by document and |
| 93 | // passage alone marks every one of them. |
| 94 | if shown != 1 { |
| 95 | t.Errorf("%d candidates marked shown, want exactly the one the card showed", shown) |
| 96 | } |
| 97 | if err := s.SaveCorrection(ctx, paul, q, "phone", pick.Handle, "That is an unused line"); err != nil { |
| 98 | t.Fatal(err) |
| 99 | } |
| 100 | got := rows(t, s) |
| 101 | if len(got) != 1 { |
| 102 | t.Fatalf("%d rows stored, want 1", len(got)) |
| 103 | } |
| 104 | for _, plain := range []string{q, "unused line", "+353", "547 7854", "The office phone number is", "Office Address"} { |
| 105 | if strings.Contains(string(got[0].SealedReport), plain) || strings.Contains(got[0].ReporterPrint, plain) { |
| 106 | t.Errorf("the stored row carries %q in plaintext", plain) |
| 107 | } |
| 108 | } |
| 109 | raw, err := s.sealer.Open(seal.PurposeReport, got[0].SealedReport) |
| 110 | if err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | var fields map[string]json.RawMessage |
| 114 | if err := json.Unmarshal(raw, &fields); err != nil { |
| 115 | t.Fatal(err) |
| 116 | } |
| 117 | var keys []string |
| 118 | for k := range fields { |
| 119 | keys = append(keys, k) |
| 120 | } |
| 121 | sort.Strings(keys) |
| 122 | if want := []string{"chosen", "kind", "listed", "note", "qualifiers", "query", "shown"}; !reflect.DeepEqual(keys, want) { |
| 123 | t.Errorf("payload fields = %q, want %q", keys, want) |
| 124 | } |
| 125 | var coord map[string]json.RawMessage |
| 126 | if err := json.Unmarshal(fields["chosen"], &coord); err != nil { |
| 127 | t.Fatal(err) |
| 128 | } |
| 129 | // The coordinate says WHICH occurrence: real offsets, the span's |
| 130 | // length apart, not zeroes. |
| 131 | var at ReportCoord |
| 132 | if err := json.Unmarshal(fields["chosen"], &at); err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | if at.Start <= 0 || at.End-at.Start != len("+353 (0)1 598 5125") { |
| 136 | t.Errorf("chosen offsets = %d..%d, want the span's own", at.Start, at.End) |
| 137 | } |
| 138 | var ckeys []string |
| 139 | for k := range coord { |
| 140 | ckeys = append(ckeys, k) |
| 141 | } |
| 142 | sort.Strings(ckeys) |
| 143 | if want := []string{"chunk_id", "document_id", "end", "ordinal", "revision", "start"}; !reflect.DeepEqual(ckeys, want) { |
| 144 | t.Errorf("coordinate fields = %q, want %q", ckeys, want) |
| 145 | } |
| 146 | for _, text := range []string{"+353", "547", "598 5125", "office phone number is", "Office Address"} { |
| 147 | if strings.Contains(string(fields["chosen"])+string(fields["shown"])+string(fields["listed"]), text) { |
| 148 | t.Errorf("a coordinate carries passage text %q", text) |
| 149 | } |
| 150 | } |
| 151 | list, err := s.Reports(ctx, paul) |
| 152 | if err != nil { |
| 153 | t.Fatal(err) |
| 154 | } |
| 155 | if len(list) != 1 || list[0].Query != q || list[0].Note != "That is an unused line" || list[0].Chosen == nil || list[0].None { |
| 156 | t.Fatalf("reports = %+v", list) |
| 157 | } |
| 158 | var doc Document |
| 159 | if err := s.db.G.First(&doc).Error; err != nil { |
| 160 | t.Fatal(err) |
| 161 | } |
| 162 | if want := "/documents/" + strconv.FormatInt(doc.ID, 10) + "#p" + strconv.Itoa(at.Ordinal); list[0].Chosen.Href != want || list[0].Chosen.Status != "" { |
| 163 | t.Errorf("chosen link = %+v, want %s", list[0].Chosen, want) |
| 164 | } |
| 165 | if at.Ordinal != 0 { |
| 166 | t.Errorf("chosen ordinal = %d; the one-passage document's passage is 0", at.Ordinal) |
| 167 | } |
| 168 | if list[0].Shown == nil { |
| 169 | t.Error("nothing recorded as shown, but the box showed the office number") |
| 170 | } |
| 171 | }) |
| 172 | |
| 173 | // Mutation: the reporter print made from an unkeyed SHA-256 → red. |
| 174 | t.Run("the reporter print is keyed", func(t *testing.T) { |
| 175 | s := corpus(t, visible()) |
| 176 | k := phones(t, s, paul) |
| 177 | if err := s.SaveCorrection(ctx, paul, q, "phone", "none", ""); err != nil { |
| 178 | t.Fatal(err) |
| 179 | } |
| 180 | got := rows(t, s)[0].ReporterPrint |
| 181 | sum := sha256.Sum256([]byte(paul.Value)) |
| 182 | if got == paul.Value || got == hex.EncodeToString(sum[:]) || got == "" { |
| 183 | t.Errorf("reporter print %q is the address or an unkeyed hash of it", got) |
| 184 | } |
| 185 | _ = k |
| 186 | }) |
| 187 | |
| 188 | // Mutation: SaveCorrection skipping the handle check → each red. |
| 189 | t.Run("a forged or foreign handle stores nothing", func(t *testing.T) { |
| 190 | s := corpus(t, visible()) |
| 191 | theirs := phones(t, s, other).Items[0].Handle |
| 192 | for name, h := range map[string]string{"forged": "deadbeef", "another person's": theirs} { |
| 193 | if err := s.SaveCorrection(ctx, paul, q, "phone", h, ""); !errors.Is(err, ErrChoiceGone) { |
| 194 | t.Errorf("%s handle: err = %v, want ErrChoiceGone", name, err) |
| 195 | } |
| 196 | } |
| 197 | if n := len(rows(t, s)); n != 0 { |
| 198 | t.Errorf("%d rows stored, want none", n) |
| 199 | } |
| 200 | }) |
| 201 | |
| 202 | // A handle is bound to its query. Mutation: the query left out of the |
| 203 | // handle → red, because the other query has the same candidates. |
| 204 | t.Run("a handle from another query stores nothing", func(t *testing.T) { |
| 205 | s := corpus(t, visible()) |
| 206 | h := phones(t, s, paul).Items[0].Handle |
| 207 | if err := s.SaveCorrection(ctx, paul, "phone number office", "phone", h, ""); !errors.Is(err, ErrChoiceGone) { |
| 208 | t.Errorf("err = %v, want ErrChoiceGone", err) |
| 209 | } |
| 210 | if n := len(rows(t, s)); n != 0 { |
| 211 | t.Errorf("%d rows stored, want none", n) |
| 212 | } |
| 213 | }) |
| 214 | |
| 215 | // Mutation: the revision dropped from the handle → red. |
| 216 | t.Run("a document revised since the page stores nothing", func(t *testing.T) { |
| 217 | s := corpus(t, visible()) |
| 218 | h := phones(t, s, paul).Items[0].Handle |
| 219 | var d Document |
| 220 | if err := s.db.G.First(&d).Error; err != nil { |
| 221 | t.Fatal(err) |
| 222 | } |
| 223 | if err := s.db.G.Model(&Document{}).Where("id = ?", d.ID).Update("revision", d.Revision+1).Error; err != nil { |
| 224 | t.Fatal(err) |
| 225 | } |
| 226 | if err := s.SaveCorrection(ctx, paul, q, "phone", h, ""); !errors.Is(err, ErrChoiceGone) { |
| 227 | t.Errorf("err = %v, want ErrChoiceGone", err) |
| 228 | } |
| 229 | if n := len(rows(t, s)); n != 0 { |
| 230 | t.Errorf("%d rows stored, want none", n) |
| 231 | } |
| 232 | }) |
| 233 | |
| 234 | // Fresh authorization. Mutation: SaveCorrection validating against |
| 235 | // handles computed without the ruling → red. |
| 236 | t.Run("visible at GET, withheld before POST, stores nothing", func(t *testing.T) { |
| 237 | cus := &perItemCustody{apps: roster} |
| 238 | s := corpus(t, cus) |
| 239 | h := phones(t, s, paul).Items[0].Handle |
| 240 | cus.give = map[string]suite.Authorization{"a": suite.NOT_VISIBLE} |
| 241 | if err := s.SaveCorrection(ctx, paul, q, "phone", h, ""); !errors.Is(err, ErrChoiceGone) { |
| 242 | t.Errorf("err = %v, want ErrChoiceGone", err) |
| 243 | } |
| 244 | if n := len(rows(t, s)); n != 0 { |
| 245 | t.Errorf("%d rows stored, want none", n) |
| 246 | } |
| 247 | }) |
| 248 | |
| 249 | // §4.1's loud withholding reaches the correction page. Mutation: the |
| 250 | // notices dropped → red. |
| 251 | t.Run("an UNKNOWN ruling is said on the correction page", func(t *testing.T) { |
| 252 | s := factCorpus(t, &perItemCustody{apps: roster, give: map[string]suite.Authorization{"b": suite.UNKNOWN}}, [][3]string{ |
| 253 | {"a", "Office", "## Office\n\nOffice phone: +353 (0)1 547 7854"}, |
| 254 | {"b", "Cork", "## Cork office\n\nOffice phone: +353 (0)21 547 0002"}, |
| 255 | }) |
| 256 | kinds, notices, err := s.SearchForCorrection(ctx, paul, "office phone") |
| 257 | if err != nil { |
| 258 | t.Fatal(err) |
| 259 | } |
| 260 | if len(kinds) == 0 { |
| 261 | t.Fatal("no candidates; the visible document should still be listed") |
| 262 | } |
| 263 | if len(notices) == 0 { |
| 264 | t.Error("a withheld passage left no notice: the list reads as whole") |
| 265 | } |
| 266 | }) |
| 267 | |
| 268 | // Mutation: Reports or DeleteReport ignoring the print → each red. |
| 269 | t.Run("only the reporter sees or deletes a report", func(t *testing.T) { |
| 270 | s := corpus(t, visible()) |
| 271 | if err := s.SaveCorrection(ctx, paul, q, "phone", "none", "mine"); err != nil { |
| 272 | t.Fatal(err) |
| 273 | } |
| 274 | if list, _ := s.Reports(ctx, other); len(list) != 0 { |
| 275 | t.Errorf("another person sees %d of Paul's reports", len(list)) |
| 276 | } |
| 277 | id := rows(t, s)[0].ID |
| 278 | if err := s.DeleteReport(ctx, other, id); !errors.Is(err, ErrReportNotFound) { |
| 279 | t.Errorf("another person deleting: err = %v, want ErrReportNotFound", err) |
| 280 | } |
| 281 | if n := len(rows(t, s)); n != 1 { |
| 282 | t.Fatalf("%d rows after a refused delete, want 1", n) |
| 283 | } |
| 284 | if err := s.SaveCorrection(ctx, paul, q, "phone", "none", "second"); err != nil { |
| 285 | t.Fatal(err) |
| 286 | } |
| 287 | if err := s.DeleteReport(ctx, paul, id); err != nil { |
| 288 | t.Fatal(err) |
| 289 | } |
| 290 | // Mutation: DeleteReport dropping the id → every report goes. |
| 291 | if left := rows(t, s); len(left) != 1 || left[0].ID == id { |
| 292 | t.Errorf("after deleting one of two, %d rows remain (want the other one)", len(left)) |
| 293 | } |
| 294 | }) |
| 295 | |
| 296 | // Mutation: SearchForCorrection using modeFull → red. |
| 297 | t.Run("a mixed query makes no model calls", func(t *testing.T) { |
| 298 | s := factCorpus(t, visible(), [][3]string{ |
| 299 | {"a", "Office", "## Contact\n\nOffice phone: +353 (0)1 547 7854\n\nOffice address: 12 Main Street, Dublin 2"}, |
| 300 | }) |
| 301 | rec := &recorder{reply: point(0, "12 Main Street, Dublin 2")} |
| 302 | s.extractor = rec |
| 303 | mixed := "office phone and postal address" |
| 304 | kinds, _, err := s.SearchForCorrection(ctx, paul, mixed) |
| 305 | if err != nil { |
| 306 | t.Fatal(err) |
| 307 | } |
| 308 | if len(kinds) == 0 { |
| 309 | t.Fatal("no candidates, so zero calls proves nothing") |
| 310 | } |
| 311 | for _, k := range kinds { |
| 312 | if k.Kind == "address" { |
| 313 | t.Error("a model kind is offered for correction") |
| 314 | } |
| 315 | } |
| 316 | if err := s.SaveCorrection(ctx, paul, mixed, "phone", kinds[0].Items[0].Handle, ""); err != nil { |
| 317 | t.Fatal(err) |
| 318 | } |
| 319 | if len(rec.calls) != 0 { |
| 320 | t.Errorf("the correction path called the model %d times", len(rec.calls)) |
| 321 | } |
| 322 | }) |
| 323 | } |
| 324 | |