// Task 168 — per-tool emit assertion for memory-update.
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { setupEmitCapture, makeMockSession } from "./_helpers/emit-capture.js";
const mockSession = makeMockSession();
vi.mock("../../lib/neo4j.js", () => ({
    getSession: () => mockSession,
}));
vi.mock("../../lib/embeddings.js", () => ({
    embed: vi.fn().mockResolvedValue([0.1, 0.2, 0.3]),
}));
vi.mock("../../lib/graph-write-gate.js", () => ({
    checkGraphWriteGate: vi.fn().mockResolvedValue({ ok: true }),
    checkPropertyWriteGate: vi.fn().mockReturnValue({ ok: true }),
    checkLabelWriteGate: vi.fn().mockReturnValue({ ok: true }),
    COMPILED_TRUTH_MAX_CHARS: 2000,
}));
vi.mock("./memory-write.js", () => ({
    buildTextRepresentation: () => "stub-text",
}));
import { memoryUpdate } from "../memory-update.js";
describe("memory-update — emit wrapper wired", () => {
    let emit;
    beforeEach(() => {
        emit = setupEmitCapture();
        mockSession.run.mockReset();
    });
    afterEach(() => emit.restore());
    it("emits result=ok line on happy path", async () => {
        // call 1: label lookup; call 2: update query
        mockSession.run.mockResolvedValueOnce({
            records: [{ get: (k) => (k === "nodeLabels" ? ["Person"] : undefined) }],
        });
        mockSession.run.mockResolvedValueOnce({
            records: [
                {
                    get: (k) => {
                        if (k === "nodeId")
                            return "4:eid:1";
                        if (k === "nodeLabels")
                            return ["Person"];
                        if (k === "allProps")
                            return { name: "Alice" };
                        return undefined;
                    },
                },
            ],
        });
        // call 3: re-embed write
        mockSession.run.mockResolvedValueOnce({ records: [] });
        await memoryUpdate({
            nodeId: "4:eid:1",
            properties: { name: "Alice" },
            accountId: "acct-1",
            createdBy: { agent: "test", session: "s1" },
        });
        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-update");
        expect(body.line).toContain("result=ok");
        expect(body.line).toContain("elementId=4:eid:1");
    });
    it("emits result=error reason=invalid-input on empty properties", async () => {
        await expect(memoryUpdate({
            nodeId: "4:eid:1",
            properties: {},
            accountId: "acct-1",
            createdBy: { agent: "test", session: "s1" },
        })).rejects.toThrow(/No properties to update/);
        expect(emit.fetchSpy).toHaveBeenCalledTimes(1);
        const body = emit.captures()[0];
        expect(body.level).toBe("error");
        expect(body.line).toContain("memory-update");
        expect(body.line).toContain("result=error");
        expect(body.line).toContain("reason=invalid-input");
    });
});
//# sourceMappingURL=memory-update-emit.test.js.map