Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/ui-test.test.mjs

Raw
import assert from 'node:assert/strict';
import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, utimesSync, writeFileSync } from 'node:fs';
import { createServer } from 'node:net';
import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import test from 'node:test';
import { runInNewContext } from 'node:vm';
import { allSuites, assertCleanLog, assertFixtureLog, assertHumanLabels, assertNiriLog, assertPlainLabels, assertPureService, assertSafeCapture, captureUiFailure, childEnvironment, finishUiRun, lastWrite, pruneEvidence, treeBytes, qtTextInputWorkaround, replayRequest, stopChild } from './ui-test.mjs';
import { assertSubscription, nativeFeatures } from './test-fixtures/ui/niri-integration.mjs';
import { barOverlayFixtures } from './test-fixtures/ui/bar.mjs';
import { featureDoubles, featureSuites, registryEntries, rootEntry } from './test-fixtures/sources.js';
import { runIsolated, spawnIsolated } from './test-fixtures/isolation.js';

test('QTBUG-146211 workaround is removed as soon as installed Qt changes', () => {
    const installed = runIsolated('/usr/lib/qt6/bin/qtpaths6', ['--qt-query', 'QT_VERSION'], { encoding: 'utf8' });
    assert.equal(installed.status, 0, installed.stderr);
    assert.equal(installed.stdout.trim(), qtTextInputWorkaround.brokenQt,
        'Qt changed: remove the QTBUG-146211 workaround (git grep QTBUG-146211), then rerun just ui-test');
});

test('QTBUG-146211 workaround accepts only the reproduced child diagnostics and counts raw records', () => {
    const disable = '  WARN qt.qpa.wayland.textinput: virtual void QtWaylandClient::QWaylandTextInputv3::disableSurface(wl_surface*) Trying to disable 0xabc123 but 0x0 is focused';
    const enable = '  WARN qt.qpa.wayland.textinput: virtual void QtWaylandClient::QWaylandTextInputv3::enableSurface(wl_surface*) Trying to enable 0xabc123 but 0xdef456 has focus';
    const native = disable.replace('  WARN', 'warning');
    const stale = disable.replace('0x0 is focused', '0xdef456 is focused');
    const log = `INFO: loaded\n${disable}\n${native}\n${enable}\n${stale}\n`;
    assert.equal(assertFixtureLog(log), 4);
    assert(log.includes(disable), 'raw evidence must not be rewritten');
    for (const warning of [disable, enable]) {
        assert.throws(() => assertCleanLog(warning), 'non-fixture gates remain strict');
        for (const unexpected of [
            warning.replace('WARN', 'ERROR'), warning.replace('textinput:', 'other:'),
            warning + ' extra diagnostic', `${warning}\nERROR genuine failure`
        ]) assert.throws(() => assertFixtureLog(unexpected));
    }
    for (const unexpected of [
        'WARN unrelated Qt warning',
        disable.replace('but 0x0 is focused', 'but 0xdef is enabled'),
        enable.replace('has focus', 'is already enabled')
    ]) assert.throws(() => assertFixtureLog(unexpected));
});

test('the label guard fails a suite on any machine identifier a person can see', () => {
    const human = {
        chips: { clipboard: { label: 'clips', badge: '', title: 'clipboard', accessibleName: 'clipboard', tooltip: '' } },
        barControls: { clock: { label: '12:30', tooltip: 'calendar' } },
        features: {
            clipboard: {
                texts: ['image', 'text · 2m', 'report.pdf', 'pasting into wezterm'],
                controls: { 'image': { text: '' }, 'copy selected item': { text: '' } },
                items: { searchField: { text: '' } },
                // Service state is data, not a label; the guard never reads it.
                service: { rows: ['/tmp/pi-clipboard-23965d8a-47d6-45ce-9d1a-f83c6b805220.png'] },
                focusOverlay: null,
            },
            noko: { texts: [], controls: {}, items: {}, focusOverlay: { texts: ['listening'], controls: {}, items: {} } },
        },
    };
    assertHumanLabels(human);
    const leak = '/tmp/pi-clipboard-23965d8a-47d6-45ce-9d1a-f83c6b805220.png';
    for (const [where, place] of [
        ['visible text', state => state.features.clipboard.texts.push(leak)],
        ['control name', state => { state.features.clipboard.controls[leak] = { text: '' }; }],
        ['control text', state => { state.features.clipboard.controls.image.text = `select ${leak}`; }],
        ['item text', state => { state.features.clipboard.items.searchField.text = 'org.mpris.MediaPlayer2.firefox.instance_1234'; }],
        ['focus overlay text', state => state.features.noko.focusOverlay.texts.push('alsa_input.pci-0000_00_1f.3.analog-stereo')],
        ['chip tooltip', state => { state.chips.clipboard.tooltip = 'file:///run/user/1000/x.png'; }],
        ['chip accessible name', state => { state.chips.clipboard.accessibleName = 'org.kde.StatusNotifierItem-1234-1'; }],
        ['bar control label', state => { state.barControls.clock.label = '23965d8a-47d6-45ce-9d1a-f83c6b805220'; }],
    ]) {
        const state = structuredClone(human);
        place(state);
        assert.throws(() => assertHumanLabels(state), /machine identifiers in rendered labels/, where);
    }
});

test('the narration guard fails a suite on a line about what the interface cannot do', () => {
    // Lines Nuguland shows today: states, next steps, consequences, short reasons.
    const plain = () => ({
        chips: { media: { label: 'media', badge: '', title: 'media', accessibleName: 'media', tooltip: 'media' } },
        barControls: { clock: { label: '12:30', tooltip: 'calendar' } },
        features: {
            media: {
                texts: ['fixture track', 'playing', 'nothing playing', 'play media in an app to control it here'],
                controls: { 'previous track': { text: '', tooltipText: 'no previous track' }, pause: { text: '', tooltipText: 'pause' } },
                items: { transportActions: { text: '', tooltipText: '' } },
                userTexts: ['fixture track'],
                focusOverlay: null,
            },
            brightness: {
                texts: ['light sensor unavailable', 'this display does not accept brightness changes; check its cable and the monitor menu'],
                controls: { adaptive: { text: 'adaptive', tooltipText: 'light sensor unavailable' } }, items: {}, userTexts: [], focusOverlay: null,
            },
            notifications: {
                // A body an app wrote is data, whatever it says.
                texts: ['clear 2 notifications?', 'this cannot be undone', 'shuffle and repeat not supported by this phone', 'reply'],
                controls: { reply: { text: 'reply', tooltipText: 'reply' } }, items: {},
                userTexts: ['shuffle and repeat not supported by this phone'], focusOverlay: null,
            },
            clipboard: {
                texts: ['this file cannot be opened', 'text · 2m', 'nothing copied yet'],
                controls: { 'select this file cannot be opened': { text: '', tooltipText: '' } }, items: {},
                userTexts: ['this file cannot be opened'], focusOverlay: null,
            },
            system: {
                texts: ['repair prompt unavailable or invalid', 'camera and microphone in use', 'crash history unavailable · journal unreadable'],
                controls: {}, items: { vaultField: { text: 'unsupported', tooltipText: '' } }, userTexts: [], focusOverlay: null,
            },
        },
    });
    assertPlainLabels(plain());
    const owner = 'shuffle, previous track, next track and repeat not supported by this player';
    for (const [where, place] of [
        ['the owner example as a line', state => state.features.media.texts.push(owner)],
        ['a player that cannot be controlled', state => state.features.media.texts.push('this player cannot be controlled')],
        ['one control', state => state.features.media.texts.push('previous track not supported by this player')],
        ['a capability list without the phrase', state => state.features.media.texts.push('shuffle and repeat unavailable')],
        ['a disabled tooltip', state => { state.features.media.controls['previous track'].tooltipText = 'previous track unavailable for this player'; }],
        ['a control name', state => { state.features.brightness.controls['adaptive (this display cannot be adjusted)'] = { text: '' }; }],
        ['an explained disabled state', state => { state.features.brightness.controls.adaptive.tooltipText = 'disabled because monitor control is missing'; }],
        ['a chip tooltip', state => { state.chips.media.tooltip = "this player doesn't support seeking"; }],
        ['a focus overlay', state => { state.features.media.focusOverlay = { texts: ['only supports play'], controls: {}, items: {}, userTexts: [] }; }],
        ['narration around written text', state => state.features.media.texts.push('fixture track cannot be controlled')],
        ['written text without its userText mark', state => { state.features.notifications.userTexts = []; }],
    ]) {
        const state = plain();
        place(state);
        assert.throws(() => assertPlainLabels(state), /labels narrate the interface/, where);
    }
});

test('fixture entries at the shell root rewrite every root-relative import', () => {
    assert.equal(rootEntry('import QtQuick\nimport "../.." as N\nimport "../../features/audio" as AudioFeature\nimport "../../Util.js" as Util\n', 2),
        'import QtQuick\nimport "." as N\nimport "./features/audio" as AudioFeature\nimport "./Util.js" as Util\n');
    assert.equal(rootEntry('import "../../.." as N\nimport "../../../features/audio" as AudioFeature\n', 3), 'import "." as N\nimport "./features/audio" as AudioFeature\n');
    assert.throws(() => rootEntry('import QtQuick\n', 2), /must import the shell root/);
    assert.throws(() => rootEntry('import "../../.." as N\n', 2), /must import the shell root/);
});

test('evidence that vanishes during a concurrent prune is skipped, not an error', () => {
    const missing = join(tmpdir(), `nuguland-ui-test-vanished-${process.pid}`);
    assert.equal(lastWrite(missing), null);
    assert.equal(treeBytes(missing), 0);
    const file = join(mkdtempSync(join(tmpdir(), 'nuguland-prune-file-')), 'run');
    writeFileSync(file, '');
    assert.throws(() => lastWrite(file), error => error.code === 'ENOTDIR', 'other errors still fail');
    rmSync(dirname(file), { recursive: true });
});

test('evidence pruning keeps the newest runs per fixture and spares recent and foreign directories', () => {
    const root = mkdtempSync(join(tmpdir(), 'nuguland-prune-test-'));
    try {
        const now = Date.now();
        const run = (name, fixture, minutesAgo) => {
            const path = join(root, name);
            mkdirSync(path);
            if (fixture) writeFileSync(join(path, 'replay.json'), JSON.stringify({ fixture }));
            writeFileSync(join(path, 'runner.log'), 'log');
            const time = new Date(now - minutesAgo * 60_000);
            for (const file of readdirSync(path)) utimesSync(join(path, file), time, time);
            utimesSync(path, time, time);
        };
        for (const minutes of [1, 2, 3, 4]) run(`nuguland-ui-test-recent${minutes}`, 'all', minutes);
        run('nuguland-ui-test-old', 'all', 60);
        run('nuguland-ui-test-bar', 'bar', 300);
        run('nuguland-ui-test-unnamed', null, 500);
        run('nuguland-ui-test-corrupt', null, 600);
        writeFileSync(join(root, 'nuguland-ui-test-corrupt', 'replay.json'), '{');
        run('foreign-dir', 'all', 999);
        const removed = pruneEvidence(root, { keep: 3, quietMs: 15 * 60_000, now });
        assert.deepEqual(removed.map(entry => entry.path), [join(root, 'nuguland-ui-test-old')]);
        assert(removed[0].bytes > 0);
        assert.deepEqual(readdirSync(root).sort(), ['foreign-dir', 'nuguland-ui-test-bar', 'nuguland-ui-test-corrupt', 'nuguland-ui-test-recent1',
            'nuguland-ui-test-recent2', 'nuguland-ui-test-recent3', 'nuguland-ui-test-recent4', 'nuguland-ui-test-unnamed']);
    } finally {
        rmSync(root, { recursive: true, force: true });
    }
});

test('named replay defaults to all and rejects invalid selections before execution', () => {
    assert.deepEqual(replayRequest([]), { version: 1, fixture: 'all', command: 'just ui-test all' });
    for (const fixture of ['all', 'controls', 'popups', 'privilege', 'niri', 'bar', 'qt-focus', 'qt-focus-editor', ...featureSuites()]) {
        assert.deepEqual(replayRequest([fixture]), { version: 1, fixture, command: `just ui-test ${fixture}` });
    }
    for (const args of [[''], ['unknown'], ['../controls'], ['all', 'noko'], ['--help']]) {
        assert.throws(() => replayRequest(args), /usage: just ui-test/);
    }
});

test('Bar geometry and service overlays remain explicit and isolated', () => {
    for (const size of ['1366x768', '2560x1440'])
        assert.deepEqual(replayRequest(['bar', size]), { version: 1, fixture: 'bar', size, command: `just ui-test bar ${size}` });
    for (const args of [['bar', '0x0'], ['bar', '../host'], ['all', '1366x768'], ['bar', '1366x768', 'extra']])
        assert.throws(() => replayRequest(args));
    assert.equal(new Set(barOverlayFixtures.map(row => row.target)).size, barOverlayFixtures.length);
    for (const { source, target } of barOverlayFixtures) {
        assert.match(source, /^(?:test-fixtures\/ui\/fixture-services\/(?:bar|lifecycle)\/|features\/[a-z-]+\/fixtures\/services\/)\w+\.qml$/);
        assert.match(target, /^\w+\.qml$/);
        assertPureService(readFileSync(source, 'utf8'));
    }
    const source = readFileSync('test-fixtures/ui/bar.qml', 'utf8');
    assert.match(source, /N\.Bar\s*\{/);
    // Registry controls never serialize editable text, only whether it is filled
    // and whether a password editor masks it.
    assert.match(source, /"text": editable \? "" : text,/);
    assert.match(source, /"masked": item\.echoMode === TextInput\.Password,/);
    assert.match(source, /&& !root\.editableItem\(item\)\) \{\n\s*state\.texts\.push\(item\.text\);\n(?:\s*\/\/.*\n)*\s*if \(item\.userText === true\)\n\s*state\.userTexts\.push\(item\.text\);/);
});

test('subscription termination requires an exact native command and direct QML parent', () => {
    const command = '/usr/bin/niri\0msg\0--json\0event-stream\0';
    assert.doesNotThrow(() => assertSubscription(42, 30, 'Name:\tniri\nPPid:\t30\n', command));
    for (const pid of [0, 1, -1, null, '42', 42.5]) {
        assert.throws(() => assertSubscription(pid, 30, 'PPid:\t30\n', command));
    }
    for (const [parent, status, args] of [
        [1, 'PPid:\t1\n', command], [30, 'PPid:\t99\n', command],
        [30, 'Name:\tniri\n', command], [30, 'PPid:\t30\n', '/usr/bin/niri\0'],
        [30, 'PPid:\t30\n', '/other/niri\0msg\0--json\0event-stream\0'],
        [30, 'PPid:\t30\n', command.replace('event-stream', 'outputs')],
        [30, 'PPid:\t30\n', command + 'extra\0']
    ]) assert.throws(() => assertSubscription(42, parent, status, args));
});

test('niri fixture drives the real Bar/Niri route', () => {
    const fixture = readFileSync('test-fixtures/ui/niri-integration.qml', 'utf8');
    for (const type of ['Bar', 'Niri']) assert(fixture.includes(`N.${type} {`));
    assert.match(fixture, /function fixtureOutputs\(enabled: bool\)/);
    const runner = readFileSync('ui-test.mjs', 'utf8');
    assert.match(runner, /const integration = replay.fixture === 'niri'/);
    assert.match(runner, /await runNiriIntegration\(/);
    // Native features keep production services only in the integration fixture.
    assert.match(runner, /featureDoubles\('\/project', null, integration \? nativeFeatures : \[\]\)/);
    const registry = readFileSync('Registry.qml', 'utf8');
    const binding = /\n    N\.Registry \{\n([\s\S]*?)\n    \}\n/.exec(fixture)[1];
    for (const name of nativeFeatures) {
        assert.match(registry, new RegExp(`^\\s*name: "${name}"$`, 'm'), `${name} is a registry feature`);
        const doubles = featureDoubles(undefined, name);
        assert(doubles.length > 0, `${name} has a double that other fixtures use`);
        assert.deepEqual(featureDoubles(undefined, name, nativeFeatures), [], `${name} keeps production in integration`);
        // The fixture binds every registry input the production service is given.
        const service = new RegExp(`\\n    readonly property \\w+\\.\\w+ ${name}: \\w+\\.\\w+ \\{\\n([\\s\\S]*?)\\n    \\}\\n`).exec(registry)[1];
        const inputs = [...registry.matchAll(/^    property [A-Z]\w* (\w+): null$/gm)].map(match => match[1]).filter(input => service.includes(`root.${input}`));
        assert(inputs.length > 0, `${name} service inputs found`);
        for (const input of inputs)
            assert.match(binding, new RegExp(`^        ${input}: \\w+$`, 'm'), `niri fixture binds ${name} input ${input}`);
    }
});

test('failure screenshots allow masked input but never revealed input or unknown auth state', () => {
    assert.doesNotThrow(() => assertSafeCapture({ auth: { visible: true, hasInput: true, inputMasked: true } }));
    assert.doesNotThrow(() => assertSafeCapture({ auth: { visible: true, hasInput: false, inputMasked: false } }));
    assert.throws(() => assertSafeCapture({ auth: { visible: true, hasInput: true, inputMasked: false } }), /unmasked input/);
    assert.throws(() => assertSafeCapture({ auth: { visible: true } }), /unmasked input/);
    assert.throws(() => assertSafeCapture(null), /snapshot unavailable/);
});

test('failure evidence retains the original error, named phase and last safe snapshot', async () => {
    const calls = [];
    const snapshot = { auth: { visible: true, hasInput: true, inputMasked: true } };
    const error = new Error('original assertion');
    const record = await captureUiFailure(error, {
        evidence: '/evidence', fixture: 'privilege', phase: 'privilege', snapshot,
        screenshot: async name => { calls.push(name); return '/evidence/failure.png'; },
        write: (path, text) => calls.push([path, JSON.parse(text)])
    });
    assert.equal(record.error, error.stack);
    assert.equal(record.fixture, 'privilege');
    assert.equal(record.phase, 'privilege');
    assert.deepEqual(record.snapshot, snapshot);
    assert.equal(record.screenshot, '/evidence/failure.png');
    assert.deepEqual(record.captureErrors, []);
    assert.equal(calls[0], 'failure');
    assert.deepEqual(calls[1], ['/evidence/failure.json', record]);
    const failedCapture = await captureUiFailure(error, {
        evidence: '/evidence', fixture: 'all', phase: 'controls', snapshot,
        screenshot: async () => { throw new Error('capture socket gone'); }, write: () => {}
    });
    assert.equal(failedCapture.error, error.stack);
    assert.equal(failedCapture.screenshot, null);
    assert.match(failedCapture.captureErrors[0], /capture socket gone/);
    assert.deepEqual(failedCapture.snapshot, snapshot);
});

test('runtime gate is captured before teardown; all children and complete logs are still checked', async () => {
    for (const original of [null, new Error('original interaction')]) {
        const calls = [];
        const diagnostic = new Error('WARN private login1');
        await assert.rejects(finishUiRun(original, {
            children: ['weston', 'niri', 'fixture'],
            checkLogs: () => { calls.push('logs'); throw diagnostic; },
            capture: async error => { calls.push(['capture', error]); },
            stop: async child => { calls.push(['stop', child]); }
        }), error => error === (original || diagnostic));
        assert.deepEqual(calls, ['logs', ['capture', original || diagnostic], ['stop', 'fixture'], ['stop', 'niri'], ['stop', 'weston'], 'logs']);
    }
});

test('clean completion checks full logs on both sides of teardown without failure capture', async () => {
    const calls = [];
    const children = ['weston', 'niri'];
    await finishUiRun(null, {
        children,
        checkLogs: () => calls.push('logs'),
        capture: async () => assert.fail('unexpected failure capture'),
        stop: async child => calls.push(child)
    });
    assert.deepEqual(calls, ['logs', 'niri', 'weston', 'logs']);
    assert.deepEqual(children, ['weston', 'niri']);
});

test('capture and teardown errors cannot replace the original error or skip sibling cleanup', async () => {
    const original = new Error('original assertion');
    const stopped = [];
    const secondary = [];
    await assert.rejects(finishUiRun(original, {
        children: ['weston', 'niri', 'fixture'],
        checkLogs: () => {},
        capture: async () => { throw new Error('evidence unwritable'); },
        stop: async child => { stopped.push(child); if (child === 'fixture') throw new Error('teardown error'); },
        report: error => secondary.push(error.message)
    }), error => error === original);
    assert.deepEqual(stopped, ['fixture', 'niri', 'weston']);
    assert.deepEqual(secondary, ['evidence unwritable', 'teardown error']);
});

test('diagnostics produced only during teardown still fail the run', async () => {
    const diagnostic = new Error('ERROR during shutdown');
    let stopped = false;
    let captured;
    await assert.rejects(finishUiRun(null, {
        children: ['niri'],
        checkLogs: () => { if (stopped) throw diagnostic; },
        capture: async error => { captured = error; },
        stop: async () => { stopped = true; }
    }), error => error === diagnostic);
    assert.equal(captured, diagnostic);
});

test('fixture snapshots serialize predicates, never editable contents', () => {
    const controls = readFileSync('ui-test.qml', 'utf8');
    assert(!/query: root\.query|text: field\.text/.test(controls));
    assert.match(controls, /queryMatchesInput: root\.query === field\.text/);
    const lifecycle = readFileSync('test-fixtures/ui/lifecycle.qml', 'utf8');
    assert.match(lifecycle, /child\.echoMode === undefined/);
    assert.match(lifecycle, /hasInput: input \? input\.text\.length > 0 : false/);
    assert.match(lifecycle, /inputMasked: input \? input\.echoMode === TextInput\.Password : false/);
});

test('snapshot predicates and visual-text selection redact hostile editable values', () => {
    const sentinel = 'fixture-only\"\\nhttps://private.invalid/account';
    const controls = readFileSync('ui-test.qml', 'utf8');
    const body = controls.match(/function snapshot\(\): string \{([\s\S]*?)\n        \}/)[1];
    const field = { text: sentinel, input: {} };
    const state = JSON.parse(runInNewContext(`(function () { ${body} })()`, {
        root: { query: sentinel, rect: () => ({ x: 1, y: 2, width: 3, height: 4 }) },
        field, window: {}, mute: {}, slider: { Accessible: {} }, surface: {},
        actionCard: {}, nestedAction: {}, iconAction: { Accessible: {} }, disabledAction: {},
        longAction: {}, iconField: { input: { text: sentinel } }
    }));
    assert.equal(state.queryMatchesInput, true);
    assert.equal(state.queryIsHeadphones, false);
    assert(!JSON.stringify(state).includes('fixture-only'));
    const lifecycle = readFileSync('test-fixtures/ui/lifecycle.qml', 'utf8');
    const visibleText = lifecycle.match(/function visibleText\(item: var, window: var\): var \{([\s\S]*?)\n    \}/)[1];
    for (const echoMode of [0, 1, 2, 3]) {
        const rows = runInNewContext(`(function (item, window) { ${visibleText} })(null, null)`, {
            tree: () => [{ visible: true, font: {}, text: sentinel, echoMode }, { visible: true, font: {}, text: 'safe label' }],
            rect: () => ({ x: 1, y: 2, width: 3, height: 4 })
        });
        assert.equal(rows.length, 1);
        assert.equal(rows[0].text, 'safe label');
        assert(!JSON.stringify(rows).includes('fixture-only'));
    }
});

test('input targets must be real sockets inside the owned runtime', async () => {
    const runtime = mkdtempSync(join(tmpdir(), 'ui-boundary-'));
    const outside = mkdtempSync(join(tmpdir(), 'ui-parent-'));
    const servers = [];
    async function socket(path) {
        const server = createServer();
        await new Promise((resolve, reject) => {
            server.once('error', reject);
            server.listen(path, resolve);
        });
        servers.push(server);
        return path;
    }
    try {
        const display = await socket(join(runtime, 'wayland-1'));
        const ipc = await socket(join(runtime, 'niri.wayland-1.42.sock'));
        const parent = await socket(join(outside, 'wayland-0'));
        const weston = await socket(join(runtime, 'weston-headless'));
        const env = childEnvironment(runtime, display, ipc);
        assert.equal(env.WAYLAND_DISPLAY, display);
        assert.equal(env.NIRI_SOCKET, ipc);
        assert.equal(env.XDG_RUNTIME_DIR, runtime);
        assert.throws(() => childEnvironment(runtime, parent, ipc), /refusing non-child socket/);
        assert.throws(() => childEnvironment(runtime, weston, ipc), /regular expression/);
        const file = join(runtime, 'wayland-2');
        writeFileSync(file, 'not a socket');
        assert.throws(() => childEnvironment(runtime, file, ipc), /must be a socket/);
        assert.throws(() => childEnvironment(runtime, ipc, display), /regular expression/);
    } finally {
        for (const server of servers) await new Promise(resolve => server.close(resolve));
        rmSync(runtime, { recursive: true });
        rmSync(outside, { recursive: true });
    }
});

test('runtime diagnostics fail rather than being filtered or downgraded', () => {
    assert.doesNotThrow(() => assertCleanLog('INFO: Configuration Loaded\n'));
    for (const diagnostic of ['WARN: unresolved binding', 'WARNING: unresolved binding', 'warning: deprecated API', 'Could not bind', 'failed to load', 'ERROR: creation failed', 'ReferenceError: missing', 'TypeError: wrong value', 'binding loop detected', 'Error parsing file']) {
        assert.throws(() => assertCleanLog(diagnostic));
    }
});

const niriRecord = (level, source, message) => `2026-09-06T14:51:45.642004Z ${level} ${source}: ${message}\n`;
const softwareFallback = niriRecord('DEBUG', 'niri::backend::winit', 'failed building default dmabuf feedback, falling back to v3: error getting EGL device render node\n\nCaused by:\n    None of the following EGL extensions is supported by the underlying EGL implementation, at least one is required: ["EGL_EXT_device_drm"]');
const nativeWriteWarning = niriRecord('WARN', 'niri::niri', 'error saving screenshot image: Os { code: 2, kind: NotFound, message: "No such file or directory" }');

test('only the exact Niri software fallback at DEBUG is a sandbox diagnostic exception', () => {
    assert.deepEqual(assertNiriLog(''), { softwareFallbacks: 0 });
    assert.deepEqual(assertNiriLog(softwareFallback), { softwareFallbacks: 1 });
    assert.deepEqual(assertNiriLog(softwareFallback.replace('DEBUG', '\x1b[34mDEBUG\x1b[0m')), { softwareFallbacks: 1 });
    for (const invalid of [
        softwareFallback.replace('DEBUG', 'WARN'), softwareFallback.replace('DEBUG', 'ERROR'),
        softwareFallback.replace('niri::backend::winit', 'niri::renderer'),
        softwareFallback.replace('EGL_EXT_device_drm', 'EGL_other_extension'),
        softwareFallback.replace('falling back to v3', 'aborting'),
        softwareFallback + softwareFallback, softwareFallback + 'WARN: unrelated\n',
        niriRecord('DEBUG', 'niri::backend::winit', 'failed to initialize renderer'),
        niriRecord('WARN', 'niri::dbus::freedesktop_login1', 'service absent'),
        niriRecord('WARN', 'niri::dbus::freedesktop_locale1', 'service absent'),
        nativeWriteWarning
    ]) assert.throws(() => assertNiriLog(invalid));
    // Production/default log checking has no exceptions.
    assert.throws(() => assertCleanLog(softwareFallback));
    assert.throws(() => assertCleanLog(nativeWriteWarning));
});

test('native diagnostic exceptions apply only to the child compositor and fixtures precede its launch', () => {
    const runner = readFileSync('ui-test.mjs', 'utf8');
    assert.match(runner, /log === join\(evidence, 'niri.log'\)\) diagnostics = assertNiriLog\(text\)/);
    assert.match(runner, /else assertCleanLog\(text\)/);
    assert(runner.indexOf("await waitFor('private login/locale fixtures'") < runner.indexOf("const compositor = start('niri'"));
    assert.match(runner, /Niri consumed private login\/locale properties/);
    const fixture = readFileSync('test-fixtures/ui/session-bus.c', 'utf8');
    assert.match(fixture, /sd_bus_set_address\(bus, address\)/);
    assert.match(fixture, /strcmp\(address, getenv\("DBUS_SYSTEM_BUS_ADDRESS"\)\)/);
    assert(!/SD_BUS_METHOD|SD_BUS_WRITABLE_PROPERTY|sd_bus_open_system/.test(fixture), 'fixture exports no mutation or implicit system bus access');
});

test('overlay service doubles are pure QtQuick Item state/functions without privileged APIs', () => {
    for (const name of ['Privilege', 'Niri']) {
        assertPureService(readFileSync(`test-fixtures/ui/fixture-services/lifecycle/${name}.qml`, 'utf8'));
    }
    const doubles = featureDoubles();
    assert(doubles.some(({ target }) => target === 'features/audio/Audio.qml'), 'audio double replaces the feature service');
    for (const { source, target } of doubles) {
        assert.notEqual(source, target);
        assertPureService(readFileSync(source, 'utf8'));
    }
    for (const body of ['Process {}', 'FileView {}', 'PolkitAgent {}', 'PamContext {}', 'WorkerScript {}', 'function bad() { Qt.openUrlExternally("test"); }', 'function bad() { new XMLHttpRequest(); }', 'function bad() { fetch("test"); }', 'function bad() { eval("test"); }', 'function bad() { Quickshell.execDetached([]); }']) {
        assert.throws(() => assertPureService(`import QtQuick\nItem { ${body} }`));
    }
    assert.throws(() => assertPureService('import QtQuick\nimport Quickshell.Io\nItem {}'));
    assert.throws(() => assertPureService('import "../../"\nItem {}'));
});

test('fixture replacement reaps only its own process, including an uncooperative child', { timeout: 8000 }, async () => {
    const sibling = spawnIsolated(process.execPath, ['-e', 'setInterval(() => {}, 1000)']);
    try {
        for (const ignoreTerm of [false, true]) {
            const child = spawnIsolated(process.execPath, ['-e', `${ignoreTerm ? 'process.on("SIGTERM", () => {});' : ''} console.log("ready"); setInterval(() => {}, 1000);`]);
            try {
                await new Promise(resolve => child.stdout.once('data', resolve));
                await stopChild(child);
                assert.equal(child.signalCode, ignoreTerm ? 'SIGKILL' : 'SIGTERM');
                assert.equal(sibling.exitCode, null);
                assert.equal(sibling.signalCode, null);
                await stopChild(child);
            } finally {
                await stopChild(child);
            }
        }
    } finally {
        await stopChild(sibling);
    }
});

test('an unsuccessful spawn has nothing to reap', async () => {
    const child = spawnIsolated('/nonexistent-nuguland-fixture-command');
    await new Promise(resolve => child.once('error', resolve));
    await stopChild(child);
});

test('invalid invocation and unsandboxed internal invocation fail before launching a compositor', () => {
    for (const args of [['unexpected'], ['--inside'], ['controls', 'noko'], ['--inside', 'invalid']]) {
        const result = runIsolated(process.execPath, ['ui-test.mjs', ...args], { encoding: 'utf8', timeout: 5000 });
        assert.equal(result.status, 1);
        assert.match(result.stderr, args.length === 1 && args[0] === '--inside' ? /internal mode requires sandbox/ : /usage: just ui-test/);
        assert(!result.stdout.includes('evidence:'));
    }
});

test('named selections reach the sandbox and its original failure evidence is never overwritten', () => {
    const dir = mkdtempSync(join(tmpdir(), 'ui-replay-'));
    try {
        writeFileSync(join(dir, 'bwrap'), `#!${process.execPath}
const fs = require('node:fs');
const args = process.argv.slice(2);
const evidence = args[args.indexOf('/evidence') - 1];
fs.writeFileSync(evidence + '/failure.json', JSON.stringify({ error: 'original inner assertion', fixture: args.at(-1) }));
process.exit(7);
`, { mode: 0o755 });
        for (const fixture of ['controls', 'popups', 'privilege', 'niri', 'bar', 'qt-focus', 'qt-focus-editor', ...featureSuites()]) {
            const result = runIsolated(process.execPath, ['ui-test.mjs', fixture], { env: { PATH: dir, TMPDIR: dir }, encoding: 'utf8', timeout: 5000 });
            const evidence = result.stdout.match(/^evidence: (.+)$/m)?.[1];
            assert(evidence, result.stderr);
            try {
                assert.equal(result.status, 1);
                assert.deepEqual(JSON.parse(readFileSync(join(evidence, 'failure.json'), 'utf8')), { error: 'original inner assertion', fixture });
                assert.deepEqual(JSON.parse(readFileSync(join(evidence, 'replay.json'), 'utf8')), { ...replayRequest([fixture]), phase: 'startup' });
                const { command } = JSON.parse(readFileSync(join(evidence, 'command.json'), 'utf8'));
                assert.deepEqual(command.slice(-2), ['--inside', fixture]);
                assert(!existsSync(command[command.indexOf('/run/user/1000') - 1]));
            } finally {
                rmSync(evidence, { recursive: true });
            }
        }
    } finally {
        rmSync(dir, { recursive: true });
    }
});

function fakeSuiteSandbox(dir, body) {
    writeFileSync(join(dir, 'bwrap'), `#!${process.execPath}
const fs = require('node:fs');
const args = process.argv.slice(2);
const evidence = args[args.indexOf('/evidence') - 1];
const fixture = args.at(-1);
${body}
`, { mode: 0o755 });
}

function suiteLines(stdout) {
    return Object.fromEntries([...stdout.matchAll(/^(pass|FAIL) suite (\S+) \([\d.]+s\): (.*)(?:\n  evidence: (.+))?$/gm)]
        .map(([, status, fixture, detail, failed]) => [fixture, { status, detail, evidence: status === 'pass' ? detail : failed }]));
}

function assertSuiteSandbox(fixture, evidence) {
    const { command } = JSON.parse(readFileSync(join(evidence, 'command.json'), 'utf8'));
    assert.deepEqual(command.slice(-2), ['--inside', fixture]);
    assert.equal(JSON.parse(readFileSync(join(evidence, 'replay.json'), 'utf8')).fixture, fixture);
    assert(!existsSync(command[command.indexOf('/run/user/1000') - 1]), `${fixture}: runtime leaked`);
}

test('every registry entry names an existing load target and a suite that all runs', () => {
    const registry = readFileSync('Registry.qml', 'utf8');
    const names = [...registry.matchAll(/^\s*FeatureEntry \{\s*name: "([a-z][a-z0-9-]*)"/gm)].map(match => match[1]);
    // Registry entries and feature directories with suites match one to one; no list to edit.
    assert(names.length > 0);
    assert.deepEqual([...names].sort(), featureSuites());
    assert.doesNotMatch(registry, /uiSuite:|loadTarget:/, 'proof locations follow the feature name');
    // Host inputs may be null in fixtures, so production must bind every one.
    const inputs = [...registry.matchAll(/^    property [A-Z]\w* (\w+): null$/gm)].map(match => match[1]);
    const composition = /\n    Registry \{\n([\s\S]*?)\n    \}\n/.exec(readFileSync('shell.qml', 'utf8'))[1];
    for (const input of inputs) assert.match(composition, new RegExp(`^        ${input}: \\w+$`, 'm'), `shell.qml binds registry input ${input}`);
    const kinds = [...registry.matchAll(/^\s*kind: "([a-z]+)"/gm)].map(match => match[1]);
    for (const kind of kinds) assert(['popup', 'overlay', 'toggle'].includes(kind), `kind ${kind} has a host`);
    for (const [kind, host] of [['popups', 'SurfaceManager.qml'], ['popup(', 'PopupHost.qml'], ['overlays', 'OverlayHost.qml'], ['focusOverlays', 'OverlayHost.qml'], ['toggle(', 'Bar.qml']])
        assert(readFileSync(host, 'utf8').includes(`registry.${kind}`), `${host} reads registry ${kind}`);
    // Popup and overlay entries render a panel; a toggle entry is only a chip.
    for (const entry of registryEntries()) assert.equal(entry.panel, entry.kind !== 'toggle', `${entry.name} panel matches kind ${entry.kind}`);
    // Hosts read entries, never a registry feature's name; the legacy catalog is gone.
    for (const host of ['Bar.qml', 'PopupHost.qml', 'SurfaceManager.qml', 'OverlayHost.qml']) {
        const source = readFileSync(host, 'utf8');
        for (const name of names) assert(!source.includes(`"${name}"`), `${host} names registry feature ${name}`);
    }
    // The bar chip renders every entry's emphasis fields; the noko suite proves them in pixels.
    const bar = readFileSync('Bar.qml', 'utf8');
    for (const field of ['barLabel', 'barTitle', 'barIcon', 'barBadge', 'barActive', 'barStrong', 'barRole', 'barBusy'])
        assert(bar.includes(`feature.${field}`), `Bar reads FeatureEntry.${field}`);
    for (const name of names) {
        assert(existsSync(`features/${name}/fixtures/load.qml`), `${name} load target`);
        assert(existsSync(`features/${name}/fixtures/suite.mjs`), `${name} suite`);
        assert(allSuites.includes(name), `${name} suite runs in all`);
        assert.deepEqual(replayRequest([name]).fixture, name);
    }
});

test('a feature production proof is a default-exported function beside its suite', async () => {
    const runner = readFileSync('ui-test.mjs', 'utf8');
    assert.match(runner, /const \{ default: proof \} = await import\(`\/project\/features\/\$\{replay\.fixture\}\/fixtures\/production\.mjs`\)/);
    let proofs = 0;
    for (const name of featureSuites()) {
        const path = `features/${name}/fixtures/production.mjs`;
        if (!existsSync(path)) continue;
        const { default: proof } = await import(`./${path}`);
        assert.equal(typeof proof, 'function', `${name} production proof`);
        proofs++;
    }
    assert(proofs > 0, 'the system suite keeps its production proof');
});

test('all runs every suite in its own sandbox and leads failures with the suite record', () => {
    // qt-focus suites are upstream Qt diagnostics.
    assert.deepEqual([...allSuites].sort(), ['controls', 'shell', 'niri', 'popups', 'privilege', 'bar', ...featureSuites()].sort());
    const dir = mkdtempSync(join(tmpdir(), 'ui-all-'));
    try {
        fakeSuiteSandbox(dir, `if (fixture === 'noko') {
    fs.writeFileSync(evidence + '/failure.json', JSON.stringify({ error: 'AssertionError: noko check label\\n    at stack' }));
    process.exit(7);
}`);
        const result = runIsolated(process.execPath, ['ui-test.mjs', 'all'], { env: { PATH: dir, TMPDIR: dir }, encoding: 'utf8', timeout: 10_000 });
        assert.equal(result.status, 1, result.stderr);
        const suites = suiteLines(result.stdout);
        assert.deepEqual(Object.keys(suites).sort(), [...allSuites].sort(), result.stdout);
        for (const fixture of allSuites) {
            assert.equal(suites[fixture].status, fixture === 'noko' ? 'FAIL' : 'pass');
            assertSuiteSandbox(fixture, suites[fixture].evidence);
        }
        assert.equal(suites.noko.detail, 'AssertionError: noko check label');
        assert.match(result.stdout, new RegExp(`^all: ${allSuites.length - 1}/${allSuites.length} suites passed in [\\d.]+s$`, 'm'));
        assert.match(result.stderr, /failed suites: noko/);
    } finally {
        rmSync(dir, { recursive: true });
    }
});

test('all bounds compositor admission; interruption cleans active suites and never launches queued suites', { timeout: 15_000 }, async () => {
    const dir = mkdtempSync(join(tmpdir(), 'ui-all-interrupt-'));
    try {
        fakeSuiteSandbox(dir, `fs.writeFileSync(${JSON.stringify(dir)} + '/ready-' + fixture, '');
process.on('SIGTERM', () => process.exit(0));
setInterval(() => {}, 1000);`);
        const child = spawnIsolated(process.execPath, ['ui-test.mjs', 'all'], { env: { PATH: dir, TMPDIR: dir }, stdio: ['ignore', 'pipe', 'pipe'] });
        let stdout = '';
        let stderr = '';
        child.stdout.on('data', data => { stdout += data; });
        child.stderr.on('data', data => { stderr += data; });
        while (readdirSync(dir).filter(name => name.startsWith('ready-')).length < 2)
            await new Promise(resolve => setTimeout(resolve, 20));
        child.kill('SIGINT');
        assert.equal(await new Promise(resolve => child.once('close', resolve)), 1);
        // One shared signal dispatcher, not one listener pair per sandbox.
        assert.doesNotMatch(stderr, /MaxListenersExceededWarning/);
        const suites = suiteLines(stdout);
        for (const fixture of allSuites) {
            assert.equal(suites[fixture]?.status, 'FAIL', stdout);
            assert.match(suites[fixture].detail, /interrupted/);
            if (allSuites.indexOf(fixture) < 2) assertSuiteSandbox(fixture, suites[fixture].evidence);
            else {
                assert.match(suites[fixture].detail, /queued suite not run/);
                assert.equal(suites[fixture].evidence, 'none');
            }
        }
        assert.equal(readdirSync(dir).filter(name => name.startsWith('ready-')).length, 2, 'no admission after interruption');
    } finally {
        rmSync(dir, { recursive: true });
    }
});

test('sandbox failure, spawn error, and interruption remove the owned runtime and fail', { timeout: 10_000 }, async () => {
    const dir = mkdtempSync(join(tmpdir(), 'ui-lifecycle-'));
    try {
        for (const mode of ['failure', 'interrupt', 'stubborn', 'missing']) {
            const executable = join(dir, 'bwrap');
            if (mode === 'missing') rmSync(executable);
            else writeFileSync(executable, `#!${process.execPath}\n${mode === 'failure' ? 'process.exit(7)' : `process.on("SIGTERM", () => { ${mode === 'stubborn' ? '' : 'process.exit(0)'} }); console.log("sandbox-ready"); setInterval(() => {}, 1000)`}\n`, { mode: 0o755 });
            const child = spawnIsolated(process.execPath, ['ui-test.mjs', 'controls'], {
                env: { PATH: dir, TMPDIR: dir },
                stdio: ['ignore', 'pipe', 'pipe']
            });
            let stdout = '';
            let stderr = '';
            child.stdout.on('data', data => {
                stdout += data;
                if (['interrupt', 'stubborn'].includes(mode) && stdout.includes('sandbox-ready')) child.kill('SIGINT');
            });
            child.stderr.on('data', data => { stderr += data; });
            const code = await new Promise((resolve, reject) => {
                child.once('error', reject);
                child.once('close', resolve);
            });
            const evidence = stdout.match(/^evidence: (.+)$/m)?.[1];
            assert(evidence, `${mode}: no evidence directory`);
            try {
                assert.equal(code, 1, `${mode}: ${stderr}`);
                const failure = JSON.parse(readFileSync(join(evidence, 'failure.json'), 'utf8'));
                assert.equal(failure.fixture, 'controls');
                assert.equal(failure.phase, 'startup');
                assert.equal(failure.snapshot, null);
                assert.equal(failure.screenshot, null);
                assert.match(failure.captureErrors[0], /sandbox unavailable/);
                assert.match(failure.error, mode === 'missing' ? /ENOENT/ : ['interrupt', 'stubborn'].includes(mode) ? /interrupted/ : /nested UI test failed/);
                const replay = JSON.parse(readFileSync(join(evidence, 'replay.json'), 'utf8'));
                assert.deepEqual(replay, { ...replayRequest(['controls']), phase: 'startup' });
                const { command } = JSON.parse(readFileSync(join(evidence, 'command.json'), 'utf8'));
                assert.deepEqual(command.slice(-2), ['--inside', 'controls']);
                const runtime = command[command.indexOf('/run/user/1000') - 1];
                assert(runtime.startsWith(join(dir, 'nuguland-ui-runtime-')));
                assert(!existsSync(runtime), `${mode}: runtime leaked`);
                assert(!command.includes('WAYLAND_DISPLAY'), `${mode}: inherited display configuration`);
                assert(!command.includes('/run/parent-display'), `${mode}: exposed host compositor`);
                if (['interrupt', 'stubborn'].includes(mode)) assert.match(stderr, /interrupted/);
            } finally {
                rmSync(evidence, { recursive: true });
            }
        }
    } finally {
        rmSync(dir, { recursive: true });
    }
});