Files
westey 3ef67eff10 .NET: [BREAKING] Refactor ChatMessageStore methods to be similar to AIContextProvider and add filtering support (#2604)
* Refactor ChatMessageStore methods to be similar to AIContextProvider

* Fix file encoding

* Ensure that AIContextProvider messages area also persisted.

* Update formatting and seal context classes

* Improve formatting

* Remove optional messages from constructor and add unit test

* Add ChatMessageStore filtering via a decorator

* Update sample and cosmos message store to store AIContextProvider messages in right order. Fix unit tests.

* Update Workflowmessage store to use aicontext provider messages.

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Improve xml docs messaging

* Address code review comments.

* Also notify message store on failure

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>
2026-01-05 11:51:15 +00:00

91 lines
2.6 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI.Abstractions.UnitTests;
/// <summary>
/// Contains tests for the <see cref="ChatMessageStore"/> class.
/// </summary>
public class ChatMessageStoreTests
{
#region GetService Method Tests
[Fact]
public void GetService_RequestingExactStoreType_ReturnsStore()
{
var store = new TestChatMessageStore();
var result = store.GetService(typeof(TestChatMessageStore));
Assert.NotNull(result);
Assert.Same(store, result);
}
[Fact]
public void GetService_RequestingBaseStoreType_ReturnsStore()
{
var store = new TestChatMessageStore();
var result = store.GetService(typeof(ChatMessageStore));
Assert.NotNull(result);
Assert.Same(store, result);
}
[Fact]
public void GetService_RequestingUnrelatedType_ReturnsNull()
{
var store = new TestChatMessageStore();
var result = store.GetService(typeof(string));
Assert.Null(result);
}
[Fact]
public void GetService_WithServiceKey_ReturnsNull()
{
var store = new TestChatMessageStore();
var result = store.GetService(typeof(TestChatMessageStore), "some-key");
Assert.Null(result);
}
[Fact]
public void GetService_WithNullServiceType_ThrowsArgumentNullException()
{
var store = new TestChatMessageStore();
Assert.Throws<ArgumentNullException>(() => store.GetService(null!));
}
[Fact]
public void GetService_Generic_ReturnsCorrectType()
{
var store = new TestChatMessageStore();
var result = store.GetService<TestChatMessageStore>();
Assert.NotNull(result);
Assert.Same(store, result);
}
[Fact]
public void GetService_Generic_ReturnsNullForUnrelatedType()
{
var store = new TestChatMessageStore();
var result = store.GetService<string>();
Assert.Null(result);
}
#endregion
private sealed class TestChatMessageStore : ChatMessageStore
{
public override ValueTask<IEnumerable<ChatMessage>> InvokingAsync(InvokingContext context, CancellationToken cancellationToken = default)
=> new(Array.Empty<ChatMessage>());
public override ValueTask InvokedAsync(InvokedContext context, CancellationToken cancellationToken = default)
=> default;
public override JsonElement Serialize(JsonSerializerOptions? jsonSerializerOptions = null)
=> default;
}
}