repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by admin
quickshell/nuguland/surface-geometry.test.mjs
Rawimport assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import vm from "node:vm";
import { createRequire } from "node:module";
const { registryEntries } = createRequire(import.meta.url)("./test-fixtures/sources.js");
const qml = readFileSync(new URL("./SurfaceManager.qml", import.meta.url), "utf8");
// The registry is the only source of popup names and sizes.
const diagnostics = vm.createContext({});
vm.runInContext(readFileSync(new URL("./diagnostics.js", import.meta.url), "utf8").replace(/^\.(?:pragma|import).*$/gm, ""), diagnostics);
const start = qml.indexOf(" function dismissTask(");
const end = qml.indexOf(" Timer {", start);
assert.ok(start >= 0 && end > start, "execute the actual SurfaceManager functions");
// Registry features come from Registry.qml itself; never mirror their data here.
// An entry whose height binds to service state has no literal height here.
const features = registryEntries().filter(entry => entry.kind === "popup").map(entry => ({
width: 520, empty: false, ...entry, retainedState: { scrollY: 0 }, opening() {}, dismissed() {},
}));
const sized = features.filter(feature => typeof feature.height === "number");
const registry = { popups: features, popup: name => features.find(feature => feature.name === String(name)) || null };
const surfaces = {
registry, screenWidth: 0, screenHeight: 0, barHeight: 34, vertical: false, popupViewport: null,
goldenRatio: 1.61803,
DiagnosticData: diagnostics, operationId: 0, console: { info() {} },
popupName: "clock", panelHeight: 0, panelSettled: false, popupAnchor: { x: 100, y: 0, width: 40, height: 34 },
popupOpen: false, popupPhase: "closed", popupOutput: "",
popupProgress: 0, lastCloseReason: "", dismissing() {}, niriService: null, returnWindowId: null,
Qt: { rect: (x, y, width, height) => ({ x, y, width, height }) },
openTimer: { stop() {}, restart() {} },
};
surfaces.root = surfaces;
vm.createContext(surfaces);
vm.runInContext(qml.slice(start, end).replaceAll('anchor: rect', 'anchor'), surfaces);
const anchor = { x: 100, y: 0, width: 40, height: 34 };
const plain = value => JSON.parse(JSON.stringify(value));
const sizes = Object.fromEntries(sized.map(feature => [feature.name, feature]));
const heights = Object.fromEntries(Object.entries(sizes).map(([name, entry]) => [name, entry.height]));
const widths = Object.fromEntries(Object.entries(sizes).map(([name, entry]) => [name, entry.width]));
for (const [screenWidth, vertical] of [[0, false], [300, false], [1366, false], [2560, false], [1366, true], [34, true], [35, true]]) {
Object.assign(surfaces, { screenWidth, vertical });
const available = screenWidth - (vertical ? 34 : 0);
for (const [name, height] of Object.entries(heights)) {
const width = available > 0 ? Math.min(widths[name], Math.max(1, available - 16)) : widths[name];
assert.equal(surfaces.popupWidth(name), width);
assert.deepEqual(plain(surfaces.popupContentSize(name)), { width, height }, `${name}: width ${screenWidth}, vertical ${vertical}`);
}
}
Object.assign(surfaces, { screenWidth: 1366, vertical: false });
for (const name of ["", "unknown", "screenshots", "toString", "constructor", "__proto__", null, undefined, 42]) {
assert.deepEqual(plain(surfaces.popupContentSize(name)), { width: 520, height: 300 }, `unknown name ${String(name)}`);
}
assert.deepEqual(plain(surfaces.popupContentSize({ toString: () => "clock" })), { width: 760, height: 580 });
for (const [screenHeight, vertical, ceiling] of [[0, false, 680], [600, false, 518], [600, true, 552], [200, false, 260], [1080, false, 998]]) {
Object.assign(surfaces, { screenHeight, vertical });
assert.equal(surfaces.heightBudget(2000), ceiling);
// Wanted heights are never inflated, only capped.
assert.equal(surfaces.heightBudget(104), 104);
assert.equal(surfaces.heightBudget("300"), Math.min(ceiling, 300));
for (const [name, height] of Object.entries(heights)) {
assert.equal(surfaces.popupContentSize(name).height, Math.min(ceiling, height), `${name}: screen height ${screenHeight}, vertical ${vertical}`);
}
}
// A registry height bound to service state is used as is: never inflated, only
// capped by the screen budget.
const dynamic = { name: "dynamic", width: 368, height: 188, empty: false, retainedState: { scrollY: 0 }, opening() {}, dismissed() {} };
features.push(dynamic);
Object.assign(surfaces, { screenWidth: 1366, screenHeight: 1080, vertical: false });
for (const height of [172, 188, 232, 364]) {
dynamic.height = height;
assert.deepEqual(plain(surfaces.popupContentSize("dynamic")), { width: 368, height });
}
surfaces.screenHeight = 200;
assert.equal(surfaces.popupContentSize("dynamic").height, 260, "dynamic height still respects the budget");
features.pop();
// Fit: the open popup's card follows its panel's natural height plus 48 px of
// inset and margins, capped by the entry height and the screen budget; other
// names and panels without a natural height keep the entry height.
for (const feature of sized) {
Object.assign(surfaces, { screenWidth: 1366, screenHeight: 1080, vertical: false, popupName: feature.name });
for (const [panelHeight, height] of [[0, feature.height], [100.2, Math.min(feature.height, 149)], [feature.height, feature.height]]) {
surfaces.panelHeight = panelHeight;
assert.equal(surfaces.popupContentSize(feature.name).height, height, `${feature.name} fits natural height ${panelHeight}`);
}
surfaces.panelHeight = 100;
assert.equal(surfaces.popupContentSize("unknown").height, 300, "another name keeps its own height");
surfaces.screenHeight = 200;
assert.equal(surfaces.popupContentSize(feature.name).height, Math.min(feature.height, 148), `${feature.name} fit below the budget floor`);
surfaces.panelHeight = 2000;
assert.equal(surfaces.popupContentSize(feature.name).height, Math.min(feature.height, 260), `${feature.name} fit still respects the budget`);
feature.empty = true;
assert.equal(surfaces.popupContentSize(feature.name).height, 104, `${feature.name} empty state wins over its natural height`);
feature.empty = false;
}
Object.assign(surfaces, { popupName: "clock", panelHeight: 0 });
// Empty-state shrink: every registry popup through its entry's `empty` flag;
// all share the host's compact empty size in either orientation.
for (const feature of sized) {
for (const vertical of [false, true]) {
Object.assign(surfaces, { screenHeight: 768, vertical });
feature.empty = true;
assert.deepEqual(plain(surfaces.popupContentSize(feature.name)), { width: 368, height: 104 }, `${feature.name} empty popup shrinks`);
feature.empty = false;
assert.deepEqual(plain(surfaces.popupContentSize(feature.name)), { width: widths[feature.name], height: heights[feature.name] }, `${feature.name} arrival restores populated dimensions`);
}
}
// Consumers retain geometry and state computed by SurfaceManager.
Object.assign(surfaces, { screenWidth: 1366, screenHeight: 768, vertical: false, popupName: "clock" });
assert.deepEqual(plain(surfaces.popupCardFrame(1)), {
source: "simple-center-slide-fade", progress: 1,
currentRect: { x: 311, y: 42, width: 744, height: 564 }, opacity: 1,
});
surfaces.vertical = true;
assert.deepEqual(plain(surfaces.popupCardFrame(1)), {
source: "left-bar-slide-fade", progress: 1,
currentRect: { x: 42, y: 102, width: 744, height: 564 }, opacity: 1,
});
surfaces.vertical = false;
// A popup on an output without the bar centres on that output and clears no bar.
surfaces.popupViewport = { width: 2560, height: 1440 };
assert.deepEqual(plain(surfaces.popupCardFrame(1)).currentRect, { x: 908, y: 8, width: 744, height: 564 });
assert.equal(surfaces.heightBudget(2000), 1392, "the whole bar-less output is the budget");
surfaces.vertical = true;
assert.deepEqual(plain(surfaces.popupCardFrame(1)).currentRect, { x: 8, y: 438, width: 744, height: 564 });
assert.equal(surfaces.popupWidth("clock"), 760, "no bar narrows a bar-less output");
surfaces.vertical = false;
surfaces.popupViewport = null;
assert.deepEqual(plain(surfaces.popupCardFrame(1)).currentRect, { x: 311, y: 42, width: 744, height: 564 }, "the bar's output keeps its geometry");
// The card animates fitted size changes only after the open panel settled,
// never into a switch or reopen.
surfaces.popupName = "";
surfaces.panelSettled = true;
const opened = JSON.parse(surfaces.openPopup("clock", anchor, "synthetic"));
assert.deepEqual(opened.anchor, anchor);
assert.equal(surfaces.panelSettled, false, "open resets the settled card");
surfaces.panelSettled = true;
surfaces.openPopup("clock", anchor, "synthetic");
assert.equal(surfaces.panelSettled, true, "a repeated open keeps the loaded panel settled");
surfaces.openPopup(sized.find(feature => feature.name !== "clock").name, anchor, "synthetic");
assert.equal(surfaces.panelSettled, false, "switch resets the settled card");
assert.equal(opened.phase, "opening");
assert.equal(opened.name, "clock");
assert.deepEqual(opened.contentSize, { width: 760, height: 580 });
assert.equal(opened.popupCardFrame.source, "simple-center-slide-fade");
const switched = JSON.parse(surfaces.openPopup(sized[0].name, anchor, "synthetic"));
assert.equal(switched.phase, "open");
assert.equal(switched.progress, 1);
assert.deepEqual(switched.contentSize, { width: sized[0].width, height: sized[0].height });
surfaces.panelSettled = true;
const closed = JSON.parse(surfaces.closePopup("escape-key"));
assert.equal(surfaces.panelSettled, false, "close resets the settled card");
assert.equal(closed.phase, "closed");
assert.equal(closed.name, "");
assert.equal(closed.lastCloseReason, "escape-key");
assert.deepEqual(closed.contentSize, { width: 520, height: 300 });
// Registry features get retained state and diagnostic names without host cases.
surfaces.panelStates = { unregistered: { scrollY: 3 } };
features[0].retainedState = { scrollY: 0, expanded: "" };
surfaces.seedFeatureStates();
assert.deepEqual(plain(surfaces.panelStates), { unregistered: { scrollY: 3 }, ...Object.fromEntries(features.map(feature => [feature.name, plain(feature.retainedState)])) });
surfaces.panelStates[features[0].name].expanded = "kept";
surfaces.seedFeatureStates();
assert.equal(surfaces.panelStates[features[0].name].expanded, "kept", "seeding never resets session state");
assert.notEqual(surfaces.panelStates[features[0].name], features[0].retainedState, "each session owns a copy of the template");
assert.equal(diagnostics.name("audio", surfaces), "audio");
assert.equal(diagnostics.name("audio", {}), "unknown");
assert.equal(diagnostics.name("clock", surfaces), "clock");
console.log("surface-geometry.test.mjs: ALL PASS");