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 / closure.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 | mod common; |
| 17 | |
| 18 | use aien_closure::error::ALL; |
| 19 | use aien_closure::Code; |
| 20 | use common::*; |
| 21 | use std::collections::BTreeSet; |
| 22 | use std::process::Command; |
| 23 | |
| 24 | #[test] |
| 25 | fn pass_case() { |
| 26 | let (root, store, ids) = seeded(&Knobs::default()); |
| 27 | let out = run(&root, &store); |
| 28 | assert!(out.ok(), "{:?}", out.findings); |
| 29 | assert_eq!(out.components, 3); |
| 30 | assert_eq!(out.passed.len(), 3); |
| 31 | let (exit, stdout) = bin(&root, &store); |
| 32 | assert_eq!(exit, 0, "{stdout}"); |
| 33 | assert!(stdout.contains("SUMMARY status=PASS findings=0 components=3")); |
| 34 | assert!(stdout.contains(&format!("PASS component=alpha receipt={}", ids["alpha"]))); |
| 35 | // closure.lock lists the transitive closure with ids. |
| 36 | let lock = read(&root, "closure.lock"); |
| 37 | assert!( |
| 38 | lock.contains(&format!("needs alpha gamma {}", ids["gamma"])), |
| 39 | "{lock}" |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | #[test] |
| 44 | fn committed_seeded_fixture_passes() { |
| 45 | let root = fixture_src(); |
| 46 | let out = run(&root, &root.join("store")); |
| 47 | assert!(out.ok(), "{:?}", out.findings); |
| 48 | assert_eq!(out.components, 3); |
| 49 | } |
| 50 | |
| 51 | /// `cargo test -- --ignored regenerate_committed_fixture` rewrites the |
| 52 | /// committed manifests, lock and store. Deterministic: same sources, same bytes. |
| 53 | #[test] |
| 54 | #[ignore] |
| 55 | fn regenerate_committed_fixture() { |
| 56 | let root = fixture_src(); |
| 57 | let store = root.join("store"); |
| 58 | let _ = std::fs::remove_dir_all(&store); |
| 59 | seed(&root, &store, &Knobs::default()); |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn unverified_dependency() { |
| 64 | // beta depends on gamma, but gamma carries no manifest. |
| 65 | let (root, store, _) = seeded(&Knobs::default()); |
| 66 | std::fs::remove_file(root.join("crates/gamma/closure.toml")).unwrap(); |
| 67 | expect_only(&root, &store, Code::UnverifiedDependency); |
| 68 | } |
| 69 | |
| 70 | #[test] |
| 71 | fn missing_receipt() { |
| 72 | let (root, store, ids) = seeded(&Knobs::default()); |
| 73 | std::fs::remove_file( |
| 74 | store |
| 75 | .join("receipts") |
| 76 | .join(format!("{}.json", ids["alpha"])), |
| 77 | ) |
| 78 | .unwrap(); |
| 79 | expect_only(&root, &store, Code::MissingReceipt); |
| 80 | } |
| 81 | |
| 82 | #[test] |
| 83 | fn receipt_hash_mismatch() { |
| 84 | // Same file name, different bytes: the content no longer hashes to its id. |
| 85 | let (root, store, ids) = seeded(&Knobs::default()); |
| 86 | let p = store |
| 87 | .join("receipts") |
| 88 | .join(format!("{}.json", ids["gamma"])); |
| 89 | let text = std::fs::read_to_string(&p).unwrap(); |
| 90 | assert!(text.contains("1786000000")); |
| 91 | std::fs::write(&p, text.replace("1786000000", "1786000001")).unwrap(); |
| 92 | let got = codes(&root, &store); |
| 93 | assert!(got.contains(&Code::ReceiptHashMismatch), "{got:?}"); |
| 94 | assert_eq!(got, BTreeSet::from([Code::ReceiptHashMismatch])); |
| 95 | assert_eq!(bin(&root, &store).0, Code::ReceiptHashMismatch.exit_code()); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn dependency_not_pinned() { |
| 100 | let (root, store, ids) = seeded(&Knobs::default()); |
| 101 | let m = read(&root, "crates/alpha/closure.toml"); |
| 102 | write( |
| 103 | &root, |
| 104 | "crates/alpha/closure.toml", |
| 105 | &m.replace(&format!("dep beta {}", ids["beta"]), "dep beta"), |
| 106 | ); |
| 107 | expect_only(&root, &store, Code::DependencyNotPinned); |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn dependency_not_pinned_when_lock_is_stale() { |
| 112 | let (root, store, _) = seeded(&Knobs::default()); |
| 113 | let lock = read(&root, "closure.lock"); |
| 114 | write( |
| 115 | &root, |
| 116 | "closure.lock", |
| 117 | &lock.replace("needs alpha gamma", "needs alpha beta"), |
| 118 | ); |
| 119 | expect_only(&root, &store, Code::DependencyNotPinned); |
| 120 | std::fs::remove_file(root.join("closure.lock")).unwrap(); |
| 121 | expect_only(&root, &store, Code::DependencyNotPinned); |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn dependency_cycle() { |
| 126 | // gamma now depends on alpha: alpha -> beta -> gamma -> alpha. |
| 127 | let (root, store, _) = seeded(&Knobs::default()); |
| 128 | let m = read(&root, "crates/gamma/Cargo.toml"); |
| 129 | write( |
| 130 | &root, |
| 131 | "crates/gamma/Cargo.toml", |
| 132 | &m.replace( |
| 133 | "[dependencies]", |
| 134 | "[dependencies]\nalpha = { path = \"../alpha\" }", |
| 135 | ), |
| 136 | ); |
| 137 | let got = codes(&root, &store); |
| 138 | assert!(got.contains(&Code::DependencyCycle), "{got:?}"); |
| 139 | assert_eq!(bin(&root, &store).0, Code::DependencyCycle.exit_code()); |
| 140 | } |
| 141 | |
| 142 | #[test] |
| 143 | fn stale_receipt() { |
| 144 | // Source changes after the receipt was taken. |
| 145 | let (root, store, _) = seeded(&Knobs::default()); |
| 146 | write( |
| 147 | &root, |
| 148 | "crates/gamma/src/lib.rs", |
| 149 | "pub fn gamma() -> u32 { 4 }\n", |
| 150 | ); |
| 151 | let got = codes(&root, &store); |
| 152 | assert_eq!(got, BTreeSet::from([Code::StaleReceipt])); |
| 153 | assert_eq!(bin(&root, &store).0, Code::StaleReceipt.exit_code()); |
| 154 | } |
| 155 | |
| 156 | #[test] |
| 157 | fn stale_receipt_when_receipt_records_other_source() { |
| 158 | // The manifest digest is current but the receipt was taken at a different one. |
| 159 | let (root, store, ids) = seeded(&Knobs::default()); |
| 160 | let p = store |
| 161 | .join("receipts") |
| 162 | .join(format!("{}.json", ids["gamma"])); |
| 163 | let text = std::fs::read_to_string(&p).unwrap(); |
| 164 | let r: aien_proof::evidence::Receipt = serde_json::from_str(&text).unwrap(); |
| 165 | let mut other = r.clone(); |
| 166 | other.input_artifacts = vec![format!("blake3:{}", "d".repeat(64))]; |
| 167 | other.id = aien_proof::evidence::receipt_id(&other).unwrap(); |
| 168 | aien_proof::evidence::store_receipt(&store, &other).unwrap(); |
| 169 | let m = read(&root, "crates/gamma/closure.toml"); |
| 170 | write( |
| 171 | &root, |
| 172 | "crates/gamma/closure.toml", |
| 173 | &m.replace(&ids["gamma"], &other.id), |
| 174 | ); |
| 175 | let got = codes(&root, &store); |
| 176 | assert!(got.contains(&Code::StaleReceipt), "{got:?}"); |
| 177 | } |
| 178 | |
| 179 | #[test] |
| 180 | fn undeclared_import() { |
| 181 | // alpha gains a direct edge to gamma that its manifest does not declare. |
| 182 | let (root, store, _) = seeded(&Knobs::default()); |
| 183 | let m = read(&root, "crates/alpha/Cargo.toml"); |
| 184 | write( |
| 185 | &root, |
| 186 | "crates/alpha/Cargo.toml", |
| 187 | &m.replace( |
| 188 | "[dependencies]", |
| 189 | "[dependencies]\ngamma = { path = \"../gamma\" }", |
| 190 | ), |
| 191 | ); |
| 192 | // Re-seed against the new graph, then drop the declaration again, so the |
| 193 | // undeclared edge is the only thing wrong. |
| 194 | seed(&root, &store, &Knobs::default()); |
| 195 | let man = read(&root, "crates/alpha/closure.toml"); |
| 196 | let kept: String = man |
| 197 | .lines() |
| 198 | .filter(|l| !l.starts_with("dep gamma")) |
| 199 | .map(|l| format!("{l}\n")) |
| 200 | .collect(); |
| 201 | write(&root, "crates/alpha/closure.toml", &kept); |
| 202 | expect_only(&root, &store, Code::UndeclaredImport); |
| 203 | } |
| 204 | |
| 205 | #[test] |
| 206 | fn tainted_artifact_dirty() { |
| 207 | let knobs = Knobs { |
| 208 | gamma_dirty: true, |
| 209 | ..Knobs::default() |
| 210 | }; |
| 211 | let (root, store, _) = seeded(&knobs); |
| 212 | expect_only(&root, &store, Code::TaintedArtifact); |
| 213 | } |
| 214 | |
| 215 | #[test] |
| 216 | fn tainted_artifact_test_only_trust() { |
| 217 | let knobs = Knobs { |
| 218 | gamma_tier: aien_proof::evidence::Tier::TestOnlyTrust, |
| 219 | ..Knobs::default() |
| 220 | }; |
| 221 | let (root, store, _) = seeded(&knobs); |
| 222 | let got = codes(&root, &store); |
| 223 | assert!(got.contains(&Code::TaintedArtifact), "{got:?}"); |
| 224 | } |
| 225 | |
| 226 | #[test] |
| 227 | fn unknown_verifier_profile() { |
| 228 | let knobs = Knobs { |
| 229 | alpha_profile: "bogus-v9", |
| 230 | ..Knobs::default() |
| 231 | }; |
| 232 | let (root, store, _) = seeded(&knobs); |
| 233 | expect_only(&root, &store, Code::UnknownVerifierProfile); |
| 234 | } |
| 235 | |
| 236 | #[test] |
| 237 | fn nine_codes_have_distinct_exit_codes_and_names() { |
| 238 | let exits: BTreeSet<i32> = ALL.iter().map(|c| c.exit_code()).collect(); |
| 239 | let names: BTreeSet<&str> = ALL.iter().map(|c| c.as_str()).collect(); |
| 240 | assert_eq!(ALL.len(), 9); |
| 241 | assert_eq!(exits.len(), 9); |
| 242 | assert_eq!(names.len(), 9); |
| 243 | assert!(exits.iter().all(|e| (10..=18).contains(e))); |
| 244 | } |
| 245 | |
| 246 | #[test] |
| 247 | fn no_manifests_fails_closed() { |
| 248 | let root = temp_dir("bare"); |
| 249 | copy_sources(&fixture_src(), &root); |
| 250 | let got = codes(&root, &root.join("store")); |
| 251 | assert_eq!(got, BTreeSet::from([Code::UnverifiedDependency])); |
| 252 | assert_eq!( |
| 253 | bin(&root, &root.join("store")).0, |
| 254 | Code::UnverifiedDependency.exit_code() |
| 255 | ); |
| 256 | } |
| 257 | |
| 258 | #[test] |
| 259 | fn usage_error_is_exit_2() { |
| 260 | let o = Command::new(env!("CARGO_BIN_EXE_verify-closure")) |
| 261 | .output() |
| 262 | .unwrap(); |
| 263 | assert_eq!(o.status.code(), Some(2)); |
| 264 | } |