feat(shipping-notice): 落地 XFA 拆分、版面签名、统一进度与处理域履约

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-08-06 10:18:37 +08:00
1 parent 3a9fbac2fc
commit d2b3d74def
571 files changed
+78064 -24681

No files matched your search

@@ -7,9 +7,19 @@
import { describe, expect, it } from 'vitest';
import {
mapLifecycleToNodeStatus,
mapShippingProcessingStatusToTaskGroup,
mergeConversationProcessingFocusMetadata,
projectConversationProcessingFromWorkItems,
projectConversationProcessingObjects,
projectConversationProcessingTasks,
groupConversationProcessingTasks,
mapUserTaskObjectsToFileRows,
resolveConversationProcessingFromUnified,
} from './conversationProcessingProjection';
import type { ShippingWorkflowItem } from '@/features/chat/types/shipping.api.types';
import type {
ShippingProcessingProjection,
ShippingWorkflowItem,
} from '@/features/chat/types/shipping.api.types';
function item(partial: Partial<ShippingWorkflowItem>): ShippingWorkflowItem {
return {
@@ -84,4 +94,581 @@ describe('conversationProcessingProjection', () => {
// 关键断言:侧栏 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' });
// 右侧总数不含 history;completed 独立成组。
expect(projection.summary).toEqual({
total_tasks: 4,
total_files: 4,
processing: 2,
needs_action: 1,
completed: 1,
history: 1,
});
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');
// [UI-02] 缺文件名时 title 为空,交给卡片回退「未命名 PDF」。
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,
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');
});
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');
});
});