mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
dc2b109b50
* Upgrade to .NET 10 - Require .NET 10 SDK - Include net10.0 assets in all assemblies - Move net9.0-only targets to net10.0 - Update LangVersion to latest - Remove complicated distinctions between debug target TFMs and release target TFMs - Remove unnecessary package dependencies when built into netcoreapp - Clean up some ifdefs - Clean up some analyzer warnings * Fix CI
120 lines
3.4 KiB
C#
120 lines
3.4 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System;
|
|
using System.Text.Json;
|
|
|
|
namespace Microsoft.Agents.AI.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 serviceThreadWrapper = new ServiceIdAgentThread.ServiceIdAgentThreadState { ServiceThreadId = "service-id-456" };
|
|
var json = JsonSerializer.SerializeToElement(serviceThreadWrapper, TestJsonSerializerContext.Default.ServiceIdAgentThreadState);
|
|
|
|
// Act
|
|
var thread = new TestServiceIdAgentThread(json);
|
|
|
|
// Assert
|
|
Assert.Equal("service-id-456", thread.GetServiceThreadId());
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WithSerializedUndefinedId_SetsProperty()
|
|
{
|
|
// Arrange
|
|
var emptyObject = new EmptyObject();
|
|
var json = JsonSerializer.SerializeToElement(emptyObject, TestJsonSerializerContext.Default.EmptyObject);
|
|
|
|
// Act
|
|
var thread = new TestServiceIdAgentThread(json);
|
|
|
|
// Assert
|
|
Assert.Null(thread.GetServiceThreadId());
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WithInvalidJson_ThrowsArgumentException()
|
|
{
|
|
// Arrange
|
|
var invalidJson = JsonSerializer.SerializeToElement(42, TestJsonSerializerContext.Default.Int32);
|
|
|
|
// Act & Assert
|
|
Assert.Throws<ArgumentException>(() => new TestServiceIdAgentThread(invalidJson));
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region SerializeAsync Tests
|
|
|
|
[Fact]
|
|
public void Serialize_ReturnsCorrectJson_WhenServiceThreadIdIsSet()
|
|
{
|
|
// Arrange
|
|
var thread = new TestServiceIdAgentThread("service-id-789");
|
|
|
|
// Act
|
|
var json = thread.Serialize();
|
|
|
|
// Assert
|
|
Assert.Equal(JsonValueKind.Object, json.ValueKind);
|
|
Assert.True(json.TryGetProperty("serviceThreadId", out var idProperty));
|
|
Assert.Equal("service-id-789", idProperty.GetString());
|
|
}
|
|
|
|
[Fact]
|
|
public void Serialize_ReturnsUndefinedServiceThreadId_WhenNotSet()
|
|
{
|
|
// Arrange
|
|
var thread = new TestServiceIdAgentThread();
|
|
|
|
// Act
|
|
var json = thread.Serialize();
|
|
|
|
// 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;
|
|
}
|
|
|
|
// Helper class to represent empty objects
|
|
internal sealed class EmptyObject;
|
|
}
|