✨ 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

+6
View File
@@ -69,3 +69,9 @@ Playwright Page。Vite 固定运行于 `127.0.0.1:5180`;分别传入 `http`
- `localShare.browser.mjs`:导出 `runLocalShareChecks(page, dark)`;Vite 运行于
`127.0.0.1:5180`。管理 API 全部拦截为独立数据,覆盖目录/搜索/分页、生成失败重试、
取件码/链接复制、空目录/加载错误和移动端溢出检查。
## WebDAV 后台完成上传
`uploadCompletion.test.mjs` 覆盖 202 轮询、临时网络错误重试及明确失败退出;
`uploadStrategy.integration.test.mjs` 验证用户在丢失完成响应后手动重试时复用原上传 ID,
不重新上传分片。发送页在此阶段显示文件校验提示,确认成功后才给出取件码。
+21
View File
@@ -0,0 +1,21 @@
import {test} from 'node:test'
import assert from 'node:assert/strict'
import {waitForUploadCompletion} from '../src/services/upload-completion.ts'
test('pending verification is polled until a code is issued and finalizing is announced once',async()=>{
const responses=[{code:202},{code:202},{code:200,detail:{code:'ABCD2345'}}];let checks=0,notices=0
const result=await waitForUploadCompletion(async()=>responses[checks++],()=>notices++)
assert.equal(result.detail.code,'ABCD2345');assert.equal(checks,3);assert.equal(notices,1)
})
test('legacy synchronous success and failure are returned without polling',async()=>{
for(const code of [200,400]){let calls=0;const r=await waitForUploadCompletion(async()=>{calls++;return {code}});assert.equal(r.code,code);assert.equal(calls,1)}
})
test('non-retryable authentication failure stops polling',async()=>{
let calls=0;await assert.rejects(waitForUploadCompletion(async()=>{calls++;throw Object.assign(new Error('unauthorized'),{response:{status:401}})}),/unauthorized/);assert.equal(calls,1)
})
test('transient network failure during verification retries the same completion request',async()=>{
let calls=0;const result=await waitForUploadCompletion(async()=>{
calls++;if(calls===1)return {code:202};if(calls===2)throw new Error('offline');return {code:200,detail:{code:'SAMECODE'}}
});assert.equal(calls,3);assert.equal(result.detail.code,'SAMECODE')
})
+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}
})