// 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_Step4Async() { using StringWriter writer = new(); VerifyingPlaybackResponder 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 Step4EntryPoint.RunAsync(writer, userGuessCallback: responder.InvokeNext); Assert.Equal("You guessed correctly! You Win!", guessResult); } [Fact] public async Task Test_RunSample_Step5Async() { using StringWriter writer = new(); VerifyingPlaybackResponder responder = new( // Iteration 1 ("Guess the number.", 50), ("Your guess was too high. Try again.", 23), // Iteration 2 ("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); } [Fact] public async Task Test_RunSample_Step6Async() { using StringWriter writer = new(); await Step6EntryPoint.RunAsync(writer); string result = writer.ToString(); string[] lines = result.Split([Environment.NewLine], StringSplitOptions.RemoveEmptyEntries); Assert.Collection(lines, line => Assert.Contains($"{HelloAgent.DefaultId}: {HelloAgent.Greeting}", line), line => Assert.Contains($"{EchoAgent.DefaultId}: {EchoAgent.Prefix}{HelloAgent.Greeting}", line) ); } [Fact] public async Task Test_RunSample_Step7Async() { using StringWriter writer = new(); await Step7EntryPoint.RunAsync(writer); string result = writer.ToString(); string[] lines = result.Split([Environment.NewLine], StringSplitOptions.RemoveEmptyEntries); Assert.Collection(lines, line => Assert.Contains($"{HelloAgent.DefaultId}: {HelloAgent.Greeting}", line), line => Assert.Contains($"{EchoAgent.DefaultId}: {EchoAgent.Prefix}{HelloAgent.Greeting}", line) ); } } internal sealed class VerifyingPlaybackResponder { 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; } }