// Task 168 — per-tool emit assertion for memory-write.
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/schema-validator.js", () => ({
    validateWrite: vi.fn(),
}));
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("../../../../../../lib/graph-write/dist/index.js", () => ({
    writeNodeWithEdges: vi.fn(),
    injectConversationProvenance: vi
        .fn()
        .mockImplementation(async ({ relationships }) => relationships),
}));
import { memoryWrite } from "../memory-write.js";
import { writeNodeWithEdges } from "../../../../../../lib/graph-write/dist/index.js";
describe("memory-write — emit wrapper wired", () => {
    let emit;
    beforeEach(() => {
        emit = setupEmitCapture();
        // Use mockClear (not mockReset) so the helper's default
        // implementation — empty `records` array — is preserved. memory-write
        // now runs resolveActiveVertical via this session before validating;
        // a wiped implementation would return undefined and crash that read.
        mockSession.run.mockClear();
        vi.mocked(writeNodeWithEdges).mockReset();
    });
    afterEach(() => emit.restore());
    it("emits result=ok line on happy path", async () => {
        vi.mocked(writeNodeWithEdges).mockResolvedValue({
            nodeId: "4:abc:1",
            labels: ["LocalBusiness"],
            created: true,
        });
        await memoryWrite({
            labels: ["LocalBusiness"],
            properties: { name: "Smalleys" },
            accountId: "acct-1",
            scope: "shared",
            relationships: [{ type: "BELONGS_TO", direction: "outgoing", targetNodeId: "4:owner:1" }],
            createdBy: { agent: "system", session: "s1" },
            validator: { schema: { brandVertical: null } },
        });
        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-write");
        expect(body.line).toContain("result=ok");
        expect(body.line).toContain("elementId=4:abc:1");
    });
    it("emits result=error reason=invalid-input on missing scope", async () => {
        await expect(memoryWrite({
            labels: ["LocalBusiness"],
            properties: { name: "Smalleys" },
            accountId: "acct-1",
            scope: "",
            relationships: [],
            createdBy: {},
            validator: { schema: { brandVertical: null } },
        })).rejects.toThrow(/scope is required/);
        expect(emit.fetchSpy).toHaveBeenCalledTimes(1);
        const body = emit.captures()[0];
        expect(body.level).toBe("error");
        expect(body.line).toContain("memory-write");
        expect(body.line).toContain("result=error");
        expect(body.line).toContain("reason=invalid-input");
    });
});
//# sourceMappingURL=memory-write-emit.test.js.map