feat(shipping-notice): 完善发货通知自动推进与多库存源复核闭环

This commit is contained in:
hugo committed 2026-08-09 11:43:53 +08:00
1 parent cfa19a0b7b
commit 8e0bbf7a81
180 files changed
+31210 -1864

No files matched your search

@@ -16,11 +16,15 @@ import {
mapUserTaskObjectsToFileRows,
resolveConversationProcessingFromUnified,
normalizeActionCodes,
normalizeLifecycleStatus,
normalizeConversationProcessingProjection,
createIntakeOtStabilizer,
buildTaskSemanticKey,
buildProcessingSemanticKey,
stabilizeTaskObjects,
} from './conversationProcessingProjection';
import type {
ConversationProcessingProjection,
ShippingProcessingProjection,
ShippingWorkflowItem,
} from '@/features/chat/types/shipping.api.types';
@@ -896,4 +900,283 @@ describe('conversationProcessingProjection — normalize & semantic & stabilize'
expect(stabilized[0].status_label).toBe('waiting_review');
expect(stabilized[0].available_actions).toEqual(['open_order_review', 'view_details']);
});
it('normalizeLifecycleStatus: waiting_human / pending_review / review_required 一律归并为 waiting_review', () => {
expect(normalizeLifecycleStatus('waiting_human')).toBe('waiting_review');
expect(normalizeLifecycleStatus('WAITING_HUMAN ')).toBe('waiting_review');
expect(normalizeLifecycleStatus('pending_review')).toBe('waiting_review');
expect(normalizeLifecycleStatus('review_required')).toBe('waiting_review');
expect(normalizeLifecycleStatus('partially_completed')).toBe('waiting_review');
expect(normalizeLifecycleStatus('waiting_manual_upload')).toBe('manual_upload_pending');
expect(normalizeLifecycleStatus('retryable')).toBe('failed');
expect(normalizeLifecycleStatus('completed')).toBe('completed');
expect(normalizeLifecycleStatus('')).toBe('');
expect(normalizeLifecycleStatus(null)).toBe('');
});
it('buildTaskSemanticKey: waiting_human 与 waiting_review 语义相同 → 同签名(不闪烁)', () => {
const shared = {
user_task_no: 'OT-SAME',
group: 'needs_action' as const,
run_no: 'RUN-1',
display_kind: 'single_document_task' as const,
file_count: 1,
title: 'a.pdf',
primary_action: 'open_order_review',
available_actions: ['open_order_review', 'view_details'],
can_append: false,
user_task_authoritative: true,
objects: [],
};
const t1 = { ...shared, status_label: 'waiting_human' };
const t2 = { ...shared, status_label: 'waiting_review' };
expect(buildTaskSemanticKey(t1)).toBe(buildTaskSemanticKey(t2));
// updated_at 不影响,这里再确认 userStatus 为 waiting_human 也一致。
const t3 = { ...shared, status_label: 'queued', userStatus: 'waiting_human' } as typeof t1 & { userStatus: string };
const t4 = { ...shared, status_label: 'queued', userStatus: 'waiting_review' } as typeof t1 & { userStatus: string };
expect(buildTaskSemanticKey(t3)).toBe(buildTaskSemanticKey(t4));
});
it('normalizeConversationProcessingProjection: embedded waiting_human → waiting_review / needs_action', () => {
const embedded: ConversationProcessingProjection = {
summary: { total_tasks: 1, total_files: 1, processing: 0, needs_action: 0, completed: 0, history: 0 },
focused_task_no: null,
recent_task_no: null,
needs_action: [],
processing: [
{
user_task_no: 'OT-LEGACY',
user_task_authoritative: false,
title: 'a.pdf',
display_kind: 'single_document_task',
document_names: ['a.pdf'],
// 旧投影错误地把 waiting_human 放进了 processing 组
group: 'processing',
status_label: 'waiting_human',
file_count: 1,
run_no: 'RUN-LEG',
can_append: false,
available_actions: ['enter_manual_review', 'view_details'],
primary_action: 'enter_manual_review',
},
],
completed: [],
history: [],
};
const normalized = normalizeConversationProcessingProjection(embedded);
const task = normalized.needs_action[0];
expect(task).toBeDefined();
expect(task!.status_label).toBe('waiting_review');
// waiting_human → waiting_review → 必须迁入 needs_action
expect(task!.group).toBe('needs_action');
expect(normalized.processing.length).toBe(0);
});
it('resolveConversationProcessingFromUnified: work_items 为空时,embedded waiting_human 任务迁入 needs_action 且统一文案', () => {
const embedded: ConversationProcessingProjection = {
summary: { total_tasks: 1, total_files: 1, processing: 1, needs_action: 0, completed: 0, history: 0 },
focused_task_no: null,
recent_task_no: null,
needs_action: [],
processing: [
{
user_task_no: 'OT-LEG2',
user_task_authoritative: false,
title: 'b.pdf',
display_kind: 'single_document_task',
document_names: ['b.pdf'],
group: 'processing',
status_label: 'waiting_human',
file_count: 1,
run_no: 'RUN-LEG2',
can_append: false,
available_actions: [],
primary_action: 'view_details',
},
],
completed: [],
history: [],
};
const result = resolveConversationProcessingFromUnified([], embedded);
expect(result).not.toBeNull();
// waiting_human 任务从 processing 组迁入 needs_action
expect(result!.processing.length).toBe(0);
expect(result!.needs_action.length).toBe(1);
expect(result!.needs_action[0]!.status_label).toBe('waiting_review');
expect(result!.needs_action[0]!.title).toBe('b.pdf');
});
it('resolveConversationProcessingFromUnified: work_items 仅含 order_intake 时,仍走 embedded normalize 路径(过渡窗口不跳状态)', () => {
const embedded: ConversationProcessingProjection = {
summary: { total_tasks: 1, total_files: 1, processing: 1, needs_action: 0, completed: 0, history: 0 },
focused_task_no: 'OT-LEG3',
recent_task_no: 'OT-LEG3',
needs_action: [],
processing: [
{
user_task_no: 'OT-LEG3',
user_task_authoritative: false,
title: 'c.pdf',
display_kind: 'single_document_task',
document_names: ['c.pdf'],
group: 'processing',
status_label: 'waiting_human',
file_count: 1,
run_no: 'RUN-LEG3',
can_append: false,
available_actions: ['enter_manual_review'],
primary_action: 'enter_manual_review',
},
],
completed: [],
history: [],
};
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: 'processing',
stage: 'building_order',
progressVersion: 1,
lastActivityAt: '2026-08-06T00:00:00Z',
currentGate: null,
availableActions: [],
sourceSummary: {
emailSubject: null,
filename: 'c.pdf',
documentCount: 1,
parsedDocumentCount: 0,
candidateCount: 0,
customerName: null,
poNumber: null,
},
childOrderTasks: [],
},
];
const result = resolveConversationProcessingFromUnified(workItems, embedded);
expect(result).not.toBeNull();
// 过渡阶段:order_intake 不触发统一投影派生;embedded waiting_human 必须 normalize 成 waiting_review/needs_action
expect(result!.focused_task_no).toBe('OT-LEG3');
expect(result!.processing.length).toBe(0);
expect(result!.needs_action.length).toBe(1);
expect(result!.needs_action[0]!.status_label).toBe('waiting_review');
expect(result!.needs_action[0]!.title).toBe('c.pdf');
expect(result!.needs_action[0]!.group).toBe('needs_action');
});
it('resolveConversationProcessingFromUnified + IntakeOtStabilizer: 看到 order_task 后 2s 内暂缺 OT 保持 previousProjection(避免轮询抖动)', () => {
const stabilizer = createIntakeOtStabilizer();
const embedded: ConversationProcessingProjection = {
summary: { total_tasks: 1, total_files: 1, processing: 0, needs_action: 1, completed: 0, history: 0 },
focused_task_no: null,
recent_task_no: null,
needs_action: [
{
user_task_no: 'OT-LG',
user_task_authoritative: false,
title: 'fallback.pdf',
display_kind: 'single_document_task',
document_names: ['fallback.pdf'],
group: 'needs_action',
status_label: 'waiting_human',
file_count: 1,
run_no: 'RUN-LG',
can_append: false,
available_actions: [],
primary_action: 'enter_manual_review',
},
],
processing: [],
completed: [],
history: [],
};
const intakeOnly: ShippingProcessingProjection[] = [
{
workItemType: 'order_intake',
workItemNo: 'SNI-1',
sourceType: 'direct_pdf',
rootOrderTaskNo: null,
shippingNoticeNo: null,
conversationId: 'conv-1',
submissionNo: 'SOS-1',
intakeNo: 'SNI-1',
status: 'processing',
stage: 'building_order',
progressVersion: 1,
lastActivityAt: '2026-08-06T00:00:00Z',
currentGate: null,
availableActions: [],
sourceSummary: { filename: 'a.pdf', documentCount: 1 },
childOrderTasks: [],
},
];
const withOrderTask: ShippingProcessingProjection[] = [
{
workItemType: 'order_task',
workItemNo: 'OT-NEW',
sourceType: 'direct_pdf',
rootOrderTaskNo: 'OT-NEW',
shippingNoticeNo: null,
conversationId: 'conv-1',
runNo: 'RUN-NEW',
caseNo: 'CASE-NEW',
submissionNo: 'SOS-1',
intakeNo: 'SNI-1',
userStatus: 'waiting_review',
status: 'waiting_review',
stage: 'waiting_human_review',
progressVersion: 1,
lastActivityAt: '2026-08-06T00:00:01Z',
primaryAction: { code: 'open_order_review', i18nKey: 'chat.shippingProcessing.action.open_order_review' },
currentGate: null,
availableActions: ['open_order_review', 'cancel_task'],
sourceSummary: { filename: 'a.pdf', documentCount: 1 },
},
];
// Step 1: 初始只有 intake,还没见过 OT → 走 embedded normalize
const step1 = resolveConversationProcessingFromUnified(
intakeOnly,
embedded,
null,
{ conversationId: 'conv-1', ...stabilizer },
);
expect(step1!.needs_action[0]!.title).toBe('fallback.pdf');
expect(step1!.needs_action[0]!.status_label).toBe('waiting_review');
// Step 2: 第一轮看到 OT → derived 生成,同时 markSeen
const step2 = resolveConversationProcessingFromUnified(
withOrderTask,
embedded,
step1,
{ conversationId: 'conv-1', ...stabilizer },
);
expect(step2!.needs_action[0]!.user_task_no).toBe('OT-NEW');
expect(step2!.needs_action[0]!.status_label).toBe('waiting_review');
// Step 3: 下一次轮询只有 intake(本次响应暂缺 OT)→ 保持 previousProjection,不抖回 embedded
const step3 = resolveConversationProcessingFromUnified(
intakeOnly,
embedded,
step2,
{ conversationId: 'conv-1', ...stabilizer },
);
expect(step3).toBe(step2);
// Step 4: 用户点刷新 reset 后,允许走 embedded(业务认为需要重新建立稳定态)
stabilizer.reset('conv-1');
const step4 = resolveConversationProcessingFromUnified(
intakeOnly,
embedded,
step2,
{ conversationId: 'conv-1', ...stabilizer },
);
expect(step4!.needs_action[0]!.title).toBe('fallback.pdf');
expect(step4!.needs_action[0]!.status_label).toBe('waiting_review');
});
});