Skip to content
← Public packages

@kody/dropbox

Reusable Dropbox OAuth helpers for account, metadata, search, file transfer, sharing, and generic API requests.

FollowFork

Files

upload-recovery.test.mjs

25 lines · 3.4 KB · JavaScript
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { buildModule, assertCliVersion } from '../scripts/upload-to-dropbox.mjs'
const AsyncFunction=Object.getPrototypeOf(async function(){}).constructor
function fixture(overrides={}) {
 let starts=0,finishes=0,acks=0;const reports=[]
 const api={startUploadSession:async()=>{starts++;return{sessionId:'sid'}},appendUploadSession:async p=>({offset:p.offset+p.bytes.length}),finishUploadSession:async()=>{finishes++;throw new TypeError('lost final response')},getUploadSessionOffset:async()=>({offset:0}),getCorrectOffsetFromError:()=>null,DropboxApiError:class extends Error{},dropboxRpc:async ep=>ep==='users/get_current_account'?{account_id:'a'}:{size:3,content_hash:'h'},fetch:async(u,i)=>{if(u==='report'){reports.push(JSON.parse(i.body));return new Response(null,{status:204})}return new Response(new Uint8Array(3),{status:206})},...overrides}
 const src=buildModule('test').replace(/import[\s\S]*?from 'kody:[^']+'\n/g,'').replace('export default async function main','async function main')+'\nreturn main(p)'
 const run=new AsyncFunction('p',...Object.keys(api),src)
 return{run:p=>run(p,...Object.values(api)),counts:()=>({starts,finishes}),reports}
}
const p={size:3,contentHash:'h',commit:{path:'/d',mode:'overwrite'},sourceUrl:'source',reportUrl:'report',chunkBytes:16*1024*1024}
test('lost finish response verifies metadata/hash rather than starting again',async()=>{const f=fixture();const r=await f.run(p);assert.equal(r.done,true);assert.deepEqual(f.counts(),{starts:1,finishes:1});assert(f.reports.some(x=>x.phase==='finishing'))})
test('restart in finishing phase verifies without any new mutation',async()=>{const f=fixture();assert.equal((await f.run({...p,sessionId:'s',accountId:'a',phase:'finishing'})).done,true);assert.deepEqual(f.counts(),{starts:0,finishes:0})})
test('autorename uncertainty, different account and unknown identity fail closed',async()=>{for(const extra of [{commit:{...p.commit,autorename:true},phase:'finishing'},{accountId:'other',sessionId:'s'},{sessionId:'s'}]){const f=fixture();await assert.rejects(f.run({...p,...extra}),/RECONCILIATION_REQUIRED/);assert.equal(f.counts().starts,0)}})
test('uncertain final with wrong hash fails closed',async()=>{const f=fixture({dropboxRpc:async ep=>ep==='users/get_current_account'?{account_id:'a'}:{size:3,content_hash:'wrong'}});await assert.rejects(f.run({...p,phase:'finishing'}),/RECONCILIATION_REQUIRED/)})
test('aborted append is not retried',async()=>{let attempts=0;const f=fixture({appendUploadSession:async()=>{attempts++;throw Object.assign(new Error('abort'),{name:'AbortError'})}});await assert.rejects(f.run({...p,size:20,chunkBytes:3}));assert.equal(attempts,1)})
test('429 Retry-After is honored; 401 is not treated as a network retry',async()=>{
 let attempts=0;const waits=[]
 const f=fixture({DropboxApiError:Error,setTimeout:(cb,ms)=>{waits.push(ms);cb()},appendUploadSession:async q=>{if(++attempts===1)throw Object.assign(new Error('rate'),{status:429,retryAfterMs:35000});return{offset:q.offset+q.bytes.length}},finishUploadSession:async()=>({size:6,content_hash:'h'})})
 assert.equal((await f.run({...p,size:6,chunkBytes:3})).done,true);assert.deepEqual(waits,[35000]);assert.equal(attempts,2)
 attempts=0
 const g=fixture({DropboxApiError:Error,appendUploadSession:async()=>{attempts++;throw Object.assign(new Error('auth'),{status:401})}})
 await assert.rejects(g.run({...p,size:6,chunkBytes:3}),/auth/);assert.equal(attempts,1)
})