// Task 168 — per-tool emit assertion for conversation-archive-derive-insights.
//
// Read-only inside the wrapper but routed through it because Task 166 wired
// every write-path tool — here it pins observability for the dispatch path,
// not data writes. Happy path requires real chunks + Haiku; error path is
// the natural archive-not-found gate.
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/oauth-llm/dist/index.js", () => ({
    callOauthLlm: vi.fn(),
}));
import { conversationArchiveDeriveInsights } from "../conversation-archive-derive-insights.js";
describe("conversation-archive-derive-insights — emit wrapper wired", () => {
    let emit;
    beforeEach(() => {
        emit = setupEmitCapture();
        mockSession.run.mockReset();
    });
    afterEach(() => emit.restore());
    it("emits result=error line when archive not found", async () => {
        // loadArchive's MATCH returns no rows → tool throws archive-not-found.
        mockSession.run.mockResolvedValue({ records: [] });
        await expect(conversationArchiveDeriveInsights({
            accountId: "acct-1",
            archiveElementId: "4:missing:1",
        })).rejects.toThrow(/archive-not-found/);
        expect(emit.fetchSpy).toHaveBeenCalledTimes(1);
        const body = emit.captures()[0];
        expect(body.tag).toBe("mcp:memory");
        expect(body.level).toBe("error");
        expect(body.line).toContain("conversation-archive-derive-insights");
        expect(body.line).toContain("result=error");
    });
    it("emits result=ok line on happy path (archive with no chunks to walk)", async () => {
        // loadArchive: one row with archive metadata + totalChunks=1 (totalChunks=0
        // is treated as archive-not-found per loadArchive guard).
        mockSession.run.mockResolvedValueOnce({
            records: [
                {
                    get: (k) => {
                        if (k === "title")
                            return "Stub Archive";
                        if (k === "totalChunks")
                            return 1;
                        return null;
                    },
                },
            ],
        });
        // loadChunks: empty — for-loop never iterates so no LLM/related-cypher fires.
        mockSession.run.mockResolvedValueOnce({ records: [] });
        await conversationArchiveDeriveInsights({
            accountId: "acct-1",
            archiveElementId: "4:archive:1",
        });
        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("conversation-archive-derive-insights");
        expect(body.line).toContain("result=ok");
    });
});
//# sourceMappingURL=conversation-archive-derive-insights-emit.test.js.map