cherry-studio/tests/main.setup.ts
fullex 8353f331f1 test: update tests to use usePreference hook and improve snapshot consistency
- Refactored tests in MainTextBlock and ThinkingBlock to utilize the usePreference hook for managing user settings.
- Updated snapshots in DraggableVirtualList test to reflect changes in class names.
- Enhanced export tests to ensure proper handling of markdown formatting and citation footnotes.
- Mocked additional dependencies globally for improved test reliability.
2025-09-16 14:07:54 +08:00

174 lines
4.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 PreferenceService globally for main tests
vi.mock('@main/data/PreferenceService', async () => {
const { MockMainPreferenceServiceExport } = await import('./__mocks__/main/PreferenceService')
return MockMainPreferenceServiceExport
})
// Mock DataApiService globally for main tests
vi.mock('@main/data/DataApiService', async () => {
const { MockMainDataApiServiceExport } = await import('./__mocks__/main/DataApiService')
return MockMainDataApiServiceExport
})
// Mock CacheService globally for main tests
vi.mock('@main/data/CacheService', async () => {
const { MockMainCacheServiceExport } = await import('./__mocks__/main/CacheService')
return MockMainCacheServiceExport
})
// Mock DbService globally for main tests (if exists)
vi.mock('@main/data/db/DbService', async () => {
try {
const { MockDbService } = await import('./__mocks__/DbService')
return MockDbService
} catch {
// Return basic mock if DbService mock doesn't exist yet
return {
dbService: {
initialize: vi.fn(),
getDb: vi.fn()
}
}
}
})
// 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()
}))