import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } 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 { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue(baseAccountingPeriod);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.accountingPeriod).toEqual(baseAccountingPeriod);
    }
  });

  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.accountingPeriod).toBeNull();
    }
  });
});
