Path: redb-route/tests/redb.Route.Tests.Xml/CatalogAndSynonymTests.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.Xml/CatalogAndSynonymTests.cs
----
using System.Xml.Linq;
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Diagnostics;
using redb.Route.Xml;
namespace redb.Route.Tests.Xml;
/// Fast/Safe processing mode of the test connector.
public enum CatMode { Fast, Safe }
/// The options of the test connector — an ordinary .
public sealed class CatEndpointOptions : EndpointOptions
{
public string? GroupId { get; set; }
public int Retries { get; set; } = 3;
[Sensitive] public string? Password { get; set; }
public CatMode Mode { get; set; } = CatMode.Fast;
public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(5);
public override void Validate() { }
}
///
/// The test connector of the anti-copy-paste criterion: Options, a Scheme, and ONE line of
/// §7.2 metadata — and it must appear in the structured form, the catalog and the generated
/// XSD without a single line of XML-specific code.
///
public sealed class CatComponent : ComponentBase
{
public override string Scheme => "cat";
public override string? StructuredPathSynonym => "queue";
public override IEndpoint CreateEndpoint(EndpointUri uri)
=> throw new NotSupportedException("registration-only test connector");
}
/// A connector whose path is a text (the sql shape).
public sealed class SqlishComponent : ComponentBase
{
public override string Scheme => "sqlish";
public override bool PathIsText => true;
public override IEndpoint CreateEndpoint(EndpointUri uri)
=> throw new NotSupportedException("registration-only test connector");
}
/// Options for the dog family — named after the BASE component.
public sealed class DogEndpointOptions : EndpointOptions
{
public bool Secure { get; set; }
public override void Validate() { }
}
/// The base of the Https shape.
public class DogComponent : ComponentBase
{
public override string Scheme => "dog";
public override IEndpoint CreateEndpoint(EndpointUri uri)
=> throw new NotSupportedException("registration-only test connector");
}
///
/// The Https shape: a DERIVED component (HttpsComponent : HttpComponent) with no options
/// class of its own — it must inherit the base's options in the catalog.
///
public sealed class DogsComponent : DogComponent
{
public override string Scheme => "dogs";
}
///
/// Component catalog metadata: the component catalog is reflection over what a connector
/// already has, the path synonym is read FROM the component, and the catalog-aware XSD makes
/// the structured endpoint children strict and enum-hinted.
///
public class CatalogAndSynonymTests : IAsyncLifetime
{
private readonly RouteContext _context = new();
public CatalogAndSynonymTests()
{
_context.AddComponent(new CatComponent());
_context.AddComponent(new SqlishComponent());
}
public Task InitializeAsync() => Task.CompletedTask;
public async Task DisposeAsync()
{
await _context.DisposeAsync();
GC.SuppressFinalize(this);
}
private static IReadOnlyList Catalog()
=> ComponentCatalog.Build([new CatComponent(), new SqlishComponent()]);
// ── the catalog is reflection, not a hand-kept list ──────────────────────
[Fact]
public void DerivedComponent_InheritsTheBaseOptions_TheHttpsShape()
{
// The live catalog run over the worker caught this: HttpsComponent : HttpComponent
// has no HttpsEndpointOptions — the naming walk must climb the base chain.
var catalog = ComponentCatalog.Build([new DogsComponent()]);
var dogs = catalog.Single(c => c.Scheme == "dogs");
dogs.OptionsType.Should().Be(typeof(DogEndpointOptions).FullName);
dogs.Options.Should().Contain(o => o.Name == "Secure");
}
[Fact]
public void Catalog_DescribesTheConnector_FromItsOwnCode()
{
var cat = Catalog().Single(c => c.Scheme == "cat");
cat.PathSynonym.Should().Be("queue");
cat.PathIsText.Should().BeFalse();
cat.OptionsType.Should().Be(typeof(CatEndpointOptions).FullName);
cat.Package.Should().Be(typeof(CatComponent).Assembly.GetName().Name);
var options = cat.Options.ToDictionary(o => o.Name);
options["Retries"].Should().BeEquivalentTo(new CatalogOption("Retries", "int", "3", false, null, Role: null, ConnectionParameter: false, ConnectionFactoryReference: false));
options["Password"].Sensitive.Should().BeTrue("the [Sensitive] mark must reach the property panel");
options["Mode"].Type.Should().Be("enum");
options["Mode"].EnumValues.Should().Equal("Fast", "Safe");
options["Mode"].Default.Should().Be("Fast");
options["Timeout"].Type.Should().Be("duration");
options["Timeout"].Default.Should().Be("00:00:05");
}
[Fact]
public void CatalogJson_IsCamelCased_AndMarksSecrets()
{
var json = ComponentCatalog.ToJson(Catalog());
json.Should().Contain("\"scheme\": \"cat\"")
.And.Contain("\"pathSynonym\": \"queue\"")
.And.Contain("\"sensitive\": true");
}
// ── the synonym comes from the component, not from a list ────────────────
[Fact]
public void StructuredForm_UsesTheComponentsPathSynonym()
{
_context.AddXmlRoutesFromContent("""
""");
var text = RouteDescriber.Describe(Examples.ExampleHarness.Definitions(_context));
text.Should().Contain("to uri=cat://orders?retries=5",
"queue= is the component's declared synonym for the path part");
}
[Fact]
public void SynonymAndPath_Together_IsASchemaError()
{
var act = () => _context.AddXmlRoutesFromContent("""
""");
act.Should().Throw().WithMessage("*carries its path more than once*");
}
[Fact]
public async Task PathIsText_TakesThePathFromContent_AndRefusesTheAttribute()
{
_context.AddXmlRoutesFromContent("""
""");
RouteDescriber.Describe(Examples.ExampleHarness.Definitions(_context))
.Should().Contain("to uri=sqlish://SELECT 1");
await using var other = new RouteContext();
other.AddComponent(new SqlishComponent());
var act = () => other.AddXmlRoutesFromContent("""
""");
act.Should().Throw()
.WithMessage("*path of is a text*element content*");
}
// ── the catalog-aware XSD: strict scheme elements with typed options ─────
[Fact]
public void CatalogAwareXsd_ValidatesTheKnownScheme_AndRejectsTheUnknown()
{
var registry = ElementRegistry.CreateDefault();
var catalog = Catalog();
XmlRouteSchema.Validate(XDocument.Parse("""
""", LoadOptions.SetLineInfo), registry, catalog)
.Should().BeEmpty();
XmlRouteSchema.Validate(XDocument.Parse("""
""", LoadOptions.SetLineInfo), registry, catalog)
.Should().NotBeEmpty("a scheme outside the catalog is not a valid endpoint child");
}
[Fact]
public void CatalogAwareXsd_EnforcesEnumOptions()
{
var problems = XmlRouteSchema.Validate(XDocument.Parse("""
""", LoadOptions.SetLineInfo), ElementRegistry.CreateDefault(), Catalog());
problems.Should().Contain(p => p.Message.Contains("Sideways"),
"enum options become xs:enumeration — the editor offers Fast/Safe");
}
}