import { describe, expect, it, vi } from 'vitest' import { mount, flushPromises } from '@vue/test-utils' import ElementPlus from 'element-plus' vi.mock('../../src/components/BusinessAssistant/AssistantField.vue', () => ({ default: { template: '' } })) import Preview from '../../src/components/BusinessAssistant/AssistantQuotePreview.vue' import Draft from '../../src/components/BusinessAssistant/AssistantDraft.vue' import { assistantApplyDraftChanges } from '../../src/utils/assistant-draft-edit.js' import { assistantExcludeDraftRows } from '../../src/utils/assistant-draft-exclusions.js' const missing = () => ({ _rowId: 'pdf-line-24', id: { keyword: '水冷一体管', spec: '白色带线1.5米' }, finalUnitPrice: 29.2, _sourceRow: 24, _sourceText: '报价第24项:水冷一体管;4×29.2', _errors: ['采购明细未匹配,请选择已有记录'], _quotePreview: [{ line: 24, name: '水冷一体管', previousPrice: null, unitPrice: 29.2, newAmount: null, sourceText: '报价第24项:水冷一体管;4×29.2', error: '采购明细未匹配,请选择已有记录' }] }) const fields = [{ key: 'id', label: '采购明细', ref: 'purchase' }, { key: 'finalUnitPrice', label: '新报价', type: 'decimal' }, { key: 'skipCorrection', label: '本项暂不更正', type: 'boolean' }] const draft = () => ({ id: 1, action: 'purchase.correctPrice', status: 'WAIT_CONFIRM', ready: false, version: 1, fields, rows: [], results: [] }) describe('unmatched PDF quotations have a visible escape without deleting source records', () => { it('real draft forwards skip and restore events by original row identity, preserving the PDF data', async () => { const row = missing(), confirmation = draft() const wrapper = mount(Draft, { props: { draft: confirmation, action: { allowed: true }, rows: [row] }, global: { plugins: [ElementPlus], stubs: { AssistantField: true } } }) try { await flushPromises() const button = wrapper.find('button[aria-label="第24项本次跳过"]') expect(button.exists()).toBe(true) expect(wrapper.findComponent(Preview).findAllComponents({ name: 'ElTableColumn' }).some(column => column.props('label') === '本次处理' && column.props('fixed') === 'right')).toBe(true) await button.trigger('click') const event = wrapper.emitted('change')[0][0] expect(event).toEqual({ rowId: 'pdf-line-24', key: 'skipCorrection', value: true }) const changed = assistantApplyDraftChanges([row], event, { action: confirmation.action, fields, draft: confirmation }) await wrapper.setProps({ rows: changed }) expect(wrapper.text()).toContain('已选择跳过,请重新校验预览') expect(wrapper.text()).toContain('报价第24项:水冷一体管;4×29.2') expect(changed[0].finalUnitPrice).toBe(29.2) await wrapper.find('button[aria-label="第24项恢复处理"]').trigger('click') expect(wrapper.emitted('change')[1][0]).toEqual({ rowId: 'pdf-line-24', key: 'skipCorrection', value: false }) await wrapper.setProps({ readOnly: true }) expect(wrapper.find('button[aria-label="第24项恢复处理"]').exists()).toBe(false) } finally { wrapper.unmount() } }) it('busy state cannot emit a skip; previewed skips are not displayed as a green successful correction', async () => { const wrapper = mount(Preview, { props: { busy: true, rows: [{ ...missing()._quotePreview[0], _draftRowId: 'pdf-line-24', _canSkip: true, _skipSelected: true, skipped: true }] }, global: { plugins: [ElementPlus] } }) try { await flushPromises() expect(wrapper.text()).toContain('本次跳过,不修改采购') expect(wrapper.findAll('.assistant-quote-new').length).toBe(0) expect(wrapper.find('button[aria-label="第24项恢复处理"]').element.disabled).toBe(true) await wrapper.find('button[aria-label="第24项恢复处理"]').trigger('click') expect(wrapper.emitted('skip')).toBeUndefined() await wrapper.setProps({ busy: false, rows: [{ ...missing()._quotePreview[0], _draftRowId: 'pdf-line-24', _canSkip: true, _skipSelected: false, skipped: true }] }) expect(wrapper.text()).toContain('已恢复处理,请重新校验预览') expect(wrapper.text()).not.toContain('本次跳过,不修改采购') expect(wrapper.findAll('.assistant-quote-new').length).toBe(0) } finally { wrapper.unmount() } }) it('purchase import drafts expose removal and restoration without editing or deleting business records', async () => { const confirmation = { ...draft(), action: 'purchase.create', fields: [{ key: 'materialName', label: '物品名称' }] } const rows = [{ _rowId: 'good', materialName: '水泵' }, { ...missing(), materialName: '表格中不存在的物品' }] const action = { allowed: true, query: false }, wrapper = mount(Draft, { props: { draft: confirmation, action, rows }, global: { plugins: [ElementPlus], stubs: { AssistantField: true } } }) try { await flushPromises(); await wrapper.find('button[aria-label="移出第24项"]').trigger('click') expect(wrapper.emitted('remove')[0][0]).toEqual(['pdf-line-24']) const result = assistantExcludeDraftRows(rows, wrapper.emitted('remove')[0][0], { draft: confirmation, action }) await wrapper.setProps({ rows: result.rows, excludedRows: result.removed }) expect(wrapper.find('[aria-label="已移出本次确认单的条目"]').text()).toContain('未删除业务记录') await wrapper.find('button[aria-label="恢复表格中不存在的物品"]').trigger('click') expect(wrapper.emitted('restore')[0][0]).toBe('pdf-line-24') await wrapper.setProps({ readOnly: true }) expect(wrapper.find('button[aria-label="移出第1项"]').exists()).toBe(false) } finally { wrapper.unmount() } }) })