First round of cleanup of runtime abstractions (#156)

This commit is contained in:
Stephen Toub
2025-07-10 07:57:51 -04:00
committed by GitHub
parent a233d31813
commit fbf1f10a8a
76 changed files with 1254 additions and 2079 deletions
@@ -17,54 +17,34 @@ public class AgentIdTests()
public void AgentIdShouldThrowArgumentExceptionWithInvalidKey(string? invalidKey)
{
// Act & Assert
ArgumentException exception = Assert.Throws<ArgumentException>(() => new AgentId("validType", invalidKey!));
Assert.Contains("Invalid AgentId key", exception.Message);
ArgumentException exception = Assert.Throws<ArgumentException>(() => new ActorId("validType", invalidKey!));
Assert.Contains("Invalid ActorId key", exception.Message);
}
[Fact]
public void AgentIdShouldInitializeCorrectlyTest()
{
AgentId agentId = new("TestType", "TestKey");
ActorId agentId = new("TestType", "TestKey");
Assert.Equal("TestType", agentId.Type);
Assert.Equal("TestKey", agentId.Key);
}
[Fact]
public void AgentIdShouldConvertFromTupleTest()
{
(string, string) agentTuple = ("TupleType", "TupleKey");
AgentId agentId = new(agentTuple);
Assert.Equal("TupleType", agentId.Type);
Assert.Equal("TupleKey", agentId.Key);
}
[Fact]
public void AgentIdShouldConvertFromAgentType()
{
AgentType agentType = "TestType";
AgentId agentId = new(agentType, "TestKey");
Assert.Equal("TestType", agentId.Type);
Assert.Equal("TestType", agentId.Type.Name);
Assert.Equal("TestKey", agentId.Key);
}
[Fact]
public void AgentIdShouldParseFromStringTest()
{
AgentId agentId = AgentId.FromStr("ParsedType/ParsedKey");
ActorId agentId = ActorId.Parse("ParsedType/ParsedKey");
Assert.Equal("ParsedType", agentId.Type);
Assert.Equal("ParsedType", agentId.Type.Name);
Assert.Equal("ParsedKey", agentId.Key);
}
[Fact]
public void AgentIdShouldCompareEqualityCorrectlyTest()
{
AgentId agentId1 = new("SameType", "SameKey");
AgentId agentId2 = new("SameType", "SameKey");
AgentId agentId3 = new("DifferentType", "DifferentKey");
ActorId agentId1 = new("SameType", "SameKey");
ActorId agentId2 = new("SameType", "SameKey");
ActorId agentId3 = new("DifferentType", "DifferentKey");
Assert.Equal(agentId2, agentId1);
Assert.NotEqual(agentId3, agentId1);
@@ -75,27 +55,18 @@ public class AgentIdTests()
[Fact]
public void AgentIdShouldGenerateCorrectHashCodeTest()
{
AgentId agentId1 = new("HashType", "HashKey");
AgentId agentId2 = new("HashType", "HashKey");
AgentId agentId3 = new("DifferentType", "DifferentKey");
ActorId agentId1 = new("HashType", "HashKey");
ActorId agentId2 = new("HashType", "HashKey");
ActorId agentId3 = new("DifferentType", "DifferentKey");
Assert.Equal(agentId2.GetHashCode(), agentId1.GetHashCode());
Assert.NotEqual(agentId3.GetHashCode(), agentId1.GetHashCode());
}
[Fact]
public void AgentIdShouldConvertExplicitlyFromStringTest()
{
AgentId agentId = (AgentId)"ConvertedType/ConvertedKey";
Assert.Equal("ConvertedType", agentId.Type);
Assert.Equal("ConvertedKey", agentId.Key);
}
[Fact]
public void AgentIdShouldReturnCorrectToStringTest()
{
AgentId agentId = new("ToStringType", "ToStringKey");
ActorId agentId = new("ToStringType", "ToStringKey");
Assert.Equal("ToStringType/ToStringKey", agentId.ToString());
}
@@ -103,7 +74,7 @@ public class AgentIdTests()
[Fact]
public void AgentIdShouldCompareInequalityForWrongTypeTest()
{
AgentId agentId1 = new("Type1", "Key1");
ActorId agentId1 = new("Type1", "Key1");
Assert.False(agentId1.Equals(Guid.NewGuid()));
}
@@ -111,8 +82,8 @@ public class AgentIdTests()
[Fact]
public void AgentIdShouldCompareInequalityCorrectlyTest()
{
AgentId agentId1 = new("Type1", "Key1");
AgentId agentId2 = new("Type2", "Key2");
ActorId agentId1 = new("Type1", "Key1");
ActorId agentId2 = new("Type2", "Key2");
Assert.True(agentId1 != agentId2);
}
@@ -8,10 +8,10 @@ public class AgentMetadataTests()
public void AgentMetadataShouldInitializeCorrectlyTest()
{
// Arrange & Act
AgentMetadata metadata = new("TestType", "TestKey", "TestDescription");
ActorMetadata metadata = new(new ActorType("TestType"), "TestKey", "TestDescription");
// Assert
Assert.Equal("TestType", metadata.Type);
Assert.Equal("TestType", metadata.Type.Name);
Assert.Equal("TestKey", metadata.Key);
Assert.Equal("TestDescription", metadata.Description);
}
@@ -10,14 +10,14 @@ namespace Microsoft.Extensions.AI.Agents.Runtime.Abstractions.Tests;
public class AgentProxyTests
{
private readonly Mock<IAgentRuntime> _mockRuntime;
private readonly AgentId _agentId;
private readonly AgentProxy _agentProxy;
private readonly ActorId _agentId;
private readonly IdProxyActor _agentProxy;
public AgentProxyTests()
{
this._mockRuntime = new Mock<IAgentRuntime>();
this._agentId = new AgentId("testType", "testKey");
this._agentProxy = new AgentProxy(this._agentId, this._mockRuntime.Object);
this._agentId = new ActorId("testType", "testKey");
this._agentProxy = new IdProxyActor(this._mockRuntime.Object, this._agentId);
}
[Fact]
@@ -30,8 +30,8 @@ public class AgentProxyTests
[Fact]
public void MetadataShouldMatchAgentTest()
{
AgentMetadata expectedMetadata = new("testType", "testKey", "testDescription");
this._mockRuntime.Setup(r => r.GetAgentMetadataAsync(this._agentId))
ActorMetadata expectedMetadata = new(new("testType"), "testKey", "testDescription");
this._mockRuntime.Setup(r => r.GetActorMetadataAsync(this._agentId, default))
.ReturnsAsync(expectedMetadata);
Assert.Equal(expectedMetadata, this._agentProxy.Metadata);
@@ -42,7 +42,7 @@ public class AgentProxyTests
{
// Arrange
object message = new { Content = "Hello" };
AgentId sender = new("senderType", "senderKey");
ActorId sender = new("senderType", "senderKey");
object response = new { Content = "Response" };
this._mockRuntime.Setup(r => r.SendMessageAsync(message, this._agentId, sender, null, It.IsAny<CancellationToken>()))
@@ -61,14 +61,14 @@ public class AgentProxyTests
// Arrange
JsonElement state = JsonDocument.Parse("{\"key\":\"value\"}").RootElement;
this._mockRuntime.Setup(r => r.LoadAgentStateAsync(this._agentId, state))
this._mockRuntime.Setup(r => r.LoadActorStateAsync(this._agentId, state, default))
.Returns(default(ValueTask));
// Act
await this._agentProxy.LoadStateAsync(state);
// Assert
this._mockRuntime.Verify(r => r.LoadAgentStateAsync(this._agentId, state), Times.Once);
this._mockRuntime.Verify(r => r.LoadActorStateAsync(this._agentId, state, default), Times.Once);
}
[Fact]
@@ -77,7 +77,7 @@ public class AgentProxyTests
// Arrange
JsonElement expectedState = JsonDocument.Parse("{\"key\":\"value\"}").RootElement;
this._mockRuntime.Setup(r => r.SaveAgentStateAsync(this._agentId))
this._mockRuntime.Setup(r => r.SaveActorStateAsync(this._agentId, default))
.ReturnsAsync(expectedState);
// Act
@@ -17,46 +17,18 @@ public class AgentTypeTests
public void AgentIdShouldThrowArgumentExceptionWithInvalidType(string? invalidType)
{
// Act & Assert
ArgumentException exception = Assert.Throws<ArgumentException>(() => new AgentType(invalidType!));
Assert.Contains("Invalid AgentId type", exception.Message);
ArgumentException exception = Assert.Throws<ArgumentException>(() => new ActorType(invalidType!));
Assert.Contains("Invalid type", exception.Message);
}
[Fact]
public void ImplicitConversionFromStringTest()
public void ConversionToStringTest()
{
// Arrange
string agentTypeName = "TestAgent";
// Act
AgentType agentType = agentTypeName;
ActorType agentType = new("TestAgent");
// Assert
Assert.Equal(agentTypeName, agentType.Name);
}
[Fact]
public void ImplicitConversionToStringTest()
{
// Arrange
AgentType agentType = "TestAgent";
// Act
string agentTypeName = agentType;
// Assert
Assert.Equal("TestAgent", agentTypeName);
}
[Fact]
public void ExplicitConversionFromTypeTest()
{
// Arrange
Type type = typeof(string);
// Act
AgentType agentType = (AgentType)type;
// Assert
Assert.Equal(type.Name, agentType.Name);
Assert.Equal("TestAgent", agentType.Name);
Assert.Equal("TestAgent", agentType.ToString());
}
}
@@ -1,80 +1,34 @@
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Threading;
namespace Microsoft.Extensions.AI.Agents.Runtime.Abstractions.Tests;
public class MessageContextTests
{
[Fact]
public void ConstructWithMessageIdAndCancellationTokenTest()
public void Properties_Roundtrip()
{
// Arrange
string messageId = Guid.NewGuid().ToString();
CancellationToken cancellationToken = new();
MessageContext ctx = new();
// Act
MessageContext messageContext = new(messageId, cancellationToken);
string id = ctx.MessageId;
Assert.NotNull(id);
Assert.True(Guid.TryParse(id, out _));
ctx.MessageId = "newid";
Assert.Equal("newid", ctx.MessageId);
// Assert
Assert.Equal(messageId, messageContext.MessageId);
Assert.Equal(cancellationToken, messageContext.CancellationToken);
}
Assert.False(ctx.IsRpc);
ctx.IsRpc = true;
Assert.True(ctx.IsRpc);
[Fact]
public void ConstructWithCancellationTokenTest()
{
// Arrange
CancellationToken cancellationToken = new();
Assert.Null(ctx.Sender);
ActorId sender = new("type", "key");
ctx.Sender = sender;
Assert.Equal(sender, ctx.Sender);
// Act
MessageContext messageContext = new(cancellationToken);
// Assert
Assert.NotNull(messageContext.MessageId);
Assert.Equal(cancellationToken, messageContext.CancellationToken);
}
[Fact]
public void AssignSenderTest()
{
// Arrange
MessageContext messageContext = new(new CancellationToken());
AgentId sender = new("type", "key");
// Act
messageContext.Sender = sender;
// Assert
Assert.Equal(sender, messageContext.Sender);
}
[Fact]
public void AssignTopicTest()
{
// Arrange
MessageContext messageContext = new(new CancellationToken());
Assert.Null(ctx.Topic);
TopicId topic = new("type", "source");
// Act
messageContext.Topic = topic;
// Assert
Assert.Equal(topic, messageContext.Topic);
}
[Fact]
public void AssignIsRpcPropertyTest()
{
// Arrange
MessageContext messageContext = new(new CancellationToken())
{
// Act
IsRpc = true
};
// Assert
Assert.True(messageContext.IsRpc);
ctx.Topic = topic;
Assert.Equal(topic, ctx.Topic);
}
}
@@ -14,7 +14,6 @@ public class TopicIdTests
// Assert
Assert.Equal("testtype", topicId.Type);
Assert.Equal(TopicId.DefaultSource, topicId.Source);
}
[Fact]
@@ -28,42 +27,28 @@ public class TopicIdTests
Assert.Equal("customsource", topicId.Source);
}
[Fact]
public void ConstructWithTupleTest()
[Theory]
[InlineData("testtype/https://github.com/cloudevents", "testtype", "https://github.com/cloudevents")]
[InlineData("testtype/mailto:cncf-wg-serverless@lists.cncf.io", "testtype", "mailto:cncf-wg-serverless@lists.cncf.io")]
[InlineData("testtype/urn:uuid:6e8bc430-9c3a-11d9-9669-0800200c9a66", "testtype", "urn:uuid:6e8bc430-9c3a-11d9-9669-0800200c9a66")]
[InlineData("testtype//cloudevents/spec/pull/123", "testtype", "/cloudevents/spec/pull/123")]
[InlineData("testtype//sensors/tn-1234567/alerts", "testtype", "/sensors/tn-1234567/alerts")]
[InlineData("testtype/1-555-123-4567", "testtype", "1-555-123-4567")]
public void ParseTest(string input, string expectedType, string expectedSource)
{
// Arrange
(string, string) tuple = ("testtype", "customsource");
TopicId topicId = TopicId.Parse(input);
// Act
TopicId topicId = new(tuple);
// Assert
Assert.Equal("testtype", topicId.Type);
Assert.Equal("customsource", topicId.Source);
}
[Fact]
public void ConvertFromStringTest()
{
// Arrange
const string TopicIdStr = "testtype/customsource";
// Act
TopicId topicId = TopicId.FromStr(TopicIdStr);
// Assert
Assert.Equal("testtype", topicId.Type);
Assert.Equal("customsource", topicId.Source);
Assert.Equal(expectedType, topicId.Type);
Assert.Equal(expectedSource, topicId.Source);
}
[Theory]
[InlineData("invalid-format")]
[InlineData("too/many/parts")]
[InlineData("")]
public void InvalidFormatFromStringThrowsTest(string invalidInput)
public void InvalidFormatParseThrowsTest(string invalidInput)
{
// Act & Assert
Assert.Throws<FormatException>(() => TopicId.FromStr(invalidInput));
Assert.Throws<FormatException>(() => TopicId.Parse(invalidInput));
}
[Fact]
@@ -141,42 +126,4 @@ public class TopicIdTests
// Assert
Assert.Equal(hash1, hash2);
}
[Fact]
public void ExplicitConversionTest()
{
// Arrange
string topicIdStr = "testtype/customsource";
// Act
TopicId topicId = (TopicId)topicIdStr;
// Assert
Assert.Equal("testtype", topicId.Type);
Assert.Equal("customsource", topicId.Source);
}
[Fact]
public void IsWildcardMatchTest()
{
// Arrange
TopicId topicId1 = new("testtype", "source1");
TopicId topicId2 = new("testtype", "source2");
// Act & Assert
Assert.True(topicId1.IsWildcardMatch(topicId2));
Assert.True(topicId2.IsWildcardMatch(topicId1));
}
[Fact]
public void IsWildcardMismatchTest()
{
// Arrange
TopicId topicId1 = new("testtype1", "source");
TopicId topicId2 = new("testtype2", "source");
// Act & Assert
Assert.False(topicId1.IsWildcardMatch(topicId2));
Assert.False(topicId2.IsWildcardMatch(topicId1));
}
}