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

300 lines
11 KiB
TypeScript

/**
* features/chat/shipping/useChatShippingSendHandler.spec.ts
* 用途:验证上传预处理与点击发送完整抽取链路,以及建批提交中的并发拦截。
*/
import { computed, ref } from 'vue';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { createShippingSendHandlers, type ShippingSendHandlerContext } from './useChatShippingSendHandler';
const isShippingNoticeBatchSubmitting = vi.hoisted(() => vi.fn(() => false));
vi.mock('./useChatShippingUploadHandler', () => ({
isShippingNoticeBatchSubmitting,
}));
const createContext = (
overrides: Partial<ShippingSendHandlerContext> = {},
): ShippingSendHandlerContext => ({
isShippingNoticeAgent: computed(() => true),
isThinking: ref(false),
isUploadingAttachments: ref(false),
messages: ref([]),
conversationId: ref('conv-send'),
history: ref([]),
input: ref(''),
attachments: ref([{
id: 'local-notice',
kind: 'local',
file: new File(['notice'], 'notice.txt', { type: 'text/plain' }),
type: 'document',
uploadStatus: 'waiting',
uploadProgress: 0,
}]),
shippingAutoFlowRequested: ref(true),
resolvedShippingTaskId: ref(''),
getActiveShippingBatchNo: () => '',
currentAgentId: computed(() => 1),
selectedKb: ref(''),
attachmentPolicy: computed(() => ({ anyEnabled: true })),
getActiveShippingTaskId: () => '',
lastAssistantMessageContent: () => '',
lastAssistantShippingActions: () => [],
sendMessage: vi.fn(async () => ({ accepted: true, completed: true })),
buildFileSpecsForSend: vi.fn(async () => []),
loadHistory: vi.fn(async () => undefined),
sendShippingMenuMessage: vi.fn(async () => undefined),
startChatTurn: vi.fn(async () => true),
openShippingInventoryPicker: vi.fn(() => true),
executeShippingDecision: vi.fn(async () => true),
executeShippingManualUpload: vi.fn(async () => true),
handleShippingMenuAction: vi.fn(async () => undefined),
handleShippingAutoUploadFile: vi.fn(async () => undefined),
handleShippingAutoUploadNoticeBatch: vi.fn(async () => undefined),
isShippingNoticeBatchSubmitting: () => isShippingNoticeBatchSubmitting(),
startOrderExtractionRun: vi.fn(async () => true),
syncShippingConversationWorkspace: vi.fn(async () => true),
refreshShippingActions: vi.fn(async () => undefined),
shippingSuggestedPrompt: computed(() => ''),
isShippingShortageQueryIntent: () => false,
queryShippingShortageDetails: vi.fn(async () => true),
msg: { warning: vi.fn(), info: vi.fn() },
t: (key: string) => key,
...overrides,
});
describe('useChatShippingSendHandler', () => {
beforeEach(() => {
isShippingNoticeBatchSubmitting.mockReset();
isShippingNoticeBatchSubmitting.mockReturnValue(false);
});
it('consumes send while notice batch is submitting and does not call sendMessage', async () => {
isShippingNoticeBatchSubmitting.mockReturnValue(true);
const ctx = createContext();
const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx);
const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '', '');
expect(consumed).toBe(true);
expect(ctx.sendMessage).not.toHaveBeenCalled();
expect(ctx.handleShippingAutoUploadNoticeBatch).not.toHaveBeenCalled();
expect(ctx.msg.info).toHaveBeenCalledWith('chat.shippingNoticeBatchSubmitting');
});
it('keeps a spreadsheet pending until the user explicitly declares it as inventory', async () => {
const attachment = {
id: 'spreadsheet', kind: 'local' as const,
file: new File(['sheet'], 'unknown.xlsx', { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }),
type: 'document' as const, uploadStatus: 'waiting' as const, uploadProgress: 0,
};
const ctx = createContext({ attachments: ref([attachment]) });
const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx);
const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '', '');
expect(consumed).toBe(true);
expect(ctx.handleShippingAutoUploadFile).not.toHaveBeenCalled();
expect(ctx.handleShippingAutoUploadNoticeBatch).not.toHaveBeenCalled();
expect(ctx.msg.info).toHaveBeenCalledWith('chat.shippingNoticeSpreadsheetIntentRequired');
expect(ctx.attachments.value).toHaveLength(1);
});
it('routes a declared xls spreadsheet to the isolated temporary inventory path', async () => {
const attachment = {
id: 'legacy-spreadsheet', kind: 'local' as const,
file: new File(['sheet'], 'inventory.xls', { type: 'application/vnd.ms-excel' }),
type: 'document' as const, uploadStatus: 'waiting' as const, uploadProgress: 0,
};
const ctx = createContext({ attachments: ref([attachment]), shippingAutoFlowRequested: ref(false) });
const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx);
const consumed = await tryHandleShippingAttachmentAutoFlow('这是库存文件,请分析', '这是库存文件,请分析', '这是库存文件,请分析');
expect(consumed).toBe(true);
expect(ctx.handleShippingAutoUploadFile).toHaveBeenCalledWith(attachment.file);
expect(ctx.handleShippingAutoUploadNoticeBatch).not.toHaveBeenCalled();
});
it('creates a background Run for parsed intakes without calling the Chatflow', async () => {
const attachment = {
id: 'notice-1',
kind: 'shipping_notice' as const,
type: 'document' as const,
uploadStatus: 'uploaded' as const,
parseStatus: 'parsed' as const,
uploadProgress: 100,
originalFilename: 'orders.eml',
fileSize: 100,
intakeNo: 'SNI-1',
};
const ctx = createContext({
attachments: ref([attachment]),
hasCandidateReviewIssues: () => true,
hasSelectedNoticeCandidates: () => true,
getParsedIntakeNos: () => ['SNI-1'],
});
const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx);
const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '', '');
expect(consumed).toBe(true);
expect(ctx.handleShippingAutoUploadNoticeBatch).not.toHaveBeenCalled();
expect(ctx.startOrderExtractionRun).toHaveBeenCalledWith(
'chat.shippingOrderExtractionDefaultPrompt',
['SNI-1'],
[{
id: 'notice-1',
name: 'orders.eml',
size: 100,
extension: 'eml',
type: 'document',
source: 'shipping_notice',
}],
);
expect(ctx.sendMessage).not.toHaveBeenCalled();
expect(ctx.attachments.value).toEqual([]);
});
it('uses the user query for full extraction even when no candidate exists before send', async () => {
const attachment = {
id: 'notice-1',
kind: 'shipping_notice' as const,
type: 'document' as const,
uploadStatus: 'uploaded' as const,
parseStatus: 'parsed' as const,
uploadProgress: 100,
originalFilename: 'orders.eml',
fileSize: 100,
intakeNo: 'SNI-1',
};
const ctx = createContext({
attachments: ref([attachment]),
hasCandidateReviewIssues: () => false,
hasSelectedNoticeCandidates: () => false,
getParsedIntakeNos: () => ['SNI-1'],
});
const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx);
const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '请解析这些附件', '请解析这些附件');
expect(consumed).toBe(true);
expect(ctx.handleShippingAutoUploadNoticeBatch).not.toHaveBeenCalled();
expect(ctx.startOrderExtractionRun).toHaveBeenCalledWith(
'请解析这些附件',
['SNI-1'],
[{
id: 'notice-1',
name: 'orders.eml',
size: 100,
extension: 'eml',
type: 'document',
source: 'shipping_notice',
}],
);
});
it('keeps attachments when Run creation reports not started', async () => {
const ctx = createContext({
attachments: ref([{
id: 'notice-retry',
kind: 'shipping_notice' as const,
type: 'document' as const,
uploadStatus: 'uploaded' as const,
parseStatus: 'parsed' as const,
uploadProgress: 100,
originalFilename: 'orders.eml',
fileSize: 100,
intakeNo: 'SNI-RETRY',
}]),
getParsedIntakeNos: () => ['SNI-RETRY'],
startOrderExtractionRun: vi.fn(async () => false),
});
const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx);
await tryHandleShippingAttachmentAutoFlow(undefined, '', '');
expect(ctx.attachments.value).toHaveLength(1);
});
it('keeps attachments and reports the error when Run creation fails', async () => {
const ctx = createContext({
attachments: ref([{
id: 'notice-failed-send',
kind: 'shipping_notice' as const,
type: 'document' as const,
uploadStatus: 'uploaded' as const,
parseStatus: 'parsed' as const,
uploadProgress: 100,
originalFilename: 'orders.eml',
fileSize: 100,
intakeNo: 'SNI-FAILED-SEND',
}]),
getParsedIntakeNos: () => ['SNI-FAILED-SEND'],
startOrderExtractionRun: vi.fn(async () => { throw new Error('network failed'); }),
});
const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx);
const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '', '');
expect(consumed).toBe(true);
expect(ctx.msg.warning).toHaveBeenCalledWith('network failed');
expect(ctx.attachments.value).toHaveLength(1);
});
it('does not call the Chatflow when Run creation is handled as unavailable', async () => {
const ctx = createContext({
attachments: ref([{
id: 'notice-handled-failure',
kind: 'shipping_notice' as const,
type: 'document' as const,
uploadStatus: 'uploaded' as const,
parseStatus: 'parsed' as const,
uploadProgress: 100,
originalFilename: 'orders.eml',
fileSize: 100,
intakeNo: 'SNI-HANDLED-FAILURE',
}]),
getParsedIntakeNos: () => ['SNI-HANDLED-FAILURE'],
startOrderExtractionRun: vi.fn(async () => false),
});
const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx);
const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '', '');
expect(consumed).toBe(true);
expect(ctx.sendMessage).not.toHaveBeenCalled();
expect(ctx.attachments.value).toHaveLength(1);
});
it('removes only the Intake set accepted by the background Run', async () => {
const submitted = {
id: 'notice-accepted-error',
kind: 'shipping_notice' as const,
type: 'document' as const,
uploadStatus: 'uploaded' as const,
parseStatus: 'parsed' as const,
uploadProgress: 100,
originalFilename: 'orders.eml',
fileSize: 100,
intakeNo: 'SNI-ACCEPTED-ERROR',
};
const retained = {
...submitted,
id: 'notice-retained',
originalFilename: 'other.eml',
intakeNo: 'SNI-RETAINED',
};
const ctx = createContext({
attachments: ref([submitted, retained]),
getParsedIntakeNos: () => ['SNI-ACCEPTED-ERROR'],
startOrderExtractionRun: vi.fn(async () => true),
});
const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx);
const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '', '');
expect(consumed).toBe(true);
expect(ctx.attachments.value).toEqual([retained]);
expect(ctx.sendMessage).not.toHaveBeenCalled();
});
});