mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Enable more analyzers and various code tweaks/cleanup (#738)
This commit is contained in:
+29
-32
@@ -1,7 +1,7 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
@@ -19,33 +19,33 @@ namespace Microsoft.Agents.Workflows.Declarative.UnitTests;
|
||||
/// </summary>
|
||||
public sealed class DeclarativeWorkflowTest(ITestOutputHelper output) : WorkflowTest(output)
|
||||
{
|
||||
private ImmutableList<WorkflowEvent> WorkflowEvents { get; set; } = ImmutableList<WorkflowEvent>.Empty;
|
||||
private List<WorkflowEvent> WorkflowEvents { get; set; } = [];
|
||||
|
||||
private ImmutableDictionary<Type, int> WorkflowEventCounts { get; set; } = ImmutableDictionary<Type, int>.Empty;
|
||||
private Dictionary<Type, int> WorkflowEventCounts { get; set; } = [];
|
||||
|
||||
[Theory]
|
||||
[InlineData("BadEmpty.yaml")]
|
||||
[InlineData("BadId.yaml")]
|
||||
[InlineData("BadKind.yaml")]
|
||||
public async Task InvalidWorkflow(string workflowFile)
|
||||
public async Task InvalidWorkflowAsync(string workflowFile)
|
||||
{
|
||||
await Assert.ThrowsAsync<DeclarativeModelException>(() => this.RunWorkflow(workflowFile));
|
||||
await Assert.ThrowsAsync<DeclarativeModelException>(() => this.RunWorkflowAsync(workflowFile));
|
||||
this.AssertNotExecuted("end_all");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task LoopEachAction()
|
||||
public async Task LoopEachActionAsync()
|
||||
{
|
||||
await this.RunWorkflow("LoopEach.yaml");
|
||||
await this.RunWorkflowAsync("LoopEach.yaml");
|
||||
this.AssertExecutionCount(expectedCount: 35);
|
||||
this.AssertExecuted("foreach_loop");
|
||||
this.AssertExecuted("end_all");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task LoopBreakAction()
|
||||
public async Task LoopBreakActionAsync()
|
||||
{
|
||||
await this.RunWorkflow("LoopBreak.yaml");
|
||||
await this.RunWorkflowAsync("LoopBreak.yaml");
|
||||
this.AssertExecutionCount(expectedCount: 7);
|
||||
this.AssertExecuted("foreach_loop");
|
||||
this.AssertExecuted("breakLoop_now");
|
||||
@@ -55,9 +55,9 @@ public sealed class DeclarativeWorkflowTest(ITestOutputHelper output) : Workflow
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task LoopContinueAction()
|
||||
public async Task LoopContinueActionAsync()
|
||||
{
|
||||
await this.RunWorkflow("LoopContinue.yaml");
|
||||
await this.RunWorkflowAsync("LoopContinue.yaml");
|
||||
this.AssertExecutionCount(expectedCount: 7);
|
||||
this.AssertExecuted("foreach_loop");
|
||||
this.AssertExecuted("continueLoop_now");
|
||||
@@ -67,18 +67,18 @@ public sealed class DeclarativeWorkflowTest(ITestOutputHelper output) : Workflow
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EndConversationAction()
|
||||
public async Task EndConversationActionAsync()
|
||||
{
|
||||
await this.RunWorkflow("EndConversation.yaml");
|
||||
await this.RunWorkflowAsync("EndConversation.yaml");
|
||||
this.AssertExecutionCount(expectedCount: 1);
|
||||
this.AssertExecuted("end_all");
|
||||
this.AssertNotExecuted("sendActivity_1");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GotoAction()
|
||||
public async Task GotoActionAsync()
|
||||
{
|
||||
await this.RunWorkflow("Goto.yaml");
|
||||
await this.RunWorkflowAsync("Goto.yaml");
|
||||
this.AssertExecutionCount(expectedCount: 2);
|
||||
this.AssertExecuted("goto_end");
|
||||
this.AssertExecuted("end_all");
|
||||
@@ -90,9 +90,9 @@ public sealed class DeclarativeWorkflowTest(ITestOutputHelper output) : Workflow
|
||||
[Theory]
|
||||
[InlineData(12)]
|
||||
[InlineData(37)]
|
||||
public async Task ConditionAction(int input)
|
||||
public async Task ConditionActionAsync(int input)
|
||||
{
|
||||
await this.RunWorkflow("Condition.yaml", input);
|
||||
await this.RunWorkflowAsync("Condition.yaml", input);
|
||||
this.AssertExecutionCount(expectedCount: 9);
|
||||
this.AssertExecuted("setVariable_test");
|
||||
this.AssertExecuted("conditionGroup_test");
|
||||
@@ -118,9 +118,9 @@ public sealed class DeclarativeWorkflowTest(ITestOutputHelper output) : Workflow
|
||||
[Theory]
|
||||
[InlineData(12, 7)]
|
||||
[InlineData(37, 9)]
|
||||
public async Task ConditionActionWithElse(int input, int expectedActions)
|
||||
public async Task ConditionActionWithElseAsync(int input, int expectedActions)
|
||||
{
|
||||
await this.RunWorkflow("ConditionElse.yaml", input);
|
||||
await this.RunWorkflowAsync("ConditionElse.yaml", input);
|
||||
this.AssertExecutionCount(expectedActions);
|
||||
this.AssertExecuted("setVariable_test");
|
||||
this.AssertExecuted("conditionGroup_test");
|
||||
@@ -149,9 +149,9 @@ public sealed class DeclarativeWorkflowTest(ITestOutputHelper output) : Workflow
|
||||
[InlineData("SetTextVariable.yaml", 1, "set_text")]
|
||||
[InlineData("ClearAllVariables.yaml", 1, "clear_all")]
|
||||
[InlineData("ResetVariable.yaml", 2, "clear_var")]
|
||||
public async Task ExecuteAction(string workflowFile, int expectedCount, string expectedId)
|
||||
public async Task ExecuteActionAsync(string workflowFile, int expectedCount, string expectedId)
|
||||
{
|
||||
await this.RunWorkflow(workflowFile);
|
||||
await this.RunWorkflowAsync(workflowFile);
|
||||
this.AssertExecutionCount(expectedCount);
|
||||
this.AssertExecuted(expectedId);
|
||||
}
|
||||
@@ -240,14 +240,13 @@ public sealed class DeclarativeWorkflowTest(ITestOutputHelper output) : Workflow
|
||||
}
|
||||
}
|
||||
|
||||
private void AssertMessage(string message)
|
||||
{
|
||||
private void AssertMessage(string message) =>
|
||||
Assert.Contains(this.WorkflowEvents.OfType<MessageActivityEvent>(), e => string.Equals(e.Message.Trim(), message, StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
private Task RunWorkflow(string workflowPath) => this.RunWorkflow<string>(workflowPath, string.Empty);
|
||||
private Task RunWorkflowAsync(string workflowPath) =>
|
||||
this.RunWorkflowAsync(workflowPath, string.Empty);
|
||||
|
||||
private async Task RunWorkflow<TInput>(string workflowPath, TInput workflowInput) where TInput : notnull
|
||||
private async Task RunWorkflowAsync<TInput>(string workflowPath, TInput workflowInput) where TInput : notnull
|
||||
{
|
||||
using StreamReader yamlReader = File.OpenText(Path.Combine("Workflows", workflowPath));
|
||||
Mock<WorkflowAgentProvider> mockAgentProvider = new(MockBehavior.Strict);
|
||||
@@ -257,7 +256,7 @@ public sealed class DeclarativeWorkflowTest(ITestOutputHelper output) : Workflow
|
||||
|
||||
StreamingRun run = await InProcessExecution.StreamAsync(workflow, workflowInput);
|
||||
|
||||
this.WorkflowEvents = run.WatchStreamAsync().ToEnumerable().ToImmutableList();
|
||||
this.WorkflowEvents = run.WatchStreamAsync().ToEnumerable().ToList();
|
||||
foreach (WorkflowEvent workflowEvent in this.WorkflowEvents)
|
||||
{
|
||||
if (workflowEvent is ExecutorInvokedEvent invokeEvent)
|
||||
@@ -278,16 +277,14 @@ public sealed class DeclarativeWorkflowTest(ITestOutputHelper output) : Workflow
|
||||
this.Output.WriteLine($"MESSAGE: {messageEvent.Response.Messages[0].Text.Trim()}");
|
||||
}
|
||||
}
|
||||
this.WorkflowEventCounts = this.WorkflowEvents.GroupBy(e => e.GetType()).ToImmutableDictionary(e => e.Key, e => e.Count());
|
||||
this.WorkflowEventCounts = this.WorkflowEvents.GroupBy(e => e.GetType()).ToDictionary(e => e.Key, e => e.Count());
|
||||
}
|
||||
|
||||
private sealed class RootExecutor() :
|
||||
ReflectingExecutor<RootExecutor>(WorkflowActionVisitor.Steps.Root("anything")),
|
||||
IMessageHandler<string>
|
||||
{
|
||||
public async ValueTask HandleAsync(string message, IWorkflowContext context)
|
||||
{
|
||||
await context.SendMessageAsync($"{this.Id}: {DateTime.UtcNow.ToShortTimeString()}").ConfigureAwait(false);
|
||||
}
|
||||
public async ValueTask HandleAsync(string message, IWorkflowContext context) =>
|
||||
await context.SendMessageAsync($"{this.Id}: {DateTime.UtcNow:t}").ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user