//! `wai.delivery.cache` (extensions/cache-authority.md): the committed corpus //! `live-conformance/cache-authority/` regenerates byte for byte from its fixed seeds, holds no //! other file, every case in it gets the outcome it states, §11 lists exactly the reasons the //! reference reports, and jwp-receipts §3.1 lists the class codes at their steps. #![cfg(feature = "cache_authority")] use std::collections::BTreeMap; use std::path::PathBuf; use wai::cache_authority::{corpus, CODES}; fn wai_dir() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("..") } fn committed() -> BTreeMap { let dir = wai_dir().join("live-conformance/cache-authority"); std::fs::read_dir(&dir) .expect("the committed corpus") .flatten() .map(|e| (e.file_name().to_string_lossy().into_owned(), e.path())) .filter(|(n, _)| n != "README.md") .map(|(n, p)| (n, std::fs::read_to_string(p).expect("a text file"))) .collect() } #[test] fn the_committed_corpus_regenerates_from_its_seeds() { let files = committed(); let generated: BTreeMap = corpus::generate().into_iter().collect(); 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_cache_authority_vectors gen`"); } #[test] fn every_case_gets_its_stated_outcome() { let fails = corpus::verify(&committed()); assert!(fails.is_empty(), "{}", fails.join("\n")); } /// §11 of the extension lists exactly the codes `CODES` has, each once, in order. #[test] fn the_extension_lists_the_reasons() { let text = std::fs::read_to_string(wai_dir().join("extensions/cache-authority.md")).expect("the extension"); let at = text.find("\n## 11. Reasons").expect("a §11"); 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 = CODES.iter().map(|c| c.to_string()).collect(); assert_eq!(listed, want, "§11's table and CODES differ"); } /// jwp-receipts §3.1's table lists the class's codes: its rules at step 4, its checks against the /// group and its objects at step 6. #[test] fn jwp_receipts_lists_the_class_codes() { let text = std::fs::read_to_string(wai_dir().join("extensions/jwp-receipts.md")).expect("jwp-receipts"); let class: Vec<&str> = CODES.iter().copied().filter(|c| c.starts_with("cache_transmission_")).collect(); assert_eq!(class.len(), 8, "the class's codes"); for (k, code) in class.iter().enumerate() { let step = if k < 3 { 4 } else { 6 }; assert!(text.contains(&format!("| {step} | `{code}` |")), "jwp-receipts §3.1 does not list {code} at step {step}"); } }