import assert from 'node:assert/strict'; import { once } from 'node:events'; import { createHash } from 'node:crypto'; import { createServer } from 'node:http'; import { copyFileSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, readlinkSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'; import { join } from 'node:path'; import { tmpdir } from 'node:os'; import { fileURLToPath } from 'node:url'; import { offscreenDiagnostic } from '../../test-fixtures/offscreen.mjs'; import isolation from '../../test-fixtures/isolation.js'; const { runIsolated, spawnIsolated } = isolation; // The production Noko service, offscreen, against a scripted engine speaking the // real JSON-RPC protocol (fixtures/scripted-engine.mjs), noko-engine.nu, a local // release server and a stand-in typing tool. Live partials, finals typed once, // named failures, a dying engine, the capture limit, a wake word hit ending // after silence, hotwords on every start, and an update that switches only // while idle and only once its models are installed. Load evidence, not pixels // or real audio. const engineScript = fileURLToPath(new URL('fixtures/scripted-engine.mjs', import.meta.url)); const path = process.env.PATH.split(':').filter(entry => !entry.includes('/mise/shims')).join(':'); const revision = letter => letter.repeat(40); const [a, b] = [revision('a'), revision('b')]; // One engine bundle as the release ships it, its executable the scripted engine. function bundle(at, rev, { missing = false } = {}) { const root = join(at, 'speak_to_me'); mkdirSync(join(root, 'bin'), { recursive: true }); mkdirSync(join(root, 'assets'), { recursive: true }); writeFileSync(join(root, 'assets', 'pipeline.json'), '{}'); writeFileSync(join(root, 'manifest.json'), JSON.stringify({ schema: 1, name: 'speak_to_me', revision: rev })); writeFileSync(join(root, 'bin', 'speak_to_me'), `#!/bin/sh\nNOKO_ENGINE_BUNDLE='${root}' NOKO_ENGINE_REVISION='${rev}' exec '${process.execPath}' '${engineScript}' "$@"\n`, { mode: 0o755 }); if (missing) writeFileSync(join(root, 'missing'), ''); } const directory = mkdtempSync(join(tmpdir(), 'nuguland-noko-service-')); const files = new Map(); let manifestRequests = 0; const server = createServer((request, response) => { if (request.url.endsWith('/latest/manifest.json')) manifestRequests++; // The startup check finds the installed engine current; later checks find B. const body = request.url.endsWith('/latest/manifest.json') ? files.get(manifestRequests === 1 ? 'manifest-a' : 'manifest-b') : files.get(request.url); response.writeHead(body === undefined ? 404 : 200); response.end(body ?? ''); }); await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); const release = `http://127.0.0.1:${server.address().port}/releases/speak_to_me`; try { for (const file of ['Noko.qml', 'noko-engine.nu']) copyFileSync(new URL(file, import.meta.url), join(directory, file)); const engine = join(directory, 'data', 'nuguland', 'noko', 'engine'); bundle(join(engine, a), a); symlinkSync(a, join(engine, 'current')); const stage = join(directory, 'stage'); bundle(join(stage, 'b'), b, { missing: true }); const archive = join(directory, 'b.tar.zst'); const tar = runIsolated('tar', ['--zstd', '--create', '--file', archive, '--directory', join(stage, 'b'), 'speak_to_me'], { encoding: 'utf8' }); assert.equal(tar.status, 0, tar.stderr); const bytes = readFileSync(archive); const manifest = (rev, size, sha256) => JSON.stringify({ schema: 1, name: 'speak_to_me', revision: rev, built_at: '2026-11-20T10:00:00Z', target: 'x86_64-unknown-linux-gnu', bundle: { file: 'speak_to_me-x86_64-linux.tar.zst', url: `${release}/${rev}/speak_to_me-x86_64-linux.tar.zst`, size, sha256 } }); files.set('manifest-a', manifest(a, 1, '0'.repeat(64))); files.set('manifest-b', manifest(b, bytes.length, createHash('sha256').update(bytes).digest('hex'))); files.set(`/releases/speak_to_me/${b}/speak_to_me-x86_64-linux.tar.zst`, bytes); const calls = join(directory, 'calls'); const typed = join(directory, 'typed'); const scenario = join(directory, 'scenario.json'); writeFileSync(calls, ''); const bin = join(directory, 'bin'); mkdirSync(bin); writeFileSync(join(bin, 'wtype'), `#!${process.execPath} let text = ''; process.stdin.on('data', chunk => { text += chunk; }); process.stdin.on('end', () => require('node:fs').appendFileSync(${JSON.stringify(typed)}, JSON.stringify({ args: process.argv.slice(2), text }) + '\\n')); `, { mode: 0o700 }); const shell = body => writeFileSync(join(directory, 'shell.qml'), `import QtQuick import Quickshell import Quickshell.Io ShellRoot { id: test property int phase: 0 property double mark: 0 property bool seenStarting: false property bool seenPending: false Noko { id: service } FileView { id: scenarioFile; path: Quickshell.env("NOKO_SCENARIO"); blockWrites: true; atomicWrites: true; printErrors: false } function hear(value) { scenarioFile.setText(JSON.stringify(value)); } function require(ok, message) { if (!ok) { console.error("ERROR phase", test.phase, message, service.stateJson()); Qt.quit(); } } function step() { const s = service; ${body} } Timer { interval: 20; running: true; repeat: true; onTriggered: test.step() } Timer { interval: 30000; running: true; onTriggered: { console.error("ERROR deadline in phase", test.phase, service.stateJson()); Qt.quit(); } } } `); shell(` if (phase === 0 && s.statusKnown && s.settingsLoaded && s.lastUpdateCheck > 0) { require(s.ready && s.status === "ready" && s.engineRevision === "${a}" && s.typingReady, "the installed engine readies the service"); require(!s.wakeEnabled && !s.wakeListening && s.wakePhrase === "hello computer", "the wake word is off by default"); s.loadHistoryText(Array.from({ length: 30 }, (_, i) => JSON.stringify({ transcript: "old " + i, duration: 1 })).join("\\n") + "\\nnot json\\n"); require(s.historyEntries.length === 20 && s.historyEntries[0].transcript === "old 29", "history loads its newest twenty"); s.editHotwords("Nuguland\\n niri \\n\\nniri"); require(s.hotwordsError === "" && JSON.stringify(s.hotwords) === '["Nuguland","niri"]', "one hotword per line, trimmed, once each"); s.editHotwords("Nuguland\\nhome/office"); require(s.hotwordsError === "line 2: drop / @ : #" && s.hotwords.length === 2, "an invalid hotword is named and never sent"); s.editHotwords("Nuguland\\nniri"); hear({ recognition: "speak" }); s.trigger(); require(s.starting && s.overlayState === "starting" && s.status === "listening" && s.busy, "capture starts at once and says it is starting"); s.install(); require(!s.installing, "capture rejects setup"); phase = 1; } else if (phase === 1) { if (s.starting) seenStarting = true; if (s.listening && s.liveText === "Hello world. and the") { require(seenStarting && s.overlayState === "capturing", "the overlay turns from starting to capturing once the engine hears"); require(s.stateJson().indexOf("Hello") < 0 && s.stateJson().indexOf("and the") < 0, "state never carries the partial"); s.stopCapture(); require(s.transcribing && s.overlayState === "transcribing", "stop turns to transcribing"); s.startCapture(); require(s.transcribing, "transcription rejects a new capture"); phase = 2; } } else if (phase === 2 && !s.dictating) { require(s.finished && !s.failed && s.captureError === "" && s.status === "ready", "the final finishes the dictation"); require(s.liveText === "Hello world. And the rest.", "the final replaces the partial"); require(s.historyEntries.length === 20 && s.historyEntries[0].transcript === "Hello world. And the rest." && s.historyEntries[19].transcript === "old 11", "history takes the transcript and stays bounded"); const state = JSON.parse(s.stateJson()); require(s.stateJson().indexOf("Hello") < 0 && s.stateJson().indexOf("Nuguland") < 0 && state.historyCount === 20 && state.hotwordCount === 2, "state never carries what was said or listened for"); hear({ recognition: "silent" }); s.trigger(); require(s.starting && !s.finished, "the next capture starts at once"); phase = 3; } else if (phase === 3 && s.listening) { s.trigger(); phase = 4; } else if (phase === 4 && !s.dictating) { require(!s.finished && s.failed && s.overlayState === "failed" && s.captureError === "no speech heard", "silence is a named failure, never finished"); require(s.status === "ready" && !s.blocked && s.historyEntries[0].transcript === "Hello world. And the rest.", "a failed dictation neither blocks nor records"); hear({ recognition: "die" }); s.trigger(); require(s.starting && s.captureError === "" && !s.failed, "the next capture clears the failure"); phase = 5; } else if (phase === 5 && !s.dictating) { require(s.failed && s.captureError === "speech engine stopped" && !s.finished, "an engine that dies is a named failure"); hear({ recognition: "speak" }); s.captureLimitMs = 400; s.trigger(); require(s.starting, "a dead engine leaves nothing stuck"); phase = 6; } else if (phase === 6 && !s.dictating) { require(s.finished && s.historyEntries[0].transcript === "Hello world. And the rest.", "a forgotten capture stops itself and is transcribed"); // The shortened limit proved the bound; later dictations get the real one. s.captureLimitMs = 300000; hear({ recognition: "wake-speech", wake: "hit" }); s.wakeSilenceMs = 300; s.editWakePhrase("hello: computer"); require(s.wakePhraseError === "drop / @ : #" && s.wakePhrase === "hello: computer", "an invalid wake phrase is named"); s.editWakePhrase(" hello computer "); s.setWakeEnabled(true); require(s.wakeWanted && s.wakeListening && s.active && s.role === "ui.accent", "arming the wake word opens the microphone visibly"); mark = s.historyEntries.length; phase = 7; } else if (phase === 7 && s.historyEntries[0].transcript === "Take a note." && !s.dictating && s.wakeListening && s.wakeOperation) { require(!s.failed, "a wake capture ends by itself after silence"); s.engineLingerMs = 100; s.setWakeEnabled(false); require(!s.wakeEnabled, "the wake word turns off"); phase = 8; } else if (phase === 8 && !s.wakeListening && s.engineMode === "" && !s.finished) { require(!s.active && s.role === "ui.focus", "off releases the microphone and the engine goes"); hear({ recognition: "speak" }); s.lastUpdateCheck = 0; s.refresh(); s.trigger(); phase = 9; } else if (phase === 9 && s.liveText.endsWith("and the") && s.pendingRevision === "${b}") { require(s.engineRunningRevision === "${a}" && s.engineRevision === "${a}", "an update waits for the dictation to end"); s.stopCapture(); phase = 10; } else if (phase === 10 && !s.dictating && s.pendingAssetsMissing && s.engineMode === "") { require(s.engineRevision === "${a}" && s.assetBytes === 463945198 && s.ready && !s.blocked, "an update missing models is held; the old engine stays"); s.trigger(); phase = 11; } else if (phase === 11 && s.liveText.endsWith("and the")) { require(s.engineRunningRevision === "${a}", "the held update leaves dictation on the old engine"); s.stopCapture(); phase = 12; } else if (phase === 12 && !s.dictating) { s.install(); require(s.installing && s.status === "installing", "consent installs the models"); s.trigger(); require(!s.dictating, "installation rejects capture"); phase = 13; } else if (phase === 13 && !s.installing) { require(s.engineRevision === "${b}" && s.ready && s.status === "ready" && s.lastError === "" && s.pendingRevision === "", "installed models switch to the update"); s.trigger(); phase = 14; } else if (phase === 14 && s.liveText.endsWith("and the")) { require(s.engineRunningRevision === "${b}", "dictation runs on the update"); s.stopCapture(); phase = 15; } else if (phase === 15 && !s.dictating) { require(s.finished, "the update dictates"); s.clearHistory(); require(s.historyEntries.length === 0, "history clears"); phase = 16; mark = Date.now(); } else if (phase === 16 && Date.now() - mark > 600) { console.log("noko service: PASS"); Qt.quit(); } `); const env = { NOKO_CALLS: calls, NOKO_SCENARIO: scenario, NOKO_RELEASE_URL: release, PATH: `${bin}:${path}` }; async function offscreen(environment) { const child = spawnIsolated('qs', ['--no-color', '-p', join(directory, 'shell.qml')], { directory, env: environment, stdio: ['ignore', 'pipe', 'pipe'] }); let output = ''; child.stdout.on('data', chunk => { output += chunk; }); child.stderr.on('data', chunk => { output += chunk; }); const timer = setTimeout(() => child.kill('SIGKILL'), 45000); const [code] = await once(child, 'close'); clearTimeout(timer); process.stdout.write(output); assert.equal(code, 0, output); assert.doesNotMatch(output, offscreenDiagnostic, output); assert.match(output, /noko service: PASS/, output); } await offscreen(env); const records = readFileSync(calls, 'utf8').trim().split('\n').map(line => JSON.parse(line)); assert.deepEqual(records.filter(row => row.method === 'busy'), [], 'one engine operation at a time, never refused Busy'); const starts = records.filter(row => row.method === 'recognition.start'); assert(starts.length >= 9, 'every dictation and the wake priming started recognition'); assert(starts.every(row => JSON.stringify(row.hotwords) === '["Nuguland","niri"]' && row.punctuation === false), 'hotwords on every start; finals typed as recognised'); assert.deepEqual(records.filter(row => row.method === 'recognition.ack').map(row => row.segment).slice(0, 2), ['segment-1', 'segment-2'], 'every final is acknowledged'); const wakes = records.filter(row => row.method === 'wake.start'); assert(wakes.length >= 2 && wakes.every(row => JSON.stringify(row.phrases) === '["hello computer"]'), 'the wake word re-arms with the trimmed phrase'); assert(records.some(row => row.method === 'recognition.finish' && row.discard && row.plan === 'wake-speech'), 'arming primes the recogniser once, discarded'); assert(records.some(row => row.method === 'operation.cancel' && row.kind === 'wake'), 'turning the wake word off cancels detection'); // The update: never served while a dictation ran on the old engine. const firstB = records.findIndex(row => row.method === 'serve' && row.revision === b); const duringDownload = records.findLastIndex((row, index) => index < firstB && row.method === 'recognition.finish' && row.revision === a); assert(firstB > duringDownload && duringDownload > 0, 'the update starts only after the running dictation finished'); assert(records.some(row => row.method === 'pipeline.plan' && row.revision === b && row.missing === 1), 'the update reported missing models'); assert.equal(records.filter(row => row.method === 'pipeline.prepare' && row.revision === b).length, 1, 'models install once, with consent'); assert.equal(records.filter(row => row.method === 'exit').length, records.filter(row => row.method === 'serve').length - records.filter(row => row.method === 'recognition.start' && row.plan === 'die').length, 'every engine that did not die ended once idle'); const typing = readFileSync(typed, 'utf8').trim().split('\n').map(line => JSON.parse(line)); assert(typing.every(row => JSON.stringify(row.args) === '["-"]'), 'text reaches wtype on stdin only'); assert.deepEqual(typing.map(row => row.text), ['Hello world. And the rest.', 'Hello world. And the rest.', 'Take a note.', 'Hello world. And the rest.', 'Hello world. And the rest.', 'Hello world. And the rest.'], 'only finals are typed, once each'); const noko = join(directory, 'data', 'nuguland', 'noko'); assert.equal(readlinkSync(join(noko, 'engine', 'current')), b); assert.deepEqual(readdirSync(join(noko, 'engine')).sort(), [b, 'current'], 'the old engine goes once the update has run'); assert(lstatSync(join(noko, 'models')).isDirectory()); const state = readdirSync(directory, { recursive: true }).map(String); const history = state.filter(file => file.endsWith('noko-history.jsonl')); assert.equal(history.length, 1); assert.equal(readFileSync(join(directory, history[0]), 'utf8'), '', 'clearing empties the stored history'); const settings = state.filter(file => file.endsWith('noko-settings.json')); assert.deepEqual(JSON.parse(readFileSync(join(directory, settings[0]), 'utf8')), { hotwords: ['Nuguland', 'niri'], wakeEnabled: false, wakePhrase: 'hello computer' }, 'settings persist in the service state file'); // Nothing installed and no release reachable: blocked, and capture asks nothing of the engine. rmSync(join(directory, 'data'), { recursive: true, force: true }); files.clear(); writeFileSync(calls, ''); shell(` if (s.statusKnown && phase === 0) { require(!s.ready && s.status === "blocked" && s.blocked && s.strong && s.role === "warning", "no engine blocks with a warning"); s.trigger(); require(!s.dictating, "capture needs an engine"); phase = 1; mark = Date.now(); } else if (phase === 1 && Date.now() - mark > 300) { require(s.latestRevision === "" && s.engineMode === "", "an unreachable release starts nothing"); console.log("noko service: PASS"); Qt.quit(); } `); await offscreen(env); assert.equal(readFileSync(calls, 'utf8'), '', 'no engine ran'); } finally { server.closeAllConnections(); await new Promise(resolve => server.close(resolve)); rmSync(directory, { recursive: true, force: true }); }