import { afterEach, beforeEach, expect, test, vi } from 'vitest' import { mount, flushPromises } from '@vue/test-utils' import { reactive } from 'vue' import ElementPlus from 'element-plus' import TaskDetail from '@/views/testing/components/TaskDetail.vue' const api = vi.hoisted(() => ({ testing: vi.fn(), push: vi.fn(), route: { query: {} }, conflict: false, runs: [] })) vi.mock('@/views/testing/shared/api', () => api) vi.mock('vue-router', () => ({ useRoute: () => api.route, useRouter: () => ({ push: api.push }), onBeforeRouteLeave: vi.fn(), onBeforeRouteUpdate: vi.fn() })) const wrappers = [] /** 模拟真实任务、同机器人标定和跨项目共享参数,不访问业务数据库。 */ beforeEach(() => { vi.clearAllMocks(); api.runs = []; api.route = reactive({ query: {} }); api.conflict = false api.testing.mockImplementation(async (path, method = 'get', body, params) => { if (path === '/tasks/4' && method === 'get') return { task: { id: 4, project_id: 7, robot_id: 9, name: '测试任务A', type_code: 'PRE', description: '', owner_name: '测试人员' }, runs: api.runs, configuration: {} } if (path === '/capabilities') return { write: true } if (path === '/catalog') return { types: [{ code: 'PRE', name: '预实验' }], metrics: [], templates: [] } if (path === '/projects/7') return { role: 'MANAGER', robots: [{ id: 9, name: '机器人A' }] } if (path === '/projects/7/calibrations') return [{ id: 11, robot_id: 9, name: '本机姿态', kind: 'POSE', version: 2 }, { id: 12, robot_id: 10, name: '另一机器人', kind: 'POSE', version: 1 }] if (path === '/analysis-parameters') return { items: params?.keyword ? [{ id: 6, name: '新搜索记录', mode: 'TRAJECTORY', version: 1 }] : [{ id: 5, name: '共享位姿', mode: 'POSE', version: 3 }], total: 1 } if (path === '/tasks/4/runs' && method === 'post') { if (api.conflict) throw new Error('数据已更新,请刷新后重试') api.runs = [{ id: 77, round_no: 2 }]; return 77 } if (path === '/runs/77') return { run: { id: 77, robotSnapshot: {}, calibrations: [], analysisParameters: {} }, task: { id: 4 }, configuration: { cases: {}, schema: { fields: [] } }, batches: [] } throw new Error(`Unexpected request: ${path}`) }) }) /** 清理真实弹窗浮层,避免后续测试看到上一轮表单。 */ afterEach(() => { wrappers.splice(0).forEach(wrapper => wrapper.unmount()); document.body.innerHTML = '' }) /** 挂载真实轮次表单,隔离无关结果编辑组件及其网络请求。 */ async function detail() { const wrapper = mount(TaskDetail, { attachTo: document.body, props: { taskId: 4 }, global: { plugins: [ElementPlus], stubs: { TaskForm: true, ThresholdEditor: true, ResultEditor: true } } }) wrappers.push(wrapper); await flushPromises(); await wrapper.vm.openRun(); await flushPromises(); return wrapper } test('new run can select shared analysis parameters separately from robot calibration', async () => { const wrapper = await detail(); expect(wrapper.vm.calibrations.map(row => row.id)).toEqual([11]) expect(document.body.textContent).toContain('关联分析参数'); wrapper.vm.runForm.calibrationIds = [11] wrapper.vm.selectAnalysisParameter(5); await wrapper.vm.createRun() const sent = api.testing.mock.calls.find(call => call[0] === '/tasks/4/runs' && call[1] === 'post')[2] expect(sent.calibrationIds).toEqual([11]); expect(sent.analysisParameterId).toBe(5); expect(sent.analysisParameterVersion).toBe(3) }) test('run association is optional and remote search does not discard the selected version', async () => { const wrapper = await detail(); wrapper.vm.selectAnalysisParameter(5); await wrapper.vm.searchAnalysisParameters('新搜索') expect(wrapper.vm.runForm.analysisParameterVersion).toBe(3); wrapper.vm.selectAnalysisParameter(null) await wrapper.vm.createRun() const sent = api.testing.mock.calls.find(call => call[0] === '/tasks/4/runs' && call[1] === 'post')[2] expect(sent.analysisParameterId).toBeNull(); expect(sent.analysisParameterVersion).toBeNull() }) test('version conflict retains selected data and keeps the run dialog open', async () => { const wrapper = await detail(); wrapper.vm.selectAnalysisParameter(5); wrapper.vm.runForm.notes = '本轮说明'; api.conflict = true await wrapper.vm.createRun(); await flushPromises() expect(wrapper.vm.runOpen).toBe(true); expect(wrapper.vm.runForm.notes).toBe('本轮说明'); expect(wrapper.vm.runForm.analysisParameterId).toBe(5) expect(document.body.textContent).toContain('数据已更新') }) /** 用可控返回顺序模拟快速切换轮次,旧快照不能覆盖已经展示的新轮次。 */ test.each(['loadRun', 'refreshSummary'])('late %s response cannot replace the currently selected run snapshot', async method => { api.runs = [{ id: 77, round_no: 1 }, { id: 88, round_no: 2 }] const wrapper = await detail(); wrapper.vm.closeRun() const requests = new Map(), original = api.testing.getMockImplementation() api.testing.mockImplementation((path, ...args) => path.startsWith('/runs/') ? new Promise(resolve => requests.set(path, resolve)) : original(path, ...args)) const oldRead = wrapper.vm[method]() api.route.query.runId = 88; await flushPromises() const payload = id => ({ run: { id, robotSnapshot: {}, calibrations: [], analysisParameters: {} }, task: { id: 4 }, configuration: { cases: {}, schema: { fields: [] } }, batches: [] }) requests.get('/runs/88')(payload(88)); await flushPromises() expect(wrapper.vm.runData.run.id).toBe(88) requests.get('/runs/77')(payload(77)); await oldRead; await flushPromises() expect(wrapper.vm.runData.run.id).toBe(88) })