| 1 | package memoria |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "log/slog" |
| 7 | "net/http/httptest" |
| 8 | "path/filepath" |
| 9 | "strconv" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "amadan.net/oficina/suite" |
| 14 | "amadan.net/rastrillo/rastrillo/db" |
| 15 | |
| 16 | "amadan.net/oficina/memoria/internal/memoria/provider" |
| 17 | "amadan.net/oficina/memoria/internal/memoria/seal" |
| 18 | ) |
| 19 | |
| 20 | // Gate 3c's flip case (suite doctrine §11, revision 11; Fable's review of |
| 21 | // c38e3ad, H3): an authorization answer is never cached. One Docs |
| 22 | // document; the app's answer for it goes VISIBLE → NOT_VISIBLE → UNKNOWN |
| 23 | // with nothing else changed — no feed row, no restart, no cache cleared — |
| 24 | // and the next request on every surface follows: shown, then hidden, |
| 25 | // then withheld loudly and naming the app. |
| 26 | // |
| 27 | // Surfaces: the results page, the results fragment, the typeahead, the AI |
| 28 | // answer (whose own cache is keyed on the ruled passages, so a flip |
| 29 | // changes the key), the document page, the documents list and the front |
| 30 | // page's Recently updated, the scriptless AI answer, and the search the |
| 31 | // native and agent APIs answer from. TestEverySurfaceAsksTheAppOfRecord |
| 32 | // covers the delete paths, which never show a sibling's document at all; |
| 33 | // the correction page lists only checked facts, and reports hold |
| 34 | // coordinates only. |
| 35 | // |
| 36 | // Written, it found the typeahead hiding an unanswered app's documents |
| 37 | // silently; it now carries §4.1's line like every other surface. |
| 38 | // |
| 39 | // MUTATION-TESTED 2026-11-02, each seen red and reverted: VisibleDocuments |
| 40 | // memoised per document id → "the document page … after NOT_VISIBLE"; |
| 41 | // the AI answer cache keyed on asker and query only → "the AI answer … |
| 42 | // after NOT_VISIBLE" (it survived until the control matched the question |
| 43 | // too, so the answer still ran after the flip); the typeahead's notices |
| 44 | // dropped → "the typeahead after UNKNOWN: hidden silently"; rule() memoised per (asker, item) → every surface |
| 45 | // after the flip. |
| 46 | func TestGate3cEverySurfaceFollowsAFlip(t *testing.T) { |
| 47 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 48 | d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) |
| 49 | if err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | t.Cleanup(func() { d.Close() }) |
| 53 | dek := make([]byte, 32) |
| 54 | for i := range dek { |
| 55 | dek[i] = byte(i*7 + 1) |
| 56 | } |
| 57 | sealer, err := seal.New(dek) |
| 58 | if err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | mux, a, err := AppForTest(d, "http://127.0.0.1:8899", sealer, logger) |
| 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | cus := &perItemCustody{apps: docsRoster(), give: map[string]suite.Authorization{}} |
| 66 | a.store.custody = cus |
| 67 | a.store.answerer = echoAnswerer{} |
| 68 | ctx := context.Background() |
| 69 | id, err := a.store.Ingest(ctx, IngestSpec{Source: "Docs", Key: "flip", Title: "Nebula launch plan", |
| 70 | Body: "## Plan\n\nThe nebula launch is planned for the spring.", ItemID: "flip", SiblingRevision: 4}) |
| 71 | if err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | // A control memoria owns, so a page that hides the Docs document still |
| 75 | // has something on it and an empty page is never mistaken for a pass. |
| 76 | // It matches the AI answer's question too, so after the flip the |
| 77 | // answer still runs — on the control alone — and an answer cached from |
| 78 | // before the flip has somewhere to be served from. |
| 79 | if _, err := a.store.Ingest(ctx, IngestSpec{Source: "upload", Key: "own", Title: "Kitchen rota", Body: "The nebula launch checklist is pinned by the kettle."}); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | |
| 83 | get := func(path string) *httptest.ResponseRecorder { |
| 84 | req := httptest.NewRequest("GET", path, nil) |
| 85 | req.Header.Set("Accept", "text/html") |
| 86 | rec := httptest.NewRecorder() |
| 87 | mux.ServeHTTP(rec, req) |
| 88 | return rec |
| 89 | } |
| 90 | docPage := "/documents/" + strconv.FormatInt(id, 10) |
| 91 | const title, notice = "Nebula launch plan", "Docs did not answer" |
| 92 | |
| 93 | type surface struct { |
| 94 | name, path string |
| 95 | // shows reports whether the response names the document. |
| 96 | shows func(*httptest.ResponseRecorder) bool |
| 97 | // loud reports whether an UNKNOWN response withholds out loud and |
| 98 | // names the app (or, for a route with no room for a sentence, is |
| 99 | // the retryable refusal). |
| 100 | loud func(*httptest.ResponseRecorder) bool |
| 101 | } |
| 102 | names := func(r *httptest.ResponseRecorder) bool { return strings.Contains(r.Body.String(), title) } |
| 103 | says := func(r *httptest.ResponseRecorder) bool { return strings.Contains(r.Body.String(), notice) } |
| 104 | surfaces := []surface{ |
| 105 | {"the results page", "/?q=nebula", names, says}, |
| 106 | {"the results fragment", "/search?q=nebula", names, says}, |
| 107 | {"the typeahead", "/suggest?q=nebula", names, says}, |
| 108 | {"the AI answer", "/answer?q=nebula+launch", |
| 109 | func(r *httptest.ResponseRecorder) bool { |
| 110 | return strings.Contains(r.Body.String(), "FROM THE SPRING PLAN") || names(r) |
| 111 | }, |
| 112 | func(r *httptest.ResponseRecorder) bool { |
| 113 | return strings.Contains(r.Body.String(), "ONLY THE CHECKLIST") |
| 114 | }}, |
| 115 | {"the document page", docPage, |
| 116 | func(r *httptest.ResponseRecorder) bool { return r.Code == 200 && names(r) }, |
| 117 | func(r *httptest.ResponseRecorder) bool { return r.Code == 503 }}, |
| 118 | {"the documents list", "/documents", names, says}, |
| 119 | {"recently updated", "/", names, says}, |
| 120 | // The scriptless AI answer, rendered into the results page. |
| 121 | {"the scriptless AI answer", "/?q=nebula+launch&ai=1", |
| 122 | func(r *httptest.ResponseRecorder) bool { |
| 123 | return strings.Contains(r.Body.String(), "FROM THE SPRING PLAN") || names(r) |
| 124 | }, says}, |
| 125 | } |
| 126 | // The native and agent APIs both answer from SearchWithoutFacts |
| 127 | // (native.go, agent_access.go); asked of it directly, with the asker |
| 128 | // the page uses. |
| 129 | asker := suite.Subject{Kind: suite.SubjectAddress, Value: "paul@tito.io"} |
| 130 | if got, ok := a.asker(httptest.NewRequest("GET", "/", nil)); ok { |
| 131 | asker = got |
| 132 | } |
| 133 | apiSays := func() (shown bool, notices string) { |
| 134 | p, err := a.store.SearchWithoutFacts(ctx, asker, "nebula", 25) |
| 135 | if err != nil { |
| 136 | t.Fatal(err) |
| 137 | } |
| 138 | for _, r := range p.Results { |
| 139 | if r.Title == title { |
| 140 | shown = true |
| 141 | } |
| 142 | } |
| 143 | return shown, strings.Join(p.Notices, " ") |
| 144 | } |
| 145 | |
| 146 | for _, step := range []struct { |
| 147 | give suite.Authorization |
| 148 | want string |
| 149 | }{{suite.VISIBLE, "shown"}, {suite.NOT_VISIBLE, "hidden"}, {suite.UNKNOWN, "withheld"}, {suite.VISIBLE, "shown"}} { |
| 150 | cus.give["flip"] = step.give |
| 151 | for _, s := range surfaces { |
| 152 | r := get(s.path) |
| 153 | switch step.want { |
| 154 | case "shown": |
| 155 | if !s.shows(r) { |
| 156 | t.Errorf("%s after %v: not shown (%d)", s.name, step.give, r.Code) |
| 157 | } |
| 158 | case "hidden": |
| 159 | if s.shows(r) { |
| 160 | t.Errorf("%s after NOT_VISIBLE: still shown — an authorization answer was cached", s.name) |
| 161 | } |
| 162 | if s.name == "the document page" && r.Code != 404 { |
| 163 | t.Errorf("the document page after NOT_VISIBLE = %d, want 404", r.Code) |
| 164 | } |
| 165 | case "withheld": |
| 166 | if s.shows(r) { |
| 167 | t.Errorf("%s after UNKNOWN: still shown — an authorization answer was cached", s.name) |
| 168 | } |
| 169 | if !s.loud(r) { |
| 170 | t.Errorf("%s after UNKNOWN: hidden silently (%d), not withheld naming Docs", s.name, r.Code) |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | shown, notices := apiSays() |
| 175 | switch step.want { |
| 176 | case "shown": |
| 177 | if !shown { |
| 178 | t.Errorf("the native and agent search after %v: not shown", step.give) |
| 179 | } |
| 180 | case "hidden": |
| 181 | if shown { |
| 182 | t.Errorf("the native and agent search after NOT_VISIBLE: still shown") |
| 183 | } |
| 184 | case "withheld": |
| 185 | if shown || !strings.Contains(notices, notice) { |
| 186 | t.Errorf("the native and agent search after UNKNOWN: shown=%v, notices %q", shown, notices) |
| 187 | } |
| 188 | } |
| 189 | // The control stays on the list throughout. |
| 190 | if !strings.Contains(get("/documents").Body.String(), "Kitchen rota") { |
| 191 | t.Fatalf("after %v the control is gone, so an empty list proves nothing", step.give) |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // echoAnswerer says which passages it was sent: a sentence from the Docs |
| 197 | // document's text if that was among them, and another if only the |
| 198 | // control was. A cached answer from before a flip is then visible by its |
| 199 | // words. |
| 200 | type echoAnswerer struct{} |
| 201 | |
| 202 | func (echoAnswerer) Answer(_ context.Context, _ string, ps []string) ([]provider.Sentence, error) { |
| 203 | for i, p := range ps { |
| 204 | if strings.Contains(p, "spring") { |
| 205 | return []provider.Sentence{{Text: "FROM THE SPRING PLAN.", Cite: []int{i}}}, nil |
| 206 | } |
| 207 | } |
| 208 | return []provider.Sentence{{Text: "ONLY THE CHECKLIST.", Cite: []int{0}}}, nil |
| 209 | } |
| 210 | |
| 211 | // The front page withholds out loud even when every recent document is |
| 212 | // withheld: the notices stand without the list (Fable's review of |
| 213 | // e1f6829, whose probe found the section, notices and all, missing). |
| 214 | // |
| 215 | // MUTATION-TESTED 2026-11-02: the notices back inside {{with .Recent}} → |
| 216 | // "said nothing". |
| 217 | func TestTheFrontPageSaysWhenEveryRecentDocumentIsWithheld(t *testing.T) { |
| 218 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 219 | d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) |
| 220 | if err != nil { |
| 221 | t.Fatal(err) |
| 222 | } |
| 223 | t.Cleanup(func() { d.Close() }) |
| 224 | dek := make([]byte, 32) |
| 225 | for i := range dek { |
| 226 | dek[i] = byte(i*5 + 2) |
| 227 | } |
| 228 | sealer, err := seal.New(dek) |
| 229 | if err != nil { |
| 230 | t.Fatal(err) |
| 231 | } |
| 232 | mux, a, err := AppForTest(d, "http://127.0.0.1:8899", sealer, logger) |
| 233 | if err != nil { |
| 234 | t.Fatal(err) |
| 235 | } |
| 236 | a.store.custody = &perItemCustody{apps: docsRoster(), give: map[string]suite.Authorization{"only": suite.UNKNOWN}} |
| 237 | if _, err := a.store.Ingest(context.Background(), IngestSpec{Source: "Docs", Key: "only", Title: "The only document", Body: "Text.", ItemID: "only", SiblingRevision: 1}); err != nil { |
| 238 | t.Fatal(err) |
| 239 | } |
| 240 | req := httptest.NewRequest("GET", "/", nil) |
| 241 | req.Header.Set("Accept", "text/html") |
| 242 | rec := httptest.NewRecorder() |
| 243 | mux.ServeHTTP(rec, req) |
| 244 | b := rec.Body.String() |
| 245 | if strings.Contains(b, "The only document") { |
| 246 | t.Fatal("the withheld document is on the front page") |
| 247 | } |
| 248 | if !strings.Contains(b, "Docs did not answer") { |
| 249 | t.Errorf("the front page withheld everything recent and said nothing:\n%s", b) |
| 250 | } |
| 251 | } |
| 252 | |