You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
apps/memos-local-plugin: viewer i18n locale default is untestable under Node (detectDefault reads navigator.language at module init) #2346
Under Node (Vitest / SSR / CI without jsdom), navigator is undefined and localStorage throws (swallowed by the try/catch), so locale is always "en". Any unit test that needs to assert zh-CN strings (e.g. "已连接") can never pass through the real detection path — tests must pin locale.value = "zh" by reaching into the store internals.
Proposal
Make the default injectable / SSR-safe so tests (and SSR hosts) can exercise the detection logic without mutating the exported signal, e.g.:
Summary: Fixes #2346 — apps/memos-local-plugin viewer i18n detectDefault is now injectable so the zh-* → "zh" branch is testable under Node/Vitest/SSR.
Root cause.viewer/src/stores/i18n.ts defined detectDefault() as a module-private function that read navigator.language and localStorage directly. Under Node these globals are absent (localStorage access throws, swallowed by try/catch), so the module-level locale signal always initialised to "en" and the zh-* branch was unreachable from unit tests. Any test needing zh strings had to reach into the store and mutate locale.value = "zh" (see tests/unit/viewer/restart-copy.test.ts for the current workaround).
Fix. Exported detectDefault(opts?: { navLanguage?: string; storage?: Pick<Storage, "getItem"> | null; storageKey?: string }) and a DetectDefaultOptions interface. All fields are optional; omitted fields fall back to the ambient browser globals (globalThis.localStorage, navigator.language) exactly as before, so export const locale = signal<Locale>(detectDefault()) at the module top level behaves byte-for-byte identically in the browser. Tests and SSR hosts can now inject a Map-backed storage stub or an explicit navLanguage and drive both branches without touching global state.
Verification.
New unit test file tests/unit/viewer/i18n-detect-default.test.ts covers 13 cases: injected zh/en saved values, ambient-global fallbacks, custom storageKey, throwing-storage fallback, case-insensitive zh-* matching, and precedence of injected options over ambient globals. All 13 pass (0 skipped).
Full viewer test suite (12 files, 69 tests) passes; the pre-existing restart-copy.test.ts workaround still works unchanged.
npm run lint (== tsc --noEmit) passes cleanly.
Whole-repo baseline: the unrelated MemOS unit-test failures under tests/unit/memory/, tests/unit/storage/, etc. exist on dev-v2.0.30 before this change and are outside the scope of the plugin viewer fix — this commit adds 13 passing tests and introduces zero new failures.
Base branch:main Branch:bugfix/autodev-2346-20260907141702290 Commit:38a5931 PR:#2347 Assigned to:@wustzdy Reviewers:@whipser030, @hijzy
Activity
Memtensor-AI commentedon Sep 7, 2026
🤖 AutoDev has picked up this issue and started working on it.
Task ID:
f0c5eb3814b42e21Working branch:
bugfix/autodev-2346-20260907141702290Target branch:
latest dev* branchWorkflow: opsp (analysis → coding → testing → PR)
I will post the PR link here once done. If I need more information, I will ask in the comments.
fix(viewer/i18n): make detectDefault injectable for Node/SSR tests
Memtensor-AI commentedon Sep 7, 2026
✅ AutoDev task
f0c5eb3814b42e21completed.Summary: Fixes #2346 — apps/memos-local-plugin viewer i18n
detectDefaultis now injectable so thezh-*→"zh"branch is testable under Node/Vitest/SSR.Root cause.
viewer/src/stores/i18n.tsdefineddetectDefault()as a module-private function that readnavigator.languageandlocalStoragedirectly. Under Node these globals are absent (localStorage access throws, swallowed by try/catch), so the module-levellocalesignal always initialised to"en"and thezh-*branch was unreachable from unit tests. Any test needing zh strings had to reach into the store and mutatelocale.value = "zh"(seetests/unit/viewer/restart-copy.test.tsfor the current workaround).Fix. Exported
detectDefault(opts?: { navLanguage?: string; storage?: Pick<Storage, "getItem"> | null; storageKey?: string })and aDetectDefaultOptionsinterface. All fields are optional; omitted fields fall back to the ambient browser globals (globalThis.localStorage,navigator.language) exactly as before, soexport const locale = signal<Locale>(detectDefault())at the module top level behaves byte-for-byte identically in the browser. Tests and SSR hosts can now inject a Map-backed storage stub or an explicitnavLanguageand drive both branches without touching global state.Verification.
tests/unit/viewer/i18n-detect-default.test.tscovers 13 cases: injected zh/en saved values, ambient-global fallbacks, custom storageKey, throwing-storage fallback, case-insensitive zh-* matching, and precedence of injected options over ambient globals. All 13 pass (0 skipped).restart-copy.test.tsworkaround still works unchanged.npm run lint(==tsc --noEmit) passes cleanly.tests/unit/memory/,tests/unit/storage/, etc. exist ondev-v2.0.30before this change and are outside the scope of the plugin viewer fix — this commit adds 13 passing tests and introduces zero new failures.Base branch:
mainBranch:
bugfix/autodev-2346-20260907141702290Commit:
38a5931PR: #2347
Assigned to: @wustzdy
Reviewers: @whipser030, @hijzy
github-actions commentedon Oct 9, 2026
This issue has been automatically marked as stale due to inactivity.
github-actions commentedon Oct 19, 2026
This issue has been automatically closed due to inactivity.