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

describe("getTaxonomyNodeChildren", () => {
  it("returns children when found", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseChildNode]);

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

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

  it("returns multiple children", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseChildNode, baseLeafNode]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.children).toHaveLength(2);
    }
  });

  it("returns empty array when no children", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([]);

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

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