Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/test-fixtures/ipc-selftest.js

Raw
'use strict';

// Shared isolated Quickshell self-test run: links the given production files into
// a private config, starts it offscreen under its own D-Bus session, and asserts that
// every IPC method on the `tests` target reports a pass with no diagnostic in the output.
// `imports` adds import lines, e.g. a pure model to check under the QML engine.
//
// Isolation from the desktop session comes from isolation.js: private HOME, XDG and
// runtime directories, no display, and a private session bus that activates nothing.
const assert = require('assert');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { runIsolated, spawnIsolated, stopGroup } = require('./isolation.js');

function runIpcSelfTests({ files, imports = [], body, methods }) {
    const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'nuguland-ipc-'));
    const name = `test-${process.pid}`;
    const configDir = path.join(tmp, 'config', 'quickshell', name);
    // The run is synchronous, so stream events never fire: output goes to a file.
    const log = path.join(tmp, 'qs.log');
    const output = () => fs.existsSync(log) ? fs.readFileSync(log, 'utf8') : '';
    let proc;
    const ipc = method => runIsolated('qs', ['ipc', '-c', name, 'call', 'tests', method], {
        directory: tmp,
        encoding: 'utf8',
        timeout: 2000
    });
    try {
        fs.mkdirSync(configDir, { recursive: true });
        for (const file of files)
            fs.symlinkSync(file, path.join(configDir, path.basename(file)));
        fs.writeFileSync(path.join(configDir, 'shell.qml'), `
import QtQuick
import Quickshell
import Quickshell.Io
${imports.join('\n')}
ShellRoot {
${body}
    IpcHandler {
        target: "tests"
${methods.map(([method, call]) => `        function ${method}(): string { return ${call}; }`).join('\n')}
        function ready(): string { return "ready"; }
    }
}
`);
        const fd = fs.openSync(log, 'w');
        try {
            proc = spawnIsolated('qs', ['-c', name], { directory: tmp, bus: true, stdio: ['ignore', fd, fd] });
        } finally {
            fs.closeSync(fd);
        }

        let ready = false;
        for (let attempt = 0; attempt < 80; attempt++) {
            const result = ipc('ready');
            if (result.status === 0 && result.stdout.trim() === 'ready') {
                ready = true;
                break;
            }
            Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 50);
        }
        assert(ready, `isolated Quickshell IPC did not become ready: ${output()}`);
        for (const [method] of methods) {
            const result = ipc(method);
            assert.strictEqual(result.status, 0, result.stderr);
            const report = JSON.parse(result.stdout);
            const passed = typeof report.pass === 'boolean' ? report.pass : report.fail === 0;
            assert.strictEqual(passed, true, `${method}: ${JSON.stringify(report.failures || report.cases || [])}`);
        }
        // The complete output, once the session has exited.
        stopGroup(proc.pid);
        assert(!/WARN|ERROR|Error parsing|deprecated|Could not|failed/.test(output()), output());
    } finally {
        if (proc) stopGroup(proc.pid);
        fs.rmSync(tmp, { recursive: true, force: true });
    }
}

module.exports = { runIpcSelfTests };