Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/features/power/fixtures/production.mjs

Raw
import assert from 'node:assert/strict';
import { spawnSync } from 'node:child_process';
import { readFileSync, writeFileSync } from 'node:fs';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const { detectIssues } = require('../../system/sysinfo.js');

export default async function powerProduction({ env, start, stop, waitFor, ipc, setFixture }) {
    assert.equal(process.env.NUGULAND_UI_SANDBOX, '1');
    const build = spawnSync('cc', ['-std=c17', '-Wall', '-Wextra', '-Werror', '/project/features/power/fixtures/upower-standin.c', '-lsystemd', '-o', '/tmp/upower-standin'], { env, encoding: 'utf8', timeout: 10000 });
    assert.equal(build.status, 0, build.stderr);
    const standin = start('/tmp/upower-standin', [], 'power-upower');
    let child;
    try {
        await waitFor('private UPower stand-in', () => readFileSync('/evidence/power-upower.log', 'utf8').includes('upower fixture ready'));
        const fixture = '/project/power-native-probe.qml';
        writeFileSync(fixture, readFileSync('/project/features/power/fixtures/native-probe.qml', 'utf8').replaceAll('import "../../../features/', 'import "./features/'));
        child = start('qs', ['--no-color', '-p', fixture], 'power-native-probe');
        setFixture(fixture, '/evidence/power-native-probe.log', child);
        const snapshot = () => JSON.parse(ipc('snapshot'));
        await waitFor('power native probe loaded', () => readFileSync('/evidence/power-native-probe.log', 'utf8').includes('Configuration Loaded'));
        const initial = await waitFor('battery ready', () => { const s = snapshot(); return s.ready && s.laptop && s.health >= 0 ? s : null; });
        assert.equal(initial.percent, 1, 'raw 1% becomes 0.01 in Quickshell, then 1%');
        assert.equal(initial.health, 95, 'raw Capacity 95 remains 95%');
        const issue = health => detectIssues({ isLaptopBattery: true, healthPercentage: health }).find(i => i.id === 'battery-health');
        assert.equal(issue(initial.health), undefined, 'healthy battery has no wear issue');
        assert.equal(issue(79).severity, 'warning');
        assert.equal(issue(59).severity, 'error');
        for (const [value, state, status, low] of [[0, 'unknown', '', false], [1, 'charging', 'charging', false], [2, 'discharging', 'on battery', true], [3, 'empty', '', false], [4, 'full', 'full', false], [5, 'pendingcharge', 'charging', false], [6, 'pendingdischarge', 'on battery', true]]) {
            const call = spawnSync('busctl', ['--address', env.DBUS_SYSTEM_BUS_ADDRESS, 'call', 'org.freedesktop.UPower', '/org/freedesktop/UPower', 'org.freedesktop.UPower', 'SetFixtureState', 'u', String(value)], { env, encoding: 'utf8', timeout: 10000 });
            assert.equal(call.status, 0, call.stderr);
            await waitFor(`native state ${state}`, () => { const s = snapshot(); return s.state === state && s.status === status && s.low === low; });
        }
        console.log('pass: private UPower percent, health, issue thresholds, enum and reactive low power');
    } finally {
        if (child) await stop(child);
        await stop(standin);
    }
}