// Task 443 — memory-empty-trash refuses a trashed :LocalBusiness candidate
// when origin !== "system"; the rest of the batch drains.
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,
    getClaudeSessionIdsByConversationElementId: vi.fn(),
}));
vi.mock("../../lib/attachments.js", () => ({
    removeAttachmentDir: vi.fn().mockResolvedValue(false),
}));
import { memoryEmptyTrash } from "../memory-empty-trash.js";
const ACCOUNT_ID = "acct-1";
const LB_EID = "4:eid:lb";
const PERSON_EID = "4:eid:p";
function stageBatch(candidates, options) {
    const state = { detachCalls: [] };
    let candidateQueryCount = 0;
    mockSession.run = vi.fn(async (cypher, params) => {
        if (cypher.includes("MATCH (n:Trashed)")) {
            candidateQueryCount++;
            return {
                records: candidates.map((c) => ({
                    get: (k) => {
                        switch (k) {
                            case "eid": return c.eid;
                            case "labels": return [...c.labels, "Trashed"];
                            case "trashedAt": return "2026-04-01T00:00:00.000Z";
                            case "trashedBy": return "test";
                            case "keysJson": return "{}";
                            default: return undefined;
                        }
                    },
                })),
            };
        }
        if (cypher.includes("DETACH DELETE n")) {
            const eid = params?.eid;
            state.detachCalls.push(eid);
            if (!options.expectDetachEids.includes(eid)) {
                throw new Error(`unexpected DETACH DELETE for ${eid}`);
            }
            return { records: [] };
        }
        throw new Error(`unexpected cypher: ${cypher.slice(0, 80)}`);
    });
    return state;
}
beforeEach(() => {
    vi.clearAllMocks();
});
afterEach(() => {
    vi.restoreAllMocks();
});
describe("memory-empty-trash reserved-label gate (Task 443)", () => {
    it("non-system sweep skips :LocalBusiness candidate and drains the rest", async () => {
        const state = stageBatch([
            { eid: LB_EID, labels: ["LocalBusiness"] },
            { eid: PERSON_EID, labels: ["Person"] },
        ], { expectDetachEids: [PERSON_EID] });
        const emit = setupEmitCapture();
        try {
            const result = await memoryEmptyTrash({ accountId: ACCOUNT_ID });
            expect(state.detachCalls).toEqual([PERSON_EID]);
            expect(result.emptied).toBe(1);
            expect(result.candidates.length).toBe(2);
        }
        finally {
            emit.restore();
        }
    });
    it("system-origin sweep hard-deletes :LocalBusiness (installer reset path)", async () => {
        const state = stageBatch([{ eid: LB_EID, labels: ["LocalBusiness"] }], { expectDetachEids: [LB_EID] });
        const emit = setupEmitCapture();
        try {
            const result = await memoryEmptyTrash({ accountId: ACCOUNT_ID, origin: "system" });
            expect(state.detachCalls).toEqual([LB_EID]);
            expect(result.emptied).toBe(1);
        }
        finally {
            emit.restore();
        }
    });
});
//# sourceMappingURL=memory-empty-trash-reserved-label.test.js.map