aboutsummaryrefslogtreecommitdiffstats
path: root/keybinds/test_binds.js
blob: df1e4b578f6cb2ac5ce774fabcb441727cd3fdc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// 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.

// Run: node keybinds/test_binds.js
// Binds.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 + "/Binds.js", "utf8")
    .replace(/^\.pragma library$/m, "");
const B = new Function(src + "\nreturn { mods, keys, group, PAD, padActions };")();

assert.deepStrictEqual(B.mods(0), []);
assert.deepStrictEqual(B.mods(65), ["Win", "SHIFT"]);
assert.deepStrictEqual(B.mods(13), ["CTRL", "ALT", "SHIFT"]);
assert.strictEqual(B.keys({ modmask: 69, key: "F1" }), "Win + CTRL + SHIFT + F1");
assert.strictEqual(B.keys({ modmask: 0, key: "XF86AudioMute" }), "Mute");
assert.strictEqual(B.keys({ modmask: 64, key: "mouse:272" }), "Win + LMB");
assert.strictEqual(B.keys({ modmask: 0, key: "F5" }), "F5");
// A key named like an Object.prototype member is not renamed.
assert.strictEqual(B.keys({ modmask: 0, key: "constructor" }), "constructor");

assert.deepStrictEqual(B.group([
    { modmask: 64, key: "s", description: "" },
    { modmask: 64, key: "w", description: "Apps: Browser" },
    { modmask: 0, key: "XF86AudioMute", description: "Media: Mute" },
    { modmask: 64, key: "e", description: "Apps: Files" },
    { modmask: 8, key: "x", description: "No colon here" },
]), [
    { name: "Apps", rows: [
        { keys: "Win + w", label: "Browser", pad: { key: 1 } },
        { keys: "Win + e", label: "Files" },
    ] },
    { name: "Media", rows: [{ keys: "Mute", label: "Mute", pad: { knob: "top", turn: "Press" } }] },
    { name: "Other", rows: [{ keys: "ALT + x", label: "No colon here" }] },
    { name: "Undescribed", rows: [{ keys: "Win + s", label: "no description" }] },
]);

// A group name matching an Object.prototype property must not resolve to
// the inherited value instead of creating a new group.
assert.deepStrictEqual(B.group([
    { modmask: 64, key: "z", description: "toString: Weird" },
]), [{ name: "toString", rows: [{ keys: "Win + z", label: "Weird" }] }]);

// The macropad: shifted keys, the workspace row built in a loop, and an
// encoder turned with Win held.
assert.deepStrictEqual(B.PAD["Win + SHIFT + r"], { key: 2, mod: "Shift" });
assert.deepStrictEqual(B.PAD["CTRL + ALT + SHIFT + l"], { key: 4, mod: "Alt" });
assert.deepStrictEqual(B.PAD["CTRL + F1"], { key: 9 });
assert.deepStrictEqual(B.PAD["Win + CTRL + SHIFT + F4"], { key: 12, mod: "Win + Shift" });
assert.deepStrictEqual(B.PAD["Win + Previous song"], { knob: "bottom", turn: "Turn left", mod: "Win" });
assert.strictEqual(B.PAD["toString"], undefined);

// Everything one pad key or encoder does, plain press first as listed.
assert.deepStrictEqual(B.padActions(B.group([
    { modmask: 64, key: "w", description: "Apps: Browser" },
    { modmask: 65, key: "w", description: "Apps: Private browser" },
    { modmask: 0, key: "XF86AudioRaiseVolume", description: "Media: Volume up" },
    { modmask: 64, key: "XF86AudioRaiseVolume", description: "Keyboard: Next layout" },
    { modmask: 64, key: "e", description: "Apps: Files" },
])), Object.assign(Object.create(null), {
    k1: [{ how: "Press", label: "Browser" }, { how: "Shift + Press", label: "Private browser" }],
    top: [{ how: "Turn right", label: "Volume up" }, { how: "Win + Turn right", label: "Next layout" }],
}));

console.log("ok");