using Microsoft.DurableTask; using XBullet.EasyTesting.AzureFunctions; using Xunit; namespace TestFunctions.IntegrationTests; public sealed class DurableFunctionTests { #region docs-functions-durable-activity [Fact] public async Task Orchestrator_dispatches_to_real_activity_and_records_call() { var activity = new GreetingActivity("Hello"); var context = new TestOrchestrationContext(async call => { Assert.Equal(nameof(GreetingActivity), call.ActivityName); Assert.Equal(typeof(string), call.ResultType); return await activity.RunAsync(Assert.IsType(call.Input)); }); var result = await GreetingOrchestrator.RunAsync(context); Assert.Equal("Hello, Ada!", result); var call = Assert.Single(context.ActivityCalls); Assert.Equal(nameof(GreetingActivity), call.ActivityName); Assert.Equal("Ada", call.Input); Assert.Null(call.Options); } #endregion #region docs-functions-durable-limitations [Fact] public void Runtime_dependent_members_are_not_supported() { var context = new TestOrchestrationContext(_ => Task.FromResult(null)); Assert.Throws(() => _ = context.InstanceId); Assert.Throws(() => context.GetInput()); Assert.Throws(() => context.NewGuid()); } #endregion [Fact] public async Task Activity_dispatch_validates_handlers_nullable_results_and_result_types() { Assert.Throws(() => new TestOrchestrationContext(null!)); var context = new TestOrchestrationContext(_ => Task.FromResult(null)); Assert.Throws(() => context.ActivityHandler = null!); Assert.Null(await context.CallActivityAsync(new TaskName("nullable"))); await Assert.ThrowsAsync(() => context.CallActivityAsync(new TaskName("null-value-type"))); context.ActivityHandler = _ => Task.FromResult("wrong"); await Assert.ThrowsAsync(() => context.CallActivityAsync(new TaskName("wrong-type"))); } private static class GreetingOrchestrator { public static Task RunAsync(TaskOrchestrationContext context) => context.CallActivityAsync( new TaskName(nameof(GreetingActivity)), "Ada"); } private sealed class GreetingActivity(string prefix) { public Task RunAsync(string name) => Task.FromResult($"{prefix}, {name}!"); } }