use kshana::verification::{verification_matrix, VerificationItem, VerificationStatus};
use std::io::Write;
fn primary_evidence(it: &VerificationItem) -> String {
for raw in it
.tests
.split(|c: char| c.is_whitespace() || c == ',' || c == ';' || c == '(' || c == ')')
{
let tok = raw.trim();
if tok.ends_with(".rs") && (tok.starts_with("tests/") || tok.starts_with("src/")) {
return tok.to_string();
}
}
if it.tests.contains("::") {
if let Some(first) = it.tests.split_whitespace().next() {
return first.to_string();
}
}
if !it.module.is_empty() {
return format!("module: {}", it.module);
}
"—".to_string()
}
fn render(version: &str, items: &[VerificationItem]) -> String {
let validated: Vec<&VerificationItem> = items
.iter()
.filter(|i| i.status == VerificationStatus::Validated)
.collect();
let mut h = String::new();
h.push_str("<!DOCTYPE html>\n<html lang=\"en\"><head><meta charset=\"utf-8\">\n");
h.push_str("<title>Kshana validation summary</title>\n<style>\n");
h.push_str(
"body{font:14px/1.5 -apple-system,Segoe UI,Roboto,sans-serif;color:#1a1a1a;\
max-width:64rem;margin:2rem auto;padding:0 1rem}\
h1{font-size:1.5rem;margin:0 0 .25rem}.sub{color:#555;margin:0 0 1.5rem}\
table{border-collapse:collapse;width:100%}th,td{border:1px solid #ddd;\
padding:.5rem .6rem;text-align:left;vertical-align:top}\
th{background:#f5f3ee}code{font-size:.85em}.ok{color:#1a7f37;font-weight:600}\
footer{margin-top:1.5rem;color:#777;font-size:.85em}\
@media print{body{margin:0;max-width:none}a{color:inherit;text-decoration:none}}\n",
);
h.push_str("</style></head><body>\n");
h.push_str(&format!(
"<h1>Kshana validation summary <span class=\"ok\">{version}</span></h1>\n"
));
h.push_str(&format!(
"<p class=\"sub\">The {} externally-validated capabilities, generated from the \
verification matrix. Every row is enforced by a test in continuous integration and \
checked against an external, authoritative oracle. This page indexes the validation \
suite; the cited test is the source of truth. The full {}-row matrix (incl. the \
honestly-Modelled and partner-owned rows) is at docs/VERIFICATION-MATRIX.md and on \
kshana.dev.</p>\n",
validated.len(),
items.len(),
));
h.push_str(
"<table>\n<thead><tr><th>Capability</th><th>What it does</th><th>Enforced by</th>\
<th>External oracle</th></tr></thead>\n<tbody>\n",
);
for it in &validated {
h.push_str(&format!(
"<tr><td class=\"ok\">{}</td><td>{}</td><td><code>{}</code></td><td>{}</td></tr>\n",
esc(it.requirement),
esc(it.capability),
esc(&primary_evidence(it)),
esc(it.oracle),
));
}
h.push_str("</tbody></table>\n");
h.push_str(
"<footer>Generated by <code>validation_report</code> from \
<code>verification_matrix()</code> — the same single source as the kshana.dev ledger \
and docs/VERIFICATION-MATRIX.md. Honest scope: this summarises what the suite asserts; \
it does not itself re-run the physics. See docs/VALIDATION.md and \
docs/CLAIMS-VS-REALITY.md.</footer>\n",
);
h.push_str("</body></html>\n");
h
}
fn esc(s: &str) -> String {
s.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
}
fn main() -> std::io::Result<()> {
let html = render(env!("CARGO_PKG_VERSION"), &verification_matrix());
match std::env::args().nth(1) {
Some(path) => {
let mut f = std::fs::File::create(&path)?;
f.write_all(html.as_bytes())?;
eprintln!("wrote validation summary to {path}");
}
None => {
print!("{html}");
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn report_is_well_formed_and_lists_every_validated_capability() {
let m = verification_matrix();
let h = render("v9.9.9", &m);
assert!(h.starts_with("<!DOCTYPE html>"));
assert!(h.trim_end().ends_with("</html>"));
assert!(h.contains("v9.9.9"));
for it in m
.iter()
.filter(|i| i.status == VerificationStatus::Validated)
{
assert!(
h.contains(&esc(it.requirement)),
"missing validated capability {}",
it.requirement
);
assert!(
h.contains(&esc(&primary_evidence(it))),
"missing evidence for {}",
it.requirement
);
}
assert!(h.to_lowercase().contains("does not itself re-run"));
}
#[test]
fn primary_evidence_prefers_a_real_test_path() {
let m = verification_matrix();
let with_path = m
.iter()
.filter(|i| i.status == VerificationStatus::Validated)
.filter(|i| {
let p = primary_evidence(i);
p.starts_with("tests/") || p.starts_with("src/")
})
.count();
assert!(
with_path >= 25,
"too few validated rows resolve to a file path: {with_path}"
);
}
#[test]
fn html_escaping_neutralises_markup() {
assert_eq!(esc("a<b>&c"), "a<b>&c");
}
}