Translate app: sound level meter; translate unfinished segments

This commit is contained in:
James Babcock committed 2026-10-25 13:12:25 -07:00
1 parent bf2addd287
commit 0b7d93a3f1
25 files changed
+679 -175

No files matched your search

+109 -23
View File
@@ -32,14 +32,13 @@ function settingsHarness({ main = 'onboard', setUp = [] } = {}) {
})('app/apps/translate/translate-settings.ts');
}
test('finals become segments; finals arriving mid-translation merge into one pending segment', () => {
test('finals translate in order; finals arriving mid-request merge into one waiting block', () => {
const model = new TranslationModel();
model.accept({ text: 'Hola, ¿qué', isFinal: false });
assert.equal(model.live, 'Hola, ¿qué');
model.accept({ text: 'Hola, ¿qué tal?', isFinal: true });
assert.equal(model.live, '');
assert.equal(model.segments.length, 1);
const first = model.nextJob();
assert.equal(first.segment.source, 'Hola, ¿qué tal?');
assert.equal(first.source, 'Hola, ¿qué tal?');
assert.deepEqual(plain(first.context), []);
assert.equal(model.nextJob(), null, 'one request at a time');
@@ -49,30 +48,91 @@ test('finals become segments; finals arriving mid-translation merge into one pen
assert.equal(model.segments.length, 2);
assert.equal(model.segments[1].source, 'Muy bien. Gracias.');
model.progress(first.segment, 'Hi, how');
assert.equal(first.segment.translation, 'Hi, how');
model.complete(first.segment, 'Hi, how are you?');
assert.equal(model.progress(first, 'Hi, how'), true);
assert.equal(first.segment.streaming, 'Hi, how');
model.complete(first, 'Hi, how are you?');
const second = model.nextJob();
assert.equal(second.segment.source, 'Muy bien. Gracias.');
assert.equal(second.source, 'Muy bien. Gracias.');
assert.deepEqual(plain(second.context), [{ source: 'Hola, ¿qué tal?', translation: 'Hi, how are you?' }]);
model.complete(second.segment, 'Very well. Thanks.');
model.complete(second, 'Very well. Thanks.');
assert.equal(model.nextJob(), null);
});
test('the live block is translated provisionally, redone as it grows, and settles on a matching final', () => {
const model = new TranslationModel();
model.accept({ text: '今日は', isFinal: false });
const first = model.nextJob();
assert.equal(first.source, '今日は');
model.accept({ text: '今日はいい天気', isFinal: false }); // grows while the request is in flight
assert.equal(model.nextJob(), null);
model.complete(first, 'Today');
assert.deepEqual(plain(model.lines(measure, 100, false)), [{ text: 'Today', gray: 170 }]);
const second = model.nextJob();
assert.equal(second.source, '今日はいい天気');
// The redo streams over the old text only once it is as long.
model.progress(second, 'To');
assert.deepEqual(plain(model.lines(measure, 100, false).map(line => line.text)), ['Today']);
model.progress(second, 'The weather');
assert.deepEqual(plain(model.lines(measure, 100, false).map(line => line.text)), ['The weather']);
model.complete(second, 'The weather is nice today');
// A final matching what was translated needs no further request.
model.accept({ text: '今日はいい天気', isFinal: true });
assert.equal(model.nextJob(), null);
assert.deepEqual(plain(model.lines(measure, 100, false)), [{ text: 'The weather is nice today', gray: 230 }]);
});
test('a final that differs from the request in flight makes it moot; a matching one keeps it', () => {
const model = new TranslationModel();
model.accept({ text: 'Bonjour', isFinal: false });
const stale = model.nextJob();
assert.equal(model.accept({ text: 'Bonjour à tous', isFinal: true }), true);
assert.equal(model.isActive(stale), false);
assert.equal(model.progress(stale, 'Hello'), false);
const redo = model.nextJob();
assert.equal(redo.source, 'Bonjour à tous');
model.complete(stale, 'Hello'); // a late reply from the cancelled request is ignored
assert.equal(model.isActive(redo), true);
model.complete(redo, 'Hello everyone');
model.accept({ text: 'Merci', isFinal: false });
const kept = model.nextJob();
assert.equal(model.accept({ text: 'Merci', isFinal: true }), false);
model.complete(kept, 'Thank you');
assert.equal(model.nextJob(), null);
assert.deepEqual(plain(model.lines(measure, 100, false).map(line => line.gray)), [230, 230]);
});
test('small revisions of a live block wait for a bigger change; its final is always redone', () => {
const model = new TranslationModel();
model.accept({ text: 'abcdef', isFinal: false });
model.complete(model.nextJob(), 'one');
model.accept({ text: 'abcdeg', isFinal: false });
assert.equal(model.nextJob(), null);
model.accept({ text: 'abcdefgh', isFinal: false });
assert.equal(model.nextJob(), null);
model.accept({ text: 'abcdefghi', isFinal: false });
model.complete(model.nextJob(), 'two');
model.accept({ text: 'abcdefghj', isFinal: true });
assert.equal(model.nextJob().source, 'abcdefghj');
});
test('an empty final keeps the live partial it replaces', () => {
const model = new TranslationModel();
model.accept({ text: 'Bonjour', isFinal: false });
model.accept({ text: '', isFinal: true });
assert.equal(model.segments[0].source, 'Bonjour');
assert.equal(model.segments[0].final, true);
});
test('context skips failed and dropped turns and stays bounded', () => {
test('context skips failed, dropped and provisional turns and stays bounded', () => {
const model = new TranslationModel();
const translate = (source, translation) => {
model.accept({ text: source, isFinal: true });
const job = model.nextJob();
if (translation === null) model.fail(job.segment);
else model.complete(job.segment, translation);
if (translation === null) model.fail(job);
else model.complete(job, translation);
};
translate('uno', 'one');
translate('ruido', null);
@@ -87,18 +147,29 @@ test('context skips failed and dropped turns and stays bounded', () => {
const long = new TranslationModel();
for (let i = 0; i < 3; i++) {
long.accept({ text: 'x'.repeat(500), isFinal: true });
long.complete(long.nextJob().segment, 'y'.repeat(500));
long.complete(long.nextJob(), 'y'.repeat(500));
}
long.accept({ text: 'next', isFinal: true });
assert.equal(long.nextJob().context.length, 1, 'the newest turn is kept even when it alone is over the cap');
// A final whose redo failed still shows its provisional translation, but
// that translates other text, so it isn't context.
const stale = new TranslationModel();
stale.accept({ text: 'Ich gehe', isFinal: false });
stale.complete(stale.nextJob(), 'I go');
stale.accept({ text: 'Ich gehe nach Hause', isFinal: true });
stale.fail(stale.nextJob());
assert.deepEqual(plain(stale.lines(measure, 100, false)), [{ text: 'I go', gray: 170 }]);
stale.accept({ text: 'Tschüss', isFinal: true });
assert.deepEqual(plain(stale.nextJob().context), []);
});
test('display lines: translations bright, originals dim until translated or when shown, live dimmest', () => {
test('display lines: settled bright, provisional dimmer, originals dim until translated or when shown', () => {
const model = new TranslationModel();
model.accept({ text: 'Guten Morgen', isFinal: true });
model.complete(model.nextJob().segment, 'Good morning');
model.complete(model.nextJob(), 'Good morning');
model.accept({ text: 'Wie geht es?', isFinal: true });
const asking = model.nextJob().segment;
const asking = model.nextJob();
model.accept({ text: 'Danke', isFinal: false });
assert.deepEqual(plain(model.lines(measure, 100, false)), [
{ text: 'Good morning', gray: 230 },
@@ -107,28 +178,29 @@ test('display lines: translations bright, originals dim until translated or when
]);
model.progress(asking, 'How are');
assert.deepEqual(plain(model.lines(measure, 100, false).map(line => line.text)), ['Good morning', 'How are', 'Danke']);
assert.deepEqual(plain(model.lines(measure, 100, true).map(line => line.gray)), [120, 230, 120, 230, 90]);
assert.deepEqual(plain(model.lines(measure, 100, true).map(line => line.gray)), [120, 230, 120, 170, 90]);
assert.deepEqual(plain(model.lines(measure, 7, false).slice(0, 2)), [{ text: 'Good', gray: 230 }, { text: 'morning', gray: 230 }]);
// A failed segment keeps its original on screen.
// A failed block keeps its original on screen.
model.fail(asking);
assert.deepEqual(plain(model.lines(measure, 100, false)[1]), { text: 'Wie geht es?', gray: 120 });
// A finished, empty translation is noise: hidden unless originals are shown.
// A settled, empty translation is noise: hidden unless originals are shown.
model.accept({ text: 'mmm', isFinal: true });
model.complete(model.nextJob().segment, '');
model.complete(model.nextJob(), '');
assert.equal(model.lines(measure, 100, false).some(line => line.text === 'mmm'), false);
assert.equal(model.lines(measure, 100, true).some(line => line.text === 'mmm'), true);
});
test('saved text pairs each original with its translation', () => {
test('saved text pairs each final original with its translation', () => {
const model = new TranslationModel();
for (const [source, translation] of [['Ciao', 'Hello'], ['Okay.', 'Okay.'], ['boh', null]]) {
model.accept({ text: source, isFinal: true });
const job = model.nextJob();
if (translation === null) model.fail(job.segment);
else model.complete(job.segment, translation);
if (translation === null) model.fail(job);
else model.complete(job, translation);
}
model.accept({ text: 'ancora', isFinal: false }); // still live: not saved
assert.equal(model.savedText(), 'Ciao\n→ Hello\n\nOkay.\n\nboh\n→ (not translated)');
});
@@ -138,6 +210,8 @@ test('the user message carries earlier turns as context, then the speech', () =>
assert.match(message, /context only/);
assert.match(message, /<speech>Bien<\/speech>\n<english>Fine<\/english>\n\nTranslate this speech/);
assert.ok(message.endsWith('<speech>¿Y tú?</speech>'));
assert.equal(buildTranslateUserMessage('こんにちは', [], 'Japanese'),
'Translate this Japanese speech into English:\n<speech>こんにちは</speech>');
});
test('Auto picks the main provider when it is multilingual, else the first one set up', () => {
@@ -164,3 +238,15 @@ test('an explicit provider is used only while it is set up, and on-device ones a
assert.equal(settings.translateProviderSetting.isDisabled('auto'), false);
assert.deepEqual(plain(settings.translateProviderSetting.values), ['auto', 'elevenlabs', 'whisper', 'soniox', 'whisper-server']);
});
test('the spoken language defaults to auto-detect and resolves to an ISO 639-1 code once chosen', () => {
const settings = settingsHarness();
assert.equal(settings.resolveTranslateLanguage(), '');
assert.equal(settings.translateLanguageSetting.displayValue('auto'), 'Auto-detect');
settings.translateLanguageSetting.set('ja');
assert.equal(settings.resolveTranslateLanguage(), 'ja');
assert.equal(settings.spokenLanguageName('ja'), 'Japanese');
assert.equal(settings.translateLanguageSetting.values[0], 'auto');
assert.ok(settings.translateLanguageSetting.values.slice(1).every(code => /^[a-z]{2}$/.test(code)));
assert.equal(settings.translateLanguageSetting.values.includes('en'), false, 'English is the target, not a source');
});