aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / tools / aien-test / tests / committed_gates.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Every `*.gate` manifest in the repository must parse under the strict |
| 17 | //! Slice A grammar (ADR 0028 Decision 3), and gate identifiers must be unique |
| 18 | //! (Decision 2, rule D2). |
| 19 | //! |
| 20 | //! `scan` finds gates the way the runner does (`aien_test::runner::discover`, |
| 21 | //! rule D1) and parses each one with `aien_test::manifest::parse`. The first |
| 22 | //! test runs it over this repository. The other tests run it over small |
| 23 | //! throwaway trees to show that a mistyped key, a duplicate gate id or a walk |
| 24 | //! that finds nothing cannot pass unnoticed. That last part is what the |
| 25 | //! mutants `discover_ignores_gate_files`, `discover_not_recursive` and |
| 26 | //! `unknown_child_key_allowed` in `mutations/aien-test.mutants` attack. |
| 27 | |
| 28 | use aien_test::manifest::{self, Manifest}; |
| 29 | use aien_test::runner::discover; |
| 30 | use std::collections::BTreeMap; |
| 31 | use std::fs; |
| 32 | use std::path::{Path, PathBuf}; |
| 33 | use std::sync::atomic::{AtomicU64, Ordering}; |
| 34 | |
| 35 | struct Scan { |
| 36 | parsed: Vec<(String, Manifest)>, |
| 37 | errors: Vec<String>, |
| 38 | } |
| 39 | |
| 40 | /// Parse every gate file under `root`. Each unparsable file and each gate id |
| 41 | /// that appears in more than one file adds an entry to `errors`; paths are |
| 42 | /// shown relative to `root`. |
| 43 | fn scan(root: &Path) -> Scan { |
| 44 | let mut parsed: Vec<(String, Manifest)> = Vec::new(); |
| 45 | let mut errors: Vec<String> = Vec::new(); |
| 46 | for path in discover(root) { |
| 47 | let shown = path |
| 48 | .strip_prefix(root) |
| 49 | .unwrap_or(&path) |
| 50 | .display() |
| 51 | .to_string(); |
| 52 | match fs::read(path) { |
| 53 | Err(e) => errors.push(format!("{shown}: cannot read: {e}")), |
| 54 | Ok(bytes) => match manifest::parse(&bytes) { |
| 55 | Ok(m) => parsed.push((shown, m)), |
| 56 | Err(e) => errors.push(format!("{shown}: BAD_MANIFEST {e}")), |
| 57 | }, |
| 58 | } |
| 59 | } |
| 60 | let mut by_id: BTreeMap<&str, Vec<&str>> = BTreeMap::new(); |
| 61 | for (shown, m) in &parsed { |
| 62 | by_id |
| 63 | .entry(m.gate.as_str()) |
| 64 | .or_default() |
| 65 | .push(shown.as_str()); |
| 66 | } |
| 67 | for (id, files) in &by_id { |
| 68 | if files.len() > 1 { |
| 69 | let list = files.join(", "); |
| 70 | errors.push(format!("duplicate gate id {id} in: {list}")); |
| 71 | } |
| 72 | } |
| 73 | Scan { parsed, errors } |
| 74 | } |
| 75 | |
| 76 | /// `tools/aien-test` sits two levels below the repository root. |
| 77 | fn repo_root() -> PathBuf { |
| 78 | let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../.."); |
| 79 | fs::canonicalize(root).expect("repository root") |
| 80 | } |
| 81 | |
| 82 | static COUNTER: AtomicU64 = AtomicU64::new(0); |
| 83 | |
| 84 | /// A throwaway directory tree, removed on drop. |
| 85 | struct TempDir(PathBuf); |
| 86 | |
| 87 | impl TempDir { |
| 88 | fn new(tag: &str) -> TempDir { |
| 89 | let n = COUNTER.fetch_add(1, Ordering::SeqCst); |
| 90 | let pid = std::process::id(); |
| 91 | let path = std::env::temp_dir().join(format!("aien-test-gates-{tag}-{pid}-{n}")); |
| 92 | fs::create_dir_all(&path).expect("create temp dir"); |
| 93 | TempDir(fs::canonicalize(path).expect("canonicalize temp dir")) |
| 94 | } |
| 95 | |
| 96 | fn path(&self) -> &Path { |
| 97 | &self.0 |
| 98 | } |
| 99 | |
| 100 | fn write(&self, rel: &str, content: &str) { |
| 101 | let p = self.0.join(rel); |
| 102 | fs::create_dir_all(p.parent().expect("parent dir")).expect("create parent dir"); |
| 103 | fs::write(p, content).expect("write fixture"); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl Drop for TempDir { |
| 108 | fn drop(&mut self) { |
| 109 | let _ = fs::remove_dir_all(&self.0); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// The smallest valid manifest, with the gate id `FIXTURE-A` replaced. |
| 114 | fn fixture(gate: &str) -> String { |
| 115 | concat!( |
| 116 | "gate: FIXTURE-A\n", |
| 117 | "manifest_version: 1\n", |
| 118 | "owner: fixture\n", |
| 119 | "requires:\n", |
| 120 | " - host\n", |
| 121 | "run:\n", |
| 122 | " exec: x.sh\n", |
| 123 | "expects:\n", |
| 124 | " verdict: PASS\n", |
| 125 | "timeout: 5s\n", |
| 126 | ) |
| 127 | .replace("FIXTURE-A", gate) |
| 128 | } |
| 129 | |
| 130 | #[test] |
| 131 | fn every_committed_gate_parses() { |
| 132 | let root = repo_root(); |
| 133 | let s = scan(&root); |
| 134 | let problems = s.errors.join("\n"); |
| 135 | assert!( |
| 136 | s.errors.is_empty(), |
| 137 | "gate manifests that do not parse or collide:\n{problems}" |
| 138 | ); |
| 139 | // A real checkout (it has crates/) carries gates; finding none means the |
| 140 | // walk is broken, not that everything is fine. |
| 141 | if root.join("crates").is_dir() { |
| 142 | let shown = root.display(); |
| 143 | assert!( |
| 144 | !s.parsed.is_empty(), |
| 145 | "no .gate file found under {shown}; the discovery walk found nothing" |
| 146 | ); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | #[test] |
| 151 | fn a_typo_in_a_top_level_key_is_reported() { |
| 152 | let t = TempDir::new("typo"); |
| 153 | t.write("tests/a/good.gate", &fixture("FIXTURE-GOOD")); |
| 154 | t.write( |
| 155 | "tests/b/typo.gate", |
| 156 | &fixture("FIXTURE-TYPO").replace("manifest_version", "manifest_versoin"), |
| 157 | ); |
| 158 | let s = scan(t.path()); |
| 159 | let errors = format!("{:?}", s.errors); |
| 160 | assert_eq!(s.parsed.len(), 1, "the good gate must be found: {errors}"); |
| 161 | assert_eq!(s.errors.len(), 1, "exactly the typo gate fails: {errors}"); |
| 162 | assert!(s.errors[0].contains("tests/b/typo.gate"), "{errors}"); |
| 163 | assert!( |
| 164 | s.errors[0].contains("unknown key manifest_versoin"), |
| 165 | "{errors}" |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | #[test] |
| 170 | fn a_typo_in_a_child_key_is_reported() { |
| 171 | let t = TempDir::new("child"); |
| 172 | t.write( |
| 173 | "tests/a/typo.gate", |
| 174 | &fixture("FIXTURE-CHILD").replace(" exec: x.sh", " exce: x.sh"), |
| 175 | ); |
| 176 | let s = scan(t.path()); |
| 177 | let errors = format!("{:?}", s.errors); |
| 178 | assert!(s.parsed.is_empty(), "{errors}"); |
| 179 | assert_eq!(s.errors.len(), 1, "{errors}"); |
| 180 | assert!( |
| 181 | s.errors[0].contains("unknown child key exce under run"), |
| 182 | "{errors}" |
| 183 | ); |
| 184 | } |
| 185 | |
| 186 | #[test] |
| 187 | fn a_duplicate_gate_id_is_reported_for_both_files() { |
| 188 | let t = TempDir::new("dup"); |
| 189 | t.write("tests/a/one.gate", &fixture("FIXTURE-DUP")); |
| 190 | t.write("tests/b/two.gate", &fixture("FIXTURE-DUP")); |
| 191 | let s = scan(t.path()); |
| 192 | let errors = format!("{:?}", s.errors); |
| 193 | assert_eq!(s.errors.len(), 1, "{errors}"); |
| 194 | assert!( |
| 195 | s.errors[0].contains("duplicate gate id FIXTURE-DUP"), |
| 196 | "{errors}" |
| 197 | ); |
| 198 | assert!(s.errors[0].contains("tests/a/one.gate"), "{errors}"); |
| 199 | assert!(s.errors[0].contains("tests/b/two.gate"), "{errors}"); |
| 200 | } |
| 201 | |
| 202 | #[test] |
| 203 | fn build_and_ignored_directories_are_not_scanned() { |
| 204 | let t = TempDir::new("skip"); |
| 205 | t.write("tests/a/good.gate", &fixture("FIXTURE-GOOD")); |
| 206 | t.write("target/skipped.gate", "this is not a manifest\n"); |
| 207 | t.write("vendored/.aien-test-ignore", ""); |
| 208 | t.write("vendored/bad.gate", "this is not a manifest\n"); |
| 209 | let s = scan(t.path()); |
| 210 | let errors = format!("{:?}", s.errors); |
| 211 | assert!(s.errors.is_empty(), "{errors}"); |
| 212 | assert_eq!(s.parsed.len(), 1, "{errors}"); |
| 213 | assert_eq!(s.parsed[0].1.gate, "FIXTURE-GOOD"); |
| 214 | } |