Files
agent-framework/dotnet/tests/Microsoft.Extensions.AI.Agents.Abstractions.UnitTests/AgentThreadTests.cs
T
Stephen Toub 03ef7f054f .NET: Add AgentWorkflowBuilder group chat (#861)
* Add AgentWorkflowBuilder group chat

And fix a variety of issues along the way:
- Use DateTime{Offset}.UtcNow rather than Now
- AIAgentHostExecutor shouldn't be publishing empty messages
- Sequential workflows should be flowing all history and not just the output from the previous agent as the input into the next agent
- Renamed some of the new agent workflow methods... still not super happy with the shape, though
- Simplified handoffs builder, e.g. using a hashset with a custom comparer instead of a dictionary
- Improved multi-service use by trying to change assistant->user role for messages created by other agents
- Changed MessageMerger to rely on M.E.AI's coalescing more and to avoid empty contents / text
- Ensured that messages from ChatClientAgent include MessageId and CreatedAt timestamps
- Avoided including instructions for agents in a handoff workflow that don't have any handoffs
- Removed the unnecessary end function in handoffs
- Improved naming of executors to include agent name for debuggability
- Use "N" formatting with Guid.ToString everywhere, to avoid the unnecessary extra dash character which is also not valid in various places (like function tool names)
- Replace `params T[]` with `params IEnumerable<T>` to make public APIs more flexible in what they consume

* Address feedback

- Fix unintentional provider change in sample
2025-09-24 16:38:34 +00:00

151 lines
3.8 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
#pragma warning disable CA1861 // Avoid constant arrays as arguments
namespace Microsoft.Extensions.AI.Agents.Abstractions.UnitTests;
/// <summary>
/// Tests for <see cref="AgentThread"/>
/// </summary>
public class AgentThreadTests
{
[Fact]
public async Task SerializeAsync_ReturnsDefaultJsonElementAsync()
{
var thread = new TestAgentThread();
var result = await thread.SerializeAsync();
Assert.Equal(default, result);
}
[Fact]
public void MessagesReceivedAsync_ReturnsCompletedTask()
{
var thread = new TestAgentThread();
var messages = new List<ChatMessage> { new(ChatRole.User, "hello") };
var result = thread.MessagesReceivedAsync(messages);
Assert.True(result.IsCompleted);
}
#region GetService Method Tests
/// <summary>
/// Verify that GetService returns the thread itself when requesting the exact thread type.
/// </summary>
[Fact]
public void GetService_RequestingExactThreadType_ReturnsThread()
{
// Arrange
var thread = new TestAgentThread();
// Act
var result = thread.GetService(typeof(TestAgentThread));
// Assert
Assert.NotNull(result);
Assert.Same(thread, result);
}
/// <summary>
/// Verify that GetService returns the thread itself when requesting the base AgentThread type.
/// </summary>
[Fact]
public void GetService_RequestingAgentThreadType_ReturnsThread()
{
// Arrange
var thread = new TestAgentThread();
// Act
var result = thread.GetService(typeof(AgentThread));
// Assert
Assert.NotNull(result);
Assert.Same(thread, result);
}
/// <summary>
/// Verify that GetService returns null when requesting an unrelated type.
/// </summary>
[Fact]
public void GetService_RequestingUnrelatedType_ReturnsNull()
{
// Arrange
var thread = new TestAgentThread();
// Act
var result = thread.GetService(typeof(string));
// Assert
Assert.Null(result);
}
/// <summary>
/// Verify that GetService returns null when a service key is provided, even for matching types.
/// </summary>
[Fact]
public void GetService_WithServiceKey_ReturnsNull()
{
// Arrange
var thread = new TestAgentThread();
// Act
var result = thread.GetService(typeof(TestAgentThread), "some-key");
// Assert
Assert.Null(result);
}
/// <summary>
/// Verify that GetService throws ArgumentNullException when serviceType is null.
/// </summary>
[Fact]
public void GetService_WithNullServiceType_ThrowsArgumentNullException()
{
// Arrange
var thread = new TestAgentThread();
// Act & Assert
Assert.Throws<ArgumentNullException>(() => thread.GetService(null!));
}
/// <summary>
/// Verify that GetService generic method works correctly.
/// </summary>
[Fact]
public void GetService_Generic_ReturnsCorrectType()
{
// Arrange
var thread = new TestAgentThread();
// Act
var result = thread.GetService<TestAgentThread>();
// Assert
Assert.NotNull(result);
Assert.Same(thread, result);
}
/// <summary>
/// Verify that GetService generic method returns null for unrelated types.
/// </summary>
[Fact]
public void GetService_Generic_ReturnsNullForUnrelatedType()
{
// Arrange
var thread = new TestAgentThread();
// Act
var result = thread.GetService<string>();
// Assert
Assert.Null(result);
}
#endregion
private sealed class TestAgentThread : AgentThread;
}