From ab3d89897941d99a4dd512f76b4115b046fd0229 Mon Sep 17 00:00:00 2001 From: Peter Ibekwe <109177538+peibekwe@users.noreply.github.com> Date: Thu, 20 Nov 2025 13:47:42 -0800 Subject: [PATCH] .NET: Add unit tests for RetrieveConversationMessageExecutor executor (#2232) * Add unit tests for create conversation executor * Update indentation and comment typo. * Added unit tests for declarative executor SetMultipleVariablesExecutor * Updated comments and syntactic sugar * Add unit test for declarative executor RetrieveConversationMessageExecutor * Removed irrelevant code statements * Updated based on copilot feedback. --- .../MockAgentProvider.cs | 18 +++++ ...RetrieveConversationMessageExecutorTest.cs | 67 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 dotnet/tests/Microsoft.Agents.AI.Workflows.Declarative.UnitTests/ObjectModel/RetrieveConversationMessageExecutorTest.cs diff --git a/dotnet/tests/Microsoft.Agents.AI.Workflows.Declarative.UnitTests/MockAgentProvider.cs b/dotnet/tests/Microsoft.Agents.AI.Workflows.Declarative.UnitTests/MockAgentProvider.cs index 67e4c68c5e..8a2e76415a 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Workflows.Declarative.UnitTests/MockAgentProvider.cs +++ b/dotnet/tests/Microsoft.Agents.AI.Workflows.Declarative.UnitTests/MockAgentProvider.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.AI; using Moq; namespace Microsoft.Agents.AI.Workflows.Declarative.UnitTests; @@ -15,10 +16,18 @@ internal sealed class MockAgentProvider : Mock { public IList ExistingConversationIds { get; } = []; + public ChatMessage? TestChatMessage { get; set; } + public MockAgentProvider() { this.Setup(provider => provider.CreateConversationAsync(It.IsAny())) .Returns(() => Task.FromResult(this.CreateConversationId())); + + this.Setup(provider => provider.GetMessageAsync( + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(Task.FromResult(this.CreateChatMessage())); } private string CreateConversationId() @@ -28,4 +37,13 @@ internal sealed class MockAgentProvider : Mock return newConversationId; } + + private ChatMessage CreateChatMessage() + { + this.TestChatMessage = new ChatMessage(ChatRole.User, Guid.NewGuid().ToString("N")) + { + MessageId = Guid.NewGuid().ToString("N"), + }; + return this.TestChatMessage; + } } diff --git a/dotnet/tests/Microsoft.Agents.AI.Workflows.Declarative.UnitTests/ObjectModel/RetrieveConversationMessageExecutorTest.cs b/dotnet/tests/Microsoft.Agents.AI.Workflows.Declarative.UnitTests/ObjectModel/RetrieveConversationMessageExecutorTest.cs new file mode 100644 index 0000000000..3f9fa0d606 --- /dev/null +++ b/dotnet/tests/Microsoft.Agents.AI.Workflows.Declarative.UnitTests/ObjectModel/RetrieveConversationMessageExecutorTest.cs @@ -0,0 +1,67 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System.Threading.Tasks; +using Microsoft.Agents.AI.Workflows.Declarative.Extensions; +using Microsoft.Agents.AI.Workflows.Declarative.ObjectModel; +using Microsoft.Bot.ObjectModel; +using Microsoft.Extensions.AI; +using Xunit.Abstractions; + +namespace Microsoft.Agents.AI.Workflows.Declarative.UnitTests.ObjectModel; + +/// +/// Tests for . +/// +public sealed class RetrieveConversationMessageExecutorTest(ITestOutputHelper output) : WorkflowActionExecutorTest(output) +{ + [Fact] + public async Task RetrieveMessageSuccessfullyAsync() + { + // Arrange, Act, Assert + await this.ExecuteTestAsync(nameof(RetrieveMessageSuccessfullyAsync), + "TestMessage"); + } + + private async Task ExecuteTestAsync( + string displayName, + string variableName) + { + // Arrange + MockAgentProvider mockAgentProvider = new(); + + RetrieveConversationMessage model = this.CreateModel( + this.FormatDisplayName(displayName), + FormatVariablePath(variableName), + "TestConversationId", + "DefaultMessageId"); + + RetrieveConversationMessageExecutor action = new(model, mockAgentProvider.Object, this.State); + + // Act + await this.ExecuteAsync(action); + + // Assert + ChatMessage testMessage = mockAgentProvider.TestChatMessage ?? new ChatMessage(); + VerifyModel(model, action); + this.VerifyState(variableName, testMessage.ToRecord()); + } + + private RetrieveConversationMessage CreateModel( + string displayName, + string messageVariable, + string conversationId, + string messageId) + { + RetrieveConversationMessage.Builder actionBuilder = + new() + { + Id = this.CreateActionId(), + DisplayName = this.FormatDisplayName(displayName), + Message = PropertyPath.Create(messageVariable), + ConversationId = StringExpression.Literal(conversationId), + MessageId = StringExpression.Literal(messageId) + }; + + return AssignParent(actionBuilder); + } +}