mod collapse;
mod fold;
mod json_tsv;
mod preview;
mod render;
use askama::Template;
use crate::output::OutputStore;
pub use preview::stored_preview;
#[derive(Template)]
#[template(path = "json_tsv_output.txt", escape = "none")]
struct JsonTsvOutput<'a> {
tsv: &'a str,
url: &'a str,
path: &'a str,
}
#[derive(Template)]
#[template(path = "folded_code_output.txt", escape = "none")]
struct FoldedCodeOutput<'a> {
folded: &'a str,
}
#[must_use]
pub fn folded_code_content(text: &str) -> Option<String> {
let folded = fold::fold_source_code(text)?;
Some(
FoldedCodeOutput {
folded: folded.trim_end_matches('\n'),
}
.render()
.expect("folded code output template must render"),
)
}
pub async fn compress_text(store: &OutputStore, text: &str) -> std::io::Result<Option<String>> {
if text.is_empty() {
return Ok(None);
}
let cleaned = collapse::collapse_blank_runs(&render::render_terminal(text));
if let Some(tsv) = json_tsv::to_tsv(&cleaned) {
let (url, path) = store.save_raw(cleaned.as_bytes()).await?;
let path = path.display().to_string();
let rendered = JsonTsvOutput {
tsv: tsv.trim_end_matches('\n'),
url: &url,
path: &path,
}
.render()
.expect("json tsv output template must render");
return Ok(Some(rendered));
}
Ok((cleaned != text).then_some(cleaned))
}
#[cfg(test)]
mod tests {
use std::path::Path;
use super::*;
fn store(dir: &Path) -> OutputStore {
OutputStore::at(dir.to_path_buf(), aither_core::AnyIdAllocator::default())
}
async fn compress(text: &str) -> Option<String> {
let dir = tempfile::tempdir().expect("tempdir");
compress_text(&store(dir.path()), text)
.await
.expect("compression succeeds")
}
#[tokio::test]
async fn empty_output_is_left_alone() {
assert!(compress("").await.is_none());
}
#[tokio::test]
async fn coloured_cargo_output_has_no_escape_residue() {
let coloured = "\u{1b}[1m\u{1b}[32m Compiling\u{1b}[0m\u{1b}[1m aither-sandbox\u{1b}[0m v0.1.0 (/src)\n\u{1b}[1m\u{1b}[31merror\u{1b}[0m\u{1b}[1m: expected `;`\u{1b}[0m\n";
let cleaned = compress(coloured).await.expect("escapes are stripped");
assert_eq!(
cleaned,
" Compiling aither-sandbox v0.1.0 (/src)\nerror: expected `;`\n"
);
for residue in ['\u{1b}', '\u{7}'] {
assert!(!cleaned.contains(residue), "{cleaned:?}");
}
assert!(!cleaned.contains("[0m"), "{cleaned:?}");
assert!(!cleaned.contains("[1m"), "{cleaned:?}");
}
#[tokio::test]
async fn coloured_git_status_has_no_escape_residue() {
let coloured = "On branch \u{1b}[1;36moutput-cleaning\u{1b}[m\nChanges not staged for commit:\n\t\u{1b}[31mmodified: sandbox/src/output.rs\u{1b}[m\n";
let cleaned = compress(coloured).await.expect("escapes are stripped");
assert_eq!(
cleaned,
"On branch output-cleaning\nChanges not staged for commit:\n\tmodified: sandbox/src/output.rs\n"
);
}
#[tokio::test]
async fn progress_line_keeps_only_its_last_frame() {
let progress =
"\u{1b}[2K\rFetching 10/90\u{1b}[2K\rFetching 50/90\u{1b}[2K\rFetching 90/90\ndone\n";
let cleaned = compress(progress).await.expect("frames are resolved");
assert_eq!(cleaned, "Fetching 90/90\ndone\n");
}
#[tokio::test]
async fn blank_runs_collapse_but_single_blank_lines_survive() {
let cleaned = compress("alpha\n\n\n\nbeta\n\ngamma\n")
.await
.expect("blank runs collapse");
let lines: Vec<&str> = cleaned.lines().collect();
assert_eq!(lines.len(), 5, "{cleaned}");
assert!(
lines[1].starts_with("[... 3 blank lines collapsed"),
"{cleaned}"
);
assert_eq!(&lines[2..], ["beta", "", "gamma"]);
}
#[tokio::test]
async fn json_table_becomes_tsv_naming_a_readable_original() {
let json = r#"[
{"id": 1, "name": "Alice", "email": "alice@aither.test", "role": "admin"},
{"id": 2, "name": "Bob", "email": "bob@aither.test", "role": "user"},
{"id": 3, "name": "Charlie", "email": "charlie@aither.test", "role": "user"},
{"id": 4, "name": "Diana", "email": "diana@aither.test", "role": "admin"}
]"#;
let dir = tempfile::tempdir().expect("tempdir");
let cleaned = compress_text(&store(dir.path()), json)
.await
.expect("compression succeeds")
.expect("json becomes tsv");
assert!(cleaned.contains('\t'), "{cleaned}");
assert!(cleaned.contains("Original JSON saved to"), "{cleaned}");
let named = cleaned
.rsplit_once(" (")
.and_then(|(_, tail)| tail.strip_suffix(").]"))
.expect("absolute path in the reference");
let stored = std::fs::read_to_string(named).expect("named original is readable");
assert_eq!(
serde_json::from_str::<serde_json::Value>(&stored).expect("stored original is JSON"),
serde_json::from_str::<serde_json::Value>(json).expect("input is JSON"),
"stored original must round-trip"
);
}
#[tokio::test]
async fn plain_text_that_needs_no_cleaning_is_left_alone() {
assert!(compress("all good\n").await.is_none());
}
#[tokio::test]
async fn output_cut_off_mid_escape_leaks_no_brackets() {
let cleaned = compress("partial output\u{1b}[32")
.await
.expect("truncated sequence is consumed");
assert_eq!(cleaned, "partial output");
}
#[tokio::test]
async fn output_without_a_final_newline_is_left_alone() {
assert!(compress("abc").await.is_none());
}
#[tokio::test]
async fn unterminated_progress_line_renders_as_its_last_frame() {
let cleaned = compress("abc\r\u{1b}[2Kxyz")
.await
.expect("the frame is resolved");
assert_eq!(cleaned, "xyz");
}
#[tokio::test]
async fn unified_diff_still_applies_after_cleaning() {
let original =
"fn main() {\n let a = 1;\n\n\n let b = 2;\n println!(\"{a}{b}\");\n}\n";
let dir = tempfile::tempdir().expect("tempdir");
let repo = dir.path();
run_git(repo, &["init", "--quiet"]);
std::fs::write(repo.join("main.rs"), original).expect("write original");
run_git(repo, &["add", "main.rs"]);
run_git(repo, &["commit", "--quiet", "-m", "seed"]);
std::fs::write(
repo.join("main.rs"),
original.replace("let b = 2;", "let b = 20;"),
)
.expect("write patched");
let diff = git_output(repo, &["diff", "--color=always", "--", "main.rs"]);
assert!(diff.contains('\u{1b}'), "git diff should be coloured");
let cleaned = compress_text(&store(repo), &diff)
.await
.expect("compression succeeds")
.expect("coloured diff is cleaned");
assert!(!cleaned.contains('\u{1b}'), "{cleaned}");
run_git(repo, &["checkout", "--", "main.rs"]);
std::fs::write(repo.join("cleaned.patch"), &cleaned).expect("write patch");
let check = crate::test_git::command()
.current_dir(repo)
.args(["apply", "--check", "cleaned.patch"])
.output()
.expect("run git apply");
assert!(
check.status.success(),
"git apply --check failed: {}\npatch:\n{cleaned}",
String::from_utf8_lossy(&check.stderr)
);
}
fn run_git(repo: &Path, args: &[&str]) {
let output = crate::test_git::command()
.args(args)
.current_dir(repo)
.output()
.expect("run git");
assert!(
output.status.success(),
"git {args:?} failed: {}",
String::from_utf8_lossy(&output.stderr)
);
}
fn git_output(repo: &Path, args: &[&str]) -> String {
let output = crate::test_git::command()
.args(args)
.current_dir(repo)
.output()
.expect("run git");
assert!(
output.status.success(),
"git {args:?} failed: {}",
String::from_utf8_lossy(&output.stderr)
);
String::from_utf8(output.stdout).expect("git output is utf-8")
}
}