Files
agent-framework/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/ReflectionSmokeTest.cs
Ben Thomas 907654a489 [BREAKING] Obsoleting ReflectingExecutor in favor of source gen (#3380)
* Initial working version with tests.

* Updates to validate class data once instead of for each handler method. Also updated Diagnostics Ids to format of MAFGENWF{NUM}

* Formatting and trying to fix generation project pack.

* Another atempt at getting the genrators project to build.

* More attempts to fix generator build and pack.

* Fixing file encodings.

* Initail round of cleanup.

* Trying to fix packing.

* Still trying to fix pipeline pack.

* Remove obsolescence markers, sample updates, and docs from generator branch.

This commit separates the generator core functionality from the
deprecation of ReflectingExecutor. The removed changes will be
re-added in a dependent branch (wf-obsolete-reflector).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* Mark ReflectingExecutor and IMessageHandler as obsolete.

This commit deprecates the reflection-based handler discovery approach
in favor of the new [MessageHandler] attribute with source generation.

Changes:
- Add [Obsolete] to ReflectingExecutor<T>, IMessageHandler<T>, IMessageHandler<T,R>
- Add #pragma to suppress warnings in internal reflection code
- Update Concurrent sample to use new [MessageHandler] pattern
- Add Directory.Build.props for samples to include generator
- Add documentation files explaining the migration

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* Obsoleteing Reflector-based workflow code generation in favor of Source Generators and updating some samples to use new pattern.

This commit deprecates the reflection-based handler discovery approach
in favor of the new [MessageHandler] attribute with source generation.

Changes:
- Add [Obsolete] to ReflectingExecutor<T>, IMessageHandler<T>, IMessageHandler<T,R>
- Add #pragma to suppress warnings in internal reflection code
- Update Concurrent sample to use new [MessageHandler] pattern
- Add Directory.Build.props for samples to include generator
- Add documentation files explaining the migration

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* Cleaning up temporary design and progress files.

---------

Co-authored-by: alliscode <bentho@microsoft.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>
2026-02-04 20:07:43 +00:00

129 lines
4.0 KiB
C#

// 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);
}
}