// Copyright (c) Microsoft. All rights reserved. using System; using System.Collections.Generic; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.AI; namespace Microsoft.Agents.AI.Abstractions.UnitTests; /// /// Contains tests for the class. /// public class ChatHistoryProviderTests { #region GetService Method Tests [Fact] public void GetService_RequestingExactProviderType_ReturnsProvider() { var provider = new TestChatHistoryProvider(); var result = provider.GetService(typeof(TestChatHistoryProvider)); Assert.NotNull(result); Assert.Same(provider, result); } [Fact] public void GetService_RequestingBaseProviderType_ReturnsProvider() { var provider = new TestChatHistoryProvider(); var result = provider.GetService(typeof(ChatHistoryProvider)); Assert.NotNull(result); Assert.Same(provider, result); } [Fact] public void GetService_RequestingUnrelatedType_ReturnsNull() { var provider = new TestChatHistoryProvider(); var result = provider.GetService(typeof(string)); Assert.Null(result); } [Fact] public void GetService_WithServiceKey_ReturnsNull() { var provider = new TestChatHistoryProvider(); var result = provider.GetService(typeof(TestChatHistoryProvider), "some-key"); Assert.Null(result); } [Fact] public void GetService_WithNullServiceType_ThrowsArgumentNullException() { var provider = new TestChatHistoryProvider(); Assert.Throws(() => provider.GetService(null!)); } [Fact] public void GetService_Generic_ReturnsCorrectType() { var provider = new TestChatHistoryProvider(); var result = provider.GetService(); Assert.NotNull(result); Assert.Same(provider, result); } [Fact] public void GetService_Generic_ReturnsNullForUnrelatedType() { var provider = new TestChatHistoryProvider(); var result = provider.GetService(); Assert.Null(result); } #endregion #region InvokingContext Tests [Fact] public void InvokingContext_Constructor_ThrowsForNullMessages() { // Arrange & Act & Assert Assert.Throws(() => new ChatHistoryProvider.InvokingContext(null!)); } [Fact] public void InvokingContext_RequestMessages_SetterThrowsForNull() { // Arrange var messages = new List { new(ChatRole.User, "Hello") }; var context = new ChatHistoryProvider.InvokingContext(messages); // Act & Assert Assert.Throws(() => context.RequestMessages = null!); } [Fact] public void InvokingContext_RequestMessages_SetterRoundtrips() { // Arrange var initialMessages = new List { new(ChatRole.User, "Hello") }; var newMessages = new List { new(ChatRole.User, "New message") }; var context = new ChatHistoryProvider.InvokingContext(initialMessages); // Act context.RequestMessages = newMessages; // Assert Assert.Same(newMessages, context.RequestMessages); } #endregion #region InvokedContext Tests [Fact] public void InvokedContext_Constructor_ThrowsForNullRequestMessages() { // Arrange & Act & Assert Assert.Throws(() => new ChatHistoryProvider.InvokedContext(null!, [])); } [Fact] public void InvokedContext_RequestMessages_SetterThrowsForNull() { // Arrange var requestMessages = new List { new(ChatRole.User, "Hello") }; var context = new ChatHistoryProvider.InvokedContext(requestMessages, []); // Act & Assert Assert.Throws(() => context.RequestMessages = null!); } [Fact] public void InvokedContext_RequestMessages_SetterRoundtrips() { // Arrange var initialMessages = new List { new(ChatRole.User, "Hello") }; var newMessages = new List { new(ChatRole.User, "New message") }; var context = new ChatHistoryProvider.InvokedContext(initialMessages, []); // Act context.RequestMessages = newMessages; // Assert Assert.Same(newMessages, context.RequestMessages); } [Fact] public void InvokedContext_ChatHistoryProviderMessages_SetterRoundtrips() { // Arrange var requestMessages = new List { new(ChatRole.User, "Hello") }; var newProviderMessages = new List { new(ChatRole.System, "System message") }; var context = new ChatHistoryProvider.InvokedContext(requestMessages, []); // Act context.ChatHistoryProviderMessages = newProviderMessages; // Assert Assert.Same(newProviderMessages, context.ChatHistoryProviderMessages); } [Fact] public void InvokedContext_AIContextProviderMessages_Roundtrips() { // Arrange var requestMessages = new List { new(ChatRole.User, "Hello") }; var aiContextMessages = new List { new(ChatRole.System, "AI context message") }; var context = new ChatHistoryProvider.InvokedContext(requestMessages, []); // Act context.AIContextProviderMessages = aiContextMessages; // Assert Assert.Same(aiContextMessages, context.AIContextProviderMessages); } [Fact] public void InvokedContext_ResponseMessages_Roundtrips() { // Arrange var requestMessages = new List { new(ChatRole.User, "Hello") }; var responseMessages = new List { new(ChatRole.Assistant, "Response message") }; var context = new ChatHistoryProvider.InvokedContext(requestMessages, []); // Act context.ResponseMessages = responseMessages; // Assert Assert.Same(responseMessages, context.ResponseMessages); } [Fact] public void InvokedContext_InvokeException_Roundtrips() { // Arrange var requestMessages = new List { new(ChatRole.User, "Hello") }; var exception = new InvalidOperationException("Test exception"); var context = new ChatHistoryProvider.InvokedContext(requestMessages, []); // Act context.InvokeException = exception; // Assert Assert.Same(exception, context.InvokeException); } #endregion private sealed class TestChatHistoryProvider : ChatHistoryProvider { public override ValueTask> InvokingAsync(InvokingContext context, CancellationToken cancellationToken = default) => new(Array.Empty()); public override ValueTask InvokedAsync(InvokedContext context, CancellationToken cancellationToken = default) => default; public override JsonElement Serialize(JsonSerializerOptions? jsonSerializerOptions = null) => default; } }