import {afterEach,it,expect} from 'vitest' import {mount} from '@vue/test-utils' import ElementPlus from 'element-plus' import DocumentLibrary from '@/components/ProjectWorkspace/DocumentLibrary.vue' const categories=[['DISCLOSURE','交底书'],['REPORT','报告'],['ACCEPTANCE','受理材料'],['GRANT','授权材料'],['OTHER','其他材料']] function fixture(count=45){return Array.from({length:count},(_,i)=>({id:i+1,title:`材料${i+1}.pdf`,category:categories[i%5][0],createBy:'测试人员',createTime:`2026-10-${String(i%20+1).padStart(2,'0')} 10:00:00`,currentFileId:`${i}-new`,finalFileId:i%2===0?`${i}-old`:null,canManage:true,canWrite:true,files:[{id:`${i}-new`,name:`材料${i+1}.pdf`,revision:2,size:4096,contentType:'application/pdf',createBy:'上传人',createTime:'2026-10-21 11:00:00'},{id:`${i}-old`,name:`历史材料${i+1}.pdf`,revision:1,size:2048,contentType:'application/pdf',createTime:'2026-10-20 10:00:00'}]}))} const mounted=[] function render(props={}){const w=mount(DocumentLibrary,{props:{documents:fixture(),categories,canWrite:true,...props},attachTo:document.body,global:{plugins:[ElementPlus]}});mounted.push(w);return w} const rows=w=>w.findAll('.library-row') afterEach(()=>{mounted.splice(0).forEach(w=>w.unmount());document.body.innerHTML=''}) it('many files use compact material rows with pagination instead of rendering every version',async()=>{ const w=render();expect(rows(w)).toHaveLength(20);expect(w.text()).toContain('45 份材料');expect(w.text()).toContain('90 个文件版本');expect(w.findAll('.document-card')).toHaveLength(0) w.findComponent({name:'ElPagination'}).vm.$emit('update:current-page',3);await w.vm.$nextTick();expect(rows(w)).toHaveLength(5) }) it('category navigation shows counts and filters locally without changing file data',async()=>{ const data=fixture(),w=render({documents:data});await w.find('.document-tabs').findAll('button').find(b=>b.text().startsWith('报告')).trigger('click') expect(rows(w)).toHaveLength(9);expect(rows(w).map(row=>data.find(d=>d.id===Number(row.attributes('aria-label').match(/\d+/)[0]))?.category).every(c=>c==='REPORT')).toBe(true);expect(data).toHaveLength(45);expect(w.text()).toContain('9 份材料 / 共 45 份') }) it('search matches historical names and explains the match without silently switching versions',async()=>{ const w=render();await w.get('input[aria-label="搜索文件"]').setValue('历史材料12') expect(rows(w)).toHaveLength(1);expect(w.text()).toContain('匹配历史版本:历史材料12.pdf');expect(w.find('.file-name').text()).toBe('材料12.pdf');expect(w.emitted('select-version')).toBeUndefined() }) it('search normalizes whitespace and empty results can clear all filters',async()=>{ const w=render();await w.get('input[aria-label="搜索文件"]').setValue(' 材 料 12 . pdf ');expect(rows(w)).toHaveLength(1) await w.get('input[aria-label="搜索文件"]').setValue('不存在的文件');expect(rows(w)).toHaveLength(0);expect(w.text()).toContain('没有匹配的文件') await w.findAll('button').find(b=>b.text()==='清除筛选').trigger('click');expect(rows(w)).toHaveLength(20) }) it('final-only mode displays and operates on the final file, not a newer unapproved version',async()=>{ const w=render({documents:fixture(2)});w.findComponent({name:'ElCheckbox'}).vm.$emit('update:modelValue',true);await w.vm.$nextTick() expect(rows(w)).toHaveLength(1);expect(w.text()).toContain('历史材料1.pdf');expect(w.text()).toContain('最终版 · 第 1 版') await w.findAll('button').find(b=>b.text()==='下载').trigger('click');expect(w.emitted('action').at(-1)).toEqual(['download',expect.objectContaining({id:1}),'0-old']) await w.get('input[aria-label="搜索文件"]').setValue('历史材料2');expect(rows(w)).toHaveLength(0) }) it('changing page filters and receiving fewer records always keep pagination in range',async()=>{ const w=render();w.findComponent({name:'ElPagination'}).vm.$emit('update:current-page',3);await w.vm.$nextTick() await w.get('input[aria-label="搜索文件"]').setValue('材料45.pdf');expect(rows(w)).toHaveLength(1) await w.get('input[aria-label="搜索文件"]').setValue('');w.findComponent({name:'ElPagination'}).vm.$emit('update:current-page',3);await w.vm.$nextTick();await w.setProps({documents:fixture(1)});expect(rows(w)).toHaveLength(1) }) it('historical versions stay selectable in read-only mode while all maintenance actions are hidden',async()=>{ const w=render({documents:fixture(1),readOnly:true});expect(w.findComponent({name:'ElDropdown'}).exists()).toBe(false) const selector=w.findAllComponents({name:'ElSelect'}).find(c=>c.props('ariaLabel')==='文件版本:材料1.pdf');expect(selector.props('disabled')).toBe(false);selector.vm.$emit('update:modelValue','0-old');expect(w.emitted('select-version').at(-1)).toEqual([1,'0-old']) await w.setProps({selected:{1:'0-old'}});expect(w.find('.file-name').text()).toBe('历史材料1.pdf');expect(selector.props('modelValue')).toBe('0-old') }) it('archive and per-document permissions hide maintenance and pending uploads disable actions',async()=>{ const data=fixture(1);data[0].canWrite=false;data[0].canManage=false;const w=render({documents:data});expect(w.findComponent({name:'ElDropdown'}).exists()).toBe(false) await w.setProps({busy:true});expect(w.findAllComponents({name:'ElSelect'}).find(c=>c.props('ariaLabel')?.startsWith('文件版本')).props('disabled')).toBe(true) await w.findAll('button').find(b=>b.text()==='下载').trigger('click');expect(w.emitted('action')).toBeUndefined() }) it('name sorting is local and unknown historical categories are still discoverable',async()=>{ const data=fixture(3);data[0].category='LEGACY';data[0].files[0].name='A.pdf';data[1].files[0].name='Z.pdf';data[2].files[0].name='B.pdf' const w=render({documents:data});w.findAllComponents({name:'ElSelect'}).find(c=>c.props('ariaLabel')==='文件排序').vm.$emit('update:modelValue','name');await w.vm.$nextTick();expect(rows(w).map(row=>row.find('.file-name').text())).toEqual(['A.pdf','B.pdf','Z.pdf']) await w.find('.document-tabs').findAll('button').find(b=>b.text().startsWith('LEGACY')).trigger('click');expect(rows(w)).toHaveLength(1) }) it('empty document records remain repairable without inventing a file preview or download',()=>{ const w=render({documents:[{id:1,title:'待补材料',category:'REPORT',files:[],canWrite:true}]});expect(w.text()).toContain('待上传文件');expect(w.findAll('button').some(b=>b.text()==='下载')).toBe(false);expect(w.findComponent({name:'ElDropdown'}).exists()).toBe(true) })