import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it, vi } from "vitest";
import { defineQuery } from "./defineQuery";
import type { QueryContext } from "./types";

describe("defineQuery", () => {
  const ctx: QueryContext = { actorId: "user-1" };
  const { db } = createKyselyMock();

  it("calls impl with db, input, and ctx", async () => {
    const impl = vi.fn().mockResolvedValue({ result: "ok" });
    const query = defineQuery("getThing", impl);

    const result = await query(db, { foo: "bar" }, ctx);

    expect(result).toEqual({ result: "ok" });
    expect(impl).toHaveBeenCalledWith(db, { foo: "bar" }, ctx);
  });

  it("passes ctx with actorId to impl", async () => {
    const impl = vi.fn().mockResolvedValue({});
    const query = defineQuery("getThing", impl);

    await query(db, { x: 1 }, ctx);

    expect(impl).toHaveBeenCalledTimes(1);
    expect(impl.mock.calls[0]).toHaveLength(3);
    expect(impl.mock.calls[0][2]).toEqual({ actorId: "user-1" });
  });
});
