// Task 168 — per-tool emit assertion for memory-edit-attachment.
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { setupEmitCapture } from "./_helpers/emit-capture.js";
vi.mock("../../lib/attachments.js", () => ({
    assertUUID: vi.fn(),
    readMeta: vi.fn(),
    findDataFile: vi.fn(),
    attachmentDir: () => "/tmp/task-168-stub",
}));
vi.mock("../memory-ingest-extract.js", () => ({
    memoryIngestExtract: vi.fn().mockResolvedValue({
        text: "stub",
        filename: "stub.md",
        cached: true,
    }),
}));
vi.mock("node:fs/promises", async () => {
    const actual = await vi.importActual("node:fs/promises");
    return {
        ...actual,
        writeFile: vi.fn().mockResolvedValue(undefined),
        stat: vi.fn().mockResolvedValue({ size: 10 }),
    };
});
import { memoryEditAttachment } from "../memory-edit-attachment.js";
import { readMeta, findDataFile } from "../../lib/attachments.js";
describe("memory-edit-attachment — emit wrapper wired", () => {
    let emit;
    beforeEach(() => {
        emit = setupEmitCapture();
        vi.mocked(readMeta).mockReset();
        vi.mocked(findDataFile).mockReset();
    });
    afterEach(() => emit.restore());
    it("emits result=ok line on happy path", async () => {
        vi.mocked(readMeta).mockResolvedValue({
            filename: "doc.md",
            mimeType: "text/markdown",
            attachmentId: "00000000-0000-0000-0000-000000000001",
            sizeBytes: 0,
            uploadedAt: "2026-01-01T00:00:00Z",
        });
        vi.mocked(findDataFile).mockResolvedValue("/tmp/task-168-stub/data.md");
        await memoryEditAttachment({
            accountId: "acct-1",
            attachmentId: "00000000-0000-0000-0000-000000000001",
            content: "new content",
        });
        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-edit-attachment");
        expect(body.line).toContain("result=ok");
    });
    it("emits result=error line when readMeta returns null (attachment not found)", async () => {
        vi.mocked(readMeta).mockResolvedValue(null);
        await expect(memoryEditAttachment({
            accountId: "acct-1",
            attachmentId: "00000000-0000-0000-0000-000000000001",
            content: "new",
        })).rejects.toThrow(/not found/);
        expect(emit.fetchSpy).toHaveBeenCalledTimes(1);
        const body = emit.captures()[0];
        expect(body.level).toBe("error");
        expect(body.line).toContain("memory-edit-attachment");
        expect(body.line).toContain("result=error");
    });
});
//# sourceMappingURL=memory-edit-attachment-emit.test.js.map