|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { hydratedMessagesToChatMessages } from '../AiChatPage'; |
| 5 | +import type { HydratedUIMessage } from '../../../hooks/useChatConversation'; |
| 6 | + |
| 7 | +function assistantWith(parts: HydratedUIMessage['parts']): HydratedUIMessage[] { |
| 8 | + return [{ id: 'm1', role: 'assistant', parts }]; |
| 9 | +} |
| 10 | + |
| 11 | +describe('AiChatPage hydration — tool invocation states', () => { |
| 12 | + it('promotes STATELESS tool parts to output-available (server ModelMessage tool-call entries)', () => { |
| 13 | + // Server conversations persist ModelMessage content: `tool-call` entries |
| 14 | + // carry toolName/toolCallId but NO UI state. Hydrated history has ended, |
| 15 | + // so stateless must render Completed — not an eternal "Running" chip. |
| 16 | + const [msg] = hydratedMessagesToChatMessages( |
| 17 | + assistantWith([ |
| 18 | + { type: 'tool-call', toolCallId: 't1', toolName: 'propose_blueprint' }, |
| 19 | + ]), |
| 20 | + ); |
| 21 | + expect(msg.toolInvocations).toEqual([ |
| 22 | + { toolCallId: 't1', toolName: 'propose_blueprint', state: 'output-available' }, |
| 23 | + ]); |
| 24 | + }); |
| 25 | + |
| 26 | + it('promotes dangling mid-stream states to output-available', () => { |
| 27 | + const [msg] = hydratedMessagesToChatMessages( |
| 28 | + assistantWith([ |
| 29 | + { type: 'tool-add_field', toolCallId: 't1', toolName: 'add_field', state: 'input-available' }, |
| 30 | + { type: 'tool-create_metadata', toolCallId: 't2', toolName: 'create_metadata', state: 'input-streaming' }, |
| 31 | + ]), |
| 32 | + ); |
| 33 | + expect(msg.toolInvocations?.map((t) => t.state)).toEqual(['output-available', 'output-available']); |
| 34 | + }); |
| 35 | + |
| 36 | + it('preserves genuine terminal states', () => { |
| 37 | + const [msg] = hydratedMessagesToChatMessages( |
| 38 | + assistantWith([ |
| 39 | + { type: 'tool-add_field', toolCallId: 't1', toolName: 'add_field', state: 'output-error', errorText: 'boom' }, |
| 40 | + { type: 'tool-verify_build', toolCallId: 't2', toolName: 'verify_build', state: 'output-denied' }, |
| 41 | + ]), |
| 42 | + ); |
| 43 | + expect(msg.toolInvocations).toEqual([ |
| 44 | + { toolCallId: 't1', toolName: 'add_field', state: 'output-error', errorText: 'boom' }, |
| 45 | + { toolCallId: 't2', toolName: 'verify_build', state: 'output-denied' }, |
| 46 | + ]); |
| 47 | + }); |
| 48 | +}); |
0 commit comments