Commit of Current Status

This commit is contained in:
Denis Urs Rudolph
2025-12-11 20:58:10 +01:00
parent d492352d50
commit 6de23e0e77
10 changed files with 827 additions and 58 deletions

62
lib/utils.test.ts Normal file
View File

@@ -0,0 +1,62 @@
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");
});
});