import { describe, it, expect } from "vitest"; import type { Shipment } from "@/payload-types"; import { getShipmentStatus, isShipmentActive } from "@/lib/shipping/client"; const makeShipment = (overrides: Partial): Shipment => { const base: Partial = { id: 0, name: "Test", status: "dispatched", dispatchedAt: new Date(Date.now() - 30 * 60 * 1000).toISOString(), estimatedArrival: new Date(Date.now() + 30 * 60 * 1000).toISOString(), }; return { ...base, ...overrides } as Shipment; }; describe("getShipmentStatus", () => { it("returns terminal statuses unchanged", () => { expect(getShipmentStatus(makeShipment({ status: "completed" }))).toBe("completed"); expect(getShipmentStatus(makeShipment({ status: "cancelled" }))).toBe("cancelled"); expect(getShipmentStatus(makeShipment({ status: "failed" }))).toBe("failed"); expect(getShipmentStatus(makeShipment({ status: "stranded" }))).toBe("stranded"); }); it("returns active status when within dispatched/ETA window", () => { const s = makeShipment({ status: "dispatched" }); expect(getShipmentStatus(s)).toBe("dispatched"); }); it("returns arrived when past ETA", () => { const s = makeShipment({ status: "dispatched", estimatedArrival: new Date(Date.now() - 60 * 1000).toISOString(), }); expect(getShipmentStatus(s)).toBe("arrived"); }); it("returns dispatched when clock skew (now < dispatchedAt)", () => { const s = makeShipment({ dispatchedAt: new Date(Date.now() + 60 * 1000).toISOString(), estimatedArrival: new Date(Date.now() + 90 * 1000).toISOString(), }); expect(getShipmentStatus(s)).toBe("dispatched"); }); it("returns in_transit when past dispatchedAt but before ETA", () => { const s = makeShipment({ status: "in_transit" }); expect(getShipmentStatus(s)).toBe("in_transit"); }); it("handles missing timestamps gracefully", () => { const s = makeShipment({ status: "dispatched", dispatchedAt: undefined as any, estimatedArrival: undefined as any, }); expect(getShipmentStatus(s)).toBe("dispatched"); }); }); describe("isShipmentActive", () => { it("returns true for dispatched/in_transit", () => { expect(isShipmentActive(makeShipment({ status: "dispatched" }))).toBe(true); expect(isShipmentActive(makeShipment({ status: "in_transit" }))).toBe(true); }); it("returns true for due shipments (status dispatched but past ETA)", () => { const s = makeShipment({ status: "dispatched", estimatedArrival: new Date(Date.now() - 60 * 1000).toISOString(), }); expect(isShipmentActive(s)).toBe(true); }); it("returns false for terminal statuses", () => { expect(isShipmentActive(makeShipment({ status: "completed" }))).toBe(false); expect(isShipmentActive(makeShipment({ status: "cancelled" }))).toBe(false); expect(isShipmentActive(makeShipment({ status: "failed" }))).toBe(false); expect(isShipmentActive(makeShipment({ status: "stranded" }))).toBe(false); }); });