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

describe("getProductCategoryAssignment", () => {
  it("returns assignment when found", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue(baseCategoryAssignment);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.assignment).toEqual(baseCategoryAssignment);
    }
  });

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

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

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