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

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

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

      expect(result.ok).toBe(true);
      if (result.ok) {
        expect(result.value.currency).toEqual(baseCurrencyUSD);
      }
    });

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

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

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

      expect(result.ok).toBe(true);
      if (result.ok) {
        expect(result.value.currency).toEqual(baseCurrencyUSD);
      }
    });

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