mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-04 21:30:06 +08:00
93 lines
2.9 KiB
TypeScript
93 lines
2.9 KiB
TypeScript
import { useState } from 'react'
|
|
import { beforeEach, expect, it, vi } from 'vitest'
|
|
|
|
import {
|
|
getRuntimeState,
|
|
installService,
|
|
restartCore,
|
|
type RunState,
|
|
} from '@/services/cmds'
|
|
import { showNotice } from '@/services/notice-service'
|
|
|
|
import { ServiceMigrationDialog } from './service-migration-dialog'
|
|
|
|
const store = vi.hoisted(() => ({
|
|
dispatch: vi.fn(),
|
|
runState: null as RunState | null,
|
|
}))
|
|
|
|
vi.mock('react', () => ({ useState: vi.fn() }))
|
|
vi.mock('react-i18next', () => ({
|
|
useTranslation: () => ({ t: (key: string) => key }),
|
|
}))
|
|
vi.mock('@mui/material', () => ({ Alert: 'div' }))
|
|
vi.mock('@/components/base', () => ({ BaseDialog: 'dialog' }))
|
|
vi.mock('@/store/app-store-context', () => ({
|
|
useAppReads: () => ({ readRunState: getRuntimeState }),
|
|
useRunState: () => store.runState,
|
|
}))
|
|
vi.mock('@/services/cmds', () => ({
|
|
getRuntimeState: vi.fn(),
|
|
installService: vi.fn(),
|
|
restartCore: vi.fn(),
|
|
}))
|
|
vi.mock('@/services/notice-service', () => ({
|
|
showNotice: { warning: vi.fn(), error: vi.fn(), success: vi.fn() },
|
|
}))
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
store.runState = null
|
|
vi.mocked(useState)
|
|
.mockReturnValueOnce([false, vi.fn()])
|
|
.mockReturnValueOnce([false, vi.fn()])
|
|
.mockReturnValueOnce([true, vi.fn()])
|
|
})
|
|
|
|
it.each([
|
|
{ service: 'ready', mode: 'NotRunning' },
|
|
{ service: 'unknown', mode: 'NotRunning' },
|
|
{ service: 'unavailable', mode: 'Service' },
|
|
{ service: 'unavailable', mode: 'NotRunning', opInFlight: true },
|
|
{ service: 'unavailable', mode: 'NotRunning', pendingAction: 'reinstall' },
|
|
] satisfies Partial<RunState>[])(
|
|
'does not offer Sidecar continuation from %j',
|
|
(state) => {
|
|
store.runState = state as unknown as RunState
|
|
const dialog = ServiceMigrationDialog()
|
|
expect(dialog.props.open).toBe(true)
|
|
expect(dialog.props.disableCancel).toBe(true)
|
|
},
|
|
)
|
|
|
|
it('leaves the choice open when installation fails', async () => {
|
|
store.runState = { service: 'notInstalled', mode: 'NotRunning' } as RunState
|
|
const error = new Error('core path is writable by Everyone')
|
|
vi.mocked(installService).mockRejectedValue(error)
|
|
vi.mocked(getRuntimeState).mockResolvedValue(store.runState)
|
|
|
|
ServiceMigrationDialog().props.onOk()
|
|
await vi.waitFor(() => expect(getRuntimeState).toHaveBeenCalled())
|
|
expect(showNotice.error).toHaveBeenCalledWith(
|
|
'layout.components.serviceMigration.errors.actionFailed',
|
|
error,
|
|
)
|
|
expect(restartCore).not.toHaveBeenCalled()
|
|
expect(vi.mocked(useState).mock.results[2].value[1]).not.toHaveBeenCalledWith(
|
|
false,
|
|
)
|
|
})
|
|
|
|
it.each([
|
|
{ service: 'notInstalled', mode: 'NotRunning' },
|
|
{ service: 'versionMismatch', mode: 'NotRunning' },
|
|
{ service: 'unavailable', mode: 'NotRunning' },
|
|
{ service: 'notInstalled', mode: 'Sidecar', pendingAction: 'install' },
|
|
] satisfies Partial<RunState>[])(
|
|
'offers Sidecar continuation from %j',
|
|
(state) => {
|
|
store.runState = state as unknown as RunState
|
|
expect(ServiceMigrationDialog().props.disableCancel).toBe(false)
|
|
},
|
|
)
|