Path: redb-route/tests/redb.Route.Tests/Reference/DslFilterReferenceTests.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests/Reference/DslFilterReferenceTests.cs
----
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.TestKit;
namespace redb.Route.Tests.Reference;
///
/// Reference / canonical DSL examples for the Filter Enterprise Integration Pattern.
///
/// These tests are designed to be readable end-to-end demonstrations of every
/// useful Filter shape supported by the REDB route DSL. They are intentionally
/// over-asserted so that both humans and LLM-driven tooling can use them as
/// authoritative documentation of the contract.
///
/// Forms covered:
/// 1. Leaf Filter(predicate) — tail-consuming, no explicit close.
/// 2. Leaf Filter(predicate).EndFilter() — explicit scope close.
/// 3. Action-overload Filter(predicate, b => ...) — body closed, tail keeps flowing.
/// 4. Nested Filter inside Choice/When.
/// 5. Nested Filter inside Split (per-item filtering).
/// 6. Nested Filter inside Loop.
/// 7. Two sequential Filters (AND-chain).
/// 8. Filter inside DoTry — catch sees only matched messages.
/// 9. Enterprise scenario: high-value order routing with audit tail.
/// 10. Route-level composite Filter DSL cascade — nested action-overload
/// filters form a gating chain (logical AND) with per-stage trace.
/// 11. Route-level composite Filter DSL cascade — Camel-style chained form
/// using Filter(…).Filter(…)…EndFilter().EndFilter() without lambdas.
///
public class DslFilterReferenceTests
{
// ── 1. Leaf form ─────────────────────────────────────────────────────────
///
/// Canonical leaf Filter: predicate gates the rest of the route.
/// Non-matching exchanges stop after the Filter; matching exchanges flow through.
///
[Fact]
public async Task Leaf_Filter_TailConsumed_NonMatchingStops()
{
await using var context = new RouteContext();
var passed = new List();
context.AddRoutes(r =>
{
r.From("direct://leaf-flt")
.Filter(e => (int)e.In.Body! >= 10)
.Process(e => passed.Add((int)e.In.Body!));
});
await context.Start();
foreach (var i in new[] { 5, 10, 7, 15, 20, 1 })
await context.SendBody("direct://leaf-flt", i);
passed.Should().Equal(10, 15, 20);
}
// ── 2. Explicit EndFilter() ──────────────────────────────────────────────
///
/// Same behaviour as the leaf form, but explicitly closes the Filter scope
/// with EndFilter(). Camel parity: end() on a filter pops the scope.
///
[Fact]
public async Task Leaf_Filter_WithEndFilter_ReopensRouteScope()
{
await using var context = new RouteContext();
var inside = new List();
var afterEnd = new List();
context.AddRoutes(r =>
{
r.From("direct://flt-endflt")
.Filter(e => (int)e.In.Body! % 2 == 0)
.Process(e => inside.Add((int)e.In.Body!))
.EndFilter()
.Process(e => afterEnd.Add((int)e.In.Body!));
});
await context.Start();
foreach (var i in new[] { 1, 2, 3, 4 })
await context.SendBody("direct://flt-endflt", i);
inside.Should().Equal(2, 4);
// EndFilter() pops the Filter scope and restores the parent (route) scope,
// so steps appended after it run for every exchange — matched and not matched.
afterEnd.Should().Equal(1, 2, 3, 4);
}
// ── 3. Action-overload (W4.4 explicit scope form) ────────────────────────
///
/// Filter(predicate, body) closes the filtered body when the inner lambda returns —
/// the route tail after the call runs for every exchange (matched and not matched).
/// This is the recommended form for embedded filtering.
///
[Fact]
public async Task ActionOverload_Filter_BodyClosed_TailRunsForAll()
{
await using var context = new RouteContext();
var inside = new List();
var tail = new List();
context.AddRoutes(r =>
{
r.From("direct://flt-action")
.Filter(e => (int)e.In.Body! > 0, b => b
.Process(e => inside.Add((int)e.In.Body!)))
.Process(e => tail.Add((int)e.In.Body!));
});
await context.Start();
foreach (var i in new[] { -1, 0, 1, 2, -3, 5 })
await context.SendBody("direct://flt-action", i);
inside.Should().Equal(1, 2, 5);
tail.Should().Equal(-1, 0, 1, 2, -3, 5);
}
// ── 4. Filter nested in Choice/When ──────────────────────────────────────
///
/// Choice → When → Filter(action) → Process. Demonstrates that the When
/// branch tail keeps flowing after the inner Filter body closes.
///
[Fact]
public async Task Filter_InsideChoiceWhen_NestedScope_BranchTailRuns()
{
await using var context = new RouteContext();
var matched = new List();
var branchTail = new List();
context.AddRoutes(r =>
{
r.From("direct://choice-flt-nested")
.Choice(c => c
.When(e => (string)e.In.Headers["type"]! == "order", b => b
.Filter(e => (decimal)e.In.Body! > 100m, fb => fb
.Process(e => matched.Add($"big:{e.In.Body}")))
.Process(e => branchTail.Add($"any:{e.In.Body}"))));
});
await context.Start();
Task Send(decimal v, string type) =>
context.SendBodyAndHeader("direct://choice-flt-nested", v, "type", type);
await Send(50m, "order");
await Send(150m, "order");
await Send(500m, "order");
await Send(999m, "invoice"); // skipped by When
matched.Should().Equal("big:150", "big:500");
branchTail.Should().Equal("any:50", "any:150", "any:500");
}
// ── 5. Filter nested in Split (per-item) ─────────────────────────────────
///
/// Split → Filter(action) per element. The Filter body sees only matching
/// items; the Split body tail sees every item.
///
[Fact]
public async Task Filter_InsideSplit_PerItem_BodyConditional_TailEveryItem()
{
await using var context = new RouteContext();
var kept = new List();
var seen = new List();
context.AddRoutes(r =>
{
r.From("direct://split-flt")
.Split(e => (IEnumerable