elsa-core/test/unit/Elsa.Resilience.Core.UnitTests/ResilienceStrategyConfigTests.cs
Sipke Schoorstra cf23279bf1
test(resilience): cover Elsa.Resilience.Core and lift its coverage gate off the Debug/Release seam (#7971)
`dotnet test test/unit/Elsa.Resilience.Core.UnitTests` exited 1 on a clean
checkout with all 56 tests passing. The failure was the coverlet gate, not a
test: the project pinned `<Threshold>49</Threshold>` against 48.17% measured
line coverage in Debug. Release measures slightly differently and cleared it,
so CI (which builds `--configuration Release`) stayed green while every local
run — Debug is the default — went red. A red exit for a suite that passes
trains people to ignore exit codes.

Rather than move the goalposts, cover the code. The gap was concentrated in
`ResilientActivityInvoker`, which had no tests at all, plus the serializer,
the activity-execution extensions and the retry telemetry listener.
`Elsa.Testing.Shared`'s `ActivityTestFixture` was already referenced here and
builds a real `ActivityExecutionContext`, which is what all of them needed.

Adds 40 tests. The invoker ones drive a real zero-delay Polly retry pipeline,
so the telemetry listener is exercised through the actual Polly path rather
than being called directly: pass-through when no strategy is configured, the
applied strategy recorded on the context, retry-then-succeed, one record per
retry carrying identifiers and details, null details dropped, the retries flag
and attempt count, exhausted retries rethrowing, and an unhandled exception
type not being retried. The extensions tests build a three-level context chain
to pin down that the retries flag propagates up the ancestor chain and not
down.

Line coverage goes 48.17% -> 98.17% in Debug and 97.8% in Release; the five
lines still uncovered are defensive early-returns. The threshold moves to 90,
below the lower of the two configurations with enough headroom that the
Debug/Release delta cannot straddle it again. Verified by deleting the invoker
tests once: coverage falls to 68.97% and the gate fails as it should.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-21 03:01:51 +02:00

58 lines
2 KiB
C#

using System.Text.Json.Nodes;
using Elsa.Expressions.Models;
using Elsa.Resilience.Models;
namespace Elsa.Resilience.Core.UnitTests;
public class ResilienceStrategyConfigTests
{
[Fact(DisplayName = "Config in identifier mode should round-trip through a JSON node")]
public void SerializeToNode_IdentifierMode_RoundTrips()
{
var config = new ResilienceStrategyConfig
{
Mode = ResilienceStrategyConfigMode.Identifier,
StrategyId = "my-strategy"
};
var result = ResilienceStrategyConfig.Deserialize(config.SerializeToNode());
Assert.NotNull(result);
Assert.Equal(ResilienceStrategyConfigMode.Identifier, result.Mode);
Assert.Equal("my-strategy", result.StrategyId);
Assert.Null(result.Expression);
}
[Fact(DisplayName = "Config in expression mode should round-trip its expression through a JSON node")]
public void SerializeToNode_ExpressionMode_RoundTripsExpression()
{
var config = new ResilienceStrategyConfig
{
Mode = ResilienceStrategyConfigMode.Expression,
Expression = new("JavaScript", "getStrategy()")
};
var result = ResilienceStrategyConfig.Deserialize(config.SerializeToNode());
Assert.NotNull(result);
Assert.Equal(ResilienceStrategyConfigMode.Expression, result.Mode);
Assert.NotNull(result.Expression);
Assert.Equal("JavaScript", result.Expression.Type);
Assert.Equal("getStrategy()", result.Expression.Value?.ToString());
}
[Fact(DisplayName = "Deserializing a null node should return null")]
public void Deserialize_NullNode_ReturnsNull()
{
Assert.Null(ResilienceStrategyConfig.Deserialize(null));
}
[Fact(DisplayName = "Deserializing an unknown mode should fail rather than silently defaulting")]
public void Deserialize_UnknownMode_Throws()
{
var node = JsonNode.Parse("""{"mode":"NotAMode"}""");
Assert.ThrowsAny<Exception>(() => ResilienceStrategyConfig.Deserialize(node));
}
}