forked from Blendman974/faceclaw
Merge branch 'main' of https://github.com/jimrandomh/faceclaw
This commit is contained in:
commit
f6a4615953
24 files changed
+518
-176
No files matched your search
@@ -690,7 +690,7 @@ test('resizing a terminal reconnects with the new grid and ignores callbacks fro
|
||||
},
|
||||
} } },
|
||||
};
|
||||
const socketContext = { exports: {}, com: context.com };
|
||||
const socketContext = { exports: {}, com: context.com, require: () => ({ toJavaBytes: bytes => bytes }) };
|
||||
vm.runInNewContext(js(read('app/native/socket.ts')), socketContext);
|
||||
context.require = name => name === './socket' ? socketContext.exports : {};
|
||||
vm.runInNewContext(js(read('app/native/g2mirror-client.ts')), context);
|
||||
|
||||
@@ -193,3 +193,28 @@ test('an unsolicited Soniox session end finalizes visible text and reconnects',
|
||||
assert.equal(h.sockets.length, 2);
|
||||
h.client.stop();
|
||||
});
|
||||
|
||||
test('provider selection falls back to on-device without a key and connects the chosen provider with one', () => {
|
||||
const sockets = [];
|
||||
const load = loader({
|
||||
setTimeout() {}, clearTimeout() {}, console: { log() {}, warn() {} },
|
||||
com: { faceclaw: { app: {
|
||||
FaceclawWebSocketListener: function(callbacks) { return callbacks; },
|
||||
FaceclawWebSocket: class { constructor(url, _listener, header, value) { sockets.push({ url, header, value }); } },
|
||||
} } },
|
||||
}, { './cloud-stt': { CLOUD_STT_SAMPLE_RATE: 16000, encodeBase64: () => '', toJavaBytes: bytes => bytes } });
|
||||
const { createCloudSttClient, usesCloudStt } = load('app/native/cloud-stt-provider.ts');
|
||||
const statuses = [];
|
||||
const options = { onTranscript() {}, onStatus: status => statuses.push(status), onError() {} };
|
||||
const selection = { provider: 'onboard', elevenLabsApiKey: '', openAiApiKey: ' ', sonioxApiKey: 'soniox-key' };
|
||||
assert.equal(usesCloudStt(selection), false);
|
||||
assert.equal(createCloudSttClient(selection, options, () => false), null);
|
||||
assert.equal(usesCloudStt({ ...selection, provider: 'whisper' }), false);
|
||||
assert.equal(createCloudSttClient({ ...selection, provider: 'whisper' }, options, () => false), null);
|
||||
assert.deepEqual(statuses, ['No OpenAI key set; using on-device voice.']);
|
||||
assert.equal(usesCloudStt({ ...selection, provider: 'soniox' }), true);
|
||||
createCloudSttClient({ ...selection, provider: 'soniox' }, options, () => false).start();
|
||||
createCloudSttClient({ ...selection, provider: 'elevenlabs', elevenLabsApiKey: ' xi ' }, options, () => false).start();
|
||||
assert.deepEqual(sockets.map(({ url, header, value }) => [new URL(url).host, header, value]),
|
||||
[['stt-rt.soniox.com', null, null], ['api.elevenlabs.io', 'xi-api-key', 'xi']]);
|
||||
});
|
||||
@@ -4,6 +4,9 @@ const fs = require('node:fs');
|
||||
const vm = require('node:vm');
|
||||
const ts = require('typescript');
|
||||
const path = require('node:path');
|
||||
// The provider settings ios-preview-controller hands the voice bridge.
|
||||
const voiceSettings = { voiceProviderSetting: { get: () => 'onboard' }, elevenLabsApiKeySetting: { get: () => '' },
|
||||
openAiApiKeySetting: { get: () => '' }, sonioxApiKeySetting: { get: () => '' } };
|
||||
function load(file, context) {
|
||||
const sandbox = { exports: {}, ...context };
|
||||
const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
|
||||
@@ -80,7 +83,8 @@ test('iOS settings prompts honor password input and report cancellation', async
|
||||
test('iOS host forwards hands-free capture mode to the voice bridge', async () => {
|
||||
const captures = [];
|
||||
const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', { require: id => id === '../native/ios-voice-input'
|
||||
? { iosVoiceInput: { startGlassesCapture: (_session, _log, endpointing) => captures.push(endpointing) } } : {} });
|
||||
? { iosVoiceInput: { startGlassesCapture: (_session, _voice, _log, endpointing) => captures.push(endpointing) } }
|
||||
: id === '../ui/dashboard-settings' ? voiceSettings : {} });
|
||||
const host = Object.create(IosPreviewController.prototype);
|
||||
host.communicator = {}; host.state = { phase: 'connected' };
|
||||
await host.startVoiceCapture(true);
|
||||
@@ -304,20 +308,20 @@ test('iOS opens the on-glasses onboarding on a new acknowledged frame, once per
|
||||
|
||||
test('iOS preview voice uses the phone microphone, while connected capture stays on glasses', async () => {
|
||||
const calls = [];
|
||||
const bridge = { prepare: async (foreground, phone) => { calls.push(['prepare', foreground, phone]); return true; },
|
||||
startPhoneCapture: async (_log, endpointing) => calls.push(['phone', endpointing]),
|
||||
startGlassesCapture: async (_session, _log, endpointing) => calls.push(['glasses', endpointing]) };
|
||||
const bridge = { prepare: async (foreground, phone, voice) => { calls.push(['prepare', foreground, phone, voice.provider]); return true; },
|
||||
startPhoneCapture: async (voice, _log, endpointing) => calls.push(['phone', voice.provider, endpointing]),
|
||||
startGlassesCapture: async (_session, voice, _log, endpointing) => calls.push(['glasses', voice.provider, endpointing]) };
|
||||
const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', {
|
||||
require: id => id === '../native/ios-voice-input' ? { iosVoiceInput: bridge } : {},
|
||||
require: id => id === '../native/ios-voice-input' ? { iosVoiceInput: bridge } : id === '../ui/dashboard-settings' ? voiceSettings : {},
|
||||
});
|
||||
const host = Object.create(IosPreviewController.prototype);
|
||||
host.active = true; host.glassesLocked = false; host.communicator = null; host.state = { phase: 'disconnected' };
|
||||
assert.equal(await host.prepareVoiceCapture(), true);
|
||||
await host.startVoiceCapture(true);
|
||||
assert.deepEqual(calls, [['prepare', true, true], ['prepare', true, true], ['phone', true]]);
|
||||
assert.deepEqual(calls, [['prepare', true, true, 'onboard'], ['prepare', true, true, 'onboard'], ['phone', 'onboard', true]]);
|
||||
calls.length = 0; host.communicator = {}; host.state = { phase: 'connected' };
|
||||
assert.equal(await host.prepareVoiceCapture(), true); await host.startVoiceCapture(false);
|
||||
assert.deepEqual(calls, [['prepare', true, false], ['glasses', false]]);
|
||||
assert.deepEqual(calls, [['prepare', true, false, 'onboard'], ['glasses', 'onboard', false]]);
|
||||
calls.length = 0; host.active = false; host.communicator = null; host.state = { phase: 'disconnected' };
|
||||
assert.equal(await host.prepareVoiceCapture(), false); await host.startVoiceCapture();
|
||||
assert.deepEqual(calls, []);
|
||||
|
||||
@@ -7,8 +7,13 @@ const { join, resolve } = require('node:path');
|
||||
const root = resolve(__dirname, '..'), dir = mkdtempSync(join(tmpdir(), 'faceclaw-socket-'));
|
||||
execFileSync('xcrun', ['clang', '-fobjc-arc', '-framework', 'Foundation', '-I', 'App_Resources/iOS/src',
|
||||
'tests/native/ios-socket-test.m', 'App_Resources/iOS/src/FaceclawSocket.m', '-o', join(dir, 'socket-test')], { cwd: root, stdio: 'inherit' });
|
||||
const server = new WebSocketServer({ host: '127.0.0.1', port: 0 });
|
||||
server.on('connection', socket => socket.on('message', data => socket.send(data.toString(), () => socket.close(1000, 'done'))));
|
||||
// Requires the handshake header; answers binary frames with their hex, echoes text and closes.
|
||||
const server = new WebSocketServer({ host: '127.0.0.1', port: 0,
|
||||
verifyClient: (info, done) => done(info.req.headers['x-faceclaw-key'] === 'secret', 401, 'Unauthorized') });
|
||||
server.on('connection', socket => socket.on('message', data => {
|
||||
if (Buffer.isBuffer(data)) socket.send(`binary:${data.toString('hex')}`);
|
||||
else socket.send(data, () => socket.close(1000, 'done'));
|
||||
}));
|
||||
server.on('listening', () => {
|
||||
const child = spawn(join(dir, 'socket-test'), [`ws://127.0.0.1:${server.address().port}`], { stdio: 'inherit' });
|
||||
child.on('exit', code => { server.close(); rmSync(dir, { recursive: true, force: true }); process.exitCode = code ?? 1; });
|
||||
|
||||
+97
-20
@@ -4,15 +4,29 @@ const fs = require('node:fs');
|
||||
const vm = require('node:vm');
|
||||
const ts = require('typescript');
|
||||
function harness() {
|
||||
const native = { startWithEndpointing(endpointing) { this.endpointing = endpointing; return ''; }, startPhoneWithEndpointing(endpointing) { this.endpointing = endpointing; this.phone = true; return this.error ?? ''; }, cancel() { this.cancels++; }, cancels: 0, finish() { this.finished = true; }, acceptPacket: p => audio.push(p) };
|
||||
const audio = [], timers = new Map(), commands = [], text = [];
|
||||
const native = { startWithEndpointing(endpointing) { this.endpointing = endpointing; return ''; }, startPhoneWithEndpointing(endpointing) { this.endpointing = endpointing; this.phone = true; return this.error ?? ''; },
|
||||
startPcmCaptureWithEndpointingPhone(endpointing, phone) { this.pcmCapture = { endpointing, phone }; return this.error ?? ''; },
|
||||
cancel() { this.cancels++; }, cancels: 0, finish() { this.finished = true; }, acceptPacket: p => audio.push(p) };
|
||||
const audio = [], timers = new Map(), commands = [], text = [], clouds = [];
|
||||
// Stands in for cloud-stt-provider: any provider but "onboard" is a keyed cloud one.
|
||||
const provider = {
|
||||
usesCloudStt: voice => voice.provider !== 'onboard',
|
||||
createCloudSttClient(voice, options) {
|
||||
if (voice.provider === 'onboard') return null;
|
||||
const client = { options, pcm: [], started: false, finished: 0, stopped: false,
|
||||
start() { this.started = true; }, acceptPcm(bytes) { this.pcm.push([...bytes]); },
|
||||
finish() { this.finished++; }, stop() { this.stopped = true; } };
|
||||
clouds.push(client); return client;
|
||||
},
|
||||
};
|
||||
let authorization = 3, microphoneAuthorization = 3, microphoneRequests = 0, permissionRequests = 0, release;
|
||||
const sandbox = { exports: {}, FaceclawSpeech: { new: () => native, authorizationStatus: () => authorization,
|
||||
requestAuthorization: done => { permissionRequests++; done(3); },
|
||||
microphoneAuthorizationStatus: () => microphoneAuthorization,
|
||||
requestMicrophoneAuthorization: done => { microphoneRequests++; done(3); } },
|
||||
setTimeout: fn => { timers.set(fn, fn); return fn; }, clearTimeout: fn => timers.delete(fn),
|
||||
NSData: { dataWithBytesLength: (b, n) => Buffer.from(b, 0, n) }, interop: { handleof: b => b } };
|
||||
NSData: { dataWithBytesLength: (b, n) => Buffer.from(b, 0, n) }, interop: { handleof: b => b, bufferFromData: d => d },
|
||||
require: name => name === './cloud-stt-provider' ? provider : {} };
|
||||
vm.runInNewContext(ts.transpileModule(fs.readFileSync('app/native/voice-control.ios.ts', 'utf8'), {
|
||||
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 } }).outputText, sandbox);
|
||||
const bridge = new sandbox.exports.IosVoiceControlBridge();
|
||||
@@ -22,20 +36,22 @@ function harness() {
|
||||
if (enabled) { this.packet = fn; return new Promise(resolve => { release = resolve; }); }
|
||||
return Promise.resolve();
|
||||
} };
|
||||
return { bridge, native, session, audio, commands, text, timers, release: () => release(),
|
||||
const onboard = { provider: 'onboard', elevenLabsApiKey: '', openAiApiKey: '', sonioxApiKey: '' };
|
||||
return { bridge, native, session, audio, commands, text, timers, clouds, release: () => release(),
|
||||
onboard, cloud: { ...onboard, provider: 'soniox', sonioxApiKey: 'key' },
|
||||
microphoneAuthorization: value => { microphoneAuthorization = value; }, microphoneRequests: () => microphoneRequests,
|
||||
authorization: value => { authorization = value; }, permissionRequests: () => permissionRequests,
|
||||
event: event => native.eventHandler(JSON.stringify(event)) };
|
||||
}
|
||||
test('iOS speech permission is requested only in foreground and denial stays actionable', async () => {
|
||||
const h = harness(); h.authorization(0);
|
||||
assert.equal(await h.bridge.prepare(false), false); assert.equal(h.permissionRequests(), 0);
|
||||
assert.equal(await h.bridge.prepare(true), true); assert.equal(h.permissionRequests(), 1);
|
||||
h.authorization(2); assert.equal(await h.bridge.prepare(true), false);
|
||||
assert.equal(await h.bridge.prepare(false, false, h.onboard), false); assert.equal(h.permissionRequests(), 0);
|
||||
assert.equal(await h.bridge.prepare(true, false, h.onboard), true); assert.equal(h.permissionRequests(), 1);
|
||||
h.authorization(2); assert.equal(await h.bridge.prepare(true, false, h.onboard), false);
|
||||
assert.match(h.bridge.statusText, /Settings/);
|
||||
});
|
||||
test('glasses speech sends PCM-source packets, replaces partials, and drains final text after manual release', async () => {
|
||||
const h = harness(); const start = h.bridge.startGlassesCapture(h.session, () => {});
|
||||
const h = harness(); const start = h.bridge.startGlassesCapture(h.session, h.onboard, () => {});
|
||||
h.session.packet(new Uint8Array([1, 2, 3])); assert.deepEqual([...h.audio[0]], [1, 2, 3]);
|
||||
h.release(); await start;
|
||||
h.event({ kind: 'transcript', text: 'hello', final: false });
|
||||
@@ -51,7 +67,7 @@ test('glasses speech sends PCM-source packets, replaces partials, and drains fin
|
||||
test('cancel and disconnect during enable cannot resurrect capture or leave its microphone listener active', async () => {
|
||||
for (const stop of ['stop', 'handleSessionEnded']) {
|
||||
const h = harness(); let ends = 0; h.bridge.onSpeechEnd(() => ends++);
|
||||
const start = h.bridge.startGlassesCapture(h.session, () => {});
|
||||
const start = h.bridge.startGlassesCapture(h.session, h.onboard, () => {});
|
||||
h.bridge[stop](); h.release(); await start;
|
||||
h.session.packet(new Uint8Array([9]));
|
||||
h.event({ kind: 'transcript', text: 'stale', final: true });
|
||||
@@ -60,7 +76,7 @@ test('cancel and disconnect during enable cannot resurrect capture or leave its
|
||||
}
|
||||
});
|
||||
test('missing microphone data ends the session and disables the mic with an actionable status', async () => {
|
||||
const h = harness(); const start = h.bridge.startGlassesCapture(h.session, () => {}); h.release(); await start;
|
||||
const h = harness(); const start = h.bridge.startGlassesCapture(h.session, h.onboard, () => {}); h.release(); await start;
|
||||
for (const fn of h.timers.values()) fn();
|
||||
assert.deepEqual(h.commands, [true, false]); assert.match(h.bridge.statusText, /No microphone audio/);
|
||||
});
|
||||
@@ -68,7 +84,7 @@ test('missing microphone data ends the session and disables the mic with an acti
|
||||
test('hands-free endpointing releases the microphone and waits for the final transcript before completing', async () => {
|
||||
const h = harness(); let ends = 0, completed = false;
|
||||
h.bridge.onSpeechEnd(() => ends++);
|
||||
const start = h.bridge.startGlassesCapture(h.session, () => {}, true); h.release(); await start;
|
||||
const start = h.bridge.startGlassesCapture(h.session, h.onboard, () => {}, true); h.release(); await start;
|
||||
assert.equal(h.native.endpointing, true);
|
||||
h.event({ kind: 'transcript', text: 'Set a timer', final: false });
|
||||
h.event({ kind: 'finishing' });
|
||||
@@ -80,27 +96,27 @@ test('hands-free endpointing releases the microphone and waits for the final tra
|
||||
h.event({ kind: 'ended' }); await completion;
|
||||
assert.equal(completed, true);
|
||||
assert.equal(h.text.at(-1).text, 'Set a timer for five minutes.');
|
||||
const manual = h.bridge.startGlassesCapture(h.session, () => {}); h.release(); await manual;
|
||||
const manual = h.bridge.startGlassesCapture(h.session, h.onboard, () => {}); h.release(); await manual;
|
||||
assert.equal(h.native.endpointing, false);
|
||||
const cancelled = h.bridge.stopPushToTalk(); h.bridge.stop(); await cancelled;
|
||||
});
|
||||
|
||||
test('phone microphone permission is requested only for foreground preview capture', async () => {
|
||||
const h = harness(); h.microphoneAuthorization(0);
|
||||
assert.equal(await h.bridge.prepare(true), true);
|
||||
assert.equal(await h.bridge.prepare(true, false, h.onboard), true);
|
||||
assert.equal(h.microphoneRequests(), 0, 'glasses input does not need phone microphone permission');
|
||||
assert.equal(await h.bridge.prepare(false, true), false);
|
||||
assert.equal(await h.bridge.prepare(false, true, h.onboard), false);
|
||||
assert.equal(h.microphoneRequests(), 0);
|
||||
assert.equal(await h.bridge.prepare(true, true), true);
|
||||
assert.equal(await h.bridge.prepare(true, true, h.onboard), true);
|
||||
assert.equal(h.microphoneRequests(), 1);
|
||||
h.microphoneAuthorization(2);
|
||||
assert.equal(await h.bridge.prepare(true, true), false);
|
||||
assert.equal(await h.bridge.prepare(true, true, h.onboard), false);
|
||||
assert.match(h.bridge.statusText, /Microphone.*Settings/);
|
||||
});
|
||||
|
||||
test('phone capture forwards endpointing and drains final text without touching Bluetooth', async () => {
|
||||
const h = harness();
|
||||
await h.bridge.startPhoneCapture(() => {}, true);
|
||||
await h.bridge.startPhoneCapture(h.onboard, () => {}, true);
|
||||
assert.equal(h.native.phone, true); assert.equal(h.native.endpointing, true);
|
||||
h.bridge.handleSessionEnded();
|
||||
h.event({ kind: 'transcript', text: 'First line\nSecond line', final: false });
|
||||
@@ -118,11 +134,11 @@ test('phone capture forwards endpointing and drains final text without touching
|
||||
|
||||
test('background cleanup stops phone capture but preserves glasses capture', async () => {
|
||||
const h = harness(); let ends = 0; h.bridge.onSpeechEnd(() => ends++);
|
||||
await h.bridge.startPhoneCapture(() => {});
|
||||
await h.bridge.startPhoneCapture(h.onboard, () => {});
|
||||
h.bridge.stopPhoneCapture();
|
||||
assert.equal(ends, 1);
|
||||
h.event({ kind: 'transcript', text: 'stale', final: true }); assert.equal(h.text.length, 0);
|
||||
const start = h.bridge.startGlassesCapture(h.session, () => {}); h.release(); await start;
|
||||
const start = h.bridge.startGlassesCapture(h.session, h.onboard, () => {}); h.release(); await start;
|
||||
h.bridge.stopPhoneCapture();
|
||||
h.event({ kind: 'transcript', text: 'on glasses', final: false });
|
||||
assert.equal(h.text.at(-1).text, 'on glasses');
|
||||
@@ -132,7 +148,68 @@ test('background cleanup stops phone capture but preserves glasses capture', asy
|
||||
test('phone microphone startup errors end capture with an actionable status', async () => {
|
||||
const h = harness(); let ends = 0; h.bridge.onSpeechEnd(() => ends++);
|
||||
h.native.error = 'No phone microphone is available.';
|
||||
await h.bridge.startPhoneCapture(() => {});
|
||||
await h.bridge.startPhoneCapture(h.onboard, () => {});
|
||||
assert.equal(ends, 1); assert.match(h.bridge.statusText, /No phone microphone/);
|
||||
await h.bridge.stopPushToTalk();
|
||||
});
|
||||
|
||||
test('a cloud provider needs no Speech Recognition permission, but phone capture still needs the microphone', async () => {
|
||||
const h = harness(); h.authorization(0); h.microphoneAuthorization(0);
|
||||
assert.equal(await h.bridge.prepare(false, false, h.cloud), true);
|
||||
assert.equal(await h.bridge.prepare(false, true, h.cloud), false);
|
||||
assert.equal(await h.bridge.prepare(true, true, h.cloud), true);
|
||||
assert.equal(h.permissionRequests(), 0); assert.equal(h.microphoneRequests(), 1);
|
||||
});
|
||||
|
||||
test('glasses cloud capture streams native PCM and completes on the final after the drained audio', async () => {
|
||||
const h = harness();
|
||||
const start = h.bridge.startGlassesCapture(h.session, h.cloud, () => {});
|
||||
assert.deepEqual({ ...h.native.pcmCapture }, { endpointing: false, phone: false });
|
||||
const client = h.clouds[0]; assert.equal(client.started, true);
|
||||
client.options.onStatus('Listening (Soniox)...');
|
||||
h.session.packet(new Uint8Array([1, 2])); assert.equal(h.audio.length, 1, 'LC3 still decodes natively');
|
||||
h.release(); await start;
|
||||
h.native.pcmHandler(new Uint8Array([5, 6]).buffer);
|
||||
h.event({ kind: 'audio', seconds: 1, packets: 20, rms: 0.1, missing: 0, errors: 0 });
|
||||
assert.equal(h.bridge.statusText, 'Listening (Soniox)...', 'the provider status stays up');
|
||||
client.options.onTranscript({ text: 'hello', isFinal: false });
|
||||
let done = false;
|
||||
const completion = h.bridge.stopPushToTalk().then(() => { done = true; });
|
||||
assert.deepEqual(h.commands, [true, false]); assert.equal(h.native.finished, true);
|
||||
h.event({ kind: 'finishing' });
|
||||
assert.equal(client.finished, 0, 'commit waits for the native audio to drain');
|
||||
h.native.pcmHandler(new Uint8Array([7]).buffer);
|
||||
h.event({ kind: 'ended', message: 'Ready to send' });
|
||||
assert.deepEqual(client.pcm, [[5, 6], [7]]); assert.equal(client.finished, 1);
|
||||
await Promise.resolve(); assert.equal(done, false);
|
||||
client.options.onTranscript({ text: 'Hello there.', isFinal: true });
|
||||
await completion;
|
||||
assert.equal(client.stopped, true); assert.equal(h.bridge.statusText, 'Ready to send');
|
||||
assert.deepEqual(h.text, [{ text: 'hello', isFinal: false }, { text: 'Hello there.', isFinal: true }]);
|
||||
assert.equal(h.timers.size, 0);
|
||||
});
|
||||
|
||||
test('cloud capture times out a missing final, keeps native errors, and ends on provider errors', async () => {
|
||||
const h = harness();
|
||||
await h.bridge.startPhoneCapture(h.cloud, () => {}, true);
|
||||
assert.deepEqual({ ...h.native.pcmCapture }, { endpointing: true, phone: true });
|
||||
const completion = h.bridge.stopPushToTalk();
|
||||
h.event({ kind: 'ended', message: 'Phone microphone interrupted.' });
|
||||
assert.equal(h.clouds[0].finished, 1); assert.equal(h.timers.size, 1);
|
||||
for (const fn of [...h.timers.values()]) fn();
|
||||
await completion;
|
||||
assert.equal(h.clouds[0].stopped, true); assert.equal(h.bridge.statusText, 'Phone microphone interrupted.');
|
||||
|
||||
let ends = 0; h.bridge.onSpeechEnd(() => ends++);
|
||||
await h.bridge.startPhoneCapture(h.cloud, () => {});
|
||||
h.clouds[1].options.onError('Soniox: Invalid API key');
|
||||
assert.equal(h.clouds[1].stopped, true); assert.equal(ends, 1);
|
||||
assert.match(h.bridge.statusText, /Invalid API key/);
|
||||
h.clouds[1].options.onTranscript({ text: 'stale', isFinal: true });
|
||||
assert.equal(h.text.length, 0);
|
||||
|
||||
h.native.error = 'No phone microphone is available.';
|
||||
await h.bridge.startPhoneCapture(h.cloud, () => {});
|
||||
assert.equal(h.clouds[2].started, false, 'no connection without audio');
|
||||
assert.match(h.bridge.statusText, /No phone microphone/);
|
||||
});
|
||||
@@ -1,23 +1,39 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "FaceclawSocket.h"
|
||||
/** Drains the socket's events until it closes; handler returns NO to stop early. */
|
||||
static void pump(FaceclawSocket *socket, BOOL (^handler)(NSDictionary *event)) {
|
||||
NSDate *deadline = [NSDate dateWithTimeIntervalSinceNow:10];
|
||||
while (deadline.timeIntervalSinceNow > 0) {
|
||||
NSArray *events = [NSJSONSerialization JSONObjectWithData:[[socket takeEvents] dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
|
||||
for (NSDictionary *event in events) if (!handler(event)) return;
|
||||
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
|
||||
}
|
||||
}
|
||||
int main(int argc, const char **argv) {
|
||||
@autoreleasepool {
|
||||
FaceclawSocket *socket = [[FaceclawSocket alloc] initWithURL:[NSString stringWithUTF8String:argv[1]]];
|
||||
BOOL opened = NO, echoed = NO, closed = NO;
|
||||
NSDate *deadline = [NSDate dateWithTimeIntervalSinceNow:10];
|
||||
while (!closed && deadline.timeIntervalSinceNow > 0) {
|
||||
NSArray *events = [NSJSONSerialization JSONObjectWithData:[[socket takeEvents] dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
|
||||
for (NSDictionary *event in events) {
|
||||
NSString *kind = event[@"kind"];
|
||||
if ([kind isEqual:@"open"]) { opened = YES; [socket sendText:@"Faceclaw echo: π 🔋"]; }
|
||||
if ([kind isEqual:@"text"]) echoed = [event[@"text"] isEqual:@"Faceclaw echo: π 🔋"];
|
||||
if ([kind isEqual:@"closed"] || [kind isEqual:@"error"]) closed = YES;
|
||||
}
|
||||
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
|
||||
}
|
||||
NSString *url = [NSString stringWithUTF8String:argv[1]];
|
||||
FaceclawSocket *socket = [[FaceclawSocket alloc] initWithURL:url headerName:@"X-Faceclaw-Key" headerValue:@"secret"];
|
||||
__block BOOL opened = NO, binary = NO, echoed = NO, closed = NO;
|
||||
pump(socket, ^BOOL(NSDictionary *event) {
|
||||
NSString *kind = event[@"kind"];
|
||||
if ([kind isEqual:@"open"]) { opened = YES; const uint8_t bytes[] = {1, 2, 255}; [socket sendData:[NSData dataWithBytes:bytes length:3]]; }
|
||||
if ([kind isEqual:@"text"] && [event[@"text"] isEqual:@"binary:0102ff"]) { binary = YES; [socket sendText:@"Faceclaw echo: π 🔋"]; }
|
||||
if ([kind isEqual:@"text"] && [event[@"text"] isEqual:@"Faceclaw echo: π 🔋"]) echoed = YES;
|
||||
if ([kind isEqual:@"closed"] || [kind isEqual:@"error"]) { closed = YES; return NO; }
|
||||
return YES;
|
||||
});
|
||||
[socket close];
|
||||
if (!opened || !echoed || !closed) { NSLog(@"FAIL open=%d echo=%d close=%d", opened, echoed, closed); return 1; }
|
||||
puts("PASS: native WebSocket opens, echoes UTF-8 and reports close");
|
||||
if (!opened || !binary || !echoed || !closed) { NSLog(@"FAIL open=%d binary=%d echo=%d close=%d", opened, binary, echoed, closed); return 1; }
|
||||
// Without the header the server refuses the handshake; the error names the status.
|
||||
FaceclawSocket *refused = [[FaceclawSocket alloc] initWithURL:url];
|
||||
__block NSString *error = nil;
|
||||
pump(refused, ^BOOL(NSDictionary *event) {
|
||||
if ([event[@"kind"] isEqual:@"error"]) { error = event[@"text"]; return NO; }
|
||||
return YES;
|
||||
});
|
||||
[refused close];
|
||||
if (![error containsString:@"HTTP 401"]) { NSLog(@"FAIL refused handshake error=%@", error); return 1; }
|
||||
puts("PASS: native WebSocket sends a handshake header, binary and UTF-8 frames, reports close and refused status");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -171,6 +171,7 @@ test('native completion is capture-specific, follows the final, and does not wai
|
||||
const { FaceclawVoiceControlBridge } = load('app/native/voice-control.ts', {
|
||||
'@nativescript/core': { Utils: { android: { getApplicationContext: () => ({}) } } },
|
||||
'./speech-pause': { SpeechPauseDetector: class { reset() {} } },
|
||||
'./cloud-stt-provider': { createCloudSttClient: () => null },
|
||||
}, { global: { isAndroid: true }, com: { faceclaw: { app: {
|
||||
FaceclawVoiceController: Controller,
|
||||
FaceclawVoiceControllerListener: function (listener) { return listener; },
|
||||
|
||||
Reference in new issue
Block a user