Files
agent-framework/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentFeatureCollectionTests.cs
T
westey eff5aee5aa .NET: Add per run / thread feature collection support and improved custom ChatMessageStore support (#2345)
* Add the ability to override services on an agent per run.

* Remove Run from AgentFeatureCollection name.

* Adding features param to GetNewThread.

* Move feature collection.

* Add features to DeserializeThread

* Remove servicecollection based option

* Add feature collection unit tests and fix bug identified in code review.

* Add more unit tests for DelegatingAIAgent and AgentRunOptions

* Fix formatting.

* Address PR comments.

* Switch to dedicated ConversationIdAgentFeature and improve 3rd party storage samples.

* Fix bug in sample.
2025-11-20 18:33:14 +00:00

120 lines
2.9 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
namespace Microsoft.Agents.AI.Abstractions.UnitTests;
/// <summary>
/// Contains unit tests for the <see cref="AgentFeatureCollection"/> class.
/// </summary>
public class AgentFeatureCollectionTests
{
[Fact]
public void AddedInterfaceIsReturned()
{
var interfaces = new AgentFeatureCollection();
var thing = new Thing();
interfaces[typeof(IThing)] = thing;
var thing2 = interfaces[typeof(IThing)];
Assert.Equal(thing2, thing);
}
[Fact]
public void IndexerAlsoAddsItems()
{
var interfaces = new AgentFeatureCollection();
var thing = new Thing();
interfaces[typeof(IThing)] = thing;
Assert.Equal(interfaces[typeof(IThing)], thing);
}
[Fact]
public void SetNullValueRemoves()
{
var interfaces = new AgentFeatureCollection();
var thing = new Thing();
interfaces[typeof(IThing)] = thing;
Assert.Equal(interfaces[typeof(IThing)], thing);
interfaces[typeof(IThing)] = null;
var thing2 = interfaces[typeof(IThing)];
Assert.Null(thing2);
}
[Fact]
public void GetMissingStructFeatureThrows()
{
var interfaces = new AgentFeatureCollection();
var ex = Assert.Throws<InvalidOperationException>(() => interfaces.Get<int>());
Assert.Equal("System.Int32 does not exist in the feature collection and because it is a struct the method can't return null. Use 'AgentFeatureCollection[typeof(System.Int32)] is not null' to check if the feature exists.", ex.Message);
}
[Fact]
public void GetMissingFeatureReturnsNull()
{
var interfaces = new AgentFeatureCollection();
Assert.Null(interfaces.Get<Thing>());
}
[Fact]
public void GetStructFeature()
{
var interfaces = new AgentFeatureCollection();
const int Value = 20;
interfaces.Set(Value);
Assert.Equal(Value, interfaces.Get<int>());
}
[Fact]
public void GetNullableStructFeatureWhenSetWithNonNullableStruct()
{
var interfaces = new AgentFeatureCollection();
const int Value = 20;
interfaces.Set(Value);
Assert.Null(interfaces.Get<int?>());
}
[Fact]
public void GetNullableStructFeatureWhenSetWithNullableStruct()
{
var interfaces = new AgentFeatureCollection();
const int Value = 20;
interfaces.Set<int?>(Value);
Assert.Equal(Value, interfaces.Get<int?>());
}
[Fact]
public void GetFeature()
{
var interfaces = new AgentFeatureCollection();
var thing = new Thing();
interfaces.Set(thing);
Assert.Equal(thing, interfaces.Get<Thing>());
}
private interface IThing
{
string Hello();
}
private sealed class Thing : IThing
{
public string Hello()
{
return "World";
}
}
}