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

describe("getPeriodClose", () => {
  it("returns period close when found by accounting period id", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue(basePeriodClose);

    const result = await run(db, { accountingPeriodId: basePeriodClose.accountingPeriodId });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.periodClose).toEqual(basePeriodClose);
    }
  });

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

    const result = await run(db, { accountingPeriodId: "nonexistent" });

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