Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/features/osd/fixtures/suite.mjs

Raw
// `just ui-test osd`: the registry OSD overlay inside nested niri. Glance-only,
// top-right, click-through, self-expiring. Events go through the real service.
// The suite contract (snapshot shape, helpers) is documented in .system/RULES.md.
export default async function osdSuite({ assert, snapshot, check, click, key, screenshot, call, within }) {
    const osd = value => value.features.osd;
    const service = value => osd(value).service;
    const card = value => osd(value).items.osdCard;

    let state = snapshot();
    assert.equal(service(state).visible, false, 'OSD starts hidden');
    assert.equal(osd(state).surface, null, 'hidden OSD maps no overlay surface');

    async function show(event, value, label, shown, name) {
        const before = service(snapshot()).restartCount;
        call('osd', 'trigger', event, value);
        let previous = '';
        const state = await check(`${event} event maps the OSD overlay`, current => {
            const items = osd(current).items;
            if (!service(current).visible || service(current).restartCount !== before + 1 || !osd(current).surface || !items.osdCard
                || items.osdLabel?.text !== label || (items.osdValue?.text ?? '') !== shown) return false;
            const geometry = JSON.stringify([osd(current).surface, items.osdCard.rect]);
            const settled = geometry === previous;
            previous = geometry;
            return settled;
        });
        const surface = osd(state).surface;
        const box = card(state).rect;
        assert(within(box, state.width, state.height), `${event} OSD card outside output`);
        assert.equal(surface.x + surface.width, state.width, `${event} overlay anchors the right edge`);
        assert.equal(surface.width, 316, `${event} overlay width comes from the registry entry`);
        assert.equal(box.x + box.width, state.width - 24, `${event} card keeps its right inset`);
        assert.equal(box.y, 56, `${event} card sits below the bar`);
        assert.equal(surface.height, box.height + 64, `${event} overlay hugs the card`);
        assert.equal(card(state).opacity, 1, `${event} card fully shown`);
        const icon = osd(state).items.osdIcon;
        assert(icon && icon.text.length > 0 && icon.text === service(state).glyph, `${event} card draws its glyph`);
        assert(icon.rect.width > 0 && icon.rect.height > 0, `${event} glyph has a rendered size`);
        await screenshot(`osd-${name}`);
        const after = service(snapshot());
        assert(after.visible && after.restartCount === before + 1, `${event} OSD stayed up through its screenshot`);
        return state;
    }

    state = await show('volume', '72', 'volume', '72%', 'volume-meter');
    assert.equal(service(state).role, 'ui.accent');
    await check('OSD expires on its own after its lifetime', value => !service(value).visible && service(value).expired && osd(value).surface === null);

    state = await show('microphone', 'muted', 'microphone', 'muted', 'microphone-muted');
    assert.equal(service(state).role, 'warning', 'muted microphone is a warning');
    const short = card(state).rect.height;
    state = await show('brightness', '40', 'brightness', '40%', 'brightness-retrigger');
    assert.equal(service(state).event, 'brightness', 'a new event replaces the visible one');
    state = await show('color', '#ff6600', 'color', '#ff6600', 'color-picked');
    assert.equal(card(state).rect.height, short, 'value-only events share one card height');
    const longMessage = 'screenshot failed: the output disappeared while the capture was still being written to disk';
    // An error is titled by its subject; the failure reads as its body.
    state = await show('error', longMessage, 'screenshot failed', '', 'long-message');
    assert.equal(service(state).role, 'error');
    assert(card(state).rect.height > short, 'error body line grows the card');
    const longBody = osd(state).items.osdMessage;
    const longCard = card(state).rect;
    assert.equal(longBody.text, 'the output disappeared while the capture was still being written to disk', 'error body carries the whole failure below its subject');
    assert(longBody.rect.y >= longCard.y && longBody.rect.y + longBody.rect.height <= longCard.y + longCard.height, 'long error body stays inside its card');
    state = await show('bogus', 'unknown', 'bogus', '', 'unknown-event');
    assert.equal(service(state).event, 'error', 'unsupported events render as errors titled by their name');
    assert.equal(osd(state).items.osdMessage.text, 'unknown');
    const shortBody = osd(state).items.osdMessage;
    assert(longBody.rect.height > 2 * shortBody.rect.height, 'long error body wraps past two lines instead of eliding');

    // Values from data (players, paths) read as human words, never as identifiers.
    state = await show('media', 'select org.mpris.MediaPlayer2.firefox.instance_1234', 'media', 'select firefox', 'media-player-name');
    state = await show('error', 'screenshot not taken · could not write /tmp/tmp.Xy7Qa2bLm0/screenshot-1a2b3c4d.png', 'screenshot not taken', '', 'temp-path-body');
    assert.equal(osd(state).items.osdMessage.text, 'could not write screenshot', 'a temp path in a failure reads as its kind');

    // The overlay covers the Bar's right end yet must never take its clicks.
    state = await check('previous OSD expired before the click-through', value => !service(value).visible);
    const restarts = service(state).restartCount;
    call('osd', 'trigger', 'volume', '30');
    const clock = value => value.barControls.clock?.rect;
    state = await check('OSD overlay maps over the Bar clock', value => service(value).visible && service(value).restartCount === restarts + 1 && osd(value).surface && clock(value));
    const surface = osd(state).surface;
    assert(clock(state).x >= surface.x && clock(state).y + clock(state).height <= surface.y + surface.height, 'clock lies under the overlay surface');
    click(clock(state));
    const during = service(snapshot());
    assert(during.visible && during.restartCount === restarts + 1, 'OSD was still mapped when the click landed');
    await check('click under the visible OSD reaches the Bar clock', value =>
        value.popup.name === 'clock' && value.popup.phase === 'open' && value.popup.windowActive && service(value).restartCount === restarts + 1);
    key('Escape');
    await check('Escape closes the calendar opened through the OSD', value => !value.popup.open);
    await check('OSD expires after the click-through', value => !service(value).visible && osd(value).surface === null);
}