aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/island/test_scene_icons.js
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-10-17 13:12:36 +0200
committerDanilo M. <danix@danix.xyz>2026-10-17 13:12:36 +0200
commitd3717bc4b42c5b0b6c7e8b5055585c1a1ba061aa (patch)
treefcc0b12d189143f9cd5f58be53c04d043ca72fb5 /desktop/island/test_scene_icons.js
parente39df38860ba10b4611572ec721a8be39cf8bcae (diff)
downloadquickshell-d3717bc4b42c5b0b6c7e8b5055585c1a1ba061aa.tar.gz
quickshell-d3717bc4b42c5b0b6c7e8b5055585c1a1ba061aa.zip
island: scene icon map logic and its tests
Diffstat (limited to 'desktop/island/test_scene_icons.js')
-rw-r--r--desktop/island/test_scene_icons.js62
1 files changed, 62 insertions, 0 deletions
diff --git a/desktop/island/test_scene_icons.js b/desktop/island/test_scene_icons.js
new file mode 100644
index 0000000..f09efdf
--- /dev/null
+++ b/desktop/island/test_scene_icons.js
@@ -0,0 +1,62 @@
+// Run: node desktop/island/test_scene_icons.js
+//
+// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+//
+// 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.
+//
+// SceneIcons.js is a QML library, so its .pragma line is stripped before node
+// evaluates it.
+
+const fs = require("fs");
+const assert = require("assert");
+
+const src = fs.readFileSync(__dirname + "/SceneIcons.js", "utf8")
+ .replace(/^\.pragma library$/m, "");
+const I = new Function(src +
+ "\nreturn { defaults, validGlyph, validMap, normalize, parse, serialize, iconFor };")();
+
+// One code point, even when it is a surrogate pair (U+F02DC, md-home).
+assert.strictEqual(I.validGlyph("\u{f02dc}"), true);
+assert.strictEqual(I.validGlyph("a"), true);
+assert.strictEqual(I.validGlyph(""), false);
+assert.strictEqual(I.validGlyph("ab"), false);
+
+// A missing or unparseable file is the starter map, not an empty one.
+assert.deepStrictEqual(I.parse(""), I.defaults);
+assert.deepStrictEqual(I.parse("not json"), I.defaults);
+
+// A valid file is used as-is: no merge with the defaults.
+const one = I.parse('{"generic":"\\udb80\\uded7","scenes":{"Desktop":"\\udb80\\udf79"}}');
+assert.deepStrictEqual(one, { generic: "\udb80\uded7", scenes: { Desktop: "\udb80\udf79" } });
+assert.strictEqual(I.iconFor(one, "Desktop"), "\udb80\udf79");
+assert.strictEqual(I.iconFor(one, "Webcam Only"), "\udb80\uded7");
+
+// Invalid values are dropped; the generic falls back.
+const bad = I.parse('{"generic":"","scenes":{"A":"ab","B":"\\udb80\\udf79"}}');
+assert.deepStrictEqual(bad, { generic: I.defaults.generic, scenes: { B: "\udb80\udf79" } });
+
+// A non-string value is dropped, never passed to charCodeAt.
+assert.deepStrictEqual(I.parse('{"scenes":{"A":42}}'),
+ { generic: I.defaults.generic, scenes: {} });
+
+// normalize is what Save writes: empty generic -> default, invalid scene dropped.
+assert.deepStrictEqual(
+ I.normalize({ generic: "", scenes: { A: I.defaults.scenes.Desktop, B: "ab" } }),
+ { generic: I.defaults.generic, scenes: { A: I.defaults.scenes.Desktop } });
+
+// Whole-draft validity: an empty generic is allowed (Save substitutes the default).
+assert.strictEqual(I.validMap({ generic: "", scenes: { A: "\u{f02dc}" } }), true);
+assert.strictEqual(I.validMap({ generic: I.defaults.generic, scenes: { A: "ab" } }), false);
+
+// Round-trip.
+assert.deepStrictEqual(I.parse(I.serialize(one)), one);
+assert.ok(I.serialize(one).endsWith("\n"));
+
+console.log("ok");