Skip to content

Commit

Permalink
improving test hygiene
Browse files Browse the repository at this point in the history
  • Loading branch information
rysweet committed Feb 24, 2025
1 parent 76a7954 commit 969f0ac
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
13 changes: 12 additions & 1 deletion typescript/src/core/InProcessRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,24 @@ export class InProcessRuntime implements IAgentRuntime {

async stop(): Promise<void> {
if (!this.isRunning) {
throw new Error("Runtime not running");
return; // Change from throwing to just returning
}

if (this.messageProcessor) {
clearInterval(this.messageProcessor);
this.messageProcessor = undefined;
}

this.isRunning = false;

// Process any remaining messages
while (this.messageDeliveryQueue.length > 0) {
await this.processNextMessage();
}

// Clear queue and subscriptions
this.messageDeliveryQueue = [];
this.subscriptions.clear();
}

private async publishMessageServicer(envelope: MessageEnvelope, deliveryToken?: AbortSignal): Promise<void> {
Expand Down
2 changes: 2 additions & 0 deletions typescript/test/core/Agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ describe('Agent', () => {
// Wait for message processing
await new Promise(resolve => setTimeout(resolve, 100));
expect(Object.keys(agent!.ReceivedMessages).length).toBe(0);

await runtime.stop(); // Add cleanup
});

it('should receive messages when subscribed', async () => {
Expand Down
34 changes: 30 additions & 4 deletions typescript/test/core/InProcessRuntime.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from '@jest/globals';
import { describe, it, expect, afterEach, jest } from '@jest/globals';
import { InProcessRuntime } from '../../src/core/InProcessRuntime';
import { TopicId, AgentId, IAgentRuntime } from '../../src/contracts/IAgentRuntime';
import { SubscribedSaveLoadAgent, SubscribedSelfPublishAgent } from './TestAgent';
Expand Down Expand Up @@ -33,6 +33,12 @@ class ThirdSubscribedAgent extends BaseAgent {
}

describe('InProcessRuntime', () => {
// Add afterEach cleanup for all tests
afterEach(async () => {
// Force cleanup any hanging runtimes
jest.clearAllTimers();
});

it('should not deliver to self by default', async () => {
const runtime = new InProcessRuntime();
console.log('Starting runtime...');
Expand Down Expand Up @@ -79,36 +85,52 @@ describe('InProcessRuntime', () => {
// Verify the text remains default (self-message wasn't delivered)
expect(agent.Text.source).toBe("DefaultTopic");
expect(agent.Text.content).toBe("DefaultContent");

await runtime.stop(); // Add cleanup
});

it('should deliver to self when deliverToSelf is true', async () => {
const runtime = new InProcessRuntime();
runtime.deliverToSelf = true;
await runtime.start(); // Add runtime start
let agent: SubscribedSelfPublishAgent;

// Create and register agent
const agentId = { type: "MyAgent", key: "test" };
await runtime.registerAgentFactoryAsync("MyAgent", async (id, runtime) => {
agent = new SubscribedSelfPublishAgent(id, runtime);
console.log('Created agent:', { id, agent: agent.constructor.name });
return agent;
});

await runtime.getAgentMetadataAsync(agentId); // Ensure agent is created
console.log('Initial agent state:', { Text: agent!.Text });

// Add subscription
await runtime.addSubscriptionAsync(new TypeSubscriptionAttribute("TestTopic").bind("MyAgent"));
console.log('Added subscription for TestTopic');

// Send message that will trigger self-publish
console.log('Publishing message...');
await runtime.publishMessageAsync("SelfMessage", { type: "TestTopic", source: "test" });
await new Promise(resolve => setTimeout(resolve, 500));


// Wait for message processing to complete - increase timeout since we have cascading messages
await new Promise(resolve => setTimeout(resolve, 1000));

console.log('Final agent state:', { Text: agent!.Text });

// Verify the text was updated (self-message was delivered)
expect(agent!.Text.source).toBe("TestTopic");
expect(agent!.Text.content).toBe("SelfMessage");
});

await runtime.stop(); // Add cleanup
}, 15000);

// Test for save/load state functionality
it('should save and load state correctly', async () => {
// Create first runtime and set up agent
const runtime = new InProcessRuntime();
await runtime.start();
let agent: SubscribedSaveLoadAgent;

await runtime.registerAgentFactoryAsync("MyAgent", async (id, runtime) => {
Expand Down Expand Up @@ -141,5 +163,9 @@ describe('InProcessRuntime', () => {

// Verify state was restored
expect(newAgent!.ReceivedMessages).toEqual(agent!.ReceivedMessages);

await runtime.stop();
// Also stop the new runtime
await newRuntime.stop();
});
});

0 comments on commit 969f0ac

Please sign in to comment.