Files
syops/frontend/src/features/chat/shipping/conversationProcessingProjection.spec.ts
T

147 lines
7.2 KiB
TypeScript

/**
* features/chat/shipping/conversationProcessingProjection.spec.ts
* 用途:验证会话进度只消费后端一次裁决的 status_groups 与 ui_state 合同。
* 依赖:Vitest、conversationProcessingProjection。
* 逻辑:覆盖 EML 叶子、响应拒绝、DTO 规范化和轮询对象稳定性。
*/
import { describe, expect, it } from 'vitest';
import {
buildProcessingSemanticKey,
buildTaskSemanticKey,
businessFileKeys,
flattenBusinessFileProjections,
normalizeConversationProcessingProjection,
normalizeLifecycleStatus,
resolveConversationProcessingFromUnified,
stabilizeTaskObjects,
} from './conversationProcessingProjection';
import type { ShippingProcessingProjection } from '@/features/chat/types/shipping.api.types';
describe('conversationProcessingProjection', () => {
it('normalizeLifecycleStatus keeps F2 waiting_human (no fold to waiting_review)', () => {
expect(normalizeLifecycleStatus('waiting_human')).toBe('waiting_human');
expect(normalizeLifecycleStatus('waiting_review')).toBe('waiting_review');
expect(normalizeLifecycleStatus('pending_review')).toBe('pending_review');
expect(normalizeLifecycleStatus('retryable')).toBe('failed');
});
it('EML 容器不计为业务文件,只保留附件叶子', () => {
const items = [{
sourceType: 'eml', sourceSummary: { filename: 'mail.eml' },
aggregateChildren: [
{ sourceType: 'eml', sourceSummary: { filename: 'order.pdf', documentId: 101 } },
{ sourceType: 'eml', sourceSummary: { filename: 'packing.xlsx', documentId: 102 } },
],
}] as ShippingProcessingProjection[];
expect(flattenBusinessFileProjections(items).map((item) => item.sourceSummary.filename))
.toEqual(['order.pdf', 'packing.xlsx']);
expect(businessFileKeys(items)).toEqual(['document:101', 'document:102']);
});
it('没有正式 status_groups 时拒绝响应,绝不从 work_items 重建', () => {
expect(resolveConversationProcessingFromUnified(
[{ workItemType: 'order_task', workItemNo: 'OT-1' } as ShippingProcessingProjection],
{ summary: { total_tasks: 1 } } as never,
)).toBeNull();
});
it('只使用后端 status_groups,不读取顶层旧四组镜像', () => {
const projection = resolveConversationProcessingFromUnified([], {
summary: { total_tasks: 1, total_files: 1, processing: 0, needs_action: 1, completed: 0, history: 0 },
status_groups: {
failed: [],
waiting_human: [{ user_task_no: 'OT-LIVE', group: 'needs_action', status_label: 'waiting_human', file_count: 1, can_append: false }],
processing: [], completed: [], cancelled: [], archived: [],
},
needs_action: [{ user_task_no: 'OT-STALE', group: 'needs_action', status_label: 'failed', file_count: 1, can_append: false }],
processing: [], completed: [], history: [], focused_task_no: null, recent_task_no: null,
} as never);
expect(projection?.status_groups?.waiting_human.map((item) => item.user_task_no))
.toEqual(['OT-LIVE']);
expect(projection).not.toHaveProperty('needs_action');
});
it('规范化后端 ui_state 的动作与生命周期跳转字段', () => {
const projection = normalizeConversationProcessingProjection({
summary: { total_tasks: 1, total_files: 1, processing: 1, needs_action: 0, completed: 0, history: 0 },
status_groups: {
failed: [], waiting_human: [],
processing: [{
user_task_no: 'OT-1', group: 'processing', status_label: 'running', file_count: 1, can_append: false,
ui_state: {
status: 'running', stage: 'intake_parse', blocking_reason: null,
primary_action: { code: 'reparse_document', i18n_key: 'shipping.action.reparse_document', execution_mode: 'execute', target_surface: null, target_lifecycle_stage: 'intake_parse', parameters: {} },
secondary_actions: [], lifecycle_entries: [{ code: 'intake_parse', state: 'current', target_surface: 'order_lifecycle_workbench' }],
},
}],
completed: [], cancelled: [], archived: [],
},
} as never);
const task = projection.status_groups!.processing[0]!;
expect(task.uiState?.primary_action).toMatchObject({
code: 'reparse_document', executionMode: 'execute', targetLifecycleStage: 'intake_parse',
});
expect(task.uiState?.lifecycleEntries).toEqual([{
code: 'intake_parse', state: 'current', targetSurface: 'order_lifecycle_workbench',
}]);
});
it('保留摘要任务编号与文件状态计数,供批次文件行关联实时叶子', () => {
const projection = normalizeConversationProcessingProjection({
summary: {
total_tasks: 1, total_files: 1,
failed_files: 0, waiting_human_files: 1, processing_files: 0, completed_files: 0,
processing: 0, needs_action: 1, completed: 0, history: 0,
},
status_groups: {
failed: [],
waiting_human: [{
user_task_no: 'INTAKE:SNI-1', group: 'needs_action', status_label: 'waiting_human',
file_count: 1, can_append: false,
}],
processing: [], completed: [], cancelled: [], archived: [],
},
} as never);
// 关键断言:后端摘要的身份与文件状态计数不得在前端规范化时丢失。
expect(projection.status_groups?.waiting_human[0]?.user_task_no).toBe('INTAKE:SNI-1');
expect(projection.summary.waiting_human_files).toBe(1);
});
it('后端撤销 ui_state 动作时,语义键必须变化', () => {
const before = {
user_task_no: 'INTAKE:1', group: 'needs_action' as const, status_label: 'waiting_human', file_count: 1, can_append: false,
uiState: { status: 'waiting_human', stage: 'order_review', blocking_reason: null, primary_action: { code: 'reparse_document', parameters: {} }, secondary_actions: [] },
};
const after = { ...before, uiState: { ...before.uiState, primary_action: null } };
expect(buildTaskSemanticKey(before)).not.toBe(buildTaskSemanticKey(after));
});
it('辅助时间字段变化不重建语义相同的轮询任务对象', () => {
const oldTask = {
user_task_no: 'OT-1', group: 'processing' as const, status_label: 'running', file_count: 1, can_append: false,
updated_at: '2026-08-22T00:00:00Z', uiState: { status: 'running', stage: 'intake_parse', blocking_reason: null, primary_action: null, secondary_actions: [] },
};
const nextTask = { ...oldTask, updated_at: '2026-08-22T00:00:02Z' };
expect(stabilizeTaskObjects([oldTask], [nextTask])[0]).toBe(oldTask);
});
it('会话语义键不包含辅助更新时间', () => {
const base = {
summary: { total_tasks: 1, total_files: 1, processing: 1, needs_action: 0, completed: 0, history: 0 },
status_groups: { failed: [], waiting_human: [], processing: [{ user_task_no: 'OT-1', group: 'processing', status_label: 'running', file_count: 1, can_append: false }], completed: [], cancelled: [], archived: [] },
} as never;
const first = normalizeConversationProcessingProjection(base);
const second = normalizeConversationProcessingProjection(base);
first.status_groups!.processing[0]!.updated_at = '2026-08-22T00:00:00Z';
second.status_groups!.processing[0]!.updated_at = '2026-08-22T00:00:02Z';
expect(buildProcessingSemanticKey(first)).toBe(buildProcessingSemanticKey(second));
});
});