Files
cherry-studio/tests/main.setup.ts
fullex 40f9601379 refactor: Unified Logger / 统一日志管理 (#8207)
* Revert "feat: optimize minapp cache with LRU (#8160)"

This reverts commit f0043b4be5.

* feat: integrate logger service and enhance logging throughout the application

- Added a new LoggerService to standardize logging across the application.
- Replaced console.error and console.warn calls with logger methods for improved consistency and error tracking.
- Introduced a new IPC channel for logging messages to the main process.
- Updated various components and services to utilize the new logging system, enhancing error handling and debugging capabilities.

* refactor: enhance logging and error handling across various components

- Integrated the LoggerService for consistent logging throughout the application.
- Updated multiple components and services to utilize the new logging system, improving error tracking and debugging capabilities.
- Refactored file handling and error management in several services to enhance reliability and clarity.
- Improved the structure and readability of the codebase by removing redundant checks and simplifying logic.

* chore: update TypeScript configuration and enhance test setup

- Added test mock paths to tsconfig.web.json for improved test coverage.
- Configured Vitest to include a setup file for main tests, ensuring consistent test environment.
- Updated IPC logger context for better clarity in logging.
- Enhanced LoggerService to handle undefined values gracefully.
- Mocked LoggerService globally in renderer tests to streamline testing process.

* refactor: standardize logging across ProxyManager and ReduxService

- Replaced instances of Logger with logger for consistent logging implementation.
- Improved logging clarity in ProxyManager's configureProxy method and ReduxService's state handling.
- Enhanced error logging in ReduxService to align with the new logging system.

* refactor: reorganize LoggerService for improved clarity and consistency

- Moved the definition of SYSTEM_INFO, APP_VERSION, and DEFAULT_LEVEL to enhance code organization.
- Simplified the getIsDev function in the renderer LoggerService for better readability.
- Updated logging conditions to ensure messages are logged correctly based on context.

* docs: add usage instructions for LoggerService and clean up logging code

- Included important usage instructions for LoggerService in both English and Chinese.
- Commented out the console transport in LoggerService to streamline logging.
- Improved logging message formatting in MCPService for clarity.
- Removed redundant logging statements in SelectionService to enhance code cleanliness.

* refactor: update LoggerService documentation paths and enhance logging implementation

- Changed the documentation paths for LoggerService usage instructions to `docs/technical/how-to-use-logger-en.md` and `docs/technical/how-to-use-logger-zh.md`.
- Replaced console logging with the loggerService in various components, including `MCPSettings`, `BlockManager`, and multiple callback files, to ensure consistent logging practices across the application.
- Improved the clarity and context of log messages for better debugging and monitoring.

* docs: emphasize logger usage guidelines in documentation

- Added a note in both English and Chinese documentation to discourage the use of `console.xxx` for logging unless necessary, promoting consistent logging practices across the application.
2025-07-18 09:40:56 +08:00

140 lines
3.0 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 electron modules that are commonly used in main process
vi.mock('electron', () => ({
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')
},
ipcMain: {
handle: vi.fn(),
on: vi.fn(),
once: vi.fn(),
removeHandler: 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()
}
},
webContents: {
getAllWebContents: vi.fn(() => [])
},
systemPreferences: {
getMediaAccessStatus: vi.fn(),
askForMediaAccess: vi.fn()
},
screen: {
getPrimaryDisplay: vi.fn(),
getAllDisplays: vi.fn()
},
Notification: vi.fn()
}))
// 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 Node.js modules
vi.mock('node:os', () => ({
platform: vi.fn(() => 'darwin'),
arch: vi.fn(() => 'x64'),
version: vi.fn(() => '20.0.0'),
cpus: vi.fn(() => [{ model: 'Mock CPU' }]),
totalmem: vi.fn(() => 8 * 1024 * 1024 * 1024) // 8GB
}))
vi.mock('node:path', async () => {
const actual = await vi.importActual('node:path')
return {
...actual,
join: vi.fn((...args: string[]) => args.join('/')),
resolve: vi.fn((...args: string[]) => args.join('/'))
}
})
vi.mock('node:fs', () => ({
promises: {
access: vi.fn(),
readFile: vi.fn(),
writeFile: vi.fn(),
mkdir: vi.fn(),
readdir: vi.fn(),
stat: vi.fn(),
unlink: vi.fn(),
rmdir: vi.fn()
},
existsSync: vi.fn(),
readFileSync: vi.fn(),
writeFileSync: vi.fn(),
mkdirSync: vi.fn(),
readdirSync: vi.fn(),
statSync: vi.fn(),
unlinkSync: vi.fn(),
rmdirSync: vi.fn(),
createReadStream: vi.fn(),
createWriteStream: vi.fn()
}))