elsa-core/test/integration/Elsa.JavaScript.IntegrationTests/JsonConverterTest.cs
Sipke Schoorstra efe849d0bc
Add integration tests for RunJavaScript activity and introduce WorkflowTestFixture
- Implement tests to validate execution of valid and invalid scripts, outcome settings, workflow variable access, and complex script handling.
- Add `WorkflowTestFixture` to streamline integration testing setup and execution.
2025-10-21 20:02:27 +02:00

62 lines
2.4 KiB
C#

using System.Dynamic;
using System.Numerics;
using System.Text.Json;
using Elsa.Common.Converters;
using Elsa.Expressions.JavaScript.Contracts;
using Elsa.Expressions.Models;
using Elsa.Testing.Shared;
using Microsoft.Extensions.DependencyInjection;
using Xunit.Abstractions;
namespace Elsa.JavaScript.IntegrationTests;
public class JsonConverterTest(ITestOutputHelper testOutputHelper)
{
private readonly IServiceProvider _serviceProvider = new TestApplicationBuilder(testOutputHelper).Build();
[Fact(DisplayName = "JavaScript BigInt mapping to BigInteger serialization")]
public async Task Test1()
{
var javaScriptEvaluator = _serviceProvider.GetRequiredService<IJavaScriptEvaluator>();
var script = @"return {
'BigNumber': BigInt('7239948466988781569')
}";
var expressionExecutionContext = new ExpressionExecutionContext(_serviceProvider, new MemoryRegister());
var result = (await javaScriptEvaluator.EvaluateAsync(script, typeof(ExpandoObject), expressionExecutionContext))!;
var options = new JsonSerializerOptions
{
Converters = { new BigIntegerJsonConverter() }
};
var serializedText = JsonSerializer.Serialize(result, options);
Assert.Equal("{\"BigNumber\":7239948466988781569}", serializedText);
}
[Fact(DisplayName = "JavaScript BigInt mapping to BigInteger serialization")]
public async Task Test2()
{
var javaScriptEvaluator = _serviceProvider.GetRequiredService<IJavaScriptEvaluator>();
var script = @"return {
'BigNumber': BigInt('7239948466988781569')
}";
var expressionExecutionContext = new ExpressionExecutionContext(_serviceProvider, new MemoryRegister());
var result = (await javaScriptEvaluator.EvaluateAsync(script, typeof(ExpandoObject), expressionExecutionContext))!;
var serializedText = JsonSerializer.Serialize(result);
Assert.Equal("{\"BigNumber\":{\"IsPowerOfTwo\":false,\"IsZero\":false,\"IsOne\":false,\"IsEven\":false,\"Sign\":1}}", serializedText);
}
[Fact(DisplayName = "BigIntegerJsonConverter Deserialize")]
public Task Test3()
{
var options = new JsonSerializerOptions
{
Converters = { new BigIntegerJsonConverter() }
};
BigInteger bigInteger = JsonSerializer.Deserialize<BigInteger>("7239948466988781569", options);
Assert.Equal(7239948466988781569, bigInteger);
return Task.CompletedTask;
}
}