Files
corescope-rx/test/battery.test.mjs
T
efiten 840475e298 Grey the battery reading and mark it 'last known' while the link is down
After a BLE drop the last voltage stayed on screen as if current, next
to the link-down banner. renderBattery now runs every monitor tick with
the link state.
2026-11-29 22:52:25 +01:00

52 lines
1.9 KiB
JavaScript

// Companion battery: frame parsing, poll cadence and the low-battery warning (issue #60).
// Run: node --test
import { test } from 'node:test';
import assert from 'node:assert';
import {
buildBatteryRequest, parseBattery, batteryDue, batteryText, batteryWarning,
BATTERY_POLL_MS, BATTERY_LOW_MV,
} from '../src/battery.js';
test('the request is the single command byte 20', () => {
assert.deepEqual([...buildBatteryRequest()], [20]);
});
test('parses the millivolts from a full RESP_CODE_BATT_AND_STORAGE frame', () => {
// 3920 mV = 0x0F50, used 12 KB, total 256 KB
const f = new Uint8Array([12, 0x50, 0x0f, 12, 0, 0, 0, 0, 1, 0, 0]);
assert.equal(parseBattery(f), 3920);
});
test('ignores other reply codes and short frames', () => {
assert.equal(parseBattery(new Uint8Array([24, 0, 0x50, 0x0f])), null);
assert.equal(parseBattery(new Uint8Array([12, 0x50])), null);
assert.equal(parseBattery(null), null);
});
test('asks right away, then once per poll interval', () => {
assert.equal(batteryDue(null, 1000), true);
assert.equal(batteryDue(1000, 1000 + BATTERY_POLL_MS - 1), false);
assert.equal(batteryDue(1000, 1000 + BATTERY_POLL_MS), true);
});
test('shows volts with two decimals, nothing for 0 mV', () => {
assert.equal(batteryText(3920), '3.92 V');
assert.equal(batteryText(0), '');
assert.equal(batteryText(null), '');
});
test('marks the reading as last known while the link is down', () => {
assert.equal(batteryText(3920, true), '3.92 V (last known)');
assert.equal(batteryText(0, true), '');
assert.match(batteryWarning(3300, true), /3\.30 V \(last known\)/);
assert.equal(batteryWarning(4100, true), '');
});
test('warns only below the threshold, never for 0 mV', () => {
assert.equal(batteryWarning(BATTERY_LOW_MV), '');
assert.equal(batteryWarning(4100), '');
assert.equal(batteryWarning(0), '');
assert.match(batteryWarning(BATTERY_LOW_MV - 1), /3\.50 V/);
assert.match(batteryWarning(3300), /battery low: 3\.30 V/);
});