using Elsa.Workflows;
using Elsa.Workflows.Models;
using JetBrains.Annotations;
using Xunit;
namespace Elsa.Testing.Shared;
///
/// Provides assertion methods for to facilitate Journal-based testing.
/// These methods integrate with xUnit's assertion framework.
///
[PublicAPI]
public static class RunWorkflowResultAssertions
{
/// The workflow result.
extension(RunWorkflowResult result)
{
///
/// Asserts that the specified activity was executed (present in the journal).
///
/// The activity that should have been executed.
public void AssertActivityExecuted(IActivity activity)
{
var context = result.GetActivityContext(activity);
Assert.NotNull(context);
}
///
/// Asserts that the specified activity was not executed (not present in the journal).
///
/// The activity that should not have been executed.
public void AssertActivityNotExecuted(IActivity activity)
{
var context = result.GetActivityContext(activity);
Assert.Null(context);
}
///
/// Asserts that the specified activity completed successfully.
///
/// The activity that should have completed.
public void AssertActivityCompleted(IActivity activity)
{
var context = result.GetActivityContext(activity);
Assert.NotNull(context);
Assert.Equal(ActivityStatus.Completed, context.Status);
}
///
/// Asserts that the specified activity has the expected status.
///
/// The activity to check.
/// The expected status.
public void AssertActivityStatus(IActivity activity, ActivityStatus expectedStatus)
{
var context = result.GetActivityContext(activity);
Assert.NotNull(context);
Assert.Equal(expectedStatus, context.Status);
}
///
/// Asserts that all specified activities were executed.
///
/// The activities that should have been executed.
public void AssertActivitiesExecuted(params IActivity?[] activities)
{
foreach (var activity in activities)
if (activity is not null) result.AssertActivityExecuted(activity);
}
///
/// Asserts that all specified activities were not executed.
///
/// The activities that should not have been executed.
public void AssertActivitiesNotExecuted(params IActivity?[] activities)
{
foreach (var activity in activities)
if (activity is not null) result.AssertActivityNotExecuted(activity);
}
///
/// Asserts that all specified activities completed successfully.
///
/// The activities that should have completed.
public void AssertActivitiesCompleted(params IActivity?[] activities)
{
foreach (var activity in activities)
if (activity is not null) result.AssertActivityCompleted(activity);
}
///
/// Asserts that the specified activity was executed exactly the expected number of times.
///
/// The activity to check.
/// The expected execution count.
public void AssertActivityExecutionCount(IActivity activity, int expectedCount)
{
var actualCount = result.GetExecutionCount(activity);
Assert.Equal(expectedCount, actualCount);
}
///
/// Asserts that the workflow completed successfully.
///
public void AssertWorkflowCompleted()
{
Assert.Equal(WorkflowStatus.Finished, result.WorkflowState.Status);
Assert.Equal(WorkflowSubStatus.Finished, result.WorkflowState.SubStatus);
}
///
/// Asserts that the workflow has the expected status and substatus.
///
/// The expected workflow status.
/// The expected workflow substatus.
public void AssertWorkflowStatus(WorkflowStatus expectedStatus, WorkflowSubStatus expectedSubStatus)
{
Assert.Equal(expectedStatus, result.WorkflowState.Status);
Assert.Equal(expectedSubStatus, result.WorkflowState.SubStatus);
}
}
}