use ascfix::{
grid::Grid,
primitives::{Box as DiagramBox, BoxStyle, PrimitiveInventory, TextRow},
renderer::render_diagram,
};
#[test]
fn test_no_text_corruption_in_simple_box() {
let input = r"╭────────────────╮
│ Rounded │
│ Corner Box │
╰────────────────╯";
let lines: Vec<&str> = input.lines().collect();
let _grid = Grid::from_lines(&lines);
let mut inventory = PrimitiveInventory::default();
inventory.boxes.push(DiagramBox {
top_left: (0, 0),
bottom_right: (3, 17),
style: BoxStyle::Rounded,
parent_idx: None,
child_indices: Vec::new(),
});
inventory.text_rows.push(TextRow {
row: 1,
start_col: 2,
end_col: 15,
content: "Rounded".to_string(),
});
inventory.text_rows.push(TextRow {
row: 2,
start_col: 2,
end_col: 15,
content: "Corner Box".to_string(),
});
let result_grid = render_diagram(&inventory);
let result = result_grid.render();
assert!(
result.contains("Rounded"),
"Text 'Rounded' should be preserved in output:\n{result}"
);
assert!(
result.contains("Corner Box"),
"Text 'Corner Box' should be preserved in output:\n{result}"
);
assert!(
!result.contains("Corner↑Box"),
"Arrow should not replace text. Output:\n{result}"
);
if let Some(ch) = result_grid.get(2, 7) {
assert!(
ch == ' ' || ch.is_alphabetic(),
"Cell at (2,7) should be space or letter, got '{ch}'"
);
}
}
#[test]
fn test_no_corruption_in_nested_boxes() {
let mut inventory = PrimitiveInventory::default();
inventory.boxes.push(DiagramBox {
top_left: (0, 0),
bottom_right: (4, 18),
style: BoxStyle::Single,
parent_idx: None,
child_indices: vec![1],
});
inventory.boxes.push(DiagramBox {
top_left: (2, 2),
bottom_right: (3, 12),
style: BoxStyle::Single,
parent_idx: Some(0),
child_indices: Vec::new(),
});
inventory.text_rows.push(TextRow {
row: 2,
start_col: 3,
end_col: 11,
content: "Child 1".to_string(),
});
let result_grid = render_diagram(&inventory);
let result = result_grid.render();
assert!(
result.contains("Child 1"),
"Text 'Child 1' should be preserved:\n{result}"
);
assert!(
!result.contains("│ Child │"),
"Pipes should not corrupt text:\n{result}"
);
}
#[test]
fn test_rendering_order_preserves_text() {
let mut inventory = PrimitiveInventory::default();
inventory.boxes.push(DiagramBox {
top_left: (0, 0),
bottom_right: (2, 10),
style: BoxStyle::Single,
parent_idx: None,
child_indices: Vec::new(),
});
inventory.text_rows.push(TextRow {
row: 1,
start_col: 2,
end_col: 8,
content: "Hello".to_string(),
});
let result_grid = render_diagram(&inventory);
assert_eq!(result_grid.get(0, 0), Some('┌')); assert_eq!(result_grid.get(2, 10), Some('┘'));
assert_eq!(
result_grid.get(1, 2),
Some('H'),
"Text should be at position (1,2), not overwritten by border"
);
assert_eq!(
result_grid.get(1, 3),
Some('e'),
"Text should be at position (1,3)"
);
}