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

describe("getFiscalYear", () => {
  it("returns fiscal year when found by id", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue(baseFiscalYear);

    const result = await run(db, { id: baseFiscalYear.id });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.fiscalYear).toEqual(baseFiscalYear);
    }
  });

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