mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
* Checkpoint * Checkpoint * Namespaces * Namespace * Cleanup * Namespace order * Fix sync * Formatting * Formatting * Namespace * Namespace order * Code convention * Naming * Naming * Text handling * Text handling * Namespace * Namespace order * Namespace ordering * Test * ValueTask * net472 * Test fix * Fix namespace (net472) * Namespace * Fix conditional namespace * Fix type expression * Compatibility and cleanup * Sample compatibility * Sample compat * Test compat * modifier order * Simply http-stub * Formating fix for unit-test * Fix test * Real fix * Test clean-up * Update dotnet/src/Microsoft.Agents.Orchestration/Handoff/HandoffOrchestration.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fix build errors after merging --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Stephen Toub <stoub@microsoft.com>
202 lines
5.2 KiB
C#
202 lines
5.2 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Agents.Orchestration.Transforms;
|
|
using Microsoft.Extensions.AI;
|
|
|
|
namespace Microsoft.Agents.Orchestration.UnitTest;
|
|
|
|
public class DefaultTransformsTests
|
|
{
|
|
[Fact]
|
|
public async Task FromInputAsyncWithEnumerableOfChatMessageReturnsInputAsync()
|
|
{
|
|
// Arrange
|
|
IEnumerable<ChatMessage> input =
|
|
[
|
|
new(ChatRole.User, "Hello"),
|
|
new(ChatRole.Assistant, "Hi there")
|
|
];
|
|
|
|
// Act
|
|
IEnumerable<ChatMessage> result = await DefaultTransforms.FromInput(input);
|
|
|
|
// Assert
|
|
Assert.Equal(input, result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task FromInputAsyncWithChatMessageReturnsInputAsListAsync()
|
|
{
|
|
// Arrange
|
|
ChatMessage input = new(ChatRole.User, "Hello");
|
|
|
|
// Act
|
|
IEnumerable<ChatMessage> result = await DefaultTransforms.FromInput(input);
|
|
|
|
// Assert
|
|
Assert.Single(result);
|
|
Assert.Equal(input, result.First());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task FromInputAsyncWithStringInputReturnsUserChatMessageAsync()
|
|
{
|
|
// Arrange
|
|
string input = "Hello, world!";
|
|
|
|
// Act
|
|
IEnumerable<ChatMessage> result = await DefaultTransforms.FromInput(input);
|
|
|
|
// Assert
|
|
Assert.Single(result);
|
|
ChatMessage message = result.First();
|
|
Assert.Equal(ChatRole.User, message.Role);
|
|
Assert.Equal(input, message.Text);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task FromInputAsyncWithObjectInputSerializesAsJsonAsync()
|
|
{
|
|
// Arrange
|
|
TestObject input = new() { Id = 1, Name = "Test" };
|
|
|
|
// Act
|
|
IEnumerable<ChatMessage> result = await DefaultTransforms.FromInput(input);
|
|
|
|
// Assert
|
|
Assert.Single(result);
|
|
ChatMessage message = result.First();
|
|
Assert.Equal(ChatRole.User, message.Role);
|
|
|
|
string expectedJson = JsonSerializer.Serialize(input);
|
|
Assert.Equal(expectedJson, message.Text);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ToOutputAsyncWithOutputTypeMatchingInputListReturnsSameListAsync()
|
|
{
|
|
// Arrange
|
|
IList<ChatMessage> input =
|
|
[
|
|
new(ChatRole.User, "Hello"),
|
|
new(ChatRole.Assistant, "Hi there")
|
|
];
|
|
|
|
// Act
|
|
IList<ChatMessage> result = await DefaultTransforms.ToOutput<IList<ChatMessage>>(input);
|
|
|
|
// Assert
|
|
Assert.Same(input, result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ToOutputAsyncWithOutputTypeChatMessageReturnsSingleMessageAsync()
|
|
{
|
|
// Arrange
|
|
IList<ChatMessage> input =
|
|
[
|
|
new(ChatRole.User, "Hello")
|
|
];
|
|
|
|
// Act
|
|
ChatMessage result = await DefaultTransforms.ToOutput<ChatMessage>(input);
|
|
|
|
// Assert
|
|
Assert.Same(input[0], result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ToOutputAsyncWithOutputTypeStringReturnsContentOfSingleMessageAsync()
|
|
{
|
|
// Arrange
|
|
string expected = "Hello, world!";
|
|
IList<ChatMessage> input =
|
|
[
|
|
new(ChatRole.User, expected)
|
|
];
|
|
|
|
// Act
|
|
string result = await DefaultTransforms.ToOutput<string>(input);
|
|
|
|
// Assert
|
|
Assert.Equal(expected, result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ToOutputAsyncWithOutputTypeDeserializableDeserializesFromContentAsync()
|
|
{
|
|
// Arrange
|
|
TestObject expected = new() { Id = 42, Name = "TestName" };
|
|
string json = JsonSerializer.Serialize(expected);
|
|
IList<ChatMessage> input =
|
|
[
|
|
new(ChatRole.User, json)
|
|
];
|
|
|
|
// Act
|
|
TestObject result = await DefaultTransforms.ToOutput<TestObject>(input);
|
|
|
|
// Assert
|
|
Assert.Equal(expected.Id, result.Id);
|
|
Assert.Equal(expected.Name, result.Name);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ToOutputAsyncWithInvalidJsonThrowsExceptionAsync()
|
|
{
|
|
// Arrange
|
|
IList<ChatMessage> input =
|
|
[
|
|
new(ChatRole.User, "Not valid JSON")
|
|
];
|
|
|
|
// Act & Assert
|
|
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
|
|
await DefaultTransforms.ToOutput<TestObject>(input)
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ToOutputAsyncWithMultipleMessagesAndNonMatchingTypeThrowsExceptionAsync()
|
|
{
|
|
// Arrange
|
|
IList<ChatMessage> input =
|
|
[
|
|
new(ChatRole.User, "Hello"),
|
|
new(ChatRole.Assistant, "Hi there")
|
|
];
|
|
|
|
// Act & Assert
|
|
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
|
|
await DefaultTransforms.ToOutput<TestObject>(input)
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ToOutputAsyncWithNullContentHandlesGracefullyAsync()
|
|
{
|
|
// Arrange
|
|
IList<ChatMessage> input =
|
|
[
|
|
new(ChatRole.User, (string?)null)
|
|
];
|
|
|
|
// Act
|
|
string result = await DefaultTransforms.ToOutput<string>(input);
|
|
|
|
// Assert
|
|
Assert.Equal(string.Empty, result);
|
|
}
|
|
|
|
private sealed class TestObject
|
|
{
|
|
public int Id { get; set; }
|
|
public string? Name { get; set; }
|
|
}
|
|
}
|