diff --git a/app/page.tsx b/app/page.tsx index 488fb1e..f0c7c35 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,5 +1,6 @@ 'use client'; +import { getVehicleDisplayStatus } from '@/lib/utils'; import { useEffect, useState } from 'react'; import { fetchVehicles, getUpdates, fetchGroups, Vehicle, FirmwareUpdate, VehicleGroup } from '@/lib/api'; import { VehicleList } from '@/components/VehicleList'; @@ -33,8 +34,8 @@ export default function Home() { return () => clearInterval(interval); }, []); - const onlineCount = vehicles.filter(v => v.status === 'Online').length; - const updatingCount = vehicles.filter(v => v.status === 'Updating').length; + const onlineCount = vehicles.filter(v => getVehicleDisplayStatus(v) === 'Online').length; + const updatingCount = vehicles.filter(v => getVehicleDisplayStatus(v) === 'Updating').length; return (
diff --git a/lib/utils.test.ts b/lib/utils.test.ts index 878046c..ecf0259 100644 --- a/lib/utils.test.ts +++ b/lib/utils.test.ts @@ -59,4 +59,18 @@ describe("getVehicleDisplayStatus", () => { }; 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"); + }); });