|
| 1 | +// ProviderCompositionConformanceTests — C# port runner. |
| 2 | +// |
| 3 | +// Five registry/provider error codes are Tier-1 cross-port invariants that the |
| 4 | +// metadata-input -> error corpus cannot reach: they are triggered by HOW |
| 5 | +// providers are composed and sealed, not by any metadata document. This runner |
| 6 | +// gates them from the shared corpus at fixtures/provider-composition-conformance/. |
| 7 | +// |
| 8 | +// Each port supplies the SAME canonical named-provider set (see the corpus |
| 9 | +// README). A manifest names providers by id; the runner maps names -> provider |
| 10 | +// objects, composes, and asserts the surfaced Code. The registry-sealed scenario |
| 11 | +// composes, seals, then runs a probe provider's RegisterTypes against the sealed |
| 12 | +// registry. |
| 13 | + |
| 14 | +using System.Text.Json; |
| 15 | +using MetaObjects; |
| 16 | +using MetaObjects.Meta; |
| 17 | +using MetaObjects.Shared; |
| 18 | +using Xunit; |
| 19 | + |
| 20 | +namespace MetaObjects.Conformance.Tests; |
| 21 | + |
| 22 | +// --- Canonical named-provider set (test-only; identical id/deps/behavior cross-port) --- |
| 23 | + |
| 24 | +/// <summary>Registers a fresh test-only type carrying a single string attr.</summary> |
| 25 | +internal sealed class AttrConflictBaseProvider : IMetaDataTypeProvider |
| 26 | +{ |
| 27 | + public const string ConflictSubType = "compositionprobe"; |
| 28 | + public const string ConflictAttr = "conflictAttr"; |
| 29 | + |
| 30 | + public string Id => "attr-conflict-base"; |
| 31 | + public IReadOnlyList<string> Dependencies => System.Array.Empty<string>(); |
| 32 | + |
| 33 | + public void RegisterTypes(TypeRegistry registry) |
| 34 | + { |
| 35 | + registry.Register(new TypeDefinition( |
| 36 | + typeId: new TypeId("template", ConflictSubType), |
| 37 | + description: "Test-only — provider-composition conflict probe.", |
| 38 | + childRules: new List<ChildRule> |
| 39 | + { |
| 40 | + new ChildRule( |
| 41 | + BaseTypes.TYPE_ATTR, |
| 42 | + Structural.CHILD_RULE_WILDCARD, |
| 43 | + Structural.CHILD_RULE_WILDCARD), |
| 44 | + }, |
| 45 | + factory: (typeId, name) => new MetaTemplate(typeId, name), |
| 46 | + attributes: new List<AttrSchema> |
| 47 | + { |
| 48 | + new AttrSchema(ConflictAttr, "string", Required: false, Description: "Conflict probe attr."), |
| 49 | + })); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/// <summary>Extends the base's type, redefining the same attr name -> attr conflict.</summary> |
| 54 | +internal sealed class AttrConflictClashProvider : IMetaDataTypeProvider |
| 55 | +{ |
| 56 | + public string Id => "attr-conflict-clash"; |
| 57 | + public IReadOnlyList<string> Dependencies => new[] { "attr-conflict-base" }; |
| 58 | + |
| 59 | + public void RegisterTypes(TypeRegistry registry) |
| 60 | + { |
| 61 | + registry.Extend( |
| 62 | + "template", |
| 63 | + AttrConflictBaseProvider.ConflictSubType, |
| 64 | + attributes: new List<AttrSchema> |
| 65 | + { |
| 66 | + new AttrSchema(AttrConflictBaseProvider.ConflictAttr, "string", Required: false, Description: "Redefined — collides."), |
| 67 | + }); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/// <summary>Attempts a mutating registration — throws against a sealed registry.</summary> |
| 72 | +internal sealed class SealProbeProvider : IMetaDataTypeProvider |
| 73 | +{ |
| 74 | + public string Id => "seal-probe"; |
| 75 | + public IReadOnlyList<string> Dependencies => System.Array.Empty<string>(); |
| 76 | + |
| 77 | + public void RegisterTypes(TypeRegistry registry) |
| 78 | + { |
| 79 | + registry.Register(new TypeDefinition( |
| 80 | + typeId: new TypeId("template", "sealprobe"), |
| 81 | + description: "Test-only — sealed-registry mutation probe.", |
| 82 | + childRules: new List<ChildRule>(), |
| 83 | + factory: (typeId, name) => new MetaTemplate(typeId, name), |
| 84 | + attributes: new List<AttrSchema>())); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +public sealed class ProviderCompositionConformanceTests |
| 89 | +{ |
| 90 | + private static readonly IReadOnlyDictionary<string, IMetaDataTypeProvider> Providers = |
| 91 | + new Dictionary<string, IMetaDataTypeProvider>(System.StringComparer.Ordinal) |
| 92 | + { |
| 93 | + ["duplicate-x"] = new NoopTestProvider("duplicate-x"), |
| 94 | + // Same Id as duplicate-x — surfaces ERR_PROVIDER_DUPLICATE_ID at compose time. |
| 95 | + ["duplicate-x-clone"] = new NoopTestProvider("duplicate-x"), |
| 96 | + ["depends-on-missing"] = new NoopTestProvider("depends-on-missing", "does-not-exist"), |
| 97 | + ["cycle-a"] = new NoopTestProvider("cycle-a", "cycle-b"), |
| 98 | + ["cycle-b"] = new NoopTestProvider("cycle-b", "cycle-a"), |
| 99 | + ["attr-conflict-base"] = new AttrConflictBaseProvider(), |
| 100 | + ["attr-conflict-clash"] = new AttrConflictClashProvider(), |
| 101 | + ["seal-probe"] = new SealProbeProvider(), |
| 102 | + }; |
| 103 | + |
| 104 | + private sealed record Manifest( |
| 105 | + string[] Providers, |
| 106 | + string ExpectedError, |
| 107 | + string? SealThenRegister); |
| 108 | + |
| 109 | + private static string CorpusRootPath() |
| 110 | + { |
| 111 | + var env = System.Environment.GetEnvironmentVariable("METAOBJECTS_PROVIDER_COMPOSITION_CORPUS"); |
| 112 | + if (!string.IsNullOrEmpty(env) && Directory.Exists(env)) return env; |
| 113 | + |
| 114 | + var dir = new DirectoryInfo(AppContext.BaseDirectory); |
| 115 | + while (dir != null) |
| 116 | + { |
| 117 | + var candidate = Path.Combine(dir.FullName, "fixtures", "provider-composition-conformance"); |
| 118 | + if (Directory.Exists(candidate)) return candidate; |
| 119 | + dir = dir.Parent; |
| 120 | + } |
| 121 | + throw new DirectoryNotFoundException( |
| 122 | + "could not locate fixtures/provider-composition-conformance from " + AppContext.BaseDirectory); |
| 123 | + } |
| 124 | + |
| 125 | + public static IEnumerable<object[]> ManifestFiles() |
| 126 | + { |
| 127 | + foreach (var file in Directory.GetFiles(CorpusRootPath(), "*.json").OrderBy(f => f, System.StringComparer.Ordinal)) |
| 128 | + yield return new object[] { Path.GetFileName(file) }; |
| 129 | + } |
| 130 | + |
| 131 | + private static IMetaDataTypeProvider Resolve(string id) => |
| 132 | + Providers.TryGetValue(id, out var p) |
| 133 | + ? p |
| 134 | + : throw new System.ArgumentException($"Unknown named provider \"{id}\" in provider-composition corpus"); |
| 135 | + |
| 136 | + [Fact] |
| 137 | + public void CorpusIsNonEmpty() |
| 138 | + { |
| 139 | + Assert.NotEmpty(ManifestFiles()); |
| 140 | + } |
| 141 | + |
| 142 | + [Theory] |
| 143 | + [MemberData(nameof(ManifestFiles))] |
| 144 | + public void ProviderComposition(string fileName) |
| 145 | + { |
| 146 | + var opts = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; |
| 147 | + var manifest = JsonSerializer.Deserialize<Manifest>( |
| 148 | + File.ReadAllText(Path.Combine(CorpusRootPath(), fileName)), opts)!; |
| 149 | + var resolved = manifest.Providers.Select(Resolve).ToList(); |
| 150 | + |
| 151 | + if (manifest.SealThenRegister != null) |
| 152 | + { |
| 153 | + // Compose (must succeed), seal, then run the probe against the sealed registry. |
| 154 | + var registry = Provider.ComposeRegistry(resolved); |
| 155 | + registry.Seal(); |
| 156 | + var probe = Resolve(manifest.SealThenRegister); |
| 157 | + var sealedEx = Assert.Throws<MetaModelException>(() => probe.RegisterTypes(registry)); |
| 158 | + Assert.Equal(manifest.ExpectedError, sealedEx.Code.ToString()); |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + // Ordinary scenario: the compose call itself throws. |
| 163 | + var ex = Assert.Throws<MetaModelException>(() => Provider.ComposeRegistry(resolved)); |
| 164 | + Assert.Equal(manifest.ExpectedError, ex.Code.ToString()); |
| 165 | + } |
| 166 | +} |
0 commit comments