| new file mode 100644 |
| index 0000000..3dab8a9 |
| --- /dev/null |
| +++ b/internal/memoria/flip_test.go |
| @@ -0,0 +1,167 @@ |
| +package memoria |
| + |
| +import ( |
| + "context" |
| + "io" |
| + "log/slog" |
| + "net/http/httptest" |
| + "path/filepath" |
| + "strconv" |
| + "strings" |
| + "testing" |
| + |
| + "amadan.net/oficina/suite" |
| + "amadan.net/rastrillo/rastrillo/db" |
| + |
| + "amadan.net/oficina/memoria/internal/memoria/provider" |
| + "amadan.net/oficina/memoria/internal/memoria/seal" |
| +) |
| + |
| +// Gate 3c's flip case (suite doctrine §11, revision 11; Fable's review of |
| +// c38e3ad, H3): an authorization answer is never cached. One Docs |
| +// document; the app's answer for it goes VISIBLE → NOT_VISIBLE → UNKNOWN |
| +// with nothing else changed — no feed row, no restart, no cache cleared — |
| +// and the next request on every surface follows: shown, then hidden, |
| +// then withheld loudly and naming the app. |
| +// |
| +// Surfaces: the results page, the results fragment, the typeahead, the AI |
| +// answer (whose own cache is keyed on the ruled passages, so a flip |
| +// changes the key), the document page, the documents list and the front |
| +// page's Recently updated. TestEverySurfaceAsksTheAppOfRecord covers the |
| +// delete paths, which never show a sibling's document at all. |
| +// |
| +// Written, it found the typeahead hiding an unanswered app's documents |
| +// silently; it now carries §4.1's line like every other surface. |
| +// |
| +// MUTATION-TESTED 2026-11-02, each seen red and reverted: VisibleDocuments |
| +// memoised per document id → "the document page … after NOT_VISIBLE"; |
| +// the AI answer cache keyed on asker and query only → "the AI answer … |
| +// after NOT_VISIBLE" (it survived until the control matched the question |
| +// too, so the answer still ran after the flip); the typeahead's notices |
| +// dropped → "the typeahead after UNKNOWN: hidden silently"; rule() memoised per (asker, item) → every surface |
| +// after the flip. |
| +func TestGate3cEverySurfaceFollowsAFlip(t *testing.T) { |
| + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| + d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { d.Close() }) |
| + dek := make([]byte, 32) |
| + for i := range dek { |
| + dek[i] = byte(i*7 + 1) |
| + } |
| + sealer, err := seal.New(dek) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + mux, a, err := AppForTest(d, "http://127.0.0.1:8899", sealer, logger) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + cus := &perItemCustody{apps: docsRoster(), give: map[string]suite.Authorization{}} |
| + a.store.custody = cus |
| + a.store.answerer = echoAnswerer{} |
| + ctx := context.Background() |
| + id, err := a.store.Ingest(ctx, IngestSpec{Source: "Docs", Key: "flip", Title: "Nebula launch plan", |
| + Body: "## Plan\n\nThe nebula launch is planned for the spring.", ItemID: "flip", SiblingRevision: 4}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + // A control memoria owns, so a page that hides the Docs document still |
| + // has something on it and an empty page is never mistaken for a pass. |
| + // It matches the AI answer's question too, so after the flip the |
| + // answer still runs — on the control alone — and an answer cached from |
| + // before the flip has somewhere to be served from. |
| + 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 { |
| + t.Fatal(err) |
| + } |
| + |
| + get := func(path string) *httptest.ResponseRecorder { |
| + req := httptest.NewRequest("GET", path, nil) |
| + req.Header.Set("Accept", "text/html") |
| + rec := httptest.NewRecorder() |
| + mux.ServeHTTP(rec, req) |
| + return rec |
| + } |
| + docPage := "/documents/" + strconv.FormatInt(id, 10) |
| + const title, notice = "Nebula launch plan", "Docs did not answer" |
| + |
| + type surface struct { |
| + name, path string |
| + // shows reports whether the response names the document. |
| + shows func(*httptest.ResponseRecorder) bool |
| + // loud reports whether an UNKNOWN response withholds out loud and |
| + // names the app (or, for a route with no room for a sentence, is |
| + // the retryable refusal). |
| + loud func(*httptest.ResponseRecorder) bool |
| + } |
| + names := func(r *httptest.ResponseRecorder) bool { return strings.Contains(r.Body.String(), title) } |
| + says := func(r *httptest.ResponseRecorder) bool { return strings.Contains(r.Body.String(), notice) } |
| + surfaces := []surface{ |
| + {"the results page", "/?q=nebula", names, says}, |
| + {"the results fragment", "/search?q=nebula", names, says}, |
| + {"the typeahead", "/suggest?q=nebula", names, says}, |
| + {"the AI answer", "/answer?q=nebula+launch", |
| + func(r *httptest.ResponseRecorder) bool { |
| + return strings.Contains(r.Body.String(), "FROM THE SPRING PLAN") || names(r) |
| + }, |
| + func(r *httptest.ResponseRecorder) bool { |
| + return strings.Contains(r.Body.String(), "ONLY THE CHECKLIST") |
| + }}, |
| + {"the document page", docPage, |
| + func(r *httptest.ResponseRecorder) bool { return r.Code == 200 && names(r) }, |
| + func(r *httptest.ResponseRecorder) bool { return r.Code == 503 }}, |
| + {"the documents list", "/documents", names, says}, |
| + {"recently updated", "/", names, says}, |
| + } |
| + |
| + for _, step := range []struct { |
| + give suite.Authorization |
| + want string |
| + }{{suite.VISIBLE, "shown"}, {suite.NOT_VISIBLE, "hidden"}, {suite.UNKNOWN, "withheld"}, {suite.VISIBLE, "shown"}} { |
| + cus.give["flip"] = step.give |
| + for _, s := range surfaces { |
| + r := get(s.path) |
| + switch step.want { |
| + case "shown": |
| + if !s.shows(r) { |
| + t.Errorf("%s after %v: not shown (%d)", s.name, step.give, r.Code) |
| + } |
| + case "hidden": |
| + if s.shows(r) { |
| + t.Errorf("%s after NOT_VISIBLE: still shown — an authorization answer was cached", s.name) |
| + } |
| + if s.name == "the document page" && r.Code != 404 { |
| + t.Errorf("the document page after NOT_VISIBLE = %d, want 404", r.Code) |
| + } |
| + case "withheld": |
| + if s.shows(r) { |
| + t.Errorf("%s after UNKNOWN: still shown — an authorization answer was cached", s.name) |
| + } |
| + if !s.loud(r) { |
| + t.Errorf("%s after UNKNOWN: hidden silently (%d), not withheld naming Docs", s.name, r.Code) |
| + } |
| + } |
| + } |
| + // The control stays on the list throughout. |
| + if !strings.Contains(get("/documents").Body.String(), "Kitchen rota") { |
| + t.Fatalf("after %v the control is gone, so an empty list proves nothing", step.give) |
| + } |
| + } |
| +} |
| + |
| +// echoAnswerer says which passages it was sent: a sentence from the Docs |
| +// document's text if that was among them, and another if only the |
| +// control was. A cached answer from before a flip is then visible by its |
| +// words. |
| +type echoAnswerer struct{} |
| + |
| +func (echoAnswerer) Answer(_ context.Context, _ string, ps []string) ([]provider.Sentence, error) { |
| + for i, p := range ps { |
| + if strings.Contains(p, "spring") { |
| + return []provider.Sentence{{Text: "FROM THE SPRING PLAN.", Cite: []int{i}}}, nil |
| + } |
| + } |
| + return []provider.Sentence{{Text: "ONLY THE CHECKLIST.", Cite: []int{0}}}, nil |
| +} |