Skip to content

Commit fca1de9

Browse files
authored
feat: dotnet AgentId and MetaData tests (#5271)
1 parent f656ff1 commit fca1de9

3 files changed

Lines changed: 143 additions & 0 deletions

File tree

dotnet/src/Microsoft.AutoGen/Contracts/AgentId.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Diagnostics;
55
using System.Diagnostics.CodeAnalysis;
6+
using System.Text.RegularExpressions;
67

78
namespace Microsoft.AutoGen.Contracts;
89

@@ -16,6 +17,9 @@ namespace Microsoft.AutoGen.Contracts;
1617
[DebuggerDisplay($"AgentId(type=\"{nameof(Type)}\", key=\"{nameof(Key)}\")")]
1718
public struct AgentId
1819
{
20+
private static readonly Regex TypeRegex = new(@"^[a-zA-Z_][a-zA-Z0-9_]*$", RegexOptions.Compiled);
21+
private static readonly Regex KeyRegex = new(@"^[\x20-\x7E]+$", RegexOptions.Compiled); // ASCII 32-126
22+
1923
/// <summary>
2024
/// An identifier that associates an agent with a specific factory function.
2125
/// Strings may only be composed of alphanumeric letters (a-z) and (0-9), or underscores (_).
@@ -35,6 +39,16 @@ public struct AgentId
3539
/// <param name="key">Agent instance identifier.</param>
3640
public AgentId(string type, string key)
3741
{
42+
if (string.IsNullOrWhiteSpace(type) || !TypeRegex.IsMatch(type))
43+
{
44+
throw new ArgumentException($"Invalid AgentId type: '{type}'. Must be alphanumeric (a-z, 0-9, _) and cannot start with a number or contain spaces.");
45+
}
46+
47+
if (string.IsNullOrWhiteSpace(key) || !KeyRegex.IsMatch(key))
48+
{
49+
throw new ArgumentException($"Invalid AgentId key: '{key}'. Must only contain ASCII characters 32-126.");
50+
}
51+
3852
Type = type;
3953
Key = key;
4054
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// AgentIdTests.cs
3+
using FluentAssertions;
4+
using Microsoft.AutoGen.Contracts;
5+
using Xunit;
6+
7+
namespace Microsoft.AutoGen.Core.Tests;
8+
9+
public class AgentIdTests()
10+
{
11+
[Fact]
12+
public void AgentIdShouldInitializeCorrectlyTest()
13+
{
14+
var agentId = new AgentId("TestType", "TestKey");
15+
16+
agentId.Type.Should().Be("TestType");
17+
agentId.Key.Should().Be("TestKey");
18+
}
19+
20+
[Fact]
21+
public void AgentIdShouldConvertFromTupleTest()
22+
{
23+
var agentTuple = ("TupleType", "TupleKey");
24+
var agentId = new AgentId(agentTuple);
25+
26+
agentId.Type.Should().Be("TupleType");
27+
agentId.Key.Should().Be("TupleKey");
28+
}
29+
30+
[Fact]
31+
public void AgentIdShouldParseFromStringTest()
32+
{
33+
var agentId = AgentId.FromStr("ParsedType/ParsedKey");
34+
35+
agentId.Type.Should().Be("ParsedType");
36+
agentId.Key.Should().Be("ParsedKey");
37+
}
38+
39+
[Fact]
40+
public void AgentIdShouldCompareEqualityCorrectlyTest()
41+
{
42+
var agentId1 = new AgentId("SameType", "SameKey");
43+
var agentId2 = new AgentId("SameType", "SameKey");
44+
var agentId3 = new AgentId("DifferentType", "DifferentKey");
45+
46+
agentId1.Should().Be(agentId2);
47+
agentId1.Should().NotBe(agentId3);
48+
(agentId1 == agentId2).Should().BeTrue();
49+
(agentId1 != agentId3).Should().BeTrue();
50+
}
51+
52+
[Fact]
53+
public void AgentIdShouldGenerateCorrectHashCodeTest()
54+
{
55+
var agentId1 = new AgentId("HashType", "HashKey");
56+
var agentId2 = new AgentId("HashType", "HashKey");
57+
var agentId3 = new AgentId("DifferentType", "DifferentKey");
58+
59+
agentId1.GetHashCode().Should().Be(agentId2.GetHashCode());
60+
agentId1.GetHashCode().Should().NotBe(agentId3.GetHashCode());
61+
}
62+
63+
[Fact]
64+
public void AgentIdShouldConvertExplicitlyFromStringTest()
65+
{
66+
var agentId = (AgentId)"ConvertedType/ConvertedKey";
67+
68+
agentId.Type.Should().Be("ConvertedType");
69+
agentId.Key.Should().Be("ConvertedKey");
70+
}
71+
72+
[Fact]
73+
public void AgentIdShouldReturnCorrectToStringTest()
74+
{
75+
var agentId = new AgentId("ToStringType", "ToStringKey");
76+
77+
agentId.ToString().Should().Be("ToStringType/ToStringKey");
78+
}
79+
80+
[Fact]
81+
public void AgentIdShouldCompareInequalityCorrectlyTest()
82+
{
83+
var agentId1 = new AgentId("Type1", "Key1");
84+
var agentId2 = new AgentId("Type2", "Key2");
85+
86+
(agentId1 != agentId2).Should().BeTrue();
87+
}
88+
89+
[Fact]
90+
public void AgentIdShouldRejectInvalidNamesTest()
91+
{
92+
// Invalid: 'Type' cannot start with a number and must only contain a-z, 0-9, or underscores.
93+
Action invalidType = () => new AgentId("123InvalidType", "ValidKey");
94+
invalidType.Should().Throw<ArgumentException>("Agent type cannot start with a number and must only contain alphanumeric letters or underscores.");
95+
96+
Action invalidTypeWithSpaces = () => new AgentId("Invalid Type", "ValidKey");
97+
invalidTypeWithSpaces.Should().Throw<ArgumentException>("Agent type cannot contain spaces.");
98+
99+
Action invalidTypeWithSpecialChars = () => new AgentId("Invalid@Type", "ValidKey");
100+
invalidTypeWithSpecialChars.Should().Throw<ArgumentException>("Agent type cannot contain special characters.");
101+
102+
// Invalid: 'Key' must contain only ASCII characters 32 (space) to 126 (~).
103+
Action invalidKey = () => new AgentId("ValidType", "InvalidKey💀");
104+
invalidKey.Should().Throw<ArgumentException>("Agent key must only contain ASCII characters between 32 (space) and 126 (~).");
105+
106+
Action validCase = () => new AgentId("Valid_Type", "Valid_Key_123");
107+
validCase.Should().NotThrow("This is a correctly formatted AgentId.");
108+
}
109+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// AgentMetaDataTests.cs
3+
using FluentAssertions;
4+
using Microsoft.AutoGen.Contracts;
5+
using Xunit;
6+
7+
namespace Microsoft.AutoGen.Core.Tests;
8+
9+
public class AgentMetadataTests()
10+
{
11+
[Fact]
12+
public void AgentMetadataShouldInitializeCorrectlyTest()
13+
{
14+
var metadata = new AgentMetadata("TestType", "TestKey", "TestDescription");
15+
16+
metadata.Type.Should().Be("TestType");
17+
metadata.Key.Should().Be("TestKey");
18+
metadata.Description.Should().Be("TestDescription");
19+
}
20+
}

0 commit comments

Comments
 (0)