Skip to content
File

Blob: worker/src/shared/contracts/hardware.test.ts

typescript54 lines
1import assert from 'node:assert/strict';
2import { test } from 'node:test';
3import { hardwareSchema } from './hardware.ts';
4import { telemetrySchema } from './robot.ts';
5 
6const hardware = {
7 sampledAtMs: 1000,
8 cpuBusyBps: [null, 2500],
9 chipTemperatureMc: 48000,
10 internal: { free: 200000, minimumFree: 190000, largestBlock: 160000 },
11 psram: { free: 14000000, minimumFree: 13000000, largestBlock: 12000000 },
12 sampleCostUs: 200,
13 samplerStackFree: 5000,
14};
15 
16test('hardware values preserve unavailable readings and reject invalid units or bounds', () => {
17 assert.deepEqual(hardwareSchema.parse(hardware), hardware);
18 assert.equal(
19 hardwareSchema.parse({ ...hardware, chipTemperatureMc: null }).chipTemperatureMc,
20 null,
21 );
22 for (const patch of [
23 { cpuBusyBps: [10001, 0] },
24 { cpuBusyBps: [0] },
25 { chipTemperatureMc: 90000 },
26 { sampleCostUs: -1 },
27 ]) {
28 assert.equal(hardwareSchema.safeParse({ ...hardware, ...patch }).success, false);
29 }
30});
31 
32test('the telemetry contract accepts old publishers without making random a displayed metric', () => {
33 const legacy = {
34 event: 'telemetry',
35 firmware: 'rust',
36 sequence: 1,
37 random: 123,
38 uptimeMs: 1000,
39 positionMs: 0,
40 durationMs: 2000,
41 paused: false,
42 rssi: -60,
43 heap: 200000,
44 led: [0, 0, 0],
45 audioErrors: 0,
46 dataErrors: 0,
47 skippedFrames: 0,
48 };
49 const parsed = telemetrySchema.parse(legacy);
50 assert.equal('random' in parsed, false);
51 assert.equal(parsed.hardware, undefined);
52 assert.equal(telemetrySchema.parse({ ...legacy, hardware, rssi: null }).rssi, null);
53});