Files
coucou/brain/tests/archive.test.ts
Claude ee0a675978 Memory › Usage: one list with a selector, and archiving the memories nobody uses
The three blocks become one card: Most useful, Most used (week, month, year, all),
Never used, Archived. Never used memories can be archived one by one or all at once
(after a confirmation); Archived brings them back.

brain archive / brain restore: an archived memory leaves the state files (so the hooks,
MCP, search index, pages and galaxy no longer see it) and is kept in .state/archived.json.
Saving a state leaves archived ids out, so a later ingestion can't bring one back.
Nothing is deleted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LJm3SRL9k3gBPaVtfPhwcT
2026-11-28 20:11:54 +00:00

69 lines
3.7 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from "vitest";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import type { BrainConfig } from "../src/config.js";
import { archiveMemories, archivedIds, readArchive, restoreMemories, withoutArchived } from "../src/archive.js";
import { recordUsage, usageReport } from "../src/usage.js";
let dir: string;
let config: BrainConfig;
const state = (id: string) => JSON.parse(fs.readFileSync(path.join(dir, ".state", "proj", `${id}.json`), "utf8"));
beforeEach(() => {
dir = fs.mkdtempSync(path.join(os.tmpdir(), "archive"));
config = {
brainPath: dir, claudeProjectsPath: path.join(dir, "none"), repoPaths: [],
chunkModel: "sonnet", mergeModel: "opus", maxChunkTokens: 100000, language: "fr", repoAliases: {},
};
const p = path.join(dir, ".state", "proj");
fs.mkdirSync(p, { recursive: true });
const el = (id: string, texte: string) => ({ id, kind: "fait", texte, date: "2026-09-01", sources: [], projets: [] });
fs.writeFileSync(path.join(p, "proj_coucou.json"), JSON.stringify({ id: "proj_coucou", title: "coucou", recordSources: [],
elements: [el("fait_a", "A"), el("fait_b", "B")] }));
fs.writeFileSync(path.join(p, "louis.json"), JSON.stringify({ id: "louis", elements: [el("pref_c", "C")] }));
});
afterEach(() => fs.rmSync(dir, { recursive: true, force: true }));
describe("archive", () => {
it("takes memories out, keeps them aside, and brings them back where they were", () => {
expect(archiveMemories(config, ["fait_a", "pref_c", "nope"], new Date("2026-11-28T10:00:00Z"))).toBe(2);
expect(state("proj_coucou").elements.map((e: { id: string }) => e.id)).toEqual(["fait_b"]);
expect(state("louis").elements).toEqual([]);
expect(state("proj_coucou").recordSources).toEqual([]); // the rest of the state is untouched
expect(readArchive(config).fait_a).toMatchObject({ state: "proj_coucou", at: "2026-11-28T10:00:00.000Z" });
const r = usageReport(config, { now: Date.parse("2026-11-28T12:00:00Z") });
expect(r.memories).toBe(1);
expect(r.archivedCount).toBe(2);
expect(r.archived.map(a => a.id).sort()).toEqual(["fait_a", "pref_c"]);
expect(restoreMemories(config, ["fait_a"])).toBe(1);
expect(state("proj_coucou").elements.map((e: { id: string }) => e.id)).toEqual(["fait_b", "fait_a"]);
expect([...archivedIds(config)]).toEqual(["pref_c"]);
});
it("an archived memory found again by an ingestion stays out", () => {
archiveMemories(config, ["fait_a"]);
const again = { id: "proj_coucou", elements: [{ id: "fait_a" }, { id: "fait_z" }] } as never;
expect((withoutArchived(again, config) as { elements: { id: string }[] }).elements.map(e => e.id)).toEqual(["fait_z"]);
});
it("restores into a state file that disappeared", () => {
archiveMemories(config, ["fait_a"]);
fs.rmSync(path.join(dir, ".state", "proj", "proj_coucou.json"));
expect(restoreMemories(config, ["fait_a"])).toBe(1);
expect(state("proj_coucou")).toMatchObject({ id: "proj_coucou", recordSources: [], elements: [{ id: "fait_a" }] });
});
it("most used by period", () => {
recordUsage(config, { via: "prompt", ids: ["fait_a"], session: "s1", t: "2025-10-01T10:00:00Z" });
recordUsage(config, { via: "prompt", ids: ["fait_a"], session: "s2", t: "2026-06-10T10:00:00Z" });
recordUsage(config, { via: "prompt", ids: ["fait_b"], session: "s3", t: "2026-11-27T10:00:00Z" });
const r = usageReport(config, { now: Date.parse("2026-11-28T12:00:00Z") });
expect(r.top.week.map(u => u.id)).toEqual(["fait_b"]);
expect(r.top.year.map(u => [u.id, u.uses])).toEqual([["fait_b", 1], ["fait_a", 1]]); // a tie: the latest first
expect(r.top.all[0]).toMatchObject({ id: "fait_a", uses: 2 });
});
});