|
1 | 1 | // Copyright (c) Microsoft. All rights reserved. |
2 | 2 |
|
3 | 3 | using System.Collections.Generic; |
| 4 | +using System.Linq; |
4 | 5 | using System.Threading.Tasks; |
| 6 | +using FluentAssertions; |
5 | 7 | using Microsoft.Extensions.AI; |
6 | 8 |
|
7 | 9 | namespace Microsoft.Agents.AI.Workflows.UnitTests; |
@@ -88,4 +90,69 @@ public void AgentResponseEvent_IsWorkflowOutputEvent() |
88 | 90 | Assert.Same(response, evt.Response); |
89 | 91 | Assert.Same(response, evt.Data); |
90 | 92 | } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Verifies that WorkflowStartedEvent is emitted first before any SuperStepStartedEvent. |
| 96 | + /// </summary> |
| 97 | + [Fact] |
| 98 | + public async Task StreamingRun_WorkflowStartedEvent_ShouldBeEmittedBefore_SuperStepStartedAsync() |
| 99 | + { |
| 100 | + // Arrange |
| 101 | + TestEchoAgent agent = new("test-agent"); |
| 102 | + Workflow workflow = AgentWorkflowBuilder.BuildSequential(agent); |
| 103 | + ChatMessage inputMessage = new(ChatRole.User, "Hello"); |
| 104 | + |
| 105 | + // Act |
| 106 | + await using StreamingRun run = await InProcessExecution.RunStreamingAsync(workflow, new List<ChatMessage> { inputMessage }); |
| 107 | + await run.TrySendMessageAsync(new TurnToken(emitEvents: true)); |
| 108 | + |
| 109 | + List<WorkflowEvent> events = []; |
| 110 | + await foreach (WorkflowEvent evt in run.WatchStreamAsync()) |
| 111 | + { |
| 112 | + events.Add(evt); |
| 113 | + } |
| 114 | + |
| 115 | + // Assert |
| 116 | + events.Should().NotBeEmpty(); |
| 117 | + |
| 118 | + List<WorkflowStartedEvent> startedEvents = events.OfType<WorkflowStartedEvent>().ToList(); |
| 119 | + startedEvents.Should().NotBeEmpty(); |
| 120 | + |
| 121 | + WorkflowStartedEvent? firstStartedEvent = startedEvents.FirstOrDefault(); |
| 122 | + SuperStepStartedEvent? firstSuperStepEvent = events.OfType<SuperStepStartedEvent>().FirstOrDefault(); |
| 123 | + firstSuperStepEvent.Should().NotBeNull(); |
| 124 | + |
| 125 | + int startedIndex = events.IndexOf(firstStartedEvent!); |
| 126 | + int superStepIndex = events.IndexOf(firstSuperStepEvent!); |
| 127 | + |
| 128 | + startedIndex.Should().BeLessThan(superStepIndex); |
| 129 | + } |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Verifies that WorkflowStartedEvent is emitted using Lockstep execution mode. |
| 133 | + /// </summary> |
| 134 | + [Fact] |
| 135 | + public async Task StreamingRun_LockstepExecution_ShouldEmit_WorkflowStartedEventAsync() |
| 136 | + { |
| 137 | + // Arrange |
| 138 | + TestEchoAgent agent = new("test-agent"); |
| 139 | + Workflow workflow = AgentWorkflowBuilder.BuildSequential(agent); |
| 140 | + ChatMessage inputMessage = new(ChatRole.User, "Hello"); |
| 141 | + |
| 142 | + // Act: Use Lockstep execution mode |
| 143 | + await using StreamingRun run = await InProcessExecution.Lockstep.RunStreamingAsync(workflow, new List<ChatMessage> { inputMessage }); |
| 144 | + await run.TrySendMessageAsync(new TurnToken(emitEvents: true)); |
| 145 | + |
| 146 | + List<WorkflowEvent> events = []; |
| 147 | + await foreach (WorkflowEvent evt in run.WatchStreamAsync()) |
| 148 | + { |
| 149 | + events.Add(evt); |
| 150 | + } |
| 151 | + |
| 152 | + // Assert |
| 153 | + events.Should().NotBeEmpty(); |
| 154 | + |
| 155 | + List<WorkflowStartedEvent> startedEvents = events.OfType<WorkflowStartedEvent>().ToList(); |
| 156 | + startedEvents.Should().NotBeEmpty(); |
| 157 | + } |
91 | 158 | } |
0 commit comments