use std::time::{Duration, Instant};
use ratatui::{buffer::Buffer, layout::Rect};
#[cfg(feature = "matrix")]
use super::draw;
use super::{
done::DONE_TTL,
task::{FOLD_MARK, draw_task},
view::{FADE, Stage},
};
use crate::{
board::Board,
event::{Event, Name, SlotMeta},
text::dur,
};
fn board_with_retry(base: &str, task_cnt: u32) -> Board {
let mut board = Board::new();
board.apply(Event::Plan {
job: task_cnt as usize,
cache: 0,
});
board.apply(Event::Pool(vec![SlotMeta {
host: "api-mew.me".into(),
nth: 1,
}]));
for id in 1..=task_cnt {
board.apply(Event::Start {
id,
name: Name::new("zh", "nt_syntax/i18n/ms/README.md"),
});
board.apply(Event::JobSlot { id, slot: 0 });
board.apply(Event::Retry {
id,
nth: 1,
total: 32,
wait: Duration::from_secs(3),
base: base.into(),
err: "HTTP 429".into(),
});
}
board
}
fn render(board: &Board, w: u16, h: u16) -> String {
let area = Rect::new(0, 0, w, h);
let mut buf = Buffer::empty(area);
draw_task(area, area, &mut buf, board, &[], 0);
let mut out = String::new();
for y in 0..h {
for x in 0..w {
out.push_str(buf[(x, y)].symbol());
}
out.push('\n');
}
out
}
#[test]
fn tree_prints_host_once() {
let text = render(&board_with_retry("https://api-mew.me/v1", 2), 120, 7);
assert_eq!(text.matches("api-mew.me").count(), 1, "{text}");
assert!(text.contains("1/32"), "{text}");
assert!(!text.contains("https://"), "{text}");
assert!(!text.contains("zh →"), "{text}");
let text = render(&board_with_retry("https://api-mew.me/v1", 2), 120, 5);
assert_eq!(text.matches("README.md").count(), 2, "{text}");
assert_eq!(text.matches("1/32").count(), 2, "{text}");
let text = render(&board_with_retry("https://ollama.com/v1", 1), 120, 4);
assert!(text.contains("https://ollama.com/v1"), "{text}");
}
#[test]
fn tree_order_and_fold() {
let mut board = Board::new();
board.apply(Event::Plan { job: 5, cache: 0 });
board.apply(Event::Pool(
[("a.com", 1), ("b.com", 1), ("a.com", 2)]
.map(|(host, nth)| SlotMeta {
host: host.into(),
nth,
})
.into(),
));
assert_eq!(board.slot_li[2].host(), "a.com");
assert_eq!(board.slot_li[2].tag(), "#2");
assert_eq!(board.slot_li[2].root, 0);
for (id, slot) in [(1, 1), (2, 2), (3, 0), (4, 1), (5, 0)] {
board.apply(Event::Start {
id,
name: Name::new("zh", &format!("en/{id}.md")),
});
board.apply(Event::JobSlot { id, slot });
}
let text = render(&board, 80, 13);
let a = text.find("a.com").unwrap_or(usize::MAX);
let b = text.find("b.com").unwrap_or(usize::MAX);
assert!(a < b && b != usize::MAX, "{text}");
assert_eq!(text.matches("a.com").count(), 1, "{text}");
assert!(text.contains("#2"), "{text}");
let text = render(&board, 80, 6);
assert_eq!(text.matches(FOLD_MARK.trim()).count(), 1, "{text}");
}
#[cfg(feature = "matrix")]
#[test]
fn empty_done_row_show_rain() {
let mut board = Board::new();
let mut stage = Stage::default();
board.apply(Event::Pool(vec![SlotMeta {
host: "api.deepseek.com".into(),
nth: 1,
}]));
board.apply(Event::Plan { job: 2, cache: 0 });
let area = Rect::new(0, 0, 80, 24);
let mut buf = Buffer::empty(area);
let found = (0..30).any(|frame| {
buf.reset();
draw(area, &mut buf, &board, &mut stage, frame);
(16..23).any(|y| (0..area.width).any(|x| buf[(x, y)].symbol() != " "))
});
assert!(found, "expected matrix rain in empty completed rows");
}
#[test]
fn done_ttl_keep_summary_and_fail_li() {
let mut board = Board::new();
let mut stage = Stage::default();
board.apply(Event::Plan { job: 2, cache: 0 });
stage.apply(
&mut board,
Event::Start {
id: 1,
name: Name::new("zh", "en/ok.md"),
},
);
stage.apply(
&mut board,
Event::End {
id: 1,
err: None,
active: Duration::from_millis(200),
},
);
stage.apply(&mut board, Event::Log("keep note".into()));
stage.apply(
&mut board,
Event::Start {
id: 2,
name: Name::new("zh", "en/err.md"),
},
);
stage.apply(
&mut board,
Event::End {
id: 2,
err: Some("boom".into()),
active: Duration::ZERO,
},
);
let t_after_fade = Instant::now() + FADE + Duration::from_millis(50);
stage.reap(t_after_fade);
assert_eq!(stage.done.fail_li.len(), 1);
assert_eq!(stage.done.note_li.len(), 1);
let t_after_done = t_after_fade + DONE_TTL + Duration::from_millis(50);
stage.reap(t_after_done);
assert_eq!(board.ok, 1);
assert_eq!(board.fail, 1);
assert_eq!(stage.done.fail_li.len(), 1);
assert_eq!(stage.done.note_li.len(), 1);
if cfg!(feature = "matrix") {
assert!(stage.done.done_li.is_empty());
} else {
assert_eq!(stage.done.done_li.len(), 3);
}
}
#[test]
fn dur_boundary_no_sixty_seconds() {
assert_eq!(dur(59.94), "59.9s");
assert_eq!(dur(59.96), "1m00s");
assert_eq!(dur(3599.96), "1h00m");
}