import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import { baseVariant } from "../testing/fixtures";
import { run } from "./getProductVariant";

describe("getProductVariant", () => {
  it("returns variant by id", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue(baseVariant);

    const result = await run(db, { id: "variant-1" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.variant).toEqual(baseVariant);
    }
  });

  it("returns variant by productId and axisValueKey", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue(baseVariant);

    const result = await run(db, { productId: "product-2", axisValueKey: "value-1|value-3" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.variant).toEqual(baseVariant);
    }
  });

  it("returns null when not found", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue(undefined);

    const result = await run(db, { id: "nonexistent" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.variant).toBeNull();
    }
  });
});
