import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import { expectOk } from "../testing/commandTestUtils";
import { baseGoodsReceiptExecution, baseGoodsReceiptExecutionLine } from "../testing/fixtures";
import { run } from "./listGoodsReceipts";

describe("listGoodsReceipts", () => {
  it("lists goods receipt executions with lines", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select
      .mockReturnValueOnce([baseGoodsReceiptExecution])
      .mockReturnValueOnce([baseGoodsReceiptExecutionLine]);

    const result = await run(db, {});
    const { items, hasNextPage } = expectOk<{
      items: { inventoryExecutionLines: unknown[] }[];
      hasNextPage: boolean;
    }>(result as never);

    expect(hasNextPage).toBe(false);
    expect(items).toHaveLength(1);
    expect(items[0].inventoryExecutionLines).toHaveLength(1);
    expect(spies.where).toHaveBeenCalledWith("executionType", "=", "GOODS_RECEIPT");
  });

  it("returns empty list when no matches", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([]);

    const result = await run(db, { status: "POSTED" });
    const { items, hasNextPage } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(
      result as never,
    );

    expect(items).toHaveLength(0);
    expect(hasNextPage).toBe(false);
  });
});
