A programming framework for agentic AI
feat: dotnet runtime tests (#5342)
Griffin Bassman442df1839774
diff --git a/dotnet/test/Microsoft.AutoGen.Core.Tests/AgentRuntimeTests.cs b/dotnet/test/Microsoft.AutoGen.Core.Tests/AgentRuntimeTests.csnew file mode 100644index 000000000..812d47c2d--- /dev/null+++ b/dotnet/test/Microsoft.AutoGen.Core.Tests/AgentRuntimeTests.cs@@ -0,0 +1,83 @@+// Copyright (c) Microsoft Corporation. All rights reserved.+// AgentRuntimeTests.cs+using FluentAssertions;+using Microsoft.AutoGen.Contracts;+using Microsoft.Extensions.Logging;+using Xunit;++namespace Microsoft.AutoGen.Core.Tests;++[Trait("Category", "UnitV2")]+public class AgentRuntimeTests()+{+ // Agent will not deliver to self will success when runtime.DeliverToSelf is false (default)+ [Fact]+ public async Task RuntimeAgentPublishToSelfDefaultNoSendTest()+ {+ var runtime = new InProcessRuntime();+ await runtime.StartAsync();++ Logger<BaseAgent> logger = new(new LoggerFactory());+ SubscribedSelfPublishAgent agent = null!;++ await runtime.RegisterAgentFactoryAsync("MyAgent", (id, runtime) =>+ {+ agent = new SubscribedSelfPublishAgent(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<SubscribedSelfPublishAgent>("MyAgent");++ var topicType = "TestTopic";++ await runtime.PublishMessageAsync("SelfMessage", new TopicId(topicType)).ConfigureAwait(true);++ await runtime.RunUntilIdleAsync();++ // Agent has default messages and could not publish to self+ agent.Text.Source.Should().Be("DefaultTopic");+ agent.Text.Content.Should().Be("DefaultContent");+ }++ // Agent delivery to self will success when runtime.DeliverToSelf is true+ [Fact]+ public async Task RuntimeAgentPublishToSelfDeliverToSelfTrueTest()+ {+ var runtime = new InProcessRuntime();+ runtime.DeliverToSelf = true;+ await runtime.StartAsync();++ Logger<BaseAgent> logger = new(new LoggerFactory());+ SubscribedSelfPublishAgent agent = null!;++ await runtime.RegisterAgentFactoryAsync("MyAgent", (id, runtime) =>+ {+ agent = new SubscribedSelfPublishAgent(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<SubscribedSelfPublishAgent>("MyAgent");++ var topicType = "TestTopic";++ await runtime.PublishMessageAsync("SelfMessage", new TopicId(topicType)).ConfigureAwait(true);++ await runtime.RunUntilIdleAsync();++ // Agent sucessfully published to self+ agent.Text.Source.Should().Be("TestTopic");+ agent.Text.Content.Should().Be("SelfMessage");+ }+}diff --git a/dotnet/test/Microsoft.AutoGen.Core.Tests/TestAgent.cs b/dotnet/test/Microsoft.AutoGen.Core.Tests/TestAgent.csindex af752ae0e..b6dadc833 100644--- a/dotnet/test/Microsoft.AutoGen.Core.Tests/TestAgent.cs+++ b/dotnet/test/Microsoft.AutoGen.Core.Tests/TestAgent.cs@@ -72,3 +72,33 @@ public class SubscribedAgent : TestAgent{}}++/// <summary>+/// The test agent showing an agent that subscribes to itself.+/// </summary>+[TypeSubscription("TestTopic")]+public class SubscribedSelfPublishAgent(AgentId id,+ IAgentRuntime runtime,+ Logger<BaseAgent>? logger = null) : BaseAgent(id, runtime, "Test Agent", logger),+ IHandle<string>,+ IHandle<TextMessage>+{+ public async ValueTask HandleAsync(string item, MessageContext messageContext)+ {+ TextMessage strToText = new TextMessage+ {+ Source = "TestTopic",+ Content = item+ };+ // This will publish the new message type which will be handled by the TextMessage handler+ await this.PublishMessageAsync(strToText, new TopicId("TestTopic"));+ }+ public ValueTask HandleAsync(TextMessage item, MessageContext messageContext)+ {+ _text = item;+ return ValueTask.CompletedTask;+ }++ private TextMessage _text = new TextMessage { Source = "DefaultTopic", Content = "DefaultContent" };+ public TextMessage Text => _text;+}