Files
agent-framework/dotnet/tests/Microsoft.Agents.Workflows.UnitTests/MessageMergerTests.cs
T
Jacob Alber 3b81164a6d .NET: fix: MessageMerger crashes when there are no dangling messages (#660)
* fix: MessageMerger crashes when there are no dangling messages

* refactor: Better logic for AgentId in Workflow-as-Agent

If the parent "agent" instance received an Id or Name when being instantiated, we should avoid stomping over it with the subagents' ids. But if there is no parent identifier, and only a single subagent yielded identified messages, pull that in.
2025-09-10 06:10:30 +00:00

43 lines
1.5 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
using FluentAssertions;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.AI.Agents;
namespace Microsoft.Agents.Workflows.UnitTests;
public class MessageMergerTests
{
public static string TestAgentId1 => "TestAgent1";
public static string TestAgentId2 => "TestAgent2";
public static string TestAuthorName1 => "Assistant1";
public static string TestAuthorName2 => "Assistant2";
[Fact]
public void Test_MessageMerger_AssemblesMessage()
{
DateTimeOffset creationTime = DateTimeOffset.UtcNow;
string responseId = Guid.NewGuid().ToString("N");
string messageId = Guid.NewGuid().ToString("N");
MessageMerger merger = new();
foreach (AgentRunResponseUpdate update in "Hello Agent Framework Workflows!".ToAgentRunStream(authorName: TestAuthorName1, agentId: TestAgentId1, messageId: messageId, createdAt: creationTime, responseId: responseId))
{
merger.AddUpdate(update);
}
AgentRunResponse response = merger.ComputeMerged(responseId);
response.Messages.Should().HaveCount(1);
response.Messages[0].Role.Should().Be(ChatRole.Assistant);
response.Messages[0].AuthorName.Should().Be(TestAuthorName1);
response.AgentId.Should().Be(TestAgentId1);
response.CreatedAt.Should().NotBe(creationTime);
response.Messages[0].CreatedAt.Should().Be(creationTime);
response.Messages[0].Contents.Should().HaveCount(1);
}
}