Files
pbakaus_impeccable/crates/cli/examples/capture_snapshot.rs
T
Paul Bakaus 6a93a35293 Add human component review and prepare Impeccable 4.4.0
Carry native comp capture and completion integrity fixes into the reviewed component workflow. Bump the skill to 4.4.0 and engine/platform pins to 0.1.6; keep publication separate from this release candidate.

AI assistance: implemented and validated with OpenAI Codex.
2026-09-13 20:41:09 -07:00

61 lines
2.3 KiB
Rust

//! Offline diagnostic using a frozen static entry. Not a production CLI verb.
//! cargo run -p impeccable --example capture_snapshot -- request.json new-output-dir
use impeccable::{
asset_capture::CdpAssetRenderer,
capture_snapshot::{HtmlSnapshot, SnapshotSelection},
};
use serde_json::Value;
use std::{fs, path::Path, sync::Arc};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<_> = std::env::args().collect();
if args.len() != 3 {
return Err("expected request.json and a new output directory".into());
}
let config: Value = serde_json::from_slice(&fs::read(&args[1])?)?;
let text = |key: &str| config[key].as_str().ok_or_else(|| format!("missing {key}"));
let snapshot = Arc::new(HtmlSnapshot::freeze(SnapshotSelection {
root: text("root")?.into(),
entry: text("entry")?.into(),
served: serde_json::from_value(config["served"].clone())?,
bound: serde_json::from_value(config["bound"].clone())?,
})?);
let regions: Vec<String> = if config["regions"].is_array() {
serde_json::from_value(config["regions"].clone())?
} else {
vec![text("region")?.into()]
};
let captures = snapshot.capture_regions(
&mut CdpAssetRenderer::from_process_env(),
text("spec")?,
&regions.iter().map(String::as_str).collect::<Vec<_>>(),
config["reducedMotion"]
.as_bool()
.ok_or("missing reducedMotion")?,
)?;
let out = Path::new(&args[2]);
fs::create_dir(out)?;
let mut summaries = Vec::new();
for (index, capture) in captures.into_iter().enumerate() {
let destination = if regions.len() == 1 {
out.to_path_buf()
} else {
let p = out.join(format!("region-{index}"));
fs::create_dir(&p)?;
p
};
for image in capture.images {
fs::write(destination.join(image.name), image.png)?;
}
fs::write(
destination.join("receipt.json"),
serde_json::to_vec_pretty(&capture.receipt)?,
)?;
summaries.push(serde_json::json!({"region":regions[index],"status":capture.receipt["status"],"reason":capture.receipt["reason"],"output":destination}));
}
println!(
"{}",
serde_json::json!({"snapshot":snapshot.digest(),"regions":summaries})
);
Ok(())
}