22 lines
1.4 KiB
JavaScript
22 lines
1.4 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')
|
|
})
|