mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-06 13:19:33 +08:00
chore: update TypeScript configuration and refactor AppUpdater tests
- Added tests mock path to tsconfig.node.json for improved test coverage. - Refactored AppUpdater to use preferenceService for language retrieval instead of configManager. - Updated AppUpdater tests to mock preferenceService and ensure correct language handling in release notes. - Changed import statements to use type imports for better clarity in MessageMcpTool component.
This commit is contained in:
parent
e5a3363021
commit
e6696def10
@ -302,7 +302,7 @@ export default class AppUpdater {
|
|||||||
*/
|
*/
|
||||||
private parseMultiLangReleaseNotes(releaseNotes: string): string {
|
private parseMultiLangReleaseNotes(releaseNotes: string): string {
|
||||||
try {
|
try {
|
||||||
const language = configManager.getLanguage()
|
const language = preferenceService.get('app.language')
|
||||||
const isChineseUser = language === 'zh-CN' || language === 'zh-TW'
|
const isChineseUser = language === 'zh-CN' || language === 'zh-TW'
|
||||||
|
|
||||||
// Create regex patterns using constants
|
// Create regex patterns using constants
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { UpdateInfo } from 'builder-util-runtime'
|
import type { UpdateInfo } from 'builder-util-runtime'
|
||||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
|
|
||||||
// Mock dependencies
|
// Mock dependencies
|
||||||
@ -12,15 +12,11 @@ vi.mock('@logger', () => ({
|
|||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
vi.mock('../ConfigManager', () => ({
|
// Mock PreferenceService using the existing mock
|
||||||
configManager: {
|
vi.mock('@data/PreferenceService', async () => {
|
||||||
getLanguage: vi.fn(),
|
const { MockMainPreferenceServiceExport } = await import('../../../../tests/__mocks__/main/PreferenceService')
|
||||||
getAutoUpdate: vi.fn(() => false),
|
return MockMainPreferenceServiceExport
|
||||||
getTestPlan: vi.fn(() => false),
|
})
|
||||||
getTestChannel: vi.fn(),
|
|
||||||
getClientId: vi.fn(() => 'test-client-id')
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
|
|
||||||
vi.mock('../WindowService', () => ({
|
vi.mock('../WindowService', () => ({
|
||||||
windowService: {
|
windowService: {
|
||||||
@ -85,14 +81,24 @@ vi.mock('electron-updater', () => ({
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
// Import after mocks
|
// Import after mocks
|
||||||
|
import { preferenceService } from '@data/PreferenceService'
|
||||||
|
|
||||||
|
import { MockMainPreferenceServiceUtils } from '../../../../tests/__mocks__/main/PreferenceService'
|
||||||
import AppUpdater from '../AppUpdater'
|
import AppUpdater from '../AppUpdater'
|
||||||
import { configManager } from '../ConfigManager'
|
|
||||||
|
// Mock clientId for ConfigManager since it's not migrated yet
|
||||||
|
vi.mock('../ConfigManager', () => ({
|
||||||
|
configManager: {
|
||||||
|
getClientId: vi.fn(() => 'test-client-id')
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
describe('AppUpdater', () => {
|
describe('AppUpdater', () => {
|
||||||
let appUpdater: AppUpdater
|
let appUpdater: AppUpdater
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
|
MockMainPreferenceServiceUtils.resetMocks()
|
||||||
appUpdater = new AppUpdater()
|
appUpdater = new AppUpdater()
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -114,7 +120,7 @@ describe('AppUpdater', () => {
|
|||||||
<!--LANG:END-->`
|
<!--LANG:END-->`
|
||||||
|
|
||||||
it('should return Chinese notes for zh-CN users', () => {
|
it('should return Chinese notes for zh-CN users', () => {
|
||||||
vi.mocked(configManager.getLanguage).mockReturnValue('zh-CN')
|
MockMainPreferenceServiceUtils.setPreferenceValue('app.language', 'zh-CN')
|
||||||
|
|
||||||
const result = (appUpdater as any).parseMultiLangReleaseNotes(sampleReleaseNotes)
|
const result = (appUpdater as any).parseMultiLangReleaseNotes(sampleReleaseNotes)
|
||||||
|
|
||||||
@ -124,7 +130,7 @@ describe('AppUpdater', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should return Chinese notes for zh-TW users', () => {
|
it('should return Chinese notes for zh-TW users', () => {
|
||||||
vi.mocked(configManager.getLanguage).mockReturnValue('zh-TW')
|
MockMainPreferenceServiceUtils.setPreferenceValue('app.language', 'zh-TW')
|
||||||
|
|
||||||
const result = (appUpdater as any).parseMultiLangReleaseNotes(sampleReleaseNotes)
|
const result = (appUpdater as any).parseMultiLangReleaseNotes(sampleReleaseNotes)
|
||||||
|
|
||||||
@ -134,7 +140,7 @@ describe('AppUpdater', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should return English notes for non-Chinese users', () => {
|
it('should return English notes for non-Chinese users', () => {
|
||||||
vi.mocked(configManager.getLanguage).mockReturnValue('en-US')
|
MockMainPreferenceServiceUtils.setPreferenceValue('app.language', 'en-US')
|
||||||
|
|
||||||
const result = (appUpdater as any).parseMultiLangReleaseNotes(sampleReleaseNotes)
|
const result = (appUpdater as any).parseMultiLangReleaseNotes(sampleReleaseNotes)
|
||||||
|
|
||||||
@ -144,7 +150,7 @@ describe('AppUpdater', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should return English notes for other language users', () => {
|
it('should return English notes for other language users', () => {
|
||||||
vi.mocked(configManager.getLanguage).mockReturnValue('ru-RU')
|
MockMainPreferenceServiceUtils.setPreferenceValue('app.language', 'ru-RU')
|
||||||
|
|
||||||
const result = (appUpdater as any).parseMultiLangReleaseNotes(sampleReleaseNotes)
|
const result = (appUpdater as any).parseMultiLangReleaseNotes(sampleReleaseNotes)
|
||||||
|
|
||||||
@ -162,7 +168,7 @@ describe('AppUpdater', () => {
|
|||||||
|
|
||||||
it('should handle malformed markers', () => {
|
it('should handle malformed markers', () => {
|
||||||
const malformedNotes = `<!--LANG:en-->English only`
|
const malformedNotes = `<!--LANG:en-->English only`
|
||||||
vi.mocked(configManager.getLanguage).mockReturnValue('zh-CN')
|
MockMainPreferenceServiceUtils.setPreferenceValue('app.language', 'zh-CN')
|
||||||
|
|
||||||
const result = (appUpdater as any).parseMultiLangReleaseNotes(malformedNotes)
|
const result = (appUpdater as any).parseMultiLangReleaseNotes(malformedNotes)
|
||||||
|
|
||||||
@ -178,12 +184,15 @@ describe('AppUpdater', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should handle errors gracefully', () => {
|
it('should handle errors gracefully', () => {
|
||||||
// Force an error by mocking configManager to throw
|
// Create a fresh instance for this test to avoid issues with constructor mocking
|
||||||
vi.mocked(configManager.getLanguage).mockImplementation(() => {
|
const testAppUpdater = new AppUpdater()
|
||||||
|
|
||||||
|
// Force an error by mocking preferenceService to throw
|
||||||
|
vi.mocked(preferenceService.get).mockImplementationOnce(() => {
|
||||||
throw new Error('Test error')
|
throw new Error('Test error')
|
||||||
})
|
})
|
||||||
|
|
||||||
const result = (appUpdater as any).parseMultiLangReleaseNotes(sampleReleaseNotes)
|
const result = (testAppUpdater as any).parseMultiLangReleaseNotes(sampleReleaseNotes)
|
||||||
|
|
||||||
// Should return original notes as fallback
|
// Should return original notes as fallback
|
||||||
expect(result).toBe(sampleReleaseNotes)
|
expect(result).toBe(sampleReleaseNotes)
|
||||||
@ -210,7 +219,7 @@ describe('AppUpdater', () => {
|
|||||||
|
|
||||||
describe('processReleaseInfo', () => {
|
describe('processReleaseInfo', () => {
|
||||||
it('should process multi-language release notes in string format', () => {
|
it('should process multi-language release notes in string format', () => {
|
||||||
vi.mocked(configManager.getLanguage).mockReturnValue('zh-CN')
|
MockMainPreferenceServiceUtils.setPreferenceValue('app.language', 'zh-CN')
|
||||||
|
|
||||||
const releaseInfo = {
|
const releaseInfo = {
|
||||||
version: '1.0.0',
|
version: '1.0.0',
|
||||||
@ -277,7 +286,7 @@ describe('AppUpdater', () => {
|
|||||||
|
|
||||||
describe('formatReleaseNotes', () => {
|
describe('formatReleaseNotes', () => {
|
||||||
it('should format string release notes with markers', () => {
|
it('should format string release notes with markers', () => {
|
||||||
vi.mocked(configManager.getLanguage).mockReturnValue('en-US')
|
MockMainPreferenceServiceUtils.setPreferenceValue('app.language', 'en-US')
|
||||||
const notes = `<!--LANG:en-->English<!--LANG:zh-CN-->中文<!--LANG:END-->`
|
const notes = `<!--LANG:en-->English<!--LANG:zh-CN-->中文<!--LANG:END-->`
|
||||||
|
|
||||||
const result = (appUpdater as any).formatReleaseNotes(notes)
|
const result = (appUpdater as any).formatReleaseNotes(notes)
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import { CopyIcon, LoadingIcon } from '@renderer/components/Icons'
|
|||||||
import { useCodeStyle } from '@renderer/context/CodeStyleProvider'
|
import { useCodeStyle } from '@renderer/context/CodeStyleProvider'
|
||||||
import { useMCPServers } from '@renderer/hooks/useMCPServers'
|
import { useMCPServers } from '@renderer/hooks/useMCPServers'
|
||||||
import { useTimer } from '@renderer/hooks/useTimer'
|
import { useTimer } from '@renderer/hooks/useTimer'
|
||||||
import { MCPToolResponse } from '@renderer/types'
|
import type { MCPToolResponse } from '@renderer/types'
|
||||||
import type { ToolMessageBlock } from '@renderer/types/newMessage'
|
import type { ToolMessageBlock } from '@renderer/types/newMessage'
|
||||||
import { isToolAutoApproved } from '@renderer/utils/mcp-tools'
|
import { isToolAutoApproved } from '@renderer/utils/mcp-tools'
|
||||||
import { cancelToolAction, confirmToolAction } from '@renderer/utils/userConfirmation'
|
import { cancelToolAction, confirmToolAction } from '@renderer/utils/userConfirmation'
|
||||||
|
|||||||
@ -9,7 +9,9 @@
|
|||||||
"packages/shared/**/*",
|
"packages/shared/**/*",
|
||||||
"scripts",
|
"scripts",
|
||||||
"packages/mcp-trace/**/*",
|
"packages/mcp-trace/**/*",
|
||||||
"src/renderer/src/services/traceApi.ts" ],
|
"src/renderer/src/services/traceApi.ts",
|
||||||
|
"tests/__mocks__/**/*"
|
||||||
|
],
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"composite": true,
|
"composite": true,
|
||||||
"incremental": true,
|
"incremental": true,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user