Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/keybind-ipc.test.mjs

Raw
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';

// This is a cross-config declaration guard, not proof of runtime behavior.
const shell = readFileSync(new URL('./shell.qml', import.meta.url), 'utf8');
const keybinds = readFileSync(new URL('../../niri/cfg/keybinds.kdl', import.meta.url), 'utf8');

function checkBindings(source, bindings) {
    const handlers = new Map();
    for (const [body] of source.matchAll(/^    IpcHandler \{\n[\s\S]*?^    \}/gm)) {
        const target = /target: "([^"]+)"/.exec(body)[1];
        handlers.set(target, new Map([...body.matchAll(/^        function (\w+)\(([^)]*)\)/gm)]
            .map(([, name, args]) => [name, args.trim() ? args.split(',').length : 0])));
    }
    let calls = 0;
    for (const line of bindings.split('\n')) {
        const tokens = [...line.matchAll(/"([^"\n]*)"/g)].map(match => match[1]);
        const start = tokens.indexOf('qs');
        if (start < 0 || tokens[start + 1] !== 'ipc' || tokens[start + 3] !== 'nuguland') continue;
        assert.deepEqual(tokens.slice(start + 1, start + 5), ['ipc', '-c', 'nuguland', 'call']);
        const [target, method, ...args] = tokens.slice(start + 5);
        assert(handlers.has(target), `missing IPC target: ${target}`);
        assert(handlers.get(target).has(method), `missing IPC method: ${target}.${method}`);
        assert.equal(handlers.get(target).get(method), args.length, `IPC argument count: ${target}.${method}`);
        calls++;
    }
    assert(calls > 0, 'keybind guard must inspect real IPC calls');
}

checkBindings(shell, keybinds);
assert.throws(() => checkBindings(shell.replace('target: "vault"', 'target: "missing"'), keybinds), /missing IPC target: vault/);
assert.throws(() => checkBindings(shell.replace('function openLauncher()', 'function gone()'), keybinds), /missing IPC method: nuguland.openLauncher/);
assert.throws(() => checkBindings(shell.replace('setOrientation(mode: string)', 'setOrientation()'), keybinds), /IPC argument count/);
console.log('keybind-ipc.test.mjs: keybind targets, methods and arities PASS (static guard)');