import { describe, expect, it, vi } from "vitest";
import { defineQuery } from "./defineQuery";
import type { QueryContext } from "./types";

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

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

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

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

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

    await query("fake-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" });
  });
});
