From f58e361cbe1807eeee93a73c4fc6b516560b4725 Mon Sep 17 00:00:00 2001 From: HuanLinOTO Date: Sun, 23 Aug 2026 16:56:07 +0800 Subject: [PATCH] fix(i18n): guard ctx.get call so absent better-locale is truly no-op MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sidebar.tsx read ctx.get('betterLocale') unconditionally. In real cordis hosts ctx.get exists and returns undefined when better-locale is absent — but the unit-test mocks build a minimal ctx without a get method, throwing 'TypeError: ctx.get is not a function' inside render and taking down 7 tests (bottom-auto-terminal + sidebar-crash suites). Add a typeof guard so the optional contract holds even when ctx.get is missing. The activation-order-safe ctx.effect path in index.tsx is unaffected (real cordis Context, runs once). --- src/client/Sidebar.tsx | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/client/Sidebar.tsx b/src/client/Sidebar.tsx index 77efde3..d7c8c07 100644 --- a/src/client/Sidebar.tsx +++ b/src/client/Sidebar.tsx @@ -197,14 +197,17 @@ export function Sidebar(props: { ctx: Context; store: SidebarStore }) { // re-render. This second uSES reads the better-locale store's `active` // directly, so an override switch fires a full re-render and t() picks // up the new override text. Optional: ctx.get returns undefined when - // better-locale is absent, in which case this is a no-op uSES. + // better-locale is absent (or when ctx is a minimal test mock without + // a `get` method), in which case this is a no-op uSES. type BetterLocaleStore = { readonly active: string | undefined subscribe(listener: () => void): () => void } - const betterLocaleStore = (ctx as unknown as { - get(name: 'betterLocale'): BetterLocaleStore | undefined - }).get('betterLocale') + const betterLocaleStore = typeof ctx.get === 'function' + ? (ctx as unknown as { + get(name: 'betterLocale'): BetterLocaleStore | undefined + }).get('betterLocale') + : undefined const betterLocaleActive = useSyncExternalStore( useMemo(() => { const store = betterLocaleStore