Files
gnome-jarvis/test/voice-phase4.test.js
T
snxraven a78708f8ff
Rolling release / release (push) Failing after 1m24s
Updates
2026-09-11 19:36:12 -04:00

114 lines
6.2 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { EventEmitter } from 'node:events';
import { VoiceLoop, fastCommand } from '../daemon/voice-loop.js';
import { pcmS16le } from '../daemon/voice-adapters.js';
import { WakeEngine } from '../daemon/wake-engine.js';
import { VadSegmenter } from '../daemon/vad.js';
import { PipeWireCapture, pcmRms } from '../daemon/audio-pipewire.js';
import { SentenceBuffer, isMeaningfulTranscript } from '../daemon/transcript.js';
test('Phase 4 transcript filtering and sentence buffering are deterministic', () => {
assert.equal(isMeaningfulTranscript('[BLANK_AUDIO]'), false);
assert.equal(isMeaningfulTranscript('hi'), false);
assert.equal(isMeaningfulTranscript('what time is it'), true);
const out = []; const buffer = new SentenceBuffer({ onSentence: (s) => out.push(s) });
buffer.push('First sentence. Second'); buffer.push(' sentence!'); buffer.flush();
assert.deepEqual(out, ['First sentence.', 'Second sentence!']);
});
test('Phase 4 wake engine emits only configured local detections', () => {
const wake = new WakeEngine({ phrases: ['hey jarvis'], detect: () => ({ phrase: 'hey jarvis' }) });
const heard = []; wake.on('wake', (phrase) => heard.push(phrase)); wake.push(Buffer.from([0]));
assert.deepEqual(heard, ['hey jarvis']);
wake.pause(); wake.push(Buffer.from([0])); assert.equal(heard.length, 1);
});
test('Phase 4 VAD emits a bounded utterance after silence', () => {
const vad = new VadSegmenter({ frameMs: 100, params: { threshold: 0.6, minSpeechDurationMs: 100, minSilenceDurationMs: 200 } });
const utterances = []; vad.on('utterance', (audio) => utterances.push(audio));
const loud = Buffer.alloc(3200); for (let i = 0; i < loud.length; i += 2) loud.writeInt16LE(20_000, i); const quiet = Buffer.alloc(3200);
vad.push(loud); vad.push(quiet); vad.push(quiet);
assert.equal(utterances.length, 1); assert.ok(utterances[0].length > 0); assert.ok(pcmRms(loud) > 0);
});
test('Phase 4 fast commands are handled before the harness', () => {
assert.equal(fastCommand('Hands Off'), 'cancel');
assert.equal(fastCommand('take the wheel'), 'computer');
assert.equal(fastCommand('ordinary question'), null);
});
test('Phase 4 feedback gate drops capture while speaking and during cooldown', () => {
const capture = new EventEmitter(); capture.start = () => {}; capture.stop = () => {};
const wake = new WakeEngine({ detect: () => ({ phrase: 'hey jarvis' }) });
const vad = new VadSegmenter({ frameMs: 20 }); let pushed = 0; const original = vad.push.bind(vad); vad.push = (x) => { pushed++; original(x); };
const daemon = new EventEmitter(); daemon.state = 'LISTENING';
const loop = new VoiceLoop({ daemon, capture, wake, vad, now: () => 1000 }); loop.running = true;
loop.isSpeaking = true; loop.pushAudio(Buffer.alloc(3200)); assert.equal(pushed, 0);
loop.isSpeaking = false; loop.cooldownUntil = 1200; loop.pushAudio(Buffer.alloc(3200)); assert.equal(pushed, 0);
});
test('Phase 4 PipeWire capture uses a named 16 kHz mono node', () => {
let args; const child = new EventEmitter(); child.stdout = new EventEmitter(); child.stderr = new EventEmitter(); child.kill = () => {};
const capture = new PipeWireCapture({ spawnImpl: (_cmd, received) => { args = received; return child; } }); capture.start();
assert.deepEqual(args, ['--record', '--raw', '--format', 's16', '--rate', '16000', '--channels', '1', '--properties', 'node.name=Jarvis', '-']);
capture.stop();
});
test('empty TTS still leaves the daemon listening', async () => {
const daemon = new EventEmitter();
daemon.state = 'SPEAKING';
daemon.voice = { finishSpeaking() { daemon.finished = true; } };
daemon.setState = (state) => { daemon.state = state; };
const capture = new EventEmitter(); capture.start = () => {}; capture.stop = () => {};
const wake = new WakeEngine({ detect: () => null });
const loop = new VoiceLoop({ daemon, capture, wake, vad: new VadSegmenter(), tts: { speak: async () => ({ samples: new Int16Array(0) }) } });
await loop.speak('[object Object]');
assert.equal(daemon.state, 'LISTENING');
assert.equal(daemon.finished, true);
});
test('PCM packing uses even s16le sample pairs', () => {
const buf = Buffer.alloc(4);
buf.writeInt16LE(256, 0);
buf.writeInt16LE(-2, 2);
const packed = pcmS16le(buf);
assert.equal(packed.samples.length, 2);
assert.equal(packed.samples[0], 256);
assert.equal(packed.samples[1], -2);
const fromBytes = pcmS16le(Uint8Array.from([0, 1, 0xfe, 0xff]));
assert.equal(fromBytes.samples.length, 2);
assert.equal(fromBytes.samples[0], 256);
});
test('ASR failure preserves speech output and reports unavailable microphone', async () => {
const capture = new EventEmitter(); let captured = false;
capture.start = () => { captured = true; }; capture.stop = () => {};
let spoken = '';
const loop = new VoiceLoop({ capture, wake: new WakeEngine(),
asr: { start: async () => { throw new Error('Whisper plugin missing'); } },
tts: { start: async () => {}, speak: async (text) => { spoken = text; return { samples: new Int16Array(8) }; } },
playback: { play: async () => {}, stop() {} },
});
loop.on('error', () => {});
await loop.start(); await loop.speak('Speech still works.');
assert.equal(spoken, 'Speech still works.'); assert.equal(captured, false);
assert.equal(loop.status.tts, true); assert.equal(loop.status.asr, false);
assert.match(loop.status.errors.asr, /Whisper/); await loop.stop();
});
test('TTS failure preserves ASR and push to talk without wake command', async () => {
const capture = new EventEmitter(); capture.start = () => {}; capture.stop = () => {};
const loop = new VoiceLoop({ capture, wake: new WakeEngine(),
asr: { start: async () => {} }, tts: { start: async () => { throw new Error('TTS failed'); } },
playback: { stop() {} },
});
loop.on('error', () => {}); await loop.start(); loop.setPushToTalk(true);
assert.equal(loop.status.asr, true); assert.equal(loop.ptt, true);
assert.equal(loop.status.tts, false); assert.equal(loop.status.wake, false); await loop.stop();
});
test('QVAC numeric PCM arrays preserve signed 16-bit samples', () => {
assert.deepEqual([...pcmS16le([256, -32768, 32767]).samples], [256, -32768, 32767]);
});