From 2c4b0de14d3ec8d6410a0fbf7d32501b66d7703b Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Tue, 13 Oct 2026 09:52:29 +0200 Subject: feat(island): pure tile table and order helpers for controls The controls panel's tiles, their icons and drawer pages, and every operation on the visible list: normalize, hide, show, move, the drop slot under a dragged tile, the audio fill and the scroll step. All pure, so the edit mode's logic is tested under node rather than by dragging. normalize keeps an empty list: hiding every tile is a choice, and only a value that is not a list falls back to the default. Co-Authored-By: Claude Sonnet 5.5 --- desktop/island/test_controls.js | 95 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 desktop/island/test_controls.js (limited to 'desktop/island/test_controls.js') diff --git a/desktop/island/test_controls.js b/desktop/island/test_controls.js new file mode 100644 index 0000000..f84e472 --- /dev/null +++ b/desktop/island/test_controls.js @@ -0,0 +1,95 @@ +// 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 desktop/island/test_controls.js +// Controls.js is a QML library, so its .pragma line is stripped before node +// evaluates it, as test_logic.js does for Logic.js. + +const fs = require("fs"); +const assert = require("assert"); + +const src = fs.readFileSync(__dirname + "/Controls.js", "utf8") + .replace(/^\.pragma library$/m, ""); +const C = new Function(src + + "\nreturn { defaults, tile, icon, normalize, hidden, move, hide, show, fill, step, slotAt };")(); + +const ALL = ["wifi", "bluetooth", "dnd", "presentation", "nolock", "output", "mic"]; + +// Defaults: all seven, a fresh copy each time. +assert.deepStrictEqual(C.defaults(), ALL); +const d = C.defaults(); d.pop(); +assert.deepStrictEqual(C.defaults(), ALL); + +// Table: label and drawer page; unknown is null. +assert.strictEqual(C.tile("wifi").page, "network"); +assert.strictEqual(C.tile("nolock").label, "Auto-lock"); +assert.strictEqual(C.tile("mic").page, "sound"); +assert.strictEqual(C.tile("nope"), null); + +// Icons by state. Output is Logic.volumeIcon's job, so "" here. +assert.strictEqual(C.icon("wifi", true), "network-wireless"); +assert.strictEqual(C.icon("wifi", false), "network-wireless-disconnected"); +assert.strictEqual(C.icon("dnd", true), "notifications-disabled"); +assert.strictEqual(C.icon("dnd", false), "notifications"); +assert.strictEqual(C.icon("mic", true), "audio-input-microphone"); +assert.strictEqual(C.icon("mic", false), "microphone-sensitivity-muted"); +assert.strictEqual(C.icon("bluetooth", false), "preferences-system-bluetooth"); +assert.strictEqual(C.icon("output", true), ""); + +// normalize: drops unknown ids and duplicates, keeps order; an empty list +// is valid (everything hidden); a non-array is the default. +assert.deepStrictEqual(C.normalize(["mic", "gone", "wifi", "mic"]), ["mic", "wifi"]); +assert.deepStrictEqual(C.normalize([]), []); +assert.deepStrictEqual(C.normalize("wifi"), ALL); +assert.deepStrictEqual(C.normalize(undefined), ALL); + +// hidden: known ids not in the list, in table order. +assert.deepStrictEqual(C.hidden(["wifi", "mic"]), ["bluetooth", "dnd", "presentation", "nolock", "output"]); +assert.deepStrictEqual(C.hidden(ALL), []); + +// move: from index to index, input untouched. +const src3 = ["a", "b", "c"]; +assert.deepStrictEqual(C.move(src3, 0, 2), ["b", "c", "a"]); +assert.deepStrictEqual(C.move(src3, 2, 0), ["c", "a", "b"]); +assert.deepStrictEqual(C.move(src3, 1, 1), ["a", "b", "c"]); +assert.deepStrictEqual(src3, ["a", "b", "c"]); + +// hide / show. +assert.deepStrictEqual(C.hide(["wifi", "mic"], "wifi"), ["mic"]); +assert.deepStrictEqual(C.show(["mic"], "wifi"), ["mic", "wifi"]); +assert.deepStrictEqual(C.show(["mic"], "mic"), ["mic"]); +assert.deepStrictEqual(C.show(["mic"], "nope"), ["mic"]); + +// fill: volume clamped to 0..1; unpopulated reads as empty, not full. +assert.strictEqual(C.fill(0.4), 0.4); +assert.strictEqual(C.fill(1.5), 1); +assert.strictEqual(C.fill(-1), 0); +assert.strictEqual(C.fill(undefined), 0); +assert.strictEqual(C.fill(NaN), 0); + +// step: 5% steps, rounded to the percent, clamped to 0..max. +assert.strictEqual(C.step(0.5, 0.05, 1.5), 0.55); +assert.strictEqual(C.step(0.02, -0.05, 1.5), 0); +assert.strictEqual(C.step(1.48, 0.05, 1.5), 1.5); +assert.strictEqual(C.step(0.98, 0.05, 1.0), 1); +assert.strictEqual(C.step(0.1 + 0.2, 0.05, 1), 0.35); +assert.strictEqual(C.step(undefined, 0.05, 1), 0.05); + +// slotAt: pitch 70, 4 columns, 7 tiles. +assert.strictEqual(C.slotAt(35, 35, 70, 4, 7), 0); +assert.strictEqual(C.slotAt(175, 35, 70, 4, 7), 2); +assert.strictEqual(C.slotAt(105, 105, 70, 4, 7), 5); +assert.strictEqual(C.slotAt(-50, -50, 70, 4, 7), 0); // off the top left +assert.strictEqual(C.slotAt(500, 35, 70, 4, 7), 3); // past the right edge +assert.strictEqual(C.slotAt(275, 105, 70, 4, 7), 6); // empty 8th slot -> last +assert.strictEqual(C.slotAt(35, 900, 70, 4, 7), 6); // below the grid -> last + +console.log("test_controls: ok"); -- cgit v1.2.3