From bd338f86989e048dd0606940ebb73b11f09c81ff Mon Sep 17 00:00:00 2001 From: CommaBot Date: Mon, 23 Nov 2026 22:54:13 +0800 Subject: [PATCH] fix(electron): do not return the translation function over IPC (#15901) The `i18n.changeLanguage` IPC handler returned the result of i18next's `changeLanguage`, which is a translation function. A function cannot be structured-cloned, so every call from the renderer rejected with `DataCloneError` even though the language had changed in the main process. The handler now awaits the change and returns nothing. A unit test calls the handler and checks the language changed and the result can be cloned; on canary it fails with `DataCloneError: (...args) => this.t(...args) could not be cloned.` Found by @keepClamDown in #15616. ## Summary by CodeRabbit * **Bug Fixes** * Language changes continue to apply as requested, while the request no longer returns an internal result. * **Tests** * Added coverage for changing the application language and restoring the default language after testing. Co-authored-by: DarkSky --- .../apps/electron/src/main/handlers.ts | 2 +- .../apps/electron/test/main/handlers.spec.ts | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 packages/frontend/apps/electron/test/main/handlers.spec.ts diff --git a/packages/frontend/apps/electron/src/main/handlers.ts b/packages/frontend/apps/electron/src/main/handlers.ts index e5344006d0..2d28c23f8b 100644 --- a/packages/frontend/apps/electron/src/main/handlers.ts +++ b/packages/frontend/apps/electron/src/main/handlers.ts @@ -28,7 +28,7 @@ export const debugHandlers = { export const i18nHandlers = { changeLanguage: async (_: Electron.IpcMainInvokeEvent, language: string) => { - return I18n.changeLanguage(language); + await I18n.changeLanguage(language); }, }; diff --git a/packages/frontend/apps/electron/test/main/handlers.spec.ts b/packages/frontend/apps/electron/test/main/handlers.spec.ts new file mode 100644 index 0000000000..825a7b3967 --- /dev/null +++ b/packages/frontend/apps/electron/test/main/handlers.spec.ts @@ -0,0 +1,49 @@ +import { afterAll, describe, expect, test, vi } from 'vitest'; + +vi.mock('electron', () => ({ ipcMain: { handle: vi.fn(), on: vi.fn() } })); +vi.mock('../../src/main/auth/handlers', () => ({ authHandlers: {} })); +vi.mock('../../src/main/byok-storage/handlers', () => ({ + byokStorageHandlers: {}, +})); +vi.mock('../../src/main/clipboard', () => ({ clipboardHandlers: {} })); +vi.mock('../../src/main/config-storage', () => ({ + configStorageHandlers: {}, +})); +vi.mock('../../src/main/find-in-page', () => ({ findInPageHandlers: {} })); +vi.mock('../../src/main/import', () => ({ importHandlers: {} })); +vi.mock('../../src/main/logger', () => ({ + getLogFilePath: vi.fn(), + logger: { debug: vi.fn(), error: vi.fn() }, + revealLogFile: vi.fn(), +})); +vi.mock('../../src/main/recording', () => ({ recordingHandlers: {} })); +vi.mock('../../src/main/security-restrictions', () => ({ + checkSource: () => true, +})); +vi.mock('../../src/main/shared-storage', () => ({ + sharedStorageHandlers: {}, +})); +vi.mock('../../src/main/ui/handlers', () => ({ uiHandlers: {} })); +vi.mock('../../src/main/updater', () => ({ updaterHandlers: {} })); +vi.mock('../../src/main/windows-manager/popup', () => ({ popupHandlers: {} })); +vi.mock('../../src/main/worker/handlers', () => ({ workerHandlers: {} })); + +import { I18n } from '@affine/i18n'; + +import { i18nHandlers } from '../../src/main/handlers'; + +describe('i18n IPC handlers', () => { + afterAll(async () => { + await I18n.changeLanguage('en'); + }); + + test('changeLanguage switches the language and returns a cloneable result', async () => { + const result = await i18nHandlers.changeLanguage( + {} as Electron.IpcMainInvokeEvent, + 'zh-Hans' + ); + + expect(I18n.language).toBe('zh-Hans'); + expect(() => structuredClone(result)).not.toThrow(); + }); +});