// Task 168 — per-tool emit assertion for profile-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/schema-validator.js", () => ({
    validateWrite: vi.fn(),
}));
vi.mock("../profile-read.js", () => ({
    loadEnabledPluginProfileKeys: () => ({}),
}));
import { profileUpdate } from "../profile-update.js";
describe("profile-update — emit wrapper wired", () => {
    let emit;
    beforeEach(() => {
        emit = setupEmitCapture();
        mockSession.run.mockReset();
    });
    afterEach(() => emit.restore());
    it("emits result=ok line on happy path (created)", async () => {
        // The impl performs many runs; mock with a single resolved shape that
        // satisfies the first happy-path branch (notApplicable=true with
        // mode=reinforce — bypasses the value requirement and most cypher).
        mockSession.run.mockResolvedValue({
            records: [
                {
                    get: (k) => {
                        if (k === "preferenceId")
                            return "pref-1";
                        if (k === "action")
                            return "created";
                        if (k === "confidence")
                            return 1.0;
                        if (k === "existed")
                            return false;
                        return null;
                    },
                },
            ],
        });
        await profileUpdate({
            accountId: "acct-1",
            userId: "user-1",
            category: "communication",
            key: "tone",
            value: "",
            notApplicable: true,
        });
        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("profile-update");
        expect(body.line).toContain("result=ok");
    });
    it("emits result=error reason=invalid-input on invalid category", async () => {
        await expect(profileUpdate({
            accountId: "acct-1",
            userId: "user-1",
            category: "unknown-category",
            key: "tone",
            value: "polite",
        })).rejects.toThrow(/Invalid category/);
        expect(emit.fetchSpy).toHaveBeenCalledTimes(1);
        const body = emit.captures()[0];
        expect(body.level).toBe("error");
        expect(body.line).toContain("profile-update");
        expect(body.line).toContain("result=error");
    });
});
//# sourceMappingURL=profile-update-emit.test.js.map