66 lines
2.8 KiB
JavaScript
66 lines
2.8 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { VoiceStateMachine } from '../daemon/voice-state.js';
|
|
import { QvacScheduler } from '../daemon/qvac-scheduler.js';
|
|
import { JarvisDaemon } from '../daemon/index.js';
|
|
import { spokenReply } from '../skills/voice-prompt.js';
|
|
|
|
test('voice state machine handles wake, reply, cancel, and idle sleep', () => {
|
|
let now = 0;
|
|
const voice = new VoiceStateMachine({ now: () => now, idleMs: 100 });
|
|
voice.wake(); voice.utterance(); voice.speak(); voice.finishSpeaking();
|
|
assert.equal(voice.state, 'LISTENING');
|
|
assert.equal(voice.command('hands off'), 'cancel');
|
|
assert.equal(voice.state, 'ARMED');
|
|
voice.wake(); now = 101; voice.expireIdle();
|
|
assert.equal(voice.state, 'SLEEPING');
|
|
});
|
|
|
|
test('typed questions wake an armed voice session before thinking', () => {
|
|
const voice = new VoiceStateMachine();
|
|
voice.typedUtterance();
|
|
assert.equal(voice.state, 'THINKING');
|
|
});
|
|
|
|
test('typed questions barge in from SPEAKING', () => {
|
|
const voice = new VoiceStateMachine();
|
|
voice.wake(); voice.utterance(); voice.speak();
|
|
voice.typedUtterance();
|
|
assert.equal(voice.state, 'THINKING');
|
|
});
|
|
|
|
test('spoken replies use harness text and strip HUD sidecars', () => {
|
|
assert.equal(spokenReply({ ok: true, text: 'Hello there.', reason: 'stop' }), 'Hello there.');
|
|
assert.equal(spokenReply({ text: 'Spoken. <jarvis_hud>{"title":"x","chips":[]}</jarvis_hud>' }), 'Spoken.');
|
|
assert.equal(spokenReply({}), '');
|
|
assert.equal(spokenReply('[object Object]'), '');
|
|
});
|
|
|
|
test('ask extracts harness reply text instead of stringifying the object', async () => {
|
|
const daemon = new JarvisDaemon();
|
|
daemon.harness = { ask: async () => ({ ok: true, text: 'Hello there.', reason: 'stop' }), cancel() {}, close: async () => {} };
|
|
daemon.voiceLoop = null;
|
|
const replies = [];
|
|
daemon.on('Reply', (text) => replies.push(text));
|
|
try {
|
|
const result = await daemon.ask('Hi');
|
|
assert.equal(result, 'Hello there.');
|
|
assert.deepEqual(replies, ['Hello there.']);
|
|
assert.equal(daemon.lastReply, 'Hello there.');
|
|
assert.equal(daemon.state, 'LISTENING');
|
|
} finally {
|
|
await daemon.close();
|
|
}
|
|
});
|
|
|
|
test('QVAC scheduler prioritizes voice and keeps one active job', async () => {
|
|
const scheduler = new QvacScheduler();
|
|
const order = [];
|
|
const first = scheduler.run(async () => { order.push('first'); await new Promise((r) => setTimeout(r, 5)); }, { lane: 'background' });
|
|
const media = scheduler.run(async () => order.push('media'), { lane: 'background' });
|
|
const voice = scheduler.run(async () => order.push('voice'), { lane: 'voice' });
|
|
await Promise.all([first, media, voice]);
|
|
assert.deepEqual(order, ['first', 'voice', 'media']);
|
|
assert.deepEqual(scheduler.status(), { running: 0, queued: [] });
|
|
});
|