21 lines
1.5 KiB
TypeScript
21 lines
1.5 KiB
TypeScript
import {test} from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import {BusinessError,requestWithFallback,submitAndRefresh} from '../src/core/transport.ts'
|
|
test('内网写入结果不明时,公网重放保持相同请求标识与内容',async()=>{
|
|
let body={requestId:'one-key',kind:'help'},calls:any[]=[]
|
|
let task=await requestWithFallback('lan','lan','public','/service-requests','POST',body,async(base:string,_path:string,_method:string,data:any)=>{calls.push({base,data});if(base==='lan')throw new Error('timeout');return{id:'same-task'}},()=>{})
|
|
assert.deepEqual(task,{id:'same-task'});assert.equal(calls.length,2);assert.equal(calls[0].data,calls[1].data)
|
|
})
|
|
test('业务错误不切换、不重放;无幂等键的写入不自动重试',async()=>{
|
|
let attempts=0
|
|
await assert.rejects(requestWithFallback('lan','lan','public','/patterns','POST',{},async()=>{attempts++;throw new BusinessError('无权限',401)},()=>{}),/无权限/)
|
|
assert.equal(attempts,1)
|
|
await assert.rejects(requestWithFallback('lan','lan','public','/patterns','POST',{},async()=>{attempts++;throw new Error('timeout')},()=>{}),/确认记录/)
|
|
assert.equal(attempts,2)
|
|
})
|
|
test('提交成功但刷新失败仍保留已提交任务,不误报提交失败',async()=>{
|
|
let committed:any=null
|
|
let result=await submitAndRefresh(async()=>({id:'task-1'}),(t:any)=>{committed=t},async()=>{throw new Error('列表暂不可用')})
|
|
assert.equal(committed.id,'task-1');assert.equal(result.task.id,'task-1');assert.match(result.refreshError,/列表暂不可用/)
|
|
})
|