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() })