Files
piik/tests/copy.test.ts
T
AAA 2921ebe9c2 refactor(site): retire the 2.x README fixture and what only it kept
This line's guide images already come from production components, and
the maintenance branch owns its 2.x screenshots and README stills. The
fixture under site/film/ui still built the 2.x room here, from
components the 3.0 product's entry no longer reaches, and was the one
thing outside previews and tests that kept them.

Gone: the fixture, its entry, page and license output in the website
build and its include in the scripts' TypeScript project; the 14
modules only it kept (the 2.x status model, presence and viewer
presentation, and the playback bar, room chip, couch, status
indicator, host toolbar and room interactions panel); and what only
they kept, followed until nothing more fell out: the title-frame
catalogues of each locale with their reader and useDocumentTitle, 109
copy keys, one stylesheet and the rules of 130 classes, Row, the TV
frame's indicator slot, the SFU route and relay-volunteer sections of
PlaybackSettings, and the shared-protocol shapes only they read.

Kept: the public film and its build, the product's own title
(usePageTitle with composeDocumentTitle), and every preview section and
test assertion about product components. The playback preview and the
status scenarios existed for the removed modules and went. Four test
files held only removed modules and went, with the title hook's; four
mixed files lost the cases about them.

How it was established: the product's entry reaches 303 files before
and after; the type check proves the copy keys, since every use is
typed against the English catalogue; every removed rule belongs to a
class no source file writes, and no class still written lost every
rule it had, except lr-playback-compact-picture, whose only rules
needed the old bar as an ancestor.

Documents: the three that described the fixture no longer do and keep
the rule that current screenshots use real components. In the visual
language standard two rows of the text-only table described the title
catalogues and the status model and are removed, with the sentence
that named the presence module, so that its links resolve.
2026-11-29 19:38:39 +08:00

246 lines
9.4 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { initialLanguage, rememberLanguage } from "../site/assets/language.js";
import { locales, primaryLanguages, resolveLang } from "../src/client/locales";
import {
isCopyKey,
say,
setCopy,
t,
type CopyKey,
} from "../src/client/ui/copy.ts";
describe("copy catalog", () => {
afterEach(() => {
vi.unstubAllGlobals();
setCopy({ lang: "zh" });
});
it.each([
["zh-CN", "zh"],
["zh-TW", "zh-Hant"],
["zh-HK", "zh-Hant"],
["zh-SG", "zh"],
["zh-Hans", "zh"],
["zh-Hant", "zh-Hant"],
["zh-Hant-HK", "zh-Hant"],
["en-US", "en"],
["ja-JP", "ja"],
[undefined, "en"],
])("uses the primary system language on first use: %s", async (language, expected) => {
vi.resetModules();
vi.stubGlobal("navigator", { language });
const [{ useCopy }, { createElement }, { renderToStaticMarkup }] = await Promise.all([
import("../src/client/ui/copy.ts"),
import("react"),
import("react-dom/server"),
]);
function Selection() {
const { lang } = useCopy();
return lang;
}
expect(renderToStaticMarkup(createElement(Selection))).toBe(expected);
});
it.each([
["zh", "zh"],
["en", "en"],
["zh-Hant", "zh-Hant"],
["ko", "ko"],
])("keeps an explicit %s choice over the system default", async (lang, expected) => {
vi.resetModules();
vi.stubGlobal("navigator", { language: "ja-JP" });
vi.stubGlobal("window", {
localStorage: {
getItem: (key: string) => key === "piik:ui-lang" ? lang : null,
},
addEventListener: () => {},
});
const [{ useCopy }, { createElement }, { renderToStaticMarkup }] = await Promise.all([
import("../src/client/ui/copy.ts"),
import("react"),
import("react-dom/server"),
]);
function Selection() {
const { lang } = useCopy();
return lang;
}
expect(renderToStaticMarkup(createElement(Selection))).toBe(expected);
});
it("renders zh and en for every key", () => {
const zh = t("zh", "join.title");
expect(zh).toBe("加入房间");
expect(t("en", "join.title")).toBe("Join a room");
expect(t("zh", "host.advanced.route.peerOnly")).toBe("隐私模式");
expect(t("en", "host.advanced.route.peerOnly")).toBe("Privacy mode");
expect(isCopyKey("viewer.reconnect")).toBe(true);
expect(isCopyKey("Reconnect media")).toBe(false);
});
it("interpolates variables", () => {
expect(t("zh", "host.title", { name: "阿舟" })).toBe("阿舟 的屏幕");
expect(t("zh", "viewer.title", { name: "$&" })).toBe("$& 的屏幕");
});
it.each(["zh-Hant", "ja", "ko"] as const)("uses English for a missing %s message", lang => {
const key = "common.copied";
const translated = locales[lang].copy[key];
try {
Reflect.deleteProperty(locales[lang].copy, key);
expect(t(lang, key)).toBe(locales.en.copy[key]);
} finally {
locales[lang].copy[key] = translated;
}
});
it("keeps primary catalogs complete and supplied translations valid", () => {
const keys = Object.keys(locales.zh.copy) as CopyKey[];
const placeholders = (value: string) => (value.match(/\{\w+\}/g) ?? []).sort();
for (const lang of primaryLanguages) {
expect(Object.keys(locales[lang].copy).sort(), lang).toEqual([...keys].sort());
}
for (const [lang, locale] of Object.entries(locales)) {
for (const [key, value] of Object.entries(locale.copy)) {
expect(isCopyKey(key), `${lang}: obsolete ${key}`).toBe(true);
expect(value.trim(), `${lang}: ${key}`).not.toBe("");
expect(placeholders(value), `${lang}: ${key}`)
.toEqual(placeholders(locales.zh.copy[key as CopyKey]));
}
for (const pool of [locale.playful.welcome.map(entry => entry.text), locale.playful.waiting]) {
expect(pool.every(text => text.trim().length > 0), lang).toBe(true);
expect(new Set(pool).size, lang).toBe(pool.length);
}
}
});
it("resolves scripts before regions and rejects invalid tags", () => {
expect(resolveLang("zh-Hans-TW")).toBe("zh");
expect(resolveLang("zh-Hant-CN")).toBe("zh-Hant");
expect(resolveLang("ko-KR")).toBe("ko");
expect(resolveLang("not_a_tag")).toBe("en");
expect(resolveLang("fr-CA")).toBe("en");
});
it("synchronizes another tab's language without writing it back", async () => {
vi.resetModules();
let saved = "en";
let onStorage: (event: { key: string }) => void = () => {};
const setItem = vi.fn();
vi.stubGlobal("document", { documentElement: { lang: "en" } });
vi.stubGlobal("window", {
localStorage: { getItem: () => saved, setItem },
addEventListener: (_name: string, listener: typeof onStorage) => { onStorage = listener; },
});
const copy = await import("../src/client/ui/copy");
saved = "zh-Hant";
onStorage({ key: "piik:ui-lang" });
expect(copy.say("common.copied")).toBe(copy.t("zh-Hant", "common.copied"));
expect(document.documentElement.lang).toBe("zh-Hant");
expect(setItem).not.toHaveBeenCalled();
});
it("follows the App window without copying its language as a preference", async () => {
vi.resetModules();
const setItem = vi.fn();
vi.stubGlobal("window", {
localStorage: { getItem: () => "ja", setItem },
addEventListener: () => {},
});
const copy = await import("../src/client/ui/copy");
expect(copy.say("common.copied")).toBe(copy.t("ja", "common.copied"));
copy.applyWindowCopy("en");
expect(copy.say("common.copied")).toBe(copy.t("en", "common.copied"));
expect(copy.hasCopyPreference()).toBe(false);
expect(setItem).not.toHaveBeenCalled();
copy.setCopy({ lang: "zh" });
copy.applyWindowCopy("en");
expect(copy.say("common.copied")).toBe(copy.t("zh", "common.copied"));
expect(copy.hasCopyPreference()).toBe(true);
expect(setItem).toHaveBeenCalledExactlyOnceWith("piik:ui-lang", "zh");
});
it.each(["zh-Hant", "ja", "ko"] as const)("persists and hands off %s", async (lang) => {
vi.resetModules();
const stored = new Map<string, string>([["piik:ui-lang", lang]]);
const registry = await import("../src/client/locales");
vi.stubGlobal("navigator", { language: "en-US" });
vi.stubGlobal("document", { documentElement: { lang: "en" } });
vi.stubGlobal("window", {
localStorage: {
getItem: (key: string) => stored.get(key) ?? null,
setItem: (key: string, value: string) => stored.set(key, value),
},
addEventListener: () => {},
location: new URL("https://site.example/"),
history: { state: null, replaceState: vi.fn() },
});
const copy = await import("../src/client/ui/copy");
const { clientLaunchURL, takeClientLaunchBootstrap } = await import("../src/client/lib/app-launch");
expect(copy.say("common.copied")).toBe(copy.t(lang, "common.copied"));
expect(document.documentElement.lang).toBe(registry.locales[lang].tag);
const presentation = { lang, theme: null };
window.location.hash = new URL(clientLaunchURL("https://site.example/#piik-client=1", presentation)).hash;
expect(takeClientLaunchBootstrap()).toEqual({ ...presentation, explicit: [] });
expect(registry.chineseOrEnglish(lang)).toBe(lang === "zh-Hant" ? "zh" : "en");
const keysBeforeSelection = [...stored.keys()];
copy.setCopy({ lang });
expect(stored.get("piik:ui-lang")).toBe(lang);
expect([...stored.keys()]).toEqual(keysBeforeSelection);
});
it("say() follows the current language", () => {
setCopy({ lang: "zh" });
expect(say("common.copied")).toBe("已复制");
setCopy({ lang: "en" });
expect(say("common.copied")).toBe("Copied");
});
it("keeps the document language aligned with the selected catalog", () => {
const documentElement = { lang: "zh-CN" };
vi.stubGlobal("document", { documentElement });
setCopy({ lang: "en" });
expect(documentElement.lang).toBe("en");
setCopy({ lang: "zh" });
expect(documentElement.lang).toBe("zh-CN");
});
});
describe("website language", () => {
afterEach(() => vi.unstubAllGlobals());
it.each([
["zh-CN", null, "", "zh-CN"],
["zh-TW", null, "", "zh-CN"],
["zh-HK", null, "", "zh-CN"],
["zh-SG", null, "", "zh-CN"],
["zh-Hans", null, "", "zh-CN"],
["zh-Hant", null, "", "zh-CN"],
["zh-Hant-HK", null, "", "zh-CN"],
["en-US", null, "", "en"],
["ja-JP", null, "", "en"],
["ja-JP", "zh-CN", "", "zh-CN"],
["zh-CN", "zh-CN", "?lang=en", "en"],
])("resolves system, saved and linked language: %s/%s/%s", (language, saved, search, expected) => {
vi.stubGlobal("navigator", { language });
vi.stubGlobal("location", { search });
vi.stubGlobal("localStorage", { getItem: () => saved });
expect(initialLanguage()).toBe(expected);
});
it("uses the system default when storage is unavailable", () => {
vi.stubGlobal("navigator", { language: "zh-CN" });
vi.stubGlobal("location", new URL("https://example.test/?preview=1#download"));
const replaceState = vi.fn();
vi.stubGlobal("history", { state: null, replaceState });
vi.stubGlobal("localStorage", {
getItem: () => { throw new Error("blocked"); },
setItem: () => { throw new Error("blocked"); },
});
expect(initialLanguage()).toBe("zh-CN");
expect(() => rememberLanguage("en")).not.toThrow();
expect(String(replaceState.mock.calls[0]?.[2])).toBe("https://example.test/?preview=1&lang=en#download");
});
});