import { describe, it, expect, vi } from "vitest";
import { writeNodeWithEdges } from "../../../../../../lib/graph-write/dist/index.js";
function rec(map) {
    return { get: (k) => map[k] };
}
// A fake tx/session that resolves the target pre-check, the CREATE, and the
// per-edge CREATE so writeNodeWithEdges runs end to end. Captures the props the
// node CREATE received so the test can inspect the embedding.
function makeFakeSession(targetId) {
    let createdProps = null;
    const tx = {
        run: vi.fn(async (q, params) => {
            if (q.includes("UNWIND $ids")) {
                return { records: [rec({ id: targetId, labels: ["Task"] })] };
            }
            if (q.startsWith("CREATE (n:")) {
                createdProps = params.props;
                return {
                    records: [rec({ nodeId: "node-1", nodeLabels: ["Person"] })],
                };
            }
            // per-edge CREATE
            return {
                summary: { counters: { updates: () => ({ relationshipsCreated: 1 }) } },
            };
        }),
    };
    const session = {
        executeWrite: async (fn) => fn(tx),
    };
    return { session, getCreatedProps: () => createdProps };
}
const baseParams = (extra) => ({
    labels: ["Person"],
    props: { accountId: "acct-1", givenName: "Ada" },
    relationships: [
        { type: "PRODUCED", direction: "incoming", targetNodeId: "target-1" },
    ],
    createdBy: { agent: "test", session: "s1", tool: "unit" },
    expectedAccountId: "acct-1",
    ...extra,
});
describe("writeNodeWithEdges embed safety net", () => {
    it("fills a missing embedding for an indexed label when embed is injected", async () => {
        const { session, getCreatedProps } = makeFakeSession("target-1");
        const embed = vi.fn(async () => [0.1, 0.2, 0.3]);
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        await writeNodeWithEdges({ session, embed, ...baseParams({}) });
        expect(embed).toHaveBeenCalledOnce();
        expect(getCreatedProps()?.embedding).toEqual([0.1, 0.2, 0.3]);
    });
    it("does not embed a non-indexed label", async () => {
        const { session } = makeFakeSession("target-1");
        const embed = vi.fn(async () => [0.1]);
        await writeNodeWithEdges({
            session,
            embed,
            ...baseParams({ labels: ["StepResult"] }),
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
        });
        expect(embed).not.toHaveBeenCalled();
    });
    it("does not re-embed when an embedding is already present", async () => {
        const { session, getCreatedProps } = makeFakeSession("target-1");
        const embed = vi.fn(async () => [9, 9, 9]);
        await writeNodeWithEdges({
            session,
            embed,
            ...baseParams({ props: { accountId: "acct-1", givenName: "Ada", embedding: [1, 2] } }),
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
        });
        expect(embed).not.toHaveBeenCalled();
        expect(getCreatedProps()?.embedding).toEqual([1, 2]);
    });
    it("proceeds with the write when the embedder throws", async () => {
        const { session, getCreatedProps } = makeFakeSession("target-1");
        const embed = vi.fn(async () => {
            throw new Error("ollama down");
        });
        const result = await writeNodeWithEdges({
            session,
            embed,
            ...baseParams({}),
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
        });
        expect(result.nodeId).toBe("node-1");
        expect(getCreatedProps()?.embedding).toBeUndefined();
    });
});
//# sourceMappingURL=graph-write-embed-net.test.js.map