// power.test.js — node test for the pure power module. // Run: node power.test.js (must print "power.test.js: ALL PASS") var assert = require('assert'); var P = require('./power.js'); var passed = 0; function eq(got, want, msg) { assert.strictEqual(got, want, msg); passed++; } function deep(got, want, msg) { assert.deepStrictEqual(got, want, msg); passed++; } function ok(cond, msg) { assert(cond, msg); passed++; } // --- minutesToLabel -------------------------------------------------------- eq(P.minutesToLabel(0), 'unknown', 'zero seconds unknown'); eq(P.minutesToLabel(2700), '45m', '45 minutes'); eq(P.minutesToLabel(7200), '2h', 'exact hours drop minutes'); eq(P.minutesToLabel(4800), '1h 20m', 'hours and minutes'); eq(P.minutesToLabel(-50), 'unknown', 'negative clamps to unknown'); // --- batteryRole ----------------------------------------------------------- eq(P.batteryRole(8), 'error', 'critical'); eq(P.batteryRole(10), 'error', 'boundary 10'); eq(P.batteryRole(20), 'warning', 'low'); eq(P.batteryRole(25), 'warning', 'boundary 25'); eq(P.batteryRole(60), 'ui.accent', 'healthy'); // --- timeEstimateLabel ----------------------------------------------------- eq(P.timeEstimateLabel('charging', 3600, 0), '1h', 'charging uses time-to-full'); eq(P.timeEstimateLabel('discharging', 0, 5400), '1h 30m', 'discharging uses time-to-empty'); // --- batteryMeta: the estimate says what it counts ---------------------------- eq(P.batteryMeta({ state: 'discharging', timeEstimate: '3 hours', health: 94 }), '3 hours left · health 94%', 'discharging counts time left'); eq(P.batteryMeta({ state: 'charging', timeEstimate: '1h', health: -1 }), '1h to full', 'charging counts time to full'); eq(P.batteryMeta({ state: 'full', timeEstimate: 'unknown', health: 91 }), 'health 91%', 'an unknown estimate is left out'); eq(P.batteryMeta({}), '', 'nothing known is an empty line'); // --- isLowPower ------------------------------------------------------------ eq(P.isLowPower('discharging'), true, 'discharging is low power'); eq(P.isLowPower('pendingdischarge'), true, 'pending discharge is low power'); eq(P.isLowPower('charging'), false, 'charging is not'); eq(P.isLowPower('full'), false, 'full is not'); // --- hasMainBattery -------------------------------------------------------- eq(P.hasMainBattery({ percent: 0, state: 'unknown', timeEstimate: 'unknown', health: -1 }), false, 'desktop display device placeholder is not a main battery'); eq(P.hasMainBattery({ percent: 72, state: 'charging', timeEstimate: '1h', health: 91 }), true, 'real laptop battery is a main battery'); eq(P.hasMainBattery({ percent: 0, state: 'discharging', timeEstimate: 'unknown', health: -1 }), true, 'reported discharging state is a main battery even at zero percent'); // --- batteryAttention: the bar chip asks only for a low battery ----------- eq(P.batteryAttention({ percent: 72, state: 'discharging' }), '', 'healthy discharge stays quiet'); eq(P.batteryAttention({ percent: 25, state: 'discharging' }), 'warning', 'low discharge warns at the boundary'); eq(P.batteryAttention({ percent: 18, state: 'pendingdischarge' }), 'warning', 'pending discharge counts'); eq(P.batteryAttention({ percent: 10, state: 'discharging' }), 'error', 'critical at the boundary'); eq(P.batteryAttention({ percent: 0, state: 'discharging' }), 'error', 'empty discharging battery is critical'); eq(P.batteryAttention({ percent: 8, state: 'charging' }), '', 'charging needs nothing from the user'); eq(P.batteryAttention({ percent: 8, state: 'pendingcharge' }), '', 'plugged in but not charging yet stays quiet'); eq(P.batteryAttention({ percent: 0, state: 'unknown', timeEstimate: 'unknown', health: -1 }), '', 'desktop never asks'); eq(P.batteryAttention(null), '', 'missing battery never asks'); // --- gridStep: arrow focus in the three-column action grid ---------------- eq(P.gridStep(3, 6, 3, 'up'), 0, 'up moves one row'); eq(P.gridStep(0, 6, 3, 'down'), 3, 'down moves one row'); eq(P.gridStep(1, 6, 3, 'left'), 0, 'left moves one cell'); eq(P.gridStep(1, 6, 3, 'right'), 2, 'right moves one cell'); eq(P.gridStep(2, 6, 3, 'right'), 2, 'right edge holds, no wrap into the next row'); eq(P.gridStep(3, 6, 3, 'left'), 3, 'left edge holds, no wrap into the previous row'); eq(P.gridStep(1, 6, 3, 'up'), 1, 'top edge holds'); eq(P.gridStep(4, 6, 3, 'down'), 4, 'bottom edge holds'); eq(P.gridStep(1, 5, 3, 'down'), 4, 'down reaches a partial row'); eq(P.gridStep(2, 5, 3, 'down'), 2, 'down never leaves the grid on a partial row'); eq(P.gridStep(4, 5, 3, 'right'), 4, 'right never leaves a partial row'); eq(P.gridStep(4, 6, 3, 'enter'), 4, 'other keys never move'); // --- requiresConfirmation -------------------------------------------------- eq(P.requiresConfirmation('poweroff'), true, 'poweroff confirms'); eq(P.requiresConfirmation('reboot'), true, 'reboot confirms'); eq(P.requiresConfirmation('logout'), true, 'logout confirms'); eq(P.requiresConfirmation('suspend'), false, 'suspend is recoverable, no confirm'); eq(P.requiresConfirmation('lock'), false, 'lock no confirm'); eq(P.requiresConfirmation('monitor-off'), false, 'monitor-off no confirm'); // --- actionPlan: the routing table (the safety-critical part) -------------- deep(P.actionPlan('lock'), { via: 'lock' }, 'lock routes to lockService'); deep(P.actionPlan('monitor-off'), { via: 'niri', args: ['power-off-monitors'] }, 'monitor-off via niri'); deep(P.actionPlan('monitor-on'), { via: 'niri', args: ['power-on-monitors'] }, 'monitor-on via niri'); deep(P.actionPlan('logout'), { via: 'niri', args: ['quit', '--skip-confirmation'] }, 'logout = niri quit, skip its prompt'); deep(P.actionPlan('suspend'), { via: 'exec', argv: ['systemctl', 'suspend'] }, 'suspend = systemctl suspend'); deep(P.actionPlan('reboot'), { via: 'exec', argv: ['systemctl', 'reboot'] }, 'reboot = systemctl reboot'); deep(P.actionPlan('poweroff'), { via: 'exec', argv: ['systemctl', 'poweroff'] }, 'poweroff = systemctl poweroff'); deep(P.actionPlan('bogus'), { via: 'none' }, 'unknown action does nothing'); // argv/args must be discrete tokens, never a single shell string ok(Array.isArray(P.actionPlan('poweroff').argv) && P.actionPlan('poweroff').argv.length === 2, 'poweroff argv is token array (no shell injection)'); // every advertised session action has a real plan (no silent no-ops) P.SESSION_ACTIONS.forEach(function (a) { ok(P.actionPlan(a).via !== 'none', 'session action "' + a + '" has a real route'); }); // --- actionLabel ----------------------------------------------------------- eq(P.actionLabel('monitor-off'), 'mon off', 'monitor-off label'); eq(P.actionLabel('poweroff'), 'power', 'poweroff label'); eq(P.actionLabel('lock'), 'lock', 'passthrough label'); console.log(passed + ' assertions passed'); console.log('power.test.js: ALL PASS');