import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { baseVariant } from "../testing/fixtures";
import { run } from "./listProductVariants";

describe("listProductVariants", () => {
  it("returns variants when found", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseVariant]);

    const result = await run(mock.db, { productId: "product-2" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.variants).toEqual([baseVariant]);
    }

    expect(mock.selects).toHaveLength(1);
    expect(mock.selects[0].sql).toMatch(/"productId"/);
    expect(mock.selects[0].parameters).toContain("product-2");
  });

  it("returns empty array when no variants", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await run(mock.db, { productId: "product-1" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.variants).toEqual([]);
    }
  });
});
