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

describe("getTaxonomyNode", () => {
  describe("by id", () => {
    it("returns node when found", async () => {
      const { db, spies } = createMockDb<DB>();
      spies.select.mockReturnValue(baseRootNode);

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

      expect(result.ok).toBe(true);
      if (result.ok) {
        expect(result.value.node).toEqual(baseRootNode);
      }
    });

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

      const result = await run(db, { id: "nonexistent" });

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

  describe("by code", () => {
    it("returns node when found", async () => {
      const { db, spies } = createMockDb<DB>();
      spies.select.mockReturnValue(baseRootNode);

      const result = await run(db, { code: "ELECTRONICS" });

      expect(result.ok).toBe(true);
      if (result.ok) {
        expect(result.value.node).toEqual(baseRootNode);
      }
    });

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

      const result = await run(db, { code: "NONEXISTENT" });

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