/** * features/chat/shipping/conversationProcessingProjection.spec.ts * 用途:验收「本次处理中」对象投影——Run/Candidate/Task/Gate 聚合与聚焦。 * 依赖:Vitest、conversationProcessingProjection。 * 逻辑:构造 WorkflowItem;断言去重、聚焦标记与 Gate 行。 */ import { describe, expect, it } from 'vitest'; import { mapLifecycleToNodeStatus, mapShippingProcessingStatusToTaskGroup, mergeConversationProcessingFocusMetadata, projectConversationProcessingFromWorkItems, projectConversationProcessingObjects, projectConversationProcessingTasks, groupConversationProcessingTasks, mapUserTaskObjectsToFileRows, resolveConversationProcessingFromUnified, } from './conversationProcessingProjection'; import type { ShippingProcessingProjection, ShippingWorkflowItem, } from '@/features/chat/types/shipping.api.types'; function item(partial: Partial): ShippingWorkflowItem { return { item_no: 'WI-1', workflow_no: 'WF-1', conversation_id: 'conv-1', root_document_id: 1, current_document_id: 1, current_version_no: 1, lifecycle_status: 'pending_review', current_stage: 'pending_review', lock_version: 1, ...partial, }; } describe('conversationProcessingProjection', () => { it('maps lifecycle to node status without inventing percentages', () => { expect(mapLifecycleToNodeStatus('inventory_matching')).toBe('running'); expect(mapLifecycleToNodeStatus('failed')).toBe('failed'); expect(mapLifecycleToNodeStatus('closed')).toBe('succeeded'); }); it('projects run / candidate / task / process_gate rows and focus', () => { const rows = projectConversationProcessingObjects([ item({ item_no: 'WI-1', current_run_no: 'RUN-1', current_case_no: 'CASE-1', current_task_no: 'SN-1', lifecycle_status: 'pending_review', process_gate: { execution_no: 'EX-1', status: 'blocked', current_blocking_gate: { gate_code: 'inventory_matching', sequence_no: 1, status: 'blocked_manual', evaluation_version: 1, }, task_no: 'SN-1', }, }), item({ item_no: 'WI-2', current_run_no: 'RUN-1', current_task_no: 'SN-1', lifecycle_status: 'inventory_matching', }), ], { focusTaskNo: 'SN-1', focusRunNo: 'RUN-1' }); // 关键断言:同 Run/Task 去重;聚焦 Task;含 Gate。 expect(rows.filter((row) => row.kind === 'run')).toHaveLength(1); expect(rows.filter((row) => row.kind === 'task')).toHaveLength(1); expect(rows.find((row) => row.kind === 'task')?.focused).toBe(true); expect(rows.some((row) => row.kind === 'candidate')).toBe(true); expect(rows.some((row) => row.kind === 'process_gate')).toBe(true); }); it('prefers backend node_status over lifecycle mapping for tasks', () => { const rows = projectConversationProcessingObjects([ item({ current_run_no: 'RUN-2', current_task_no: 'SN-2', lifecycle_status: 'inventory_matching', node_status: 'manual_takeover', progress_nodes: [ { code: 'freight_upload', label: '货代上传', status: 'manual_takeover' }, ], }), ]); // 关键断言:侧栏 Task 使用后端权威 node_status,不覆盖为 running。 expect(rows.find((row) => row.kind === 'task')?.node_status).toBe('manual_takeover'); }); it('groups tasks into needs_action / processing / completed; closed stays history', () => { const tasks = projectConversationProcessingTasks([ item({ item_no: 'WI-A', current_run_no: 'RUN-A', root_document_id: 1, lifecycle_status: 'pending_review', current_document_filename: 'a.pdf', }), item({ item_no: 'WI-B', current_run_no: 'RUN-B', root_document_id: 2, lifecycle_status: 'extracting', current_document_filename: 'b.pdf', }), item({ item_no: 'WI-DONE', current_run_no: 'RUN-DONE', root_document_id: 3, lifecycle_status: 'completed', current_document_filename: 'done.pdf', }), item({ item_no: 'WI-C', current_run_no: 'RUN-C', root_document_id: 4, lifecycle_status: 'closed', current_document_filename: 'c.pdf', }), item({ item_no: 'WI-NO-NAME', current_run_no: 'RUN-NO-NAME', root_document_id: 5, lifecycle_status: 'extracting', current_document_filename: null, }), ]); const projection = groupConversationProcessingTasks(tasks, { focusedTaskNo: 'OT-RUN-A' }); // summary.total_* 含 history(正式 OT 全量);合成路径无截断。 expect(projection.summary).toEqual({ total_tasks: 5, total_files: 5, processing: 2, needs_action: 1, completed: 1, history: 1, returned: { needs_action: 1, processing: 2, completed: 1, history: 1, }, truncated: { active: false, history: false }, has_more_active: false, has_more_history: false, }); expect(projection.needs_action[0]?.user_task_no).toBe('OT-RUN-A'); expect(projection.processing.map((t) => t.user_task_no).sort()).toEqual([ 'OT-RUN-B', 'OT-RUN-NO-NAME', ]); expect(projection.completed?.[0]?.user_task_no).toBe('OT-RUN-DONE'); expect(projection.history[0]?.user_task_no).toBe('OT-RUN-C'); // title 为空,卡片统一展示「订单识别任务」。 const unnamed = projection.processing.find((t) => t.user_task_no === 'OT-RUN-NO-NAME'); expect(unnamed?.title).toBeNull(); }); it('uses multi_document_task when one run has two different PDFs', () => { const tasks = projectConversationProcessingTasks([ item({ item_no: 'WI-1', current_run_no: 'RUN-MULTI', root_document_id: 91, current_document_id: 91, content_hash: 'hash-a', user_task_no: 'OT-MULTI', lifecycle_status: 'extracting', current_document_filename: '4518838152.pdf', }), item({ item_no: 'WI-2', current_run_no: 'RUN-MULTI', root_document_id: 92, current_document_id: 92, content_hash: 'hash-b', user_task_no: 'OT-MULTI', lifecycle_status: 'layout_parsing', current_document_filename: '4518838154.pdf', }), ]); expect(tasks).toHaveLength(1); expect(tasks[0]?.title).toBeNull(); expect(tasks[0]?.display_kind).toBe('multi_document_task'); expect(tasks[0]?.file_count).toBe(2); expect(tasks[0]?.document_names).toEqual(['4518838152.pdf', '4518838154.pdf']); const docs = (tasks[0]?.objects || []).filter((row) => row.kind === 'document'); expect(docs).toHaveLength(2); }); it('keeps multi PDF when same OT spans different run_nos', () => { const tasks = projectConversationProcessingTasks([ item({ item_no: 'WI-1', current_run_no: 'RUN-A', root_document_id: 91, current_document_id: 91, content_hash: 'hash-a', user_task_no: 'OT-SHARED', lifecycle_status: 'pending_review', current_document_filename: 'a.pdf', last_activity_at: '2026-08-04T01:00:00Z', }), item({ item_no: 'WI-2', current_run_no: 'RUN-B', root_document_id: 92, current_document_id: 92, content_hash: 'hash-b', user_task_no: 'OT-SHARED', lifecycle_status: 'pending_review', current_document_filename: 'b.pdf', last_activity_at: '2026-08-04T02:00:00Z', }), ]); // 同 OT 跨 Run 时按 PDF 身份选最新版;未在新 Run 重识别的 PDF 继续有效。 expect(tasks).toHaveLength(1); expect(tasks[0]?.file_count).toBe(2); expect(tasks[0]?.display_kind).toBe('multi_document_task'); expect(tasks[0]?.title).toBeNull(); expect(tasks[0]?.document_names).toEqual(['a.pdf', 'b.pdf']); expect(tasks[0]?.run_no).toBe('RUN-B'); const documents = tasks[0]?.objects.filter((row) => row.kind === 'document') || []; expect(documents.map((row) => row.current_run_no)).toEqual(['RUN-A', 'RUN-B']); }); it('keeps only latest version when same PDF spans different run_nos', () => { const tasks = projectConversationProcessingTasks([ item({ item_no: 'WI-OLD', current_run_no: 'RUN-A', root_document_id: 91, current_document_id: 91, content_hash: 'hash-a', user_task_no: 'OT-SHARED', lifecycle_status: 'failed', current_document_filename: 'old.pdf', last_activity_at: '2026-08-04T01:00:00Z', }), item({ item_no: 'WI-NEW', current_run_no: 'RUN-B', root_document_id: 91, current_document_id: 191, content_hash: 'hash-a', user_task_no: 'OT-SHARED', lifecycle_status: 'pending_review', current_document_filename: 'latest.pdf', last_activity_at: '2026-08-04T02:00:00Z', }), ]); expect(tasks).toHaveLength(1); expect(tasks[0]?.file_count).toBe(1); expect(tasks[0]?.title).toBe('latest.pdf'); const document = tasks[0]?.objects.find((row) => row.kind === 'document'); expect(document?.document_id).toBe(191); expect(document?.current_run_no).toBe('RUN-B'); }); it('keeps single PDF filename title for one-document run', () => { const tasks = projectConversationProcessingTasks([ item({ item_no: 'WI-1', current_run_no: 'RUN-ONE', root_document_id: 7, content_hash: 'solo', user_task_no: 'OT-ONE', lifecycle_status: 'extracting', current_document_filename: 'solo.pdf', }), ]); expect(tasks[0]?.title).toBe('solo.pdf'); expect(tasks[0]?.display_kind).toBe('single_document_task'); expect(tasks[0]?.file_count).toBe(1); }); it('merges same root_document_id across runs into one fallback task', () => { const tasks = projectConversationProcessingTasks([ item({ item_no: 'WI-OLD', current_run_no: 'RUN-OLD', root_document_id: 42, lifecycle_status: 'failed', current_document_filename: 'order.pdf', last_activity_at: '2026-08-03T10:00:00Z', }), item({ item_no: 'WI-NEW', current_run_no: 'RUN-NEW', root_document_id: 42, lifecycle_status: 'extracting', current_document_filename: 'order.pdf', last_activity_at: '2026-08-03T11:00:00Z', }), ]); const projection = groupConversationProcessingTasks(tasks); // [UI-01] 同 root 只保留当前有效版本(处理中),旧失败不再单独出卡。 expect(projection.summary.total_tasks).toBe(1); expect(projection.processing).toHaveLength(1); expect(projection.needs_action).toHaveLength(0); expect(projection.processing[0]?.run_no).toBe('RUN-NEW'); expect(projection.processing[0]?.title).toBe('order.pdf'); }); it('merges different root_document_id with same content_hash into one fallback task', () => { const tasks = projectConversationProcessingTasks([ item({ item_no: 'WI-A', current_run_no: 'RUN-A', root_document_id: 101, content_hash: 'aaa111', lifecycle_status: 'failed', current_document_filename: 'order.pdf', last_activity_at: '2026-08-03T10:00:00Z', }), item({ item_no: 'WI-B', current_run_no: 'RUN-B', root_document_id: 202, content_hash: 'aaa111', lifecycle_status: 'extracting', current_document_filename: 'order.pdf', last_activity_at: '2026-08-03T11:00:00Z', }), ]); const projection = groupConversationProcessingTasks(tasks); // [UI-01] 普通重传换 root,但同内容哈希只保留当前有效版本。 expect(projection.summary.total_tasks).toBe(1); expect(projection.processing).toHaveLength(1); expect(projection.processing[0]?.run_no).toBe('RUN-B'); }); it('merges different root_document_id with same user_task_no into one fallback task', () => { const tasks = projectConversationProcessingTasks([ item({ item_no: 'WI-A', current_run_no: 'RUN-A', root_document_id: 301, user_task_no: 'OT-SHARED', content_hash: 'hash-a', lifecycle_status: 'failed', current_document_filename: 'order.pdf', last_activity_at: '2026-08-03T10:00:00Z', }), item({ item_no: 'WI-B', current_run_no: 'RUN-B', root_document_id: 302, user_task_no: 'OT-SHARED', content_hash: 'hash-b', lifecycle_status: 'extracting', current_document_filename: 'order.pdf', last_activity_at: '2026-08-03T11:00:00Z', }), ]); const projection = groupConversationProcessingTasks(tasks); // user_task_no 优先于 content_hash / root;输出必须原样保留真实 OT。 expect(projection.summary.total_tasks).toBe(1); expect(projection.processing[0]?.run_no).toBe('RUN-B'); expect(projection.processing[0]?.user_task_no).toBe('OT-SHARED'); expect(projection.processing[0]?.user_task_authoritative).toBe(true); }); it('preserves authoritative user_task_no for focus/viewed APIs', () => { const tasks = projectConversationProcessingTasks([ item({ item_no: 'WI-1', current_run_no: 'RUN-XXX', root_document_id: 55, user_task_no: 'OT-REAL-001', content_hash: 'abc', lifecycle_status: 'extracting', current_document_filename: 'order.pdf', last_activity_at: '2026-08-03T11:00:00Z', }), ]); // [UI-01] 禁止输出伪造 OT-${runNo},否则 focus/viewed 会打到不存在的任务号。 expect(tasks).toHaveLength(1); expect(tasks[0]?.user_task_no).toBe('OT-REAL-001'); expect(tasks[0]?.user_task_no).not.toBe('OT-RUN-XXX'); expect(tasks[0]?.user_task_authoritative).toBe(true); }); it('marks fabricated fallback task no as non-authoritative', () => { const tasks = projectConversationProcessingTasks([ item({ item_no: 'WI-1', current_run_no: 'RUN-LOCAL', root_document_id: 66, lifecycle_status: 'extracting', current_document_filename: 'order.pdf', }), ]); expect(tasks[0]?.user_task_no).toBe('OT-RUN-LOCAL'); expect(tasks[0]?.user_task_authoritative).toBe(false); }); it('does not merge different root_document_id with same filename', () => { const tasks = projectConversationProcessingTasks([ item({ item_no: 'WI-A', current_run_no: 'RUN-A', root_document_id: 101, lifecycle_status: 'extracting', current_document_filename: 'order.pdf', last_activity_at: '2026-08-03T10:00:00Z', }), item({ item_no: 'WI-B', current_run_no: 'RUN-B', root_document_id: 202, lifecycle_status: 'extracting', current_document_filename: 'order.pdf', last_activity_at: '2026-08-03T11:00:00Z', }), ]); const projection = groupConversationProcessingTasks(tasks); // [UI-01] 同名不同内容(不同 root)不得合并成一张卡。 expect(projection.summary.total_tasks).toBe(2); expect(projection.processing).toHaveLength(2); }); it('prefers newer failed over older processing for same identity', () => { const tasks = projectConversationProcessingTasks([ item({ item_no: 'WI-OLD', current_run_no: 'RUN-OLD', root_document_id: 77, lifecycle_status: 'extracting', current_document_filename: 'order.pdf', last_activity_at: '2026-08-03T10:00:00Z', }), item({ item_no: 'WI-NEW', current_run_no: 'RUN-NEW', root_document_id: 77, lifecycle_status: 'failed', current_document_filename: 'order.pdf', last_activity_at: '2026-08-03T11:00:00Z', }), ]); const projection = groupConversationProcessingTasks(tasks); // [UI-02] 最新有效版本按活动时间,不按 processing 严重度遮盖新失败。 expect(projection.summary.total_tasks).toBe(1); expect(projection.processing).toHaveLength(0); expect(projection.needs_action).toHaveLength(1); expect(projection.needs_action[0]?.run_no).toBe('RUN-NEW'); expect(projection.needs_action[0]?.status_label).toBe('failed'); }); it('synthesizes conversation groups from unified order_task work_items', () => { const workItems: ShippingProcessingProjection[] = [ { workItemType: 'order_intake', workItemNo: 'SNI-1', sourceType: 'direct_pdf', rootOrderTaskNo: null, shippingNoticeNo: null, conversationId: 'conv-1', submissionNo: 'SOS-1', intakeNo: 'SNI-1', status: 'failed', stage: 'document_parsing', progressVersion: 1, lastActivityAt: '2026-08-05T00:00:00Z', currentGate: null, availableActions: ['retry_parse'], sourceSummary: { emailSubject: null, filename: 'a.pdf', documentCount: 1, parsedDocumentCount: 0, candidateCount: 0, customerName: null, poNumber: null, }, childOrderTasks: [], }, { workItemType: 'order_task', workItemNo: 'OT-WAIT', sourceType: 'eml', rootOrderTaskNo: 'OT-WAIT', shippingNoticeNo: null, runNo: 'RUN-WAIT', caseNo: 'CASE-WAIT', conversationId: 'conv-1', submissionNo: 'SOS-1', intakeNo: 'SNI-2', status: 'waiting_review', stage: 'waiting_review', progressVersion: 2, lastActivityAt: '2026-08-05T01:00:00Z', currentGate: { gateCode: 'order_freeze_readiness', status: 'blocked', nextActionCode: 'open_order_review', blockingReasonCode: 'review_case_draft', attemptNo: 0, }, availableActions: ['open_order_review'], sourceSummary: { emailSubject: null, filename: 'po.pdf', documentCount: 2, parsedDocumentCount: 2, candidateCount: 1, customerName: 'ACME', poNumber: 'PO-1', }, childOrderTasks: [], }, { workItemType: 'order_task', workItemNo: 'OT-RUN', sourceType: 'direct_pdf', rootOrderTaskNo: 'OT-RUN', shippingNoticeNo: null, conversationId: 'conv-1', submissionNo: 'SOS-2', intakeNo: 'SNI-3', status: 'processing', stage: 'document_parsing', progressVersion: 1, lastActivityAt: '2026-08-05T02:00:00Z', currentGate: null, availableActions: ['view_details'], sourceSummary: { emailSubject: null, filename: 'solo.pdf', documentCount: 1, parsedDocumentCount: 1, candidateCount: 0, customerName: null, poNumber: null, }, childOrderTasks: [], }, ]; expect(mapShippingProcessingStatusToTaskGroup(workItems[1]!)).toBe('needs_action'); expect(mapShippingProcessingStatusToTaskGroup(workItems[2]!)).toBe('processing'); const projection = projectConversationProcessingFromWorkItems(workItems); // 关键断言:仅 order_task 入组;intake 不进入 conversation groups。 expect(projection).not.toBeNull(); expect(projection!.needs_action.map((t) => t.user_task_no)).toEqual(['OT-WAIT']); expect(projection!.processing.map((t) => t.user_task_no)).toEqual(['OT-RUN']); expect(projection!.needs_action[0]?.file_count).toBe(2); expect(projection!.needs_action[0]?.display_kind).toBe('multi_document_task'); // [P0] 合成任务必须透传 run_no,禁止写死 null。 expect(projection!.needs_action[0]?.run_no).toBe('RUN-WAIT'); }); it('deduplicates conversation total_files by authoritative PDF id', () => { const base = { sourceType: 'direct_pdf' as const, shippingNoticeNo: null, conversationId: 'conv-1', submissionNo: 'SOS-1', intakeNo: 'SNI-1', status: 'waiting_review' as const, stage: 'waiting_review' as const, progressVersion: 1, lastActivityAt: '2026-08-06T00:00:00Z', currentGate: null, availableActions: ['open_order_review'], childOrderTasks: [], }; const workItems: ShippingProcessingProjection[] = [ { ...base, workItemType: 'order_task', workItemNo: 'OT-A', rootOrderTaskNo: 'OT-A', sourceSummary: { emailSubject: null, filename: 'a.pdf', documentId: 17, documentCount: 1, parsedDocumentCount: 1, candidateCount: 1, customerName: null, poNumber: 'PO-A', }, }, { ...base, workItemType: 'order_task', workItemNo: 'OT-B', rootOrderTaskNo: 'OT-A', sourceSummary: { emailSubject: null, filename: 'a.pdf', documentId: 17, documentCount: 1, parsedDocumentCount: 1, candidateCount: 1, customerName: null, poNumber: 'PO-B', }, }, ]; const projection = projectConversationProcessingFromWorkItems(workItems); // 异常重复绑定同一主 PDF 时,顶部必须仍显示 1 个文件。 expect(projection?.summary.total_files).toBe(1); }); it('prefers work_items task lists over embedded conversation_processing when both exist', () => { const workItems: ShippingProcessingProjection[] = [ { workItemType: 'order_task', workItemNo: 'OT-ROOT', sourceType: 'eml', rootOrderTaskNo: 'OT-ROOT', shippingNoticeNo: null, conversationId: 'conv-1', submissionNo: 'SOS-1', intakeNo: 'SNI-1', status: 'waiting_review', stage: 'waiting_review', progressVersion: 1, lastActivityAt: '2026-08-05T00:00:00Z', currentGate: null, availableActions: ['open_order_review'], sourceSummary: { emailSubject: null, filename: 'root.pdf', documentCount: 1, parsedDocumentCount: 1, candidateCount: 1, customerName: null, poNumber: 'PO-ROOT', }, childOrderTasks: [], }, { workItemType: 'order_task', workItemNo: 'OT-SIBLING', sourceType: 'eml', rootOrderTaskNo: 'OT-ROOT', shippingNoticeNo: null, conversationId: 'conv-1', submissionNo: 'SOS-1', intakeNo: 'SNI-1', status: 'processing', stage: 'candidate_creating', progressVersion: 1, lastActivityAt: '2026-08-05T01:00:00Z', currentGate: null, availableActions: ['view_details'], sourceSummary: { emailSubject: null, filename: 'sibling.pdf', documentCount: 1, parsedDocumentCount: 1, candidateCount: 1, customerName: null, poNumber: 'PO-SIB', }, childOrderTasks: [], }, ]; const embedded = { summary: { total_tasks: 1, total_files: 1, processing: 0, needs_action: 1, completed: 0, history: 0, }, focused_task_no: 'OT-ROOT', recent_task_no: 'OT-SIBLING', needs_action: [{ user_task_no: 'OT-ROOT', group: 'needs_action', can_append: false, file_count: 1, }], processing: [], completed: [], history: [], }; const projection = resolveConversationProcessingFromUnified(workItems, embedded); // 关键断言:兄弟 OT 仅出现在 work_items;embedded 缺 sibling 时不得覆盖任务列表。 expect(projection?.needs_action.map((t) => t.user_task_no)).toEqual(['OT-ROOT']); expect(projection?.processing.map((t) => t.user_task_no)).toEqual(['OT-SIBLING']); expect(projection?.focused_task_no).toBe('OT-ROOT'); expect(projection?.recent_task_no).toBe('OT-SIBLING'); }); it('merges focus metadata from embedded onto work_items-derived projection', () => { const derived = projectConversationProcessingFromWorkItems([{ workItemType: 'order_task', workItemNo: 'OT-1', sourceType: 'direct_pdf', rootOrderTaskNo: 'OT-1', shippingNoticeNo: null, conversationId: 'conv-1', submissionNo: 'SOS-1', intakeNo: 'SNI-1', status: 'processing', stage: 'document_parsing', progressVersion: 1, lastActivityAt: '2026-08-05T00:00:00Z', currentGate: null, availableActions: [], sourceSummary: { emailSubject: null, filename: 'a.pdf', documentCount: 1, parsedDocumentCount: 1, candidateCount: 0, customerName: null, poNumber: null, }, childOrderTasks: [], }]); const merged = mergeConversationProcessingFocusMetadata(derived, { summary: derived!.summary, focused_task_no: 'OT-FOCUS', recent_task_no: 'OT-RECENT', needs_action: [{ user_task_no: 'OT-STALE', group: 'needs_action', can_append: false }], processing: [], history: [], }); expect(merged?.processing[0]?.user_task_no).toBe('OT-1'); expect(merged?.focused_task_no).toBe('OT-FOCUS'); expect(merged?.recent_task_no).toBe('OT-RECENT'); }); });