37 lines
2.4 KiB
JavaScript
37 lines
2.4 KiB
JavaScript
|
|
import {beforeEach,expect,it,vi} from 'vitest'
|
||
|
|
const capture=vi.hoisted(()=>({success:null,failure:null}))
|
||
|
|
vi.mock('axios',()=>({default:{defaults:{headers:{}},isCancel:()=>false,create:()=>({interceptors:{request:{use:()=>{}},response:{use:(success,failure)=>{capture.success=success;capture.failure=failure}}}})}}))
|
||
|
|
vi.mock('@/utils/auth',()=>({getToken:()=>null}))
|
||
|
|
vi.mock('@/store/modules/user',()=>({default:()=>({})}))
|
||
|
|
vi.mock('@/plugins/cache',()=>({default:{session:{getJSON:()=>undefined,setJSON:()=>{}}}}))
|
||
|
|
vi.mock('element-plus',()=>({ElMessage:vi.fn(),ElMessageBox:{confirm:vi.fn(()=>Promise.reject())},ElNotification:{error:vi.fn()},ElLoading:{}}))
|
||
|
|
import {ElMessage,ElNotification,ElMessageBox} from 'element-plus'
|
||
|
|
import '@/utils/request'
|
||
|
|
const config={url:'/project/workspace/capabilities',method:'get',workspaceCapabilityProbe:true}
|
||
|
|
beforeEach(()=>vi.clearAllMocks())
|
||
|
|
it('only the optional capabilities probe tolerates the exact Spring legacy missing-route envelope',()=>{
|
||
|
|
const result=capture.success({config,data:{code:500,msg:'No static resource project/workspace/capabilities.'}})
|
||
|
|
expect(result.data).toEqual({supported:false,ready:false});expect(ElMessage).not.toHaveBeenCalled()
|
||
|
|
})
|
||
|
|
it('an HTTP 404 from that probe also identifies a legacy backend without error toast',()=>{
|
||
|
|
expect(capture.failure({config,response:{status:404},message:'Request failed with status code 404'}).data.supported).toBe(false)
|
||
|
|
expect(ElMessage).not.toHaveBeenCalled()
|
||
|
|
})
|
||
|
|
it('unrelated endpoints and unexpected server errors do not fall back',async()=>{
|
||
|
|
for(const [cfg,msg] of [[{...config,url:'/project/workspace/71'},'No static resource project/workspace/capabilities.'],[config,'Database unavailable']]){
|
||
|
|
await expect(capture.success({config:cfg,request:{},data:{code:500,msg}})).rejects.toThrow()
|
||
|
|
}
|
||
|
|
expect(ElMessage).toHaveBeenCalledTimes(2)
|
||
|
|
})
|
||
|
|
it('permission failures remain failures and expired sessions still request login',async()=>{
|
||
|
|
await expect(capture.success({config,request:{},data:{code:403,msg:'请联系项目负责人'}})).rejects.toThrow()
|
||
|
|
expect(ElNotification.error).toHaveBeenCalled()
|
||
|
|
await expect(capture.success({config,request:{},data:{code:401}})).rejects.toBeTruthy()
|
||
|
|
expect(ElMessageBox.confirm).toHaveBeenCalled()
|
||
|
|
})
|
||
|
|
it('network failures never identify a legacy backend',async()=>{
|
||
|
|
const error={config,message:'Network Error'}
|
||
|
|
await expect(capture.failure(error)).rejects.toBe(error)
|
||
|
|
expect(ElMessage).toHaveBeenCalled()
|
||
|
|
})
|