// Copyright (C) 2026 Danilo M. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License version 2 as // published by the Free Software Foundation. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // Run: node window-switcher/test_expose.js // Expose.js is a QML library, so its .pragma line is stripped before node // evaluates it, as test_perf.js does for Perf.js. const fs = require("fs"); const assert = require("assert"); const src = fs.readFileSync(__dirname + "/Expose.js", "utf8") .replace(/^\.pragma library$/m, ""); const { layout, step } = new Function(src + "\nreturn { layout, step };")(); const W = 2560, H = 1080; const wide = { w: 2556, h: 1076 }, tall = { w: 1076, h: 1916 }; function apart(a, b) { return a.x + a.w <= b.x || b.x + b.w <= a.x || a.y + a.h <= b.y || b.y + b.h <= a.y; } function check(out) { for (const o of out) { assert.ok(o.x >= 0 && o.y >= 0 && o.x + o.w <= W + 1e-6 && o.y + o.h <= H + 1e-6, "inside the screen"); } for (let i = 0; i < out.length; i++) for (let j = i + 1; j < out.length; j++) assert.ok(apart(out[i], out[j]), `windows ${i} and ${j} overlap`); } assert.deepStrictEqual(layout([], W, H), []); // Seven identical full-screen windows, the normal case here: no overlap. const seven = Array(7).fill(wide); check(layout(seven, W, H)); // A busy mixed desk still separates within the pass limit. check(layout(Array.from({ length: 14 }, (_, i) => i % 3 ? wide : tall), W, H)); // Deterministic: the same input gives the same scatter. assert.deepStrictEqual(layout(seven, W, H), layout(seven, W, H)); // A lone window is capped at half size. const one = layout([wide], W, H)[0]; assert.ok(Math.abs(one.w - wide.w * 0.5) < 1e-6); // One scale for all: sizes stay relative. const [a, b] = layout([wide, tall], W, H); assert.ok(Math.abs(a.w / b.w - wide.w / tall.w) < 1e-9); assert.ok(Math.abs(a.h / b.h - wide.h / tall.h) < 1e-9); // Arrow selection: a 2x2 grid, plus one far off to the upper right. const g = [{ x: 0, y: 0, w: 10, h: 10 }, { x: 20, y: 0, w: 10, h: 10 }, { x: 0, y: 20, w: 10, h: 10 }, { x: 20, y: 20, w: 10, h: 10 }, { x: 100, y: -60, w: 10, h: 10 }]; assert.strictEqual(step(g, -1, 1, 0), 0); // top-left first assert.strictEqual(step(g, 0, 1, 0), 1); // level neighbour beats the far one assert.strictEqual(step(g, 0, 0, 1), 2); assert.strictEqual(step(g, 3, -1, 0), 2); assert.strictEqual(step(g, 2, -1, 0), 2); // nothing to the left: stays assert.strictEqual(step([], -1, 1, 0), -1); console.log("test_expose: ok");