|
1 | 1 | import {describe, expect, it, vi, beforeEach} from 'vitest'; |
2 | 2 | import type {CodexAuthRequest} from "../../CodexAuthMethod"; |
3 | | -import {createTestFixture, type TestFixture} from "../acp-test-utils"; |
| 3 | +import {createTestFixture, createCodexMockTestFixture, type TestFixture} from "../acp-test-utils"; |
4 | 4 | import type {ServerNotification} from "../../app-server"; |
5 | 5 | import type {SessionState} from "../../CodexAcpServer"; |
6 | 6 |
|
@@ -64,7 +64,7 @@ describe('ACP server test', { timeout: 40_000 }, () => { |
64 | 64 | { method: "item/agentMessage/delta", params: { threadId: "string", turnId: "string", itemId: "string", delta: "ll", }}, |
65 | 65 | { method: "item/agentMessage/delta", params: { threadId: "string", turnId: "string", itemId: "string", delta: "o!", }}, |
66 | 66 | ]; |
67 | | - function onServerNotification(callback: (event: ServerNotification) => void){ |
| 67 | + function onServerNotification(_sessionId: string, callback: (event: ServerNotification) => void){ |
68 | 68 | for (const notification of serverNotifications) { |
69 | 69 | callback(notification); |
70 | 70 | } |
@@ -95,6 +95,102 @@ describe('ACP server test', { timeout: 40_000 }, () => { |
95 | 95 |
|
96 | 96 | }); |
97 | 97 |
|
| 98 | + it('should not duplicate messages on follow-up prompts', async () => { |
| 99 | + const mockFixture = createCodexMockTestFixture(); |
| 100 | + const codexAcpAgent = mockFixture.getCodexAcpAgent(); |
| 101 | + |
| 102 | + mockFixture.getCodexAppServerClient().turnStart = vi.fn().mockResolvedValue(undefined); |
| 103 | + mockFixture.getCodexAppServerClient().awaitTurnCompleted = vi.fn().mockResolvedValue(undefined); |
| 104 | + |
| 105 | + const sessionState: SessionState = { |
| 106 | + pendingPrompt: null, |
| 107 | + sessionMetadata: { |
| 108 | + sessionId: "id", |
| 109 | + currentModelId: "model-id", |
| 110 | + models: [], |
| 111 | + } |
| 112 | + }; |
| 113 | + vi.spyOn(codexAcpAgent, "getSessionState").mockReturnValue(sessionState); |
| 114 | + |
| 115 | + // First prompt - registers first notification handler |
| 116 | + await codexAcpAgent.prompt({ sessionId: "id", prompt: [{type: "text", text: "First message"}] }); |
| 117 | + |
| 118 | + // Follow-up prompt - should NOT accumulate handlers |
| 119 | + await codexAcpAgent.prompt({ sessionId: "id", prompt: [{type: "text", text: "Follow-up message"}] }); |
| 120 | + |
| 121 | + mockFixture.clearAcpConnectionDump(); |
| 122 | + |
| 123 | + // Trigger notifications after both prompts - should produce only 3 events, not 6 |
| 124 | + const serverNotifications: ServerNotification[] = [ |
| 125 | + { method: "item/agentMessage/delta", params: { threadId: "string", turnId: "string", itemId: "string", delta: "He", }}, |
| 126 | + { method: "item/agentMessage/delta", params: { threadId: "string", turnId: "string", itemId: "string", delta: "ll", }}, |
| 127 | + { method: "item/agentMessage/delta", params: { threadId: "string", turnId: "string", itemId: "string", delta: "o!", }}, |
| 128 | + ]; |
| 129 | + for (const notification of serverNotifications) { |
| 130 | + mockFixture.sendServerNotification(notification); |
| 131 | + } |
| 132 | + |
| 133 | + // Wait for async handlers to complete |
| 134 | + await vi.waitFor(() => { |
| 135 | + const dump = mockFixture.getAcpConnectionDump([]); |
| 136 | + expect(dump.length).toBeGreaterThan(0); |
| 137 | + }); |
| 138 | + |
| 139 | + await expect(mockFixture.getAcpConnectionDump([])).toMatchFileSnapshot("data/follow-up-no-duplicates.json"); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should handle multiple sessions independently', async () => { |
| 143 | + const mockFixture = createCodexMockTestFixture(); |
| 144 | + const codexAcpAgent = mockFixture.getCodexAcpAgent(); |
| 145 | + |
| 146 | + mockFixture.getCodexAppServerClient().turnStart = vi.fn().mockResolvedValue(undefined); |
| 147 | + mockFixture.getCodexAppServerClient().awaitTurnCompleted = vi.fn().mockResolvedValue(undefined); |
| 148 | + |
| 149 | + const sessionState1: SessionState = { |
| 150 | + pendingPrompt: null, |
| 151 | + sessionMetadata: { |
| 152 | + sessionId: "session-1", |
| 153 | + currentModelId: "model-id", |
| 154 | + models: [], |
| 155 | + } |
| 156 | + }; |
| 157 | + const sessionState2: SessionState = { |
| 158 | + pendingPrompt: null, |
| 159 | + sessionMetadata: { |
| 160 | + sessionId: "session-2", |
| 161 | + currentModelId: "model-id", |
| 162 | + models: [], |
| 163 | + } |
| 164 | + }; |
| 165 | + |
| 166 | + vi.spyOn(codexAcpAgent, "getSessionState").mockImplementation((sessionId: string) => { |
| 167 | + return sessionId === "session-1" ? sessionState1 : sessionState2; |
| 168 | + }); |
| 169 | + |
| 170 | + // Start prompts for two different sessions |
| 171 | + await codexAcpAgent.prompt({ sessionId: "session-1", prompt: [{type: "text", text: "Message to session 1"}] }); |
| 172 | + await codexAcpAgent.prompt({ sessionId: "session-2", prompt: [{type: "text", text: "Message to session 2"}] }); |
| 173 | + |
| 174 | + mockFixture.clearAcpConnectionDump(); |
| 175 | + |
| 176 | + // Trigger notifications - both session handlers should receive them |
| 177 | + const serverNotifications: ServerNotification[] = [ |
| 178 | + { method: "item/agentMessage/delta", params: { threadId: "string", turnId: "string", itemId: "string", delta: "Hello", }}, |
| 179 | + ]; |
| 180 | + for (const notification of serverNotifications) { |
| 181 | + mockFixture.sendServerNotification(notification); |
| 182 | + } |
| 183 | + |
| 184 | + // Wait for async handlers to complete |
| 185 | + await vi.waitFor(() => { |
| 186 | + const dump = mockFixture.getAcpConnectionDump([]); |
| 187 | + expect(dump.length).toBeGreaterThan(0); |
| 188 | + }); |
| 189 | + |
| 190 | + // Should have 2 events - one for each session's handler |
| 191 | + await expect(mockFixture.getAcpConnectionDump([])).toMatchFileSnapshot("data/multiple-sessions.json"); |
| 192 | + }); |
| 193 | + |
98 | 194 | //dev-time test |
99 | 195 | it.skip('should convert session notification to acp events', async () => { |
100 | 196 | fixture.onCodexConnectionEvent((event) => { |
|
0 commit comments