2025-01-24 19:24:00 -08:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
// AgentGrpcTests.cs
|
|
|
|
|
using FluentAssertions;
|
|
|
|
|
using Microsoft.AutoGen.Contracts;
|
2025-02-05 11:34:02 -05:00
|
|
|
// using Microsoft.AutoGen.Core.Tests;
|
|
|
|
|
using Microsoft.AutoGen.Core.Grpc.Tests.Protobuf;
|
2025-01-24 19:24:00 -08:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.AutoGen.Core.Grpc.Tests;
|
|
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
[Trait("Category", "GRPC")]
|
2025-02-11 16:03:02 -05:00
|
|
|
public class AgentGrpcTests : TestBase
|
2025-01-24 19:24:00 -08:00
|
|
|
{
|
|
|
|
|
[Fact]
|
2025-02-05 11:34:02 -05:00
|
|
|
public async Task AgentShouldNotReceiveMessagesWhenNotSubscribedTest()
|
2025-01-24 19:24:00 -08:00
|
|
|
{
|
2025-02-05 11:34:02 -05:00
|
|
|
var fixture = new GrpcAgentRuntimeFixture();
|
2025-02-11 16:03:02 -05:00
|
|
|
var runtime = (GrpcAgentRuntime)await fixture.StartAsync();
|
2025-01-24 19:24:00 -08:00
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
Logger<BaseAgent> logger = new(new LoggerFactory());
|
|
|
|
|
TestProtobufAgent agent = null!;
|
2025-01-24 19:24:00 -08:00
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
await runtime.RegisterAgentFactoryAsync("MyAgent", async (id, runtime) =>
|
2025-01-24 19:24:00 -08:00
|
|
|
{
|
2025-02-05 11:34:02 -05:00
|
|
|
agent = new TestProtobufAgent(id, runtime, logger);
|
|
|
|
|
return await 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");
|
|
|
|
|
|
|
|
|
|
var topicType = "TestTopic";
|
|
|
|
|
|
|
|
|
|
await runtime.PublishMessageAsync(new Protobuf.TextMessage { Source = topicType, Content = "test" }, new TopicId(topicType)).ConfigureAwait(true);
|
|
|
|
|
|
|
|
|
|
agent.ReceivedMessages.Any().Should().BeFalse("Agent should not receive messages when not subscribed.");
|
|
|
|
|
fixture.Dispose();
|
2025-01-24 19:24:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2025-02-05 11:34:02 -05:00
|
|
|
public async Task AgentShouldReceiveMessagesWhenSubscribedTest()
|
2025-01-24 19:24:00 -08:00
|
|
|
{
|
2025-02-05 11:34:02 -05:00
|
|
|
var fixture = new GrpcAgentRuntimeFixture();
|
2025-02-11 16:03:02 -05:00
|
|
|
var runtime = (GrpcAgentRuntime)await fixture.StartAsync();
|
2025-02-05 11:34:02 -05:00
|
|
|
|
|
|
|
|
Logger<BaseAgent> logger = new(new LoggerFactory());
|
|
|
|
|
SubscribedProtobufAgent agent = null!;
|
|
|
|
|
|
|
|
|
|
await runtime.RegisterAgentFactoryAsync("MyAgent", async (id, runtime) =>
|
2025-01-24 19:24:00 -08:00
|
|
|
{
|
2025-02-05 11:34:02 -05:00
|
|
|
agent = new SubscribedProtobufAgent(id, runtime, logger);
|
|
|
|
|
return await 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<SubscribedProtobufAgent>("MyAgent");
|
2025-01-24 19:24:00 -08:00
|
|
|
|
|
|
|
|
var topicType = "TestTopic";
|
|
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
await runtime.PublishMessageAsync(new TextMessage { Source = topicType, Content = "test" }, new TopicId(topicType)).ConfigureAwait(true);
|
2025-01-24 19:24:00 -08:00
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
// Wait for the message to be processed
|
|
|
|
|
await Task.Delay(100);
|
2025-01-24 19:24:00 -08:00
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
agent.ReceivedMessages.Any().Should().BeTrue("Agent should receive messages when subscribed.");
|
|
|
|
|
fixture.Dispose();
|
2025-01-24 19:24:00 -08:00
|
|
|
}
|
|
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
[Fact]
|
|
|
|
|
public async Task SendMessageAsyncShouldReturnResponseTest()
|
2025-01-24 19:24:00 -08:00
|
|
|
{
|
2025-02-05 11:34:02 -05:00
|
|
|
// Arrange
|
|
|
|
|
var fixture = new GrpcAgentRuntimeFixture();
|
2025-02-11 16:03:02 -05:00
|
|
|
var runtime = (GrpcAgentRuntime)await fixture.StartAsync();
|
2025-02-05 11:34:02 -05:00
|
|
|
|
|
|
|
|
Logger<BaseAgent> logger = new(new LoggerFactory());
|
|
|
|
|
await runtime.RegisterAgentFactoryAsync("MyAgent", async (id, runtime) => await ValueTask.FromResult(new TestProtobufAgent(id, runtime, logger)));
|
|
|
|
|
var agentId = new AgentId("MyAgent", "default");
|
|
|
|
|
var response = await runtime.SendMessageAsync(new RpcTextMessage { Source = "TestTopic", Content = "Request" }, agentId);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.NotNull(response);
|
|
|
|
|
Assert.IsType<RpcTextMessage>(response);
|
|
|
|
|
if (response is RpcTextMessage responseString)
|
2025-01-24 19:24:00 -08:00
|
|
|
{
|
2025-02-05 11:34:02 -05:00
|
|
|
Assert.Equal("Request", responseString.Content);
|
2025-01-24 19:24:00 -08:00
|
|
|
}
|
2025-02-05 11:34:02 -05:00
|
|
|
fixture.Dispose();
|
2025-01-24 19:24:00 -08:00
|
|
|
}
|
|
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
public class ReceiverAgent(AgentId id,
|
|
|
|
|
IAgentRuntime runtime) : BaseAgent(id, runtime, "Receiver Agent", null),
|
|
|
|
|
IHandle<TextMessage>
|
2025-01-24 19:24:00 -08:00
|
|
|
{
|
2025-02-05 11:34:02 -05:00
|
|
|
public ValueTask HandleAsync(TextMessage item, MessageContext messageContext)
|
|
|
|
|
{
|
|
|
|
|
ReceivedItems.Add(item.Content);
|
|
|
|
|
return ValueTask.CompletedTask;
|
|
|
|
|
}
|
2025-01-24 19:24:00 -08:00
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
public List<string> ReceivedItems { get; private set; } = [];
|
2025-01-24 19:24:00 -08:00
|
|
|
}
|
|
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
[Fact]
|
|
|
|
|
public async Task SubscribeAsyncRemoveSubscriptionAsyncAndGetSubscriptionsTest()
|
2025-01-24 19:24:00 -08:00
|
|
|
{
|
2025-02-05 11:34:02 -05:00
|
|
|
var fixture = new GrpcAgentRuntimeFixture();
|
2025-02-11 16:03:02 -05:00
|
|
|
var runtime = (GrpcAgentRuntime)await fixture.StartAsync();
|
2025-02-05 11:34:02 -05:00
|
|
|
ReceiverAgent? agent = null;
|
|
|
|
|
await runtime.RegisterAgentFactoryAsync("MyAgent", async (id, runtime) =>
|
|
|
|
|
{
|
|
|
|
|
agent = new ReceiverAgent(id, runtime);
|
|
|
|
|
return await ValueTask.FromResult(agent);
|
|
|
|
|
});
|
2025-01-24 19:24:00 -08:00
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
Assert.Null(agent);
|
|
|
|
|
await runtime.GetAgentAsync("MyAgent", lazy: false);
|
|
|
|
|
Assert.NotNull(agent);
|
|
|
|
|
Assert.True(agent.ReceivedItems.Count == 0);
|
2025-01-24 19:24:00 -08:00
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
var topicTypeName = "TestTopic";
|
|
|
|
|
await runtime.PublishMessageAsync(new TextMessage { Source = "topic", Content = "test" }, new TopicId(topicTypeName));
|
|
|
|
|
await Task.Delay(100);
|
2025-01-24 19:24:00 -08:00
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
Assert.True(agent.ReceivedItems.Count == 0);
|
2025-01-24 19:24:00 -08:00
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
var subscription = new TypeSubscription(topicTypeName, "MyAgent");
|
|
|
|
|
await runtime.AddSubscriptionAsync(subscription);
|
2025-01-24 19:24:00 -08:00
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
await runtime.PublishMessageAsync(new TextMessage { Source = "topic", Content = "test" }, new TopicId(topicTypeName));
|
|
|
|
|
await Task.Delay(100);
|
2025-01-24 19:24:00 -08:00
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
Assert.True(agent.ReceivedItems.Count == 1);
|
|
|
|
|
Assert.Equal("test", agent.ReceivedItems[0]);
|
2025-01-24 19:24:00 -08:00
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
await runtime.RemoveSubscriptionAsync(subscription.Id);
|
|
|
|
|
await runtime.PublishMessageAsync(new TextMessage { Source = "topic", Content = "test" }, new TopicId(topicTypeName));
|
|
|
|
|
await Task.Delay(100);
|
2025-01-24 19:24:00 -08:00
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
Assert.True(agent.ReceivedItems.Count == 1);
|
|
|
|
|
fixture.Dispose();
|
2025-01-24 19:24:00 -08:00
|
|
|
}
|
|
|
|
|
}
|