Files
agent-framework/dotnet/tests/Microsoft.Extensions.AI.Agents.Abstractions.UnitTests/ServiceIdAgentThreadTests.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

116 lines
3.2 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Text.Json;
using System.Threading.Tasks;
namespace Microsoft.Extensions.AI.Agents.Abstractions.UnitTests;
/// <summary>
/// Tests for <see cref="ServiceIdAgentThread"/>.
/// </summary>
public class ServiceIdAgentThreadTests
{
#region Constructor and Property Tests
[Fact]
public void Constructor_SetsDefaults()
{
// Arrange & Act
var thread = new TestServiceIdAgentThread();
// Assert
Assert.Null(thread.GetServiceThreadId());
}
[Fact]
public void Constructor_WithServiceThreadId_SetsProperty()
{
// Arrange & Act
var thread = new TestServiceIdAgentThread("service-id-123");
// Assert
Assert.Equal("service-id-123", thread.GetServiceThreadId());
}
[Fact]
public void Constructor_WithSerializedId_SetsProperty()
{
// Arrange
var json = JsonSerializer.SerializeToElement(new { ServiceThreadId = "service-id-456" });
// Act
var thread = new TestServiceIdAgentThread(json);
// Assert
Assert.Equal("service-id-456", thread.GetServiceThreadId());
}
[Fact]
public void Constructor_WithSerializedUndefinedId_SetsProperty()
{
// Arrange
var json = JsonSerializer.SerializeToElement(new { });
// Act
var thread = new TestServiceIdAgentThread(json);
// Assert
Assert.Null(thread.GetServiceThreadId());
}
[Fact]
public void Constructor_WithInvalidJson_ThrowsArgumentException()
{
// Arrange
var invalidJson = JsonSerializer.SerializeToElement(42);
// Act & Assert
Assert.Throws<ArgumentException>(() => new TestServiceIdAgentThread(invalidJson));
}
#endregion
#region SerializeAsync Tests
[Fact]
public async Task SerializeAsync_ReturnsCorrectJson_WhenServiceThreadIdIsSetAsync()
{
// Arrange
var thread = new TestServiceIdAgentThread("service-id-789");
// Act
var json = await thread.SerializeAsync();
// Assert
Assert.Equal(JsonValueKind.Object, json.ValueKind);
Assert.True(json.TryGetProperty("serviceThreadId", out var idProperty));
Assert.Equal("service-id-789", idProperty.GetString());
}
[Fact]
public async Task SerializeAsync_ReturnsUndefinedServiceThreadId_WhenNotSetAsync()
{
// Arrange
var thread = new TestServiceIdAgentThread();
// Act
var json = await thread.SerializeAsync();
// Assert
Assert.Equal(JsonValueKind.Object, json.ValueKind);
Assert.False(json.TryGetProperty("serviceThreadId", out _));
}
#endregion
// Sealed test subclass to expose protected members for testing
private sealed class TestServiceIdAgentThread : ServiceIdAgentThread
{
public TestServiceIdAgentThread() { }
public TestServiceIdAgentThread(string serviceThreadId) : base(serviceThreadId) { }
public TestServiceIdAgentThread(JsonElement serializedThreadState) : base(serializedThreadState) { }
public string? GetServiceThreadId() => this.ServiceThreadId;
}
}