|
| 1 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 2 | +import { FlowChatStore } from '../../store/FlowChatStore'; |
| 3 | +import type { DialogTurn, FlowToolItem, ModelRound, Session } from '../../types/flow-chat'; |
| 4 | +import { processToolParamsPartialInternal } from './ToolEventModule'; |
| 5 | + |
| 6 | +function resetStore(): void { |
| 7 | + FlowChatStore.getInstance().setState(() => ({ |
| 8 | + sessions: new Map(), |
| 9 | + activeSessionId: null, |
| 10 | + })); |
| 11 | +} |
| 12 | + |
| 13 | +function createSessionWithTool(tool: FlowToolItem): Session { |
| 14 | + const round: ModelRound = { |
| 15 | + id: 'round-1', |
| 16 | + index: 0, |
| 17 | + items: [tool], |
| 18 | + isStreaming: true, |
| 19 | + isComplete: false, |
| 20 | + status: 'streaming', |
| 21 | + startTime: 1000, |
| 22 | + }; |
| 23 | + const turn: DialogTurn = { |
| 24 | + id: 'turn-1', |
| 25 | + sessionId: 'session-1', |
| 26 | + userMessage: { |
| 27 | + id: 'user-1', |
| 28 | + content: 'Inspect this file', |
| 29 | + timestamp: 900, |
| 30 | + }, |
| 31 | + modelRounds: [round], |
| 32 | + status: 'processing', |
| 33 | + startTime: 900, |
| 34 | + }; |
| 35 | + |
| 36 | + return { |
| 37 | + sessionId: 'session-1', |
| 38 | + title: 'Session 1', |
| 39 | + dialogTurns: [turn], |
| 40 | + status: 'active', |
| 41 | + config: { agentType: 'agentic' }, |
| 42 | + createdAt: 800, |
| 43 | + lastActiveAt: 1000, |
| 44 | + error: null, |
| 45 | + sessionKind: 'normal', |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +describe('processToolParamsPartialInternal', () => { |
| 50 | + afterEach(() => { |
| 51 | + resetStore(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('drops malformed non-string params fragments without replacing existing preview state', () => { |
| 55 | + const existingParams = { file_path: 'src/main.rs' }; |
| 56 | + const tool: FlowToolItem = { |
| 57 | + id: 'tool-1', |
| 58 | + type: 'tool', |
| 59 | + toolName: 'Read', |
| 60 | + timestamp: 1001, |
| 61 | + status: 'streaming', |
| 62 | + toolCall: { |
| 63 | + id: 'tool-1', |
| 64 | + input: existingParams, |
| 65 | + }, |
| 66 | + isParamsStreaming: true, |
| 67 | + partialParams: existingParams, |
| 68 | + _paramsBuffer: '{"file_path":"src/main.rs"}', |
| 69 | + }; |
| 70 | + |
| 71 | + FlowChatStore.getInstance().setState(() => ({ |
| 72 | + sessions: new Map([['session-1', createSessionWithTool(tool)]]), |
| 73 | + activeSessionId: 'session-1', |
| 74 | + })); |
| 75 | + |
| 76 | + expect(() => { |
| 77 | + processToolParamsPartialInternal('session-1', 'turn-1', { |
| 78 | + event_type: 'ParamsPartial', |
| 79 | + tool_id: 'tool-1', |
| 80 | + tool_name: 'Read', |
| 81 | + params: { file_path: 'src/lib.rs' } as any, |
| 82 | + }); |
| 83 | + }).not.toThrow(); |
| 84 | + |
| 85 | + const updatedTool = FlowChatStore.getInstance() |
| 86 | + .findToolItem('session-1', 'turn-1', 'tool-1') as FlowToolItem; |
| 87 | + |
| 88 | + expect(updatedTool._paramsBuffer).toBe('{"file_path":"src/main.rs"}'); |
| 89 | + expect(updatedTool.partialParams).toEqual(existingParams); |
| 90 | + expect(updatedTool.toolCall.input).toEqual(existingParams); |
| 91 | + }); |
| 92 | +}); |
0 commit comments