//! `wai.media.upscale` (extensions/exact-upscale.md): the committed corpus //! `live-conformance/upscale/` regenerates byte for byte from its fixed seeds and its two //! pinned trained priors, holds no other file, every case in it gets the outcome it states, //! and the extension's §8 lists exactly the reasons the reference reports. The generator //! itself asserts that the corpus gives every one of them. #![cfg(feature = "exact_upscale")] use std::collections::BTreeMap; use std::path::PathBuf; use wai::exact_upscale::{corpus, Reason}; fn wai_dir() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("..") } fn committed() -> BTreeMap> { let dir = wai_dir().join("live-conformance/upscale"); let mut files = BTreeMap::new(); for sub in ["", "priors"] { for e in std::fs::read_dir(dir.join(sub)).expect("the committed corpus").flatten() { let name = e.file_name().to_string_lossy().into_owned(); if e.path().is_dir() || (sub.is_empty() && name == "README.md") { continue; } let key = if sub.is_empty() { name } else { format!("{sub}/{name}") }; files.insert(key, std::fs::read(e.path()).expect("a file")); } } files } fn inputs(files: &BTreeMap>) -> BTreeMap> { corpus::INPUT_PRIORS.iter().filter_map(|(n, _)| files.get(&format!("priors/{n}")).map(|b| (n.to_string(), b.clone()))).collect() } #[test] fn the_trained_priors_are_the_pinned_ones() { assert_eq!(corpus::check_input_priors(&inputs(&committed())), Vec::::new()); } #[test] fn the_committed_corpus_regenerates_from_its_seeds() { let files = committed(); let mut generated: BTreeMap> = corpus::generate(&inputs(&files)).into_iter().collect(); for (n, _) in corpus::INPUT_PRIORS { let k = format!("priors/{n}"); generated.insert(k.clone(), files[&k].clone()); } let extra: Vec<&String> = files.keys().filter(|k| !generated.contains_key(*k)).collect(); let missing: Vec<&String> = generated.keys().filter(|k| !files.contains_key(*k)).collect(); let differ: Vec<&String> = generated.iter().filter(|(k, v)| files.get(*k).is_some_and(|f| f != *v)).map(|(k, _)| k).collect(); assert!(extra.is_empty() && missing.is_empty() && differ.is_empty(), "extra {extra:?}, missing {missing:?}, differ {differ:?}: run `wai_upscale_vectors gen`"); } #[test] fn every_case_gets_its_stated_outcome() { let fails = corpus::verify(&committed()); assert!(fails.is_empty(), "{}", fails.join("\n")); } /// §8 of the extension lists exactly the codes `Reason::ALL` has, each once, in order. #[test] fn the_extension_lists_the_reasons() { let text = std::fs::read_to_string(wai_dir().join("extensions/exact-upscale.md")).expect("the extension"); let at = text.find("\n## 8. Reasons").expect("a §8"); let end = text[at + 1..].find("\n## ").map_or(text.len(), |e| at + 1 + e); let listed: Vec = text[at..end].lines().filter(|l| l.starts_with("| `")).filter_map(|l| l.split('`').nth(1).map(String::from)).collect(); let want: Vec = Reason::ALL.iter().map(|r| r.code().to_string()).collect(); assert_eq!(listed, want, "§8's table and Reason::ALL differ"); } /// Every derivation output the corpus states is in `sha256.json`, and each digest there is /// an output of the corpus's accepted upscale records. #[test] fn the_sidecar_names_every_accepted_output() { let files = committed(); let side: serde_json::Value = serde_json::from_slice(&files["sha256.json"]).expect("sha256.json"); let doc: serde_json::Value = serde_json::from_slice(&files["vectors.json"]).expect("vectors.json"); let mut want = 0; for v in doc["vectors"].as_array().unwrap().iter().filter(|v| v["outcome"] == "accept") { for (i, t) in v["upscales"].as_array().unwrap().iter().enumerate() { let u = wai::exact_upscale::read_upscale(t.as_str().unwrap()).unwrap(); let k = format!("{}/upscale/{i}/output", v["name"].as_str().unwrap()); assert_eq!(side[&k]["sha256"].as_str(), Some(wai::exact_upscale::hex(&u.output).as_str()), "{k}"); want += 1; } } assert_eq!(side.as_object().unwrap().len(), want); }