import { describe, it, expect, vi } from "vitest";
import { writeKnowledgeDocSections } from "../memory-archive-write.js";
// Task 636 — the archive :Section writer is idempotent (DETACH DELETE prior
// sections), creates one :Section per chunk with its embedding, and chains them
// with :NEXT. This drives a fake tx so no live Neo4j is needed.
function makeFakeTx() {
    const calls = [];
    let n = 0;
    const tx = {
        run: vi.fn(async (q, p = {}) => {
            calls.push({ q, p });
            if (q.includes("CREATE (s:Section)")) {
                return { records: [{ get: (_k) => `sec-${n++}` }] };
            }
            return { records: [] };
        }),
    };
    return { tx, calls };
}
describe("writeKnowledgeDocSections", () => {
    it("deletes prior sections, creates one per chunk with embedding, and chains NEXT", async () => {
        const { tx, calls } = makeFakeTx();
        await writeKnowledgeDocSections(tx, {
            docId: "doc-1",
            accountId: "acct",
            scope: "admin",
            source: "obsidian",
            createdByAgent: "obsidian-import",
            sessionId: "s1",
            sourceDocumentKey: "notes/a.md",
            sections: [
                { title: "A", body: "alpha", embedding: [0.1] },
                { title: "B", body: "beta", embedding: [0.2] },
            ],
        });
        const deletes = calls.filter((c) => c.q.includes("DETACH DELETE s"));
        expect(deletes).toHaveLength(1);
        const creates = calls.filter((c) => c.q.includes("CREATE (s:Section)"));
        expect(creates).toHaveLength(2);
        expect(creates[0].p.props.embedding).toEqual([0.1]);
        expect(creates[0].p.props.title).toBe("A");
        const nexts = calls.filter((c) => c.q.includes("[n:NEXT]"));
        expect(nexts).toHaveLength(1); // 2 sections → 1 chain edge
    });
    it("no sections → only the cleanup delete runs", async () => {
        const { tx, calls } = makeFakeTx();
        await writeKnowledgeDocSections(tx, {
            docId: "doc-1",
            accountId: "acct",
            scope: "admin",
            source: "notion",
            createdByAgent: "notion-import",
            sessionId: null,
            sourceDocumentKey: "nid",
            sections: [],
        });
        expect(calls).toHaveLength(1);
        expect(calls[0].q).toContain("DETACH DELETE s");
    });
});
//# sourceMappingURL=memory-archive-section-writer.test.js.map