/** * features/chat/shipping/normalizeShippingProcessing.spec.ts * 用途:验收 snake_case → camelCase 规范与缺字段安全默认。 * 依赖:Vitest、normalizeShippingProcessing。 */ import { describe, expect, it } from 'vitest'; import { normalizeShippingProcessingProjection, normalizeShippingProcessingWorkItems, } from './normalizeShippingProcessing'; describe('normalizeShippingProcessingProjection', () => { it('maps snake_case backend payload to camelCase DTO', () => { const normalized = normalizeShippingProcessingProjection({ work_item_type: 'order_task', work_item_no: 'OT-1', source_type: 'direct_pdf', root_order_task_no: 'OT-1', shipping_notice_no: null, conversation_id: 'conv-1', submission_no: 'SOS-1', intake_no: 'SNI-1', status: 'waiting_review', stage: 'waiting_review', progress_version: 3, last_activity_at: '2026-08-04T12:00:00Z', current_gate: { gate_code: 'field_conflict_resolution', status: 'blocked', next_action_code: 'resolve_field_conflicts', blocking_reason_code: 'pdf_preferred_critical_conflict', attempt_no: 1, }, process_gate: { execution_no: 'EXE-1', status: 'blocked', current_gate_code: 'inventory_preparation', expected_gate_version: 3, current_blocking_gate: { gate_code: 'inventory_preparation', status: 'blocked_manual', evaluation_version: 3, }, next_action_code: 'retry_inventory_preparation', allowed_actions: ['retry_inventory_preparation', 'upload_inventory_file'], }, available_actions: ['open_review'], ui_state: { status: 'waiting_human', stage: 'order_review', blocking_reason: { code: 'pdf_preferred_critical_conflict' }, primary_action: { code: 'resolve_field_conflicts', i18n_key: 'action.resolve_field_conflicts' }, secondary_actions: [{ code: 'view_details', enabled: true }], }, source_summary: { email_subject: null, filename: 'a.pdf', document_id: 17, document_count: 1, parsed_document_count: 1, candidate_count: 1, customer_name: 'ACME', po_number: 'PO-1', failure_reason_code: null, parse_attempt_count: 0, asset_status: 'ready', }, child_order_tasks: [], }); expect(normalized).not.toBeNull(); expect(normalized!.workItemNo).toBe('OT-1'); expect(normalized!.sourceType).toBe('direct_pdf'); expect(normalized!.status).toBe('waiting_review'); expect(normalized!.currentGate?.gateCode).toBe('field_conflict_resolution'); expect(normalized!.currentGate?.attemptNo).toBe(1); expect(normalized!.processGate).toMatchObject({ execution_no: 'EXE-1', current_gate_code: 'inventory_preparation', expected_gate_version: 3, next_action_code: 'retry_inventory_preparation', }); expect(normalized!.sourceSummary.filename).toBe('a.pdf'); expect(normalized!.sourceSummary.documentId).toBe(17); expect(normalized!.sourceSummary.assetStatus).toBe('ready'); expect(normalized!.availableActions).toEqual(['resolve_field_conflicts', 'view_details']); expect(normalized!.uiState).toMatchObject({ status: 'waiting_human', stage: 'order_review', blocking_reason: { code: 'pdf_preferred_critical_conflict', i18nKey: null, details: {} }, primary_action: { code: 'resolve_field_conflicts', i18nKey: 'action.resolve_field_conflicts', parameters: {} }, secondary_actions: [{ code: 'view_details', enabled: true }], }); }); it('normalizes stage_nodes from backend', () => { const normalized = normalizeShippingProcessingProjection({ work_item_no: 'OT-2', work_item_type: 'order_task', status: 'processing', stage: 'document_parsing', stage_nodes: [ { stage: 'upload_received', absolute_index: 0, status: 'passed' }, { stage: 'document_parsing', absolute_index: 2, status: 'running' }, ], }); expect(normalized?.stageNodes).toHaveLength(2); expect(normalized?.stageNodes?.[1]?.status).toBe('running'); expect(normalized?.stageNodes?.[1]?.absoluteIndex).toBe(2); }); it('preserves explainability message and next_action on blocking_reason', () => { // v4 阻塞原因具体化:冻结 explainability 契约透传的 message / next_action // 是「阻塞原因行」与「下一步建议动作」的数据源,归一化不得剥离。 const normalized = normalizeShippingProcessingProjection({ work_item_no: 'OT-REASON', work_item_type: 'order_task', status: 'waiting_human', stage: 'order_review', ui_state: { status: 'waiting_human', stage: 'order_review', blocking_reason: { code: 'customer_unresolved', message: '客户尚未确认', next_action: 'confirm_customer', }, primary_action: null, secondary_actions: [], }, source_summary: { email_subject: null, filename: 'reason.pdf', document_id: 91, document_count: 1, parsed_document_count: 1, candidate_count: 1, customer_name: null, po_number: null, failure_reason_code: null, parse_attempt_count: 0, asset_status: 'ready', }, child_order_tasks: [], }); const reason = normalized?.uiState?.blocking_reason; expect(reason?.code).toBe('customer_unresolved'); expect(reason?.message).toBe('客户尚未确认'); expect(reason?.nextAction).toBe('confirm_customer'); }); it('normalizes the EML status-partition file count', () => { const normalized = normalizeShippingProcessingProjection({ work_item_no: 'INTAKE:SNI-EML', work_item_type: 'order_task', aggregate_summary: { intake_no: 'SNI-EML', total: 3, in_group: 2, }, }); expect(normalized?.aggregateSummary).toMatchObject({ intakeNo: 'SNI-EML', total: 3, inGroup: 2, }); }); it('保留后端五阶段生命周期入口,包括 freight_upload', () => { const normalized = normalizeShippingProcessingProjection({ work_item_no: 'OT-LIFECYCLE-5', work_item_type: 'order_task', status: 'waiting_review', stage: 'freight_uploading', ui_state: { status: 'waiting_human', stage: 'freight_uploading', lifecycle_stage: 'freight_upload', lifecycle_entries: [ { code: 'intake_parse', state: 'available', target_surface: 'order_lifecycle_workbench' }, { code: 'order_review', state: 'available', target_surface: 'order_lifecycle_workbench' }, { code: 'fulfillment_prep', state: 'available', target_surface: 'order_lifecycle_workbench' }, { code: 'freight_upload', state: 'current', target_surface: 'order_lifecycle_workbench' }, { code: 'upload_complete', state: 'unavailable', target_surface: 'order_lifecycle_workbench' }, ], }, }); expect(normalized?.uiState?.lifecycleEntries).toEqual([ { code: 'intake_parse', state: 'available', targetSurface: 'order_lifecycle_workbench', disabledReasonKey: null }, { code: 'order_review', state: 'available', targetSurface: 'order_lifecycle_workbench', disabledReasonKey: null }, { code: 'fulfillment_prep', state: 'available', targetSurface: 'order_lifecycle_workbench', disabledReasonKey: null }, { code: 'freight_upload', state: 'current', targetSurface: 'order_lifecycle_workbench', disabledReasonKey: null }, { code: 'upload_complete', state: 'unavailable', targetSurface: 'order_lifecycle_workbench', disabledReasonKey: null }, ]); }); it('normalizes structured inventory candidate facts', () => { const projection = normalizeShippingProcessingProjection({ work_item_no: 'SN-001', work_item_type: 'order_task', source_type: 'eml', submission_no: 'SUB-1', intake_no: 'INT-1', status: 'processing', stage: 'inventory_matching', progress_version: 2, last_activity_at: '2026-08-08T22:00:00', source_summary: {}, available_actions: [], child_order_tasks: [], blocking_reason: { code: 'inventory_candidates_missing', i18n_key: 'workspace.shippingNotice.processGate.reasons.inventory_candidates_missing', }, facts: { inventory_milestones: { candidates_calculated: 'completed', candidates_frozen: 'completed', snapshots_ready: 'pending', matched: 'pending', }, candidate_set_id: 12, candidate_set_version: 3, candidate_set_status: 'frozen', candidate_source_codes: ['DBS_DUB'], prepared_source_codes: ['DBS_DUB'], pending_source_codes: ['DBS_RTM'], failed_source_codes: ['DBS_SIN'], source_attempts: { DBS_DUB: { status: 'consumed', attempt_no: 1 }, }, }, }); expect(projection?.facts?.candidateSetId).toBe(12); expect(projection?.facts?.candidateSetVersion).toBe(3); expect(projection?.facts?.candidateSetStatus).toBe('frozen'); expect(projection?.facts?.candidateSourceCodes).toEqual(['DBS_DUB']); expect(projection?.facts?.preparedSourceCodes).toEqual(['DBS_DUB']); expect(projection?.facts?.pendingSourceCodes).toEqual(['DBS_RTM']); expect(projection?.facts?.failedSourceCodes).toEqual(['DBS_SIN']); expect(projection?.facts?.sourceAttempts).toEqual({ DBS_DUB: { status: 'consumed', attempt_no: 1 }, }); expect(projection?.facts?.inventoryMilestones).toEqual({ candidatesCalculated: 'completed', candidatesFrozen: 'completed', snapshotsReady: 'pending', matched: 'pending', }); expect(projection?.blockingReason?.code).toBe('inventory_candidates_missing'); }); it('returns null when work_item_no missing', () => { expect(normalizeShippingProcessingProjection({ status: 'processing' })).toBeNull(); }); it('returns null for already-camelCase DTO (must only run at API snake boundary)', () => { // 关键断言:normalize 只读 snake;若 composable 对 API 已规范化结果再跑一遍, // 会把 workItemNo 读空并整包洗成 null(右卡永久「获取中/待机」)。 expect(normalizeShippingProcessingProjection({ workItemNo: 'OT-1', workItemType: 'order_task', status: 'waiting_review', stage: 'inventory_matching', userStatus: 'waiting_human', uiState: { status: 'waiting_human', lifecycleStage: 'fulfillment_prep' }, })).toBeNull(); }); it('normalizes work_items arrays', () => { const items = normalizeShippingProcessingWorkItems([ { work_item_no: 'OT-A', work_item_type: 'order_task', status: 'processing', stage: 'document_parsing' }, { status: 'processing' }, ]); expect(items).toHaveLength(1); expect(items[0].workItemNo).toBe('OT-A'); }); });