diff --git a/package.json b/package.json index 692286f..02fa246 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "screen:preview": "tsx scripts/preview.ts", "brand": "tsx scripts/brand.ts", "e2e": "playwright test", - "holidays:sync": "tsx scripts/sync-holidays.ts" + "holidays:sync": "tsx scripts/sync-holidays.ts", + "translate:test": "tsx scripts/test-translation.ts" }, "dependencies": { "@fontsource-variable/inter": "^5.3.0", diff --git a/scripts/test-translation.ts b/scripts/test-translation.ts new file mode 100644 index 0000000..2e2c4a9 --- /dev/null +++ b/scripts/test-translation.ts @@ -0,0 +1,56 @@ +/** + * Round-trips a phrase through the translation service. + * + * npm run translate:test ["texte à traduire"] + * + * The settings page has the same button; this exists to check a deployment + * from the host, before anyone signs in, and to see the cache working. + */ + +import 'dotenv/config'; + +import { prisma } from '../lib/db'; +import { defaultProvider, translateToEnglish } from '../lib/translation/service'; + +async function main(): Promise { + const text = process.argv[2] ?? 'Fermeture exceptionnelle jeudi après-midi'; + const provider = defaultProvider(); + + if (!provider) { + console.error('Service non configuré : renseignez TRANSLATION_API_URL et TRANSLATION_API_KEY.'); + process.exitCode = 1; + return; + } + + console.log(`Modèle : ${provider.model}`); + console.log(`Français : ${text}`); + + const started = Date.now(); + const first = await translateToEnglish(text, provider); + const elapsed = Date.now() - started; + + if (!first.ok) { + console.error(`Échec : ${first.error}`); + process.exitCode = 1; + return; + } + + console.log(`Anglais : ${first.text}`); + console.log(`Durée : ${elapsed} ms (cache : ${first.cached ? 'oui' : 'non'})`); + + // Second pass proves the cache: it must not touch the network at all. + const again = Date.now(); + const second = await translateToEnglish(text, provider); + console.log( + `2e appel : ${Date.now() - again} ms (cache : ${second.ok && second.cached ? 'oui' : 'non'})`, + ); +} + +main() + .catch((error: unknown) => { + console.error(error); + process.exitCode = 1; + }) + .finally(() => { + void prisma.$disconnect(); + });