Files
FileCodeBoxFronted2026/tests/uploadStrategy.integration.test.mjs
T

31 lines
2.1 KiB
JavaScript

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}
})
test('small file initialization never requests a chunk bigger than the file',async()=>{
const originalInit=service.initChunkUpload;let size
const previousWindow=globalThis.window;globalThis.window={isSecureContext:true}
service.initChunkUpload=async request=>{size=request.chunk_size;return {code:200,detail:{existed:true,code:'existing'}}}
try{await upload(new File(['small'],'a.txt'),{expireValue:1,expireStyle:'day'});assert.equal(size,5)}finally{service.initChunkUpload=originalInit;globalThis.window=previousWindow}
})