mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
ec82ed15d2
* Add a StateBag to AgentSession and pass Agent and AgentSession to AIContextProvider and ChatHistoryProviders * Remove statebag code from this branch, to get the refactoring out of the way first * Apply suggestion from @rogerbarreto Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com> * Apply suggestion from @westey-m * Apply suggestion from @westey-m --------- Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>
124 lines
4.6 KiB
C#
124 lines
4.6 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using AgentConformance.IntegrationTests.Support;
|
|
using Microsoft.Agents.AI;
|
|
using Microsoft.Extensions.AI;
|
|
|
|
namespace AgentConformance.IntegrationTests;
|
|
|
|
/// <summary>
|
|
/// Conformance tests for run methods on agents.
|
|
/// </summary>
|
|
/// <typeparam name="TAgentFixture">The type of test fixture used by the concrete test implementation.</typeparam>
|
|
/// <param name="createAgentFixture">Function to create the test fixture with.</param>
|
|
public abstract class RunTests<TAgentFixture>(Func<TAgentFixture> createAgentFixture) : AgentTests<TAgentFixture>(createAgentFixture)
|
|
where TAgentFixture : IAgentFixture
|
|
{
|
|
public virtual Func<Task<AgentRunOptions?>> AgentRunOptionsFactory { get; set; } = () => Task.FromResult(default(AgentRunOptions));
|
|
|
|
[RetryFact(Constants.RetryCount, Constants.RetryDelay)]
|
|
public virtual async Task RunWithNoMessageDoesNotFailAsync()
|
|
{
|
|
// Arrange
|
|
var agent = this.Fixture.Agent;
|
|
var session = await agent.CreateSessionAsync();
|
|
await using var cleanup = new SessionCleanup(session, this.Fixture);
|
|
|
|
// Act
|
|
var chatResponse = await agent.RunAsync(session);
|
|
|
|
// Assert
|
|
Assert.NotNull(chatResponse);
|
|
}
|
|
|
|
[RetryFact(Constants.RetryCount, Constants.RetryDelay)]
|
|
public virtual async Task RunWithStringReturnsExpectedResultAsync()
|
|
{
|
|
// Arrange
|
|
var agent = this.Fixture.Agent;
|
|
var session = await agent.CreateSessionAsync();
|
|
await using var cleanup = new SessionCleanup(session, this.Fixture);
|
|
|
|
// Act
|
|
var response = await agent.RunAsync("What is the capital of France.", session, await this.AgentRunOptionsFactory.Invoke());
|
|
|
|
// Assert
|
|
Assert.NotNull(response);
|
|
Assert.Single(response.Messages);
|
|
Assert.Contains("Paris", response.Text);
|
|
Assert.Equal(agent.Id, response.AgentId);
|
|
}
|
|
|
|
[RetryFact(Constants.RetryCount, Constants.RetryDelay)]
|
|
public virtual async Task RunWithChatMessageReturnsExpectedResultAsync()
|
|
{
|
|
// Arrange
|
|
var agent = this.Fixture.Agent;
|
|
var session = await agent.CreateSessionAsync();
|
|
await using var cleanup = new SessionCleanup(session, this.Fixture);
|
|
|
|
// Act
|
|
var response = await agent.RunAsync(new ChatMessage(ChatRole.User, "What is the capital of France."), session, await this.AgentRunOptionsFactory.Invoke());
|
|
|
|
// Assert
|
|
Assert.NotNull(response);
|
|
Assert.Single(response.Messages);
|
|
Assert.Contains("Paris", response.Text);
|
|
}
|
|
|
|
[RetryFact(Constants.RetryCount, Constants.RetryDelay)]
|
|
public virtual async Task RunWithChatMessagesReturnsExpectedResultAsync()
|
|
{
|
|
// Arrange
|
|
var agent = this.Fixture.Agent;
|
|
var session = await agent.CreateSessionAsync();
|
|
await using var cleanup = new SessionCleanup(session, this.Fixture);
|
|
|
|
// Act
|
|
var response = await agent.RunAsync(
|
|
[
|
|
new ChatMessage(ChatRole.User, "Hello."),
|
|
new ChatMessage(ChatRole.User, "What is the capital of France.")
|
|
],
|
|
session,
|
|
await this.AgentRunOptionsFactory.Invoke());
|
|
|
|
// Assert
|
|
Assert.NotNull(response);
|
|
Assert.Single(response.Messages);
|
|
Assert.Contains("Paris", response.Text);
|
|
}
|
|
|
|
[RetryFact(Constants.RetryCount, Constants.RetryDelay)]
|
|
public virtual async Task SessionMaintainsHistoryAsync()
|
|
{
|
|
// Arrange
|
|
const string Q1 = "What is the capital of France.";
|
|
const string Q2 = "And Austria?";
|
|
var agent = this.Fixture.Agent;
|
|
var session = await agent.CreateSessionAsync();
|
|
await using var cleanup = new SessionCleanup(session, this.Fixture);
|
|
|
|
// Act
|
|
var options = await this.AgentRunOptionsFactory.Invoke();
|
|
var result1 = await agent.RunAsync(Q1, session, options);
|
|
var result2 = await agent.RunAsync(Q2, session, options);
|
|
|
|
// Assert
|
|
Assert.Contains("Paris", result1.Text);
|
|
Assert.Contains("Vienna", result2.Text);
|
|
|
|
var chatHistory = await this.Fixture.GetChatHistoryAsync(agent, session);
|
|
Assert.Equal(4, chatHistory.Count);
|
|
Assert.Equal(2, chatHistory.Count(x => x.Role == ChatRole.User));
|
|
Assert.Equal(2, chatHistory.Count(x => x.Role == ChatRole.Assistant));
|
|
Assert.Equal(Q1, chatHistory[0].Text);
|
|
Assert.Contains("Paris", chatHistory[1].Text);
|
|
Assert.Equal(Q2, chatHistory[2].Text);
|
|
Assert.Contains("Vienna", chatHistory[3].Text);
|
|
}
|
|
}
|