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

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

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

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

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

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

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

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