/** * features/chat/shipping/useChatShippingMenuHandler.spec.ts * 用途:验证发货聊天动作菜单在工作台显式 taskId 场景下会命中指定任务,而不是误用当前激活任务。 * 依赖:vitest、vue refs、shipping.api、useChatShippingMenuHandler。 * 逻辑:覆盖 retry_rpa、generate_excel 等动作,锁死 Chat Turn 分发时的任务归属。 */ import { computed, ref } from 'vue'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { createShippingMenuHandlers, type ShippingMenuHandlerContext } from './useChatShippingMenuHandler'; const { downloadWorkspaceShippingExcelFile } = vi.hoisted(() => ({ downloadWorkspaceShippingExcelFile: vi.fn(), })); vi.mock('./shippingDownload', () => ({ downloadWorkspaceShippingExcelFile, })); const createContext = ( overrides: Partial = {}, ): ShippingMenuHandlerContext => ({ isThinking: ref(false), currentAgentId: computed(() => 42), getActiveShippingTaskId: () => 'SN-ACTIVE-001', messages: ref([]), sendShippingMenuMessage: vi.fn(async () => undefined), openShippingInventoryPicker: vi.fn((_taskId?: string) => true), openShippingNoticePicker: vi.fn((_taskId?: string) => true), executeShippingDecision: vi.fn(async () => true), executeShippingManualUpload: vi.fn(async () => true), refreshShippingActions: vi.fn(async () => undefined), startChatTurn: vi.fn(async () => true), msg: { warning: vi.fn(), info: vi.fn(), }, t: (key: string) => key, ...overrides, }); describe('useChatShippingMenuHandler', () => { beforeEach(() => { downloadWorkspaceShippingExcelFile.mockReset(); }); it('does not execute retry_rpa from a Chat menu', async () => { const ctx = createContext(); const { handleShippingMenuAction } = createShippingMenuHandlers(ctx); await handleShippingMenuAction( { id: 'retry_rpa', label: '重新获取库存', message: '重新获取库存' }, undefined, { taskId: 'sn-target-009' }, ); expect(ctx.executeShippingDecision).not.toHaveBeenCalled(); expect(ctx.refreshShippingActions).toHaveBeenCalledOnce(); }); it('does not dispatch legacy generate_excel Chat Turn', async () => { const ctx = createContext(); const { handleShippingMenuAction } = createShippingMenuHandlers(ctx); await handleShippingMenuAction( { id: 'generate_excel', label: '生成发货文件', message: '请生成发货文件' }, undefined, { taskId: 'sn-target-777' }, ); expect(ctx.startChatTurn).not.toHaveBeenCalled(); expect(ctx.refreshShippingActions).toHaveBeenCalled(); }); it('does not dispatch legacy match_inventory Chat Turn', async () => { const ctx = createContext(); const { handleShippingMenuAction } = createShippingMenuHandlers(ctx); await handleShippingMenuAction( { id: 'match_inventory', label: '匹配库存', message: '请匹配库存' }, undefined, { taskId: 'sn-target-778' }, ); expect(ctx.startChatTurn).not.toHaveBeenCalled(); expect(ctx.refreshShippingActions).toHaveBeenCalled(); }); it('does not dispatch a legacy freeze action through Chat Turn', async () => { const ctx = createContext(); const { handleShippingMenuAction } = createShippingMenuHandlers(ctx); await handleShippingMenuAction( { id: 'freeze_order', label: '冻结订单', message: '冻结已复核订单' }, undefined, { taskId: 'sn-freeze-901' }, ); expect(ctx.startChatTurn).not.toHaveBeenCalled(); }); it('passes explicit taskId into inventory picker actions', async () => { const ctx = createContext(); const { handleShippingMenuAction } = createShippingMenuHandlers(ctx); await handleShippingMenuAction( { id: 'upload_inventory_file', label: '上传库存文件', message: '请上传库存文件' }, undefined, { taskId: 'sn-picker-123' }, ); expect(ctx.openShippingInventoryPicker).toHaveBeenCalledWith('SN-PICKER-123'); }); it('passes explicit taskId into notice picker actions', async () => { const ctx = createContext(); const { handleShippingMenuAction } = createShippingMenuHandlers(ctx); await handleShippingMenuAction( { id: 'replace_notice_file', label: '重新上传通知', message: '请重新上传通知' }, undefined, { taskId: 'sn-notice-123' }, ); expect(ctx.openShippingNoticePicker).toHaveBeenCalledWith('SN-NOTICE-123'); }); });