116 lines
8.5 KiB
JavaScript
116 lines
8.5 KiB
JavaScript
import { afterEach, beforeEach, expect, test, vi } from 'vitest'
|
||
import { mount, flushPromises } from '@vue/test-utils'
|
||
import ElementPlus from 'element-plus'
|
||
import AnalysisParameterPage from '@/views/testing/analysisParameters/index.vue'
|
||
import ScreenshotCard from '@/views/testing/analysisParameters/ScreenshotCard.vue'
|
||
import { parameterValues } from '@/views/testing/analysisParameters/model'
|
||
|
||
const api = vi.hoisted(() => ({ testing: vi.fn(), uploadAnalysisParameters: vi.fn(), analysisParameterEvidence: vi.fn(), recognizeAnalysisParameters: vi.fn(),
|
||
confirm: vi.fn(), success: vi.fn(), info: vi.fn(), rows: [], write: true }))
|
||
vi.mock('@/views/testing/shared/api', () => api)
|
||
vi.mock('element-plus', async original => ({ ...await original(), ElMessageBox: { confirm: api.confirm }, ElMessage: { success: api.success, info: api.info } }))
|
||
const wrappers = []
|
||
|
||
/** 构造可控网络延迟,验证图片和弹窗会话改变后旧响应失效。 */
|
||
function deferred() { let resolve; const promise = new Promise(done => { resolve = done }); return { promise, resolve } }
|
||
|
||
/** 独立资料没有项目/机器人字段,响应保持真实的分页及详情格式。 */
|
||
beforeEach(() => {
|
||
vi.clearAllMocks(); api.rows = []; api.write = true; api.confirm.mockResolvedValue('confirm')
|
||
vi.stubGlobal('URL', { createObjectURL: vi.fn(), revokeObjectURL: vi.fn() })
|
||
api.testing.mockImplementation(async (path, method = 'get', body) => {
|
||
if (method === 'post' || method === 'put') {
|
||
const saved = { ...structuredClone(body), id: 5, version: method === 'put' ? 4 : 1 }; api.rows = [saved]; return saved
|
||
}
|
||
if (path === '/capabilities') return { write: api.write }
|
||
if (path === '/analysis-parameters') return { items: api.rows, total: api.rows.length }
|
||
if (path === '/analysis-parameters/5') return { id: 5, name: '位姿参数A', mode: 'POSE', imageId: 77, version: 3, values: { p1R: '-115.9060' } }
|
||
if (/\/analysis-parameter-images\/\d+\/info$/.test(path)) return { id: Number(path.match(/\d+/)[0]), mode: 'POSE', name: '当前参数.png', width: 1400, height: 800 }
|
||
throw new Error(`Unexpected request: ${path}`)
|
||
})
|
||
api.analysisParameterEvidence.mockImplementation(async id => `blob:parameter-${id}`)
|
||
api.recognizeAnalysisParameters.mockImplementation(async id => ({ imageId: id, mode: 'POSE', data: { mode: 'POSE', values: { p1R: '-115.9060', measurementDwellTime: '1' } } }))
|
||
api.uploadAnalysisParameters.mockResolvedValue({ id: 78, mode: 'POSE' })
|
||
})
|
||
|
||
/** 释放真实 Element Plus 浮层和鉴权 Blob,避免跨用例保留旧表单。 */
|
||
afterEach(() => { wrappers.splice(0).forEach(wrapper => wrapper.unmount()); document.body.innerHTML = ''; vi.unstubAllGlobals() })
|
||
|
||
/** 挂载真实参数表单,仅隔离文件网络组件以免列表测试隐式下载图片。 */
|
||
async function page() {
|
||
const wrapper = mount(AnalysisParameterPage, { attachTo: document.body, global: { plugins: [ElementPlus], stubs: { ScreenshotCard: { template: '<div />' } } } })
|
||
wrappers.push(wrapper); await flushPromises(); return wrapper
|
||
}
|
||
|
||
/** 挂载真实截图组件并接收 v-model,走上传后当前图自动识别的完整顺序。 */
|
||
async function screenshot(props = {}) {
|
||
let wrapper
|
||
wrapper = mount(ScreenshotCard, { props: { mode: 'POSE', modelValue: 77, ...props, 'onUpdate:modelValue': id => wrapper.setProps({ modelValue: id }) }, global: { plugins: [ElementPlus] } })
|
||
wrappers.push(wrapper); await flushPromises(); return wrapper
|
||
}
|
||
|
||
test('both modes contain only requested fields and never apply sample defaults', async () => {
|
||
const values = parameterValues('TRAJECTORY', { p1R: '-115.9060', circularReferenceSegment: '1/4', diagramChoice: '1' })
|
||
expect(Object.keys(values)).toHaveLength(17); expect(values.p1R).toBe('-115.9060'); expect(values.circularReferenceSegment).toBe('1/4')
|
||
expect(values.c6c7).toBe(''); expect(values).not.toHaveProperty('diagramChoice')
|
||
const wrapper = await page(); await wrapper.vm.openForm(); await flushPromises()
|
||
expect(document.querySelectorAll('[data-parameter]')).toHaveLength(17); expect(wrapper.text()).not.toContain('选择测试项目')
|
||
await wrapper.vm.changeMode('POSE'); await flushPromises()
|
||
expect(document.querySelectorAll('[data-parameter]')).toHaveLength(11); expect(wrapper.vm.form.values.p1R).toBe('')
|
||
})
|
||
|
||
test('current screenshot values are editable and saved as an independent record', async () => {
|
||
const wrapper = await page(); await wrapper.vm.openForm(); await wrapper.vm.changeMode('POSE')
|
||
wrapper.vm.form.name = '共享位姿'; wrapper.vm.form.imageId = 77
|
||
await wrapper.vm.fillRecognized({ imageId: 77, mode: 'POSE', data: { mode: 'POSE', values: { p1R: '-115.9060', p1Y: '550', p1Yaw: '17.442', measurementDwellTime: '1' } } })
|
||
wrapper.vm.form.values.p1X = '100.000'; await wrapper.vm.save(); await flushPromises()
|
||
const saved = api.testing.mock.calls.find(call => call[1] === 'post')
|
||
expect(saved[0]).toBe('/analysis-parameters'); expect(saved[2].values.p1X).toBe('100.000'); expect(saved[2].values.p1R).toBe('-115.9060')
|
||
expect(saved[2].imageId).toBe(77); expect(saved[2]).not.toHaveProperty('projectId'); expect(wrapper.vm.rows[0].name).toBe('共享位姿')
|
||
})
|
||
|
||
test('edit uses the fetched version and immediately refreshes the saved list', async () => {
|
||
const wrapper = await page(); await wrapper.vm.openForm({ id: 5 }); wrapper.vm.form.values.p1R = '-99.00'
|
||
await wrapper.vm.save(); await flushPromises()
|
||
const saved = api.testing.mock.calls.find(call => call[1] === 'put')
|
||
expect(saved[0]).toBe('/analysis-parameters/5'); expect(saved[2].version).toBe(3); expect(saved[2].values.p1R).toBe('-99.00')
|
||
expect(wrapper.vm.rows[0].version).toBe(4); expect(wrapper.vm.rows[0].values.p1R).toBe('-99.00')
|
||
})
|
||
|
||
test('old image and closed form candidates cannot overwrite a current form', async () => {
|
||
const wrapper = await page(); await wrapper.vm.openForm({ id: 5 })
|
||
const candidate = { imageId: 77, mode: 'POSE', data: { mode: 'POSE', values: { p1R: '2' } } }
|
||
wrapper.vm.form.imageId = 78; await wrapper.vm.fillRecognized(candidate); expect(wrapper.vm.form.values.p1R).toBe('-115.9060')
|
||
wrapper.vm.form.imageId = 77; const wait = deferred(); api.confirm.mockReturnValueOnce(wait.promise)
|
||
const filling = wrapper.vm.fillRecognized(candidate); wrapper.vm.closeForm(); await wrapper.vm.openForm({ id: 5 })
|
||
wait.resolve('confirm'); await filling; expect(wrapper.vm.form.values.p1R).toBe('-115.9060')
|
||
})
|
||
|
||
test('edits made while replacement confirmation is open remain intact', async () => {
|
||
const wrapper = await page(); await wrapper.vm.openForm({ id: 5 }); const wait = deferred(); api.confirm.mockReturnValueOnce(wait.promise)
|
||
const filling = wrapper.vm.fillRecognized({ imageId: 77, mode: 'POSE', data: { mode: 'POSE', values: { p1R: '2' } } })
|
||
wrapper.vm.form.values.p1R = '77.00'; wait.resolve('confirm'); await filling
|
||
expect(wrapper.vm.form.values.p1R).toBe('77.00')
|
||
})
|
||
|
||
test('read-only users can inspect but cannot save or trigger recognition', async () => {
|
||
api.write = false; const wrapper = await page(); await wrapper.vm.openForm({ id: 5 }, true); await wrapper.vm.save()
|
||
expect(api.testing.mock.calls.some(call => ['post', 'put'].includes(call[1]))).toBe(false)
|
||
const card = await screenshot({ readonly: true }); await card.vm.recognize(); await card.vm.choose({ raw: new File(['a'], '参数.png') })
|
||
expect(api.recognizeAnalysisParameters).not.toHaveBeenCalled(); expect(api.uploadAnalysisParameters).not.toHaveBeenCalled()
|
||
})
|
||
|
||
test('upload recognizes the newly selected image rather than the previously displayed one', async () => {
|
||
const card = await screenshot(); await card.vm.choose({ raw: new File(['new-image'], '新参数.png', { type: 'image/png' }) }); await flushPromises()
|
||
expect(api.uploadAnalysisParameters.mock.calls[0][0]).toBe('POSE'); expect(api.recognizeAnalysisParameters.mock.calls[0][0]).toBe(78)
|
||
expect(card.emitted('recognized')[0][0].imageId).toBe(78); expect(card.vm.preview).toBe('blob:parameter-78')
|
||
})
|
||
|
||
test('late recognition for another image is discarded and preview URLs are released', async () => {
|
||
const card = await screenshot(); const wait = deferred(); api.recognizeAnalysisParameters.mockReturnValueOnce(wait.promise)
|
||
const work = card.vm.recognize(); await card.setProps({ modelValue: 78 }); await flushPromises()
|
||
wait.resolve({ imageId: 77, mode: 'POSE', data: { mode: 'POSE', values: { p1R: '2' } } }); await work
|
||
expect(card.emitted('recognized')).toBeUndefined(); expect(card.vm.preview).toBe('blob:parameter-78')
|
||
card.unmount(); wrappers.splice(wrappers.indexOf(card), 1); expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:parameter-78')
|
||
})
|