//! Replay the published `efficiency/1` payload pack from disk. //! //! jcp SPEC §5.2-5.3 states the rules and `gen_efficiency_vectors` emits the //! vectors. This reads those bytes back, the same bytes a third party would, //! and checks the reference reader reproduces every recorded verdict. The //! refusals are the half that matters: a reader that accepts a partial figure //! presented as a task total has failed in the direction that flatters. use jcp_efficiency::{aggregate, read_payload}; use serde_json::{json, Value}; fn pack() -> Value { let p = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../../conformance/v1/efficiency.json"); // Assert, not skip: a `return` here would pass green in the one case this // exists to catch, the pack going missing. assert!(p.exists(), "conformance pack missing at {} — it is what this test replays", p.display()); let bytes = std::fs::read(&p).unwrap_or_else(|e| panic!("read {}: {e}", p.display())); serde_json::from_slice(&bytes).expect("pack parses") } #[test] fn the_pack_declares_its_profile_and_spec() { let v = pack(); assert_eq!(v["profile"], "efficiency/1"); assert!(v["spec"].as_str().unwrap().contains("5.2")); } #[test] fn every_positive_payload_reads_as_recorded() { let v = pack(); let cases = v["payload"]["positive"].as_array().expect("positives"); assert!(cases.len() >= 2, "expected the published positive cases"); for c in cases { let note = c["note"].as_str().unwrap_or("?"); let r = read_payload(&c["input"]).unwrap_or_else(|e| panic!("{note}: must read: {e}")); let got = json!({ "energy_uj": r.energy_uj, "provenance": r.provenance, "energy_coverage": r.energy_coverage.label(), "uj_per_accepted_token": r.uj_per_accepted_token, }); assert_eq!(got, c["expected"], "{note}"); } } #[test] fn every_negative_payload_is_refused_for_its_recorded_reason() { let v = pack(); let cases = v["payload"]["negative"].as_array().expect("negatives"); assert!(cases.len() >= 3, "expected the published refusals"); for c in cases { let note = c["note"].as_str().unwrap_or("?"); let because = c["expected"]["refused"].as_str().expect("a refusal reason"); match read_payload(&c["input"]) { Ok(r) => panic!("{note}: accepted, as {r:?}"), Err(e) => assert!(e.to_string().contains(because), "{note}: refused for '{e}', not '{because}'"), } } } #[test] fn every_aggregate_sums_to_its_recorded_totals() { let v = pack(); let cases = v["aggregate"]["cases"].as_array().expect("aggregate cases"); assert!(cases.len() >= 2); for c in cases { let note = c["note"].as_str().unwrap_or("?"); let rs: Vec<_> = c["input"] .as_array() .expect("payloads") .iter() .map(|p| read_payload(p).unwrap_or_else(|e| panic!("{note}: {e}"))) .collect(); let t = aggregate(&rs); let got = json!({ "completed_uj": t.completed_uj, "wasted_uj": t.wasted_uj, "coverage": t.coverage.label(), "provenance_floor": t.provenance_floor.map(|p| p.jcr1_label()), "uj_per_completed_unit": t.uj_per_completed_unit(), "tokens_per_completed_unit": t.tokens_per_completed_unit(), }); assert_eq!(got, c["expected"], "{note}"); } }