import {afterEach,beforeEach,expect,it,vi} from 'vitest' import {mount,flushPromises} from '@vue/test-utils' import ElementPlus from 'element-plus' import Releases from '@/views/system/mobileRelease/index.vue' import Publish from '@/views/system/mobileRelease/PublishDialog.vue' const api=vi.hoisted(()=>({list:vi.fn(),publish:vi.fn(),remove:vi.fn(),migrate:vi.fn(),user:{id:1}})) vi.mock('@/store/modules/user',()=>({default:()=>api.user})) vi.mock('@/api/system/mobile-release',()=>({listAppPackages:api.list,publishAppPackage:api.publish,removeAppPackage:api.remove,migrateAppPackages:api.migrate})) vi.mock('@/api/system/files',()=>({fileSize:n=>n+' B',downloadFile:vi.fn()})) vi.mock('@/utils/display-format',()=>({formatDateTime:v=>v||'—'})) vi.mock('@/components/ManagedFiles/Upload.vue',()=>({default:{name:'AppUpload',props:['purpose'],emits:['uploaded','pending'],template:''}})) let w const data={rows:[{id:'new',name:'new.apk',versionName:'1.0.6',size:100,canPublish:true,current:false},{id:'old',name:'old.apk',versionName:'1.0.5',size:100,canPublish:false,current:true}],current:{versionName:'1.0.5',size:100,notes:'优化布局\n精简更新'},total:2,legacyCount:0,environment:'dev'} beforeEach(()=>{vi.resetAllMocks();api.user.id=1;api.list.mockResolvedValue({data});api.publish.mockResolvedValue({data:{versionName:'1.0.6'}})}) afterEach(()=>{w?.unmount();document.body.innerHTML=''}) function render(){w=mount(Releases,{global:{plugins:[ElementPlus],stubs:{BusinessListPage:{template:'
'},BusinessTable:{props:['data'],template:''},Pagination:true}}});return w} it('ordinary users cannot load version administration even with a direct route',async()=>{api.user.id=2;render();await flushPromises();expect(api.list).not.toHaveBeenCalled();expect(w.text()).toContain('仅超级管理员');expect(w.find('.upload-fixture').exists()).toBe(false)}) it('admin page uses a dedicated upload target and completing an upload never publishes implicitly',async()=>{render();await flushPromises();expect(w.text()).toContain('1.0.5');expect(w.findComponent({name:'AppUpload'}).props('purpose')).toBe('appRelease');expect(w.find('.upload-fixture').exists()).toBe(true);await w.find('.upload-fixture').trigger('click');await flushPromises();expect(api.list).toHaveBeenCalledTimes(2);expect(api.publish).not.toHaveBeenCalled()}) it('publishing requires explicit confirmation and sends only the chosen package and notes',async()=>{ w=mount(Publish,{global:{plugins:[ElementPlus]}});w.vm.show(data.rows[0]);await flushPromises();expect(api.publish).not.toHaveBeenCalled();w.vm.$.setupState.notes='优化下载\n修复显示';await flushPromises();const button=[...document.querySelectorAll('button')].find(b=>b.textContent.includes('确认发布新版'));button.click();await flushPromises();expect(api.publish).toHaveBeenCalledWith({fileId:'new',notes:'优化下载\n修复显示'});expect(w.emitted('published')[0][0].versionName).toBe('1.0.6') }) it('publishing failure keeps the dialog and the entered update notes available',async()=>{api.publish.mockRejectedValue(new Error('版本过低'));w=mount(Publish,{global:{plugins:[ElementPlus]}});w.vm.show(data.rows[0]);await flushPromises();w.vm.$.setupState.notes='保留说明';await w.vm.$.setupState.publish();await flushPromises();expect(document.body.textContent).toContain('版本过低');expect(w.vm.$.setupState.notes).toBe('保留说明');expect(w.emitted('published')).toBeUndefined()})