mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-07-08 08:27:39 +08:00
Main previously imported all 12 renderer translation JSONs by relative path to
build its t() table — a main->renderer boundary violation that also inlined
~3.86 MB of translations the main process never uses. Main now owns a small,
independent catalog under src/main/i18n (~59 keys: app menu, tray, dialogs,
context menu, the OAuth callback page and a few shared strings), statically
imported for all 12 languages (~48 KB total).
- src/main/utils/language.ts -> src/main/i18n/index.ts (git mv): swap the renderer
JSON imports for the local catalog, add an en-US fallback to t(), and narrow the
now-internal `locales` export. AppMenuService reads getI18n() instead of the
locales map; the OAuth callback drops its duplicate translator for t().
- src/main/i18n/{locales,translate}/*.json: the main catalog, extracted from the
renderer catalog (zero translation loss), aligned and sorted across all 12 files.
- Renderer catalog: delete the now main-exclusive keys (appMenu.*, tray.*, dialog.*,
settings.mcp.oauth.callback.*, common.{inspect,paste,cut},
agent.session.workspace_status.{missing,not_directory}).
- Composer delete buttons used t('appMenu.delete') by mistake; switch to
t('common.delete') (byte-identical in all 12 languages) so appMenu is fully
main-owned.
- readErrorMessage: take a pre-translated `fallback` string instead of an i18next
key, and move it from @shared/ai into src/main/ai/provider/custom (main-only after
the change); its 6 provider call sites pass main's t(...).
- Tooling: check-i18n now validates the main catalog's 12 files plus a literal-t()
key-coverage check for main sources; sync-i18n and auto-translate-i18n cover both
catalogs. eslint bans relative **/renderer/** in main/preload; TopicNamingService
test reads the renderer catalog from disk instead of importing it.
- Docs: main-process-architecture.md records i18n as a governed top-level expansion
and closes the resolved deviation; CLAUDE.md lists the new directory.
213 lines
5.6 KiB
TypeScript
213 lines
5.6 KiB
TypeScript
import { vi } from 'vitest'
|
|
|
|
// Mock LoggerService globally for main process tests
|
|
vi.mock('@logger', async () => {
|
|
const { MockMainLoggerService, mockMainLoggerService } = await import('./__mocks__/MainLoggerService')
|
|
return {
|
|
LoggerService: MockMainLoggerService,
|
|
loggerService: mockMainLoggerService
|
|
}
|
|
})
|
|
|
|
// Mock service modules globally for main tests.
|
|
// These mocks export both the class and instance names for backward compat.
|
|
vi.mock('@main/data/PreferenceService', async () => {
|
|
const { MockMainPreferenceServiceExport } = await import('./__mocks__/main/PreferenceService')
|
|
return {
|
|
...MockMainPreferenceServiceExport,
|
|
PreferenceService: vi.fn() // Class export for serviceRegistry
|
|
}
|
|
})
|
|
|
|
vi.mock('@main/data/DataApiService', async () => {
|
|
const { MockMainDataApiServiceExport } = await import('./__mocks__/main/DataApiService')
|
|
return {
|
|
...MockMainDataApiServiceExport,
|
|
DataApiService: vi.fn() // Class export for serviceRegistry
|
|
}
|
|
})
|
|
|
|
vi.mock('@main/data/CacheService', async () => {
|
|
const { MockMainCacheServiceExport } = await import('./__mocks__/main/CacheService')
|
|
return {
|
|
...MockMainCacheServiceExport,
|
|
CacheService: vi.fn() // Class export for serviceRegistry
|
|
}
|
|
})
|
|
|
|
vi.mock('@main/data/db/DbService', async () => {
|
|
const { MockMainDbServiceExport } = await import('./__mocks__/main/DbService')
|
|
return {
|
|
...MockMainDbServiceExport,
|
|
DbService: vi.fn() // Class export for serviceRegistry
|
|
}
|
|
})
|
|
|
|
// Mock application globally - provides type-safe service access via application.get()
|
|
vi.mock('@application', async () => {
|
|
const { mockApplicationFactory } = await import('./__mocks__/main/application')
|
|
return mockApplicationFactory()
|
|
})
|
|
|
|
// Mock electron modules that are commonly used in main process
|
|
vi.mock('electron', () => {
|
|
const mock = {
|
|
app: {
|
|
getPath: vi.fn((key: string) => {
|
|
switch (key) {
|
|
case 'userData':
|
|
return '/mock/userData'
|
|
case 'temp':
|
|
return '/mock/temp'
|
|
case 'logs':
|
|
return '/mock/logs'
|
|
default:
|
|
return '/mock/unknown'
|
|
}
|
|
}),
|
|
getVersion: vi.fn(() => '1.0.0'),
|
|
getLocale: vi.fn(() => 'en-US')
|
|
},
|
|
ipcMain: {
|
|
handle: vi.fn(),
|
|
on: vi.fn(),
|
|
once: vi.fn(),
|
|
removeHandler: vi.fn(),
|
|
removeListener: vi.fn(),
|
|
removeAllListeners: vi.fn()
|
|
},
|
|
BrowserWindow: vi.fn(),
|
|
dialog: {
|
|
showErrorBox: vi.fn(),
|
|
showMessageBox: vi.fn(),
|
|
showOpenDialog: vi.fn(),
|
|
showSaveDialog: vi.fn()
|
|
},
|
|
shell: {
|
|
openExternal: vi.fn(),
|
|
showItemInFolder: vi.fn()
|
|
},
|
|
session: {
|
|
defaultSession: {
|
|
clearCache: vi.fn(),
|
|
clearStorageData: vi.fn(),
|
|
webRequest: {
|
|
onBeforeSendHeaders: vi.fn()
|
|
}
|
|
}
|
|
},
|
|
webContents: {
|
|
getAllWebContents: vi.fn(() => [])
|
|
},
|
|
systemPreferences: {
|
|
getMediaAccessStatus: vi.fn(),
|
|
askForMediaAccess: vi.fn()
|
|
},
|
|
nativeTheme: {
|
|
themeSource: 'system',
|
|
shouldUseDarkColors: false,
|
|
on: vi.fn(),
|
|
removeListener: vi.fn()
|
|
},
|
|
screen: {
|
|
getPrimaryDisplay: vi.fn(),
|
|
getAllDisplays: vi.fn()
|
|
},
|
|
Notification: vi.fn(),
|
|
net: {
|
|
fetch: vi.fn()
|
|
}
|
|
}
|
|
|
|
return { __esModule: true, ...mock, default: mock }
|
|
})
|
|
|
|
// Mock Winston for LoggerService dependencies
|
|
vi.mock('winston', () => ({
|
|
createLogger: vi.fn(() => ({
|
|
log: vi.fn(),
|
|
error: vi.fn(),
|
|
warn: vi.fn(),
|
|
info: vi.fn(),
|
|
debug: vi.fn(),
|
|
level: 'info',
|
|
on: vi.fn(),
|
|
end: vi.fn()
|
|
})),
|
|
format: {
|
|
combine: vi.fn(),
|
|
splat: vi.fn(),
|
|
timestamp: vi.fn(),
|
|
errors: vi.fn(),
|
|
json: vi.fn()
|
|
},
|
|
transports: {
|
|
Console: vi.fn(),
|
|
File: vi.fn()
|
|
}
|
|
}))
|
|
|
|
// Mock winston-daily-rotate-file
|
|
vi.mock('winston-daily-rotate-file', () => {
|
|
return vi.fn().mockImplementation(() => ({
|
|
on: vi.fn(),
|
|
log: vi.fn()
|
|
}))
|
|
})
|
|
|
|
// Mock electron-store to avoid file system operations
|
|
vi.mock('electron-store', () => {
|
|
return {
|
|
default: vi.fn().mockImplementation(() => ({
|
|
get: vi.fn((key: string, defaultValue?: unknown) => defaultValue),
|
|
set: vi.fn(),
|
|
delete: vi.fn(),
|
|
clear: vi.fn(),
|
|
has: vi.fn(() => false),
|
|
store: {}
|
|
}))
|
|
}
|
|
})
|
|
|
|
// Mock Node.js modules
|
|
//
|
|
// The fs/os/path modules are passed through to their real implementations
|
|
// (`...await vi.importActual(...)`) so that third-party libraries such as
|
|
// `drizzle-orm/better-sqlite3/migrator` can read files from disk. Historically these
|
|
// modules were replaced wholesale with vi.fn() stubs, which caused any code
|
|
// reading migration files, tmp directories, or real paths to silently break.
|
|
//
|
|
// Individual tests that require controlled fs/os/path behaviour should spy
|
|
// on the specific method(s) they need (`vi.spyOn(fs, 'existsSync')`) or
|
|
// declare a local `vi.mock(..., factory)` inside the test file.
|
|
//
|
|
// `os.homedir()` is still stubbed to `/mock/home` because many existing
|
|
// tests assume this deterministic value when building expected paths.
|
|
vi.mock('node:os', async () => {
|
|
const actual = await vi.importActual<typeof import('node:os')>('node:os')
|
|
return {
|
|
...actual,
|
|
homedir: vi.fn(() => '/mock/home'),
|
|
default: {
|
|
...actual,
|
|
homedir: () => '/mock/home'
|
|
}
|
|
}
|
|
})
|
|
|
|
vi.mock('node:path', async () => {
|
|
const actual = await vi.importActual<typeof import('node:path')>('node:path')
|
|
return {
|
|
...actual,
|
|
default: actual
|
|
}
|
|
})
|
|
|
|
vi.mock('node:fs', async () => {
|
|
const actual = await vi.importActual<typeof import('node:fs')>('node:fs')
|
|
return {
|
|
...actual,
|
|
default: actual
|
|
}
|
|
})
|