Files
agent-framework/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentSessionTests.cs
T
westey 6255abd687 .NET: [BREAKING] Move AgentSession.Serialize to AIAgent (#3650)
* Move AgentSession.Serialize to AIAgent

* Address PR comments.

* Improve code and fix unit test

* Update test agents to return a default json element instead of throwing where the the result of the serialization is never used.

* Update further tests to actually serialize the session
2026-02-04 15:52:15 +00:00

132 lines
3.3 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
#pragma warning disable CA1861 // Avoid constant arrays as arguments
namespace Microsoft.Agents.AI.Abstractions.UnitTests;
/// <summary>
/// Tests for <see cref="AgentSession"/>
/// </summary>
public class AgentSessionTests
{
#region GetService Method Tests
/// <summary>
/// Verify that GetService returns the session itself when requesting the exact session type.
/// </summary>
[Fact]
public void GetService_RequestingExactThreadType_ReturnsSession()
{
// Arrange
var session = new TestAgentSession();
// Act
var result = session.GetService(typeof(TestAgentSession));
// Assert
Assert.NotNull(result);
Assert.Same(session, result);
}
/// <summary>
/// Verify that GetService returns the session itself when requesting the base AgentSession type.
/// </summary>
[Fact]
public void GetService_RequestingAgentSessionType_ReturnsSession()
{
// Arrange
var session = new TestAgentSession();
// Act
var result = session.GetService(typeof(AgentSession));
// Assert
Assert.NotNull(result);
Assert.Same(session, result);
}
/// <summary>
/// Verify that GetService returns null when requesting an unrelated type.
/// </summary>
[Fact]
public void GetService_RequestingUnrelatedType_ReturnsNull()
{
// Arrange
var session = new TestAgentSession();
// Act
var result = session.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 session = new TestAgentSession();
// Act
var result = session.GetService(typeof(TestAgentSession), "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 session = new TestAgentSession();
// Act & Assert
Assert.Throws<ArgumentNullException>(() => session.GetService(null!));
}
/// <summary>
/// Verify that GetService generic method works correctly.
/// </summary>
[Fact]
public void GetService_Generic_ReturnsCorrectType()
{
// Arrange
var session = new TestAgentSession();
// Act
var result = session.GetService<TestAgentSession>();
// Assert
Assert.NotNull(result);
Assert.Same(session, result);
}
/// <summary>
/// Verify that GetService generic method returns null for unrelated types.
/// </summary>
[Fact]
public void GetService_Generic_ReturnsNullForUnrelatedType()
{
// Arrange
var session = new TestAgentSession();
// Act
var result = session.GetService<string>();
// Assert
Assert.Null(result);
}
#endregion
private sealed class TestAgentSession : AgentSession;
}