Files
FileCodeBoxFronted2026/tests/uploadCompletion.test.mjs
T
orion d03c4e1315 🐛 fix: 限制文件完成阶段的连续失败重试
* 保留处理中轮询,避免处理中响应重置失败计数导致无限重试
* 验证交替处理中及存储错误在四次失败后结束,保留手动重试能力
2026-10-06 01:58:21 +08:00

35 lines
1.9 KiB
JavaScript

import {test} from 'node:test'
import assert from 'node:assert/strict'
import {waitForUploadCompletion} from '../src/services/upload-completion.ts'
test('pending verification is polled until a code is issued and finalizing is announced once',async()=>{
const responses=[{code:202},{code:202},{code:200,detail:{code:'ABCD2345'}}];let checks=0,notices=0
const result=await waitForUploadCompletion(async()=>responses[checks++],()=>notices++)
assert.equal(result.detail.code,'ABCD2345');assert.equal(checks,3);assert.equal(notices,1)
})
test('legacy synchronous success and failure are returned without polling',async()=>{
for(const code of [200,400]){let calls=0;const r=await waitForUploadCompletion(async()=>{calls++;return {code}});assert.equal(r.code,code);assert.equal(calls,1)}
})
test('non-retryable authentication failure stops polling',async()=>{
let calls=0;await assert.rejects(waitForUploadCompletion(async()=>{calls++;throw Object.assign(new Error('unauthorized'),{response:{status:401}})}),/unauthorized/);assert.equal(calls,1)
})
test('transient network failure during verification retries the same completion request',async()=>{
let calls=0;const result=await waitForUploadCompletion(async()=>{
calls++;if(calls===1)return {code:202};if(calls===2)throw new Error('offline');return {code:200,detail:{code:'SAMECODE'}}
});assert.equal(calls,3);assert.equal(result.detail.code,'SAMECODE')
})
test('pending replies between failed completion tasks do not reset the retry limit',async t=>{
t.mock.timers.enable({apis:['setTimeout']})
let calls=0
const pending=waitForUploadCompletion(async()=>{
calls++;if(calls%2===1)return {code:202}
throw Object.assign(new Error('storage unavailable'),{response:{status:503}})
})
const rejected=assert.rejects(pending,/storage unavailable/)
for(let i=0;i<8;i++){await new Promise(r=>setImmediate(r));t.mock.timers.tick(3000)}
await rejected;assert.equal(calls,8);t.mock.timers.reset()
})