Skip to content

Commit

Permalink
fix: add unit tests for dotnet and improve test infrastructure (#5269)
Browse files Browse the repository at this point in the history
  • Loading branch information
bassmang authored Jan 30, 2025
1 parent fca1de9 commit 994c866
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 144 deletions.
198 changes: 55 additions & 143 deletions dotnet/test/Microsoft.AutoGen.Core.Tests/AgentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,71 @@

namespace Microsoft.AutoGen.Core.Tests;

//[Collection(ClusterFixtureCollection.Name)]
public class AgentTests()
{
[Fact]
public async Task Agent_ShouldNotReceiveMessages_WhenNotSubscribed()
public async Task AgentShouldNotReceiveMessagesWhenNotSubscribedTest()
{
var runtime = new InProcessRuntime();
await runtime.StartAsync();

Logger<BaseAgent> logger = new(new LoggerFactory());
await runtime.RegisterAgentFactoryAsync("MyAgent", (id, runtime) => ValueTask.FromResult(new TestAgent(id, runtime, logger)));
await runtime.RegisterImplicitAgentSubscriptionsAsync<TestAgent>("MyAgent");
TestAgent agent = null!;

var topicType = "TestTopic";
await runtime.RegisterAgentFactoryAsync("MyAgent", (id, runtime) =>
{
agent = new TestAgent(id, runtime, logger);
return ValueTask.FromResult(agent);
});

// Ensure the agent is actually created
AgentId agentId = await runtime.GetAgentAsync("MyAgent", lazy: false);

await runtime.PublishMessageAsync(new TextMessage { Source = topicType, Content = "test" }, new TopicId("TestTopic")).ConfigureAwait(true);
// Validate agent ID
agentId.Should().Be(agent.Id, "Agent ID should match the registered agent");

var topicType = "TestTopic";

await runtime.PublishMessageAsync(new TextMessage { Source = topicType, Content = "test" }, new TopicId(topicType)).ConfigureAwait(true);
await runtime.RunUntilIdleAsync();

TestAgent.ReceivedMessages.Any().Should().BeFalse("Agent should not receive messages when not subscribed.");
agent.ReceivedMessages.Any().Should().BeFalse("Agent should not receive messages when not subscribed.");
}

[Fact]
public async Task Agent_ShoulReceiveMessages_WhenSubscribed()
public async Task AgentShouldReceiveMessagesWhenSubscribedTest()
{
var runtime = new InProcessRuntime();
await runtime.StartAsync();

Logger<BaseAgent> logger = new(new LoggerFactory());
await runtime.RegisterAgentFactoryAsync("MyAgent", (id, runtime) => ValueTask.FromResult(new SubscribedAgent(id, runtime, logger)));
SubscribedAgent agent = null!;

await runtime.RegisterAgentFactoryAsync("MyAgent", (id, runtime) =>
{
agent = new SubscribedAgent(id, runtime, logger);
return ValueTask.FromResult(agent);
});

// Ensure the agent is actually created
AgentId agentId = await runtime.GetAgentAsync("MyAgent", lazy: false);

// Validate agent ID
agentId.Should().Be(agent.Id, "Agent ID should match the registered agent");

await runtime.RegisterImplicitAgentSubscriptionsAsync<SubscribedAgent>("MyAgent");

var topicType = "TestTopic";

await runtime.PublishMessageAsync(new TextMessage { Source = topicType, Content = "test" }, new TopicId("TestTopic")).ConfigureAwait(true);
await runtime.PublishMessageAsync(new TextMessage { Source = topicType, Content = "test" }, new TopicId(topicType)).ConfigureAwait(true);

await runtime.RunUntilIdleAsync();

TestAgent.ReceivedMessages.Any().Should().BeTrue("Agent should receive messages when subscribed.");
agent.ReceivedMessages.Any().Should().BeTrue("Agent should receive messages when subscribed.");
}

[Fact]
public async Task SendMessageAsync_ShouldReturnResponse()
public async Task SendMessageAsyncShouldReturnResponseTest()
{
// Arrange
var runtime = new InProcessRuntime();
Expand All @@ -72,43 +94,6 @@ public async Task SendMessageAsync_ShouldReturnResponse()
}
}

// / <summary>
// / Verify that if the agent is not initialized via AgentWorker, it should throw the correct exception.
// / </summary>
// / <returns>void</returns>
// [Fact]
// public async Task Agent_ShouldThrowException_WhenNotInitialized()
// {
// using var fixture = new InMemoryAgentRuntimeFixture();
// var agent = ActivatorUtilities.CreateInstance<TestAgent>(fixture.AppHost.Services);
// await Assert.ThrowsAsync<UninitializedAgentWorker.AgentInitalizedIncorrectlyException>(
// async () =>
// {
// await agent.AddSubscriptionAsync("TestEvent");
// }
// );
// }

// /// <summary>
// /// validate that the agent is initialized correctly with implicit subs
// /// </summary>
// /// <returns>void</returns>
// [Fact]
// public async Task Agent_ShouldInitializeCorrectly()
// {
// var fixture = new InMemoryAgentRuntimeFixture();
// var (runtime, agent) = fixture.Start();
// Assert.Equal(nameof(AgentRuntime), runtime.GetType().Name);
// var subscriptions = await agent.GetSubscriptionsAsync();
// Assert.Equal(2, subscriptions.Count);
// fixture.Stop();
// }
/// <summary>
/// Test AddSubscriptionAsync method
/// </summary>
/// <returns>void</returns>
///

public class ReceiverAgent(AgentId id,
IAgentRuntime runtime) : BaseAgent(id, runtime, "Receiver Agent", null),
IHandle<string>
Expand All @@ -123,7 +108,7 @@ public ValueTask HandleAsync(string item, MessageContext messageContext)
}

[Fact]
public async Task SubscribeAsync_UnsubscribeAsync_and_GetSubscriptionsTest()
public async Task SubscribeAsyncRemoveSubscriptionAsyncAndGetSubscriptionsTest()
{
var runtime = new InProcessRuntime();
await runtime.StartAsync();
Expand Down Expand Up @@ -161,97 +146,24 @@ await runtime.RegisterAgentFactoryAsync("MyAgent", (id, runtime) =>
Assert.True(agent.ReceivedItems.Count == 1);
}

// /// <summary>
// /// Test StoreAsync and ReadAsync methods
// /// </summary>
// /// <returns>void</returns>
// [Fact]
// public async Task StoreAsync_and_ReadAsyncTest()
// {
// var fixture = new InMemoryAgentRuntimeFixture();
// var (_, agent) = fixture.Start();
// Dictionary<string, string> state = new()
// {
// { "testdata", "Active" }
// };
// await agent.StoreAsync(new AgentState
// {
// AgentId = agent.AgentId,
// TextData = JsonSerializer.Serialize(state)
// }).ConfigureAwait(true);
// var readState = await agent.ReadAsync<AgentState>(agent.AgentId).ConfigureAwait(true);
// var read = JsonSerializer.Deserialize<Dictionary<string, string>>(readState.TextData) ?? new Dictionary<string, string> { { "data", "No state data found" } };
// read.TryGetValue("testdata", out var value);
// Assert.Equal("Active", value);
// fixture.Stop();
// }

// /// <summary>
// /// Test PublishMessageAsync method and ReceiveMessage method
// /// </summary>
// /// <returns>void</returns>
// [Fact]
// public async Task PublishMessageAsync_and_ReceiveMessageTest()
// {
// var fixture = new InMemoryAgentRuntimeFixture();
// var (_, agent) = fixture.Start();
// var topicType = "TestTopic";
// await agent.AddSubscriptionAsync(topicType).ConfigureAwait(true);
// var subscriptions = await agent.GetSubscriptionsAsync().ConfigureAwait(true);
// var found = false;
// foreach (var subscription in subscriptions)
// {
// if (subscription.TypeSubscription.TopicType == topicType)
// {
// found = true;
// }
// }
// Assert.True(found);
// await agent.PublishMessageAsync(new TextMessage()
// {
// Source = topicType,
// TextMessage_ = "buffer"
// }, topicType).ConfigureAwait(true);
// await Task.Delay(100);
// Assert.True(TestAgent.ReceivedMessages.ContainsKey(topicType));
// fixture.Stop();
// }

// [Fact]
// public async Task InvokeCorrectHandler()
// {
// var agent = new TestAgent(new AgentsMetadata(TypeRegistry.Empty, new Dictionary<string, Type>(), new Dictionary<Type, HashSet<string>>(), new Dictionary<Type, HashSet<string>>()), new Logger<Agent>(new LoggerFactory()));
// await agent.HandleObjectAsync("hello world");
// await agent.HandleObjectAsync(42);
// agent.ReceivedItems.Should().HaveCount(2);
// agent.ReceivedItems[0].Should().Be("hello world");
// agent.ReceivedItems[1].Should().Be(42);
// }

// [Fact]
// public async Task DelegateMessageToTestAgentAsync()
// {
// var runtime = new InMemoryAgentRuntimeFixture();
// var client = runtime.AppHost.Services.GetRequiredService<Client>();
// await client.PublishMessageAsync(new TextMessage()
// {
// Source = nameof(DelegateMessageToTestAgentAsync),
// TextMessage_ = "buffer"
// }, token: CancellationToken.None);

// // wait for 10 seconds
// var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
// while (!TestAgent.ReceivedMessages.ContainsKey(nameof(DelegateMessageToTestAgentAsync)) && !cts.Token.IsCancellationRequested)
// {
// await Task.Delay(100);
// }

// TestAgent.ReceivedMessages[nameof(DelegateMessageToTestAgentAsync)].Should().NotBeNull();
// }

// [CollectionDefinition(Name)]
// public sealed class ClusterFixtureCollection : ICollectionFixture<InMemoryAgentRuntimeFixture>
// {
// public const string Name = nameof(ClusterFixtureCollection);
// }
[Fact]
public async Task AgentShouldSaveStateCorrectlyTest()
{
var runtime = new InProcessRuntime();
await runtime.StartAsync();

Logger<BaseAgent> logger = new(new LoggerFactory());
TestAgent agent = new TestAgent(new AgentId("TestType", "TestKey"), runtime, logger);

var state = await agent.SaveStateAsync();

// Ensure state is a dictionary
state.Should().NotBeNull();
state.Should().BeOfType<Dictionary<string, object>>();
state.Should().BeEmpty("Default SaveStateAsync should return an empty dictionary.");

// Add a sample value and verify it updates correctly
state["testKey"] = "testValue";
state.Should().ContainKey("testKey").WhoseValue.Should().Be("testValue");
}
}
3 changes: 2 additions & 1 deletion dotnet/test/Microsoft.AutoGen.Core.Tests/TestAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public ValueTask<string> HandleAsync(RpcTextMessage item, MessageContext message
/// Key: source
/// Value: message
/// </summary>
public static Dictionary<string, object> ReceivedMessages { get; private set; } = new();
private readonly Dictionary<string, object> _receivedMessages = new();
public Dictionary<string, object> ReceivedMessages => _receivedMessages;
}

[TypeSubscription("TestTopic")]
Expand Down

0 comments on commit 994c866

Please sign in to comment.