import { describe, it, expect, setSystemTime, beforeAll, afterAll } from "bun:test"; import { getVehicleDisplayStatus } from "./utils"; import { Vehicle } from "./api"; describe("getVehicleDisplayStatus", () => { beforeAll(() => { setSystemTime(new Date("2024-01-01T12:00:10Z")); // Mock Current Time: 12:00:10 UTC }); afterAll(() => { setSystemTime(); // Reset }); it("should return Online if heartbeat is recent (<5s)", () => { const vehicle: Vehicle = { vin: "TEST", status: "Online", currentVersion: "1.0", lastHeartbeat: "2024-01-01T12:00:08", // 2 seconds ago (assuming UTC fix) groupId: null }; // The function appends Z -> 12:00:08Z. Diff is 2000ms. expect(getVehicleDisplayStatus(vehicle)).toBe("Online"); }); it("should return Offline if heartbeat is old (>5s)", () => { const vehicle: Vehicle = { vin: "TEST", status: "Online", currentVersion: "1.0", lastHeartbeat: "2024-01-01T12:00:04", // 6 seconds ago groupId: null }; // The function appends Z -> 12:00:04Z. Diff is 6000ms. expect(getVehicleDisplayStatus(vehicle)).toBe("Offline"); }); it("should handle already UTC string with Z", () => { const vehicle: Vehicle = { vin: "TEST", status: "Online", currentVersion: "1.0", lastHeartbeat: "2024-01-01T12:00:09Z", // 1s ago groupId: null }; expect(getVehicleDisplayStatus(vehicle)).toBe("Online"); }); it("should preserve Updating status even if old heartbeat", () => { // Usually updating vehicles might not heartbeat? Or they do. // Logic says: if (displayStatus === 'Online' && diffMs > 5000) -> Offline. // If status is 'Updating', it stays 'Updating'. const vehicle: Vehicle = { vin: "TEST", status: "Updating", currentVersion: "1.0", lastHeartbeat: "2024-01-01T12:00:00", // 10s ago groupId: null }; expect(getVehicleDisplayStatus(vehicle)).toBe("Updating"); }); it("should handle microsecond precision timestamps", () => { // Backend sends 6 digit precision e.g. .991694 // 2024-01-01T12:00:04.991694 -> 6s ago const vehicle: Vehicle = { vin: "TEST", status: "Online", currentVersion: "1.0", lastHeartbeat: "2024-01-01T12:00:04.991694", groupId: null }; // If it parses correctly, diff is ~6s > 5s -> Offline expect(getVehicleDisplayStatus(vehicle)).toBe("Offline"); }); });