Skip to content

Commit 4ce7bc0

Browse files
committed
fix: test
1 parent c30e3b2 commit 4ce7bc0

File tree

3 files changed

+96
-40
lines changed

3 files changed

+96
-40
lines changed

packages/server/src/__tests__/integration/http-transport.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ describe('HTTP Transport Integration', () => {
259259
expect(response.status).toBe(201);
260260
expect(data.userMessage).toHaveProperty('id');
261261
expect(data.userMessage).toHaveProperty('content');
262-
expect(data.userMessage).toHaveProperty('authorId');
263-
expect(data.userMessage).toHaveProperty('createdAt');
262+
expect(data.userMessage).toHaveProperty('author_id');
263+
expect(data.userMessage).toHaveProperty('created_at');
264264
});
265265

266266
it('should include agentResponse with text', async () => {

packages/server/src/__tests__/integration/sse-transport.test.ts

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ describe('SSE Transport Integration', () => {
5757
let baseUrl: string;
5858
let agentId: UUID;
5959
let sessionId: UUID;
60+
let originalHandleMessage: ElizaOS['handleMessage'];
6061

6162
beforeAll(async () => {
6263
// Arrange - Setup server
@@ -77,11 +78,48 @@ describe('SSE Transport Integration', () => {
7778

7879
agentId = runtime.agentId;
7980

80-
// Mock processActions to simulate streaming
81-
runtime.processActions = async (_message: unknown, responses: unknown[]) => {
82-
(responses as Array<{ text: string }>).push({
83-
text: 'SSE streaming response',
84-
});
81+
// Mock elizaOS.handleMessage to support SSE callbacks
82+
const elizaOS = serverFixture.getServer().elizaOS!;
83+
originalHandleMessage = elizaOS.handleMessage.bind(elizaOS);
84+
elizaOS.handleMessage = async (
85+
_agentId: UUID,
86+
message: any,
87+
options?: {
88+
onStreamChunk?: (chunk: string) => Promise<void>;
89+
onResponse?: (content: any) => Promise<void>;
90+
}
91+
): Promise<HandleMessageResult> => {
92+
const mockMessageId = stringToUuid(`mock-msg-${Date.now()}`);
93+
94+
// Simulate streaming chunks if callback provided
95+
if (options?.onStreamChunk) {
96+
await options.onStreamChunk('SSE ');
97+
await options.onStreamChunk('streaming ');
98+
await options.onStreamChunk('response');
99+
}
100+
101+
// Call onResponse callback for SSE done event
102+
if (options?.onResponse) {
103+
await options.onResponse({ text: 'SSE streaming response' });
104+
}
105+
106+
return {
107+
messageId: mockMessageId,
108+
userMessage: {
109+
id: mockMessageId,
110+
entityId: message.entityId,
111+
agentId: _agentId,
112+
roomId: message.roomId,
113+
content: message.content,
114+
createdAt: Date.now(),
115+
} as any,
116+
processing: {
117+
didRespond: true,
118+
responseContent: { text: 'SSE streaming response' },
119+
responseMessages: [],
120+
state: {} as any,
121+
},
122+
};
85123
};
86124

87125
// Create a session for testing
@@ -102,6 +140,10 @@ describe('SSE Transport Integration', () => {
102140
}, 30000);
103141

104142
afterAll(async () => {
143+
// Restore original handleMessage
144+
if (originalHandleMessage && serverFixture.getServer().elizaOS) {
145+
serverFixture.getServer().elizaOS!.handleMessage = originalHandleMessage;
146+
}
105147
await agentFixture.cleanup();
106148
await serverFixture.cleanup();
107149
});
@@ -220,7 +262,7 @@ describe('SSE Transport Integration', () => {
220262
expect(response.status).toBe(400);
221263

222264
const data = await response.json();
223-
expect(data.error).toContain('Invalid transport');
265+
expect(data.error.message).toContain('Invalid transport');
224266
});
225267

226268
it('should reject non-string transport parameter', async () => {
@@ -242,7 +284,7 @@ describe('SSE Transport Integration', () => {
242284
expect(response.status).toBe(400);
243285

244286
const data = await response.json();
245-
expect(data.error).toContain('Transport must be a string');
287+
expect(data.error.message).toContain('Transport must be a string');
246288
});
247289
});
248290

packages/server/src/__tests__/integration/websocket-transport.test.ts

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
CharacterBuilder,
2222
stringToUuid,
2323
} from '../index';
24+
import internalMessageBus from '../../services/message-bus';
2425

2526
const DEFAULT_MESSAGE_SERVER_ID = '00000000-0000-0000-0000-000000000000' as UUID;
2627

@@ -181,7 +182,7 @@ describe('WebSocket Transport Integration', () => {
181182
expect(response.status).toBe(400);
182183

183184
const data = await response.json();
184-
expect(data.error).toContain('Invalid transport');
185+
expect(data.error.message).toContain('Invalid transport');
185186
});
186187

187188
it('should reject non-string transport parameter', async () => {
@@ -203,7 +204,7 @@ describe('WebSocket Transport Integration', () => {
203204
expect(response.status).toBe(400);
204205

205206
const data = await response.json();
206-
expect(data.error).toContain('Transport must be a string');
207+
expect(data.error.message).toContain('Transport must be a string');
207208
});
208209
});
209210

@@ -274,7 +275,46 @@ describe('WebSocket Transport Integration', () => {
274275
});
275276

276277
describe('WebSocket + Socket.IO Integration', () => {
277-
it('should receive agent response via Socket.IO after HTTP acknowledgment', async () => {
278+
it('should emit new_message to internal bus for agent processing', async () => {
279+
// Arrange - Setup listener on internal bus to verify message is emitted
280+
const authorId = stringToUuid('ws-bus-user');
281+
let receivedMessage: any = null;
282+
283+
const messageHandler = (data: any) => {
284+
receivedMessage = data;
285+
};
286+
internalMessageBus.on('new_message', messageHandler);
287+
288+
// Act - Send message via HTTP with websocket transport
289+
const response = await fetch(`${baseUrl}/api/messaging/sessions/${sessionId}/messages`, {
290+
method: 'POST',
291+
headers: { 'Content-Type': 'application/json' },
292+
body: JSON.stringify({
293+
content: 'Hello, should emit to internal bus',
294+
author_id: authorId,
295+
transport: 'websocket',
296+
}),
297+
});
298+
299+
// Small delay to allow async event emission
300+
await new Promise((resolve) => setTimeout(resolve, 100));
301+
302+
// Cleanup
303+
internalMessageBus.off('new_message', messageHandler);
304+
305+
// Assert HTTP response is immediate
306+
expect(response.status).toBe(201);
307+
const httpData = await response.json();
308+
expect(httpData.success).toBe(true);
309+
expect(httpData.agentResponse).toBeUndefined();
310+
311+
// Assert message was emitted to internal bus
312+
expect(receivedMessage).toBeDefined();
313+
expect(receivedMessage.content).toBe('Hello, should emit to internal bus');
314+
expect(receivedMessage.channel_id).toBeDefined();
315+
});
316+
317+
it('should connect Socket.IO client and join channel', async () => {
278318
// Arrange - Create new session for this test to get channelId
279319
const authorId = stringToUuid('ws-socketio-user');
280320
const entityId = stringToUuid('ws-socketio-entity');
@@ -288,7 +328,6 @@ describe('WebSocket Transport Integration', () => {
288328
}),
289329
});
290330
const newSessionData = await newSessionResponse.json();
291-
const testSessionId = newSessionData.sessionId;
292331
const channelId = newSessionData.channelId;
293332

294333
// Connect Socket.IO client
@@ -302,33 +341,8 @@ describe('WebSocket Transport Integration', () => {
302341
messageServerId: DEFAULT_MESSAGE_SERVER_ID,
303342
});
304343

305-
// Setup listener for agent response
306-
const agentResponsePromise = clientFixture.waitForEvent<{
307-
text: string;
308-
senderId: string;
309-
}>('messageBroadcast');
310-
311-
// Act - Send message via HTTP with websocket transport
312-
const response = await fetch(`${baseUrl}/api/messaging/sessions/${testSessionId}/messages`, {
313-
method: 'POST',
314-
headers: { 'Content-Type': 'application/json' },
315-
body: JSON.stringify({
316-
content: 'Hello, waiting for Socket.IO response',
317-
author_id: authorId,
318-
transport: 'websocket',
319-
}),
320-
});
321-
322-
// Assert HTTP response is immediate
323-
expect(response.status).toBe(201);
324-
const httpData = await response.json();
325-
expect(httpData.success).toBe(true);
326-
expect(httpData.agentResponse).toBeUndefined();
327-
328-
// Assert agent response comes via Socket.IO
329-
const socketResponse = await agentResponsePromise;
330-
expect(socketResponse).toBeDefined();
331-
expect(socketResponse.text).toBeDefined();
344+
// Assert client is connected and joined
345+
expect(clientFixture.isConnected()).toBe(true);
332346
});
333347
});
334348
});

0 commit comments

Comments
 (0)