forked from microsoft/agent-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionSmokeTest.cs
More file actions
128 lines (103 loc) · 4 KB
/
ReflectionSmokeTest.cs
File metadata and controls
128 lines (103 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) Microsoft. All rights reserved.
#pragma warning disable CS0618 // Type or member is obsolete - Testing legacy reflection-based pattern
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Agents.AI.Workflows.Execution;
using Microsoft.Agents.AI.Workflows.Reflection;
using Moq;
namespace Microsoft.Agents.AI.Workflows.UnitTests;
public class BaseTestExecutor<TActual>(string id) : ReflectingExecutor<TActual>(id) where TActual : ReflectingExecutor<TActual>
{
protected void OnInvokedHandler() => this.InvokedHandler = true;
public bool InvokedHandler
{
get;
private set;
}
}
public class DefaultHandler() : BaseTestExecutor<DefaultHandler>(nameof(DefaultHandler)), IMessageHandler<object>
{
public ValueTask HandleAsync(object message, IWorkflowContext context, CancellationToken cancellationToken = default)
{
this.OnInvokedHandler();
return this.Handler(message, context);
}
public Func<object, IWorkflowContext, ValueTask> Handler
{
get;
set;
} = (message, context) => default;
}
public class TypedHandler<TInput>() : BaseTestExecutor<TypedHandler<TInput>>(nameof(TypedHandler<>)), IMessageHandler<TInput>
{
public ValueTask HandleAsync(TInput message, IWorkflowContext context, CancellationToken cancellationToken = default)
{
this.OnInvokedHandler();
return this.Handler(message, context);
}
public Func<TInput, IWorkflowContext, ValueTask> Handler
{
get;
set;
} = (message, context) => default;
}
public class TypedHandlerWithOutput<TInput, TResult>() : BaseTestExecutor<TypedHandlerWithOutput<TInput, TResult>>(nameof(TypedHandlerWithOutput<,>)), IMessageHandler<TInput, TResult>
{
public ValueTask<TResult> HandleAsync(TInput message, IWorkflowContext context, CancellationToken cancellationToken)
{
this.OnInvokedHandler();
return this.Handler(message, context);
}
public Func<TInput, IWorkflowContext, ValueTask<TResult>> Handler
{
get;
set;
} = (message, context) => default;
}
public class RoutingReflectionTests
{
private static async ValueTask<CallResult?> RunTestReflectAndRouteMessageAsync<TInput, TE>(BaseTestExecutor<TE> executor, TInput? input = default) where TInput : new() where TE : ReflectingExecutor<TE>
{
MessageRouter router = executor.Router;
Assert.NotNull(router);
input ??= new();
Assert.True(router.CanHandle(input.GetType()));
Assert.True(router.CanHandle(input));
CallResult? result = await router.RouteMessageAsync(input, Mock.Of<IWorkflowContext>());
Assert.True(executor.InvokedHandler);
return result;
}
[Fact]
public async Task Test_ReflectAndExecute_DefaultHandlerAsync()
{
DefaultHandler executor = new();
CallResult? result = await RunTestReflectAndRouteMessageAsync<object, DefaultHandler>(executor);
Assert.NotNull(result);
Assert.True(result.IsSuccess);
Assert.True(result.IsVoid);
}
[Fact]
public async Task Test_ReflectAndExecute_HandlerReturnsVoidAsync()
{
TypedHandler<int> executor = new();
CallResult? result = await RunTestReflectAndRouteMessageAsync<object, TypedHandler<int>>(executor, 3);
Assert.NotNull(result);
Assert.True(result.IsSuccess);
Assert.True(result.IsVoid);
}
[Fact]
public async Task Test_ReflectAndExecute_HandlerReturnsValueAsync()
{
TypedHandlerWithOutput<int, string> executor = new()
{
Handler = (message, context) => new ValueTask<string>($"{message}")
};
const string Expected = "3";
CallResult? result = await RunTestReflectAndRouteMessageAsync<object, TypedHandlerWithOutput<int, string>>(executor, int.Parse(Expected));
Assert.NotNull(result);
Assert.True(result.IsSuccess);
Assert.False(result.IsVoid);
Assert.Equal(Expected, result.Result);
}
}