cmvr-wms/ruoyi-ui-vue3/tests/runtime/testing-analysis-parameter-runs.spec.js

91 lines
6.9 KiB
JavaScript

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'
import { calibrationData } from '@/views/testing/calibrations/model'
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 }, { id: 13, robot_id: 9, name: '本机分析参数', kind: 'ANALYSIS_PARAMETERS', version: 3 }]
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, CalibrationScreenshot: { props: ['partCode', 'modelValue'], template: '<div class="frozen-screenshot">{{partCode}} #{{modelValue}}</div>' } } } })
wrappers.push(wrapper); await flushPromises(); await wrapper.vm.openRun(); await flushPromises(); return wrapper
}
test('new run selects analysis parameters through the existing robot calibration selector', async () => {
const wrapper = await detail(); expect(wrapper.vm.calibrations.map(row => row.id)).toEqual([11, 13])
expect(document.body.textContent).not.toContain('关联分析参数(可选)'); wrapper.vm.runForm.calibrationIds = [11, 13]
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, 13]); expect(sent).not.toHaveProperty('analysisParameterId'); expect(sent).not.toHaveProperty('analysisParameterVersion')
expect(api.testing.mock.calls.some(call => call[0] === '/analysis-parameters')).toBe(false)
})
test('run calibration association remains optional', async () => {
const wrapper = await detail()
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([])
})
test('version conflict retains selected data and keeps the run dialog open', async () => {
const wrapper = await detail(); wrapper.vm.runForm.calibrationIds = [13]; 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.calibrationIds).toEqual([13])
expect(document.body.textContent).toContain('数据已更新')
})
test('selected run displays both frozen analysis regions without fetching current calibration data', async () => {
const data = calibrationData('ANALYSIS_PARAMETERS'); data.images = { ANALYSIS_TRAJECTORY: 101, ANALYSIS_POSE: 102 }
data.parameters.TRAJECTORY.p1R = '-115.9060'; data.parameters.TRAJECTORY.circularReferenceSegment = '1/4'; data.parameters.POSE.measurementDwellTime = '1.00'
const wrapper = await detail(); wrapper.vm.closeRun()
wrapper.vm.runData = { run: { id: 77, robotSnapshot: {}, calibrations: [{ id: 13, project_id: 7, robot_id: 9, kind: 'ANALYSIS_PARAMETERS', name: '原标定版本', version: 3, data_json: JSON.stringify(data) }] }, task: { id: 4 }, configuration: { cases: {}, schema: { fields: [] } }, batches: [] }
wrapper.vm.calibrationExpanded = [13]; api.testing.mockClear(); await flushPromises()
expect(wrapper.findAll('.frozen-screenshot').map(item => item.text())).toEqual(['ANALYSIS_TRAJECTORY #101', 'ANALYSIS_POSE #102'])
expect(wrapper.findAll('[data-parameter]')).toHaveLength(28)
expect(wrapper.find('[data-parameter="p1R"] input').element.value).toBe('-115.9060')
expect(wrapper.find('[data-parameter="measurementDwellTime"] input').element.value).toBe('1.00')
expect(api.testing).not.toHaveBeenCalled()
})
/** 用可控返回顺序模拟快速切换轮次,旧快照不能覆盖已经展示的新轮次。 */
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)
})