Path: redb-route/tests/redb.Route.Tests.Xml/XmlRouteSchemaTests.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/XmlRouteSchemaTests.cs
----
using System.Xml.Linq;
using AwesomeAssertions;
using redb.Route.Xml;
using redb.Route.Xml.CoreElements;
namespace redb.Route.Tests.Xml;
///
/// The XSD is generated from the element registry, so the parser, the schema
/// and the catalog read one list and cannot drift apart — pinned by tests, not by discipline.
///
public class XmlRouteSchemaTests
{
private static readonly ElementRegistry Registry = ElementRegistry.CreateDefault();
private static IReadOnlyList<(int Line, int Column, string Message)> Validate(string xml)
=> XmlRouteSchema.Validate(XDocument.Parse(xml, LoadOptions.SetLineInfo), Registry);
// ── the catalog-aware schema ─────────────────────────────────────────────
///
/// The shipped catalog XSD must COMPILE, or the editor binds a schema it cannot use. A
/// component whose path synonym is also one of its options (seda's queue, amqp's path)
/// declared that attribute twice — «the attribute 'queue' already exists», twelve times
/// over the real catalog (found before 4.1.0, 2026-09-20).
///
[Fact]
public void CatalogSchema_WithAPathSynonymThatIsAlsoAnOption_Compiles()
{
var component = new CatalogComponent(
"seda", [], "redb.Route", "redb.Route.Components.SedaEndpointOptions", "queue", false,
[
new CatalogOption("Queue", "string", null, false, null, Role: null, ConnectionParameter: false, ConnectionFactoryReference: false),
new CatalogOption("Size", "int", "1000", false, null, Role: null, ConnectionParameter: false, ConnectionFactoryReference: false),
], Lenient: false);
var schema = XmlRouteSchema.Generate(Registry, [component]).ToString();
var errors = new List();
var set = new System.Xml.Schema.XmlSchemaSet();
set.ValidationEventHandler += (_, e) => errors.Add(e.Message);
set.Add("urn:redb:route:1.0", System.Xml.XmlReader.Create(new StringReader(schema)));
set.Compile();
errors.Should().BeEmpty("a generated schema that does not compile is no schema at all");
}
// ── the one-list guarantee ───────────────────────────────────────────────
[Fact]
public void AttributeType_KeepsItsNumericValues_AcrossReleases()
{
// Package contributions (Cache, Http, redb.Route.Core, ...) are compiled against this enum
// and store the NUMBERS. Inserting a member in the middle shifts them: a released package's
// Bool would read as whatever took its slot. New members go to the end, never between.
((int)AttributeType.String).Should().Be(0);
((int)AttributeType.Expression).Should().Be(1);
((int)AttributeType.Uri).Should().Be(2);
((int)AttributeType.Reference).Should().Be(3);
((int)AttributeType.TypeName).Should().Be(4);
((int)AttributeType.Bool).Should().Be(5);
((int)AttributeType.Int).Should().Be(6);
((int)AttributeType.Long).Should().Be(7);
((int)AttributeType.Double).Should().Be(8);
((int)AttributeType.Duration).Should().Be(9);
((int)AttributeType.Enum).Should().Be(10);
((int)AttributeType.TypeNameList).Should().Be(11);
}
[Fact]
public void EveryCoreContribution_HasAPinnedSpec_WithMatchingNameAndKind()
{
foreach (var contribution in CoreContributions.All)
{
CoreContributions.Specs.Should().ContainKey(contribution.Name,
$"the core element <{contribution.Name}> must declare its shape for the schema and the catalog");
var spec = contribution.Spec;
spec.Name.Should().Be(contribution.Name);
spec.Kind.Should().Be(contribution.Kind,
$"<{contribution.Name}>: the registered kind and the spec kind must agree");
}
}
[Fact]
public void EverySpecEntry_BelongsToARegisteredContribution()
{
var registered = CoreContributions.All.Select(c => c.Name).ToHashSet(StringComparer.Ordinal);
CoreContributions.Specs.Keys.Should().OnlyContain(name => registered.Contains(name),
"a spec without a contribution is dead weight the schema would advertise falsely");
}
[Fact]
public void TheSchema_Compiles_AndGenerationIsDeterministic()
{
XmlRouteSchema.For(Registry).Should().NotBeNull();
XmlRouteSchema.Generate(Registry).ToString()
.Should().Be(XmlRouteSchema.Generate(Registry).ToString());
}
[Fact]
public void EnumOption_LongerThanThreeMembers_LosesTheCaseInsensitiveUnion()
{
// The union spells every member out as character classes, so a long enum costs thousands of
// characters: at the catalog's five hundred enum sites the schema grew to 21k lines, and the
// editor's language server died with an out-of-memory error on the first completion
// (2026-10-08). Short lists keep the union, long ones go back to the plain member list.
var component = new CatalogComponent(
"seda", [], "redb.Route", "redb.Route.Components.SedaEndpointOptions", "queue", false,
[
new CatalogOption("AckMode", "enum", "Manual", false, ["Manual", "Auto"],
Role: null, ConnectionParameter: false, ConnectionFactoryReference: false),
new CatalogOption("FileExist", "enum", "Override", false,
["Override", "Append", "Fail", "Ignore", "Move", "TryRename"],
Role: null, ConnectionParameter: false, ConnectionFactoryReference: false),
], Lenient: false);
var schema = XDocument.Parse(XmlRouteSchema.Generate(Registry, [component]).ToString());
var xs = schema.Root!.Name.Namespace;
XElement Option(string name) => schema.Descendants(xs + "attribute")
.First(a => (string?)a.Attribute("name") == name);
Option("ackMode").Descendants(xs + "union").Should().HaveCount(1,
"a two-member enum still gets the case-insensitive union");
Option("fileExist").Descendants(xs + "union").Should().BeEmpty();
Option("fileExist").Descendants(xs + "enumeration").Select(e => (string?)e.Attribute("value"))
.Should().Equal("Override", "Append", "Fail", "Ignore", "Move", "TryRename");
}
// ── validation behavior the phase promises ───────────────────────────────
[Fact]
public void EveryShippedExample_IsSchemaValid()
{
foreach (var file in Directory.GetFiles(Examples.ExampleHarness.Dir, "*.route.xml"))
{
var problems = XmlRouteSchema.Validate(
XDocument.Load(file, LoadOptions.SetLineInfo), Registry);
problems.Should().BeEmpty($"{Path.GetFileName(file)} must validate against the generated schema");
}
}
[Fact]
public void UnknownElement_FailsValidation()
{
var problems = Validate("""
""");
problems.Should().NotBeEmpty();
problems.Should().Contain(p => p.Message.Contains("nosuch"));
}
[Fact]
public void ForeignNamespaceAttribute_IsAccepted()
{
var problems = Validate("""
""");
problems.Should().BeEmpty("a foreign-namespace attribute is tolerated (anyAttribute ##other lax)");
}
[Fact]
public void EnumAttribute_OffersAndEnforcesItsValues()
{
var problems = Validate("""
text
""");
problems.Should().Contain(p => p.Message.Contains("Loud"),
"a log level outside the enumeration must fail, which is what powers editor value hints");
}
[Fact]
public void RequiredAttribute_IsEnforced()
{
var problems = Validate("""
""");
problems.Should().Contain(p => p.Message.Contains("type"));
}
[Fact]
public void StructuredEndpointChild_IsOpenContent_UntilTheCatalogArrives()
{
var problems = Validate("""
""");
problems.Should().BeEmpty("a scheme element inside an address step is lax content in the base schema");
}
[Fact]
public void ExternalContribution_AppearsInTheSchema_Automatically()
{
var registry = ElementRegistry.CreateDefault([new StampSpecContribution()]);
var problems = XmlRouteSchema.Validate(XDocument.Parse("""
""", LoadOptions.SetLineInfo), registry);
problems.Should().BeEmpty("a registered package element validates like a core one");
XmlRouteSchema.Validate(XDocument.Parse("""
""", LoadOptions.SetLineInfo), Registry)
.Should().NotBeEmpty("without the package, its element is rejected by the schema too");
}
// ── the §3.4 load-time pass: schema findings join the parse, deduped ─────
[Fact]
public void AttributeTypo_IsCaughtAtLoad_ByTheSchemaPass()
{
using var context = new Route.Core.RouteContext();
var act = () => context.AddXmlRoutesFromContent("""
""", "typo.xml");
act.Should().Throw()
.WithMessage("*typo.xml(4,*[schema]*bogus*",
"an unqualified attribute typo is exactly what the parser leaves to the schema");
}
[Fact]
public void ParserAndSchema_DoNotDoubleReportTheSameLine()
{
using var context = new Route.Core.RouteContext();
var act = () => context.AddXmlRoutesFromContent("""
""");
var errors = act.Should().Throw().Which.Errors;
errors.Should().ContainSingle("the parser's did-you-mean wins the line; the schema stays quiet on it")
.Which.Should().Contain("Did you mean ?").And.NotContain("[schema]");
}
[Fact]
public async Task TheSwitch_TurnsTheSchemaPassOff()
{
await using var context = new Route.Core.RouteContext();
var act = () => context.AddXmlRoutesFromContent("""
""", options: new XmlRouteLoaderOptions { ValidateAgainstSchema = false });
act.Should().NotThrow("a document carrying a newer schema's extensions may opt out");
}
// ── the §3.2 version policy on the namespace ─────────────────────────────
[Fact]
public void NewerMinorVersion_SaysWhatToUpdate()
{
using var context = new Route.Core.RouteContext();
var act = () => context.AddXmlRoutesFromContent(
"""""");
act.Should().Throw()
.WithMessage("*needs format 1.5*supports up to 1.0*update the redb.Route.Xml package*");
}
[Fact]
public void DifferentMajorVersion_IsADifferentFormat()
{
using var context = new Route.Core.RouteContext();
var act = () => context.AddXmlRoutesFromContent(
"""""");
act.Should().Throw()
.WithMessage("*format 2.0*major version 1*different format*");
}
private sealed class StampSpecContribution : IXmlElementContribution
{
public string Name => "stamp";
public XmlElementKind Kind => XmlElementKind.Step;
public ElementSpec Spec => ElementSpec.Leaf("stamp", new AttributeSpec("value", AttributeType.String));
public Abstractions.IRouteDefinition Apply(
XElement element, Abstractions.IRouteDefinition current, XmlParseContext context)
=> current.SetHeader("stamped", context.Attr(element, "value") ?? "yes");
}
}