Path: redb-route/tests/redb.Route.Tests/Components/ComponentConstructionTests.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/Components/ComponentConstructionTests.cs
----
using AwesomeAssertions;
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Components;
using redb.Route.Core;
using redb.Route.Extensions;
namespace redb.Route.Tests.Components;
///
/// The one rule by which a scanned component type is created, shared by the engine's AddComponents and the XML
/// catalog command: a constructor callable without arguments (none, or only optional ones), and a report naming every
/// component left out, so a scheme never drops out in silence.
///
public sealed class ComponentConstructionTests
{
/// A component whose only constructor needs an argument, as mcp's did.
public sealed class NeedsArgumentComponent(string required) : ComponentBase
{
public override string Scheme => "needs-" + required;
public override IEndpoint CreateEndpoint(EndpointUri uri) => throw new NotSupportedException();
}
/// A component whose constructor throws.
public sealed class ThrowingComponent : ComponentBase
{
public ThrowingComponent() => throw new InvalidOperationException("no config");
public override string Scheme => "throwing";
public override IEndpoint CreateEndpoint(EndpointUri uri) => throw new NotSupportedException();
}
/// A component whose constructor has only optional parameters, as HttpComponent(string scheme = "http").
public sealed class OptionalArgumentComponent(string scheme = "optional-arg") : ComponentBase
{
public override string Scheme => scheme;
public override IEndpoint CreateEndpoint(EndpointUri uri) => throw new NotSupportedException();
}
[Fact]
public void A_component_without_an_argument_free_constructor_is_reported_with_its_reason()
{
var skipped = new List<(Type Type, string Reason)>();
var components = ComponentConstruction
.Create([typeof(NeedsArgumentComponent), typeof(TimerComponent), typeof(LogComponent)],
(type, reason) => skipped.Add((type, reason)))
.ToList();
components.Select(c => c.Scheme).Should().BeEquivalentTo(["timer", "log"], "log's constructor has only optional parameters");
skipped.Should().Equal((typeof(NeedsArgumentComponent), ComponentConstruction.NoArgumentFreeConstructor));
}
[Fact]
public void A_constructor_that_throws_is_reported_with_its_reason()
{
var skipped = new List<(Type Type, string Reason)>();
ComponentConstruction.Create([typeof(ThrowingComponent)], (type, reason) => skipped.Add((type, reason)))
.Should().BeEmpty();
skipped.Should().Equal((typeof(ThrowingComponent), "constructor failed (no config)"));
}
[Fact]
public void Abstract_types_and_non_components_are_not_reported()
{
var skipped = new List<(Type Type, string Reason)>();
ComponentConstruction.Create([typeof(ComponentBase), typeof(string)], (type, reason) => skipped.Add((type, reason)))
.Should().BeEmpty();
skipped.Should().BeEmpty();
}
[Fact]
public void AddComponents_registers_a_component_whose_constructor_has_only_optional_parameters()
{
var ctx = new RouteContext();
ctx.AddComponents(typeof(OptionalArgumentComponent).Assembly);
ctx.HasComponent("optional-arg").Should().BeTrue(
"the engine's scan follows the catalog's rule: http's constructor has only an optional scheme");
}
[Fact]
public void AddComponents_names_each_component_it_leaves_out_in_a_warning()
{
var provider = new RecordingLoggerProvider();
using var factory = LoggerFactory.Create(b => b.AddProvider(provider));
var ctx = new RouteContext(loggerFactory: factory);
ctx.AddComponents(typeof(ThrowingComponent).Assembly);
provider.Warnings.Should().Contain(w => w.Contains(typeof(ThrowingComponent).FullName!) && w.Contains("no config"));
provider.Warnings.Should().Contain(w =>
w.Contains(typeof(NeedsArgumentComponent).FullName!) && w.Contains(ComponentConstruction.NoArgumentFreeConstructor));
ctx.HasComponent("throwing").Should().BeFalse();
}
private sealed class RecordingLoggerProvider : ILoggerProvider
{
public readonly List Warnings = [];
public ILogger CreateLogger(string categoryName) => new Recorder(this);
public void Dispose() { }
private sealed class Recorder(RecordingLoggerProvider owner) : ILogger
{
public IDisposable? BeginScope(TState state) where TState : notnull => null;
public bool IsEnabled(LogLevel logLevel) => true;
public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
Func formatter)
{
if (logLevel == LogLevel.Warning)
lock (owner.Warnings)
owner.Warnings.Add(formatter(state, exception));
}
}
}
}