import test from 'node:test' import assert from 'node:assert/strict' import {createHash} from 'node:crypto' import {FileSha256,hashBlob} from '../../src/utils/file-sha256.js' import {resumableUpload} from '../../src/utils/resumable-upload.js' const digest=bytes=>createHash('sha256').update(bytes).digest('hex') test('streaming hash matches SHA-256 vectors and arbitrary block boundaries without crypto.subtle',async()=>{ for(const size of [0,1,55,56,63,64,65,127,1024*1024+3]){const bytes=Buffer.alloc(size,0x61),hash=new FileSha256();for(let i=0;i{calls.push(config);if(fail)await fail(config);if(config.url.endsWith('/complete'))return {data:{...state,complete:true,result:{id:'native'}}};if(config.method==='put'){const bytes=new Uint8Array(await config.data.get('file').arrayBuffer());assert.equal(config.data.get('sha256'),digest(bytes))}return {data:state}};return {file,calls,progress,request}} test('resume skips acknowledged chunks and always verifies the final receipt',async()=>{const f=fixture({received:[0]});const r=await resumableUpload(f.file,{purpose:'product',targetId:1},{request:f.request,onProgress:p=>f.progress.push(p)});assert.equal(r.transfer.resumed,true);assert.equal(f.calls.filter(c=>c.method==='put').length,1);assert.match(f.calls.find(c=>c.method==='put').url,/chunks\/1$/);assert.equal(f.progress.at(-1).phase,'complete')}) test('instant upload sends no bytes, but still calls completion authorization',async()=>{const f=fixture({done:true});const r=await resumableUpload(f.file,{purpose:'product',targetId:1},{request:f.request});assert.equal(r.transfer.instant,true);assert.deepEqual(f.calls.map(c=>c.method),['post','post']);assert.equal(f.calls.at(-1).url.endsWith('/complete'),true)}) test('lost chunk acknowledgement retries only the same chunk',async()=>{let dropped=false;const f=fixture({fail:async c=>{if(c.url.endsWith('/chunks/0')&&!dropped){dropped=true;throw Object.assign(new Error('lost'),{code:'ERR_NETWORK'})}}});await resumableUpload(f.file,{purpose:'assistant'},{request:f.request});assert.equal(f.calls.filter(c=>c.url.endsWith('/chunks/0')).length,2)}) test('permission rejection stops immediately without retrying or sending file bytes',async()=>{const f=fixture({fail:async()=>{throw Object.assign(new Error('not enabled'),{code:403})}});await assert.rejects(resumableUpload(f.file,{purpose:'product',targetId:1},{request:f.request}),{code:403});assert.equal(f.calls.length,1)}) test('pause during transport never completes a native upload',async()=>{const controller=new AbortController(),f=fixture({fail:async c=>{if(c.method==='put')controller.abort()}});await assert.rejects(resumableUpload(f.file,{purpose:'product',targetId:1},{request:f.request,signal:controller.signal}),{code:'ERR_CANCELED'});assert.equal(f.calls.some(c=>c.url.endsWith('/complete')),false)}) test('resumed finalization with all bytes already acknowledged sends no extra chunks',async()=>{const f=fixture({received:[0,1]});await resumableUpload(f.file,{purpose:'supplier'},{request:f.request});assert.equal(f.calls.filter(c=>c.method==='put').length,0)}) test('switching accounts during hashing cannot send the former user file or metadata',async()=>{let identity='first';const f=fixture();await assert.rejects(resumableUpload(f.file,{purpose:'product',targetId:1},{request:f.request,identity:()=>identity,hash:async()=>{identity='second';return 'a'.repeat(64)}}),{code:401});assert.equal(f.calls.length,0)}) test('switching accounts after a chunk prevents any further chunk or native completion',async()=>{let identity='first';const f=fixture({fail:async c=>{if(c.method==='put')identity='second'}});await assert.rejects(resumableUpload(f.file,{purpose:'product',targetId:1},{request:f.request,identity:()=>identity}),{code:401});assert.equal(f.calls.filter(c=>c.method==='put').length,1);assert.equal(f.calls.some(c=>c.url.endsWith('/complete')),false)}) test('project documents accept exactly 100 MiB while existing product uploads retain their 50 MiB cap',async()=>{ const size=100*1024*1024,file={name:'边界.pdf',size},calls=[] const state={id:'11111111-1111-1111-1111-111111111111',size,chunkSize:1024*1024,totalChunks:100,uploaded:[],complete:true,result:{id:'native'}} const request=async c=>{calls.push(c);return {data:state}} await resumableUpload(file,{purpose:'projectDocument',targetId:71},{request,hash:async()=>'a'.repeat(64)}) assert.equal(calls.length,2) await assert.rejects(resumableUpload({...file,size:size+1},{purpose:'projectDocument',targetId:71},{request}),/100 MB/) await assert.rejects(resumableUpload({...file,size:51*1024*1024},{purpose:'product',targetId:1},{request}),/50 MB/) assert.equal(calls.length,2) })