// Copyright (c) Microsoft. All rights reserved.
using System.Net.Http;
using Microsoft.Agents.AI.CopilotStudio;
using Microsoft.Agents.CopilotStudio.Client;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
namespace Microsoft.Agents.AI.UnitTests;
///
/// Unit tests for the class.
///
public class CopilotStudioAgentTests
{
private static CopilotClient CreateTestCopilotClient()
{
// Create mock dependencies for CopilotClient
var mockSettings = new Mock();
var mockHttpClientFactory = new Mock();
var mockHttpClient = new Mock();
mockHttpClientFactory.Setup(f => f.CreateClient(It.IsAny())).Returns(mockHttpClient.Object);
return new CopilotClient(mockSettings.Object, mockHttpClientFactory.Object, NullLogger.Instance, "test-client");
}
#region GetService Method Tests
///
/// Verify that GetService returns CopilotClient when requested.
///
[Fact]
public void GetService_RequestingCopilotClient_ReturnsCopilotClient()
{
// Arrange
var client = CreateTestCopilotClient();
var agent = new CopilotStudioAgent(client, NullLoggerFactory.Instance);
// Act
var result = agent.GetService(typeof(CopilotClient));
// Assert
Assert.NotNull(result);
Assert.Same(client, result);
}
///
/// Verify that GetService returns AIAgentMetadata when requested.
///
[Fact]
public void GetService_RequestingAIAgentMetadata_ReturnsMetadata()
{
// Arrange
var client = CreateTestCopilotClient();
var agent = new CopilotStudioAgent(client, NullLoggerFactory.Instance);
// Act
var result = agent.GetService(typeof(AIAgentMetadata));
// Assert
Assert.NotNull(result);
Assert.IsType(result);
var metadata = (AIAgentMetadata)result;
Assert.Equal("copilot-studio", metadata.ProviderName);
}
///
/// Verify that GetService returns null for unknown service types.
///
[Fact]
public void GetService_RequestingUnknownServiceType_ReturnsNull()
{
// Arrange
var client = CreateTestCopilotClient();
var agent = new CopilotStudioAgent(client, NullLoggerFactory.Instance);
// Act
var result = agent.GetService(typeof(string));
// Assert
Assert.Null(result);
}
///
/// Verify that GetService with serviceKey parameter returns null for unknown service types.
///
[Fact]
public void GetService_WithServiceKey_ReturnsNull()
{
// Arrange
var client = CreateTestCopilotClient();
var agent = new CopilotStudioAgent(client, NullLoggerFactory.Instance);
// Act
var result = agent.GetService(typeof(string), "test-key");
// Assert
Assert.Null(result);
}
///
/// Verify that GetService calls base.GetService() first and returns the agent itself when requesting CopilotStudioAgent type.
///
[Fact]
public void GetService_RequestingCopilotStudioAgentType_ReturnsBaseImplementation()
{
// Arrange
var client = CreateTestCopilotClient();
var agent = new CopilotStudioAgent(client, NullLoggerFactory.Instance);
// Act
var result = agent.GetService(typeof(CopilotStudioAgent));
// Assert
Assert.NotNull(result);
Assert.Same(agent, result);
}
///
/// Verify that GetService calls base.GetService() first and returns the agent itself when requesting AIAgent type.
///
[Fact]
public void GetService_RequestingAIAgentType_ReturnsBaseImplementation()
{
// Arrange
var client = CreateTestCopilotClient();
var agent = new CopilotStudioAgent(client, NullLoggerFactory.Instance);
// Act
var result = agent.GetService(typeof(AIAgent));
// Assert
Assert.NotNull(result);
Assert.Same(agent, result);
}
///
/// Verify that GetService calls base.GetService() first but continues to derived logic when base returns null.
///
[Fact]
public void GetService_RequestingCopilotClientWithServiceKey_CallsBaseFirstThenDerivedLogic()
{
// Arrange
var client = CreateTestCopilotClient();
var agent = new CopilotStudioAgent(client, NullLoggerFactory.Instance);
// Act - Request CopilotClient with a service key (base.GetService will return null due to serviceKey)
var result = agent.GetService(typeof(CopilotClient), "some-key");
// Assert
Assert.NotNull(result);
Assert.Same(client, result);
}
///
/// Verify that GetService returns consistent AIAgentMetadata across multiple calls.
///
[Fact]
public void GetService_RequestingAIAgentMetadata_ReturnsConsistentMetadata()
{
// Arrange
var client = CreateTestCopilotClient();
var agent = new CopilotStudioAgent(client, NullLoggerFactory.Instance);
// Act
var result1 = agent.GetService(typeof(AIAgentMetadata));
var result2 = agent.GetService(typeof(AIAgentMetadata));
// Assert
Assert.NotNull(result1);
Assert.NotNull(result2);
Assert.Same(result1, result2); // Should return the same instance
Assert.IsType(result1);
var metadata = (AIAgentMetadata)result1;
Assert.Equal("copilot-studio", metadata.ProviderName);
}
#endregion
}