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

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

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

      expect(result.ok).toBe(true);
      if (result.ok) {
        expect(result.value.uoMCategory).toEqual(baseUoMCategory);
      }
    });

    it("returns null when category 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.uoMCategory).toBeNull();
      }
    });
  });

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

      const result = await run(db, { name: "Weight" });

      expect(result.ok).toBe(true);
      if (result.ok) {
        expect(result.value.uoMCategory).toEqual(baseUoMCategory);
      }
    });

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

      const result = await run(db, { name: "Nonexistent" });

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