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

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

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

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

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

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

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

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