cmvr-wms/ruoyi-ui-vue3/tests/runtime/archive-analysis.spec.js
lixiaolong f8f286d2a1 feat(assistant): 新增用户核对功能并优化对话流程
- 新增 user.check 功能用于核对人员是否已有系统账号
- 在 assistant-table-layout.js 中为用户表格添加 checkedIdentity 和 matchResult 字段
- 优化 AssistantConversationService 中的任务记忆和澄清流程
- 支持对草稿特定行进行修改,无需重新填写整张表
- 限制 user.check 操作最大查询数量为 100 条记录
- 实现中断任务恢复机制,保持原始请求上下文
- 添加针对多行数据选择性修改的目标行标识支持
2026-10-27 09:04:41 +08:00

24 lines
5.8 KiB
JavaScript

import {beforeEach,afterEach,expect,test,vi} from 'vitest'
import {shallowMount,flushPromises} from '@vue/test-utils'
import {defineComponent,h} from 'vue'
import Dashboard from '@/views/archive/cost-analysis.vue'
import {projectPayload} from '@/views/archive/archive-model'
const api=vi.hoisted(()=>({archiveMetadata:vi.fn(),archiveAnalysis:vi.fn(),archiveAnalysisPeople:vi.fn(),archiveDetail:vi.fn(),archiveCostExport:vi.fn()}))
vi.mock('@/api/archive',()=>api)
vi.mock('echarts/core',()=>({use:vi.fn(),init:()=>({setOption:vi.fn(),getDom:vi.fn(),dispose:vi.fn(),resize:vi.fn()})}))
const report=()=>({from:'2026-01-01',to:'2026-12-31',basis:'START',mode:'COMPLETE',total:41,included:39,unsaved:1,incomplete:1,unreadable:0,legacyStart:2,summary:{income:100000,cost:60000,profit:40000,margin:0.4},rows:[{id:1,name:'虚构项目',manager_name:'甲',income:1000,cost:600,profit:400,margin:0.4,included:true,complete:true,archive_state:'EDITING'}],categories:[{name:'外协费',amount:60000}],trend:[],managers:[]})
const stubs=Object.fromEntries('ElResult ElButton ElForm ElFormItem ElDatePicker ElSelect ElOption ElTooltip ElAlert ElProgress ElEmpty ElRadioGroup ElRadioButton ElTable ElTableColumn ElTag ElPagination ElDialog ArchiveCost'.split(' ').map(k=>[k,true]))
stubs.ElTableColumn=defineComponent({render:()=>null})
stubs.ElAlert=defineComponent({setup:(_,ctx)=>()=>h('aside',ctx.slots.title?.())})
let wrappers=[]
const mount=()=>{const w=shallowMount(Dashboard,{global:{stubs,renderStubDefaultSlot:true,directives:{loading:()=>{}}}});wrappers.push(w);return w}
beforeEach(()=>{vi.clearAllMocks();vi.stubGlobal('ResizeObserver',class{observe(){}disconnect(){}});api.archiveMetadata.mockResolvedValue({data:{canAnalyzeCosts:true}});api.archiveAnalysis.mockImplementation(async()=>({data:report()}));api.archiveAnalysisPeople.mockResolvedValue({data:[]})})
afterEach(()=>{wrappers.forEach(w=>w.unmount());wrappers=[];vi.useRealTimers();vi.unstubAllGlobals()})
test('denied manager never requests cross-project financial data',async()=>{api.archiveMetadata.mockResolvedValue({data:{canAnalyzeCosts:false}});const w=mount();await flushPromises();expect(api.archiveAnalysis).not.toHaveBeenCalled();expect(w.findComponent({name:'ElResult'}).exists()).toBe(true)})
test('financial totals use all-result server aggregates rather than current project page',async()=>{const w=mount();await flushPromises();expect(w.text()).toContain('100,000.00');expect(w.text()).toContain('40.00%');expect(w.text()).toContain('41 个项目 / 39 个计入金额');expect(w.text()).toContain('2 个项目未计入金额');expect(w.text()).toContain('2 个历史项目未填开始时间');w.vm.query.page=2;await w.vm.load();expect(api.archiveAnalysis.mock.calls.at(-1)[0].page).toBe(2);expect(w.text()).toContain('100,000.00')})
test('custom date range filters only projects owned by the selected manager',async()=>{const w=mount();await flushPromises();w.vm.range=['2026-03-01','2026-06-30'];w.vm.query.managerId=2;w.vm.query.basis='END';w.vm.query.page=3;await w.vm.search();await flushPromises();expect(api.archiveAnalysis.mock.calls.at(-1)[0]).toMatchObject({from:'2026-03-01',to:'2026-06-30',managerId:2,basis:'END',page:1});w.vm.reset();await flushPromises();expect(api.archiveAnalysis.mock.calls.at(-1)[0]).toMatchObject({managerId:null,basis:'START',mode:'COMPLETE',page:1})})
test('zero is shown as zero while no included projects and undefined margins remain blank',async()=>{let data=report();data.summary={income:0,cost:30,profit:-30,margin:null};data.included=1;api.archiveAnalysis.mockResolvedValue({data});const w=mount();await flushPromises();expect(w.text()).toContain('-30.00');expect(w.text()).toContain('0.00');expect(w.vm.percent(null)).toBe('—');data={...data,included:0};api.archiveAnalysis.mockResolvedValue({data});await w.vm.load();expect(w.findAll('.metric-card>strong').every(card=>card.text()==='—')).toBe(true)})
test('stale report and detail replies cannot overwrite a newer selection',async()=>{const w=mount();await flushPromises();let resolveOld;api.archiveAnalysis.mockImplementationOnce(()=>new Promise(r=>resolveOld=r));const old=w.vm.load();api.archiveAnalysis.mockResolvedValue({data:{...report(),total:7}});await w.vm.load();resolveOld({data:{...report(),total:999}});await old;expect(w.vm.report.total).toBe(7);let oldDetail;api.archiveDetail.mockImplementationOnce(()=>new Promise(r=>oldDetail=r)).mockResolvedValue({data:{id:2,name:'新项目'}});const pending=w.vm.openCost({id:1});await w.vm.openCost({id:2});oldDetail({data:{id:1,name:'旧项目'}});await pending;expect(w.vm.detail.id).toBe(2)})
test('people search is debounced, hides admin and preserves selected label',async()=>{const w=mount();await flushPromises();vi.useFakeTimers();api.archiveAnalysisPeople.mockResolvedValue({data:[{id:1,name:'admin',account:'admin'},{id:4,name:'王甲',account:'wang'}]});w.vm.findPeople('王','manager');w.vm.findPeople('王甲','manager');await vi.advanceTimersByTimeAsync(251);expect(api.archiveAnalysisPeople).toHaveBeenCalledTimes(1);expect(api.archiveAnalysisPeople.mock.calls[0][0]).toBe('王甲');expect(w.vm.options.manager.map(p=>p.id)).toEqual([4]);w.vm.query.managerId=4;api.archiveAnalysisPeople.mockResolvedValue({data:[]});w.vm.findPeople('无匹配','manager');await vi.advanceTimersByTimeAsync(251);expect(w.vm.options.manager[0].name).toBe('王甲')})
test('project date payload preserves explicit dates and uses creation time only as legacy fallback',()=>{expect(projectPayload({start_time:null,create_time:'2025-12-31T10:20:30',expected_end_time:null,end_time:null})).toMatchObject({startTime:'2025-12-31 10:20:30',expectedEndTime:null,endTime:null});expect(projectPayload({start_time:'2026-01-01 00:00:00',expected_end_time:'2026-08-01 09:00:00',end_time:'2026-07-31 12:00:00'})).toMatchObject({startTime:'2026-01-01 00:00:00',expectedEndTime:'2026-08-01 09:00:00',endTime:'2026-07-31 12:00:00'})})