/** * features/chat/components/messages/TransferMatchCard.spec.ts * 用途:匹配卡顶栏「来源失败」横幅过滤——路由未解析失败只行内展示,不进顶栏。 * 依赖:@vue/test-utils、i18n。 * 逻辑:来源失败顶栏(sources[].error_code/error_message)防止“安静全 0”。 * 后端分层后路由失败保持行级(result=blocked_route_not_resolved), * 顶栏需按 error_code 客户端排除(历史卡片回放同样生效)。 */ import { mount } from '@vue/test-utils'; import { describe, expect, it } from 'vitest'; import i18n from '@/i18n'; import TransferMatchCard from './TransferMatchCard.vue'; import type { TransferMatchingItem, TransferMatchingSummary, TransferSourceProgress } from '../../types/transfer.types'; /** 最小合法匹配摘要(TransferMatchingSummary 全必填字段齐备,计数为 0) */ function buildSummary(): TransferMatchingSummary { return { matching_id: 'MTG-T', status: 'completed', business_date: '2026-08-06', expires_at: '2026-08-06T23:59:59', counts: { file_invoice_count: 0, matched_count: 0, executable_count: 0, blocked_count: 0, pending_stock_check_count: 0, invalid_count: 0, }, estimated_transfer_quantity: '0', estimated_external_write_count: 0, matching_hash: 'hash-test', }; } function mountCard(sources: TransferSourceProgress[]) { return mount(TransferMatchCard, { props: { summary: buildSummary(), items: [], selected: new Set(), sources, }, global: { plugins: [i18n] }, }); } describe('TransferMatchCard 来源失败顶栏', () => { it('顶栏不展示 ROUTE_NOT_RESOLVED 来源失败(路由只行内)', () => { // [断言意图] 路由失败不再升级为来源级 failed(后端分层),顶栏排除 // TRANSFER.ROUTE_NOT_RESOLVED(客户端按 error_code 过滤,历史回放同样生效); // 其余来源级失败(如文件 schema 无效)仍必须在顶栏可见。 const wrapper = mountCard([ { source_code: 'A', stage: 'failed', error_code: 'TRANSFER.ROUTE_NOT_RESOLVED', error_message: 'route not resolved' }, { source_code: 'B', stage: 'failed', error_code: 'TRANSFER_FILE_SCHEMA_INVALID', error_message: 'schema' }, ]); const banner = wrapper.find('[data-testid="transfer-match-failed-sources"]'); expect(banner.exists()).toBe(true); expect(banner.text()).not.toContain('route not resolved'); expect(banner.text()).toContain('schema'); }); it('仅路由失败时顶栏不渲染', () => { // [断言意图] 唯一失败来源为路由未解析时,顶栏整体不出现(避免空横幅/全 0 误导)。 const wrapper = mountCard([ { source_code: 'A', stage: 'blocked_route', error_code: 'TRANSFER.ROUTE_NOT_RESOLVED', error_message: 'route not resolved' }, ]); expect(wrapper.find('[data-testid="transfer-match-failed-sources"]').exists()).toBe(false); }); }); /** [2026-10-12 单据数口径修正] 预计 ERP 单据数按执行建单五元组去重。 * 行分组键 = (external_org, source_code, movement_date, from_warehouse, * to_warehouse),与后端 execution_service.prepare_execution 同口径: * 同组多行执行期合并一张 ERP 单,预估数不得等于可执行行数。 */ function buildItem(overrides: Partial): TransferMatchingItem { return { item_id: 1, source_code: 'DBS-AWSBI', material_no: 'MAT-001', material_name: '连接器组件', specification: 'A 型', invoice_no: 'HOM-001', movement_date: '2026-08-05', from_warehouse: 'DBS-TRANSIT', from_quantity: '100', to_warehouse: 'HUB-CN01', to_quantity: '100', result: 'executable', external_org: 'HX-CN01', ...overrides, }; } /** 计数卡第 4 格 = 预计 ERP 单据(可执行/阻断/数量/单据/待查/失效)。 */ function estimateCardText(wrapper: ReturnType): string { const cells = wrapper.find('[data-testid="transfer-match-counts"]').findAll('div.rounded-lg'); return cells[3]?.text() ?? ''; } describe('TransferMatchCard 预计 ERP 单据数(五元组去重)', () => { const sameGroupA = buildItem({ item_id: 1, invoice_no: 'HOM-001' }); const sameGroupB = buildItem({ item_id: 2, invoice_no: 'HOX-002' }); const crossDate = buildItem({ item_id: 3, invoice_no: 'HOM-003', movement_date: '2026-08-04' }); it('计数卡:同组两行 + 跨日一行 → 预估 2(不等于可执行行数 3)', () => { // [断言意图] 锁定五元组去重口径:movement_date 入键,同组合并、跨组拆分, // 历史快照旧口径(=行数)持久值不再上屏。 const wrapper = mount(TransferMatchCard, { props: { summary: buildSummary(), items: [sameGroupA, sameGroupB, crossDate], selected: new Set(), }, global: { plugins: [i18n] }, }); expect(estimateCardText(wrapper).replace(/\D/g, '')).toBe('2'); }); it('生成按钮旁预估随勾选联动:勾选同组两行仍为 1,跨组勾选后为 2', async () => { // [断言意图] 底部预估回答「点生成会出几张单」:仅统计勾选的可执行行并 // 按五元组分组;同组两行勾选=1 张(合并),追加跨日行勾选=2 张。 const wrapper = mount(TransferMatchCard, { props: { summary: buildSummary(), items: [sameGroupA, sameGroupB, crossDate], selected: new Set([1, 2]), }, global: { plugins: [i18n] }, }); expect(wrapper.find('[data-testid="transfer-estimate-selected"]').text()).toContain(':1'); await wrapper.setProps({ selected: new Set([1, 2, 3]) }); expect(wrapper.find('[data-testid="transfer-estimate-selected"]').text()).toContain(':2'); }); it('阻断行不参与预估:勾选阻断行不改变单据数', () => { const blocked = buildItem({ item_id: 4, invoice_no: 'HOM-BLOCKED', result: 'blocked_quantity_mismatch' }); const wrapper = mount(TransferMatchCard, { props: { summary: buildSummary(), items: [sameGroupA, blocked], selected: new Set([1, 4]), }, global: { plugins: [i18n] }, }); expect(wrapper.find('[data-testid="transfer-estimate-selected"]').text()).toContain(':1'); }); });