package memoriatest import ( "context" "testing" memoria "amadan.net/oficina/memoria/internal/memoria" "amadan.net/oficina/memoria/internal/memoria/notion" ) // TestAFailureAfterImportKeepsWhatWeHold. // // A page imports fine, and on a LATER run it 404s — deleted or unshared // in the meantime. Recording that failure used to write document_id=0 and // an empty ingested edit time, which erased memoria's own record that it // holds the document. Two consequences, both wrong: "Imported" dropped on // the status page for a document that is still there, and the next run // had to fetch and re-ingest a page that had never actually changed. // // The document itself was never at risk — memoria does not delete on // absence — which is exactly why this was easy to miss. // // Found by a code review. // // MUTATION-TESTED 2026-09-05: passing 0, "", "" to MarkPage from // markGone (the pre-fix behaviour) makes this fail. Reverted. func TestAFailureAfterImportKeepsWhatWeHold(t *testing.T) { src := newSource() src.add("root", "Handbook", "", "Expenses are reimbursed within thirty days.") src.add("kid", "Onboarding", "root", "Week one covers travel booking.", "Handbook") h, app, cid, _ := connectorApp(t, src) sync(t, h, app, cid) _, importedBefore, err := app.Store().CountPages(context.Background(), cid) if err != nil { t.Fatal(err) } if importedBefore != 2 { t.Fatalf("imported %d before the failure, want 2 — nothing to lose otherwise", importedBefore) } // The page goes away upstream. src.failWith["kid"] = ¬ion.Error{Code: notion.CodeTraversalFailed, Status: 404} sync(t, h, app, cid) run := latest(t, app, cid) if run.State != memoria.RunDone { t.Fatalf("run state %q, want done", run.State) } if run.FailedPages != 1 { t.Errorf("failed_pages %d, want 1", run.FailedPages) } // The document is still held, and memoria still KNOWS it holds it. if hits, _ := searchAsReader(app.Store(), context.Background(), "travel booking", 10); len(hits) == 0 { t.Error("the document was removed when its page 404'd; memoria does not delete on absence") } _, importedAfter, err := app.Store().CountPages(context.Background(), cid) if err != nil { t.Fatal(err) } if importedAfter != importedBefore { t.Errorf("imported dropped %d→%d because a fetch failed; the record that memoria holds "+ "the document was erased, though the document is still there", importedBefore, importedAfter) } }