Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

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

Raw
import assert from 'node:assert/strict';
import { existsSync, mkdirSync, readFileSync, symlinkSync, writeFileSync } from 'node:fs';

// Production Noko inside the sandbox: noko-engine.nu launches an installed
// engine revision (the scripted engine speaking the real protocol), finals reach
// a private wtype on stdin only, a typing failure reaches service state, and copy
// reaches the nested clipboard. The release is unreachable here: no update runs.
export default async function nokoProduction({ env, start, stop, waitFor, ipc, setFixture }) {
    assert.equal(process.env.NUGULAND_UI_SANDBOX, '1');
    const bin = '/tmp/noko-production-bin';
    const capture = '/evidence/noko-insert.json';
    const calls = '/evidence/noko-engine-calls.jsonl';
    const scenario = '/tmp/noko-production-scenario.json';
    const revision = 'f'.repeat(40);
    const data = `${env.XDG_DATA_HOME || `${env.HOME}/.local/share`}/nuguland/noko`;
    const bundle = `${data}/engine/${revision}/speak_to_me`;
    mkdirSync(bin);
    mkdirSync(`${bundle}/bin`, { recursive: true });
    mkdirSync(`${bundle}/assets`);
    writeFileSync(`${bundle}/manifest.json`, JSON.stringify({ schema: 1, name: 'speak_to_me', revision }));
    writeFileSync(`${bundle}/bin/speak_to_me`, `#!/bin/sh\nNOKO_ENGINE_BUNDLE='${bundle}' NOKO_ENGINE_REVISION='${revision}' exec node /project/features/noko/fixtures/scripted-engine.mjs "$@"\n`, { mode: 0o755 });
    symlinkSync(revision, `${data}/engine/current`);
    writeFileSync(scenario, JSON.stringify({ recognition: 'speak', finals: ['-fixture π', 'zweite Zeile β'] }));
    writeFileSync(`${bin}/wtype`, `#!/usr/bin/env node
let text = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', chunk => { text += chunk; });
process.stdin.on('end', () => {
    require('node:fs').appendFileSync('${capture}', JSON.stringify({args: process.argv.slice(2), text}) + '\\n');
    process.stderr.write('fixture insertion error\\n');
});
`, { mode: 0o700 });
    const fixture = '/project/noko-production-probe.qml';
    writeFileSync(fixture, readFileSync('/project/features/noko/fixtures/production-probe.qml', 'utf8').replaceAll('import "../../../features/', 'import "./features/'));
    const child = start('qs', ['--no-color', '-p', fixture], 'noko-production-probe', {
        ...env, PATH: `${bin}:${env.PATH}`, NOKO_CALLS: calls, NOKO_SCENARIO: scenario, NOKO_RELEASE_URL: 'http://127.0.0.1:9/releases/speak_to_me',
    });
    setFixture(fixture, '/evidence/noko-production-probe.log', child);
    try {
        await waitFor('production noko probe loaded', () => readFileSync('/evidence/noko-production-probe.log', 'utf8').includes('Configuration Loaded'));
        const state = () => JSON.parse(ipc('state'));
        const progress = () => JSON.parse(ipc('progress'));
        await waitFor('the installed engine readies production noko', () => state().ready && state().status === 'ready');
        const records = () => readFileSync(capture, 'utf8').trim().split('\n').map(line => JSON.parse(line));
        for (const round of [1, 2]) {
            ipc('trigger');
            await waitFor(`dictation ${round} captures through the engine`, () => progress().phase === 'capturing' && progress().finals >= 1);
            ipc('trigger');
            await waitFor(`dictation ${round} reaches private wtype stdin`, () => existsSync(capture) && records().length === round);
        }
        assert.deepEqual(records(), [1, 2].map(() => ({ args: ['-'], text: '-fixture π zweite Zeile β' })));
        assert(!ipc('state').includes('fixture'), 'state never carries the transcript');
        await waitFor('insertion stderr reaches service state', () => ipc('insertError') === 'fixture insertion error');
        const engine = readFileSync(calls, 'utf8').trim().split('\n').map(line => JSON.parse(line));
        assert.equal(engine.filter(row => row.method === 'serve').length, 1, 'one engine serves consecutive dictations');
        assert.equal(engine.filter(row => row.method === 'recognition.start').length, 2);
        ipc('copy');
        await waitFor('service copy reaches nested clipboard', () => {
            if (JSON.parse(ipc('copied')).matches) return true;
            ipc('read');
            return false;
        });
    } finally {
        await stop(child);
    }
}