using Elsa.Workflows;
using Elsa.Workflows.Models;
using JetBrains.Annotations;
namespace Elsa.Testing.Shared;
///
/// Provides extension methods for to facilitate testing with Journal-based assertions.
///
[PublicAPI]
public static class RunWorkflowResultExtensions
{
/// The workflow result.
extension(RunWorkflowResult result)
{
///
/// Gets the activity execution context for the specified activity from the workflow result's journal.
///
/// The activity to find.
/// The activity execution context, or null if not found.
public ActivityExecutionContext? GetActivityContext(IActivity activity) => result.Journal.ActivityExecutionContexts.FirstOrDefault(x => x.Activity == activity);
///
/// Checks if the specified activity was executed (present in the journal).
///
/// The activity to check.
/// True if the activity was executed, false otherwise.
public bool WasExecuted(IActivity activity) => result.GetActivityContext(activity) != null;
///
/// Checks if the specified activity completed successfully.
///
/// The activity to check.
/// True if the activity completed, false otherwise.
public bool WasCompleted(IActivity activity)
{
var context = result.GetActivityContext(activity);
return context?.Status == ActivityStatus.Completed;
}
///
/// Gets the execution status of the specified activity.
///
/// The activity to check.
/// The activity status, or null if the activity was not executed.
public ActivityStatus? GetActivityStatus(IActivity activity) => result.GetActivityContext(activity)?.Status;
///
/// Gets all executed activities from the workflow result's journal.
///
/// Collection of executed activities.
public IEnumerable GetExecutedActivities() => result.Journal.ActivityExecutionContexts.Select(x => x.Activity);
///
/// Gets all completed activities from the workflow result's journal.
///
/// Collection of completed activities.
public IEnumerable GetCompletedActivities() =>
result.Journal.ActivityExecutionContexts
.Where(x => x.Status == ActivityStatus.Completed)
.Select(x => x.Activity);
///
/// Counts how many times the specified activity was executed.
/// Useful for activities that may execute multiple times (e.g., in loops).
///
/// The activity to count.
/// The number of times the activity was executed.
public int GetExecutionCount(IActivity activity) =>
result.Journal.ActivityExecutionContexts.Count(x => x.Activity == activity);
}
}