From 00a132a0953016a3e535229ff959c85de912acef Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 13 May 2026 21:28:35 +0000 Subject: [PATCH] Add next speaker validation test Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/322c9e2d-59bc-42ad-9a1e-f6fd4c866b26 Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com> --- .../MagenticOrchestrationTests.cs | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/MagenticOrchestrationTests.cs b/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/MagenticOrchestrationTests.cs index fa57050433..5e1c596da5 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/MagenticOrchestrationTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/MagenticOrchestrationTests.cs @@ -142,6 +142,45 @@ public class MagenticOrchestrationTests planEvent.FullTaskLedger.Should().NotBeNull(); } + [Fact] + public async Task NextSpeaker_Invalid_Triggers_FinalAnswer() + { + // Arrange: ProgressLedger returns invalid next_speaker + List factsResponse = CreatePlanResponse("Facts about the task"); + List planResponse = CreatePlanResponse("Step 1: Execute"); + List invalidNextSpeakerLedger = CreateProgressLedgerResponse( + isRequestSatisfied: false, + isInLoop: false, + isProgressBeingMade: true, + nextSpeaker: "NonExistentAgent", // Invalid - doesn't match any team member + instructionOrQuestion: "Continue"); + List finalAnswer = CreateFinalAnswerResponse("Forced to conclude due to invalid speaker"); + + TestReplayAgent manager = new( + [factsResponse, planResponse, invalidNextSpeakerLedger, finalAnswer], + name: "Manager"); + TestEchoAgent worker = new(name: "Worker"); + + List collectedEvents = []; + + Workflow workflow = new MagenticWorkflowBuilder(manager) + .AddParticipants(worker) + .RequirePlanSignoff(false) + .Build(); + + // Act + WorkflowRunResult runResult = await RunMagenticWorkflowAsync( + workflow, + [new ChatMessage(ChatRole.User, "Do task")], + eventCollector: collectedEvents); + + // Assert: Warning should be emitted and final answer prepared + collectedEvents.OfType() + .Should().Contain(e => e.Data != null && e.Data.ToString()!.Contains("Invalid next speaker")); + runResult.Result.Should().NotBeNull(); + runResult.Result![0].Text.Should().Contain("Forced to conclude"); + } + [Fact] public async Task ProgressLedger_Updated_Event_Emitted() {