Files
piik/tests/copy.test.ts
T
AAA 9925486cec feat(app): improve startup and media recovery (#478)
This compatible update improves quality recovery after congestion and stream changes, and lets an approved Site become the App's startup page. It also aligns responsive controls and external brand assets while preserving the in-page mascot and illustrations.

The existing media allocator, room graph and authorization owners remain in place. The scoped LiveKit demand-input patch is pinned and submitted upstream in livekit/livekit#5016; its removal condition is documented. No wire or storage version changes.

## Validation

- Web type-check, unit tests and production build; Go core/native acceptance and website build.
- Focused controlled recovery/relay/NAT evidence reused from this phase, plus final integrated lifecycle checks.
- Independent media and UI reviews; corrected the ready App button's stale passive styling.
- Repository hygiene, documentation links, responsive launcher/theme previews and package icon propagation.
- Physical macOS/Linux capture and unmatched reporter cases remain documented; controlled tests do not establish a public connection success rate.

## Release notes

### 中文

这是一次兼容 2.x 的功能与维护更新,重点改善网络波动后的画质恢复,并让常用站点的 App 启动流程更方便。

- **更快恢复画质:** 修复部分 App 分享与服务器中转线路在网络恢复后仍长时间保持低码率的问题;调整 Windows 原生编码,减少短暂波动造成的分辨率下降,并加快恢复。
- **更稳定的中继播放:** 修复原生中继在分辨率变化、画面数据不完整和换源时的恢复处理,以及延迟关键帧请求被丢失的问题。
- **启动时打开常用站点:** 可以为已授权站点加星,下次启动 App 时直接打开它;仍可通过网页 App 按钮或终端地址返回启动页管理授权和选择模式。
- **简化连接设置:** NAT 穿透增强在能力可用时自动启用;高级用户可通过 `PIIK_STUN_URLS` 指定 App 公网邀请使用的 STUN 服务。
- **界面与图标打磨:** 修正窄屏房间工具栏、昵称编辑和浮层排版,精简启动页说明,统一官网与应用的语言、深浅色控件,并更新 App 图标与网站 favicon。页面内的 mascot 和插画保持原有风格。
- **补充连接排查指引:** 更清楚地区分公网邀请服务、站点访问与媒体连接问题。

本次更新不改变 2.x 通信协议或数据库格式。浏览器采集在部分网络波动后仍可能恢复较慢;此次修复不代表所有网络和设备上的画质问题均已消除。

### English

This compatible 2.x feature and maintenance update improves quality recovery after network disturbances and makes it easier to launch the App with your usual Site.

- **Faster quality recovery:** Fixed cases where native App sharing or server-relayed media stayed at a low bitrate after bandwidth returned. Tuned Windows native encoding to reduce resolution drops during brief disturbances and recover sooner.
- **More reliable relay playback:** Fixed native relay recovery across resolution changes, incomplete frames and source replacement, and preserved delayed keyframe requests until they can be fulfilled.
- **Open your usual Site on startup:** Star an approved Site to open it when the App starts. The page's App button and the terminal's launcher address still provide access to approval management and room modes.
- **Simpler connection settings:** Available NAT traversal enhancement now activates automatically. Advanced users can set `PIIK_STUN_URLS` to choose the STUN services used by App public invitations.
- **Interface and icon polish:** Fixed narrow-screen room controls, nickname editing and overlays; simplified launcher guidance; aligned website language and theme controls with the App; and refreshed App icons and website favicons. In-page mascots and illustrations retain their existing appearance.
- **Clearer connection troubleshooting:** Updated guidance distinguishes public-invitation service failures, Site access and media connectivity.

This update preserves the 2.x communication protocols and database format. Browser capture can still recover slowly after some network disturbances; these fixes do not resolve every quality issue on every network or device.
2026-10-16 21:56:49 +08:00

229 lines
8.9 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { initialLanguage, rememberLanguage } from "../site/assets/language.js";
import { locales, visualTitleFrames } from "../src/client/locales";
import {
getTitleFrames,
isCopyKey,
say,
setCopy,
t,
type CopyKey,
} from "../src/client/ui/copy.ts";
describe("copy catalog", () => {
afterEach(() => {
vi.unstubAllGlobals();
setCopy({ lang: "zh", vis: false });
});
it.each([
["zh-CN", "zh:false"],
["zh-TW", "zh:false"],
["zh-HK", "zh:false"],
["zh-SG", "zh:false"],
["zh-Hans", "zh:false"],
["zh-Hant", "zh:false"],
["zh-Hant-HK", "zh:false"],
["en-US", "en:false"],
["ja-JP", "en:false"],
[undefined, "en:false"],
])("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, vis } = useCopy();
return `${lang}:${vis}`;
}
expect(renderToStaticMarkup(createElement(Selection))).toBe(expected);
});
it.each([
["zh", "text", "zh:false"],
["en", "vis", "en:true"],
])("keeps an explicit %s/%s choice over the system default", async (lang, mode, expected) => {
vi.resetModules();
vi.stubGlobal("navigator", { language: "ja-JP" });
vi.stubGlobal("window", {
localStorage: {
getItem: (key: string) => key === "piik:ui-lang" ? lang : mode,
},
addEventListener: () => {},
});
const [{ useCopy }, { createElement }, { renderToStaticMarkup }] = await Promise.all([
import("../src/client/ui/copy.ts"),
import("react"),
import("react-dom/server"),
]);
function Selection() {
const { lang, vis } = useCopy();
return `${lang}:${vis}`;
}
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("keeps every registered translation complete with matching placeholders", () => {
const keys = Object.keys(locales.zh.copy) as CopyKey[];
const placeholders = (value: string) => (value.match(/\{\w+\}/g) ?? []).sort();
for (const [lang, locale] of Object.entries(locales)) {
expect(Object.keys(locale.copy).sort(), lang).toEqual([...keys].sort());
for (const key of keys) {
expect(locale.copy[key].trim(), `${lang}: ${key}`).not.toBe("");
expect(placeholders(locale.copy[key]), `${lang}: ${key}`)
.toEqual(placeholders(locales.zh.copy[key]));
}
for (const frames of Object.values(locale.titleFrames)) {
expect(frames.label.trim().length, lang).toBeGreaterThan(0);
expect(frames.variations.every((frame: string) => frame.trim().length > 0), lang).toBe(true);
}
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("carries a contributed locale through selection and App handoff", async () => {
vi.resetModules();
const registry = await import("../src/client/locales");
// Exercise registration without shipping an unreviewed translation.
Object.assign(registry.locales, {
fr: { ...registry.locales.en, name: "Français", short: "FR", tag: "fr" },
});
try {
const stored = new Map<string, string>([["piik:ui-lang", "fr"]]);
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/session");
expect(registry.resolveLang("FR-ca")).toBe("fr");
expect(registry.resolveLang("zh-CN")).toBe("zh");
expect(registry.isLang("toString")).toBe(false);
expect(copy.currentLang()).toBe("fr");
expect(document.documentElement.lang).toBe("fr");
expect(copy.t(copy.currentLang(), "common.host")).toBe("Host");
const presentation = { lang: copy.currentLang(), vis: false, theme: null };
window.location.hash = new URL(clientLaunchURL("https://site.example/#piik-client=1", presentation)).hash;
expect(takeClientLaunchBootstrap().presentation).toEqual({ ...presentation, explicit: [] });
expect(registry.consoleLanguage(presentation.lang, false)).toBe("en");
expect(registry.consoleLanguage(presentation.lang, true)).toBe("vis");
expect(registry.consoleLanguage("zh", false)).toBe("zh");
} finally {
Reflect.deleteProperty(registry.locales, "fr");
vi.resetModules();
}
});
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 title variations in the selected content catalog", () => {
const zhHost = getTitleFrames("zh", false, "hostActive");
const enViewer = getTitleFrames("en", false, "viewerActive");
const visualViewer = getTitleFrames("zh", true, "viewerActive");
expect(zhHost.label).toBe("分享中");
expect(zhHost.variations).toContain("小电视上工");
expect(enViewer.label).toBe("Watching");
expect(enViewer.variations).toContain("Popcorn ready");
expect(visualViewer.label).toBe("📺");
expect(visualViewer.variations).toContain("🍿");
expect(getTitleFrames("zh", false, "hostIdle").variations).toContain("天线在打盹");
expect(getTitleFrames("en", false, "viewerWaiting").variations).toContain(
"Couch saved you a spot",
);
expect(getTitleFrames("zh", true, "hostReady").variations).toContain("🍵");
});
it("keeps actionable titles fixed and lets playful pools vary independently", () => {
const playful = new Set([
"hostActive", "viewerActive", "hostStarting", "hostReady", "hostIdle", "viewerWaiting",
]);
for (const catalog of [locales.zh.titleFrames, locales.en.titleFrames, visualTitleFrames]) {
for (const [state, frames] of Object.entries(catalog)) {
if (!playful.has(state)) expect(frames.variations, state).toEqual([]);
expect(new Set(frames.variations).size, state).toBe(frames.variations.length);
}
}
});
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", { search: "" });
vi.stubGlobal("localStorage", {
getItem: () => { throw new Error("blocked"); },
setItem: () => { throw new Error("blocked"); },
});
expect(initialLanguage()).toBe("zh-CN");
expect(() => rememberLanguage("en")).not.toThrow();
});
});