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 / crates / aien-closure / tests / common / mod.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 | //! Fixture helpers. Receipts are built and sealed with aien-proof's own API, so |
| 17 | //! every id in the store is a real canonical identity. |
| 18 | #![allow(dead_code)] |
| 19 | |
| 20 | use aien_closure::digest::source_digest; |
| 21 | use aien_closure::graph::{cargo_packages, graph_of}; |
| 22 | use aien_closure::manifest::{self, Manifest}; |
| 23 | use aien_closure::verify::{render_lock, LOCK_FILE}; |
| 24 | use aien_closure::{verify, Code, Options}; |
| 25 | use aien_proof::evidence::{ |
| 26 | receipt_id, store_receipt, Assertion, Mutation, Receipt, Tier, Verdict, SCHEMA, SCHEMA_VERSION, |
| 27 | }; |
| 28 | use std::collections::{BTreeMap, BTreeSet}; |
| 29 | use std::fs; |
| 30 | use std::path::{Path, PathBuf}; |
| 31 | use std::process::Command; |
| 32 | use std::sync::atomic::{AtomicUsize, Ordering}; |
| 33 | |
| 34 | static N: AtomicUsize = AtomicUsize::new(0); |
| 35 | |
| 36 | pub fn fixture_src() -> PathBuf { |
| 37 | Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/seeded") |
| 38 | } |
| 39 | |
| 40 | pub fn temp_dir(tag: &str) -> PathBuf { |
| 41 | let n = N.fetch_add(1, Ordering::SeqCst); |
| 42 | let dir = std::env::temp_dir().join(format!("aien-closure-{tag}-{}-{n}", std::process::id())); |
| 43 | let _ = fs::remove_dir_all(&dir); |
| 44 | fs::create_dir_all(&dir).unwrap(); |
| 45 | dir |
| 46 | } |
| 47 | |
| 48 | /// Copy the fixture sources only (no manifests, lock, or store). |
| 49 | pub fn copy_sources(src: &Path, dst: &Path) { |
| 50 | fs::create_dir_all(dst).unwrap(); |
| 51 | for e in fs::read_dir(src).unwrap() { |
| 52 | let e = e.unwrap(); |
| 53 | let name = e.file_name().to_string_lossy().into_owned(); |
| 54 | if matches!( |
| 55 | name.as_str(), |
| 56 | "closure.toml" | "closure.lock" | "store" | "target" |
| 57 | ) { |
| 58 | continue; |
| 59 | } |
| 60 | let to = dst.join(&name); |
| 61 | if e.file_type().unwrap().is_dir() { |
| 62 | copy_sources(&e.path(), &to); |
| 63 | } else { |
| 64 | fs::copy(e.path(), to).unwrap(); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// Knobs that change what the seeded receipts claim. |
| 70 | #[derive(Clone)] |
| 71 | pub struct Knobs { |
| 72 | pub gamma_dirty: bool, |
| 73 | pub gamma_tier: Tier, |
| 74 | pub alpha_profile: &'static str, |
| 75 | } |
| 76 | |
| 77 | impl Default for Knobs { |
| 78 | fn default() -> Self { |
| 79 | Knobs { |
| 80 | gamma_dirty: false, |
| 81 | gamma_tier: Tier::HostTest, |
| 82 | alpha_profile: "host-v1", |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | fn receipt_for(name: &str, source: &str, deps: Vec<String>, tier: Tier, dirty: bool) -> Receipt { |
| 88 | let mut r = Receipt { |
| 89 | schema: SCHEMA.to_string(), |
| 90 | version: SCHEMA_VERSION, |
| 91 | id: String::new(), |
| 92 | kind: "component-qualification".to_string(), |
| 93 | tier, |
| 94 | result: Verdict::Pass, |
| 95 | timestamp: 1_786_000_000, |
| 96 | repo: "https://github.com/aien-dev/aien-sovereign-core".to_string(), |
| 97 | commit: "a".repeat(40), |
| 98 | dirty, |
| 99 | toolchain: "rustc fixture".to_string(), |
| 100 | procedure: format!("cargo test -p {name}"), |
| 101 | machine: "host".to_string(), |
| 102 | env_class: tier.as_str().to_string(), |
| 103 | input_artifacts: vec![source.to_string()], |
| 104 | output_artifacts: vec![], |
| 105 | assertions: vec![Assertion { |
| 106 | id: "cargo_test_pass".to_string(), |
| 107 | expected: "pass".to_string(), |
| 108 | observed: "pass".to_string(), |
| 109 | pass: true, |
| 110 | source: "cargo".to_string(), |
| 111 | note: String::new(), |
| 112 | }], |
| 113 | dependencies: deps, |
| 114 | declared_mutation: Mutation::None, |
| 115 | observed_mutation: Mutation::None, |
| 116 | authority: String::new(), |
| 117 | output_digest: blake3::hash(name.as_bytes()).to_hex().to_string(), |
| 118 | external_refs: vec![], |
| 119 | ledger: None, |
| 120 | lease: None, |
| 121 | required_features: vec![], |
| 122 | reserved: String::new(), |
| 123 | }; |
| 124 | r.id = receipt_id(&r).unwrap(); |
| 125 | r |
| 126 | } |
| 127 | |
| 128 | /// Seed manifests, receipts and the lock for the gamma, beta, alpha fixture. |
| 129 | /// Returns component name -> receipt id. |
| 130 | pub fn seed(root: &Path, store: &Path, knobs: &Knobs) -> BTreeMap<String, String> { |
| 131 | let packages = cargo_packages(root).unwrap(); |
| 132 | let graph = graph_of(&packages); |
| 133 | let mut ids: BTreeMap<String, String> = BTreeMap::new(); |
| 134 | let mut manifests: BTreeMap<String, Manifest> = BTreeMap::new(); |
| 135 | for name in ["gamma", "beta", "alpha"] { |
| 136 | let pkg = packages.iter().find(|p| p.name == name).unwrap(); |
| 137 | let source = format!("blake3:{}", source_digest(&pkg.dir).unwrap()); |
| 138 | let (tier, dirty, profile) = match name { |
| 139 | "gamma" => (knobs.gamma_tier, knobs.gamma_dirty, "host-v1"), |
| 140 | "alpha" => (Tier::HostTest, false, knobs.alpha_profile), |
| 141 | _ => (Tier::HostTest, false, "host-v1"), |
| 142 | }; |
| 143 | let deps: BTreeMap<String, Option<String>> = pkg |
| 144 | .deps |
| 145 | .iter() |
| 146 | .map(|d| (d.clone(), Some(ids[d].clone()))) |
| 147 | .collect(); |
| 148 | let dep_ids = deps.values().flatten().cloned().collect(); |
| 149 | let r = receipt_for(name, &source, dep_ids, tier, dirty); |
| 150 | store_receipt(store, &r).unwrap(); |
| 151 | ids.insert(name.to_string(), r.id.clone()); |
| 152 | let m = Manifest { |
| 153 | component: name.to_string(), |
| 154 | profile: profile.to_string(), |
| 155 | receipt: r.id, |
| 156 | source, |
| 157 | deps, |
| 158 | }; |
| 159 | fs::write(pkg.dir.join(manifest::FILE_NAME), manifest::render(&m)).unwrap(); |
| 160 | manifests.insert(name.to_string(), m); |
| 161 | } |
| 162 | fs::write(root.join(LOCK_FILE), render_lock(&manifests, &graph)).unwrap(); |
| 163 | ids |
| 164 | } |
| 165 | |
| 166 | /// Seed into a fresh temp copy of the fixture. Returns (root, store, ids). |
| 167 | pub fn seeded(knobs: &Knobs) -> (PathBuf, PathBuf, BTreeMap<String, String>) { |
| 168 | let root = temp_dir("ws"); |
| 169 | copy_sources(&fixture_src(), &root); |
| 170 | let store = root.join("store"); |
| 171 | let ids = seed(&root, &store, knobs); |
| 172 | (root, store, ids) |
| 173 | } |
| 174 | |
| 175 | pub fn write(root: &Path, rel: &str, text: &str) { |
| 176 | fs::write(root.join(rel), text).unwrap(); |
| 177 | } |
| 178 | |
| 179 | pub fn read(root: &Path, rel: &str) -> String { |
| 180 | fs::read_to_string(root.join(rel)).unwrap() |
| 181 | } |
| 182 | |
| 183 | pub fn run(root: &Path, store: &Path) -> aien_closure::Outcome { |
| 184 | verify(&Options { |
| 185 | root: root.to_path_buf(), |
| 186 | store: store.to_path_buf(), |
| 187 | write_lock: false, |
| 188 | }) |
| 189 | .unwrap() |
| 190 | } |
| 191 | |
| 192 | pub fn codes(root: &Path, store: &Path) -> BTreeSet<Code> { |
| 193 | let out = run(root, store); |
| 194 | for f in &out.findings { |
| 195 | eprintln!("{}", f.line()); |
| 196 | } |
| 197 | out.findings.iter().map(|f| f.code).collect() |
| 198 | } |
| 199 | |
| 200 | /// Run the real binary and return (exit code, stdout). |
| 201 | pub fn bin(root: &Path, store: &Path) -> (i32, String) { |
| 202 | let o = Command::new(env!("CARGO_BIN_EXE_verify-closure")) |
| 203 | .args([ |
| 204 | "--root", |
| 205 | root.to_str().unwrap(), |
| 206 | "--store", |
| 207 | store.to_str().unwrap(), |
| 208 | ]) |
| 209 | .output() |
| 210 | .unwrap(); |
| 211 | ( |
| 212 | o.status.code().unwrap(), |
| 213 | String::from_utf8_lossy(&o.stdout).into_owned(), |
| 214 | ) |
| 215 | } |
| 216 | |
| 217 | /// The case must produce exactly `want`, and the binary must exit with the |
| 218 | /// matching code and print one FINDING line per finding. |
| 219 | pub fn expect_only(root: &Path, store: &Path, want: Code) { |
| 220 | assert_eq!(codes(root, store), BTreeSet::from([want]), "wrong findings"); |
| 221 | let (exit, stdout) = bin(root, store); |
| 222 | assert_eq!(exit, want.exit_code(), "wrong exit code:\n{stdout}"); |
| 223 | assert!( |
| 224 | stdout.contains(&format!("FINDING {} ", want.as_str())), |
| 225 | "{stdout}" |
| 226 | ); |
| 227 | assert!(stdout.contains("SUMMARY status=FAIL"), "{stdout}"); |
| 228 | } |