cmvr-wms/ruoyi-ui-vue3/tests/unit/resumable-upload.test.mjs
lixiaolong cd86623061 feat(app): 添加服务器连接管理和到货操作功能
- 集成 ServerConnection 组件用于开发环境服务器切换
- 实现多服务器令牌隔离存储机制
- 添加 ArrivalOperation 屏幕处理到货和验收流程
- 支持待登记到货和待验收状态切换
- 优化库存操作界面的字段显示逻辑
- 增强 Excel 文件分片上传支持产品文件识别
- 添加 AI 助手会话滚动跟踪功能
- 实现开发服务器地址持久化存储
- 优化登录界面集成服务器连接组件
- 添加领用出库功能支持项目物资使用
- 改进盘点确认生效的操作流程
- 增强采购到货登记和验收的权限控制
- 优化移动端文件上传的断点续传机制
- 添加服务器切换时的数据清理保护机制
- 实现紧凑金额显示模式减少界面冗余
- 增强库存操作的数量验证和错误提示
- 优化 AI 助手的会话状态同步机制
2026-10-20 10:04:17 +08:00

20 lines
4.4 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)})