Files
DeepTutor/web/tests/app-shell-language-bootstrap.test.ts
Dongyu bf76ccfd83 fix(web): honour the saved interface language on first load (#730)
The interface language is persisted in the backend's ui settings, but only the settings route ever read it into state — so /home and every other page started in English until the user picked the language again in that browser.

The app shell now adopts the saved value once after mount, and only when the browser has recorded no choice of its own, so a local selection still wins. It goes through apiFetch (works with auth enabled), leaves the routes statically prerendered, and keeps the zh locale a lazy chunk.

Also replaces the duplicated "New conversation" sentinel check with a shared helper — the chat header showed the untranslated English sentinel the same way the sidebar did — and adds three composer aria-label keys that were referenced but missing from both catalogs.

Closes #729.
2026-08-03 21:45:40 +08:00

66 lines
2.1 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import {
LANGUAGE_STORAGE_KEY,
hasStoredLanguage,
readStoredLanguage,
} from "../context/app-shell-storage";
/** Minimal localStorage stand-in — the helpers only need get/set. */
function withLocalStorage(entries: Record<string, string>, run: () => void) {
const store = new Map(Object.entries(entries));
const original = (globalThis as { window?: unknown }).window;
(globalThis as { window?: unknown }).window = {
localStorage: {
getItem: (key: string) => store.get(key) ?? null,
setItem: (key: string, value: string) => void store.set(key, value),
},
dispatchEvent: () => true,
};
try {
run();
} finally {
(globalThis as { window?: unknown }).window = original;
}
}
test("an absent choice is distinguishable from an explicit English one", () => {
// readStoredLanguage normalizes both to "en", so the bootstrap cannot use it
// to decide whether the server-side preference may be adopted.
withLocalStorage({}, () => {
assert.equal(hasStoredLanguage(), false);
assert.equal(readStoredLanguage(), "en");
});
withLocalStorage({ [LANGUAGE_STORAGE_KEY]: "en" }, () => {
assert.equal(hasStoredLanguage(), true);
assert.equal(readStoredLanguage(), "en");
});
});
test("a stored choice is reported for either language", () => {
withLocalStorage({ [LANGUAGE_STORAGE_KEY]: "zh" }, () => {
assert.equal(hasStoredLanguage(), true);
assert.equal(readStoredLanguage(), "zh");
});
});
test("an unusable value still counts as a choice and normalizes to English", () => {
withLocalStorage({ [LANGUAGE_STORAGE_KEY]: "fr" }, () => {
assert.equal(hasStoredLanguage(), true);
assert.equal(readStoredLanguage(), "en");
});
});
test("server-side rendering reports no stored choice instead of throwing", () => {
const original = (globalThis as { window?: unknown }).window;
(globalThis as { window?: unknown }).window = undefined;
try {
assert.equal(hasStoredLanguage(), false);
assert.equal(readStoredLanguage(), "en");
} finally {
(globalThis as { window?: unknown }).window = original;
}
});