use std::fs;
use std::path::PathBuf;
use ratatui::buffer::Buffer;
pub(crate) const GOLDEN_WIDTH: u16 = 120;
pub(crate) const GOLDEN_HEIGHT: u16 = 40;
#[must_use]
pub(crate) fn buffer_to_text(buffer: &Buffer) -> String {
let area = *buffer.area();
let mut out = String::new();
for y in 0..area.height {
let mut line = String::new();
for x in 0..area.width {
if let Some(cell) = buffer.cell((x, y)) {
line.push_str(cell.symbol());
}
}
out.push_str(line.trim_end());
out.push('\n');
}
out
}
#[must_use]
fn golden_path(category: &str, name: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("render")
.join("golden")
.join(category)
.join(name)
}
#[track_caller]
pub(crate) fn assert_golden(category: &str, name: &str, actual: &str) {
let path = golden_path(category, name);
if std::env::var_os("UPDATE_GOLDENS").is_some() {
if let Some(parent) = path.parent()
&& let Err(e) = fs::create_dir_all(parent)
{
panic!(
"failed to create golden directory {}: {e}",
parent.display()
);
}
if let Err(e) = fs::write(&path, actual) {
panic!("failed to write golden {}: {e}", path.display());
}
return;
}
let expected = match fs::read_to_string(&path) {
Ok(text) => text,
Err(e) => panic!(
"missing render golden {} ({e}); regenerate with `UPDATE_GOLDENS=1 cargo test`",
path.display(),
),
};
assert_eq!(
actual, expected,
"render golden {category}/{name} mismatch; if this screen change is \
intended, regenerate with `UPDATE_GOLDENS=1 cargo test` and commit the diff",
);
}