import {afterEach,beforeEach,expect,it,vi} from 'vitest' import {mount,flushPromises} from '@vue/test-utils' import {h,reactive} from 'vue' import ElementPlus from 'element-plus' import AppRelease from '@/views/system/file/AppRelease.vue' import Library from '@/views/system/file/index.vue' const state=vi.hoisted(()=>({user:{id:1},request:vi.fn(),list:vi.fn(),message:vi.fn(),row:{id:'test-file',name:'release.apk',size:128,canDownload:true,canRemove:true}})) vi.mock('@/store/modules/user',()=>({default:()=>state.user})) vi.mock('@/utils/request',()=>({default:state.request})) vi.mock('@/api/system/files',()=>({listFiles:state.list,downloadFile:vi.fn(),removeFile:vi.fn(),fileSize:n=>`${n} B`})) vi.mock('element-plus',async importOriginal=>({...await importOriginal(),ElMessage:{success:state.message,error:vi.fn()}})) let wrapper beforeEach(()=>{state.user.id=1;state.request.mockReset();state.list.mockResolvedValue({data:{rows:[state.row],total:1,canUpload:true}});state.message.mockReset()}) afterEach(()=>{wrapper?.unmount();document.body.innerHTML=''}) const modal={props:['modelValue'],template:'
'} function dialog(){wrapper=mount(AppRelease,{attachTo:document.body,global:{plugins:[ElementPlus],stubs:{ElDialog:modal}}});wrapper.vm.show(state.row);return wrapper} it('publishes the selected APK only after confirmation and passes the release notes',async()=>{ state.request.mockResolvedValue({data:{versionName:'1.0.0',versionCode:3}});dialog();await flushPromises();await wrapper.find('textarea').setValue('完善现场操作');expect(state.request).not.toHaveBeenCalled();await wrapper.findAll('button').find(b=>b.text().includes('确认发布新版')).trigger('click');await flushPromises();expect(state.request).toHaveBeenCalledWith(expect.objectContaining({url:'/system/mobile-release',method:'post',data:{fileId:'test-file',notes:'完善现场操作'}}));expect(state.message).toHaveBeenCalled();expect(wrapper.text()).not.toContain('release.apk') }) it('shows backend validation errors and allows a retry without losing the notes',async()=>{ state.request.mockRejectedValueOnce(new Error('安装包环境不匹配')).mockResolvedValueOnce({data:{versionName:'1.0.0',versionCode:3}});dialog();await flushPromises();await wrapper.find('textarea').setValue('机器人更新');await wrapper.findAll('button').find(b=>b.text().includes('确认发布新版')).trigger('click');await flushPromises();expect(wrapper.text()).toContain('安装包环境不匹配');expect(wrapper.find('textarea').element.value).toBe('机器人更新');await wrapper.findAll('button').find(b=>b.text().includes('确认发布新版')).trigger('click');await flushPromises();expect(state.request).toHaveBeenCalledTimes(2) }) it.each([[1,true],[2,false]])('only the super administrator sees the APK publish entry (user %s)',async(id,allowed)=>{ state.user.id=id;const column={setup(_,ctx){return()=>h('div',ctx.slots.default?.({row:state.row}))}}; wrapper=mount(Library,{global:{plugins:[ElementPlus],stubs:{BusinessListPage:{template:'
'},BusinessTable:{template:'
'},ElTableColumn:column,Pagination:true,FileUpload:true,AppRelease:true}}});await flushPromises();expect(wrapper.text().includes('发布新版')).toBe(allowed) }) it('does not offer publishing for ordinary documents',async()=>{const old=state.row.name;state.row.name='说明.pdf';const column={setup(_,ctx){return()=>h('div',ctx.slots.default?.({row:state.row}))}};wrapper=mount(Library,{global:{plugins:[ElementPlus],stubs:{BusinessListPage:{template:'
'},BusinessTable:{template:'
'},ElTableColumn:column,Pagination:true,AppRelease:true}}});await flushPromises();expect(wrapper.text()).not.toContain('发布新版');state.row.name=old})