Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/features/notifications/fixtures/production.mjs

Raw
import assert from 'node:assert/strict';
import { appendFileSync, readFileSync, writeFileSync } from 'node:fs';
import { spawnSync } from 'node:child_process';

export default async function notificationsProduction({ env, start, stop, waitFor, ipc, setFixture, screenshot }) {
    assert.equal(process.env.NUGULAND_UI_SANDBOX, '1');
    const fixture = '/project/notifications-production-probe.qml';
    writeFileSync(fixture, readFileSync('/project/features/notifications/fixtures/production-probe.qml', 'utf8').replaceAll('import "../../../features/', 'import "./features/').replace('import "../../.." as N', 'import "." as N'));
    const child = start('qs', ['--no-color', '-p', fixture], 'notifications-production-probe', env);
    setFixture(fixture, '/evidence/notifications-production-probe.log', child);
    try {
        await waitFor('production notifications probe loaded', () => readFileSync('/evidence/notifications-production-probe.log', 'utf8').includes('Configuration Loaded'));
        ipc('copy');
        const state = await waitFor('notification body survives clipboard stdin', () => {
            const value = JSON.parse(ipc('result'));
            if (value.matches) return value;
            ipc('read');
            return null;
        });
        assert.equal(state.length, 52015);
        await nativeLifecycle({ env, start, stop, waitFor, ipc, screenshot });
    } finally {
        await stop(child);
    }
}

async function nativeLifecycle({ env, start, stop, waitFor, ipc, screenshot }) {
    const monitor = start('dbus-monitor', ['--session', "type='signal',interface='org.freedesktop.Notifications'"], 'notification-signals', env);
    const signalLog = () => readFileSync('/evidence/notification-signals.log', 'utf8');
    const expectedClosed = [];
    const bus = (method, signature, ...args) => {
        const argv = ['--user', 'call', 'org.freedesktop.Notifications', '/org/freedesktop/Notifications', 'org.freedesktop.Notifications', method, signature, ...args.map(String)];
        const result = spawnSync('busctl', argv, { env, encoding: 'utf8', timeout: 3000 });
        appendFileSync('/evidence/commands.log', JSON.stringify({ command: 'busctl', argv, status: result.status, stdout: result.stdout, stderr: result.stderr }) + '\n');
        assert.ifError(result.error);
        assert.equal(result.status, 0, result.stderr);
        return result.stdout.trim();
    };
    const notify = ({ replace = 0, transient = false, resident = false, summary = 'synthetic', urgency = 1, timeout = 5000, actions = [] } = {}) =>
        Number(bus('Notify', 'susssasa{sv}i', 'probe', replace, '', summary, 'synthetic body', actions.length, ...actions,
            4, 'transient', 'b', transient, 'resident', 'b', resident, 'urgency', 'y', urgency, 'suppress-sound', 'b', true, timeout).match(/^u (\d+)$/)[1]);
    const facts = () => JSON.parse(ipc('facts'));
    const intent = (name, id = '') => ipc('intent', name, String(id));
    const closeSignals = () => [...signalLog().matchAll(/member=NotificationClosed\s+uint32 (\d+)\s+uint32 (\d+)/g)].map(m => [Number(m[1]), Number(m[2])]);
    const closed = async (id, reason, ...others) => {
        expectedClosed.push([id, reason], ...others);
        await waitFor('native close signal', () => closeSignals().length >= expectedClosed.length);
        assert.deepEqual(closeSignals(), expectedClosed, 'one protocol close per notification, correct reason');
    };
    const counts = (history, toasts, native) => {
        const f = facts();
        assert.equal(f.state.activeCount, history);
        assert.equal(f.state.activeToasts.length, toasts);
        assert.equal(f.state.trackedNativeCount, native);
        assert.equal(f.native.length, native, 'JS ownership matches upstream tracking');
        return f;
    };
    try {
        await waitFor('private notification signal monitor', () => signalLog().includes('member=NameLost'));
        intent('all');
        counts(0, 0, 0);
        intent('dnd');
        const suppressed = notify({ transient: true });
        counts(0, 0, 0);
        await closed(suppressed, 1);
        const critical = notify({ transient: true, urgency: 2 });
        assert.equal(facts().state.activeToasts[0].timeoutMs, 0);
        intent('toast', critical);
        await closed(critical, 2);
        intent('dnd');

        const expiring = notify({ transient: true, timeout: 1000 });
        const timed = counts(0, 1, 1).state.activeToasts[0];
        assert.equal(timed.timeoutMs, 1000);
        assert(timed.expiresAt > 0);
        await closed(expiring, 1);
        counts(0, 0, 0);
        const dismissed = notify({ transient: true, timeout: 0 });
        assert.equal(facts().state.activeToasts[0].expiresAt, 0);
        intent('toast', dismissed);
        counts(0, 0, 0);
        await closed(dismissed, 2);
        const pruned = notify({ transient: true });
        intent('dnd');
        counts(0, 0, 0);
        await closed(pruned, 1);
        intent('dnd');

        const history = notify({ summary: 'original' });
        const deadline = facts().state.activeToasts[0].expiresAt;
        intent('read');
        assert.equal(notify({ replace: history, summary: 'replacement' }), history);
        let f = counts(1, 1, 1);
        assert.equal(f.state.groups[0].notifications[0].summary, 'replacement');
        assert.equal(f.state.groups[0].notifications[0].read, true);
        assert.equal(f.state.activeToasts[0].expiresAt, deadline);
        intent('expire', history);
        counts(1, 0, 1);
        assert.deepEqual(closeSignals(), expectedClosed, 'toast expiry preserves native history');
        const sticky = notify({ transient: true, urgency: 2 });
        intent('all');
        counts(0, 0, 0);
        await closed(history, 2, [sticky, 2]);
        const fresh = notify({ replace: history, summary: 'after clear' });
        assert.notEqual(fresh, history);
        assert.equal(counts(1, 1, 1).state.groups[0].notifications[0].summary, 'after clear');
        intent('all');
        await closed(fresh, 2);

        for (const route of ['action', 'reply']) {
            const actions = route === 'action' ? ['default', 'open'] : ['inline-reply', 'reply'];
            for (const resident of [false, true]) {
                const id = notify({ resident, actions });
                intent(route, id);
                counts(resident ? 1 : 0, resident ? 1 : 0, resident ? 1 : 0);
                if (resident) intent('close', id);
                await closed(id, 2);
                intent('all');
            }
            // Replacement changes live residency without re-emitting acceptance.
            for (const resident of [false, true]) {
                const id = notify({ resident, actions });
                // Quickshell retains inline reply capability, so omit that action
                // during replacement rather than sending duplicate inline-reply.
                notify({ replace: id, resident: !resident, actions: route === 'reply' ? [] : actions });
                intent(route, id);
                counts(resident ? 0 : 1, resident ? 0 : 1, resident ? 0 : 1);
                if (!resident) intent('close', id);
                await closed(id, 2);
                intent('all');
            }
        }

        for (const route of ['all', 'toast', 'expire', 'action', 'reply', 'close']) {
            const id = notify({ transient: route === 'toast' || route === 'expire', actions: ['default', 'open', 'inline-reply', 'reply'] });
            bus('CloseNotification', 'u', id);
            await closed(id, 3);
            assert.equal(facts().native.length, 0);
            intent(route, id);
            intent('all');
            counts(0, 0, 0);
        }
        assert.deepEqual(closeSignals(), expectedClosed);
        appendFileSync('/evidence/notification-native-facts.json', JSON.stringify({ closed: expectedClosed, final: facts() }, null, 2));
        notify({ summary: 'synthetic toast', timeout: 0 });
        await screenshot('notifications-native-toast');
        intent('all');
    } finally {
        await stop(monitor);
    }
}