Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

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

Raw
// `just ui-test noko`: the registry noko feature in the real Bar inside nested
// niri, driven by real pointer and keyboard against the feature's Noko double.
// Capture leads the panel by pointer and keyboard; it shows starting until the
// engine hears, then the newest words, and stopping it there closes the popup
// so the text lands where the keyboard was. Setup lives in Config and its link
// shows only while setup needs something; every opening rechecks setup;
// downloads name their size and wait for consent: a first install, models of
// the current engine, an update held for its models. The wake word turns on in
// Config and its open microphone shows on the chip without opening anything;
// hotwords are typed one per line. Keyboard-reachable history copy routed to
// the service, scroll retained across close and reorientation, history cleared
// only after an armed second press; the chip's busy, strong and warning
// emphasis; and the focus overlay's capture tab through every phase with the
// live words eliding at the start, the typed text after finishing, a dictation
// without text included, still in low-power mode, clear of the bar in both
// orientations.
// The suite contract (snapshot shape, helpers) is documented in .system/RULES.md.
export default async function nokoSuite({ assert, snapshot, open, check, click, key, input, screenshot, within, call, fixture }) {
    const noko = value => value.features.noko;
    const service = value => noko(value).service;
    const control = (value, name) => noko(value).controls[name];
    const capturing = value => service(value).starting || service(value).listening;
    const capture = value => control(value, capturing(value) ? 'stop capture' : 'start capture');
    const copy = (value, index) => noko(value).items[`copyTranscript${index}`];
    const viewport = value => noko(value).items.nokoHistory;
    const overlay = value => noko(value).focusOverlay;
    const tab = value => overlay(value)?.items.nokoStatusTab;
    const words = value => overlay(value)?.items.nokoLiveText;
    const chip = value => value.chips.noko;
    const config = value => value.features.config;
    const configControl = (value, name) => config(value).controls[name];
    const setupHidden = value => !control(value, 'configure noko') && !noko(value).texts.some(text => text.startsWith('speech engine'));
    const quietChip = value => chip(value).label === 'noko' && !chip(value).active && !chip(value).strong && !chip(value).busy && chip(value).role === 'ui.focus';
    const armedChip = value => chip(value).label === 'noko' && chip(value).active && !chip(value).strong && !chip(value).busy && chip(value).role === 'ui.accent' && chip(value).mark === 'activity' && chip(value).title === 'noko dictation · microphone on';
    const longWords = 'and so the dictation goes on and on while the newest words stay readable at the end of the line where you are still speaking';

    let state = await open('noko', value => capture(value)?.rect);
    state = await check('opening focuses capture', value => capture(value)?.focused);
    assert.equal(service(state).checks, 1, 'opening rechecks setup once');
    assert(quietChip(state), 'ready noko has a quiet chip');
    assert(['ready', 'start capture', 'clear history', 'last transcripts', 'synthetic capture 0', '2 s of speech'].every(text => noko(state).texts.includes(text)));
    assert(noko(state).items.nokoStatusLine && !noko(state).items.nokoStatus, 'routine state stays inline');
    assert(!noko(state).texts.includes('noko'), 'the panel does not repeat the chip name');
    assert(!noko(state).items.nokoWave && !noko(state).items.nokoLiveText, 'idle shows no wave and no words');
    assert(setupHidden(state), 'setup that needs nothing stays in Config');
    assert(capture(state).rect.y < control(state, 'clear history').rect.y, 'capture leads the panel');
    assert.equal(overlay(state), null, 'no focus overlay while idle');
    await screenshot('noko-open');

    click(capture(state).rect);
    state = await check('capture button starts the fixture action, the busy chip and the starting tab', value =>
        service(value).starting && service(value).triggers === 1 && chip(value).busy && chip(value).active && !chip(value).strong && chip(value).role === 'ui.accent'
        && overlay(value)?.texts.includes('starting') && !words(value) && noko(value).texts.includes('starting') && control(value, 'stop capture')
        && noko(value).items.nokoWave?.rect.y > capture(value).rect.y);
    call('noko', 'beginListening');
    call('noko', 'hear', 'take a note about the');
    state = await check('the engine hears: capturing with the newest words in panel and overlay', value =>
        service(value).listening && overlay(value)?.texts.includes('capturing') && noko(value).texts.includes('capturing')
        && noko(value).items.nokoLiveText && noko(value).userTexts.includes('take a note about the') && overlay(value).userTexts.includes('take a note about the'));
    await screenshot('noko-capture-busy-chip');
    key('space');
    await check('keyboard stop ends capture and hands the keyboard back', value =>
        !capturing(value) && service(value).triggers === 2 && quietChip(value) && overlay(value) === null && !value.popup.open && value.popup.lastCloseReason === 'capture-stopped');

    state = await open('noko', value => capture(value)?.rect);
    await check('reopening focuses capture', value => capture(value)?.focused);
    key('Tab');
    await check('clearing history is keyboard reachable', value => control(value, 'clear history')?.focused);
    key('Tab');
    await check('history copy is keyboard reachable', value => copy(value, 0)?.focused);
    key('space');
    await check('copy displays completion', value => copy(value, 0)?.text === 'copied' && copy(value, 0).checked);
    await check('copy sends transcript to the service', value => service(value).copiedTranscript === 'synthetic capture 0');
    for (let i = 1; i <= 8; i++) {
        key('Tab');
        await check(`history keyboard focus moves on and its whole row stays visible ${i}`, value => {
            const focused = copy(value, i), row = noko(value).items[`transcript${i}`], visible = viewport(value)?.rect;
            return focused?.focused && row && visible && row.rect.y >= visible.y && row.rect.y + row.rect.height <= visible.y + visible.height;
        });
    }
    const scroll = noko(snapshot()).retained.scrollY;
    assert(scroll > 0, 'history actually crosses its viewport boundary');
    await screenshot('noko-history');
    key('Escape');
    await check('Escape closes noko', value => !value.popup.open);

    // The focus overlay on the fixture's only output, below the horizontal bar.
    // Words show while capturing and transcribing, the typed text once finished;
    // a dictation without text says why and shows no check: never finished.
    for (const [setup, phase, label, shot, shown] of [
        [['beginStarting'], 'starting', 'starting', 'starting', ''],
        [['beginListening'], 'capturing', 'capturing', 'capturing', longWords],
        [['beginTranscribing'], 'transcribing', 'transcribing', 'transcribing', longWords],
        [['completeTranscription'], 'finished', 'finished', 'finished', 'Typed into the window you were in.'],
        [['failTranscription'], 'failed', 'no speech heard', 'no-speech', ''],
    ]) {
        for (const method of setup) call('noko', method);
        if (phase === 'capturing') call('noko', 'hear', longWords);
        let previous = '';
        state = await check(`focus overlay renders ${phase}`, value => {
            if (service(value).overlayState !== phase || !tab(value) || !overlay(value).texts.includes(label)) return false;
            if (shown ? words(value)?.text !== shown : words(value)) return false;
            const geometry = JSON.stringify([tab(value).rect, words(value)?.rect]);
            const settled = geometry === previous;
            previous = geometry;
            return settled;
        });
        assert.deepEqual(overlay(state).surface, { x: 0, y: 0, width: state.width, height: state.height }, 'focus overlay covers the output');
        assert(within(tab(state).rect, state.width, state.height));
        assert.equal(tab(state).rect.y, state.barThickness + 18);
        assert.equal(tab(state).rect.x, Math.round((state.width - tab(state).rect.width) / 2));
        if (shown) {
            const line = words(state).rect;
            assert(line.x >= tab(state).rect.x && line.x + line.width <= tab(state).rect.x + tab(state).rect.width && line.y + line.height <= tab(state).rect.y + tab(state).rect.height, `${phase} words sit inside the tab`);
            assert(overlay(state).userTexts.includes(shown), 'spoken words are the speaker\'s text');
            if (shown === longWords)
                assert.equal(line.width, Math.min(640, state.width - 120), 'long words are bounded and elide');
        }
        if (phase === 'finished' || phase === 'failed') {
            assert(!overlay(state).items.nokoWaveBar && overlay(state).texts.includes(phase === 'finished' ? '✓' : '!') && !overlay(state).texts.includes(phase === 'finished' ? '!' : '✓'), `${phase} shows its mark instead of the wave`);
        } else {
            // The fixture starts in low-power mode: a still wave; motion animates it.
            assert.equal(overlay(state).items.nokoWaveBar.rect.height, 7, `${phase} wave holds still in low-power mode`);
            fixture('motion-on');
            await check(`${phase} wave animates with motion`, value => overlay(value)?.items.nokoWaveBar && overlay(value).items.nokoWaveBar.rect.height !== 7);
            fixture('motion-off');
            await check(`${phase} wave stills again in low-power mode`, value => overlay(value)?.items.nokoWaveBar?.rect.height === 7);
        }
        await screenshot(`noko-${shot}`);
    }
    // The panel keeps the reason until the next capture; a transcript that could
    // not be typed says so beside it.
    call('noko', 'failTyping');
    state = await open('noko', value => capture(value)?.rect);
    assert(['no speech heard · try again', 'typing failed · copy it below'].every(text => noko(state).texts.includes(text)), 'the panel names what went wrong and the next step');
    assert(capture(state).enabled && quietChip(state), 'a dictation without text blocks nothing');
    await screenshot('noko-no-text');
    click(capture(state).rect);
    await check('the next capture clears both problems', value => capturing(value) && !noko(value).texts.some(text => text.includes('try again') || text.includes('typing')));
    click(capture(snapshot()).rect);
    await check('pointer stop closes noko too', value => !capturing(value) && !value.popup.open);
    call('noko', 'finish');
    await check('idle removes the focus overlay', value => overlay(value) === null && service(value).overlayState === '' && quietChip(value));

    // Config: settled controls only, so a click lands where it is drawn.
    async function activate(name) {
        let previous = '';
        const state = await check('settled Noko Config ' + name, value => {
            const target = configControl(value, name);
            if (!value.popup.windowActive || !target?.enabled) return false;
            const geometry = JSON.stringify([value.popup.card, target.rect]);
            const settled = geometry === previous;
            previous = geometry;
            return settled;
        });
        click(configControl(state, name).rect);
    }

    // The wake word: on in Config, the open microphone visible on the chip alone.
    state = await open('config', value => value.features.config.controls.noko?.rect);
    await activate('noko');
    state = await check('noko Config shows the engine, typing and preferences', value => configControl(value, 'on wake word')
        && ['speech engine · ready', 'typing into apps · ready', 'wake word', 'wake phrase', 'hotwords', 'one term per line'].every(text => config(value).texts.includes(text))
        && configControl(value, 'wake phrase')?.filled && configControl(value, 'hotwords')?.filled && !configControl(value, 'install noko') && !configControl(value, 'install speech model'));
    assert(configControl(state, 'off wake word').checked && !configControl(state, 'on wake word').checked, 'the wake word is off by default');
    await screenshot('noko-config-ready');
    await activate('on wake word');
    state = await check('the wake word turns on and says the microphone is on', value => service(value).wakeEnabled && service(value).wakeListening
        && configControl(value, 'on wake word').checked && config(value).texts.includes('microphone on') && armedChip(value));
    // Hotwords: a multi-line field reached by keyboard from the toggle above,
    // Return adds a line, Tab leaves it again.
    key('Tab');
    await check('Tab reaches the wake phrase field', value => configControl(value, 'wake phrase')?.focused);
    key('Tab');
    await check('the hotwords field takes the keyboard', value => configControl(value, 'hotwords')?.focused);
    const edits = service(snapshot()).hotwordEdits;
    key('End');
    key('Return');
    input('type', 'Direct Strike');
    await check('typed hotwords reach the service', value => service(value).hotwordEdits > edits && !config(value).texts.some(text => text.includes('Direct Strike')));
    await screenshot('noko-config-hotwords');
    key('Tab');
    await check('Tab leaves the hotwords field', value => !configControl(value, 'hotwords')?.focused);
    key('Escape');
    state = await check('Config closes; the armed microphone stays on the chip', value => !value.popup.open && armedChip(value) && overlay(value) === null);
    await screenshot('noko-wake-armed-chip');
    call('noko', 'setWakeEnabled', false);
    await check('turning the wake word off quiets the chip', value => quietChip(value) && !service(value).wakeListening);

    // Missing models: strong warning chip, deep link to Config, consent names the size.
    call('noko', 'setMissingAssets', true);
    state = await check('missing models turn the chip strong in the warning role', value =>
        chip(value).label === 'noko' && chip(value).strong && chip(value).active && !chip(value).busy && chip(value).role === 'warning');
    await screenshot('noko-blocked-chip');
    const checks = service(snapshot()).checks;
    state = await open('noko', value => control(value, 'configure noko')?.rect);
    state = await check('missing setup focuses configure action', value => control(value, 'configure noko')?.focused);
    assert.equal(service(state).checks, checks + 1);
    assert.equal(capture(state).enabled, false);
    key('Return');
    state = await check('deep link names the model download in Config', value =>
        value.popup.name === 'config' && configControl(value, 'install speech model')?.enabled && config(value).texts.includes('464 mb download') && !configControl(value, 'install noko'));
    await screenshot('noko-config-missing');
    await activate('install speech model');
    await check('declaration starts installation once', value => service(value).installs === 1 && service(value).installing && !configControl(value, 'install speech model').enabled);
    call('noko', 'install');
    call('noko', 'trigger');
    assert(service(snapshot()).installs === 1 && !capturing(snapshot()), 'service rejects duplicate install and capture');
    call('noko', 'setInstalling', '/tmp/noko-download-3f2a9c1b7e4d');
    await check('download sanitizes machine path', value => config(value).texts.includes('file') && config(value).texts.includes('40%') && !config(value).texts.some(text => text.includes('/tmp')));
    await screenshot('noko-download-label');
    await activate('appearance');
    key('Escape');
    await check('navigation and dismissal do not reset installation', value => !value.popup.open && service(value).installing && service(value).installs === 1);
    state = await open('noko', value => control(value, 'configure noko')?.rect);
    assert(!capture(state).enabled && service(state).installing);
    click(control(state, 'configure noko').rect);
    await check('deep link retains installation', value => value.popup.name === 'config' && configControl(value, 'install speech model') && service(value).installing);
    call('noko', 'completeInstall');
    await check('native completion restores readiness and retires the setup action', value =>
        !service(value).installing && !configControl(value, 'install speech model') && config(value).texts.includes('speech engine · ready'));

    // A first install: nothing on disk, consent names the engine size.
    call('noko', 'setEngineMissing', true);
    await check('a first install asks for the engine with its size', value =>
        configControl(value, 'install noko')?.enabled && config(value).texts.includes('speech engine · 98 mb') && config(value).texts.includes('speech engine · missing') && !configControl(value, 'install speech model'));
    await screenshot('noko-config-first-install');
    call('noko', 'setEngineMissing', false);
    // An update missing models is held: the old engine dictates, Config offers the models.
    call('noko', 'setUpdateHeld', true);
    state = await check('a held update offers its models without blocking', value =>
        configControl(value, 'install speech model')?.enabled && config(value).texts.includes('464 mb download') && !chip(value).strong && service(value).ready);
    await screenshot('noko-config-update-held');
    call('noko', 'setUpdateHeld', false);
    await check('the held update row retires', value => !configControl(value, 'install speech model'));
    key('Escape');
    await check('Config closes after setup', value => !value.popup.open);

    // The vertical bar moves the tab without remounting it.
    call('noko', 'completeTranscription');
    state = await check('finished tab maps before reorientation', value => tab(value)?.rect.y === value.barThickness + 18);
    const tabId = tab(state).id;
    fixture('vertical');
    state = await check('focus overlay keeps clear of the vertical bar without remounting', value =>
        value.orientation === 'vertical' && tab(value)?.id === tabId && tab(value).rect.y === 18
        && tab(value).rect.x === Math.round(value.barThickness + (value.width - value.barThickness - tab(value).rect.width) / 2));
    assert(within(tab(state).rect, state.width, state.height));
    await screenshot('noko-vertical');
    call('noko', 'finish');
    await check('idle removes the vertical focus overlay', value => overlay(value) === null);
    call('noko', 'setWakeEnabled', true);
    await check('the armed microphone shows as a glyph on the vertical bar', value => armedChip(value) && chip(value).icon === '\u{F036C}');
    await screenshot('noko-wake-armed-vertical');
    call('noko', 'setWakeEnabled', false);
    await check('the vertical chip returns to rest', value => quietChip(value) && chip(value).icon === '\u{F147D}');

    state = await open('noko', value => capture(value)?.rect);
    assert.equal(noko(state).retained.scrollY, scroll, 'history restores its viewport position after close and reorientation');
    await screenshot('noko-vertical-popup');
    assert(setupHidden(state));

    // Clearing discards for good: the first press arms, leaving disarms, the
    // second press clears.
    const clear = value => control(value, 'clear history') || control(value, 'confirm clear');
    key('Tab');
    await check('clear is focused', value => clear(value)?.focused && clear(value).text === 'clear history');
    key('space');
    await check('the first press only arms', value => clear(value)?.text === 'confirm clear' && service(value).clears === 0 && service(value).historyCount === 12);
    key('Tab');
    await check('leaving disarms', value => clear(value)?.text === 'clear history' && !clear(value).focused && service(value).clears === 0);
    key('shift+Tab');
    await check('clear is focused again', value => clear(value)?.focused);
    key('space');
    await check('armed again', value => clear(value)?.text === 'confirm clear');
    await screenshot('noko-clear-armed');
    key('space');
    state = await check('the second press clears history and retires the action', value =>
        service(value).clears === 1 && service(value).historyCount === 0 && !clear(value) && noko(value).texts.includes('no captures yet') && !noko(value).texts.includes('synthetic capture 0'));
    assert(within(state.popup.card, state.width, state.height));
    await screenshot('noko-history-cleared');
    key('Escape');
    await check('Escape closes vertical noko', value => !value.popup.open);
}