File: Contents\HostedVectorStoreContentTests.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.Text.Json;
using Xunit;
 
namespace Microsoft.Extensions.AI;
 
public class HostedVectorStoreContentTests
{
    [Fact]
    public void Constructor_InvalidInput_Throws()
    {
        Assert.Throws<ArgumentNullException>("vectorStoreId", () => new HostedVectorStoreContent(null!));
        Assert.Throws<ArgumentException>("vectorStoreId", () => new HostedVectorStoreContent(string.Empty));
        Assert.Throws<ArgumentException>("vectorStoreId", () => new HostedVectorStoreContent(" "));
    }
 
    [Fact]
    public void Constructor_String_PropsDefault()
    {
        HostedVectorStoreContent c = new("id123");
        Assert.Null(c.RawRepresentation);
        Assert.Null(c.AdditionalProperties);
        Assert.Equal("id123", c.VectorStoreId);
    }
 
    [Fact]
    public void Constructor_PropsRoundtrip()
    {
        HostedVectorStoreContent c = new("id123");
 
        Assert.Equal("id123", c.VectorStoreId);
        c.VectorStoreId = "id456";
        Assert.Equal("id456", c.VectorStoreId);
 
        Assert.Throws<ArgumentNullException>("value", () => c.VectorStoreId = null!);
        Assert.Throws<ArgumentException>("value", () => c.VectorStoreId = string.Empty);
        Assert.Throws<ArgumentException>("value", () => c.VectorStoreId = " ");
        Assert.Equal("id456", c.VectorStoreId);
 
        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);
    }
 
    [Fact]
    public void Serialization_Roundtrips()
    {
        var content = new HostedVectorStoreContent("vectorstore123");
 
        var json = JsonSerializer.Serialize(content, AIJsonUtilities.DefaultOptions);
        var deserializedContent = JsonSerializer.Deserialize<HostedVectorStoreContent>(json, AIJsonUtilities.DefaultOptions);
 
        Assert.NotNull(deserializedContent);
        Assert.Equal(content.VectorStoreId, deserializedContent.VectorStoreId);
    }
 
    [Fact]
    public void JsonDeserialization_KnownPayload()
    {
        const string Json = """
            {
              "$type": "hostedVectorStore",
              "vectorStoreId": "vs-abc123",
              "additionalProperties": {
                "key": "val"
              }
            }
            """;
 
        AIContent? result = JsonSerializer.Deserialize<AIContent>(Json, AIJsonUtilities.DefaultOptions);
 
        Assert.NotNull(result);
        var vectorStore = Assert.IsType<HostedVectorStoreContent>(result);
        Assert.Equal("vs-abc123", vectorStore.VectorStoreId);
        Assert.NotNull(vectorStore.AdditionalProperties);
        Assert.Equal("val", vectorStore.AdditionalProperties["key"]?.ToString());
    }
}