Offline Piper narration: downloadable voices (settings), automatic level prefetch, durable audio and Pyodide caches

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
denis
2026-10-17 14:50:06 +02:00
parent 3f10adb36b
commit 5d2cc0eedf
13 changed files with 431 additions and 9 deletions
+53
View File
@@ -206,6 +206,59 @@ test('narration : fichier Piper lu jusqu\'au bout (politique autoplay par défau
assert.deepEqual(errors, []);
});
test('voix hors ligne : télécharger les narrations Piper puis les écouter sans connexion', { skip, timeout: 180000 }, async () => {
const context = await browser.newContext();
const page = await context.newPage();
const errors = [];
page.on('pageerror', (error) => errors.push(error.message));
await page.addInitScript(() => {
window.__audio = { ended: 0, errors: 0 };
const Original = window.Audio;
window.Audio = class extends Original {
constructor(...args) {
super(...args);
this.addEventListener('ended', () => {
if (this.src.startsWith('blob:')) window.__audio.ended += 1; // lecture d'un fichier Piper (pas la voix système)
});
this.addEventListener('error', () => (window.__audio.errors += 1));
}
};
// La voix du système ne doit jamais servir ici : on compte ses lectures.
window.__systemVoice = 0;
const speak = window.speechSynthesis?.speak?.bind(window.speechSynthesis);
if (speak) window.speechSynthesis.speak = (...args) => ((window.__systemVoice += 1), speak(...args));
});
await page.goto(base);
await page.waitForSelector('.explain', { timeout: 60000 });
// Le service worker contrôle la page (condition pour servir le cache hors ligne).
await page.waitForFunction(() => navigator.serviceWorker.controller !== null, null, { timeout: 30000 });
// Réglages : télécharger toutes les voix de la langue.
await page.locator('#settings-toggle').click();
const group = page.locator('.settings .voices-group');
await group.getByRole('button', { name: 'Télécharger les voix pour le hors ligne' }).click();
await group.locator('.voices-message').getByText(/Voix téléchargées/).waitFor({ timeout: 90000 });
const status = await group.locator('.voices-status').textContent();
const [, cached, total] = status.match(/(\d+) voix sur (\d+)/) ?? [];
assert.ok(Number(total) > 40 && cached === total, `toutes les voix du français sont en cache : ${status}`);
assert.equal(await group.getByRole('button', { name: 'Télécharger les voix pour le hors ligne' }).isDisabled(), true);
await page.locator('.drawer-right .drawer-close').click();
await page.locator('.drawer-right.is-open').waitFor({ state: 'hidden' });
// Hors connexion : un niveau lointain se lit avec la voix Piper (fichier du cache), pas avec la synthèse système.
await context.setOffline(true);
await page.evaluate(() => (location.hash = '#/levels/09-dicts'));
await page.waitForFunction(() => document.querySelector('#level-current')?.textContent.includes('Niveau 10'), null, { timeout: 30000 });
const listen = page.locator('.explain .voice-button').first();
await listen.waitFor({ state: 'visible' });
await listen.click();
await page.waitForFunction(() => window.__audio.ended >= 1, null, { timeout: 30000 });
assert.equal(await page.evaluate(() => window.__systemVoice), 0, 'la voix du système ne doit pas avoir servi');
assert.equal(await page.evaluate(() => window.__audio.errors), 0);
assert.doesNotMatch(await page.textContent('.toast-region'), /Impossible de lire/);
assert.deepEqual(errors, []);
});
test('niveau 07 : boucle while avec plusieurs saisies successives dans l\'interface', { skip, timeout: 120000 }, async () => {
const page = await (await browser.newContext()).newPage();
const errors = [];
+97
View File
@@ -0,0 +1,97 @@
import { test } from 'node:test';
import assert from 'node:assert';
import { OfflineVoices, AUDIO_CACHE } from '../../app/js/core/OfflineVoices.js';
// Faux CacheStorage minimal : match/put par URL.
function fakeCaches() {
const stores = new Map();
return {
stores,
open: async (name) => {
if (!stores.has(name)) stores.set(name, new Map());
const store = stores.get(name);
return { match: async (url) => store.get(String(url)), put: async (url, response) => void store.set(String(url), response) };
},
};
}
const manifests = {
'00-intro': { fr: { aaa: {}, bbb: {} }, en: { ccc: {} } },
'01-hello': { fr: { ddd: {} } },
'02-none': {},
};
function server({ failUrls = [], offline = false } = {}) {
const calls = [];
const fetchFn = async (input) => {
const url = String(input);
calls.push(url);
if (offline) throw new TypeError('offline');
const id = url.match(/levels\/([^/]+)\/audio\/manifest\.json$/)?.[1];
if (id) return id in manifests ? new Response(JSON.stringify(manifests[id])) : new Response('', { status: 404 });
if (failUrls.some((bad) => url.endsWith(bad))) return new Response('', { status: 500 });
return new Response('mp3');
};
return { fetchFn, calls };
}
const levelsUrl = 'https://x.test/levels/';
const ids = ['00-intro', '01-hello', '02-none', '99-missing'];
test('les URLs viennent des manifestes, par langue ; niveaux sans voix ou inconnus ignorés', async () => {
const { fetchFn } = server();
const voices = new OfflineVoices({ levelsUrl, fetchFn, cachesApi: fakeCaches() });
assert.deepEqual(await voices.urlsFor(ids, 'fr'), [
'https://x.test/levels/00-intro/audio/fr/aaa.mp3',
'https://x.test/levels/00-intro/audio/fr/bbb.mp3',
'https://x.test/levels/01-hello/audio/fr/ddd.mp3',
]);
assert.deepEqual(await voices.urlsFor(ids, 'en'), ['https://x.test/levels/00-intro/audio/en/ccc.mp3']);
});
test('télécharger remplit le cache audio, avec progression ; seul ce qui manque est téléchargé', async () => {
const caches = fakeCaches();
const { fetchFn, calls } = server();
const voices = new OfflineVoices({ levelsUrl, fetchFn, cachesApi: caches });
assert.deepEqual(await voices.status(ids, 'fr'), { total: 3, cached: 0 });
const progress = [];
const result = await voices.download(ids, 'fr', { onProgress: (done, total) => progress.push([done, total]) });
assert.deepEqual(result, { total: 3, downloaded: 3, failed: 0 });
assert.deepEqual(progress.at(-1), [3, 3]);
assert.equal(caches.stores.get(AUDIO_CACHE).size, 3);
assert.deepEqual(await voices.status(ids, 'fr'), { total: 3, cached: 3 });
const mp3Before = calls.filter((url) => url.endsWith('.mp3')).length;
assert.deepEqual(await voices.download(ids, 'fr'), { total: 3, downloaded: 0, failed: 0 });
assert.equal(calls.filter((url) => url.endsWith('.mp3')).length, mp3Before, 'rien de retéléchargé');
});
test('un échec isolé est compté sans bloquer les autres ; hors ligne, tout échoue proprement', async () => {
const caches = fakeCaches();
const voices = new OfflineVoices({ levelsUrl, fetchFn: server({ failUrls: ['bbb.mp3'] }).fetchFn, cachesApi: caches });
assert.deepEqual(await voices.download(ids, 'fr'), { total: 3, downloaded: 2, failed: 1 });
assert.equal(caches.stores.get(AUDIO_CACHE).size, 2);
// Relancer ne réessaie que le fichier manquant.
const retry = new OfflineVoices({ levelsUrl, fetchFn: server().fetchFn, cachesApi: caches });
assert.deepEqual(await retry.download(ids, 'fr'), { total: 3, downloaded: 1, failed: 0 });
const offline = new OfflineVoices({ levelsUrl, fetchFn: server({ offline: true }).fetchFn, cachesApi: fakeCaches() });
assert.deepEqual(await offline.download(ids, 'fr'), { total: 0, downloaded: 0, failed: 0 });
});
test('annulation : plus aucun téléchargement après abort', async () => {
const controller = new AbortController();
const { fetchFn, calls } = server();
const voices = new OfflineVoices({ levelsUrl, fetchFn, cachesApi: fakeCaches() });
controller.abort();
const result = await voices.download(ids, 'fr', { signal: controller.signal });
assert.equal(result.downloaded, 0);
assert.equal(calls.filter((url) => url.endsWith('.mp3')).length, 0);
});
test('sans API Cache : non pris en charge, aucune erreur', async () => {
const voices = new OfflineVoices({ levelsUrl, fetchFn: server().fetchFn, cachesApi: undefined });
assert.equal(voices.supported, false);
assert.deepEqual(await voices.status(ids, 'fr'), { total: 3, cached: 0 });
assert.deepEqual(await voices.download(ids, 'fr'), { total: 3, downloaded: 0, failed: 3 });
});