✨ feat(frontend): 支持上传后台校验与断线恢复

This commit is contained in:
orion committed 2026-09-18 11:51:35 +08:00
1 parent e8c12af834
commit 1ea2fa3c6e
10 files changed
+108 -12

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
import {test,before,after} from 'node:test'
import assert from 'node:assert/strict'
import {createServer} from 'vite'
let server,upload,service
before(async()=>{
server=await createServer({server:{middlewareMode:true,watch:null},appType:'custom'})
service=(await server.ssrLoadModule('/src/services/file.ts')).FileService
upload=(await server.ssrLoadModule('/src/services/upload-strategy.ts')).uploadChunkedFile
})
after(async()=>await server?.close())
test('manual retry after lost completion response reuses upload id and never retransmits chunks',async()=>{
const originals={init:service.initChunkUpload,chunk:service.uploadChunk,complete:service.completeChunkUpload}
let inits=0,chunks=0,completions=0
const previousWindow=globalThis.window;globalThis.window={isSecureContext:true}
service.initChunkUpload=async()=>{inits++;return {code:200,detail:{upload_id:'original-session',uploaded_chunks:[]}}}
service.uploadChunk=async()=>{chunks++;return {code:200}}
service.completeChunkUpload=async(id)=>{assert.equal(id,'original-session');completions++;if(completions<=4)throw new Error('offline');return {code:200,detail:{code:'ORIGINAL'}}}
const file=new File(['small payload'],'file.txt'),options={expireValue:1,expireStyle:'day'}
try {
await assert.rejects(upload(file,options),/offline/)
const result=await upload(file,options)
assert.equal(result.detail.code,'ORIGINAL');assert.equal(inits,1);assert.equal(chunks,1);assert.equal(completions,5)
}finally{service.initChunkUpload=originals.init;service.uploadChunk=originals.chunk;service.completeChunkUpload=originals.complete;globalThis.window=previousWindow}
})