Files
FileCodeBoxFronted2026/tests/emptyUpload.integration.test.mjs
T
orion f7bae99fe4 🐛 fix: 上传前拦截空文件并明确提示
* 普通选择和拖拽均拒绝零字节文件,清空无效选择及文件哈希
* 提交时复核单文件和打包结果,阻止分片或预签名初始化空文件
* 补充空文件入口及非空上传回归,记录生产分片代理限制修复
2026-10-06 00:52:12 +08:00

55 lines
3.4 KiB
JavaScript

import { test, before, after } from 'node:test'
import assert from 'node:assert/strict'
import { createServer } from 'vite'
import { fileURLToPath } from 'node:url'
import { createSSRApp, h } from 'vue'
import { renderToString } from 'vue/server-renderer'
import { createPinia } from 'pinia'
import { createI18n } from 'vue-i18n'
let server, createFlow, service, presign, alerts, config, zh, oldStorage, oldWindow
before(async () => {
oldStorage=globalThis.localStorage;oldWindow=globalThis.window
globalThis.localStorage={getItem:()=>null,setItem(){},removeItem(){}}
globalThis.window={isSecureContext:true,location:{origin:'https://example.test'}}
server=await createServer({resolve:{alias:{'file-saver':fileURLToPath(new URL('./fixtures/browser-save.stub.mjs',import.meta.url))}},server:{middlewareMode:true,watch:null},appType:'custom'})
createFlow=(await server.ssrLoadModule('/src/composables/useSendFlow.ts')).useSendFlow
const exports=await server.ssrLoadModule('/src/services/index.ts');service=exports.FileService;presign=exports.PresignUploadService
alerts=(await server.ssrLoadModule('/src/stores/alertStore.ts')).useAlertStore
config=(await server.ssrLoadModule('/src/stores/configStore.ts')).useConfigStore
zh=(await server.ssrLoadModule('/src/i18n/locales/zh-CN.ts')).default
})
after(async()=>{await server?.close();globalThis.localStorage=oldStorage;globalThis.window=oldWindow})
async function flow(enableChunk=1) {
let f,notifications=[]
const app=createSSRApp({setup(){alerts().showAlert=(message,type)=>notifications.push({message,type});config().config.enableChunk=enableChunk;f=createFlow();return()=>h('div')}})
app.use(createPinia());app.use(createI18n({legacy:false,locale:'zh-CN',messages:{'zh-CN':zh}}));await renderToString(app)
return {f,notifications}
}
test('empty selection and drop are rejected with a friendly message and cleared file',async()=>{
for(const kind of ['select','drop']){
const {f,notifications}=await flow()
const file=new File([],'BNApp64-v3.13.5.apk')
if(kind==='select')await f.handleFileSelected(file);else await f.handleFileDrop({dataTransfer:{files:[file]}})
assert.equal(f.selectedFile.value,null);assert.deepEqual(notifications,[{message:'无法读取空文件',type:'error'}])
}
})
test('submit revalidates selected file before both chunk and presign initialization',async()=>{
const originals={chunk:service.initChunkUpload,presign:presign.initUpload};let requests=0
service.initChunkUpload=presign.initUpload=async()=>{requests++;throw Error('request must not be sent')}
try{
for(const enabled of [0,1]){
const {f,notifications}=await flow(enabled);f.selectedFile.value=new File([],'empty.apk')
await f.handleSubmit();assert.equal(requests,0);assert.equal(f.selectedFile.value,null);assert.equal(f.isSubmitting.value,false)
assert.deepEqual(notifications,[{message:'无法读取空文件',type:'error'}])
}
}finally{service.initChunkUpload=originals.chunk;presign.initUpload=originals.presign}
})
test('nonempty file initialization still carries its actual size',async()=>{
const {f}=await flow();const original=service.initChunkUpload;let request
service.initChunkUpload=async data=>{request=data;throw Error('stop after initialization')}
try{
f.selectedFile.value=new File(['apk-data'],'valid.apk');await f.handleSubmit()
assert.equal(request.file_size,8);assert.equal(request.chunk_size,8);assert.equal(f.isSubmitting.value,false)
}finally{service.initChunkUpload=original}
})