You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

AgentIdTests.cs 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // AgentIdTests.cs
  3. using FluentAssertions;
  4. using Microsoft.AutoGen.Contracts;
  5. using Xunit;
  6. namespace Microsoft.AutoGen.Core.Tests;
  7. [Trait("Category", "UnitV2")]
  8. public class AgentIdTests()
  9. {
  10. [Fact]
  11. public void AgentIdShouldInitializeCorrectlyTest()
  12. {
  13. var agentId = new AgentId("TestType", "TestKey");
  14. agentId.Type.Should().Be("TestType");
  15. agentId.Key.Should().Be("TestKey");
  16. }
  17. [Fact]
  18. public void AgentIdShouldConvertFromTupleTest()
  19. {
  20. var agentTuple = ("TupleType", "TupleKey");
  21. var agentId = new AgentId(agentTuple);
  22. agentId.Type.Should().Be("TupleType");
  23. agentId.Key.Should().Be("TupleKey");
  24. }
  25. [Fact]
  26. public void AgentIdShouldParseFromStringTest()
  27. {
  28. var agentId = AgentId.FromStr("ParsedType/ParsedKey");
  29. agentId.Type.Should().Be("ParsedType");
  30. agentId.Key.Should().Be("ParsedKey");
  31. }
  32. [Fact]
  33. public void AgentIdShouldCompareEqualityCorrectlyTest()
  34. {
  35. var agentId1 = new AgentId("SameType", "SameKey");
  36. var agentId2 = new AgentId("SameType", "SameKey");
  37. var agentId3 = new AgentId("DifferentType", "DifferentKey");
  38. agentId1.Should().Be(agentId2);
  39. agentId1.Should().NotBe(agentId3);
  40. (agentId1 == agentId2).Should().BeTrue();
  41. (agentId1 != agentId3).Should().BeTrue();
  42. }
  43. [Fact]
  44. public void AgentIdShouldGenerateCorrectHashCodeTest()
  45. {
  46. var agentId1 = new AgentId("HashType", "HashKey");
  47. var agentId2 = new AgentId("HashType", "HashKey");
  48. var agentId3 = new AgentId("DifferentType", "DifferentKey");
  49. agentId1.GetHashCode().Should().Be(agentId2.GetHashCode());
  50. agentId1.GetHashCode().Should().NotBe(agentId3.GetHashCode());
  51. }
  52. [Fact]
  53. public void AgentIdShouldConvertExplicitlyFromStringTest()
  54. {
  55. var agentId = (AgentId)"ConvertedType/ConvertedKey";
  56. agentId.Type.Should().Be("ConvertedType");
  57. agentId.Key.Should().Be("ConvertedKey");
  58. }
  59. [Fact]
  60. public void AgentIdShouldReturnCorrectToStringTest()
  61. {
  62. var agentId = new AgentId("ToStringType", "ToStringKey");
  63. agentId.ToString().Should().Be("ToStringType/ToStringKey");
  64. }
  65. [Fact]
  66. public void AgentIdShouldCompareInequalityCorrectlyTest()
  67. {
  68. var agentId1 = new AgentId("Type1", "Key1");
  69. var agentId2 = new AgentId("Type2", "Key2");
  70. (agentId1 != agentId2).Should().BeTrue();
  71. }
  72. [Fact]
  73. public void AgentIdShouldRejectInvalidNamesTest()
  74. {
  75. // Invalid: 'Type' cannot start with a number and must only contain a-z, 0-9, or underscores.
  76. Action invalidType = () => new AgentId("123InvalidType", "ValidKey");
  77. invalidType.Should().Throw<ArgumentException>("Agent type cannot start with a number and must only contain alphanumeric letters or underscores.");
  78. Action invalidTypeWithSpaces = () => new AgentId("Invalid Type", "ValidKey");
  79. invalidTypeWithSpaces.Should().Throw<ArgumentException>("Agent type cannot contain spaces.");
  80. Action invalidTypeWithSpecialChars = () => new AgentId("Invalid@Type", "ValidKey");
  81. invalidTypeWithSpecialChars.Should().Throw<ArgumentException>("Agent type cannot contain special characters.");
  82. // Invalid: 'Key' must contain only ASCII characters 32 (space) to 126 (~).
  83. Action invalidKey = () => new AgentId("ValidType", "InvalidKey💀");
  84. invalidKey.Should().Throw<ArgumentException>("Agent key must only contain ASCII characters between 32 (space) and 126 (~).");
  85. Action validCase = () => new AgentId("Valid_Type", "Valid_Key_123");
  86. validCase.Should().NotThrow("This is a correctly formatted AgentId.");
  87. }
  88. }