Skip to content

Commit bf45729

Browse files
committed
update unit tests
1 parent 654fe7a commit bf45729

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

typescript-sdk/packages/client/src/agent/__tests__/agent-mutations.test.ts

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ jest.mock("@/utils", () => ({
2626
},
2727
}));
2828

29+
// Helper function to wait for async notifications to complete
30+
const waitForAsyncNotifications = async () => {
31+
// Wait for the next tick of the event loop to ensure async operations complete
32+
await new Promise((resolve) => setImmediate(resolve));
33+
};
34+
2935
// Mock the verify and chunks modules
3036
jest.mock("@/verify", () => ({
3137
verifyEvents: jest.fn(() => (source$: Observable<any>) => source$),
@@ -86,7 +92,7 @@ describe("Agent Mutations", () => {
8692
expect(agent.messages[1]).toBe(userMessage);
8793

8894
// Wait for async notifications
89-
await new Promise((resolve) => setTimeout(resolve, 10));
95+
await waitForAsyncNotifications();
9096

9197
// Should fire onNewMessage and onMessagesChanged
9298
expect(mockSubscriber.onNewMessage).toHaveBeenCalledWith({
@@ -116,7 +122,7 @@ describe("Agent Mutations", () => {
116122
agent.addMessage(assistantMessage);
117123

118124
// Wait for async notifications
119-
await new Promise((resolve) => setTimeout(resolve, 10));
125+
await waitForAsyncNotifications();
120126

121127
expect(mockSubscriber.onNewMessage).toHaveBeenCalledWith({
122128
message: assistantMessage,
@@ -165,7 +171,7 @@ describe("Agent Mutations", () => {
165171
agent.addMessage(assistantMessage);
166172

167173
// Wait for async notifications
168-
await new Promise((resolve) => setTimeout(resolve, 10));
174+
await waitForAsyncNotifications();
169175

170176
expect(mockSubscriber.onNewMessage).toHaveBeenCalledWith({
171177
message: assistantMessage,
@@ -236,7 +242,7 @@ describe("Agent Mutations", () => {
236242
expect(agent.messages).toHaveLength(initialLength + 3);
237243

238244
// Wait for async notifications
239-
await new Promise((resolve) => setTimeout(resolve, 10));
245+
await waitForAsyncNotifications();
240246

241247
// Should fire onNewMessage for each message
242248
expect(mockSubscriber.onNewMessage).toHaveBeenCalledTimes(3);
@@ -266,7 +272,7 @@ describe("Agent Mutations", () => {
266272
expect(agent.messages).toHaveLength(initialLength);
267273

268274
// Wait for async notifications
269-
await new Promise((resolve) => setTimeout(resolve, 10));
275+
await waitForAsyncNotifications();
270276

271277
// Should still fire onMessagesChanged even for empty array
272278
expect(mockSubscriber.onMessagesChanged).toHaveBeenCalledTimes(1);
@@ -310,7 +316,7 @@ describe("Agent Mutations", () => {
310316
expect(agent.messages[1]).toEqual(newMessages[1]);
311317

312318
// Wait for async notifications
313-
await new Promise((resolve) => setTimeout(resolve, 10));
319+
await waitForAsyncNotifications();
314320

315321
// Should ONLY fire onMessagesChanged
316322
expect(mockSubscriber.onMessagesChanged).toHaveBeenCalledTimes(1);
@@ -331,7 +337,7 @@ describe("Agent Mutations", () => {
331337
expect(agent.messages).toHaveLength(0);
332338

333339
// Wait for async notifications
334-
await new Promise((resolve) => setTimeout(resolve, 10));
340+
await waitForAsyncNotifications();
335341

336342
expect(mockSubscriber.onMessagesChanged).toHaveBeenCalledTimes(1);
337343
expect(mockSubscriber.onNewMessage).not.toHaveBeenCalled();
@@ -354,7 +360,7 @@ describe("Agent Mutations", () => {
354360
expect(agent.state).not.toBe(newState); // Should be a clone
355361

356362
// Wait for async notifications
357-
await new Promise((resolve) => setTimeout(resolve, 10));
363+
await waitForAsyncNotifications();
358364

359365
// Should ONLY fire onStateChanged
360366
expect(mockSubscriber.onStateChanged).toHaveBeenCalledTimes(1);
@@ -376,40 +382,38 @@ describe("Agent Mutations", () => {
376382
expect(agent.state).toEqual({});
377383

378384
// Wait for async notifications
379-
await new Promise((resolve) => setTimeout(resolve, 10));
385+
await waitForAsyncNotifications();
380386

381387
expect(mockSubscriber.onStateChanged).toHaveBeenCalledTimes(1);
382388
});
383389
});
384390

385-
describe("sequential execution", () => {
386-
it("should execute subscriber notifications sequentially", async () => {
391+
describe("execution order", () => {
392+
it("should execute subscriber notifications in registration order", async () => {
387393
const callOrder: string[] = [];
388394

389-
const slowSubscriber: RunAgentSubscriber = {
390-
onNewMessage: jest.fn().mockImplementation(async () => {
391-
await new Promise((resolve) => setTimeout(resolve, 20));
392-
callOrder.push("slow-newMessage");
395+
const firstSubscriber: RunAgentSubscriber = {
396+
onNewMessage: jest.fn().mockImplementation(() => {
397+
callOrder.push("first-newMessage");
393398
}),
394-
onMessagesChanged: jest.fn().mockImplementation(async () => {
395-
await new Promise((resolve) => setTimeout(resolve, 10));
396-
callOrder.push("slow-messagesChanged");
399+
onMessagesChanged: jest.fn().mockImplementation(() => {
400+
callOrder.push("first-messagesChanged");
397401
}),
398402
};
399403

400-
const fastSubscriber: RunAgentSubscriber = {
404+
const secondSubscriber: RunAgentSubscriber = {
401405
onNewMessage: jest.fn().mockImplementation(() => {
402-
callOrder.push("fast-newMessage");
406+
callOrder.push("second-newMessage");
403407
}),
404408
onMessagesChanged: jest.fn().mockImplementation(() => {
405-
callOrder.push("fast-messagesChanged");
409+
callOrder.push("second-messagesChanged");
406410
}),
407411
};
408412

409413
// Clear the default subscriber and add our test subscribers
410414
agent.subscribers = [];
411-
agent.subscribe(slowSubscriber);
412-
agent.subscribe(fastSubscriber);
415+
agent.subscribe(firstSubscriber);
416+
agent.subscribe(secondSubscriber);
413417

414418
const message: Message = {
415419
id: "test-msg",
@@ -419,15 +423,17 @@ describe("Agent Mutations", () => {
419423

420424
agent.addMessage(message);
421425

422-
// Wait for all async operations to complete
423-
await new Promise((resolve) => setTimeout(resolve, 100));
426+
// Wait for all async operations to complete by polling until all calls are made
427+
while (callOrder.length < 4) {
428+
await waitForAsyncNotifications();
429+
}
424430

425431
// Verify sequential execution order
426432
expect(callOrder).toEqual([
427-
"slow-newMessage",
428-
"fast-newMessage",
429-
"slow-messagesChanged",
430-
"fast-messagesChanged",
433+
"first-newMessage",
434+
"second-newMessage",
435+
"first-messagesChanged",
436+
"second-messagesChanged",
431437
]);
432438
});
433439
});
@@ -464,7 +470,7 @@ describe("Agent Mutations", () => {
464470
agent.addMessage(message);
465471

466472
// Wait for async notifications
467-
await new Promise((resolve) => setTimeout(resolve, 10));
473+
await waitForAsyncNotifications();
468474

469475
// All subscribers should receive notifications
470476
[mockSubscriber, subscriber2, subscriber3].forEach((sub) => {

0 commit comments

Comments
 (0)