test
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

This commit is contained in:
2026-01-24 03:05:12 +11:00
parent f78f2388b3
commit 539852f81c
2584 changed files with 287471 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,30 @@
// Copyright (c) Microsoft. All rights reserved.
using System.Text.Json;
namespace Microsoft.Agents.AI.A2A.UnitTests;
/// <summary>
/// Unit tests for the <see cref="A2AAgentThread"/> class.
/// </summary>
public sealed class A2AAgentThreadTests
{
[Fact]
public void Constructor_RoundTrip_SerializationPreservesState()
{
// Arrange
const string ContextId = "context-rt-001";
const string TaskId = "task-rt-002";
A2AAgentThread originalThread = new() { ContextId = ContextId, TaskId = TaskId };
// Act
JsonElement serialized = originalThread.Serialize();
A2AAgentThread deserializedThread = new(serialized);
// Assert
Assert.Equal(originalThread.ContextId, deserializedThread.ContextId);
Assert.Equal(originalThread.TaskId, deserializedThread.TaskId);
}
}

View File

@@ -0,0 +1,152 @@
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Text.Json;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI.A2A.UnitTests;
/// <summary>
/// Unit tests for the <see cref="A2AContinuationToken"/> class.
/// </summary>
public sealed class A2AContinuationTokenTests
{
[Fact]
public void Constructor_WithValidTaskId_InitializesTaskIdProperty()
{
// Arrange
const string TaskId = "task-123";
// Act
var token = new A2AContinuationToken(TaskId);
// Assert
Assert.Equal(TaskId, token.TaskId);
}
[Fact]
public void ToBytes_WithValidToken_SerializesToJsonBytes()
{
// Arrange
const string TaskId = "task-456";
var token = new A2AContinuationToken(TaskId);
// Act
var bytes = token.ToBytes();
// Assert
Assert.NotEqual(0, bytes.Length);
var jsonString = System.Text.Encoding.UTF8.GetString(bytes.ToArray());
using var jsonDoc = JsonDocument.Parse(jsonString);
var root = jsonDoc.RootElement;
Assert.True(root.TryGetProperty("taskId", out var taskIdElement));
Assert.Equal(TaskId, taskIdElement.GetString());
}
[Fact]
public void FromToken_WithA2AContinuationToken_ReturnsSameInstance()
{
// Arrange
const string TaskId = "task-direct";
var originalToken = new A2AContinuationToken(TaskId);
// Act
var resultToken = A2AContinuationToken.FromToken(originalToken);
// Assert
Assert.Same(originalToken, resultToken);
Assert.Equal(TaskId, resultToken.TaskId);
}
[Fact]
public void FromToken_WithSerializedToken_DeserializesCorrectly()
{
// Arrange
const string TaskId = "task-deserialized";
var originalToken = new A2AContinuationToken(TaskId);
var serialized = originalToken.ToBytes();
// Create a mock token wrapper to pass to FromToken
var mockToken = new MockResponseContinuationToken(serialized);
// Act
var resultToken = A2AContinuationToken.FromToken(mockToken);
// Assert
Assert.Equal(TaskId, resultToken.TaskId);
Assert.IsType<A2AContinuationToken>(resultToken);
}
[Fact]
public void FromToken_RoundTrip_PreservesTaskId()
{
// Arrange
const string TaskId = "task-roundtrip-123";
var originalToken = new A2AContinuationToken(TaskId);
var serialized = originalToken.ToBytes();
var mockToken = new MockResponseContinuationToken(serialized);
// Act
var deserializedToken = A2AContinuationToken.FromToken(mockToken);
var reserialized = deserializedToken.ToBytes();
var mockToken2 = new MockResponseContinuationToken(reserialized);
var deserializedAgain = A2AContinuationToken.FromToken(mockToken2);
// Assert
Assert.Equal(TaskId, deserializedAgain.TaskId);
}
[Fact]
public void FromToken_WithEmptyData_ThrowsArgumentException()
{
// Arrange
var emptyToken = new MockResponseContinuationToken(ReadOnlyMemory<byte>.Empty);
// Act & Assert
Assert.Throws<ArgumentException>(() => A2AContinuationToken.FromToken(emptyToken));
}
[Fact]
public void FromToken_WithMissingTaskIdProperty_ThrowsException()
{
// Arrange
var jsonWithoutTaskId = System.Text.Encoding.UTF8.GetBytes("{ \"someOtherProperty\": \"value\" }").AsMemory();
var mockToken = new MockResponseContinuationToken(jsonWithoutTaskId);
// Act & Assert
Assert.Throws<JsonException>(() => A2AContinuationToken.FromToken(mockToken));
}
[Fact]
public void FromToken_WithValidTaskId_ParsesTaskIdCorrectly()
{
// Arrange
const string TaskId = "task-multi-prop";
var json = System.Text.Encoding.UTF8.GetBytes($"{{ \"taskId\": \"{TaskId}\" }}").AsMemory();
var mockToken = new MockResponseContinuationToken(json);
// Act
var resultToken = A2AContinuationToken.FromToken(mockToken);
// Assert
Assert.Equal(TaskId, resultToken.TaskId);
}
/// <summary>
/// Mock implementation of ResponseContinuationToken for testing.
/// </summary>
private sealed class MockResponseContinuationToken : ResponseContinuationToken
{
private readonly ReadOnlyMemory<byte> _data;
public MockResponseContinuationToken(ReadOnlyMemory<byte> data)
{
this._data = data;
}
public override ReadOnlyMemory<byte> ToBytes()
{
return this._data;
}
}
}

View File

@@ -0,0 +1,87 @@
// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Generic;
using A2A;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI.A2A.UnitTests;
/// <summary>
/// Unit tests for the <see cref="A2AAIContentExtensions"/> class.
/// </summary>
public sealed class A2AAIContentExtensionsTests
{
[Fact]
public void ToA2AParts_WithEmptyCollection_ReturnsNull()
{
// Arrange
var emptyContents = new List<AIContent>();
// Act
var result = emptyContents.ToParts();
// Assert
Assert.Null(result);
}
[Fact]
public void ToA2AParts_WithMultipleContents_ReturnsListWithAllParts()
{
// Arrange
var contents = new List<AIContent>
{
new TextContent("First text"),
new UriContent("https://example.com/file1.txt", "file/txt"),
new TextContent("Second text"),
};
// Act
var result = contents.ToParts();
// Assert
Assert.NotNull(result);
Assert.Equal(3, result.Count);
var firstTextPart = Assert.IsType<TextPart>(result[0]);
Assert.Equal("First text", firstTextPart.Text);
var filePart = Assert.IsType<FilePart>(result[1]);
Assert.Equal("https://example.com/file1.txt", filePart.File.Uri?.ToString());
var secondTextPart = Assert.IsType<TextPart>(result[2]);
Assert.Equal("Second text", secondTextPart.Text);
}
[Fact]
public void ToA2AParts_WithMixedSupportedAndUnsupportedContent_IgnoresUnsupportedContent()
{
// Arrange
var contents = new List<AIContent>
{
new TextContent("First text"),
new MockAIContent(), // Unsupported - should be ignored
new UriContent("https://example.com/file.txt", "file/txt"),
new MockAIContent(), // Unsupported - should be ignored
new TextContent("Second text")
};
// Act
var result = contents.ToParts();
// Assert
Assert.NotNull(result);
Assert.Equal(3, result.Count);
var firstTextPart = Assert.IsType<TextPart>(result[0]);
Assert.Equal("First text", firstTextPart.Text);
var filePart = Assert.IsType<FilePart>(result[1]);
Assert.Equal("https://example.com/file.txt", filePart.File.Uri?.ToString());
var secondTextPart = Assert.IsType<TextPart>(result[2]);
Assert.Equal("Second text", secondTextPart.Text);
}
// Mock class for testing unsupported scenarios
private sealed class MockAIContent : AIContent;
}

View File

@@ -0,0 +1,108 @@
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using A2A;
namespace Microsoft.Agents.AI.A2A.UnitTests;
/// <summary>
/// Unit tests for the <see cref="A2AAgentCardExtensions"/> class.
/// </summary>
public sealed class A2AAgentCardExtensionsTests
{
private readonly AgentCard _agentCard;
public A2AAgentCardExtensionsTests()
{
this._agentCard = new AgentCard
{
Name = "Test Agent",
Description = "A test agent for unit testing",
Url = "http://test-endpoint/agent"
};
}
[Fact]
public void GetAIAgent_ReturnsAIAgent()
{
// Act
var agent = this._agentCard.AsAIAgent();
// Assert
Assert.NotNull(agent);
Assert.IsType<A2AAgent>(agent);
Assert.Equal("Test Agent", agent.Name);
Assert.Equal("A test agent for unit testing", agent.Description);
}
[Fact]
public async Task RunIAgentAsync_SendsRequestToTheUrlSpecifiedInAgentCardAsync()
{
// Arrange
using var handler = new HttpMessageHandlerStub();
using var httpClient = new HttpClient(handler, false);
handler.ResponsesToReturn.Enqueue(new AgentMessage
{
Role = MessageRole.Agent,
Parts = [new TextPart { Text = "Response" }],
});
var agent = this._agentCard.AsAIAgent(httpClient);
// Act
await agent.RunAsync("Test input");
// Assert
Assert.Single(handler.CapturedUris);
Assert.Equal(new Uri("http://test-endpoint/agent"), handler.CapturedUris[0]);
}
internal sealed class HttpMessageHandlerStub : HttpMessageHandler
{
public Queue ResponsesToReturn { get; } = new();
public List<Uri> CapturedUris { get; } = [];
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
this.CapturedUris.Add(request.RequestUri!);
var response = this.ResponsesToReturn.Dequeue();
if (response is AgentCard agentCard)
{
var json = JsonSerializer.Serialize(agentCard);
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(json, Encoding.UTF8, "application/json")
};
}
else if (response is AgentMessage message)
{
var jsonRpcResponse = JsonRpcResponse.CreateJsonRpcResponse<A2AEvent>("response-id", message);
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(JsonSerializer.Serialize(jsonRpcResponse), Encoding.UTF8, "application/json")
};
}
// Return empty agent card if none specified
var emptyCard = new AgentCard();
var emptyJson = JsonSerializer.Serialize(emptyCard);
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(emptyJson, Encoding.UTF8, "application/json")
};
}
}
}

View File

@@ -0,0 +1,169 @@
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using A2A;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI.A2A.UnitTests;
/// <summary>
/// Unit tests for the <see cref="A2AAgentTaskExtensions"/> class.
/// </summary>
public sealed class A2AAgentTaskExtensionsTests
{
[Fact]
public void ToChatMessages_WithNullAgentTask_ThrowsArgumentNullException()
{
// Arrange
AgentTask agentTask = null!;
// Act & Assert
Assert.Throws<ArgumentNullException>(() => agentTask.ToChatMessages());
}
[Fact]
public void ToAIContents_WithNullAgentTask_ThrowsArgumentNullException()
{
// Arrange
AgentTask agentTask = null!;
// Act & Assert
Assert.Throws<ArgumentNullException>(() => agentTask.ToAIContents());
}
[Fact]
public void ToChatMessages_WithEmptyArtifactsAndNoUserInputRequests_ReturnsNull()
{
// Arrange
var agentTask = new AgentTask
{
Id = "task1",
Artifacts = [],
Status = new AgentTaskStatus { State = TaskState.Completed },
};
// Act
IList<ChatMessage>? result = agentTask.ToChatMessages();
// Assert
Assert.Null(result);
}
[Fact]
public void ToChatMessages_WithNullArtifactsAndNoUserInputRequests_ReturnsNull()
{
// Arrange
var agentTask = new AgentTask
{
Id = "task1",
Artifacts = null,
Status = new AgentTaskStatus { State = TaskState.Completed },
};
// Act
IList<ChatMessage>? result = agentTask.ToChatMessages();
// Assert
Assert.Null(result);
}
[Fact]
public void ToAIContents_WithEmptyArtifactsAndNoUserInputRequests_ReturnsNull()
{
// Arrange
var agentTask = new AgentTask
{
Id = "task1",
Artifacts = [],
Status = new AgentTaskStatus { State = TaskState.Completed },
};
// Act
IList<AIContent>? result = agentTask.ToAIContents();
// Assert
Assert.Null(result);
}
[Fact]
public void ToAIContents_WithNullArtifactsAndNoUserInputRequests_ReturnsNull()
{
// Arrange
var agentTask = new AgentTask
{
Id = "task1",
Artifacts = null,
Status = new AgentTaskStatus { State = TaskState.Completed },
};
// Act
IList<AIContent>? result = agentTask.ToAIContents();
// Assert
Assert.Null(result);
}
[Fact]
public void ToChatMessages_WithValidArtifact_ReturnsChatMessages()
{
// Arrange
var artifact = new Artifact
{
Parts = [new TextPart { Text = "response" }],
};
var agentTask = new AgentTask
{
Id = "task1",
Artifacts = [artifact],
Status = new AgentTaskStatus { State = TaskState.Completed },
};
// Act
IList<ChatMessage>? result = agentTask.ToChatMessages();
// Assert
Assert.NotNull(result);
Assert.NotEmpty(result);
Assert.All(result, msg => Assert.Equal(ChatRole.Assistant, msg.Role));
Assert.Equal("response", result[0].Contents[0].ToString());
}
[Fact]
public void ToAIContents_WithMultipleArtifacts_FlattenAllContents()
{
// Arrange
var artifact1 = new Artifact
{
Parts = [new TextPart { Text = "content1" }],
};
var artifact2 = new Artifact
{
Parts =
[
new TextPart { Text = "content2" },
new TextPart { Text = "content3" }
],
};
var agentTask = new AgentTask
{
Id = "task1",
Artifacts = [artifact1, artifact2],
Status = new AgentTaskStatus { State = TaskState.Completed },
};
// Act
IList<AIContent>? result = agentTask.ToAIContents();
// Assert
Assert.NotNull(result);
Assert.NotEmpty(result);
Assert.Equal(3, result.Count);
Assert.Equal("content1", result[0].ToString());
Assert.Equal("content2", result[1].ToString());
Assert.Equal("content3", result[2].ToString());
}
}

View File

@@ -0,0 +1,107 @@
// 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);
}
}

View File

@@ -0,0 +1,126 @@
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using A2A;
namespace Microsoft.Agents.AI.A2A.UnitTests;
/// <summary>
/// Unit tests for the <see cref="A2ACardResolverExtensions"/> class.
/// </summary>
public sealed class A2ACardResolverExtensionsTests : IDisposable
{
private readonly HttpClient _httpClient;
private readonly HttpMessageHandlerStub _handler;
private readonly A2ACardResolver _resolver;
public A2ACardResolverExtensionsTests()
{
this._handler = new HttpMessageHandlerStub();
this._httpClient = new HttpClient(this._handler, false);
this._resolver = new A2ACardResolver(new Uri("http://test-host"), httpClient: this._httpClient);
}
[Fact]
public async Task GetAIAgentAsync_WithValidAgentCard_ReturnsAIAgentAsync()
{
// Arrange
this._handler.ResponsesToReturn.Enqueue(new AgentCard
{
Name = "Test Agent",
Description = "A test agent for unit testing",
Url = "http://test-endpoint/agent"
});
// Act
var agent = await this._resolver.GetAIAgentAsync();
// Assert
Assert.NotNull(agent);
Assert.IsType<A2AAgent>(agent);
Assert.Equal("Test Agent", agent.Name);
Assert.Equal("A test agent for unit testing", agent.Description);
// Verify that there was only one request made to retrieve the agent card
Assert.Single(this._handler.CapturedUris);
Assert.StartsWith("http://test-host/", this._handler.CapturedUris[0].ToString());
}
[Fact]
public async Task RunIAgentAsync_WithUrlFromAgentCard_SendsRequestToTheUrlAsync()
{
// Arrange
this._handler.ResponsesToReturn.Enqueue(new AgentCard
{
Url = "http://test-endpoint/agent"
});
this._handler.ResponsesToReturn.Enqueue(new AgentMessage
{
Role = MessageRole.Agent,
Parts = [new TextPart { Text = "Response" }],
});
var agent = await this._resolver.GetAIAgentAsync(this._httpClient);
// Act
await agent.RunAsync("Test input");
// Assert
Assert.Equal(2, this._handler.CapturedUris.Count); // One for getting the card, one for sending the message to the agent
Assert.Equal(new Uri("http://test-endpoint/agent"), this._handler.CapturedUris[1]);
}
public void Dispose()
{
this._handler.Dispose();
this._httpClient.Dispose();
}
internal sealed class HttpMessageHandlerStub : HttpMessageHandler
{
public Queue ResponsesToReturn { get; } = new();
public List<Uri> CapturedUris { get; } = [];
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
this.CapturedUris.Add(request.RequestUri!);
var response = this.ResponsesToReturn.Dequeue();
if (response is AgentCard agentCard)
{
var json = JsonSerializer.Serialize(agentCard);
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(json, Encoding.UTF8, "application/json")
};
}
else if (response is AgentMessage message)
{
var jsonRpcResponse = JsonRpcResponse.CreateJsonRpcResponse<A2AEvent>("response-id", message);
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(JsonSerializer.Serialize(jsonRpcResponse), Encoding.UTF8, "application/json")
};
}
// Return empty agent card if none specified
var emptyCard = new AgentCard();
var emptyJson = JsonSerializer.Serialize(emptyCard);
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(emptyJson, Encoding.UTF8, "application/json")
};
}
}
}

View File

@@ -0,0 +1,33 @@
// Copyright (c) Microsoft. All rights reserved.
using System;
using A2A;
namespace Microsoft.Agents.AI.A2A.UnitTests;
/// <summary>
/// Unit tests for the A2AClientExtensions class.
/// </summary>
public sealed class A2AClientExtensionsTests
{
[Fact]
public void GetAIAgent_WithAllParameters_ReturnsA2AAgentWithSpecifiedProperties()
{
// Arrange
var a2aClient = new A2AClient(new Uri("http://test-endpoint"));
const string TestId = "test-agent-id";
const string TestName = "Test Agent";
const string TestDescription = "This is a test agent description";
// Act
var agent = a2aClient.AsAIAgent(TestId, TestName, TestDescription);
// Assert
Assert.NotNull(agent);
Assert.IsType<A2AAgent>(agent);
Assert.Equal(TestId, agent.Id);
Assert.Equal(TestName, agent.Name);
Assert.Equal(TestDescription, agent.Description);
}
}

View File

@@ -0,0 +1,67 @@
// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Generic;
using System.Text.Json;
using A2A;
namespace Microsoft.Agents.AI.A2A.UnitTests;
/// <summary>
/// Unit tests for the <see cref="A2AMetadataExtensions"/> class.
/// </summary>
public sealed class A2AMetadataExtensionsTests
{
[Fact]
public void ToAdditionalProperties_WithNullMetadata_ReturnsNull()
{
// Arrange
Dictionary<string, JsonElement>? metadata = null;
// Act
var result = metadata.ToAdditionalProperties();
// Assert
Assert.Null(result);
}
[Fact]
public void ToAdditionalProperties_WithEmptyMetadata_ReturnsNull()
{
// Arrange
var metadata = new Dictionary<string, JsonElement>();
// Act
var result = metadata.ToAdditionalProperties();
// Assert
Assert.Null(result);
}
[Fact]
public void ToAdditionalProperties_WithMultipleProperties_ReturnsAdditionalPropertiesDictionaryWithAllProperties()
{
// Arrange
var metadata = new Dictionary<string, JsonElement>
{
{ "stringKey", JsonSerializer.SerializeToElement("stringValue") },
{ "numberKey", JsonSerializer.SerializeToElement(42) },
{ "booleanKey", JsonSerializer.SerializeToElement(true) }
};
// Act
var result = metadata.ToAdditionalProperties();
// Assert
Assert.NotNull(result);
Assert.Equal(3, result.Count);
Assert.True(result.ContainsKey("stringKey"));
Assert.Equal("stringValue", ((JsonElement)result["stringKey"]!).GetString());
Assert.True(result.ContainsKey("numberKey"));
Assert.Equal(42, ((JsonElement)result["numberKey"]!).GetInt32());
Assert.True(result.ContainsKey("booleanKey"));
Assert.True(((JsonElement)result["booleanKey"]!).GetBoolean());
}
}

View File

@@ -0,0 +1,186 @@
// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Generic;
using System.Text.Json;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI.A2A.UnitTests;
/// <summary>
/// Unit tests for the <see cref="AdditionalPropertiesDictionaryExtensions"/> class.
/// </summary>
public sealed class AdditionalPropertiesDictionaryExtensionsTests
{
[Fact]
public void ToA2AMetadata_WithNullAdditionalProperties_ReturnsNull()
{
// Arrange
AdditionalPropertiesDictionary? additionalProperties = null;
// Act
Dictionary<string, JsonElement>? result = additionalProperties.ToA2AMetadata();
// Assert
Assert.Null(result);
}
[Fact]
public void ToA2AMetadata_WithEmptyAdditionalProperties_ReturnsNull()
{
// Arrange
AdditionalPropertiesDictionary additionalProperties = [];
// Act
Dictionary<string, JsonElement>? result = additionalProperties.ToA2AMetadata();
// Assert
Assert.Null(result);
}
[Fact]
public void ToA2AMetadata_WithStringValue_ReturnsMetadataWithJsonElement()
{
// Arrange
AdditionalPropertiesDictionary additionalProperties = new()
{
{ "stringKey", "stringValue" }
};
// Act
Dictionary<string, JsonElement>? result = additionalProperties.ToA2AMetadata();
// Assert
Assert.NotNull(result);
Assert.Single(result);
Assert.True(result.ContainsKey("stringKey"));
Assert.Equal("stringValue", result["stringKey"].GetString());
}
[Fact]
public void ToA2AMetadata_WithNumericValue_ReturnsMetadataWithJsonElement()
{
// Arrange
AdditionalPropertiesDictionary additionalProperties = new()
{
{ "numberKey", 42 }
};
// Act
Dictionary<string, JsonElement>? result = additionalProperties.ToA2AMetadata();
// Assert
Assert.NotNull(result);
Assert.Single(result);
Assert.True(result.ContainsKey("numberKey"));
Assert.Equal(42, result["numberKey"].GetInt32());
}
[Fact]
public void ToA2AMetadata_WithBooleanValue_ReturnsMetadataWithJsonElement()
{
// Arrange
AdditionalPropertiesDictionary additionalProperties = new()
{
{ "booleanKey", true }
};
// Act
Dictionary<string, JsonElement>? result = additionalProperties.ToA2AMetadata();
// Assert
Assert.NotNull(result);
Assert.Single(result);
Assert.True(result.ContainsKey("booleanKey"));
Assert.True(result["booleanKey"].GetBoolean());
}
[Fact]
public void ToA2AMetadata_WithMultipleProperties_ReturnsMetadataWithAllProperties()
{
// Arrange
AdditionalPropertiesDictionary additionalProperties = new()
{
{ "stringKey", "stringValue" },
{ "numberKey", 42 },
{ "booleanKey", true }
};
// Act
Dictionary<string, JsonElement>? result = additionalProperties.ToA2AMetadata();
// Assert
Assert.NotNull(result);
Assert.Equal(3, result.Count);
Assert.True(result.ContainsKey("stringKey"));
Assert.Equal("stringValue", result["stringKey"].GetString());
Assert.True(result.ContainsKey("numberKey"));
Assert.Equal(42, result["numberKey"].GetInt32());
Assert.True(result.ContainsKey("booleanKey"));
Assert.True(result["booleanKey"].GetBoolean());
}
[Fact]
public void ToA2AMetadata_WithArrayValue_ReturnsMetadataWithJsonElement()
{
// Arrange
int[] arrayValue = [1, 2, 3];
AdditionalPropertiesDictionary additionalProperties = new()
{
{ "arrayKey", arrayValue }
};
// Act
Dictionary<string, JsonElement>? result = additionalProperties.ToA2AMetadata();
// Assert
Assert.NotNull(result);
Assert.Single(result);
Assert.True(result.ContainsKey("arrayKey"));
Assert.Equal(JsonValueKind.Array, result["arrayKey"].ValueKind);
Assert.Equal(3, result["arrayKey"].GetArrayLength());
}
[Fact]
public void ToA2AMetadata_WithNullValue_ReturnsMetadataWithNullJsonElement()
{
// Arrange
AdditionalPropertiesDictionary additionalProperties = new()
{
{ "nullKey", null! }
};
// Act
Dictionary<string, JsonElement>? result = additionalProperties.ToA2AMetadata();
// Assert
Assert.NotNull(result);
Assert.Single(result);
Assert.True(result.ContainsKey("nullKey"));
Assert.Equal(JsonValueKind.Null, result["nullKey"].ValueKind);
}
[Fact]
public void ToA2AMetadata_WithJsonElementValue_ReturnsMetadataWithJsonElement()
{
// Arrange
JsonElement jsonElement = JsonSerializer.SerializeToElement(new { name = "test", value = 123 });
AdditionalPropertiesDictionary additionalProperties = new()
{
{ "jsonElementKey", jsonElement }
};
// Act
Dictionary<string, JsonElement>? result = additionalProperties.ToA2AMetadata();
// Assert
Assert.NotNull(result);
Assert.Single(result);
Assert.True(result.ContainsKey("jsonElementKey"));
Assert.Equal(JsonValueKind.Object, result["jsonElementKey"].ValueKind);
Assert.Equal("test", result["jsonElementKey"].GetProperty("name").GetString());
Assert.Equal(123, result["jsonElementKey"].GetProperty("value").GetInt32());
}
}

View File

@@ -0,0 +1,89 @@
// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Generic;
using A2A;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI.A2A.UnitTests;
/// <summary>
/// Unit tests for the <see cref="ChatMessageExtensions"/> class.
/// </summary>
public sealed class ChatMessageExtensionsTests
{
[Fact]
public void ToA2AMessage_WithMessageContainingMultipleContents_AddsAllContentsAsParts()
{
// Arrange
var contents = new List<AIContent>
{
new UriContent("https://example.com/report.pdf", "file/pdf"),
new TextContent("please summarize the file content"),
new TextContent("and send it to me over email")
};
var chatMessage = new ChatMessage(ChatRole.User, contents);
var messages = new List<ChatMessage> { chatMessage };
// Act
var a2aMessage = messages.ToA2AMessage();
// Assert
Assert.NotNull(a2aMessage);
Assert.NotNull(a2aMessage.MessageId);
Assert.NotEmpty(a2aMessage.MessageId);
Assert.Equal(MessageRole.User, a2aMessage.Role);
Assert.NotNull(a2aMessage.Parts);
Assert.Equal(3, a2aMessage.Parts.Count);
var filePart = Assert.IsType<FilePart>(a2aMessage.Parts[0]);
Assert.NotNull(filePart.File);
Assert.Equal("https://example.com/report.pdf", filePart.File.Uri?.ToString());
var secondTextPart = Assert.IsType<TextPart>(a2aMessage.Parts[1]);
Assert.Equal("please summarize the file content", secondTextPart.Text);
var thirdTextPart = Assert.IsType<TextPart>(a2aMessage.Parts[2]);
Assert.Equal("and send it to me over email", thirdTextPart.Text);
}
[Fact]
public void ToA2AMessage_WithMixedMessages_AddsAllContentsAsParts()
{
// Arrange
var firstMessage = new ChatMessage(ChatRole.User, [
new UriContent("https://example.com/report.pdf", "file/pdf"),
]);
var secondMessage = new ChatMessage(ChatRole.User, [
new TextContent("please summarize the file content")
]);
var thirdMessage = new ChatMessage(ChatRole.User, [
new TextContent("and send it to me over email")
]);
var messages = new List<ChatMessage> { firstMessage, secondMessage, thirdMessage };
// Act
var a2aMessage = messages.ToA2AMessage();
// Assert
Assert.NotNull(a2aMessage);
Assert.NotNull(a2aMessage.MessageId);
Assert.NotEmpty(a2aMessage.MessageId);
Assert.Equal(MessageRole.User, a2aMessage.Role);
Assert.NotNull(a2aMessage.Parts);
Assert.Equal(3, a2aMessage.Parts.Count);
var filePart = Assert.IsType<FilePart>(a2aMessage.Parts[0]);
Assert.NotNull(filePart.File);
Assert.Equal("https://example.com/report.pdf", filePart.File.Uri?.ToString());
var secondTextPart = Assert.IsType<TextPart>(a2aMessage.Parts[1]);
Assert.Equal("please summarize the file content", secondTextPart.Text);
var thirdTextPart = Assert.IsType<TextPart>(a2aMessage.Parts[2]);
Assert.Equal("and send it to me over email", thirdTextPart.Text);
}
}

View File

@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.Agents.AI.A2A\Microsoft.Agents.AI.A2A.csproj" />
</ItemGroup>
</Project>