import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { expectOk } from "../testing/commandTestUtils";
import { baseShipment, postedShipment, baseShipmentLine } from "../testing/fixtures";
import { run } from "./getShipment";

describe("getShipment", () => {
  it("returns shipment when found by id", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseShipment], [baseShipmentLine]);

    const result = await run(mock.db, { id: baseShipment.id });
    const { shipment } = expectOk<{ shipment: unknown }>(result as never);

    expect(shipment).not.toBeNull();
    expect(shipment).toMatchObject({ id: baseShipment.id });
  });

  it("returns posted shipment with lines", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([postedShipment], [baseShipmentLine]);

    const result = await run(mock.db, { id: postedShipment.id });
    const { shipment } = expectOk<{
      shipment: { status: string; lines: unknown[] };
    }>(result as never);

    expect(shipment.status).toBe("POSTED");
    expect(shipment.lines).toHaveLength(1);
  });

  it("returns null when shipment is not found", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await run(mock.db, { id: "non-existent-id" });
    const { shipment } = expectOk<{ shipment: null }>(result as never);

    expect(shipment).toBeNull();
  });
});
