crates/edge/verify/tests/differential.rs 5.6 KiB 167 lines Raw
1 //! Two bindings, one engine-visible sequence.2 //!3 //! The mock and the Python fixture serve different catalogues. A differential4 //! test cannot require the same capability set; it requires the same behaviour5 //! where both bindings answer. The catalogue step therefore agrees on its6 //! verdict and the jobs that the fixture does not declare are the only allowed7 //! difference.8 9 use std::path::{Path, PathBuf};10 use std::process::Command;11 use std::sync::atomic::{AtomicU64, Ordering};12 use std::time::Duration;13 14 use inf3r_verify::{Step, Trace, Verify, diff};15 16 /// The time that a whole trace may take. The Python fixture loads no model, so17 /// the budget only covers process startup and the checks.18 const BUDGET: Duration = Duration::from_secs(30);19 20 /// The capability that the fixture declares in its one model.21 const FIXTURE_CAPABILITY: &str = "chat";22 23 /// The steps whose verdict must agree even though their catalogue entries do24 /// not. A catalogue difference is allowed only in the model text, not in the25 /// judgement that it is valid.26 const AGREED: [&str; 6] = [27     "handshake",28     "catalogue",29     "cancel",30     "credit",31     "crash",32     "restart",33 ];34 35 #[test]36 fn the_mock_and_the_python_fixture_trace_the_same_shape() {37     let suite = suite_file(38         r#"{"steps":[39             {"capability":"chat","parts":[{"kind":"text","role":"user","text":""}],"expect":"completed"},40             {"capability":"embed","parts":[{"kind":"text","text":"one"}],"expect":"completed"}41         ]}"#,42     );43     let mock = Verify::command(Command::new(binary("inf3r-mock-x64-cpu")))44         .suite(&suite)45         .budget(BUDGET)46         .trace()47         .expect("the mock traces");48     let fixture = Verify::command(fixture_command())49         .suite(&suite)50         .budget(BUDGET)51         .trace()52         .expect("the fixture traces");53 54     for what in AGREED {55         let left = step(&mock, what);56         let right = step(&fixture, what);57         if what == "catalogue" || what == "handshake" {58             assert_eq!(verdict(&left.detail), verdict(&right.detail), "{what}");59         } else {60             assert_eq!(left, right, "{what}");61         }62     }63 64     let diffs = diff(&comparable(&mock), &comparable(&fixture));65     assert!(!diffs.is_empty(), "the fixture does not declare every step");66     for difference in diffs {67         // The fixture declares only chat, so a capability that produces a68         // file artifact is a declared difference, like an undeclared job.69         if difference.a.what == "artifact" && difference.b.what == "artifact" {70             continue;71         }72         // The wire version is a negotiation, not behaviour. One peer may choose73         // 2 and the other 1 while both complete the same checks.74         if difference.a.what == "handshake" && difference.b.what == "handshake" {75             continue;76         }77         // The fixture has no cache codec, so the mock's cache checks are a78         // declared capability difference rather than a behavioural one.79         if difference.a.what.starts_with("cache_") || difference.b.what.starts_with("cache_") {80             continue;81         }82         assert_eq!(difference.a.what, "job", "{difference:?}");83         assert_eq!(difference.b.what, "job", "{difference:?}");84         let capability = capability_of(&difference.a);85         assert_eq!(capability, capability_of(&difference.b), "{difference:?}");86         assert_ne!(87             capability, FIXTURE_CAPABILITY,88             "the fixture declares {capability}: {difference:?}"89         );90     }91 92     let _ = std::fs::remove_file(&suite);93 }94 95 /// The trace with a catalogue reduced to its verdict.96 ///97 /// The sets of models and capabilities are allowed to differ. A pass against a98 /// fail is not, so the verdict survives the reduction.99 fn comparable(trace: &Trace) -> Trace {100     Trace {101         begin: trace.begin.clone(),102         steps: trace103             .steps104             .iter()105             .filter(|step| !step.what.starts_with("cache_"))106             .map(|step| {107                 if step.what == "catalogue" {108                     Step {109                         what: step.what,110                         detail: verdict(&step.detail).to_string(),111                     }112                 } else {113                     step.clone()114                 }115             })116             .collect(),117     }118 }119 120 fn verdict(detail: &str) -> &str {121     detail.split_whitespace().next().unwrap_or_default()122 }123 124 fn capability_of(step: &Step) -> &str {125     step.detail126         .split_whitespace()127         .nth(1)128         .expect("a job step names its capability")129 }130 131 fn step<'a>(trace: &'a Trace, what: &str) -> &'a Step {132     trace133         .steps134         .iter()135         .find(|step| step.what == what)136         .unwrap_or_else(|| panic!("no {what} step"))137 }138 139 fn fixture_command() -> Command {140     let root = Path::new(env!("CARGO_MANIFEST_DIR"))141         .ancestors()142         .nth(3)143         .expect("the crate is three levels below the repo root");144     let mut command = Command::new("python3");145     command.arg(root.join("plugins/fixtures/echo-py/plugin.py"));146     command147 }148 149 fn suite_file(json: &str) -> PathBuf {150     static NEXT: AtomicU64 = AtomicU64::new(1);151     let path = std::env::temp_dir().join(format!(152         "inf3r-verify-differential-{}-{}.json",153         std::process::id(),154         NEXT.fetch_add(1, Ordering::Relaxed)155     ));156     std::fs::write(&path, json).expect("write the suite file");157     path158 }159 160 fn binary(name: &str) -> PathBuf {161     let mut path = std::env::current_exe().expect("the test binary has a path");162     path.pop();163     if path.file_name().is_some_and(|name| name == "deps") {164         path.pop();165     }166     path.join(name)167 }