1
0
Fork 0
polaris-task-force/tests/int/shipping-client.int.spec.ts
Z8MB1E f8ce3f3dcc feat(logistics): timestamp shipment lifecycle with client relay
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-09-20 02:43:29 -04:00

80 lines
3 KiB
TypeScript

import { describe, it, expect } from "vitest";
import type { Shipment } from "@/payload-types";
import { getShipmentStatus, isShipmentActive } from "@/lib/shipping/client";
const makeShipment = (overrides: Partial<Shipment>): Shipment => {
const base: Partial<Shipment> = {
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);
});
});