using FluentAssertions; using Workflow.Application.BuildFlow.CustomFunctions.Commands; using Workflow.Application.BuildFlow.CustomFunctions.Handlers; using Workflow.Application.BuildFlow.CustomFunctions.Services; using Workflow.Domain.Enums; using Xunit; namespace BuildFlow.Workflow.Tests.BuildFlow; /// /// 自定义函数测试,约束函数必须发布后才能被运行,禁用后立即阻断。 /// public class CustomFunctionTests { [Fact] public async Task ExecuteAsync_PublishedFunction_ReturnsScriptResult() { await using var db = TestDbFactory.Create(); var createHandler = new CreateCustomFunctionCommandHandler(db); var publishHandler = new PublishCustomFunctionCommandHandler(db); var executor = new CustomFunctionExecutionService(db); var function = await createHandler.Handle( new CreateCustomFunctionCommand( "amount-level", "金额分级", "按金额输出审批等级", CustomFunctionType.Script, "((System.Text.Json.JsonElement)Variables[\"amount\"]).GetDecimal() > 1000 ? \"high\" : \"normal\"", null, null, null, null, """{"type":"object"}""", """{"type":"string"}""", 5000), CancellationToken.None); await publishHandler.Handle(new PublishCustomFunctionCommand(function.Id), CancellationToken.None); var result = await executor.ExecuteAsync( function.Id, """{"amount":1200}""", CancellationToken.None); result.Status.Should().Be(CustomFunctionExecutionStatus.Success); result.ResultJson.Should().Be("\"high\""); } [Fact] public async Task ExecuteAsync_DisabledFunction_ThrowsBusinessException() { await using var db = TestDbFactory.Create(); var createHandler = new CreateCustomFunctionCommandHandler(db); var publishHandler = new PublishCustomFunctionCommandHandler(db); var disableHandler = new DisableCustomFunctionCommandHandler(db); var executor = new CustomFunctionExecutionService(db); var function = await createHandler.Handle( new CreateCustomFunctionCommand( "disabled-func", "禁用函数", null, CustomFunctionType.Script, """"ok"""", null, null, null, null, null, null, 5000), CancellationToken.None); await publishHandler.Handle(new PublishCustomFunctionCommand(function.Id), CancellationToken.None); await disableHandler.Handle(new DisableCustomFunctionCommand(function.Id), CancellationToken.None); var act = () => executor.ExecuteAsync(function.Id, "{}", CancellationToken.None); await act.Should().ThrowAsync() .WithMessage("*已禁用*"); } }