Files
agent-framework/dotnet/tests/Microsoft.Agents.Workflows.UnitTests/SampleSmokeTest.cs
T
Jacob Alber 32711cfa95 .Net: WIP: feat: Define Workflow and Executor APIs (#261)
* feat: Define Workflow and Executor APIs

* feat: Define IExecutionContext and Events

* feat: Simple Workflow Demos

* refactor: Move Workflows classes to separate assembly

* feat: Move FanOut/In to LowLevel API with new semantics

* feat: Implement Local Execution

* refactor: Assembly name .Workflow => .Workflows

* feat: Enable Default Message Handling

* also lifts Bind in MessageHandlerInfo to better be able to direclty invoke handlers (for AOT, later)

* feat: Implement StreamingHandle APIs

This allows the user to respond to WorkflowEvents with external messages, enabling HIL.

* feat: Add checks for duplicate edges and chain cycles

* feat: Add built-in WorkflowEvents

* refactor: Pull classes into own files

* refactor: Simplify Disposal pattern in Executor

* refactor: Break EdgeRunner file into per-type files

* refactor: Use Throw.IfNull()

* refactor: Remove AddLoop()

Per https://github.com/microsoft/agent-framework/pull/272#discussion_r2241739079 we decided this was not very useful.

* refactor: Normalize use of ValueTask

* fix: Build Break from removing .AddLoop

* refactor: Explicit routing and RouteBuilder

Split out reflection from MessageRouter implemention into build phase, enabling AOT compilation to drive RouteBuilding without reflection.

* test: Add Reflection/Invocation tests

* fix: Terminate on Completion event

* refactor: Update public API surface

* feat: Add support for external requests

* feat: Support hosting AIAgent instances in Workflows

* fix: Fix routing to go through Executor.ExecuteAsync

* test: Update samples for "must SendMessage" semantics

* Add invoking samples to unit tests to avoid future breaks

* fix: ExternalRequest should block Workflow completion

* feat: Normalize API surface against Python

* Also adds xmldoc to all public APIs

* refactor: Normalize UnitTest and Sample namespaces

* fix: Formatting

* refactor: Normalize project/folder names

* feat: Remove DynamicCodeExecution from ValueTaskTypeErasure

* fix: Fix ILTrim warnings

* docs: Add missing docs and fix typos

* feat: Hosted Agents should report Run events

* fix: Fix type propagation for ILTrim changes

* refactor: Simplify DynamicallyAccessedMembers annotations

* sample: Use static-Type construction of InputPort

* feat: Support non-Streaming Run Mode

* test: Add test for non-streaming execution

* refactor: Remove unused types

* refactor: Simplify Event and EdgeData type hierarchies

* feat: Add Switch (=Conditional Edge Group) control flow

* feat: Make .NET AutoSend the MessageHandler result

* feat: Implement State APIs

* refactor: Simplify public namespaces and code organization

* refactor: Reconcile .NET and Python names

---------

Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>
2025-08-13 22:34:39 +00:00

110 lines
3.3 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Agents.Workflows.Sample;
namespace Microsoft.Agents.Workflows.UnitTests;
public class SampleSmokeTest
{
[Fact]
public async Task Test_RunSample_Step1Async()
{
using StringWriter writer = new();
await Step1EntryPoint.RunAsync(writer);
string result = writer.ToString();
string[] lines = result.Split([Environment.NewLine], StringSplitOptions.RemoveEmptyEntries);
const string INPUT = "Hello, World!";
Assert.Collection(lines,
line => Assert.Contains($"UppercaseExecutor: {INPUT.ToUpperInvariant()}", line),
line => Assert.Contains($"ReverseTextExecutor: {new string(INPUT.ToUpperInvariant().Reverse().ToArray())}", line)
);
}
[Fact]
public async Task Test_RunSample_Step1aAsync()
{
using StringWriter writer = new();
await Step1aEntryPoint.RunAsync(writer);
string result = writer.ToString();
string[] lines = result.Split([Environment.NewLine], StringSplitOptions.RemoveEmptyEntries);
const string INPUT = "Hello, World!";
Assert.Collection(lines,
line => Assert.Contains($"UppercaseExecutor: {INPUT.ToUpperInvariant()}", line),
line => Assert.Contains($"ReverseTextExecutor: {new string(INPUT.ToUpperInvariant().Reverse().ToArray())}", line)
);
}
[Fact]
public async Task Test_RunSample_Step2Async()
{
using StringWriter writer = new();
string spamResult = await Step2EntryPoint.RunAsync(writer);
Assert.Equal(RemoveSpamExecutor.ActionResult, spamResult);
string nonSpamResult = await Step2EntryPoint.RunAsync(writer, "This is a valid message.");
Assert.Equal(RespondToMessageExecutor.ActionResult, nonSpamResult);
}
[Fact]
public async Task Test_RunSample_Step3Async()
{
using StringWriter writer = new();
string guessResult = await Step3EntryPoint.RunAsync(writer);
Assert.Equal("Guessed the number: 42", guessResult);
}
[Fact]
public async Task Test_RunSample_Step5Async()
{
using StringWriter writer = new();
VerifyingPlaybackResponder<string, int> responder = new(
("Guess the number.", 50),
("Your guess was too high. Try again.", 23),
("Your guess was too low. Try again.", 42));
string guessResult = await Step5EntryPoint.RunAsync(writer, userGuessCallback: responder.InvokeNext);
Assert.Equal("You guessed correctly! You Win!", guessResult);
}
}
internal sealed class VerifyingPlaybackResponder<TInput, TResponse>
{
public (TInput input, TResponse response)[] Responses { get; }
private int _position = 0;
public VerifyingPlaybackResponder(params (TInput input, TResponse response)[] responses)
{
this.Responses = responses;
}
public int Remaining => Math.Max(0, this.Responses.Length - this._position);
public TResponse InvokeNext(TInput input)
{
Assert.True(this.Remaining > 0);
(TInput expectedInput, TResponse expectedResponse) = this.Responses[this._position++];
Assert.Equal(expectedInput, input);
return expectedResponse;
}
}