|
| 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 | +} |
0 commit comments