From edc6cc8c365956bb429749011bce9b63d5757bd5 Mon Sep 17 00:00:00 2001 From: Doug Mealing Date: Thu, 3 Dec 2026 16:49:50 -0500 Subject: [PATCH] test(rust): run the provider-composition corpus in the Rust port Add metaobjects/tests/provider_composition_conformance.rs, which runs all 18 cases of fixtures/provider-composition-conformance/ through the canonical named providers: the 11 error-shape manifests (duplicate id, missing dependency, cycle, attr conflict, attr shadow, common-attr conflict and ERR_REGISTRY_SEALED), the 4 compose-load manifests (a consumer extend of view.currency survives a strict load and a typo or misplaced core attr is still refused) and the 3 attr-ownership manifests (ADR-0062 owners). The registry already implemented every rule; no library code changes. Mark Rust green for the corpus in docs/CONFORMANCE.md and docs/ports/rust.md. metamodelVersion and the registered vocabulary do not move. --- docs/CONFORMANCE.md | 4 +- docs/ports/rust.md | 1 + .../tests/provider_composition_conformance.rs | 393 ++++++++++++++++++ 3 files changed, 396 insertions(+), 2 deletions(-) create mode 100644 server/rust/metaobjects/tests/provider_composition_conformance.rs diff --git a/docs/CONFORMANCE.md b/docs/CONFORMANCE.md index a59c04be8..cb8647c4a 100644 --- a/docs/CONFORMANCE.md +++ b/docs/CONFORMANCE.md @@ -46,7 +46,7 @@ regenerate with `ls -d fixtures//*/ | wc -l` for directory-shaped corpor | [`fixtures/template-codegen-conformance/`](../fixtures/template-codegen-conformance/) | 3 + the `docs-view/` case (16 files: the docs-view scopes, `{path}`, `subDir`, assets and the link check) | ✓ (both) | ✓ (root case; `docs-view/` planned) | ✓ (root case; `docs-view/` planned) | ✓ (root case; `docs-view/` planned) | ✓ (root case; `docs-view/` planned) | — | | [`fixtures/template-output-render-conformance/`](../fixtures/template-output-render-conformance/) | 5 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ (generated `render-helper` crates) | | [`fixtures/generator-registry-conformance/`](../fixtures/generator-registry-conformance/) | 1 canonical manifest | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | -| [`fixtures/provider-composition-conformance/`](../fixtures/provider-composition-conformance/) | 18 (11 error-shape + 4 compose-load + 3 attr-ownership) | ✓ | ✓ | — (JVM registry via Java) | ✓ | ✓ | — | +| [`fixtures/provider-composition-conformance/`](../fixtures/provider-composition-conformance/) | 18 (11 error-shape + 4 compose-load + 3 attr-ownership) | ✓ | ✓ | — (JVM registry via Java) | ✓ | ✓ | ✓ | | [`fixtures/source-resolution-conformance/`](../fixtures/source-resolution-conformance/) | 30 cases | ✓ (reference implementation) | ✓ | inherits via Java | ✓ | ✓ | ✓ | | [`fixtures/reference-source-conformance/`](../fixtures/reference-source-conformance/) | 6 cases | ✓ (reference implementation) | ✓ | inherits via Java (plus its own codegen exclusion test) | ✓ | ✓ | ✓ | | [`fixtures/response-verify-conformance/`](../fixtures/response-verify-conformance/) | 7 cases | ✓ (reference implementation) | ✓ | inherits via Java | ✓ | ✓ | ✓ | @@ -423,7 +423,7 @@ Per-port runners + commands: | Kotlin | `cd server/java && mvn -pl codegen-kotlin test` (snapshot suite) | `mvn -f server/java/integration-tests-kotlin/pom.xml test` (needs Docker) | `mvn -f server/java/integration-tests-kotlin/pom.xml test -Dtest=ApiContractConformanceTest` (needs Docker) | | C# | `dotnet test` (per project) | `scripts/integration-test.sh csharp` (needs Docker) | `dotnet test server/csharp/MetaObjects.IntegrationTests/MetaObjects.IntegrationTests.csproj --filter "FullyQualifiedName~ApiContractConformanceTest"` (needs Docker) | | Python | `pytest` (per package) | `scripts/integration-test.sh python` (needs Docker) | `cd server/python && uv run --extra integration pytest tests/integration/test_api_contract.py` (needs Docker) | -| Rust | `cd server/rust && cargo test` (registry, metamodel, YAML, source-resolution, reference-source, scope, dependency, naming, skip, validation, wire-vector, writer, generator-registry, render, verify, response-verify, extract, output-prompt, template-output-render, list-query, persistence and api-contract corpora, and the `verify --templates` CLI test; the codegen-compile and scale gates, which run `cargo check` on generated crates and drive the generated client over HTTP against a local stub) | — | — | +| Rust | `cd server/rust && cargo test` (registry, metamodel, YAML, provider-composition, source-resolution, reference-source, scope, dependency, naming, skip, validation, wire-vector, writer, generator-registry, render, verify, response-verify, extract, output-prompt, template-output-render, list-query, persistence and api-contract corpora, and the `verify --templates` CLI test; the codegen-compile and scale gates, which run `cargo check` on generated crates and drive the generated client over HTTP against a local stub) | — | — | The `persistence-conformance` corpus is intentionally **on-demand** — none of the unit-test runners (`bun test`, `dotnet test`, `pytest`, `mvn test`) pull Docker. diff --git a/docs/ports/rust.md b/docs/ports/rust.md index e5e8d022a..f266c457d 100644 --- a/docs/ports/rust.md +++ b/docs/ports/rust.md @@ -654,6 +654,7 @@ outright. | `conformance` (the metamodel corpus) | **599 of 599**, and the ledger is empty | | `yaml-conformance` | all 19, and the ledger is empty | | `source-resolution-conformance` | all 30 cases | +| `provider-composition-conformance` | all 18 cases (11 error-shape, 4 compose-load, 3 attr-ownership): the provider error codes and `ERR_REGISTRY_SEALED` through `providers::compose_registry`, a consumer provider's `extend` of a core subtype surviving a strict load, and the owner `TypeRegistry::attr_owner` and `common_attr_owner` report for each attribute (ADR-0062), from the corpus's canonical named providers (`metaobjects/tests/provider_composition_conformance.rs`) | | `reference-source-conformance` | all 6 cases: a `reference: true` source loads so references resolve against it, a node declared only in reference files is a reference node no generator emits, and a `template.*` declared only there is refused (`ERR_REFERENCE_SOURCE_TEMPLATE`) | | `dependency-conformance` | the resolution arm, all 36 cases: a declared dependency loads from its committed snapshot (`.metaobjects/deps//`), verified against `.metaobjects/deps.lock.json` (a stale, missing or mismatched snapshot is `ERR_DEPENDENCY_SNAPSHOT_STALE`), its objects are left out of generation unless `scope.include` names their package, and a new node declared into its package is refused (`ERR_DEPENDENCY_PACKAGE_NOT_OWNED`). The 7 ledger cases are asserted through `dependencies::requirement_ledger`, since the port has no requirement gate yet. The check arm, all 11 cases, through `dependency_check::check_dependencies`, the engine behind `verify --deps` | | `scope-conformance` | all 10 cases, through `metaobjects::scope`, the matcher `migrate.scope` is read with | diff --git a/server/rust/metaobjects/tests/provider_composition_conformance.rs b/server/rust/metaobjects/tests/provider_composition_conformance.rs new file mode 100644 index 000000000..91aab5448 --- /dev/null +++ b/server/rust/metaobjects/tests/provider_composition_conformance.rs @@ -0,0 +1,393 @@ +//! The provider-composition conformance runner: the shared corpus at +//! `fixtures/provider-composition-conformance/` gates the registry/provider error codes +//! a metadata document cannot reach, because they come from how providers compose and +//! seal (ADR-0023, ADR-0062, #265). +//! +//! Three passes, one per manifest shape (the corpus README owns the contract): +//! - the flat directory: composing the named providers fails with `expectedError`, or, +//! for `sealThenRegister`, composing succeeds and the probe fails on the sealed registry; +//! - `compose-load/`: the library providers compose first, the named ones after, then the +//! registry is inspected (`expectAttrs`) and a document strict-loads (`expectErrors`); +//! - `attr-ownership/`: composition succeeds and the registry names each attribute's owner. +//! +//! The named providers are test-only and match every other port's id, dependencies and +//! registration exactly. + +use std::path::{Path, PathBuf}; + +use metaobjects::errors::MetaError; +use metaobjects::loader::Loader; +use metaobjects::providers::{Provider, compose_registry, core_providers}; +use metaobjects::registry::{AttrSchema, ChildRule, TypeDefinition, TypeRegistry}; +use serde_json::Value; + +const TEMPLATE: &str = "template"; +const BASE: &str = "base"; +/// The fresh template subtype the attr-conflict and seal providers use. +const CONFLICT_SUBTYPE: &str = "compositionprobe"; +const CONFLICT_ATTR: &str = "conflictAttr"; +/// The fresh template subtype the ADR-0062 ownership providers use. +const SHADOW_SUBTYPE: &str = "shadowprobe"; + +fn corpus_root() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../../../fixtures/provider-composition-conformance") +} + +fn string_attr(name: &str) -> AttrSchema { + AttrSchema::simple( + name, + Some("string"), + "Test-only provider-composition probe attr.", + ) +} + +fn template_type(sub_type: &str, attr: &str) -> TypeDefinition { + let mut def = TypeDefinition::new( + TEMPLATE, + sub_type, + "Test-only provider-composition probe type.", + ); + def.attributes.push(string_attr(attr)); + def +} + +fn register_template(r: &mut TypeRegistry, sub_type: &str, attr: &str) -> Result<(), MetaError> { + r.register(template_type(sub_type, attr)) +} + +fn register_common(r: &mut TypeRegistry, attr: &str) -> Result<(), MetaError> { + r.register_common_attrs(vec![string_attr(attr)]) +} + +fn noop(_: &mut TypeRegistry) -> Result<(), MetaError> { + Ok(()) +} + +fn attr_conflict_base(r: &mut TypeRegistry) -> Result<(), MetaError> { + let mut def = template_type(CONFLICT_SUBTYPE, CONFLICT_ATTR); + def.child_rules.push(ChildRule::wildcard("attr")); + r.register(def) +} + +fn attr_conflict_clash(r: &mut TypeRegistry) -> Result<(), MetaError> { + r.extend( + TEMPLATE, + CONFLICT_SUBTYPE, + vec![string_attr(CONFLICT_ATTR)], + Vec::new(), + ) +} + +fn seal_probe(r: &mut TypeRegistry) -> Result<(), MetaError> { + r.register(TypeDefinition::new( + TEMPLATE, + "sealprobe", + "Test-only seal probe.", + )) +} + +fn extend_spec_subtype(r: &mut TypeRegistry) -> Result<(), MetaError> { + r.extend( + "view", + "currency", + vec![AttrSchema::simple( + "decimals", + Some("int"), + "Test-only #265 compose-load probe attr.", + )], + Vec::new(), + ) +} + +fn shadow_base(r: &mut TypeRegistry) -> Result<(), MetaError> { + register_template(r, BASE, "shadowAttr") +} + +fn shadow_sub_clash(r: &mut TypeRegistry) -> Result<(), MetaError> { + register_template(r, SHADOW_SUBTYPE, "shadowAttr") +} + +fn shadow_sub_owner(r: &mut TypeRegistry) -> Result<(), MetaError> { + register_template(r, SHADOW_SUBTYPE, "reverseAttr") +} + +fn shadow_base_late(r: &mut TypeRegistry) -> Result<(), MetaError> { + register_template(r, BASE, "reverseAttr") +} + +fn shadow_sub_distinct(r: &mut TypeRegistry) -> Result<(), MetaError> { + register_template(r, SHADOW_SUBTYPE, "distinctAttr") +} + +fn shadow_self(r: &mut TypeRegistry) -> Result<(), MetaError> { + register_template(r, BASE, "selfAttr")?; + register_template(r, SHADOW_SUBTYPE, "selfAttr") +} + +fn shadow_sub_extend_clash(r: &mut TypeRegistry) -> Result<(), MetaError> { + r.extend( + TEMPLATE, + SHADOW_SUBTYPE, + vec![string_attr("shadowAttr")], + Vec::new(), + ) +} + +fn common_owner(r: &mut TypeRegistry) -> Result<(), MetaError> { + register_common(r, "commonAttr") +} + +fn common_self(r: &mut TypeRegistry) -> Result<(), MetaError> { + register_common(r, "selfCommonAttr")?; + register_template(r, SHADOW_SUBTYPE, "selfCommonAttr")?; + register_common(r, "selfCommonAttr") +} + +fn common_sub_clash(r: &mut TypeRegistry) -> Result<(), MetaError> { + register_template(r, SHADOW_SUBTYPE, "commonAttr") +} + +fn common_sub_owner(r: &mut TypeRegistry) -> Result<(), MetaError> { + register_template(r, SHADOW_SUBTYPE, "lateCommonAttr") +} + +fn common_late(r: &mut TypeRegistry) -> Result<(), MetaError> { + register_common(r, "lateCommonAttr") +} + +/// The canonical named-provider set, keyed by the name a manifest uses. The key and the +/// provider id differ only for `duplicate-x-clone`, whose id collides on purpose. +fn named_provider(name: &str) -> Provider { + let p = |id: &str, deps: &[&str], register: fn(&mut TypeRegistry) -> Result<(), MetaError>| { + Provider::new( + id, + deps, + "Test-only provider-composition named provider.", + register, + ) + }; + match name { + "duplicate-x" | "duplicate-x-clone" => p("duplicate-x", &[], noop), + "depends-on-missing" => p("depends-on-missing", &["does-not-exist"], noop), + "cycle-a" => p("cycle-a", &["cycle-b"], noop), + "cycle-b" => p("cycle-b", &["cycle-a"], noop), + "attr-conflict-base" => p("attr-conflict-base", &[], attr_conflict_base), + "attr-conflict-clash" => p( + "attr-conflict-clash", + &["attr-conflict-base"], + attr_conflict_clash, + ), + "seal-probe" => p("seal-probe", &[], seal_probe), + "extend-spec-subtype" => p("extend-spec-subtype", &[], extend_spec_subtype), + "shadow-base" => p("shadow-base", &[], shadow_base), + "shadow-sub-clash" => p("shadow-sub-clash", &["shadow-base"], shadow_sub_clash), + "shadow-sub-owner" => p("shadow-sub-owner", &[], shadow_sub_owner), + "shadow-base-late" => p("shadow-base-late", &["shadow-sub-owner"], shadow_base_late), + "shadow-sub-distinct" => p("shadow-sub-distinct", &["shadow-base"], shadow_sub_distinct), + "shadow-self" => p("shadow-self", &[], shadow_self), + "shadow-sub-extend-clash" => p( + "shadow-sub-extend-clash", + &["shadow-sub-distinct"], + shadow_sub_extend_clash, + ), + "common-owner" => p("common-owner", &[], common_owner), + "common-clash" => p("common-clash", &["common-owner"], common_owner), + "common-self" => p("common-self", &[], common_self), + "common-sub-clash" => p("common-sub-clash", &["common-owner"], common_sub_clash), + "common-sub-owner" => p("common-sub-owner", &[], common_sub_owner), + "common-late" => p("common-late", &["common-sub-owner"], common_late), + other => panic!("unknown named provider \"{other}\" in the provider-composition corpus"), + } +} + +fn manifests(dir: &Path) -> Vec<(String, Value)> { + let mut paths: Vec = std::fs::read_dir(dir) + .unwrap_or_else(|e| panic!("{}: {e}", dir.display())) + .map(|e| e.unwrap().path()) + .filter(|p| p.extension().is_some_and(|x| x == "json")) + .collect(); + paths.sort(); + assert!(!paths.is_empty(), "{} holds no manifests", dir.display()); + paths + .into_iter() + .map(|p| { + let name = p.file_name().unwrap().to_string_lossy().into_owned(); + let doc = serde_json::from_str(&std::fs::read_to_string(&p).unwrap()) + .unwrap_or_else(|e| panic!("{name}: {e}")); + (name, doc) + }) + .collect() +} + +fn str_list<'a>(manifest: &'a Value, key: &str) -> Vec<&'a str> { + manifest[key] + .as_array() + .map(|a| { + a.iter() + .map(|v| v.as_str().expect("a string entry")) + .collect() + }) + .unwrap_or_default() +} + +fn named_providers(manifest: &Value) -> Vec { + str_list(manifest, "providers") + .into_iter() + .map(named_provider) + .collect() +} + +#[test] +fn flat_corpus_surfaces_the_expected_error() { + let mut failures = Vec::new(); + let cases = manifests(&corpus_root()); + for (name, manifest) in &cases { + let expected = manifest["expectedError"].as_str().expect("expectedError"); + let providers = named_providers(manifest); + let actual = match manifest["sealThenRegister"].as_str() { + Some(probe) => match compose_registry(&providers) { + Err(e) => Err(format!("compose failed before the seal: {e:?}")), + Ok(mut registry) => { + registry.seal(); + let probe = named_provider(probe); + Ok((probe.register)(&mut registry).err().map(|e| e.code)) + } + }, + None => Ok(compose_registry(&providers).err().map(|e| e.code)), + }; + match actual { + Ok(Some(code)) if code == expected => {} + Ok(other) => failures.push(format!("{name}: expected {expected}, got {other:?}")), + Err(why) => failures.push(format!("{name}: {why}")), + } + } + assert!( + failures.is_empty(), + "{} of {} failed:\n{}", + failures.len(), + cases.len(), + failures.join("\n") + ); +} + +#[test] +fn compose_load_corpus_keeps_consumer_extensions() { + let mut failures = Vec::new(); + let cases = manifests(&corpus_root().join("compose-load")); + for (name, manifest) in &cases { + let mut providers = if manifest["composeWithCore"].as_bool() == Some(true) { + core_providers() + } else { + Vec::new() + }; + providers.extend(named_providers(manifest)); + let mut registry = match compose_registry(&providers) { + Ok(r) => r, + Err(e) => { + failures.push(format!("{name}: compose failed: {e:?}")); + continue; + } + }; + registry.seal(); + if let Some(expect) = manifest.get("expectAttrs") { + let (t, s) = ( + expect["type"].as_str().unwrap(), + expect["subType"].as_str().unwrap(), + ); + match registry.find(t, s) { + None => failures.push(format!("{name}: no registered {t}.{s}")), + Some(def) => { + for attr in str_list(expect, "contains") { + if def.attr(attr).is_none() { + failures.push(format!("{name}: {t}.{s} does not declare {attr}")); + } + } + } + } + } + if let Some(doc) = manifest.get("metadata") { + let sources = vec![(format!("{name}#metadata.json"), doc.to_string())]; + let loaded = Loader::new(®istry).strict(true).load_sources(&sources); + let mut actual: Vec = + loaded.errors.iter().map(|e| e.code.to_string()).collect(); + let mut expected: Vec = str_list(manifest, "expectErrors") + .into_iter() + .map(str::to_string) + .collect(); + actual.sort(); + expected.sort(); + if actual != expected { + failures.push(format!( + "{name}: expected errors {expected:?}, got {actual:?} ({:?})", + loaded.errors + )); + } + } + } + assert!( + failures.is_empty(), + "{} of {} failed:\n{}", + failures.len(), + cases.len(), + failures.join("\n") + ); +} + +#[test] +fn attr_ownership_corpus_names_each_owner() { + let mut failures = Vec::new(); + let cases = manifests(&corpus_root().join("attr-ownership")); + for (name, manifest) in &cases { + let owners = manifest["expectOwners"] + .as_array() + .cloned() + .unwrap_or_default(); + let common = manifest["expectCommonOwners"] + .as_array() + .cloned() + .unwrap_or_default(); + assert!( + !owners.is_empty() || !common.is_empty(), + "{name}: carries neither expectOwners nor expectCommonOwners" + ); + let registry = match compose_registry(&named_providers(manifest)) { + Ok(r) => r, + Err(e) => { + failures.push(format!("{name}: compose failed: {e:?}")); + continue; + } + }; + for o in &owners { + let (t, s, a) = ( + o["type"].as_str().unwrap(), + o["subType"].as_str().unwrap(), + o["attr"].as_str().unwrap(), + ); + let actual = registry.attr_owner(t, s, a); + if actual != o["owner"].as_str() { + failures.push(format!( + "{name}: owner of {t}.{s} @{a} is {actual:?}, expected {}", + o["owner"] + )); + } + } + for o in &common { + let a = o["attr"].as_str().unwrap(); + let actual = registry.common_attr_owner(a); + if actual != o["owner"].as_str() { + failures.push(format!( + "{name}: common owner of @{a} is {actual:?}, expected {}", + o["owner"] + )); + } + } + } + assert!( + failures.is_empty(), + "{} of {} failed:\n{}", + failures.len(), + cases.len(), + failures.join("\n") + ); +}