Skip to content

apps/memos-local-plugin: viewer i18n locale default is untestable under Node (detectDefault reads navigator.language at module init) #2346

Closed as not planned

Description

@chiefmojo

Summary

apps/memos-local-plugin/viewer/src/stores/i18n.ts computes the module-level default locale at import time:

function detectDefault(): Locale {
  try {
    const saved = localStorage.getItem(STORAGE_KEY);
    if (saved === "en" || saved === "zh") return saved;
  } catch {
    // ignore
  }
  const nav = (typeof navigator !== "undefined" && navigator.language) || "en";
  return nav.toLowerCase().startsWith("zh") ? "zh" : "en";
}

export const locale = signal<Locale>(detectDefault());

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.:

export function detectDefault(opts?: { navLanguage?: string; storageKey?: string }): Locale

or lazy-evaluate detectDefault so a test can stub globalThis.navigator before first read.

Either keeps the browser behavior identical while letting tests cover the zh-* → zh branch that currently ships untested.

Happy to send a small PR if you'd like — we have one ready downstream.

Activity

  1. added
    ai:taskDispatched to AI coding agent | 已派发给 AI 编码任务
    status:in-progressSomeone or AI is working on it | 人工或 AI 正在处理
    types:bugSomething isn't working | 功能异常
    on Sep 7, 2026
  2. Memtensor-AI commented on Sep 7, 2026

    @Memtensor-AI
    Collaborator

    🤖 AutoDev has picked up this issue and started working on it.

    Task ID: f0c5eb3814b42e21
    Working branch: bugfix/autodev-2346-20260907141702290
    Target branch: latest dev* branch
    Workflow: 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.

  3. self-assigned this
    on Sep 7, 2026
  4. added a commit that references this issue on Sep 7, 2026
    38a5931
  5. Memtensor-AI commented on Sep 7, 2026

    @Memtensor-AI
    Collaborator

    ✅ AutoDev task f0c5eb3814b42e21 completed.

    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

  6. added
    ai:testingAI agent is running tests | AI 正在运行测试
    and removed
    ai:codingAI agent is coding | AI 正在编码
    on Sep 7, 2026
  7. github-actions commented on Oct 9, 2026

    @github-actions
    Contributor

    This issue has been automatically marked as stale due to inactivity.

  8. added
    status:staleAuto-marked stale after 30d inactivity; closes 7d later unless exempt.
    on Oct 9, 2026
  9. github-actions commented on Oct 19, 2026

    @github-actions
    Contributor

    This issue has been automatically closed due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

ai:taskDispatched to AI coding agent | 已派发给 AI 编码任务ai:testingAI agent is running tests | AI 正在运行测试area:pluginOpenClaw & Hermesstatus:in-progressSomeone or AI is working on it | 人工或 AI 正在处理status:staleAuto-marked stale after 30d inactivity; closes 7d later unless exempt.types:bugSomething isn't working | 功能异常

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions