const { chromium } = require(process.env.PLAYWRIGHT_MODULE || 'playwright'); const assert = require('node:assert/strict'); const fs = require('node:fs'); (async () => { const api = process.env.AI_E2E_API, token = process.env.AI_E2E_TOKEN, url = process.env.AI_E2E_URL; assert(api && token && url); const german = JSON.parse(fs.readFileSync('src/i18n/de-DE.json', 'utf8')); const english = JSON.parse(fs.readFileSync('src/i18n/en-US.json', 'utf8')); assert.deepEqual(Object.keys(german).sort(), Object.keys(english).sort()); for (const key of Object.keys(german)) { assert(german[key] && english[key], `Empty translation: ${key}`); assert.deepEqual(german[key].match(/\{\w+\}/g)?.sort() ?? [], english[key].match(/\{\w+\}/g)?.sort() ?? [], key); } const browser = await chromium.launch({ headless: true, args: ['--no-sandbox'] }); try { const page = await browser.newPage(); page.setDefaultTimeout(15000); const errors = []; page.on('pageerror', (error) => errors.push(error.message)); const headers = { Authorization: `Bearer ${token}` }; const me = async () => { const response = await page.request.get(api + '/me', { headers }); assert.equal(response.status(), 200); return response.json(); }; const patch = async (data) => { const response = await page.request.patch(api + '/me/preferences', { headers, data }); assert.equal(response.status(), 200, await response.text()); return response.json(); }; await page.goto(url); await page.getByRole('heading', { name: 'Einloggen', exact: true }).waitFor(); await page.getByRole('combobox', { name: 'Oberflächensprache' }).selectOption('en-US'); await page.getByRole('heading', { name: 'Log in', exact: true }).waitFor(); assert.equal(await page.locator('html').getAttribute('lang'), 'en-US'); await page.reload(); await page.getByRole('heading', { name: 'Log in', exact: true }).waitFor(); await page.getByRole('button', { name: 'Create account', exact: true }).click(); await page.getByText('The English course currently uses German task instructions and explanations.').waitFor(); await page.setViewportSize({ width: 360, height: 780 }); assert(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth), 'Registration overflows on a narrow screen'); await page.setViewportSize({ width: 1280, height: 800 }); const email = `languages-${Date.now()}@example.test`, password = 'local-language-test-password'; await page.getByRole('textbox', { name: 'Email', exact: true }).fill(email); await page.getByLabel('Password', { exact: true }).fill(password); const registration = page.waitForResponse((r) => r.url().endsWith('/auth/register') && r.request().method() === 'POST'); await page.getByRole('button', { name: 'Register', exact: true }).click(); const registered = await (await registration).json(); assert.equal(registered.user.ui_language, 'en-US'); assert.equal(registered.user.instruction_language, 'de-DE'); await page.getByRole('button', { name: 'Today', exact: true }).waitFor(); await page.getByText('Preferred explanation language: German.', { exact: true }).waitFor(); await page.getByRole('heading', { name: 'A1 pronunciation practice', exact: true }).waitFor(); await page.getByRole('button', { name: 'Log out', exact: true }).click(); await page.getByRole('combobox', { name: 'Interface language' }).selectOption('de-DE'); await page.getByRole('textbox', { name: 'E-Mail', exact: true }).fill(email); await page.getByLabel('Passwort', { exact: true }).fill(password); await page.getByRole('button', { name: 'Einloggen', exact: true }).click(); await page.getByRole('button', { name: 'Today', exact: true }).waitFor(); await page.getByRole('button', { name: 'Progress', exact: true }).click(); await page.getByRole('heading', { name: 'Progress', exact: true }).waitFor(); await page.getByRole('heading', { name: 'Your pronunciation profile · en-US', exact: true }).waitFor(); await page.getByText('Your new pronunciation profile needs confirmed measurements.', { exact: false }).waitFor(); await page.getByRole('button', { name: 'Log out', exact: true }).click(); const original = await me(); await page.evaluate((value) => localStorage.setItem('speech_token', value), token); await page.reload(); await page.getByRole('combobox', { name: 'Oberflächensprache' }).selectOption('en-US'); await page.getByRole('button', { name: 'Pronunciation', exact: true }).click(); await page.getByRole('button', { name: 'Record', exact: true }).waitFor(); const sentence = await page.locator('.target-text').innerText(); await page.getByRole('combobox', { name: 'Interface language' }).selectOption('de-DE'); await page.getByRole('button', { name: 'Aufnehmen', exact: true }).waitFor(); assert.equal(await page.locator('.target-text').innerText(), sentence); await page.getByRole('combobox', { name: 'Oberflächensprache' }).selectOption('en-US'); await page.getByRole('button', { name: 'Conversation', exact: true }).click(); await page.getByRole('button', { name: 'Start', exact: true }).click(); const reply = page.getByRole('textbox', { name: 'Your conversation reply', exact: true }); await reply.fill('My name is Mira.'); await page.getByRole('combobox', { name: 'Interface language' }).selectOption('de-DE'); assert.equal(await page.getByRole('textbox', { name: 'Deine Dialogantwort', exact: true }).inputValue(), 'My name is Mira.'); await page.getByRole('combobox', { name: 'Oberflächensprache' }).selectOption('en-US'); const turnResponse = page.waitForResponse(r => /\/conversation\/[^/]+\/turn$/.test(r.url())); await page.getByRole('button', { name: 'Send', exact: true }).click(); assert.equal((await turnResponse).request().postDataJSON().text, 'My name is Mira.'); await page.locator('.bubble.learner').getByText('My name is Mira.', { exact: true }).waitFor(); await page.getByRole('button', { name: 'Administration', exact: true }).click(); await page.getByRole('heading', { name: 'AI settings and usage', exact: true }).waitFor(); const action = page.getByLabel('Action').first(); await action.selectOption('next_exercise'); await page.getByRole('option', { name: 'Pronunciation generation', exact: true }).first().waitFor({ state: 'attached' }); await page.getByLabel('Monthly limit in USD', { exact: true }).fill('0.12'); await page.getByRole('combobox', { name: 'Interface language' }).selectOption('de-DE'); assert.equal(await page.getByLabel('Monatslimit in USD', { exact: true }).inputValue(), '0.12'); await page.getByRole('combobox', { name: 'Oberflächensprache' }).selectOption('en-US'); assert.equal(await page.getByLabel('Monthly limit in USD', { exact: true }).inputValue(), '0.12'); const settings = await (await page.request.get(api + '/admin/ai/settings', { headers })).json(); assert.equal(settings.budget.monthly_limit_microusd, 0, 'Changing UI language must not save budget edits'); await page.getByLabel('Monthly limit in USD', { exact: true }).fill('0'); await page.getByRole('button', { name: 'Save budget', exact: true }).click(); await page.getByRole('status').filter({ hasText: 'Budget saved.' }).waitFor(); await page.getByRole('combobox', { name: 'Interface language' }).selectOption('de-DE'); await page.getByRole('status').filter({ hasText: 'Budget gespeichert.' }).waitFor(); await page.getByRole('combobox', { name: 'Oberflächensprache' }).selectOption('en-US'); await page.getByRole('status').filter({ hasText: 'Budget saved.' }).waitFor(); await page.getByRole('button', { name: 'A1 course', exact: true }).click(); await page.getByRole('button', { name: 'Open the original fixed course', exact: true }).click(); await page.getByText('Guided A1 course', { exact: true }).waitFor(); await page.getByRole('region', { name: 'Module progress', exact: true }).waitFor(); await patch({ ui_language: original.user.ui_language }); const learningProfile = await patch({ target_language: 'es-ES', cefr_level: 'A2', daily_goal_minutes: 23 }); await page.evaluate((value) => localStorage.setItem('speech_token', value), token); await page.reload(); const picker = page.getByRole('combobox', { name: 'Oberflächensprache' }); await picker.waitFor(); await picker.selectOption('en-US'); await page.getByRole('button', { name: 'Today', exact: true }).waitFor(); let account = await me(); assert.equal(account.user.ui_language, 'en-US'); assert.equal(account.user.instruction_language, original.user.instruction_language); assert.deepEqual(account.profile, learningProfile); await page.reload(); await page.getByRole('combobox', { name: 'Interface language' }).waitFor(); assert.equal(await page.locator('html').getAttribute('lang'), 'en-US'); await page.route('**/me/preferences', (route) => route.fulfill({ status: 503, contentType: 'application/json', body: '{"error":"fixture unavailable"}' })); await page.getByRole('combobox', { name: 'Interface language' }).selectOption('de-DE'); await page.getByRole('alert').filter({ hasText: 'Unable to save the language.' }).waitFor(); assert.equal(await page.getByRole('combobox', { name: 'Interface language' }).inputValue(), 'en-US'); account = await me(); assert.equal(account.user.ui_language, 'en-US'); assert.deepEqual(account.profile, learningProfile); await page.unroute('**/me/preferences'); const rejected = await page.request.patch(api + '/me/preferences', { headers, data: { ui_language: 'xx-XX' } }); assert.equal(rejected.status(), 400); await patch({ ui_language: original.user.ui_language, target_language: original.profile.target_language, cefr_level: original.profile.cefr_level, daily_goal_minutes: original.profile.daily_goal_minutes }); assert.deepEqual(errors, []); console.log('PASS: catalog parity; language before login; registration/login; account persistence; independent instructions; pronunciation, conversation, admin and legacy controls; unsaved input survives language switches; partial PATCH preserves course/level/goal; rejected saves; no browser errors.'); } finally { await browser.close(); } })().catch((error) => { console.error(error); process.exitCode = 1; });