// Task 168 — per-tool emit assertion for memory-archive-write.
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { setupEmitCapture } from "./_helpers/emit-capture.js";
const mockSession = {
    run: vi.fn(),
    executeWrite: vi.fn(),
    close: vi.fn().mockResolvedValue(undefined),
};
vi.mock("../../lib/neo4j.js", () => ({
    getSession: () => mockSession,
}));
import { memoryArchiveWrite } from "../memory-archive-write.js";
describe("memory-archive-write — emit wrapper wired", () => {
    let emit;
    beforeEach(() => {
        emit = setupEmitCapture();
        mockSession.run.mockReset();
        mockSession.executeWrite.mockReset();
    });
    afterEach(() => emit.restore());
    it("emits result=ok line on happy path", async () => {
        // verifyArchive's MATCH: owner found with :Person label and matching account.
        mockSession.run.mockResolvedValue({
            records: [
                {
                    get: (k) => {
                        if (k === "labels")
                            return ["Person"];
                        if (k === "accountId")
                            return "acct-1";
                        return null;
                    },
                },
            ],
        });
        const noopCounters = {
            nodesCreated: 0,
            relationshipsCreated: 0,
            propertiesSet: 0,
        };
        // writeBatch wraps personRes / orgRes inside executeWrite. Return the
        // counter shapes the handler reads.
        mockSession.executeWrite.mockImplementation(async (fn) => {
            return fn({
                run: vi.fn().mockResolvedValue({
                    summary: { counters: { updates: () => noopCounters } },
                }),
            });
        });
        await memoryArchiveWrite({
            archiveType: "linkedin-connections",
            ownerNodeId: "4:owner:1",
            accountId: "acct-1",
            rows: [
                {
                    firstName: "Alice",
                    lastName: "Carter",
                    email: "alice@example.com",
                    company: "ExampleCo",
                    position: "Director",
                    connectedOn: "2024-01-01",
                    url: "https://www.linkedin.com/in/alice",
                },
            ],
        });
        expect(emit.fetchSpy).toHaveBeenCalledTimes(1);
        const body = emit.captures()[0];
        expect(body.tag).toBe("mcp:memory");
        expect(body.level).toBe("info");
        expect(body.line).toContain("memory-archive-write");
        expect(body.line).toContain("result=ok");
    });
    it("emits result=error line on missing accountId (invalid-input)", async () => {
        await expect(memoryArchiveWrite({
            archiveType: "linkedin-connections",
            ownerNodeId: "4:owner:1",
            accountId: "",
            rows: [{}],
        })).rejects.toThrow(/accountId is required/);
        expect(emit.fetchSpy).toHaveBeenCalledTimes(1);
        const body = emit.captures()[0];
        expect(body.level).toBe("error");
        expect(body.line).toContain("memory-archive-write");
        expect(body.line).toContain("result=error");
        expect(body.line).toContain("reason=invalid-input");
    });
});
//# sourceMappingURL=memory-archive-write-emit.test.js.map