168 lines
7.4 KiB
TypeScript
168 lines
7.4 KiB
TypeScript
/**
|
|
* features/chat/api/shipping.api.request-dedupe.spec.ts
|
|
* 用途:锁定重型订单抽取 Run 读取的同 key 在途请求合并契约。
|
|
* 依赖:Vitest、shipping.api、统一 requestJson。
|
|
* 逻辑:两个消费者同时读取同一 Run 时必须复用一个 HTTP 请求;请求结束后才允许下一次读取。
|
|
*/
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
const requestJson = vi.hoisted(() => vi.fn());
|
|
|
|
vi.mock('@/shared/api/client', () => ({
|
|
BusinessError: class BusinessError extends Error {},
|
|
requestJson,
|
|
}));
|
|
|
|
import {
|
|
getWorkspaceShippingOrderExtractionRun,
|
|
getWorkspaceShippingProcessing,
|
|
getWorkspaceShippingTask,
|
|
} from './shipping.api';
|
|
|
|
describe('getWorkspaceShippingTask', () => {
|
|
beforeEach(() => {
|
|
requestJson.mockReset();
|
|
});
|
|
|
|
it('coalesces concurrent reads of the same task detail into one HTTP request', async () => {
|
|
let resolveRequest!: (value: { task_no: string }) => void;
|
|
requestJson.mockImplementationOnce(() => new Promise((resolve) => {
|
|
resolveRequest = resolve;
|
|
}));
|
|
|
|
const first = getWorkspaceShippingTask('SN-DEDUPE-1');
|
|
const second = getWorkspaceShippingTask('SN-DEDUPE-1');
|
|
|
|
// 关键断言:重型任务详情在途时,同 task_no 的并行消费者(工作台 loadFormalTaskDetail
|
|
// 与对话侧 syncActiveShippingTaskDetail)必须复用一个网络请求,禁止重复拉取。
|
|
expect(requestJson).toHaveBeenCalledTimes(1);
|
|
resolveRequest({ task_no: 'SN-DEDUPE-1' });
|
|
await expect(Promise.all([first, second])).resolves.toEqual([
|
|
{ task_no: 'SN-DEDUPE-1' },
|
|
{ task_no: 'SN-DEDUPE-1' },
|
|
]);
|
|
});
|
|
|
|
it('does not cache a completed task detail across a later read', async () => {
|
|
requestJson.mockResolvedValueOnce({ task_no: 'SN-FRESH-1' });
|
|
requestJson.mockResolvedValueOnce({ task_no: 'SN-FRESH-1' });
|
|
|
|
// 单飞去重只合并「在途」请求;完成后不得返回过期快照,保证 Gate 动作后的新鲜重读。
|
|
await getWorkspaceShippingTask('SN-FRESH-1');
|
|
await getWorkspaceShippingTask('SN-FRESH-1');
|
|
|
|
expect(requestJson).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it('keys dedup by view so workbench and full projections do not merge', async () => {
|
|
requestJson.mockResolvedValue({ task_no: 'SN-VIEW-1' });
|
|
// 关键断言:workbench(裁剪)与 full 是两个不同响应体,缺省 view 与 view=workbench
|
|
// 共享同一个 task_no 时不能合并成一个请求,否则会把裁剪版响应交给 full 消费者。
|
|
const full = getWorkspaceShippingTask('SN-VIEW-1');
|
|
const wb = getWorkspaceShippingTask('SN-VIEW-1', { view: 'workbench' });
|
|
const wbAgain = getWorkspaceShippingTask('SN-VIEW-1', { view: 'workbench' });
|
|
|
|
// 两个视图各一次、workbench 同视图合并:共 2 次底层请求。
|
|
expect(requestJson).toHaveBeenCalledTimes(2);
|
|
expect(requestJson).toHaveBeenNthCalledWith(1, '/api/workspace/shipping-tasks/SN-VIEW-1');
|
|
expect(requestJson).toHaveBeenNthCalledWith(
|
|
2,
|
|
'/api/workspace/shipping-tasks/SN-VIEW-1?view=workbench',
|
|
);
|
|
await Promise.allSettled([full, wb, wbAgain]);
|
|
});
|
|
});
|
|
|
|
describe('getWorkspaceShippingOrderExtractionRun', () => {
|
|
beforeEach(() => {
|
|
requestJson.mockReset();
|
|
// 模块级短缓存会跨用例保留;本文件的会话 ID 均唯一,避免上一用例污染下一用例。
|
|
});
|
|
|
|
it('coalesces concurrent reads of the same Run into one HTTP request', async () => {
|
|
let resolveRequest!: (value: { run_no: string }) => void;
|
|
requestJson.mockImplementationOnce(() => new Promise((resolve) => {
|
|
resolveRequest = resolve;
|
|
}));
|
|
|
|
const first = getWorkspaceShippingOrderExtractionRun('SNER-SAME');
|
|
const second = getWorkspaceShippingOrderExtractionRun('SNER-SAME');
|
|
|
|
// 关键断言:慢 Run 投影在途时,同一文件的第二个消费者不得再创建独立网络请求。
|
|
expect(requestJson).toHaveBeenCalledTimes(1);
|
|
resolveRequest({ run_no: 'SNER-SAME' });
|
|
await expect(Promise.all([first, second])).resolves.toEqual([
|
|
{ run_no: 'SNER-SAME' },
|
|
{ run_no: 'SNER-SAME' },
|
|
]);
|
|
});
|
|
|
|
it('lets one caller stop waiting without aborting a shared Run request', async () => {
|
|
let resolveRequest!: (value: { run_no: string }) => void;
|
|
requestJson.mockImplementationOnce(() => new Promise((resolve) => {
|
|
resolveRequest = resolve;
|
|
}));
|
|
const controller = new AbortController();
|
|
|
|
const cancelledCaller = getWorkspaceShippingOrderExtractionRun('SNER-SHARED', controller.signal);
|
|
const activeCaller = getWorkspaceShippingOrderExtractionRun('SNER-SHARED');
|
|
controller.abort();
|
|
|
|
// 关键断言:路由离开只结束当前消费者等待,不能中止其他组件复用的重型读取。
|
|
await expect(cancelledCaller).rejects.toMatchObject({ name: 'AbortError' });
|
|
expect(requestJson).toHaveBeenCalledTimes(1);
|
|
resolveRequest({ run_no: 'SNER-SHARED' });
|
|
await expect(activeCaller).resolves.toEqual({ run_no: 'SNER-SHARED' });
|
|
});
|
|
|
|
it('reuses a successful conversation summary during the short refresh burst', async () => {
|
|
requestJson.mockResolvedValue({
|
|
conversation_id: 'conversation-summary-cache',
|
|
work_items: [],
|
|
conversation_processing: null,
|
|
});
|
|
|
|
await getWorkspaceShippingProcessing('conversation-summary-cache');
|
|
await getWorkspaceShippingProcessing('conversation-summary-cache');
|
|
|
|
// 关键断言:首屏 watcher 连续触发时必须复用刚完成的摘要,避免重复慢查询。
|
|
expect(requestJson).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('binds every summary leaf to its status-group task for batch rendering', async () => {
|
|
requestJson.mockResolvedValue({
|
|
conversation_id: 'conversation-leaf-binding-20260822',
|
|
conversation_processing: {
|
|
summary: { total_tasks: 1, total_files: 1, 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,
|
|
aggregate_children: [{
|
|
work_item_type: 'order_task', work_item_no: 'DOCUMENT:1', source_type: 'eml',
|
|
conversation_id: 'conversation-leaf-binding-20260822', intake_no: 'SNI-1', submission_no: 'SUB-1',
|
|
// 同一邮件内另一 Candidate 的原始 OT 不能用作父卡关联键。
|
|
root_order_task_no: 'OT-OTHER-CANDIDATE',
|
|
status: 'waiting_human', stage: 'candidate_creating', progress_version: 1,
|
|
source_summary: { document_id: 1, filename: 'order.pdf' },
|
|
}],
|
|
}],
|
|
processing: [], completed: [], cancelled: [], archived: [],
|
|
},
|
|
},
|
|
});
|
|
|
|
const response = await getWorkspaceShippingProcessing('conversation-leaf-binding-20260822');
|
|
|
|
// 关键断言:批次卡按父任务查实时叶子时,必须能找到本摘要组的文件。
|
|
expect(response.conversation_processing?.status_groups?.waiting_human[0]?.aggregateChildren?.[0]).toMatchObject({
|
|
workItemNo: 'DOCUMENT:1',
|
|
});
|
|
expect(response.conversation_processing?.status_groups?.waiting_human[0]?.aggregateChildren).toHaveLength(1);
|
|
expect(response.work_items).toHaveLength(1);
|
|
expect(response.work_items[0]?.rootOrderTaskNo).toBe('INTAKE:SNI-1');
|
|
expect(response.work_items[0]?.sourceSummary.documentId).toBe(1);
|
|
});
|
|
});
|