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

describe("getJournalEntry", () => {
  it("returns journal entry with journal lines when found by id", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select
      .mockReturnValueOnce(baseJournalEntry)
      .mockReturnValueOnce([baseJournalLine, baseJournalLine2]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.journalEntry).toEqual(baseJournalEntry);
      expect(result.value.journalLines).toEqual([baseJournalLine, baseJournalLine2]);
    }
  });

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