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

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

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

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

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

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

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

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

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