* feat(bpmn): scope variables, trigger opt-out and composability for BpmnProcess Completes the container W2 left minimal, with the four things it deferred. Scope variables. BpmnScopeVariables implements IBpmnVariableReader over the scope's memory register, walking outward so an inner scope sees the enclosing one's data, and BpmnScopeHost now declares ScopeVariables and hands the reader to every snapshot. The read is three-valued: false for a name nothing in scope declares, Null for a declared variable holding nothing, and StoredExternally for a value JSON cannot carry. That last case deviates from the issue, deliberately. The issue names the unmaterialized-driver case, which is not detectable from the container's side: PersistentVariablesMiddleware loads with no excludeTags, and VariablePersistenceManager marks a block IsInitialized before testing the exclusion, so a variable whose driver was never read is indistinguishable from one whose driver returned null. Closing that needs a change to Elsa.Workflows.Core, which is out of bounds here, so the reader answers only what the block actually says and the XML doc records why. The route it does have is real and in the same spirit: a value the host holds and cannot put on the wire faults loudly rather than reading as an empty collection. Trigger opt-out. BpmnProcess.IsRootScope names the BPMN meaning of Elsa's CanStartWorkflow rather than adding a second flag that could disagree with the gate TriggerIndexer actually reads. It is off unless something says otherwise, and the applier refuses to start a BpmnProcess that claims root position as another scope's work: the damage a mis-flagged subprocess does happens at publish time, so repairing the object graph at runtime would leave the trigger registered while every test went green. ITrigger itself remains #7929. Composability and outcomes. A BpmnProcess in a Flowchart runs and the flowchart carries on (D11), and a nested transaction completing Cancelled reaches its parent's completion callback with that outcome intact, which is the only reason the parent routes the cancel boundary rather than the ordinary sequence flow. Every guard was mutation-tested red before green: both non-Present answers of the reader, the reader left unwired, the opt-out's default flipped (7 tests red, including the pre-existing nested-scope ones), the refusal removed, and the outcome dropped at each end of the trip to the parent. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(bpmn): apply review findings on scope variables, command batching, and outcome doc Read a scope variable through Elsa's configured serializer (via IPayloadSerializer, serialized against the value's own runtime type so a polymorphic value is not wrapped in Elsa's type-tagged envelope) instead of bare JsonSerializerDefaults, so a value only Elsa's converters can carry no longer collapses to StoredExternally. Refuse a root-scope StartWork before any command in the batch is applied, not mid-list, so a refusal cannot leave scope memory partially mutated under ContinueWithIncidentsStrategy. Document that BpmnProcess completes with only its interpreter outcome, so a default/null-port Flowchart connection never fires from it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * refactor(bpmn): filter the pre-scan explicitly Use commands.OfType<BpmnHostCommand.StartWork>() in ApplyAsync's root-scope pre-scan instead of a foreach + type-check, matching the static analysis suggestion. The apply loop below is unchanged. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
76 lines
3.5 KiB
C#
76 lines
3.5 KiB
C#
using System.Collections.Generic;
|
|
using Bpmn.Semantics;
|
|
using Elsa.Bpmn.Activities;
|
|
using Elsa.Bpmn.Hosting;
|
|
using Elsa.Testing.Shared;
|
|
using Elsa.Workflows;
|
|
using Elsa.Workflows.Activities;
|
|
using Elsa.Workflows.Models;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace Elsa.Bpmn.UnitTests;
|
|
|
|
/// <summary>
|
|
/// Covers the root-position refusal in <see cref="BpmnCommandApplier.ApplyAsync"/>: a <c>StartWork</c> command bound
|
|
/// to a nested <see cref="BpmnProcess"/> that (mis)declares itself the workflow's root scope must be refused before
|
|
/// any command in the same batch is applied — not mid-list, after earlier commands already mutated and saved scope
|
|
/// memory. Under <c>ContinueWithIncidentsStrategy</c> the throw is absorbed into an incident rather than surfaced, so
|
|
/// a partial application would otherwise leave the scope silently half-mutated.
|
|
/// </summary>
|
|
public class BpmnCommandApplierTests
|
|
{
|
|
private const string OrdinaryActivityId = "ordinary-activity";
|
|
private const string NestedProcessActivityId = "nested-activity";
|
|
|
|
[Fact]
|
|
public async Task ApplyAsync_RefusesTheWholeBatch_WhenALaterCommandBindsARootScopeProcess()
|
|
{
|
|
var (scopeContext, process) = await BuildScopeAsync();
|
|
var memory = BpmnScopeMemory.Load(scopeContext);
|
|
var applier = new BpmnCommandApplier(scopeContext, process, memory);
|
|
|
|
var commands = new BpmnHostCommand[]
|
|
{
|
|
new BpmnHostCommand.StartWork("ordinary-binding", "ordinary-element", "token-1", "cause", new Dictionary<string, string>(), null, null),
|
|
new BpmnHostCommand.StartWork("nested-binding", "nested-element", "token-2", "cause", new Dictionary<string, string>(), null, null)
|
|
};
|
|
|
|
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => applier.ApplyAsync(commands).AsTask());
|
|
|
|
Assert.Contains(NestedProcessActivityId, exception.Message);
|
|
|
|
// The earlier command in the same batch — the ordinary StartWork — must not have been applied: no work
|
|
// record persisted, and no child context created for it.
|
|
var reloaded = BpmnScopeMemory.Load(scopeContext);
|
|
Assert.Empty(reloaded.Work.Records);
|
|
Assert.DoesNotContain(scopeContext.WorkflowExecutionContext.ActivityExecutionContexts, context => context.Activity.Id == OrdinaryActivityId);
|
|
}
|
|
|
|
private static async Task<(ActivityExecutionContext ScopeContext, BpmnProcess Process)> BuildScopeAsync()
|
|
{
|
|
var ordinaryActivity = new WriteLine("ordinary") { Id = OrdinaryActivityId };
|
|
var nestedProcess = new BpmnProcess { Id = NestedProcessActivityId, IsRootScope = true };
|
|
var process = new BpmnProcess
|
|
{
|
|
Id = "process-activity",
|
|
Activities = { ordinaryActivity, nestedProcess },
|
|
WorkBindings = new Dictionary<string, string>(StringComparer.Ordinal)
|
|
{
|
|
["ordinary-binding"] = OrdinaryActivityId,
|
|
["nested-binding"] = NestedProcessActivityId
|
|
}
|
|
};
|
|
|
|
var fixture = new ActivityTestFixture(process);
|
|
fixture.ConfigureServices(services => services.AddSingleton<IIdentityGenerator, GuidIdentityGenerator>());
|
|
|
|
var scopeContext = await fixture.BuildAsync();
|
|
var workflowExecutionContext = scopeContext.WorkflowExecutionContext;
|
|
|
|
await workflowExecutionContext.ActivityRegistry.RegisterAsync(typeof(WriteLine));
|
|
await workflowExecutionContext.ActivityRegistry.RegisterAsync(typeof(BpmnProcess));
|
|
|
|
return (scopeContext, process);
|
|
}
|
|
}
|