using Elsa.Expressions.JavaScript.Contracts;
using Elsa.Expressions.Models;
using Elsa.Testing.Shared;
using Elsa.Workflows.Memory;
using Microsoft.Extensions.DependencyInjection;
using Xunit.Abstractions;
namespace Elsa.JavaScript.IntegrationTests;
///
/// Tests that validate the behavior of JavaScript custom functions, not just their existence.
///
public class JintJavaScriptFunctionBehaviorTests(ITestOutputHelper testOutputHelper)
{
private readonly WorkflowTestFixture _fixture = new(testOutputHelper);
[Fact(DisplayName = "All JavaScript functions should execute without errors (smoke test)")]
public async Task All_Functions_Should_Execute_Without_Errors()
{
// Arrange
var script = @"
// Execute all functions that don't require arguments to ensure they work
var results = {
// Workflow info functions
workflowDefId: getWorkflowDefinitionId(),
workflowDefVersionId: getWorkflowDefinitionVersionId(),
workflowDefVersion: getWorkflowDefinitionVersion(),
workflowInstanceId: getWorkflowInstanceId(),
correlationId: getCorrelationId(),
workflowName: getWorkflowInstanceName(),
// GUID functions
newGuidValue: newGuid(),
newGuidStringValue: newGuidString(),
newShortGuidValue: newShortGuid(),
// Deprecated GUID functions
getGuidStringValue: getGuidString(),
getShortGuidValue: getShortGuid(),
// String utility functions
isNullOrWhiteSpaceEmpty: isNullOrWhiteSpace(''),
isNullOrWhiteSpaceText: isNullOrWhiteSpace('text'),
isNullOrEmptyEmpty: isNullOrEmpty(''),
isNullOrEmptyText: isNullOrEmpty('text'),
// Encoding functions
toJsonValue: toJson({ key: 'value' }),
stringToBase64Value: stringToBase64('test'),
bytesToStringValue: bytesToString([72, 101, 108, 108, 111]),
bytesToBase64Value: bytesToBase64([72, 101, 108, 108, 111]),
};
return toJson(results);
";
// Act
var result = await EvaluateScriptAsync(script);
// Assert - script should execute and return JSON
Assert.NotNull(result);
Assert.Contains("workflowDefId", result);
}
[Fact(DisplayName = "GUID functions should return valid formats")]
public async Task Guid_Functions_Should_Return_Valid_Formats()
{
// Arrange
var script = @"
return {
guid: newGuid().toString(),
guidString: newGuidString(),
shortGuid: newShortGuid(),
parsedGuid: parseGuid('12345678-1234-1234-1234-123456789abc').toString()
};
";
// Act
var result = await EvaluateScriptAsync