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

describe("listProductAttributeValues", () => {
  it("returns values when found", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseColorRed, baseColorBlue]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.values).toEqual([baseColorRed, baseColorBlue]);
    }

    expect(mock.selects).toHaveLength(1);
    expect(mock.selects[0].sql).toMatch(/"attributeId"/);
    expect(mock.selects[0].parameters).toContain("attr-1");
    expect(mock.selects[0].sql).toMatch(/order by .*"createdAt" asc/i);
  });

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

    const result = await run(mock.db, { attributeId: "attr-99" });

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