import { expect, test, type Page } from "./utils/test"; import { getTenantCredentials, loginAsTenantUser, tenantSettingsProfilePath } from "./utils/auth"; import { expectNoCriticalA11yViolations } from "./utils/axe"; import { expectNoHorizontalOverflow, narrowMobileViewport } from "./utils/mobile"; import { escapeRegExp } from "./utils/navigation"; import { skipOrFailWhenE2eFixtureMissing, skipOrFailWhenE2eInputsMissing } from "./utils/readiness"; const credentials = getTenantCredentials(); const dashboardPath = process.env.E2E_TENANT_DASHBOARD_PATH ?? "/app/dashboard"; const supportedLocales = new Set(["de", "en"]); test.describe("authenticated mobile locale persistence", () => { test.use({ viewport: narrowMobileViewport, isMobile: true, hasTouch: true }); test.beforeEach(() => { skipOrFailWhenE2eInputsMissing(["E2E_TENANT_EMAIL", "E2E_TENANT_PASSWORD"], "Authenticated mobile locale inputs not configured"); }); test("persists an authenticated locale switch across mobile shell navigation", async ({ page }) => { await loginAsTenantUser(page, credentials!); await page.goto(dashboardPath, { waitUntil: "domcontentloaded" }); await expect(page).toHaveURL(new RegExp(`${escapeRegExp(dashboardPath)}(?:[?#].*)?$`, "u")); await expectNoHorizontalOverflow(page); const initialLocale = await currentHtmlLocale(page); if (!supportedLocales.has(initialLocale)) { skipOrFailWhenE2eFixtureMissing(`Authenticated session resolved unsupported locale ${initialLocale}`); } const targetLocale = initialLocale === "de" ? "en" : "de"; try { await setAuthenticatedLocale(page, targetLocale, dashboardPath); await page.goto(dashboardPath, { waitUntil: "domcontentloaded" }); await expect(page).toHaveURL(new RegExp(`${escapeRegExp(dashboardPath)}(?:[?#].*)?$`, "u")); await expectAuthenticatedLocaleMobileReadinessPage(page, targetLocale); await page.reload({ waitUntil: "domcontentloaded" }); await expect(page).toHaveURL(new RegExp(`${escapeRegExp(dashboardPath)}(?:[?#].*)?$`, "u")); await expectAuthenticatedLocaleMobileReadinessPage(page, targetLocale); const mobileSidebar = page.locator('[data-ui-id="ui.surface.app.sidebar.mobile"], [data-mobile="true"]').first(); await expect(mobileSidebar).toBeHidden(); await page.locator('[data-ui-id="ui.action.shell.sidebarTrigger"]').click(); await expect(mobileSidebar).toBeVisible(); await expect(mobileSidebar.locator('[data-ui-id="ui.nav.app.dashboard"]').first()).toBeVisible(); const settingsLink = mobileSidebar.locator('[data-ui-id="ui.nav.app.settings"]').first(); await expect(settingsLink).toBeVisible(); await settingsLink.click(); await expect(page).toHaveURL(new RegExp(`${escapeRegExp(tenantSettingsProfilePath(dashboardPath))}(?:[?#].*)?$`, "u")); await expect(mobileSidebar).toBeHidden(); await expectAuthenticatedLocaleMobileReadinessPage(page, targetLocale); } finally { await restoreLocale(page, initialLocale); } }); }); async function currentHtmlLocale(page: Page): Promise { return (await page.locator("html").getAttribute("lang")) ?? ""; } async function setAuthenticatedLocale(page: Page, locale: string, redirectPath: string): Promise { const csrf = await page.locator('meta[name="csrf-token"]').getAttribute("content"); expect(csrf, "expected an authenticated csrf token before changing locale").toBeTruthy(); const result = await page.evaluate( async ({ csrfToken, lng, redirect }) => { const body = new URLSearchParams({ action: "setLocale", csrf: csrfToken, lng, redirect }); const response = await fetch("/preferences", { method: "POST", headers: { "content-type": "application/x-www-form-urlencoded" }, body, credentials: "same-origin", }); return { ok: response.ok, status: response.status, url: response.url }; }, { csrfToken: csrf ?? "", lng: locale, redirect: redirectPath } ); expect(result.ok, `setLocale request failed with ${result.status} at ${result.url}`).toBe(true); } async function expectAuthenticatedLocaleMobileReadinessPage(page: Page, locale: string): Promise { await expect(page.locator("html")).toHaveAttribute("lang", locale); await expect(page.locator("html")).toHaveAttribute("dir", "ltr"); await expect(page.locator('[data-ui-id="ui.action.shell.sidebarTrigger"]').first()).toBeVisible(); await expectNoHorizontalOverflow(page); await expectNoCriticalA11yViolations(page); } async function restoreLocale(page: Page, locale: string): Promise { if (!supportedLocales.has(locale) || page.isClosed()) { return; } await page.goto(dashboardPath, { waitUntil: "domcontentloaded" }); if (/\/login(?:[?#].*)?$/u.test(new URL(page.url()).pathname)) { return; } await setAuthenticatedLocale(page, locale, dashboardPath); }