Ignore 0% battery readings in low-battery alerting
CI / build + unit (pull_request) Successful in 1m18s
CI / build + unit (pull_request) Successful in 1m18s
A 0% batteryLevel is an invalid/no-data value (no battery wired or
misconfigured node), not a genuinely critical battery, so it must not
produce a "⚠️ battery low" alert. Add a non-positive guard to
shouldAlert after the undefined check: 0 (and any non-positive) now
skips, while 1-20 still alert and >20 still doesn't. Threshold/cooldown/
batching unchanged.
Tests: 3 new specs in the check-threshold block (0% excluded from the
batch, lone 0% sends nothing, 1% still alerts). 180 -> 183.
This commit is contained in:
@@ -119,6 +119,47 @@ describe('BatteryAlertService check threshold', () => {
|
||||
expect(sendText).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('ignores a 0% reading (invalid/no battery) even though 0 <= threshold', async () => {
|
||||
const sendText = vi.fn(async (..._args: unknown[]) => 1);
|
||||
// 0% must not alert, but a genuinely-low 5% node in the same batch must
|
||||
const svc = makeService([node(1, 0), node(2, 5)], sendText);
|
||||
|
||||
await svc.check();
|
||||
|
||||
expect(sendText).toHaveBeenCalledTimes(1);
|
||||
// only the 5% node — the 0% node is excluded entirely
|
||||
expect(sendText.mock.calls[0]).toEqual([
|
||||
BROADCAST_ADDR,
|
||||
'⚠️ battery low: @!00000002 5%',
|
||||
undefined,
|
||||
2,
|
||||
]);
|
||||
});
|
||||
|
||||
it('ignores a lone 0% reading (no broadcast at all)', async () => {
|
||||
const sendText = vi.fn(async (..._args: unknown[]) => 1);
|
||||
const svc = makeService([node(1, 0)], sendText); // only an invalid 0% reading
|
||||
|
||||
await svc.check();
|
||||
|
||||
expect(sendText).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('still alerts a genuine 1% reading (lowest valid value)', async () => {
|
||||
const sendText = vi.fn(async (..._args: unknown[]) => 1);
|
||||
const svc = makeService([node(1, 1)], sendText);
|
||||
|
||||
await svc.check();
|
||||
|
||||
expect(sendText).toHaveBeenCalledTimes(1);
|
||||
expect(sendText.mock.calls[0]).toEqual([
|
||||
BROADCAST_ADDR,
|
||||
'⚠️ battery low: @!00000001 1%',
|
||||
undefined,
|
||||
2,
|
||||
]);
|
||||
});
|
||||
|
||||
it('merges all qualifying nodes into exactly ONE broadcast with the broadcast target', async () => {
|
||||
const sendText = vi.fn(async (..._args: unknown[]) => 1);
|
||||
const svc = makeService(
|
||||
|
||||
@@ -60,6 +60,7 @@ export class BatteryAlertService {
|
||||
|
||||
private shouldAlert(node: NodeSummary, now: number): boolean {
|
||||
if (node.batteryLevel === undefined) return false; // no battery reported — skip
|
||||
if (node.batteryLevel <= 0) return false; // 0/invalid reading — no battery wired or misconfigured node, not a real low-battery alert
|
||||
// <= on purpose: a node at exactly the threshold is already "low" (spec allowed either < or <=; tests pin this choice)
|
||||
if (!(node.batteryLevel <= this.threshold)) return false;
|
||||
const last = this.lastAlertAt.get(node.num);
|
||||
|
||||
Reference in New Issue
Block a user