import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { baseAccountingPeriod } from "../testing/fixtures";
import { run } from "./getAccountingPeriod";

describe("getAccountingPeriod", () => {
  it("returns accounting period when found by id", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseAccountingPeriod]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.accountingPeriod).toEqual(baseAccountingPeriod);
    }
    expect(mock.selects).toHaveLength(1);
    expect(mock.selects[0].parameters).toContain(baseAccountingPeriod.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.accountingPeriod).toBeNull();
    }
  });
});
