// Task 168 — per-tool emit assertion for memory-ingest-web.
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("../memory-ingest-extract.js", () => ({
    memoryIngestExtract: vi.fn(),
    extractCache: new Map(),
}));
vi.mock("node:fs/promises", async () => {
    const actual = await vi.importActual("node:fs/promises");
    return {
        ...actual,
        writeFile: vi.fn().mockResolvedValue(undefined),
        unlink: vi.fn().mockResolvedValue(undefined),
    };
});
import { memoryIngestWeb } from "../memory-ingest-web.js";
import { memoryIngestExtract } from "../memory-ingest-extract.js";
describe("memory-ingest-web — emit wrapper wired", () => {
    let emit;
    beforeEach(() => {
        emit = setupEmitCapture();
        mockSession.run.mockReset();
        vi.mocked(memoryIngestExtract).mockReset();
    });
    afterEach(() => emit.restore());
    it("emits result=ok line on happy path", async () => {
        // checkExistingDocument: no record.
        mockSession.run.mockResolvedValue({ records: [] });
        vi.mocked(memoryIngestExtract).mockResolvedValue({
            filename: "Page Title",
            mimeType: "text/markdown",
            sizeBytes: 10,
            textLength: 10,
            preview: "hello",
        });
        await memoryIngestWeb({
            accountId: "acct-1",
            url: "https://example.com/page",
            content: "# Page Title\n\nbody",
        });
        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-ingest-web");
        expect(body.line).toContain("result=ok");
    });
    it("emits result=error line on empty content (invalid-input via guidance message)", async () => {
        await expect(memoryIngestWeb({
            accountId: "acct-1",
            url: "https://example.com/page",
            content: "   ",
        })).rejects.toThrow(/empty/);
        expect(emit.fetchSpy).toHaveBeenCalledTimes(1);
        const body = emit.captures()[0];
        expect(body.level).toBe("error");
        expect(body.line).toContain("memory-ingest-web");
        expect(body.line).toContain("result=error");
    });
});
//# sourceMappingURL=memory-ingest-web-emit.test.js.map