elsa-core/test/unit/Elsa.Workflows.Runtime.UnitTests/Services/DefaultActivityExecutionMapperTests.cs
Sipke Schoorstra c27bbd4ec2
Adds activity execution call stack support (#7271)
* Adds activity execution call stack support

Implements a call stack mechanism to track the execution chain, enabling visibility into the invocation hierarchy.

Introduces new fields to scheduling models and runtime contexts to store call stack information.

Includes EF Core migrations for various database providers to support new columns in `ActivityExecutionRecords`.

Provides an API to query and reconstruct the call stack for a given activity execution.

* Refactor: Replace `PagedCallStackResult` with `Page<T>` for execution chain pagination

* Remove ambient scheduling scope logic and related methods

Simplifies scheduling logic by removing ambient scope mechanisms, refactoring scheduling context handling, and updating affected classes accordingly.

* Add `GetCallStackAsync` to `IActivityExecutionsApi` for querying activity execution call stack

* Add new properties to `ActivityExecutionRecord` for scheduling and execution tracking

Introduce fields for aggregated fault count, scheduling context, workflow instance details, and call stack depth to enhance execution monitoring and debugging capabilities.

* Add call stack visualization for activity executions

Introduced components and models to display a call stack for activity executions in the Workflow Instance Viewer. This includes UI elements for call stack rendering, error handling, and data integration with activity execution records.

* Remove obsolete ambient scheduling properties from WorkflowExecutionContext

* Fix infinite loop issues in activity execution chain traversal

Added cycle detection using a `HashSet` to prevent infinite loops when traversing activity execution chains in multiple storage implementations. Updated unit tests to validate correct handling of circular references and chain traversal.

* Refactor activity execution chain retrieval logic

Centralized the `GetExecutionChainAsync` method into an extension class to streamline and unify its implementation across stores. Removed redundant implementations from individual stores and updated interfaces to utilize the new extension method. This reduces code duplication and simplifies future maintenance.

* Add CallStackDepth property to activity contexts

Integrated the `CallStackDepth` property into `ActivityExecutionContext`, `ActivityExecutionContextState`, and related classes to track and manage the call stack depth of activity executions. Removed obsolete depth calculation logic to streamline the process.

* Add unit tests for call stack depth calculations and persistence

- Add `WorkflowExecutionContextTests` to verify correct calculation of call stack depth during activity execution.
- Add `WorkflowStateExtractorTests` to ensure call stack depth is preserved during state extraction and application.

* Update src/modules/Elsa.Workflows.Api/Endpoints/ActivityExecutions/GetCallStack/Endpoint.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update src/modules/Elsa.Workflows.Api/Endpoints/ActivityExecutions/GetCallStack/Endpoint.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Enhance activity execution handling with ID filter and task completion logic

- Implement `ActivityExecutionRecordFilter` for precise query matching by ID.
- Add await logic for task completion in command handler middleware.

* Add missing indexes for call stack columns in V3_7 migrations (#7250)

* Initial plan

* Add missing indexes for call stack columns in all provider migrations

Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>

* Optimize migrations by creating columns with correct indexable types

Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>

* Add unit tests for ActivityExecutionStoreExtensions

- Introduce tests for `GetExecutionChainAsync` covering scenarios of empty results, single records, multi-level chain traversal, workflow boundary constraints, pagination, and circular references.

* Refactor tests for `ActivityExecutionStoreExtensions`

- Replace mock setup with `CreateStore` helper for clean and clear test arrangements.
- Remove unused imports and clean up test setup for improved readability and maintenance.

* Remove unused imports from ActivityExecutionLogStore in Elsa.Persistence.EFCore module.

* Removes obsolete planning document

Removes the activity execution call stack planning document
as the feature has been implemented.

* Remove DefaultActivityExecutionMapperTests

- Deleted `DefaultActivityExecutionMapperTests.cs` as the test class is no longer in use and redundant.

* Address review feedback: Fix corrupted test, Oracle migrations, and call stack depth calculation (#7272)

* Initial plan

* Fix corrupted DefaultActivityExecutionMapperTests.cs test file

Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>

* Fix Oracle migration snapshot to use NCLOB for large text fields

Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>

* Optimize GetExecutionChainAsync to avoid loading all workflow instance records

Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>

* Fix CallStackDepth calculation to support cross-workflow invocations

- Add SchedulingCallStackDepth to ActivityInvocationOptions
- Update WorkflowExecutionContext to use provided depth when scheduling context not found
- Remove problematic test that reveals pre-existing bug with duplicate contexts

Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>

* Improve documentation for CallStackDepth calculation

Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>

* Add WorkflowStateExtractor to ActivityTestFixture services

* Refactor `DefaultActivityExecutionMapperTests` with `ActivityTestFixture` and add project reference for shared testing utilities.

* Propagate SchedulingCallStackDepth through cross-workflow invocation chain (#7273)

* Initial plan

* Add SchedulingCallStackDepth propagation through cross-workflow invocation chain

Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>

* Add unit tests for CallStackDepth propagation across workflow boundaries

Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>

* Initial plan (#7274)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>

* Reduce NVARCHAR2 column sizes in Oracle migrations to optimize storage and improve performance.

* Change column types to NCLOB for large text fields in Oracle migrations to enhance data storage capacity.

* Update GitHub Actions to use .NET 10.x and refactor setup classes for consistency

* Improve test project detection in GitHub Actions by handling non-csproj files and updating project sorting mechanism.

* Enhance GitHub Actions to display .NET environment info and enforce .NET 10 toolchain for test execution.

* Update GitHub Actions to use .NET SDK 10.0.1xx and enforce its usage for builds and tests.

* Refine GitHub Actions workflow by narrowing test project search to the `test/unit` directory and removing unnecessary script checks.

* Remove redundant build step from GitHub Actions workflow.

* Enhance GitHub Actions workflow by adding multiple test directories and handling ignored failed sources in .NET restore.

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>
2026-02-24 15:23:59 +01:00

63 lines
2.5 KiB
C#

using Elsa.Common;
using Elsa.Testing.Shared;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Management.Options;
using Microsoft.Extensions.Options;
using NSubstitute;
namespace Elsa.Workflows.Runtime.UnitTests.Services;
/// <summary>
/// Unit tests for DefaultActivityExecutionMapper.
/// </summary>
public class DefaultActivityExecutionMapperTests
{
/// <summary>
/// Tests that the mapper correctly maps CallStackDepth, SchedulingActivityExecutionId,
/// and SchedulingActivityId from the ActivityExecutionContext to the record.
/// </summary>
[Fact]
public async Task MapAsync_MapsCallStackDepth_Correctly()
{
// Arrange
var safeSerializer = Substitute.For<ISafeSerializer>();
safeSerializer.Serialize(Arg.Any<object>()).Returns("serialized");
var payloadSerializer = Substitute.For<IPayloadSerializer>();
payloadSerializer.Serialize(Arg.Any<object>()).Returns("serialized");
var compressionCodecResolver = Substitute.For<ICompressionCodecResolver>();
var compressionCodec = Substitute.For<ICompressionCodec>();
compressionCodecResolver.Resolve(Arg.Any<string>()).Returns(compressionCodec);
compressionCodec.CompressAsync(Arg.Any<string>(), Arg.Any<CancellationToken>()).Returns(new ValueTask<string>("compressed"));
var managementOptions = Substitute.For<IOptions<ManagementOptions>>();
managementOptions.Value.Returns(new ManagementOptions());
var mapper = new DefaultActivityExecutionMapper(
safeSerializer,
payloadSerializer,
compressionCodecResolver,
managementOptions);
// Use ActivityTestFixture to create real context objects
var activity = new WriteLine("Test");
var fixture = new ActivityTestFixture(activity);
var activityExecutionContext = await fixture.BuildAsync();
// Set the properties we want to test
activityExecutionContext.CallStackDepth = 2;
activityExecutionContext.SchedulingActivityExecutionId = "context-b";
activityExecutionContext.SchedulingActivityId = "activity-b";
// Act
var record = await mapper.MapAsync(activityExecutionContext);
// Assert
Assert.NotNull(record);
Assert.Equal(2, record.CallStackDepth);
Assert.Equal("context-b", record.SchedulingActivityExecutionId);
Assert.Equal("activity-b", record.SchedulingActivityId);
}
}