147 lines
4.4 KiB
TypeScript
147 lines
4.4 KiB
TypeScript
// @vitest-environment jsdom
|
|||
|
|
|
||
|
|
import { AxiosError, type AxiosAdapter, type AxiosResponse, type InternalAxiosRequestConfig } from 'axios'
|
||
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||
|
|
|
||
|
|
const messageError = vi.hoisted(() => vi.fn())
|
||
|
|
const modalError = vi.hoisted(() => vi.fn())
|
||
|
|
|
||
|
|
vi.mock('ant-design-vue', () => ({
|
||
|
|
message: {
|
||
|
|
error: messageError
|
||
|
|
},
|
||
|
|
Modal: {
|
||
|
|
error: modalError
|
||
|
|
}
|
||
|
|
}))
|
||
|
|
|
||
|
|
import api from '../../src/api'
|
||
|
|
|
||
|
|
const createResponseError = (config: InternalAxiosRequestConfig, status: number): AxiosError => {
|
||
|
|
const response = {
|
||
|
|
data: {
|
||
|
|
code: status,
|
||
|
|
message: `HTTP ${status}`
|
||
|
|
},
|
||
|
|
status,
|
||
|
|
statusText: 'Error',
|
||
|
|
headers: {},
|
||
|
|
config,
|
||
|
|
request: {}
|
||
|
|
} as AxiosResponse
|
||
|
|
|
||
|
|
return new AxiosError(
|
||
|
|
`Request failed with status code ${status}`,
|
||
|
|
AxiosError.ERR_BAD_RESPONSE,
|
||
|
|
config,
|
||
|
|
{},
|
||
|
|
response
|
||
|
|
)
|
||
|
|
}
|
||
|
|
|
||
|
|
const createNetworkError = (config: InternalAxiosRequestConfig): AxiosError =>
|
||
|
|
new AxiosError('Network Error', AxiosError.ERR_NETWORK, config, {})
|
||
|
|
|
||
|
|
type MockRequestResult = AxiosError | null
|
||
|
|
|
||
|
|
const mockRequestResults = (results: MockRequestResult[]) => {
|
||
|
|
let currentResultIndex = 0
|
||
|
|
api.defaults.adapter = ((config: InternalAxiosRequestConfig) => {
|
||
|
|
const result = results[currentResultIndex]
|
||
|
|
currentResultIndex += 1
|
||
|
|
if (!result) {
|
||
|
|
return Promise.resolve({
|
||
|
|
data: { code: 200, data: { ok: true } },
|
||
|
|
status: 200,
|
||
|
|
statusText: 'OK',
|
||
|
|
headers: {},
|
||
|
|
config,
|
||
|
|
request: {}
|
||
|
|
} as AxiosResponse)
|
||
|
|
}
|
||
|
|
result.config = config
|
||
|
|
return Promise.reject(result)
|
||
|
|
}) as AxiosAdapter
|
||
|
|
}
|
||
|
|
|
||
|
|
describe('api system unavailable handling', () => {
|
||
|
|
const originalAdapter = api.defaults.adapter
|
||
|
|
|
||
|
|
beforeEach(() => {
|
||
|
|
vi.useFakeTimers()
|
||
|
|
vi.setSystemTime(1_000)
|
||
|
|
messageError.mockClear()
|
||
|
|
modalError.mockClear()
|
||
|
|
localStorage.clear()
|
||
|
|
sessionStorage.clear()
|
||
|
|
})
|
||
|
|
|
||
|
|
afterEach(() => {
|
||
|
|
api.defaults.adapter = originalAdapter
|
||
|
|
localStorage.clear()
|
||
|
|
sessionStorage.clear()
|
||
|
|
vi.useRealTimers()
|
||
|
|
})
|
||
|
|
|
||
|
|
it('shows one system unavailable modal after three server failures in the window', async () => {
|
||
|
|
const config = {} as InternalAxiosRequestConfig
|
||
|
|
mockRequestResults([
|
||
|
|
createResponseError(config, 500),
|
||
|
|
createResponseError(config, 502),
|
||
|
|
createResponseError(config, 503),
|
||
|
|
createResponseError(config, 500)
|
||
|
|
])
|
||
|
|
|
||
|
|
await expect(api.get('/health')).rejects.toMatchObject({ response: { status: 500 } })
|
||
|
|
await expect(api.get('/health')).rejects.toMatchObject({ response: { status: 502 } })
|
||
|
|
expect(modalError).not.toHaveBeenCalled()
|
||
|
|
|
||
|
|
await expect(api.get('/health')).rejects.toMatchObject({ response: { status: 503 } })
|
||
|
|
expect(modalError).toHaveBeenCalledTimes(1)
|
||
|
|
expect(modalError).toHaveBeenCalledWith(expect.objectContaining({
|
||
|
|
title: '系统暂不可用',
|
||
|
|
content: '当前服务暂时无法响应,请稍后重试。如问题持续存在,请联系管理员。',
|
||
|
|
okText: '我知道了'
|
||
|
|
}))
|
||
|
|
|
||
|
|
await expect(api.get('/health')).rejects.toMatchObject({ response: { status: 500 } })
|
||
|
|
expect(modalError).toHaveBeenCalledTimes(1)
|
||
|
|
modalError.mock.calls[0]?.[0]?.afterClose?.()
|
||
|
|
})
|
||
|
|
|
||
|
|
it('counts network failures as system failures', async () => {
|
||
|
|
const config = {} as InternalAxiosRequestConfig
|
||
|
|
vi.setSystemTime(70_000)
|
||
|
|
mockRequestResults([
|
||
|
|
createNetworkError(config),
|
||
|
|
createNetworkError(config),
|
||
|
|
createNetworkError(config)
|
||
|
|
])
|
||
|
|
|
||
|
|
await expect(api.get('/health')).rejects.toBeInstanceOf(AxiosError)
|
||
|
|
await expect(api.get('/health')).rejects.toBeInstanceOf(AxiosError)
|
||
|
|
await expect(api.get('/health')).rejects.toBeInstanceOf(AxiosError)
|
||
|
|
|
||
|
|
expect(modalError).toHaveBeenCalledTimes(1)
|
||
|
|
expect(messageError).toHaveBeenCalledTimes(1)
|
||
|
|
})
|
||
|
|
|
||
|
|
it('resets failure count after a successful response', async () => {
|
||
|
|
const config = {} as InternalAxiosRequestConfig
|
||
|
|
vi.setSystemTime(140_000)
|
||
|
|
mockRequestResults([
|
||
|
|
createResponseError(config, 500),
|
||
|
|
createResponseError(config, 500),
|
||
|
|
null,
|
||
|
|
createResponseError(config, 500)
|
||
|
|
])
|
||
|
|
|
||
|
|
await expect(api.get('/health')).rejects.toMatchObject({ response: { status: 500 } })
|
||
|
|
await expect(api.get('/health')).rejects.toMatchObject({ response: { status: 500 } })
|
||
|
|
await expect(api.get('/health')).resolves.toEqual({ ok: true })
|
||
|
|
await expect(api.get('/health')).rejects.toMatchObject({ response: { status: 500 } })
|
||
|
|
|
||
|
|
expect(modalError).not.toHaveBeenCalled()
|
||
|
|
})
|
||
|
|
})
|