import { describe, expect, it } from 'vitest'; import { collectCopilotUi, collapseCopilotUiActions, summarizeCapabilityResult } from '../src/mcp/orderEntryAgentHost'; describe('collapseCopilotUiActions', () => { it('keeps a single navigate as the last one', () => { const result = collapseCopilotUiActions([ { type: 'navigate', path: '/orders', label: '订单管理', web: { path: '/orders' } }, { type: 'navigate', path: '/dashboard', label: '数据看板', web: { path: '/dashboard' } }, { type: 'navigate', path: '/finance', label: '财务概览', web: { path: '/finance' } }, ]); expect(result).toEqual([ { type: 'navigate', path: '/finance', label: '财务概览', web: { path: '/finance' } }, ]); }); it('puts navigate before set_fields even if the model called set first', () => { const result = collapseCopilotUiActions([ { type: 'set_fields', patches: [{ name: 'date_from', value: '2026-09-02' }] }, { type: 'navigate', path: '/purchase', web: { path: '/purchase' } }, ]); expect(result[0]).toEqual({ type: 'navigate', path: '/purchase', web: { path: '/purchase' } }); expect(result[1]).toEqual({ type: 'set_fields', patches: [{ name: 'date_from', value: '2026-09-02' }] }); }); it('keeps request_pick alongside a single navigate', () => { const result = collapseCopilotUiActions([ { type: 'request_pick', payload: { pick_id: 'p1', slot: 'customer_code', prompt: '请选择客户', options: [{ id: 'C1', label: '食堂' }], }, }, { type: 'navigate', path: '/orders', web: { path: '/orders' } }, ]); expect(result.some((row) => row.type === 'request_pick')).toBe(true); expect(result[0].type).toBe('navigate'); }); it('keeps show_query and preview_change alongside a single navigate', () => { const result = collapseCopilotUiActions([ { type: 'show_query', payload: { capability: 'list_orders', title: '订单', summary: '订单 1 个', columns: ['order_code'], rows: [{ order_code: 'ORD1' }], }, }, { type: 'navigate', path: '/orders', web: { path: '/orders' } }, { type: 'preview_change', payload: { action_type: 'create', capability: 'create_after_sale', target_type: 'after_sale', summary: '新建售后', diff: [], }, }, ]); expect(result[0]?.type).toBe('navigate'); expect(result.some((row) => row.type === 'show_query')).toBe(true); expect(result.some((row) => row.type === 'preview_change')).toBe(true); }); }); describe('collectCopilotUi', () => { it('flattens ui arrays from list tools', () => { const actions = collectCopilotUi({ ui: [ { type: 'navigate', path: '/orders', web: { path: '/orders' } }, { type: 'set_fields', patches: [{ name: 'search', value: '白菜' }] }, ], }); expect(actions).toHaveLength(2); expect(actions[1]).toMatchObject({ type: 'set_fields' }); }); }); describe('summarizeCapabilityResult', () => { it('includes date_from date_field and search for list_orders', () => { const summary = summarizeCapabilityResult('list_orders', { ok: true, data: { count: 5, total_amount: 9200.85, date_from: '2026-09-01', date_to: '2026-09-01', date_field: 'created_at', search: '白菜', }, }); expect(summary).toContain('订单 5 个'); expect(summary).toContain('9200.85'); expect(summary).toContain('下单日 2026-09-01'); expect(summary).toContain('搜索 白菜'); }); });