Some checks failed
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
dotnet-build-and-test / paths-filter (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test (Debug, windows-latest, net9.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test (Release, integration, true, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test (Release, integration, true, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test (Release, ubuntu-latest, net8.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test-check (push) Has been cancelled
Python - Merge - Tests / paths-filter (push) Has been cancelled
Python - Merge - Tests / Python Tests - Core (integration, ubuntu-latest, 3.10) (push) Has been cancelled
Python - Merge - Tests / Python Tests - Azure AI (integration, ubuntu-latest, 3.10) (push) Has been cancelled
Python - Merge - Tests / python-integration-tests-check (push) Has been cancelled
Python - Lab Tests / paths-filter (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.10) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.11) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.12) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.13) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.14) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.10) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.11) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.12) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.13) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.14) (push) Has been cancelled
Check .md links / markdown-link-check (push) Has been cancelled
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;
|
|
}
|