mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-19 21:03:09 +00:00
211 lines
11 KiB
JavaScript
211 lines
11 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const vm = require('node:vm');
|
|
const ts = require('typescript');
|
|
|
|
function load(file, requireModule = () => ({}), globals = {}) {
|
|
const exports = {};
|
|
vm.runInNewContext(ts.transpileModule(fs.readFileSync(path.join(__dirname, '..', file), 'utf8'), {
|
|
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
|
|
}).outputText, { exports, require: requireModule, console, global: { isAndroid: true }, ...globals });
|
|
return exports;
|
|
}
|
|
const languages = load('app/native/transcription-language.ts');
|
|
function settings(values = new Map()) {
|
|
return load('app/ui/dashboard-settings.ts', name => name.endsWith('/transcription-language') ? languages
|
|
: name.includes('settings-store') ? {
|
|
getStringSetting: (key, fallback) => values.get(key) ?? fallback,
|
|
setStringSetting: (key, value) => values.set(key, value),
|
|
getBooleanSetting: (_key, fallback) => fallback,
|
|
onSettingsStoreChanged() {},
|
|
} : { Layer: class {}, ASSISTANT_MODEL_CHOICES: [] });
|
|
}
|
|
|
|
test('language defaults preserve local English, cloud detection and an existing server preference', () => {
|
|
const values = new Map([['voice.whisperServerLanguage', 'sk']]);
|
|
const s = settings(values);
|
|
assert.equal(s.voiceLanguageSetting.get(), 'default');
|
|
assert.equal(s.getVoiceTranscriptionLanguage('onboard-whisper'), 'en');
|
|
assert.equal(s.getVoiceTranscriptionLanguage('whisper'), '');
|
|
assert.equal(s.getVoiceTranscriptionLanguage('whisper-server'), 'sk');
|
|
assert.deepEqual([...values], [['voice.whisperServerLanguage', 'sk']]);
|
|
});
|
|
|
|
test('French and auto-detection persist across restart without overwriting server exceptions', () => {
|
|
const values = new Map([['voice.whisperServerLanguage', 'sk']]);
|
|
let s = settings(values);
|
|
s.voiceLanguageSetting.set('fr');
|
|
s = settings(values);
|
|
assert.equal(s.voiceLanguageSetting.displayValue(), 'French');
|
|
for (const provider of ['whisper', 'onboard-whisper', 'whisper-server', 'elevenlabs', 'soniox']) {
|
|
assert.equal(s.getVoiceTranscriptionLanguage(provider), 'fr');
|
|
}
|
|
assert.equal(s.getVoiceTranscriptionLanguage('onboard'), '');
|
|
s.voiceLanguageSetting.set('auto');
|
|
s = settings(values);
|
|
assert.equal(s.getVoiceTranscriptionLanguage('onboard-whisper'), '');
|
|
assert.equal(s.getVoiceTranscriptionLanguage('whisper'), '');
|
|
s.voiceLanguageSetting.set('default');
|
|
assert.equal(s.getVoiceTranscriptionLanguage('whisper-server'), 'sk');
|
|
assert.equal(s.getVoiceTranscriptionLanguage('onboard-whisper'), 'en');
|
|
});
|
|
|
|
test('corrupt language preferences revert to the compatible provider defaults', () => {
|
|
const s = settings(new Map([['voice.language', 'invalid']]));
|
|
assert.equal(s.getVoiceTranscriptionLanguage('onboard-whisper'), 'en');
|
|
assert.equal(s.getVoiceTranscriptionLanguage('whisper'), '');
|
|
});
|
|
|
|
test('Android and iOS capture options use the shared language and preserve Translate overrides', () => {
|
|
const s = settings();
|
|
s.voiceLanguageSetting.set('fr');
|
|
s.voiceProviderSetting.set('whisper');
|
|
s.openAiTranscriptionModelSetting.set('gpt-4o-mini-transcribe');
|
|
for (const [file, name, method] of [
|
|
['app/g2/dashboard-controller.ts', 'DashboardController', 'voiceCaptureOptions'],
|
|
['app/g2/ios-preview-controller.ts', 'IosPreviewController', 'voiceProvider'],
|
|
]) {
|
|
const host = { continuousClients: new Map() };
|
|
host.transcriptionChoice = () => [...host.continuousClients.values()].find(x => x.transcription)?.transcription;
|
|
// Run each real capture-options method with only its native dependencies stubbed.
|
|
const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
|
|
const tree = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true);
|
|
const declaration = tree.statements.find(n => ts.isClassDeclaration(n) && n.name.text === name);
|
|
const member = declaration.members.find(n => n.name?.getText(tree) === method);
|
|
const capture = loadBody(member.body.getText(tree), s, host);
|
|
assert.equal(capture().language, 'fr');
|
|
assert.equal(capture().openAiTranscriptionModel, 'gpt-4o-mini-transcribe');
|
|
host.continuousClients.set('translate', { transcription: { provider: 'soniox', language: 'ja' } });
|
|
assert.equal(capture().provider, 'soniox');
|
|
assert.equal(capture().language, 'ja');
|
|
host.continuousClients.clear();
|
|
assert.equal(capture().language, 'fr');
|
|
assert.equal(capture().openAiTranscriptionModel, 'gpt-4o-mini-transcribe');
|
|
}
|
|
});
|
|
|
|
function loadBody(body, s, host) {
|
|
const globals = { ...s, wearerVerificationOptions: () => null, micSession: { captureProcessingOptions: () => ({}) } };
|
|
return vm.runInNewContext(`(function(communicator) ${body})`, globals).bind(host, null);
|
|
}
|
|
|
|
test('multilingual downloads and deletion leave an existing English model intact', () => {
|
|
const files = new Map(), downloads = [];
|
|
const asr = load('app/native/asr-model.ts', () => ({
|
|
Utils: { android: { getApplicationContext: () => ({ getFilesDir: () => ({ getAbsolutePath: () => '/files' }) }) } },
|
|
}), {
|
|
java: { io: { File: class {
|
|
constructor(name) { this.name = name; }
|
|
exists() { return files.has(this.name); }
|
|
length() { return files.get(this.name) ?? 0; }
|
|
delete() { return files.delete(this.name); }
|
|
} } },
|
|
com: { faceclaw: { app: {
|
|
FaceclawModelDownloaderListener: function(listener) { return listener; },
|
|
FaceclawModelDownloader: class {
|
|
constructor(url, destination, hash, size, listener) {
|
|
Object.assign(this, { url, destination, hash, size, listener }); downloads.push(this);
|
|
}
|
|
start() {}
|
|
cancel() {}
|
|
},
|
|
} } },
|
|
});
|
|
const english = asr.ASR_MODELS['whisper-base-en'];
|
|
for (const file of english.files) files.set(`/files/faceclaw-voice-asr/${english.dirName}/${file.name}`, file.sizeBytes);
|
|
assert.equal(asr.isAsrModelReady('whisper-base-en'), true);
|
|
assert.equal(asr.isAsrModelReady('whisper-base'), false);
|
|
asr.startAsrModelDownload('whisper-base');
|
|
assert.equal(asr.asrModelState('whisper-base').status, 'downloading');
|
|
for (let index = 0; index < 3; index++) {
|
|
const download = downloads[index];
|
|
assert.match(download.url, /sherpa-onnx-whisper-base\/resolve\/[a-f0-9]{40}\/base-/);
|
|
assert.match(download.hash, /^[a-f0-9]{64}$/);
|
|
files.set(download.destination, download.size);
|
|
download.listener.onDone();
|
|
}
|
|
assert.equal(downloads.length, 3);
|
|
assert.equal(asr.isAsrModelReady('whisper-base'), true);
|
|
asr.deleteAsrModel('whisper-base');
|
|
assert.equal(asr.isAsrModelReady('whisper-base'), false);
|
|
assert.equal(asr.isAsrModelReady('whisper-base-en'), true);
|
|
});
|
|
|
|
test('OpenAI transcription model preserves legacy default and persists independently of language', () => {
|
|
const values = new Map();
|
|
let s = settings(values);
|
|
assert.equal(s.openAiTranscriptionModelSetting.get(), 'gpt-realtime-whisper');
|
|
s.openAiTranscriptionModelSetting.set('gpt-4o-mini-transcribe');
|
|
s.voiceLanguageSetting.set('fr');
|
|
s = settings(values);
|
|
assert.equal(s.openAiTranscriptionModelSetting.get(), 'gpt-4o-mini-transcribe');
|
|
assert.equal(s.voiceLanguageSetting.get(), 'fr');
|
|
assert.equal(settings(new Map([['voice.openAiTranscriptionModel', 'invalid']])).openAiTranscriptionModelSetting.get(), 'gpt-realtime-whisper');
|
|
});
|
|
|
|
test('French provider persists independently and never overwrites shared language preferences', () => {
|
|
const values = new Map([['voice.language', 'ja'], ['voice.provider', 'onboard-whisper']]);
|
|
let s = settings(values);
|
|
s.voiceProviderSetting.set('onboard-whisper-fr');
|
|
s = settings(values);
|
|
assert.equal(s.voiceProviderSetting.get(), 'onboard-whisper-fr');
|
|
assert.equal(s.getVoiceTranscriptionLanguage('onboard-whisper-fr'), 'fr');
|
|
assert.equal(s.getVoiceTranscriptionLanguage('onboard-whisper'), 'ja');
|
|
assert.equal(s.voiceLanguageSetting.get(), 'ja');
|
|
s.voiceProviderSetting.set('onboard-whisper');
|
|
assert.equal(s.getVoiceTranscriptionLanguage('onboard-whisper'), 'ja');
|
|
});
|
|
|
|
test('French model download resumes, verifies every file including its license, and preserves other models', () => {
|
|
const files = new Map(), downloads = [];
|
|
const asr = load('app/native/asr-model.ts', () => ({
|
|
Utils: { android: { getApplicationContext: () => ({ getFilesDir: () => ({ getAbsolutePath: () => '/files' }) }) } },
|
|
}), {
|
|
java: { io: { File: class {
|
|
constructor(name) { this.name = name; }
|
|
exists() { return files.has(this.name); }
|
|
length() { return files.get(this.name) ?? 0; }
|
|
delete() { return files.delete(this.name); }
|
|
} } },
|
|
com: { faceclaw: { app: {
|
|
FaceclawModelDownloaderListener: function(listener) { return listener; },
|
|
FaceclawModelDownloader: class {
|
|
constructor(url, destination, hash, size, listener) {
|
|
Object.assign(this, { url, destination, hash, size, listener }); downloads.push(this);
|
|
}
|
|
start() {}
|
|
cancel() {}
|
|
},
|
|
} } },
|
|
});
|
|
for (const id of ['moonshine', 'whisper-base-en', 'whisper-base']) {
|
|
const model = asr.ASR_MODELS[id];
|
|
for (const file of model.files) files.set(`/files/faceclaw-voice-asr/${model.dirName}/${file.name}`, file.sizeBytes);
|
|
}
|
|
const model = asr.ASR_MODELS['whisper-small-fr'];
|
|
const manifest = JSON.parse(fs.readFileSync(path.join(__dirname, '../docs/models/whisper-small-fr/manifest.json')));
|
|
assert.deepEqual(JSON.parse(JSON.stringify(model.files)), manifest.files);
|
|
assert.equal(model.totalBytes, model.files.reduce((total, file) => total + file.sizeBytes, 0));
|
|
assert.equal(asr.isAsrModelReady('whisper-small-fr'), false);
|
|
asr.startAsrModelDownload('whisper-small-fr');
|
|
files.set(downloads[0].destination, downloads[0].size);
|
|
downloads[0].listener.onDone();
|
|
downloads[1].listener.onError('network unavailable');
|
|
assert.equal(asr.asrModelState('whisper-small-fr').status, 'absent');
|
|
asr.startAsrModelDownload('whisper-small-fr');
|
|
assert.equal(downloads[2].destination, downloads[1].destination, 'resume the missing decoder');
|
|
for (let index = 2; index < 5; index++) {
|
|
const download = downloads[index];
|
|
assert.match(download.url, /api\/packages\/Blendman974\/generic\/faceclaw-asr\/whisper-small-fr-v1\//);
|
|
assert.match(download.hash, /^[a-f0-9]{64}$/);
|
|
files.set(download.destination, download.size);
|
|
download.listener.onDone();
|
|
}
|
|
assert.equal(asr.isAsrModelReady('whisper-small-fr'), true);
|
|
asr.deleteAsrModel('whisper-small-fr');
|
|
assert.equal(asr.isAsrModelReady('whisper-small-fr'), false);
|
|
for (const id of ['moonshine', 'whisper-base-en', 'whisper-base']) assert.equal(asr.isAsrModelReady(id), true);
|
|
});
|