repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by admin
quickshell/nuguland/features/system/sparkline.test.mjs
Rawimport { copyFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { assertOffscreenPass, runOffscreen } from "../../test-fixtures/offscreen.mjs";
// Sparkline bars follow new samples without being rebuilt, keep the newest that fit,
// and split up/down around the midline; offscreen geometry, not pixels.
const directory = mkdtempSync(join(tmpdir(), "nuguland-sparkline-"));
try {
for (const file of ["Theme.qml", "nugu.json"])
copyFileSync(new URL("../../" + file, import.meta.url), join(directory, file));
const feature = join(directory, "features", "system");
mkdirSync(feature, { recursive: true });
copyFileSync(new URL("Sparkline.qml", import.meta.url), join(feature, "Sparkline.qml"));
writeFileSync(join(directory, "shell.qml"), `import QtQuick
import QtQuick.Window
import Quickshell
import "features/system"
ShellRoot {
id: test
property int step: 0
property var firstBar: null
function rectangles(item) {
let result = [];
for (const child of item.children || [])
result = result.concat(child instanceof Rectangle ? [child] : [], rectangles(child));
return result;
}
function bars(item) {
let result = [];
for (const child of item.children || []) {
if (child instanceof Rectangle && child.visible && child.parent.parent.visible) {
const point = child.mapToItem(spark, 0, 0);
result.push([point.x, point.y, child.height].map(value => Math.round(value * 10) / 10).join(":"));
}
result = result.concat(bars(child));
}
return result;
}
function expect(expected) {
const actual = bars(spark).sort((a, b) => Number(a.split(":")[0]) - Number(b.split(":")[0]) || Number(a.split(":")[1]) - Number(b.split(":")[1])).join(" ");
if (actual !== expected) {
console.error("ERROR sparkline step " + step + ": " + actual + " != " + expected);
Qt.quit();
}
}
Theme { id: theme }
Window {
visible: true
width: 40
height: 40
Sparkline {
id: spark
theme: theme
width: 8
height: 21
}
}
Timer {
interval: 25
running: true
repeat: true
onTriggered: {
// x:y:height per bar; width 8 shows the newest four 2 px bars, right-aligned,
// scaled to the shown maximum or scaleMax; split halves are 10 px each
// around the pixel-aligned midline at y 11.
if (test.step === 0) {
spark.values = [1, 3];
} else if (test.step === 1) {
test.expect("4:14:7 6:0:21");
test.firstBar = test.rectangles(spark)[0];
spark.values = [3, 1];
} else if (test.step === 2) {
test.expect("4:0:21 6:14:7");
// Same-length samples reuse the bars instead of rebuilding them.
if (test.rectangles(spark)[0] !== test.firstBar) {
console.error("ERROR sparkline step 2: bars were rebuilt");
Qt.quit();
}
spark.values = [9, 1, 1, 1, 1, 3];
} else if (test.step === 3) {
test.expect("0:14:7 2:14:7 4:14:7 6:0:21");
spark.scaleMax = 7;
} else if (test.step === 4) {
test.expect("0:18:3 2:18:3 4:18:3 6:12:9");
spark.scaleMax = 0;
spark.split = true;
spark.values = [4, 0];
spark.valuesDown = [2, 8];
} else if (test.step === 5) {
test.expect("4:5:5 4:12:2.5 6:10:0 6:12:10");
} else {
console.log("sparkline qml: PASS");
Qt.quit();
}
test.step++;
}
}
Timer {
interval: 5000
running: true
onTriggered: {
console.error("ERROR sparkline: deadline");
Qt.quit();
}
}
}
`);
const run = runOffscreen(join(directory, "shell.qml"), { directory, cwd: directory, timeout: 10000 });
assertOffscreenPass(run, /sparkline qml: PASS/);
console.log("sparkline.test.mjs: PASS (offscreen bar geometry; not pixel proof)");
} finally {
rmSync(directory, { recursive: true, force: true });
}