Files
agent-framework/dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/Extensions/A2AArtifactExtensionsTests.cs
Ren Finlayson 539852f81c
Some checks failed
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
dotnet-build-and-test / paths-filter (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test (Debug, windows-latest, net9.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test (Release, integration, true, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test (Release, integration, true, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test (Release, ubuntu-latest, net8.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test-check (push) Has been cancelled
Python - Merge - Tests / paths-filter (push) Has been cancelled
Python - Merge - Tests / Python Tests - Core (integration, ubuntu-latest, 3.10) (push) Has been cancelled
Python - Merge - Tests / Python Tests - Azure AI (integration, ubuntu-latest, 3.10) (push) Has been cancelled
Python - Merge - Tests / python-integration-tests-check (push) Has been cancelled
Python - Lab Tests / paths-filter (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.10) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.11) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.12) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.13) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (ubuntu-latest, 3.14) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.10) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.11) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.12) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.13) (push) Has been cancelled
Python - Lab Tests / Python Lab Tests (windows-latest, 3.14) (push) Has been cancelled
Check .md links / markdown-link-check (push) Has been cancelled
test
2026-01-24 03:05:12 +11:00

108 lines
3.4 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Generic;
using System.Text.Json;
using A2A;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI.A2A.UnitTests;
/// <summary>
/// Unit tests for the <see cref="A2AArtifactExtensions"/> class.
/// </summary>
public sealed class A2AArtifactExtensionsTests
{
[Fact]
public void ToChatMessage_WithMultiplePartsMetadataAndRawRepresentation_ReturnsCorrectChatMessage()
{
// Arrange
var artifact = new Artifact
{
ArtifactId = "artifact-comprehensive",
Name = "comprehensive-artifact",
Parts =
[
new TextPart { Text = "First part" },
new TextPart { Text = "Second part" },
new TextPart { Text = "Third part" }
],
Metadata = new Dictionary<string, JsonElement>
{
{ "key1", JsonSerializer.SerializeToElement("value1") },
{ "key2", JsonSerializer.SerializeToElement(42) }
}
};
// Act
var result = artifact.ToChatMessage();
// Assert - Verify multiple parts
Assert.NotNull(result);
Assert.Equal(ChatRole.Assistant, result.Role);
Assert.Equal(3, result.Contents.Count);
Assert.All(result.Contents, content => Assert.IsType<TextContent>(content));
Assert.Equal("First part", ((TextContent)result.Contents[0]).Text);
Assert.Equal("Second part", ((TextContent)result.Contents[1]).Text);
Assert.Equal("Third part", ((TextContent)result.Contents[2]).Text);
// Assert - Verify metadata conversion to AdditionalProperties
Assert.NotNull(result.AdditionalProperties);
Assert.Equal(2, result.AdditionalProperties.Count);
Assert.True(result.AdditionalProperties.ContainsKey("key1"));
Assert.True(result.AdditionalProperties.ContainsKey("key2"));
// Assert - Verify RawRepresentation is set to artifact
Assert.NotNull(result.RawRepresentation);
Assert.Same(artifact, result.RawRepresentation);
}
[Fact]
public void ToAIContents_WithMultipleParts_ReturnsCorrectList()
{
// Arrange
var artifact = new Artifact
{
ArtifactId = "artifact-ai-multi",
Name = "test",
Parts =
[
new TextPart { Text = "Part 1" },
new TextPart { Text = "Part 2" },
new TextPart { Text = "Part 3" }
],
Metadata = null
};
// Act
var result = artifact.ToAIContents();
// Assert
Assert.NotNull(result);
Assert.Equal(3, result.Count);
Assert.All(result, content => Assert.IsType<TextContent>(content));
Assert.Equal("Part 1", ((TextContent)result[0]).Text);
Assert.Equal("Part 2", ((TextContent)result[1]).Text);
Assert.Equal("Part 3", ((TextContent)result[2]).Text);
}
[Fact]
public void ToAIContents_WithEmptyParts_ReturnsEmptyList()
{
// Arrange
var artifact = new Artifact
{
ArtifactId = "artifact-empty",
Name = "test",
Parts = [],
Metadata = null
};
// Act
var result = artifact.ToAIContents();
// Assert
Assert.NotNull(result);
Assert.Empty(result);
}
}