/** * features/chat/components/shipping-review/ReviewDocumentEvidenceDrawer.spec.ts * 用途:锁定 PDF 证据浮窗/侧栏的打开、导航、展开/固定、resize 句柄与无 bbox 引文展示契约。 * 依赖:vitest、@vue/test-utils、vue-i18n。 * 逻辑:验证上一/下一证据、关闭/展开/固定 emit、「更多」内退出后保留、八向 resize,以及无 bbox 时展示引文。 */ import { afterEach, describe, expect, it, vi } from 'vitest'; import { flushPromises, mount } from '@vue/test-utils'; import { createI18n } from 'vue-i18n'; import ReviewDocumentEvidenceDrawer from './ReviewDocumentEvidenceDrawer.vue'; import type { ShippingOrderReviewEvidence } from '@/features/chat/types/shipping.api.types'; vi.mock('./ReviewDocumentPreview.vue', () => ({ default: { name: 'ReviewDocumentPreview', template: '
', }, })); const evidenceList: ShippingOrderReviewEvidence[] = [ { evidence_id: 'ev-1', field_id: 'fld-po', page_no: 1, quote: 'PO-A', bbox: { x0: 0.1, y0: 0.1, x1: 0.2, y1: 0.2 }, source: 'document_cell', highlight_quality: 'candidate', highlight_source: 'pdf_coordinate', highlight_bbox: { x0: 0.1, y0: 0.1, x1: 0.2, y1: 0.2 }, coordinate_status: 'candidate', coordinate_issue_codes: [], evidence_ref: null, token_ids: [], value_match_status: 'matched', }, { evidence_id: 'ev-2', field_id: 'fld-qty', line_index: 0, page_no: 2, quote: '10 EA', bbox: null, source: 'document_cell', highlight_quality: 'unavailable', highlight_source: 'machine_extraction', highlight_bbox: null, coordinate_status: 'unavailable', coordinate_issue_codes: ['coordinate_missing'], evidence_ref: null, token_ids: [], value_match_status: 'matched', }, ]; /** * 挂载证据抽屉。 */ function mountDrawer(overrides: Record = {}) { const i18n = createI18n({ legacy: false, locale: 'zh-CN', messages: { 'zh-CN': { chat: { shippingOrderReview: { previewTitle: 'PDF', previewLoading: '加载', previewUnavailable: '不可用', emptyValue: '空', evidenceAt: '第 {page} 页', evidenceNoHighlight: '无坐标高亮', evidenceInvalidHighlight: '坐标无效', locateUnavailableHint: '无可靠坐标', restoreMachine: '恢复机器值', llmSuggestion: { request: '请求 AI' }, workbench: { pinPdf: '退出后保留', unpinPdf: '退出后关闭', closePdf: '关闭', pinPdfHint: '退出后保留', unpinPdfHint: '退出后关闭', expandPdf: '展开', collapsePdf: '收起', expandPdfHint: '放大', collapsePdfHint: '收起', dockPdf: '固定为侧栏', undockPdf: '取消固定', dockPdfHint: '双栏', undockPdfHint: '浮窗', movePdfLeft: '移至左侧', movePdfRight: '移至右侧', movePdfSideHint: '换侧', moreActions: '更多', }, highlightQuality: { verified: '已验证', candidate: '候选', unavailable: '无坐标', invalid: '坐标无效', }, highlightSource: { pdf_coordinate: 'PDF 原生坐标', machine_extraction: '机器抽取', }, coordinateIssues: { coordinate_missing: '未获得可靠字段级坐标', }, evidenceDrawer: { title: '证据核对', pageUnknown: '未知页', prevEvidence: '上一证据', nextEvidence: '下一证据', field: '字段:{field}', currentValue: '当前值', machineValue: '机器值', source: '来源', highlightQuality: '高亮质量', highlightSource: '坐标来源', layoutPath: '版面路径', detailsToggle: '证据详情', }, }, }, }, }, }); return mount(ReviewDocumentEvidenceDrawer, { props: { open: true, pinned: false, expanded: false, docked: false, floatSide: 'right', src: 'blob:mock', evidence: evidenceList[0], evidenceList, fieldLabel: '客户 PO', draftValue: 'PO-A', machineValue: 'PO-A', valueSourceLabel: '系统识别', showAiActions: true, ...overrides, }, global: { plugins: [i18n] }, attachTo: document.body, }); } afterEach(() => { document.body.innerHTML = ''; localStorage.clear(); }); describe('ReviewDocumentEvidenceDrawer', () => { it('renders floating panel when open and emits close/expand/dock; pin lives in more menu', async () => { const wrapper = mountDrawer(); const root = wrapper.find('[data-testid="review-document-evidence-drawer"]'); expect(root.exists()).toBe(true); expect(root.attributes('data-presentation')).toBe('floating'); expect(root.attributes('role')).toBe('dialog'); expect(root.attributes('aria-modal')).toBe('true'); expect(root.classes()).toContain('fixed'); expect(root.attributes('data-width')).toBeTruthy(); expect(root.attributes('data-height')).toBeTruthy(); expect(wrapper.find('[data-testid="review-evidence-ordinal"]').text()).toContain('1/2'); await wrapper.find('[data-testid="review-evidence-close"]').trigger('click'); expect(wrapper.emitted('close')).toBeTruthy(); await wrapper.find('[data-testid="review-evidence-toggle-expand"]').trigger('click'); expect(wrapper.emitted('toggle-expand')).toBeTruthy(); await wrapper.find('[data-testid="review-evidence-toggle-dock"]').trigger('click'); expect(wrapper.emitted('toggle-dock')).toBeTruthy(); // 关键断言:低频操作收入「更多」,打开后再点「退出后保留」。 expect(wrapper.find('[data-testid="review-evidence-toggle-pin"]').exists()).toBe(false); await wrapper.find('[data-testid="review-evidence-more"]').trigger('click'); expect(wrapper.find('[data-testid="review-evidence-more-menu"]').exists()).toBe(true); await wrapper.find('[data-testid="review-evidence-toggle-pin"]').trigger('click'); expect(wrapper.emitted('toggle-pin')).toBeTruthy(); }); it('exposes eight resize handles on floating panel only', () => { const floating = mountDrawer({ docked: false }); for (const dir of ['n', 's', 'e', 'w', 'ne', 'nw', 'se', 'sw']) { expect(floating.find(`[data-testid="review-evidence-resize-${dir}"]`).exists()).toBe(true); } floating.unmount(); const docked = mountDrawer({ docked: true }); expect(docked.find('[data-testid="review-evidence-resize-se"]').exists()).toBe(false); }); it('persists float geometry to localStorage after resize end', async () => { const wrapper = mountDrawer(); const handleEl = wrapper.find('[data-testid="review-evidence-resize-se"]').element; handleEl.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true, clientX: 100, clientY: 100, })); window.dispatchEvent(new PointerEvent('pointermove', { clientX: 180, clientY: 160 })); window.dispatchEvent(new PointerEvent('pointerup')); await flushPromises(); const raw = localStorage.getItem('hxops.shippingReview.evidenceFloat.v1'); // 关键断言:拖拽结束写入尺寸位置,供下次打开复用。 expect(raw).toBeTruthy(); const parsed = JSON.parse(String(raw)) as { width: number; height: number }; expect(parsed.width).toBeGreaterThanOrEqual(360); expect(parsed.height).toBeGreaterThanOrEqual(320); }); it('closes floating panel on Escape', async () => { const wrapper = mountDrawer({ open: true, docked: false }); await flushPromises(); document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true, cancelable: true, })); // 关键断言:浮动态 Esc 关闭,便于键盘退出。 expect(wrapper.emitted('close')).toBeTruthy(); }); it('uses complementary semantics when docked as sidebar', () => { const wrapper = mountDrawer({ docked: true }); const root = wrapper.find('[data-testid="review-document-evidence-drawer"]'); expect(root.attributes('data-presentation')).toBe('docked'); expect(root.attributes('role')).toBe('complementary'); expect(root.attributes('aria-modal')).toBeUndefined(); expect(root.classes()).toContain('relative'); expect(root.classes()).not.toContain('fixed'); }); it('enables next evidence and disables prev on first item', async () => { const wrapper = mountDrawer(); expect((wrapper.find('[data-testid="review-evidence-prev"]').element as HTMLButtonElement).disabled).toBe(true); expect((wrapper.find('[data-testid="review-evidence-next"]').element as HTMLButtonElement).disabled).toBe(false); await wrapper.find('[data-testid="review-evidence-next"]').trigger('click'); expect(wrapper.emitted('next-evidence')).toBeTruthy(); }); it('shows quote when evidence has no bbox', () => { const wrapper = mountDrawer({ evidence: evidenceList[1] }); expect(wrapper.find('[data-testid="review-evidence-no-bbox"]').exists()).toBe(true); expect(wrapper.find('[data-testid="review-evidence-no-bbox"]').text()).toContain('10 EA'); }); it('does not render when closed', () => { const wrapper = mountDrawer({ open: false }); expect(wrapper.find('[data-testid="review-document-evidence-drawer"]').exists()).toBe(false); }); });