File: Contents\McpServerToolResultContentTests.cs
Project: ..\..\..\test\Libraries\Microsoft.Extensions.AI.Abstractions.Tests\Microsoft.Extensions.AI.Abstractions.Tests.csproj (Microsoft.Extensions.AI.Abstractions.Tests)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using System;
using System.Collections.Generic;
using System.Text.Json;
using Xunit;
 
namespace Microsoft.Extensions.AI;
 
public class McpServerToolResultContentTests
{
    [Fact]
    public void Constructor_PropsDefault()
    {
        McpServerToolResultContent c = new("callId");
        Assert.Equal("callId", c.CallId);
        Assert.Null(c.RawRepresentation);
        Assert.Null(c.AdditionalProperties);
        Assert.Null(c.Outputs);
    }
 
    [Fact]
    public void Constructor_PropsRoundtrip()
    {
        McpServerToolResultContent c = new("callId");
 
        Assert.Null(c.RawRepresentation);
        object raw = new();
        c.RawRepresentation = raw;
        Assert.Same(raw, c.RawRepresentation);
 
        Assert.Null(c.AdditionalProperties);
        AdditionalPropertiesDictionary props = new() { { "key", "value" } };
        c.AdditionalProperties = props;
        Assert.Same(props, c.AdditionalProperties);
 
        Assert.Equal("callId", c.CallId);
 
        Assert.Null(c.Outputs);
        IList<AIContent> outputs = [new TextContent("test result")];
        c.Outputs = outputs;
        Assert.Same(outputs, c.Outputs);
    }
 
    [Fact]
    public void Constructor_Throws()
    {
        Assert.Throws<ArgumentException>("callId", () => new McpServerToolResultContent(string.Empty));
        Assert.Throws<ArgumentNullException>("callId", () => new McpServerToolResultContent(null!));
    }
 
    [Fact]
    public void Serialization_Roundtrips()
    {
        var content = new McpServerToolResultContent("call123")
        {
            Outputs = [new TextContent("result")]
        };
 
        AssertSerializationRoundtrips<McpServerToolResultContent>(content);
        AssertSerializationRoundtrips<ToolResultContent>(content);
        AssertSerializationRoundtrips<AIContent>(content);
 
        static void AssertSerializationRoundtrips<T>(McpServerToolResultContent content)
            where T : AIContent
        {
            T contentAsT = (T)(object)content;
            string json = JsonSerializer.Serialize(contentAsT, AIJsonUtilities.DefaultOptions);
            T? deserialized = JsonSerializer.Deserialize<T>(json, AIJsonUtilities.DefaultOptions);
            Assert.NotNull(deserialized);
            var deserializedContent = Assert.IsType<McpServerToolResultContent>(deserialized);
            Assert.Equal(content.CallId, deserializedContent.CallId);
            Assert.NotNull(deserializedContent.Outputs);
            Assert.Equal("result", Assert.IsType<TextContent>(Assert.Single(deserializedContent.Outputs)).Text);
        }
    }
 
    [Fact]
    public void JsonDeserialization_KnownPayload()
    {
        const string Json = """
            {
              "$type": "mcpServerToolResult",
              "callId": "call1",
              "outputs": [
                {
                  "$type": "text",
                  "text": "output text"
                }
              ],
              "additionalProperties": {
                "key": "val"
              }
            }
            """;
 
        AIContent? result = JsonSerializer.Deserialize<AIContent>(Json, AIJsonUtilities.DefaultOptions);
 
        Assert.NotNull(result);
        var mcpResult = Assert.IsType<McpServerToolResultContent>(result);
        Assert.Equal("call1", mcpResult.CallId);
        Assert.NotNull(mcpResult.Outputs);
        Assert.Single(mcpResult.Outputs);
        var textOutput = Assert.IsType<TextContent>(mcpResult.Outputs[0]);
        Assert.Equal("output text", textOutput.Text);
        Assert.NotNull(mcpResult.AdditionalProperties);
        Assert.Equal("val", mcpResult.AdditionalProperties["key"]?.ToString());
    }
}