import assert from "node:assert/strict"; import { chmodSync, copyFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { assertOffscreenPass, runOffscreen } from "../../test-fixtures/offscreen.mjs"; // The production Resources cadence, offscreen with synthetic systemctl, journalctl // and df: while closed, the failed-service and disk check still feeds the bar chip's // attention and no history or journal work runs; another surface showing rates gets // samples without sparkline history; opening restarts history and reads the journal; // closing stops history again; retry reads the journal once more. Load evidence, not pixels. const directory = mkdtempSync(join(tmpdir(), "nuguland-resources-cadence-")); try { const feature = join(directory, "features", "system"); mkdirSync(join(feature, "prompts"), { recursive: true }); for (const file of ["Resources.qml", "sysinfo.js", "system-problems.js", "resources.js", "prompts/system-repair.md"]) copyFileSync(new URL(file, import.meta.url), join(feature, file)); const bin = join(directory, "bin"); const log = join(directory, "calls.log"); mkdirSync(bin); const programs = { systemctl: "echo systemctl >> \"$CADENCE_LOG\"\necho 'fixture.service loaded failed failed fixture service'", journalctl: "echo journalctl >> \"$CADENCE_LOG\"\nexit 1", df: "echo df >> \"$CADENCE_LOG\"\nprintf 'Filesystem 1024-blocks Used Available Capacity Mounted on\\n/dev/fixture 100 10 90 10%% /\\n'", checkupdates: "exit 2", }; for (const [name, body] of Object.entries(programs)) { writeFileSync(join(bin, name), "#!/bin/sh\n" + body + "\n"); chmodSync(join(bin, name), 0o700); } writeFileSync(join(directory, "shell.qml"), `import QtQuick import Quickshell import Quickshell.Io import "features/system" ShellRoot { id: test property int phase: 0 property int ticks: 0 property int heldRing: 0 property int liveSamples: 0 // Each finished systemctl run reassigns failedUnits. property int checks: 0 function fail(message) { console.error("ERROR resources cadence phase " + phase + ": " + message); Qt.quit(); } function next() { phase++; ticks = 0; } Resources { id: resources powerProfiles: null liveSampleMs: 60 closedSampleMs: 100 closedCheckMs: 150 slowRefreshMs: 100 onFailedUnitsChanged: test.checks++ onPrevRateTimeChanged: if (liveRates) test.liveSamples++ } Process { id: openMark command: ["sh", "-c", "echo open >> \\"$CADENCE_LOG\\""] onExited: resources.popupOpen = true } Timer { interval: 50 running: true repeat: true onTriggered: { test.ticks++; if (test.phase === 0) { // Closed: repeated checks reach the chip; no history, no journal. if (resources.cpuRing.length !== 0 || resources.netRxRing.length !== 0 || resources.incidentStatus !== "not checked") test.fail("closed service kept history or read the journal"); else if (test.checks >= 4 && resources.attention === "error" && resources.failedUnits.length > 0) { resources.liveRates = true; test.next(); } } else if (test.phase === 1) { // Rates shown elsewhere: samples without sparkline history. if (resources.cpuRing.length !== 0 || resources.netRxRing.length !== 0 || resources.netTxRing.length !== 0) test.fail("live rates kept sparkline history"); else if (test.liveSamples >= 2) { resources.liveRates = false; openMark.running = true; test.next(); } } else if (test.phase === 2 && resources.popupOpen && resources.cpuRing.length >= 2 && resources.incidentStatus === "ready") { resources.popupOpen = false; test.next(); } else if (test.phase === 3 && test.ticks === 2) { test.heldRing = resources.cpuRing.length; } else if (test.phase === 3 && test.ticks >= 8) { if (resources.cpuRing.length !== test.heldRing) test.fail("history kept growing after close"); else if (resources.attention !== "error") test.fail("attention lost after close"); else { retryMark.running = true; test.next(); } } else if (test.phase === 4 && test.retried && resources.incidentStatus === "ready") { console.log("resources cadence qml: PASS"); Qt.quit(); } } } // Retry reads the journal at once and stays pending until the result lands. property bool retried: false Process { id: retryMark command: ["sh", "-c", "echo retry >> \\"$CADENCE_LOG\\""] onExited: { resources.retryIncidents(); if (resources.incidentStatus !== "not checked") test.fail("retry did not show pending history"); test.retried = true; } } Timer { interval: 8000 running: true onTriggered: test.fail("deadline") } } `); const run = runOffscreen(join(directory, "shell.qml"), { directory, cwd: directory, timeout: 15000, env: { PATH: `${bin}:/usr/bin`, CADENCE_LOG: log }, }); assertOffscreenPass(run, /resources cadence qml: PASS/); const calls = readFileSync(log, "utf8").trim().split("\n"); const opened = calls.indexOf("open"); assert(opened > 0, "the fixture opened the popup: " + calls.join(",")); const closed = calls.slice(0, opened); assert(closed.filter(call => call === "systemctl").length >= 4, "closed checks repeat the failed-service probe: " + closed.join(",")); assert(closed.includes("df"), "closed checks read disk use"); assert(!closed.includes("journalctl"), "a closed popup never reads the journal"); const retried = calls.indexOf("retry"); assert(calls.slice(opened, retried).includes("journalctl"), "opening reads the journal"); assert(calls.slice(retried).includes("journalctl"), "retry reads the journal again although the popup polls nothing"); console.log("resources-cadence.test.mjs: PASS (offscreen production cadence; not pointer, keyboard, or pixel proof)"); } finally { rmSync(directory, { recursive: true, force: true }); }