/** * useChatShippingRefresh.spec.ts * 用途:验证发货 UI State 刷新只同步通知态,不再把 action_buttons 映射为消息按钮。 * 依赖:vitest、vue refs、useChatShippingRefresh 纯函数导出。 * 逻辑:覆盖任务编号解析优先级,以及刷新后 shipping_actions 始终清空。 */ import { computed, ref } from 'vue'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { createShippingActionsRefresher, resolvePreferredShippingTaskId } from './useChatShippingRefresh'; const { getWorkspaceShippingTaskUiState } = vi.hoisted(() => ({ getWorkspaceShippingTaskUiState: vi.fn(), })); vi.mock('@/features/chat/api/shipping.api', () => ({ getWorkspaceShippingTaskUiState, })); describe('useChatShippingRefresh', () => { beforeEach(() => { getWorkspaceShippingTaskUiState.mockReset(); }); it('prefers active task over message, resolved and query anchors', () => { expect(resolvePreferredShippingTaskId({ activeTaskId: 'sn-active-001', messageTaskId: 'sn-message-001', resolvedTaskId: 'sn-resolved-001', queryTaskId: 'sn-query-001', })).toBe('SN-ACTIVE-001'); }); it('returns empty string when no explicit task anchor is available', () => { expect(resolvePreferredShippingTaskId({ activeTaskId: '', messageTaskId: '', resolvedTaskId: '', queryTaskId: '', })).toBe(''); }); it('syncs ui_state for notifications without attaching chat action buttons', async () => { getWorkspaceShippingTaskUiState.mockResolvedValue({ task_no: 'SN-FOCUS-B', lifecycle_stage: 'fulfillment_prep', user_mode: 'auto_continue', position: 'ready_to_export', match_status: 'fulfillable', review_status: null, pending_human_reason: null, current_step: null, steps: [], summary: { title: '可生成发货文件', message: '库存匹配已满足发货条件,可以生成发货文件。', error_message: null, entry_surface: 'task_workbench', }, action_buttons: [ { id: 'generate_excel', label: '生成发货文件', message: '请生成发货文件', variant: 'primary', }, ], updated_at: '2026-07-01T10:00:00+08:00', }); const messages = ref([ { role: 'assistant' as const, content: '请继续处理当前任务', shipping_actions: [ { id: 'old-action', label: '旧动作', message: '旧动作', variant: 'secondary' as const, }, ], }, ]); const resolvedShippingTaskId = ref('SN-FOCUS-A'); const { refreshShippingActions } = createShippingActionsRefresher({ isShippingNoticeAgent: computed(() => true), messages, conversationId: ref('conv-focus-switch'), isThinking: ref(false), resolvedShippingTaskId, shippingTaskIdFromQuery: computed(() => ''), getActiveShippingTaskId: () => 'SN-FOCUS-B', }); await refreshShippingActions(); expect(resolvedShippingTaskId.value).toBe('SN-FOCUS-B'); expect(getWorkspaceShippingTaskUiState).toHaveBeenCalledWith('SN-FOCUS-B'); expect(messages.value[0].shipping_ui_state?.task_no).toBe('SN-FOCUS-B'); expect(messages.value[0].shipping_actions).toBeUndefined(); }); it('clears legacy shipping_actions even when api returns action_buttons', async () => { getWorkspaceShippingTaskUiState.mockResolvedValue({ task_no: 'SN-SOURCE-001', lifecycle_stage: 'fulfillment_prep', user_mode: 'auto_continue', position: 'ready_to_export', match_status: 'fulfillable', review_status: null, pending_human_reason: null, current_step: null, steps: [], summary: { title: '可生成发货文件', message: '库存匹配已满足发货条件,可以生成发货文件。', error_message: null, }, action_buttons: [ { id: 'download_excel', label: '下载发货文件', message: '请下载发货文件', variant: 'primary', }, ], updated_at: '2026-07-03T10:00:00+08:00', }); const messages = ref([ { role: 'assistant' as const, content: '请使用下方按钮继续。', shipping_actions: [ { id: 'legacy-local-action', label: '旧按钮', message: '旧按钮', variant: 'secondary' as const, }, ], shipping_ui_state: { action_buttons: [ { id: 'legacy-ui-state-action', label: '旧状态按钮', message: '旧状态按钮', variant: 'secondary' as const, }, ], }, }, ]); const { refreshShippingActions } = createShippingActionsRefresher({ isShippingNoticeAgent: computed(() => true), messages, conversationId: ref('conv-source'), isThinking: ref(false), resolvedShippingTaskId: ref('SN-SOURCE-001'), shippingTaskIdFromQuery: computed(() => ''), getActiveShippingTaskId: () => 'SN-SOURCE-001', }); await refreshShippingActions(); expect(messages.value[0].shipping_actions).toBeUndefined(); expect(messages.value[0].shipping_ui_state?.summary?.title).toBe('可生成发货文件'); }); it('does not guess a task id from history metadata when no explicit anchor exists', async () => { const messages = ref([ { role: 'assistant' as const, content: '请继续处理当前任务', }, ]); const { refreshShippingActions } = createShippingActionsRefresher({ isShippingNoticeAgent: computed(() => true), messages, conversationId: ref('conv-no-anchor'), isThinking: ref(false), resolvedShippingTaskId: ref(''), shippingTaskIdFromQuery: computed(() => ''), getActiveShippingTaskId: () => '', }); await refreshShippingActions(); expect(getWorkspaceShippingTaskUiState).not.toHaveBeenCalled(); expect(messages.value[0].shipping_actions).toBeUndefined(); }); it('treats lifecycle upload_complete as terminal without position code', async () => { getWorkspaceShippingTaskUiState.mockResolvedValue({ task_no: 'SN-DONE-001', lifecycle_stage: 'upload_complete', user_mode: 'complete', steps: [], summary: { title: '已完成', message: '上传完成' }, }); const messages = ref([ { role: 'assistant' as const, content: '处理中', shipping_actions: [{ id: 'x', label: 'x', message: 'x', variant: 'primary' as const }], }, ]); const { refreshShippingActions } = createShippingActionsRefresher({ isShippingNoticeAgent: computed(() => true), messages, conversationId: ref('conv-done'), isThinking: ref(false), resolvedShippingTaskId: ref('SN-DONE-001'), shippingTaskIdFromQuery: computed(() => ''), getActiveShippingTaskId: () => 'SN-DONE-001', }); await refreshShippingActions(); expect(messages.value[0].shipping_actions).toBeUndefined(); expect(messages.value[0].shipping_ui_state?.lifecycle_stage).toBe('upload_complete'); }); });