cmvr-wms/ruoyi-ui-vue3/tests/unit/resumable-upload.test.mjs
lixiaolong 6ae0682546 feat(workspace): 优化工作空间页面布局并改进到货管理功能
- 新增 ArrivalListTools 和 ArrivalCard 组件用于到货列表管理
- 集成 arrivalListQuery 查询逻辑优化数据加载
- 调整页面标题文字样式增加最小宽度和行高
- 更新底部标签栏字体大小和行高设置
- 重构到货操作界面添加键盘避让视图支持
- 优化到货操作按钮状态管理增加次要按钮样式
- 实现助手组件分页功能支持动态页面大小
- 扩展文件存储区域配置支持项目工作空间
- 增加文件流式上传下载方法支持大文件处理
- 优化采购表格单元格键盘输入体验
- 修复移动端到货操作界面键盘遮挡问题
- 调整测试用例数据量验证分页功能正确性
2026-10-21 15:13:39 +08:00

31 lines
5.2 KiB
JavaScript

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<size;i+=17)hash.update(bytes.subarray(i,i+17));assert.equal(hash.hex(),digest(bytes));assert.equal(await hashBlob(new Blob([bytes])),digest(bytes))}
assert.equal(new FileSha256().update(new TextEncoder().encode('abc')).hex(),'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad')
})
function fixture({received=[],done=false,fail}={}){const file=new File([new Uint8Array(1024*1024+9)],'说明.txt'),calls=[],progress=[];const state={id:'11111111-1111-1111-1111-111111111111',size:file.size,chunkSize:1024*1024,totalChunks:2,uploaded:received,complete:done,result:done?{id:'native'}:null};const request=async config=>{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)
})