use std::fs;
use std::io::Write;
use std::path::Path;
use std::process::Command;
use std::time::Duration;
use avt::Vt;
use crate::evidence::collector::CapturedStep;
use crate::terminal::attributed::palette_color;
use crate::terminal::attributed::rgb_color;
use crate::terminal::attributed::screen_svg;
use crate::terminal::attributed::AttributedCell;
use crate::terminal::attributed::AttributedScreen;
use crate::terminal::attributed::SvgMetrics;
use crate::terminal::attributed::DEFAULT_CELL_H;
use crate::terminal::attributed::DEFAULT_CELL_W;
use crate::terminal::attributed::DEFAULT_COLUMNS;
use crate::terminal::attributed::DEFAULT_FONT_PX;
use crate::terminal::attributed::DEFAULT_PADDING;
use crate::terminal::attributed::DEFAULT_ROWS;
use crate::terminal::proc::combined_output;
use crate::terminal::proc::run_with_timeout;
const RSVG_CONVERT: &str = "/usr/bin/rsvg-convert";
const FFMPEG: &str = "/usr/local/bin/ffmpeg";
const RSVG_TIMEOUT: Duration = Duration::from_secs(30);
const FFMPEG_TIMEOUT: Duration = Duration::from_secs(120);
pub const DEFAULT_CHECKPOINT_HOLD: f64 = 3.0;
const REPAINT_PREFIX: &str = "\x1b[m\x1b[r\x1b[H\x1b[2J";
const HOLD_TERMINATOR: &str = "\x1b[m";
pub const MIN_CHECKPOINT_HOLD: f64 = 1e-6;
pub type ToolRunner = Box<dyn Fn(&str, &[String], Duration) -> Result<(), String> + Send + Sync>;
pub fn default_runner() -> ToolRunner {
Box::new(|executable, args, timeout| {
let mut cmd = Command::new(executable);
cmd.args(args);
let output = run_with_timeout(cmd, timeout).map_err(|e| e.to_string())?;
if !output.status.success() {
return Err(format!(
"{} exited {}: {}",
executable,
output.status.code().unwrap_or(-1),
combined_output(&output),
));
}
Ok(())
})
}
pub struct CastVideoConverter {
pub fps: u32,
pub idle_time_limit: f64,
pub columns: usize,
pub rows: usize,
pub cell_w: f64,
pub cell_h: f64,
pub font_px: u32,
pub padding: u32,
pub width: Option<usize>,
pub height: Option<usize>,
runner: ToolRunner,
}
impl Default for CastVideoConverter {
fn default() -> Self {
CastVideoConverter {
fps: 24,
idle_time_limit: 1.0,
columns: DEFAULT_COLUMNS,
rows: DEFAULT_ROWS,
cell_w: DEFAULT_CELL_W,
cell_h: DEFAULT_CELL_H,
font_px: DEFAULT_FONT_PX,
padding: DEFAULT_PADDING,
width: None,
height: None,
runner: default_runner(),
}
}
}
impl CastVideoConverter {
pub fn new() -> Self {
Self::default()
}
pub fn with_runner(runner: ToolRunner) -> Self {
CastVideoConverter {
runner,
..Self::default()
}
}
pub fn metrics(&self) -> SvgMetrics {
let mut metrics = SvgMetrics {
columns: self.columns,
rows: self.rows,
cell_w: self.cell_w,
cell_h: self.cell_h,
font_px: self.font_px,
padding: self.padding,
width: 0,
height: 0,
};
metrics.width = self.width.unwrap_or_else(|| metrics.derived_width());
metrics.height = self.height.unwrap_or_else(|| metrics.derived_height());
metrics
}
pub fn frame_width(&self) -> usize {
self.metrics().width
}
pub fn frame_height(&self) -> usize {
self.metrics().height
}
pub fn output_path_for(&self, cast_path: &str) -> String {
let p = Path::new(cast_path);
p.with_extension("mp4").to_string_lossy().to_string()
}
pub fn convert(&self, cast_path: &str, video_path: Option<&str>) -> Result<String, String> {
let output_path = video_path
.map(|s| s.to_string())
.unwrap_or_else(|| self.output_path_for(cast_path));
if let Some(parent) = Path::new(&output_path).parent() {
if !parent.as_os_str().is_empty() {
fs::create_dir_all(parent).map_err(|e| e.to_string())?;
}
}
let frames = self.replay_frames(cast_path)?;
let tmpdir = tempfile::Builder::new()
.prefix("termproof_cast_frames_")
.tempdir()
.map_err(|e| e.to_string())?;
let metrics = self.metrics();
for (index, frame) in frames.iter().enumerate() {
let svg_path = tmpdir.path().join(format!("frame-{:05}.svg", index));
let png_path = tmpdir.path().join(format!("frame-{:05}.png", index));
fs::write(&svg_path, screen_svg(frame, &metrics)).map_err(|e| e.to_string())?;
(self.runner)(
RSVG_CONVERT,
&[
"--output".to_string(),
png_path.to_string_lossy().to_string(),
svg_path.to_string_lossy().to_string(),
],
RSVG_TIMEOUT,
)?;
}
let frame_glob = tmpdir.path().join("frame-%05d.png");
(self.runner)(
FFMPEG,
&[
"-y".to_string(),
"-framerate".to_string(),
self.fps.to_string(),
"-i".to_string(),
frame_glob.to_string_lossy().to_string(),
"-c:v".to_string(),
"libx264".to_string(),
"-pix_fmt".to_string(),
"yuv444p".to_string(),
"-crf".to_string(),
"15".to_string(),
"-preset".to_string(),
"slower".to_string(),
"-tune".to_string(),
"stillimage".to_string(),
"-movflags".to_string(),
"+faststart".to_string(),
output_path.clone(),
],
FFMPEG_TIMEOUT,
)?;
Ok(output_path)
}
#[cfg(feature = "_test-harness")]
#[doc(hidden)]
pub fn frames_from_cast(&self, cast_path: &str) -> Result<Vec<AttributedScreen>, String> {
self.replay_frames(cast_path)
}
fn replay_frames(&self, cast_path: &str) -> Result<Vec<AttributedScreen>, String> {
let contents = fs::read_to_string(cast_path).map_err(|e| e.to_string())?;
let mut lines = contents.lines();
let header_line = lines.next().ok_or("empty cast file")?;
let header: serde_json::Value =
serde_json::from_str(header_line).map_err(|e| e.to_string())?;
let width = header
.get("width")
.and_then(|v| v.as_u64())
.map(|v| v as usize)
.unwrap_or(self.columns);
let height = header
.get("height")
.and_then(|v| v.as_u64())
.map(|v| v as usize)
.unwrap_or(self.rows);
let mut vt = Vt::new(width.max(1), height.max(1));
let mut frames: Vec<AttributedScreen> = Vec::new();
let mut next_frame_at = 0.0_f64;
let mut playback_time = 0.0_f64;
let mut previous_cast_time = 0.0_f64;
let frame_interval = 1.0 / self.fps as f64;
for line in lines {
if line.trim().is_empty() {
continue;
}
let event: serde_json::Value = serde_json::from_str(line).map_err(|e| e.to_string())?;
let arr = match event.as_array() {
Some(a) if a.len() >= 3 => a,
_ => continue,
};
let event_type = arr[1].as_str().unwrap_or("");
if event_type != "o" {
continue;
}
let timestamp = arr[0].as_f64().unwrap_or(0.0);
let data = arr[2].as_str().unwrap_or("");
let elapsed = (timestamp - previous_cast_time).max(0.0);
playback_time += elapsed.min(self.idle_time_limit);
previous_cast_time = timestamp;
vt.feed_str(data);
while next_frame_at <= playback_time {
frames.push(attributed_screen_from_avt(&vt));
next_frame_at += frame_interval;
}
}
let final_snapshot = attributed_screen_from_avt(&vt);
if frames.is_empty() || frames.last() != Some(&final_snapshot) {
frames.push(final_snapshot);
}
Ok(frames)
}
}
pub fn append_checkpoint_frames(
cast_path: &str,
steps: &[CapturedStep],
hold_seconds: Option<f64>,
) -> Result<usize, String> {
if steps.is_empty() {
return Ok(0);
}
let hold = hold_seconds.unwrap_or(DEFAULT_CHECKPOINT_HOLD);
if !hold.is_finite() || hold < MIN_CHECKPOINT_HOLD {
return Err(format!(
"checkpoint hold must be at least {MIN_CHECKPOINT_HOLD} seconds, got {hold}"
));
}
let contents = fs::read_to_string(cast_path).map_err(|e| e.to_string())?;
let mut lines = contents.lines();
lines.next().ok_or("empty cast file")?;
let last_event_at = last_event_time(lines);
let mut appended = String::new();
if !contents.ends_with('\n') {
appended.push('\n');
}
for (offset, step) in steps.iter().enumerate() {
let at = last_event_at + hold * (offset + 1) as f64;
appended.push_str(&cast_event_line(at, &repaint(&step.screen))?);
}
let end = last_event_at + hold * (steps.len() + 1) as f64;
appended.push_str(&cast_event_line(end, HOLD_TERMINATOR)?);
let mut file = fs::OpenOptions::new()
.append(true)
.open(cast_path)
.map_err(|e| e.to_string())?;
file.write_all(appended.as_bytes())
.map_err(|e| e.to_string())?;
Ok(steps.len())
}
fn last_event_time<'a>(lines: impl Iterator<Item = &'a str>) -> f64 {
lines
.filter_map(|line| serde_json::from_str::<serde_json::Value>(line).ok())
.filter_map(|event| event.get(0).and_then(|v| v.as_f64()))
.filter(|at| at.is_finite())
.fold(0.0_f64, f64::max)
}
fn repaint(screen: &str) -> String {
format!(
"{REPAINT_PREFIX}{}",
screen.replace("\r\n", "\n").replace('\n', "\r\n")
)
}
fn cast_event_line(at: f64, data: &str) -> Result<String, String> {
let event = serde_json::json!([round_micros(at), "o", data]);
let line = serde_json::to_string(&event).map_err(|e| e.to_string())?;
Ok(format!("{line}\n"))
}
fn round_micros(at: f64) -> f64 {
(at * 1e6).round() / 1e6
}
fn attributed_screen_from_avt(vt: &Vt) -> AttributedScreen {
let rows = vt
.view()
.map(|line| line.cells().iter().map(cell_from_avt).collect())
.collect();
let cursor = vt.cursor();
AttributedScreen {
rows,
cursor_row: cursor.row,
cursor_column: cursor.col,
cursor_hidden: !cursor.visible,
}
}
fn cell_from_avt(cell: &avt::Cell) -> AttributedCell {
let pen = cell.pen();
let width = cell.width().min(2);
AttributedCell {
text: if width == 0 {
String::new()
} else {
cell.char().to_string()
},
fg: avt_color(pen.foreground()),
bg: avt_color(pen.background()),
bold: pen.is_bold(),
dim: pen.is_faint(),
italic: pen.is_italic(),
underline: pen.is_underline(),
strikethrough: pen.is_strikethrough(),
reverse: pen.is_inverse(),
width,
}
}
fn avt_color(color: Option<avt::Color>) -> String {
match color {
None => "default".to_string(),
Some(avt::Color::Indexed(index)) => palette_color(index as u16),
Some(avt::Color::RGB(rgb)) => rgb_color(rgb.r, rgb.g, rgb.b),
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use std::sync::Mutex;
use super::*;
use crate::evidence::CaptureKind;
use crate::terminal::attributed::attributed_screen_from_text;
fn cast_with(events: &str) -> (tempfile::TempDir, String) {
let dir = tempfile::tempdir().unwrap();
let cast = dir.path().join("r.cast");
fs::write(
&cast,
format!("{{\"version\":2,\"width\":10,\"height\":3}}\n{}", events),
)
.unwrap();
let path = cast.to_string_lossy().to_string();
(dir, path)
}
fn frames(events: &str) -> Vec<AttributedScreen> {
let (_dir, path) = cast_with(events);
CastVideoConverter::new().replay_frames(&path).unwrap()
}
#[test]
fn output_path_swaps_extension() {
let conv = CastVideoConverter::new();
assert_eq!(conv.output_path_for("/tmp/a/b.cast"), "/tmp/a/b.mp4");
}
#[test]
fn frames_from_simple_cast() {
let f = frames("[0.0, \"o\", \"hi\"]\n[0.5, \"o\", \"!\"]\n");
assert!(!f.is_empty());
assert_eq!(f.last().unwrap().text_lines(true)[0], "hi!");
}
#[test]
fn non_output_events_are_ignored() {
let f = frames("[0.0, \"i\", \"typed\"]\n[0.1, \"o\", \"ok\"]\n");
assert_eq!(f.last().unwrap().text_lines(true)[0], "ok");
}
#[test]
fn frame_count_follows_the_frame_rate() {
let f = frames("[0.0, \"o\", \"a\"]\n[1.0, \"o\", \"b\"]\n");
assert_eq!(f.len(), 25);
}
#[test]
fn a_long_pause_is_collapsed_to_the_idle_limit() {
let f = frames("[0.0, \"o\", \"a\"]\n[60.0, \"o\", \"b\"]\n");
assert_eq!(f.len(), 25);
}
#[test]
fn cast_frames_use_avts_width_table() {
let f = frames("[0.0, \"o\", \"\\ud83e\\ude89x\"]\n");
let row = &f.last().unwrap().rows[0];
assert_eq!(
row[0].width, 1,
"avt no longer measures U+1FA89 with the unicode-width 0.1 table; \
re-check whether avt and vt100 can now share one table"
);
assert_eq!(row[1].text, "x", "x should sit in the adjacent column");
let mut parser = vt100::Parser::new(3, 10, 0);
parser.process("\u{1FA89}x".as_bytes());
let live = crate::terminal::attributed::from_vt100(parser.screen());
assert_eq!(live.rows[0][0].width, 2);
assert_eq!(live.rows[0][2].text, "x");
}
#[test]
fn alternate_screen_repaints_are_captured() {
let f = frames(
"[0.0, \"o\", \"scrollback\"]\n\
[0.1, \"o\", \"\\u001b[?1049h\\u001b[H\\u001b[2Jalt\"]\n",
);
assert_eq!(f.last().unwrap().text_lines(true)[0], "alt");
}
#[test]
fn scroll_region_is_honored() {
let f = frames(
"[0.0, \"o\", \"a\\r\\nb\\r\\nkeep\"]\n\
[0.1, \"o\", \"\\u001b[1;2r\\u001b[2;1H\\n\\rc\"]\n",
);
let text = f.last().unwrap().text_lines(true);
assert_eq!(text[0], "b");
assert_eq!(text[1], "c");
assert_eq!(text[2], "keep");
}
#[test]
fn colors_survive_into_the_frame() {
let f = frames("[0.0, \"o\", \"\\u001b[31mred\\u001b[0m\"]\n");
let cell = &f.last().unwrap().rows[0][0];
assert_eq!(cell.fg, "red");
assert_eq!(f.last().unwrap().rows[0][3].fg, "default");
}
#[test]
fn indexed_and_rgb_colors_map_to_hex() {
let f = frames("[0.0, \"o\", \"\\u001b[38;5;196ma\\u001b[38;2;1;2;3mb\"]\n");
let row = &f.last().unwrap().rows[0];
assert_eq!(row[0].fg, "ff0000");
assert_eq!(row[1].fg, "010203");
}
#[test]
fn text_attributes_survive_into_the_frame() {
let f = frames("[0.0, \"o\", \"\\u001b[1;3;4;9;7ma\"]\n");
let cell = &f.last().unwrap().rows[0][0];
assert!(cell.bold && cell.italic && cell.underline);
assert!(cell.strikethrough && cell.reverse);
}
#[test]
fn wide_glyph_keeps_its_filler_column() {
let f = frames("[0.0, \"o\", \"\\u4f60x\"]\n");
let row = &f.last().unwrap().rows[0];
assert_eq!(row[0].width, 2);
assert_eq!(row[1].width, 0);
assert_eq!(row[2].text, "x");
}
#[test]
fn frames_are_rendered_and_stitched() {
let calls = Arc::new(Mutex::new(Vec::new()));
let sink = calls.clone();
let conv = CastVideoConverter::with_runner(Box::new(move |exe, args, _| {
sink.lock()
.expect("poisoned")
.push((exe.to_string(), args.to_vec()));
Ok(())
}));
let (dir, path) = cast_with("[0.0, \"o\", \"hi\"]\n");
let out = dir.path().join("out.mp4").to_string_lossy().to_string();
assert_eq!(conv.convert(&path, Some(&out)).unwrap(), out);
let calls = calls.lock().unwrap();
let (ffmpeg_exe, ffmpeg_args) = calls.last().unwrap();
assert_eq!(ffmpeg_exe, FFMPEG);
assert!(ffmpeg_args.contains(&"yuv444p".to_string()));
assert!(ffmpeg_args.contains(&"stillimage".to_string()));
assert!(ffmpeg_args.contains(&"24".to_string()));
assert!(calls[..calls.len() - 1]
.iter()
.all(|(e, _)| e == RSVG_CONVERT));
}
#[test]
fn a_frame_is_the_still_the_screenshot_renderer_would_draw() {
let seen = Arc::new(Mutex::new(Vec::new()));
let sink = seen.clone();
let conv = CastVideoConverter::with_runner(Box::new(move |exe, args, _| {
if exe == RSVG_CONVERT {
sink.lock()
.expect("poisoned")
.push(fs::read_to_string(&args[2]).expect("frame written"));
}
Ok(())
}));
let (dir, path) = cast_with("[0.0, \"o\", \"hi\"]\n");
let out = dir.path().join("out.mp4").to_string_lossy().to_string();
conv.convert(&path, Some(&out)).unwrap();
let expected = screen_svg(&frames("[0.0, \"o\", \"hi\"]\n")[0], &conv.metrics());
let seen = seen.lock().expect("poisoned");
assert_eq!(seen.first().map(String::as_str), Some(expected.as_str()));
}
fn step(index: usize, label: &str, screen: &str, kind: CaptureKind) -> CapturedStep {
CapturedStep {
index,
label: label.to_string(),
kind,
screen: screen.to_string(),
attributed: attributed_screen_from_text(screen, 10, 3),
raw_output: None,
}
}
fn checkpoint(index: usize, label: &str, screen: &str) -> CapturedStep {
step(index, label, screen, CaptureKind::Checkpoint)
}
fn timestamp_text(path: &str) -> Vec<String> {
fs::read_to_string(path)
.unwrap()
.lines()
.skip(1)
.map(|line| {
line.trim_start_matches('[')
.split(',')
.next()
.expect("timestamp")
.to_string()
})
.collect()
}
fn events_in(path: &str) -> Vec<(f64, String)> {
fs::read_to_string(path)
.unwrap()
.lines()
.skip(1)
.map(|line| serde_json::from_str::<serde_json::Value>(line).expect("valid event"))
.map(|e| {
(
e[0].as_f64().expect("timestamp"),
e[2].as_str().expect("data").to_string(),
)
})
.collect()
}
#[test]
fn checkpoints_are_appended_in_capture_order() {
let (_dir, path) = cast_with("[0.5, \"o\", \"live\"]\n");
let steps = [
checkpoint(0, "one", "first"),
checkpoint(1, "two", "second"),
];
assert_eq!(append_checkpoint_frames(&path, &steps, None).unwrap(), 2);
let events = events_in(&path);
assert_eq!(events.len(), 4);
assert_eq!(events[0].1, "live");
assert!(events[1].1.ends_with("first"), "{:?}", events[1].1);
assert!(events[2].1.ends_with("second"), "{:?}", events[2].1);
assert_eq!(events[3].1, HOLD_TERMINATOR);
}
#[test]
fn timestamps_continue_from_the_session_and_only_increase() {
let (_dir, path) = cast_with("[0.5, \"o\", \"a\"]\n[2.25, \"o\", \"b\"]\n");
let steps = [
checkpoint(0, "one", "first"),
checkpoint(1, "two", "second"),
];
append_checkpoint_frames(&path, &steps, Some(1.5)).unwrap();
let times: Vec<f64> = events_in(&path).iter().map(|(at, _)| *at).collect();
assert_eq!(times, vec![0.5, 2.25, 3.75, 5.25, 6.75]);
assert!(times.windows(2).all(|w| w[1] > w[0]));
}
#[test]
fn the_hold_is_the_gap_between_frames_and_defaults() {
let (_dir, path) = cast_with("[0.0, \"o\", \"a\"]\n");
let steps = [
checkpoint(0, "one", "first"),
checkpoint(1, "two", "second"),
];
append_checkpoint_frames(&path, &steps, None).unwrap();
let times: Vec<f64> = events_in(&path).iter().map(|(at, _)| *at).collect();
assert_eq!(times, vec![0.0, 3.0, 6.0, 9.0]);
}
#[test]
fn a_fractional_hold_is_rounded_the_way_python_rounds() {
let (_dir, path) = cast_with("[0.5000005, \"o\", \"a\"]\n");
let steps = [
checkpoint(0, "one", "first"),
checkpoint(1, "two", "second"),
checkpoint(2, "three", "third"),
];
append_checkpoint_frames(&path, &steps, Some(0.2)).unwrap();
assert_eq!(
timestamp_text(&path),
vec!["0.5000005", "0.700001", "0.900001", "1.100001", "1.300001"]
);
}
#[test]
fn no_checkpoints_is_a_silent_no_op() {
let (_dir, path) = cast_with("[0.0, \"o\", \"a\"]\n");
let before = fs::read_to_string(&path).unwrap();
assert_eq!(append_checkpoint_frames(&path, &[], None).unwrap(), 0);
assert_eq!(fs::read_to_string(&path).unwrap(), before);
let missing = format!("{path}.does-not-exist");
assert_eq!(append_checkpoint_frames(&missing, &[], None).unwrap(), 0);
}
#[test]
fn the_appended_cast_still_replays() {
let (_dir, path) = cast_with("[0.0, \"o\", \"live\"]\n");
let steps = [
checkpoint(0, "one", "first"),
checkpoint(1, "two", "second"),
];
append_checkpoint_frames(&path, &steps, None).unwrap();
let frames = CastVideoConverter::new().replay_frames(&path).unwrap();
let painted: Vec<String> = frames
.iter()
.map(|f| f.text_lines(true)[0].clone())
.collect();
assert_eq!(painted.last().unwrap(), "second");
assert!(painted.contains(&"live".to_string()));
assert!(
painted.iter().position(|t| t == "first") < painted.iter().position(|t| t == "second"),
"the evidence replays in capture order"
);
}
#[test]
fn a_multi_line_screen_is_repainted_row_by_row() {
let (_dir, path) = cast_with("[0.0, \"o\", \"live\"]\n");
append_checkpoint_frames(&path, &[checkpoint(0, "menu", "MENU\nitem")], None).unwrap();
let frames = CastVideoConverter::new().replay_frames(&path).unwrap();
let text = frames.last().unwrap().text_lines(true);
assert_eq!(text[0], "MENU");
assert_eq!(text[1], "item");
}
#[test]
fn a_scroll_region_left_by_the_session_does_not_eat_rows() {
let dir = tempfile::tempdir().unwrap();
let cast = dir.path().join("r.cast");
fs::write(
&cast,
"{\"version\":2,\"width\":20,\"height\":8}\n\
[0.0, \"o\", \"\\u001b[3;6r\\u001b[?6h\"]\n",
)
.unwrap();
let path = cast.to_string_lossy().to_string();
let screen = "L1\nL2\nL3\nL4\nL5\nL6\nL7\nL8";
append_checkpoint_frames(&path, &[checkpoint(0, "full", screen)], None).unwrap();
let frames = CastVideoConverter::new().replay_frames(&path).unwrap();
assert_eq!(
frames.last().unwrap().text_lines(true),
vec!["L1", "L2", "L3", "L4", "L5", "L6", "L7", "L8"],
"rows were scrolled out of the region instead of painted"
);
}
#[test]
fn a_checkpoint_paints_over_the_screen_before_it() {
let (_dir, path) = cast_with("[0.0, \"o\", \"live\"]\n");
let steps = [
checkpoint(0, "long", "abcdefgh"),
checkpoint(1, "short", "xy"),
];
append_checkpoint_frames(&path, &steps, None).unwrap();
let frames = CastVideoConverter::new().replay_frames(&path).unwrap();
assert_eq!(frames.last().unwrap().text_lines(true)[0], "xy");
}
#[test]
fn the_failure_screen_is_appended_too() {
let (_dir, path) = cast_with("[0.0, \"o\", \"live\"]\n");
let steps = [
checkpoint(0, "ok", "fine"),
step(1, "boom", "ERROR", CaptureKind::Failure),
];
assert_eq!(append_checkpoint_frames(&path, &steps, None).unwrap(), 2);
let frames = CastVideoConverter::new().replay_frames(&path).unwrap();
assert_eq!(frames.last().unwrap().text_lines(true)[0], "ERROR");
}
#[test]
fn a_cast_missing_its_final_newline_is_not_spliced() {
let dir = tempfile::tempdir().unwrap();
let cast = dir.path().join("r.cast");
fs::write(
&cast,
"{\"version\":2,\"width\":10,\"height\":3}\n[0.0, \"o\", \"a\"]",
)
.unwrap();
let path = cast.to_string_lossy().to_string();
append_checkpoint_frames(&path, &[checkpoint(0, "one", "first")], None).unwrap();
assert_eq!(events_in(&path).len(), 3);
}
#[test]
fn a_hold_that_would_stall_the_timestamps_is_rejected() {
let (_dir, path) = cast_with("[0.0, \"o\", \"a\"]\n");
let steps = [checkpoint(0, "one", "first")];
for bad in [0.0, -1.0, f64::NAN, f64::INFINITY, 1e-9, 6e-7, 7.5e-7] {
assert!(
append_checkpoint_frames(&path, &steps, Some(bad)).is_err(),
"hold {bad} should be rejected"
);
}
assert_eq!(events_in(&path).len(), 1);
assert_eq!(MIN_CHECKPOINT_HOLD, 1e-6);
assert!(append_checkpoint_frames(&path, &steps, Some(1e-6)).is_ok());
let times: Vec<f64> = events_in(&path).iter().map(|(at, _)| *at).collect();
assert!(times.windows(2).all(|w| w[1] > w[0]), "{times:?}");
}
#[test]
fn frame_size_is_derived_but_overridable() {
let conv = CastVideoConverter::new();
assert_eq!(conv.frame_width(), 120 * 10 + 20);
assert_eq!(conv.frame_height(), (40.0 * 22.0) as usize + 20);
let conv = CastVideoConverter {
width: Some(640),
height: Some(480),
..Default::default()
};
assert_eq!(conv.frame_width(), 640);
assert_eq!(conv.frame_height(), 480);
}
}