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

describe("getTrialBalance", () => {
  it("returns trial balance with lines when found by id", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(baseTrialBalance).mockReturnValueOnce([baseTrialBalanceLine]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.trialBalance).toEqual(baseTrialBalance);
      expect(result.value.lines).toEqual([baseTrialBalanceLine]);
    }
  });

  it("returns null with empty lines 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.trialBalance).toBeNull();
      expect(result.value.lines).toEqual([]);
    }
  });
});
