import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } 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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseFiscalYear]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.fiscalYear).toEqual(baseFiscalYear);
    }
    expect(mock.selects).toHaveLength(1);
    expect(mock.selects[0].parameters).toContain(baseFiscalYear.id);
    expect(mock.selects[0].sql).toMatch(/"id"/);
  });

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

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

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