use crate::image;
use crate::script::{exit, Frame, Script, Step};
use crate::tools::ToolSet;
use serde::Serialize;
use serde_json::{json, Value};
use std::path::{Path, PathBuf};
use std::time::Instant;
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Outcome {
Pass,
ExpectationFailed,
ToolError,
Timeout,
}
#[derive(Debug, Clone, Serialize)]
pub struct StepResult {
pub index: usize,
pub tool: String,
pub outcome: Outcome,
pub elapsed_ms: u128,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub failures: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub image: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub diff_ratio: Option<f64>,
}
#[derive(Debug, Clone, Serialize)]
pub struct VideoResult {
pub out: String,
pub written: Vec<String>,
pub frames: usize,
pub bytes: usize,
pub size: [u32; 2],
pub duration_ms: u32,
pub encode_ms: u128,
}
#[derive(Debug, Clone, Serialize)]
pub struct RunResult {
pub script: String,
pub passed: bool,
pub exit_code: i32,
pub steps: Vec<StepResult>,
#[serde(skip_serializing_if = "Option::is_none")]
pub failed_step: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub video: Option<VideoResult>,
}
pub use crate::script::{PLM_REQUEST, PLM_SERVER};
pub struct RunOptions {
pub out_dir: PathBuf,
pub baselines_dir: PathBuf,
pub update_baselines: bool,
pub stop_on_fail: bool,
pub video_root: Option<PathBuf>,
pub plm: Option<std::sync::Arc<crate::plm_fixture::PlmFixture>>,
}
async fn stale_frames(tools: &ToolSet) -> Option<u64> {
let get = tools.get("state_get")?;
let out = (get.handler)(json!({ "names": ["__brepPerf"] })).await.ok()?;
out.json.get("__brepPerf")?.get("staleFrames")?.as_u64()
}
pub async fn run(script: &Script, tools: &ToolSet, opts: &RunOptions) -> RunResult {
let _ = std::fs::create_dir_all(&opts.out_dir);
let deadline = Instant::now() + std::time::Duration::from_millis(script.timeout_ms);
let mut steps = Vec::new();
let mut failed_step = None;
let total_frames = script.steps.iter().filter(|s| s.frame.is_some()).count();
let mut shots: Vec<image::GifFrame> = Vec::new();
let mut framed = 0usize;
let mut recording_failed = false;
let stale_before = stale_frames(tools).await;
for (index, step) in script.steps.iter().enumerate() {
if Instant::now() > deadline {
steps.push(StepResult {
index,
tool: step.tool.clone(),
outcome: Outcome::Timeout,
elapsed_ms: 0,
failures: vec![format!("script timeout ({} ms) reached before step {index}", script.timeout_ms)],
image: None,
diff_ratio: None,
});
failed_step = Some(index);
break;
}
let motion = script
.video
.as_ref()
.and(step.frame.as_ref())
.and_then(|f| f.motion.as_ref());
let mut capture_args = None;
if let (Some(video), Some(frame)) = (&script.video, &step.frame) {
framed += 1;
let recorded = match motion {
None => capture_frame(tools, video, frame, framed, total_frames)
.await
.map(|shot| shots.push(shot)),
Some(motion) => {
capture_args = Some(json!({ "capture": {
"frames": motion.frames,
"max_width": video.width,
"cursor_scale": motion.cursor_scale,
}}));
set_annotation(tools, video, frame, framed, total_frames).await
}
};
if let Err(why) = recorded {
steps.push(StepResult {
index,
tool: "annotate+screenshot".into(),
outcome: Outcome::ToolError,
elapsed_ms: 0,
failures: vec![format!("recording the walkthrough frame before step {index}: {why}")],
image: None,
diff_ratio: None,
});
recording_failed = true;
failed_step = Some(index);
break;
}
}
let (result, images) = run_step(index, step, tools, opts, capture_args).await;
if let (Some(motion), Some(frame)) = (motion, step.frame.as_ref()) {
if let Some(annotate) = tools.get("annotate") {
let _ = (annotate.handler)(json!({})).await;
}
match motion_frames(&images, frame, motion) {
Ok(mut filmed) => shots.append(&mut filmed),
Err(why) => {
steps.push(result);
steps.push(StepResult {
index,
tool: "annotate+screenshot".into(),
outcome: Outcome::ToolError,
elapsed_ms: 0,
failures: vec![format!("filming the drag at step {index}: {why}")],
image: None,
diff_ratio: None,
});
recording_failed = true;
failed_step = Some(index);
break;
}
}
}
let failed = result.outcome != Outcome::Pass;
steps.push(result);
if failed {
failed_step = Some(index);
if opts.stop_on_fail {
break;
}
}
}
match (stale_before, stale_frames(tools).await) {
(Some(before), Some(after)) => {
eprintln!("stale-frame check: {} {}", script.name, after - before);
if after > before {
steps.push(StepResult {
index: script.steps.len(),
tool: "stale-frame check".into(),
outcome: Outcome::ExpectationFailed,
elapsed_ms: 0,
failures: vec![format!(
"{} frame(s) changed what the viewport draws without a render (BREP_STALE_FRAME lines on stderr name the inputs)",
after - before
)],
image: None,
diff_ratio: None,
});
failed_step.get_or_insert(script.steps.len());
}
}
_ => eprintln!("stale-frame check: {} unavailable", script.name),
}
let mut video = None;
if let Some(spec) = &script.video {
if !recording_failed {
match write_video(spec, &shots, opts) {
Ok(v) => video = Some(v),
Err(why) => {
steps.push(StepResult {
index: script.steps.len(),
tool: "video".into(),
outcome: Outcome::ToolError,
elapsed_ms: 0,
failures: vec![why],
image: None,
diff_ratio: None,
});
failed_step.get_or_insert(script.steps.len());
}
}
}
}
let exit_code = steps
.iter()
.map(|s| match s.outcome {
Outcome::Pass => exit::PASS,
Outcome::ExpectationFailed => exit::EXPECTATION_FAILED,
Outcome::ToolError => exit::TOOL_ERROR,
Outcome::Timeout => exit::TIMEOUT,
})
.max()
.unwrap_or(exit::PASS);
let result = RunResult {
script: script.name.clone(),
passed: exit_code == exit::PASS,
exit_code,
steps,
failed_step,
video,
};
if let Ok(text) = serde_json::to_string_pretty(&result) {
let _ = std::fs::write(opts.out_dir.join("result.json"), text);
}
result
}
async fn capture_frame(
tools: &ToolSet,
video: &crate::script::Video,
frame: &Frame,
n: usize,
total: usize,
) -> Result<image::GifFrame, String> {
let screenshot = tools.get("screenshot").ok_or("no `screenshot` tool in this session")?;
set_annotation(tools, video, frame, n, total).await?;
let shot = (screenshot.handler)(json!({
"region": "full",
"max_width": video.width,
"cursor": false
}))
.await?;
let png = &shot.images.first().ok_or("the screenshot tool produced no image")?.png;
let image = image::decode_png(png)?;
let annotate = tools.get("annotate").ok_or("no `annotate` tool in this session")?;
(annotate.handler)(json!({})).await?;
Ok(image::GifFrame { image, hold_ms: frame.hold_ms })
}
async fn set_annotation(
tools: &ToolSet,
video: &crate::script::Video,
frame: &Frame,
n: usize,
total: usize,
) -> Result<(), String> {
let annotate = tools
.get("annotate")
.ok_or("no `annotate` tool in this session: a walkthrough needs a live app host")?;
let mut spec = json!({ "caption": frame.caption, "step": [n, total] });
if let Some(title) = &video.title {
spec["title"] = json!(title);
}
if let Some(highlight) = &frame.highlight {
spec["highlight"] = highlight.clone();
}
if let Some(entity) = &frame.entity {
spec["entity"] = entity.clone();
}
if let Some(point) = &frame.point {
spec["point"] = point.clone();
}
(annotate.handler)(spec).await?;
Ok(())
}
fn motion_frames(
images: &[crate::tools::ToolImage],
frame: &Frame,
motion: &crate::script::Motion,
) -> Result<Vec<image::GifFrame>, String> {
let expected = motion.frames as usize + 1;
if images.len() != expected {
return Err(format!(
"the drag returned {} pictures, expected {expected} (the still plus {} motion frames) — does this tool take a `capture`?",
images.len(),
motion.frames
));
}
images
.iter()
.enumerate()
.map(|(i, img)| {
Ok(image::GifFrame {
image: image::decode_png(&img.png)?,
hold_ms: if i == 0 { frame.hold_ms } else { motion.hold_ms },
})
})
.collect()
}
fn write_video(spec: &crate::script::Video, shots: &[image::GifFrame], opts: &RunOptions) -> Result<VideoResult, String> {
if shots.is_empty() {
return Err(format!(
"the script declares a video (`{}`) but no step carries a `frame`, so there is nothing to record",
spec.out
));
}
let out = Path::new(&spec.out);
if out.is_absolute() || out.components().any(|c| matches!(c, std::path::Component::ParentDir)) {
return Err(format!("video.out `{}` must be a relative path with no `..`", spec.out));
}
let started = Instant::now();
let bytes = image::encode_gif(shots, spec.speed)?;
let encode_ms = started.elapsed().as_millis();
let name = out
.file_name()
.ok_or_else(|| format!("video.out `{}` has no file name", spec.out))?;
let mut written = Vec::new();
let local = opts.out_dir.join(name);
std::fs::write(&local, &bytes).map_err(|e| format!("{}: {e}", local.display()))?;
written.push(local.display().to_string());
if let Some(root) = &opts.video_root {
let dst = root.join(&spec.out);
if let Some(parent) = dst.parent() {
std::fs::create_dir_all(parent).map_err(|e| format!("{}: {e}", parent.display()))?;
}
std::fs::write(&dst, &bytes).map_err(|e| format!("{}: {e}", dst.display()))?;
written.push(dst.display().to_string());
}
let (w, h) = shots[0].image.dimensions();
Ok(VideoResult {
out: spec.out.clone(),
written,
frames: shots.len(),
bytes: bytes.len(),
size: [w, h],
duration_ms: shots.iter().map(|f| f.hold_ms).sum(),
encode_ms,
})
}
async fn run_step(
index: usize,
step: &Step,
tools: &ToolSet,
opts: &RunOptions,
extra_args: Option<Value>,
) -> (StepResult, Vec<crate::tools::ToolImage>) {
let started = Instant::now();
let mut res = StepResult {
index,
tool: step.tool.clone(),
outcome: Outcome::Pass,
elapsed_ms: 0,
failures: Vec::new(),
image: None,
diff_ratio: None,
};
let step_args = if step.args.is_null() { json!({}) } else { step.args.clone() };
let substituted = match &opts.plm {
Some(plm) => plm.substitute(step_args).and_then(|args| {
let expect = serde_json::to_value(&step.expect).map_err(|e| e.to_string())?;
let expect = serde_json::from_value(plm.substitute(expect)?).map_err(|e| e.to_string())?;
Ok((args, expect))
}),
None => Ok((step_args, step.expect.clone())),
};
let (step_args, expects): (Value, Vec<crate::script::Expect>) = match substituted {
Ok(both) => both,
Err(e) => {
write_step_json(opts, index, &step.tool, &json!({ "error": e }));
match &step.expect_error {
Some(pattern) if crate::script::glob_match(pattern, &e) => {}
Some(pattern) => {
res.outcome = Outcome::ExpectationFailed;
res.failures.push(format!("tool failed as expected but with `{e}`, which does not match `{pattern}`"));
}
None => {
res.outcome = Outcome::ToolError;
res.failures.push(e);
}
}
res.elapsed_ms = started.elapsed().as_millis();
return (res, Vec::new());
}
};
if step.tool == PLM_REQUEST || step.tool == PLM_SERVER {
let outcome = match &opts.plm {
None => Err(format!("`{}` needs the script to declare `plm`", step.tool)),
Some(plm) if step.tool == PLM_SERVER => {
let plm = plm.clone();
let action = step_args["action"].as_str().unwrap_or_default().to_string();
match tokio::task::spawn_blocking(move || plm.server(&action)).await {
Ok(answer) => answer,
Err(e) => Err(e.to_string()),
}
}
Some(plm) => match serde_json::from_value::<crate::script::PlmCall>(step_args) {
Err(e) => Err(format!("plm_request arguments: {e}")),
Ok(call) => {
let plm = plm.clone();
match tokio::task::spawn_blocking(move || plm.call(&call).map(|a| a.to_value())).await {
Ok(answer) => answer,
Err(e) => Err(e.to_string()),
}
}
},
};
let output = match outcome {
Ok(v) => crate::tools::ToolOutput::json(v),
Err(e) => crate::tools::ToolOutput::json(json!({ "error": e })),
};
let failed = output.json.get("error").and_then(Value::as_str).map(str::to_string);
write_step_json(opts, index, &step.tool, &output.json);
match (failed, &step.expect_error) {
(Some(e), Some(pattern)) if crate::script::glob_match(pattern, &e) => {}
(Some(e), Some(pattern)) => {
res.outcome = Outcome::ExpectationFailed;
res.failures.push(format!("tool failed as expected but with `{e}`, which does not match `{pattern}`"));
}
(Some(e), None) => {
res.outcome = Outcome::ToolError;
res.failures.push(e);
}
(None, Some(pattern)) => {
res.outcome = Outcome::ExpectationFailed;
res.failures.push(format!("expected the tool to fail with `{pattern}` but it succeeded"));
}
(None, None) => {
if let (Some(plm), false) = (&opts.plm, step.bind.is_empty()) {
if let Err(e) = plm.bind(&output.json, &step.bind) {
res.failures.push(e);
}
}
for e in &expects {
if let Err(why) = e.check(&output.json) {
res.failures.push(why);
}
}
if !res.failures.is_empty() {
res.outcome = Outcome::ExpectationFailed;
}
}
}
res.elapsed_ms = started.elapsed().as_millis();
return (res, Vec::new());
}
let Some(spec) = tools.get(&step.tool) else {
res.outcome = Outcome::ToolError;
res.failures.push(format!("no tool `{}` in this session", step.tool));
res.elapsed_ms = started.elapsed().as_millis();
return (res, Vec::new());
};
let mut args = substitute_out(step_args, &opts.out_dir);
if let (Some(Value::Object(extra)), Value::Object(into)) = (extra_args, &mut args) {
into.extend(extra);
}
let output = match (spec.handler)(args).await {
Ok(o) => {
if let Some(pattern) = &step.expect_error {
res.outcome = Outcome::ExpectationFailed;
res.failures.push(format!("expected the tool to fail with `{pattern}` but it succeeded"));
res.elapsed_ms = started.elapsed().as_millis();
write_step_json(opts, index, &step.tool, &o.json);
return (res, Vec::new());
}
o
}
Err(e) => {
write_step_json(opts, index, &step.tool, &json!({ "error": e }));
if let Some(pattern) = &step.expect_error {
if !crate::script::glob_match(pattern, &e) {
res.outcome = Outcome::ExpectationFailed;
res.failures.push(format!("tool failed as expected but with `{e}`, which does not match `{pattern}`"));
}
res.elapsed_ms = started.elapsed().as_millis();
return (res, Vec::new());
}
res.outcome = Outcome::ToolError;
res.failures.push(e);
res.elapsed_ms = started.elapsed().as_millis();
return (res, Vec::new());
}
};
write_step_json(opts, index, &step.tool, &output.json);
if !step.bind.is_empty() {
let bound = match &opts.plm {
Some(plm) => plm.bind(&output.json, &step.bind),
None => Err("`bind` needs the script to declare `plm`, which holds the variables".to_string()),
};
if let Err(e) = bound {
res.failures.push(e);
}
}
for e in &expects {
if let Err(why) = e.check(&output.json) {
res.failures.push(why);
}
}
if let Some(first) = output.images.first() {
let name = step
.save
.clone()
.unwrap_or_else(|| format!("{index:04}-{}.png", step.tool));
let path = opts.out_dir.join(&name);
let _ = std::fs::write(&path, &first.png);
res.image = Some(name.clone());
if let Some(cmp) = &step.compare {
let baseline = opts.baselines_dir.join(&cmp.baseline);
match (image::decode_png(&first.png), std::fs::read(&baseline)) {
(Ok(actual), Ok(base_bytes)) => match image::decode_png(&base_bytes) {
Ok(base) => {
let d = image::diff(&base, &actual, 8);
res.diff_ratio = Some(d.ratio);
if d.ratio > cmp.max_diff_ratio {
if opts.update_baselines {
let _ = std::fs::create_dir_all(baseline.parent().unwrap_or(Path::new(".")));
let _ = std::fs::write(&baseline, &first.png);
} else {
let _ = std::fs::write(opts.out_dir.join(format!("{name}.diff.png")), image::encode_png(&d.mask).unwrap_or_default());
res.failures.push(format!(
"image differs from baseline `{}` by {:.4} (max {:.4})",
cmp.baseline, d.ratio, cmp.max_diff_ratio
));
}
}
}
Err(e) => res.failures.push(format!("baseline `{}`: {e}", cmp.baseline)),
},
(Ok(_), Err(_)) => {
if opts.update_baselines {
let _ = std::fs::create_dir_all(baseline.parent().unwrap_or(Path::new(".")));
let _ = std::fs::write(&baseline, &first.png);
} else {
res.failures.push(format!(
"no baseline `{}` (run with --update-baselines to create it)",
cmp.baseline
));
}
}
(Err(e), _) => res.failures.push(format!("step image: {e}")),
}
}
} else if step.compare.is_some() || step.save.is_some() {
res.failures.push(format!("step {index} asked to save/compare an image but `{}` produced none", step.tool));
}
if !res.failures.is_empty() {
res.outcome = Outcome::ExpectationFailed;
}
res.elapsed_ms = started.elapsed().as_millis();
(res, output.images)
}
pub fn prepare_store(out_dir: &Path, fixtures: &[String]) -> Result<PathBuf, String> {
let store = out_dir.join("store");
if store.exists() {
std::fs::remove_dir_all(&store).map_err(|e| format!("clear store {}: {e}", store.display()))?;
}
std::fs::create_dir_all(&store).map_err(|e| format!("store {}: {e}", store.display()))?;
if !fixtures.is_empty() {
let models = store.join("models");
std::fs::create_dir_all(&models).map_err(|e| format!("store models {}: {e}", models.display()))?;
for fixture in fixtures {
let from = Path::new(fixture);
let name = from
.file_name()
.ok_or_else(|| format!("fixture `{fixture}` has no file name"))?;
std::fs::copy(from, models.join(name)).map_err(|e| format!("fixture {fixture}: {e}"))?;
}
}
Ok(store)
}
#[derive(Debug, Clone)]
pub struct BootSample {
pub script: String,
pub pass: String,
pub state: Value,
}
pub fn order_check_failures(samples: &[BootSample]) -> Vec<String> {
let mut by_script: std::collections::BTreeMap<&str, Vec<&BootSample>> = std::collections::BTreeMap::new();
for s in samples {
by_script.entry(s.script.as_str()).or_default().push(s);
}
let mut failures = Vec::new();
for (script, group) in by_script {
let Some(first) = group.first() else { continue };
for other in group.iter().skip(1) {
if other.state == first.state {
continue;
}
let mut diffs = Vec::new();
diff_paths(&first.state, &other.state, "", &mut diffs);
if diffs.len() > 8 {
let rest = diffs.len() - 8;
diffs.truncate(8);
diffs.push(format!(" (+{rest} more)"));
}
failures.push(format!(
"{script}: the session it starts in differs between pass `{}` and pass `{}`\n{}",
first.pass,
other.pass,
diffs.join("\n")
));
break;
}
}
failures
}
fn diff_paths(a: &Value, b: &Value, at: &str, out: &mut Vec<String>) {
match (a, b) {
(Value::Object(x), Value::Object(y)) => {
let mut keys: Vec<&String> = x.keys().chain(y.keys()).collect();
keys.sort();
keys.dedup();
for k in keys {
let null = Value::Null;
diff_paths(x.get(k).unwrap_or(&null), y.get(k).unwrap_or(&null), &format!("{at}/{k}"), out);
}
}
(Value::Array(x), Value::Array(y)) if x.len() == y.len() => {
for (i, (xi, yi)) in x.iter().zip(y).enumerate() {
diff_paths(xi, yi, &format!("{at}/{i}"), out);
}
}
_ if a != b => out.push(format!(" {at}: {a} vs {b}")),
_ => {}
}
}
fn substitute_out(v: Value, out: &Path) -> Value {
match v {
Value::String(s) if s.contains("${out}") => Value::String(s.replace("${out}", &out.display().to_string())),
Value::Array(a) => Value::Array(a.into_iter().map(|x| substitute_out(x, out)).collect()),
Value::Object(o) => Value::Object(o.into_iter().map(|(k, x)| (k, substitute_out(x, out))).collect()),
other => other,
}
}
fn write_step_json(opts: &RunOptions, index: usize, tool: &str, value: &Value) {
if let Ok(text) = serde_json::to_string_pretty(value) {
let _ = std::fs::write(opts.out_dir.join(format!("{index:04}-{tool}.json")), text);
}
}