#![allow(dead_code, reason = "not all test binaries use every helper")]
#![allow(
clippy::allow_attributes,
reason = "need allow(dead_code) for shared test helpers"
)]
use alacritty_terminal::event::{Event as AlacEvent, EventListener};
use alacritty_terminal::grid::Dimensions as _;
use alacritty_terminal::index::{Column, Line};
use alacritty_terminal::term::cell::Flags;
use alacritty_terminal::term::test::TermSize;
use alacritty_terminal::term::{Config, Term, TermMode};
use alacritty_terminal::vte::ansi::Processor;
use infinity_agent_cli::display::DisplayEvent;
use infinity_agent_cli::term_io::{EventSource, TermOut};
use infinity_agent_cli::terminal::{self, DetachResult, SessionChanged};
use infinity_protocol::{ModelInfo, SessionInfo};
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use std::collections::HashMap;
use std::io::{self, Write};
use std::sync::{Arc, Mutex};
use tokio::sync::mpsc;
type BoxError = Box<dyn std::error::Error + Send + Sync>;
pub type DisplayItem = (Option<infinity_protocol::ThreadRef>, DisplayEvent);
pub trait Emulator: Send {
fn process(&mut self, bytes: &[u8]);
fn size(&self) -> (u16, u16);
fn cursor_position(&self) -> (u16, u16);
fn cursor_hidden(&self) -> bool;
fn title(&self) -> String;
fn resize(&mut self, cols: u16, rows: u16);
fn row_text(&self, row: u16) -> String;
fn row_bg(&self, row: u16) -> String;
fn history_len(&self) -> usize;
fn history_row_text(&self, idx: usize) -> String;
}
pub type SharedEmulator = Arc<Mutex<Box<dyn Emulator>>>;
fn lock_emu(emu: &SharedEmulator) -> std::sync::MutexGuard<'_, Box<dyn Emulator>> {
emu.lock().expect("bug: emulator lock poisoned")
}
pub struct Vt100Emulator {
parser: vt100::Parser,
}
impl Vt100Emulator {
pub fn new(cols: u16, rows: u16) -> Self {
Self {
parser: vt100::Parser::new(rows, cols, 0),
}
}
}
impl Emulator for Vt100Emulator {
fn process(&mut self, bytes: &[u8]) {
self.parser.process(bytes);
}
fn size(&self) -> (u16, u16) {
let (rows, cols) = self.parser.screen().size();
(cols, rows)
}
fn cursor_position(&self) -> (u16, u16) {
let (row, col) = self.parser.screen().cursor_position();
(col, row)
}
fn cursor_hidden(&self) -> bool {
self.parser.screen().hide_cursor()
}
fn title(&self) -> String {
self.parser.screen().title().to_owned()
}
fn resize(&mut self, cols: u16, rows: u16) {
self.parser.set_size(rows, cols);
}
fn row_text(&self, row: u16) -> String {
let screen = self.parser.screen();
let (_, cols) = screen.size();
let mut out = String::new();
let mut col = 0;
while col < cols {
match screen.cell(row, col) {
Some(cell) if !cell.contents().is_empty() => {
out.push_str(&cell.contents());
col += if cell.is_wide() { 2 } else { 1 };
}
_ => {
out.push(' ');
col += 1;
}
}
}
out
}
fn row_bg(&self, row: u16) -> String {
let screen = self.parser.screen();
let (_, cols) = screen.size();
let mut out = String::new();
let mut col = 0;
while col < cols {
match screen.cell(row, col) {
Some(cell) => {
let marker = if cell.bgcolor() == vt100::Color::Default {
' '
} else {
'#'
};
let width = if cell.is_wide() { 2 } else { 1 };
for _ in 0..width {
out.push(marker);
}
col += width;
}
None => {
out.push(' ');
col += 1;
}
}
}
out
}
fn history_len(&self) -> usize {
0
}
fn history_row_text(&self, _idx: usize) -> String {
String::new()
}
}
#[derive(Clone)]
struct TitleProxy(Arc<Mutex<String>>);
impl EventListener for TitleProxy {
fn send_event(&self, event: AlacEvent) {
let mut title = self.0.lock().expect("bug: title lock poisoned");
match event {
AlacEvent::Title(t) => *title = t,
AlacEvent::ResetTitle => title.clear(),
_ => {}
}
}
}
pub struct AlacrittyEmulator {
term: Term<TitleProxy>,
parser: Processor,
title: Arc<Mutex<String>>,
}
impl AlacrittyEmulator {
pub fn new(cols: u16, rows: u16) -> Self {
let title = Arc::new(Mutex::new(String::new()));
let term = Term::new(
Config::default(),
&TermSize::new(cols as usize, rows as usize),
TitleProxy(Arc::clone(&title)),
);
Self {
term,
parser: Processor::new(),
title,
}
}
fn line_text(&self, line: Line) -> String {
let grid = self.term.grid();
let cols = self.term.grid().columns();
let row = &grid[line];
let mut out = String::new();
for col in 0..cols {
let cell = &row[Column(col)];
if cell.flags.contains(Flags::LEADING_WIDE_CHAR_SPACER) {
out.push(' ');
} else if !cell.flags.contains(Flags::WIDE_CHAR_SPACER) {
out.push(cell.c);
}
}
out
}
}
impl Emulator for AlacrittyEmulator {
fn process(&mut self, bytes: &[u8]) {
self.parser.advance(&mut self.term, bytes);
}
fn size(&self) -> (u16, u16) {
let grid = self.term.grid();
(grid.columns() as u16, grid.screen_lines() as u16)
}
fn cursor_position(&self) -> (u16, u16) {
let point = self.term.grid().cursor.point;
(point.column.0 as u16, point.line.0.max(0) as u16)
}
fn cursor_hidden(&self) -> bool {
!self.term.mode().contains(TermMode::SHOW_CURSOR)
}
fn title(&self) -> String {
self.title.lock().expect("bug: title lock poisoned").clone()
}
fn resize(&mut self, cols: u16, rows: u16) {
self.term
.resize(TermSize::new(cols as usize, rows as usize));
}
fn row_text(&self, row: u16) -> String {
self.line_text(Line(row as i32))
}
fn row_bg(&self, row: u16) -> String {
use alacritty_terminal::vte::ansi::{Color as AnsiColor, NamedColor};
let grid = self.term.grid();
let cols = grid.columns();
let line = &grid[Line(row as i32)];
(0..cols)
.map(|col| {
if line[Column(col)].bg == AnsiColor::Named(NamedColor::Background) {
' '
} else {
'#'
}
})
.collect()
}
fn history_len(&self) -> usize {
self.term.grid().history_size()
}
fn history_row_text(&self, idx: usize) -> String {
let history = self.term.grid().history_size() as i32;
self.line_text(Line(idx as i32 - history))
}
}
pub struct VirtualTerm {
emu: SharedEmulator,
}
impl VirtualTerm {
pub fn new(emu: SharedEmulator) -> Self {
Self { emu }
}
}
impl Write for VirtualTerm {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
lock_emu(&self.emu).process(buf);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
impl TermOut for VirtualTerm {
fn size(&mut self) -> io::Result<(u16, u16)> {
Ok(lock_emu(&self.emu).size())
}
fn cursor_position(&mut self) -> io::Result<(u16, u16)> {
Ok(lock_emu(&self.emu).cursor_position())
}
fn enable_raw_mode(&mut self) -> io::Result<()> {
Ok(())
}
fn disable_raw_mode(&mut self) -> io::Result<()> {
Ok(())
}
}
pub struct ScriptedEvents {
rx: mpsc::UnboundedReceiver<Event>,
pending: Option<Event>,
}
impl ScriptedEvents {
pub fn new(rx: mpsc::UnboundedReceiver<Event>) -> Self {
Self { rx, pending: None }
}
}
impl EventSource for ScriptedEvents {
async fn wait_for_event(&mut self) {
if self.pending.is_none() {
match self.rx.recv().await {
Some(event) => self.pending = Some(event),
None => std::future::pending::<()>().await,
}
}
}
fn try_read_event(&mut self) -> io::Result<Option<Event>> {
if let Some(event) = self.pending.take() {
return Ok(Some(event));
}
Ok(self.rx.try_recv().ok())
}
}
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub enum Backend {
#[default]
Vt100,
Alacritty,
}
pub struct HarnessOptions {
pub backend: Backend,
pub cols: u16,
pub rows: u16,
pub model_name: String,
pub provider_id: String,
pub context_window: usize,
pub initial_sessions: HashMap<infinity_protocol::ThreadRef, SessionInfo>,
pub available_models: Vec<ModelInfo>,
pub initial_message: Option<String>,
}
impl Default for HarnessOptions {
fn default() -> Self {
Self {
backend: Backend::default(),
cols: 80,
rows: 20,
model_name: "mock-model".to_owned(),
provider_id: "mock".to_owned(),
context_window: 100_000,
initial_sessions: HashMap::new(),
available_models: vec![
ModelInfo {
display_name: "Mock Model".to_owned(),
provider_id: "mock".to_owned(),
model_id: "mock-model".to_owned(),
context_window: 100_000,
},
ModelInfo {
display_name: "Mock Mini".to_owned(),
provider_id: "mock".to_owned(),
model_id: "mock-mini".to_owned(),
context_window: 32_000,
},
],
initial_message: None,
}
}
}
pub struct TuiHarness {
emu: SharedEmulator,
pub event_tx: mpsc::UnboundedSender<Event>,
pub display_tx: mpsc::UnboundedSender<DisplayItem>,
pub session_tx: mpsc::UnboundedSender<SessionChanged>,
pub model_switched_tx: mpsc::UnboundedSender<terminal::ModelSwitched>,
pub sessions_updated_tx:
mpsc::UnboundedSender<HashMap<infinity_protocol::ThreadRef, SessionInfo>>,
pub detach_result_tx: mpsc::UnboundedSender<DetachResult>,
pub input_rx: mpsc::UnboundedReceiver<String>,
pub load_session_rx: mpsc::UnboundedReceiver<(Option<infinity_protocol::ThreadRef>, bool)>,
pub model_switch_rx: mpsc::UnboundedReceiver<usize>,
pub soft_detach_rx: mpsc::UnboundedReceiver<()>,
pub choice_answered_rx: mpsc::UnboundedReceiver<(infinity_protocol::ChoiceId, usize)>,
pub handle: tokio::task::JoinHandle<Result<bool, BoxError>>,
}
impl TuiHarness {
pub async fn spawn(cols: u16, rows: u16) -> Self {
Self::spawn_with(HarnessOptions {
cols,
rows,
..HarnessOptions::default()
})
.await
}
pub async fn spawn_reflowing(cols: u16, rows: u16) -> Self {
Self::spawn_with(HarnessOptions {
backend: Backend::Alacritty,
cols,
rows,
..HarnessOptions::default()
})
.await
}
pub async fn spawn_with(opts: HarnessOptions) -> Self {
let emu: SharedEmulator = Arc::new(Mutex::new(match opts.backend {
Backend::Vt100 => Box::new(Vt100Emulator::new(opts.cols, opts.rows)),
Backend::Alacritty => {
Box::new(AlacrittyEmulator::new(opts.cols, opts.rows)) as Box<dyn Emulator>
}
}));
let term = VirtualTerm {
emu: Arc::clone(&emu),
};
let (event_tx, event_rx) = mpsc::unbounded_channel();
Self::spawn_with_term(term, emu, event_tx, event_rx, opts).await
}
pub async fn spawn_with_term<T: TermOut + Send + 'static>(
term: T,
emu: SharedEmulator,
event_tx: mpsc::UnboundedSender<Event>,
event_rx: mpsc::UnboundedReceiver<Event>,
opts: HarnessOptions,
) -> Self {
let events = ScriptedEvents {
rx: event_rx,
pending: None,
};
let (input_tx, input_rx) = mpsc::unbounded_channel();
let (display_tx, display_rx) = mpsc::unbounded_channel::<DisplayItem>();
let (load_session_tx, load_session_rx) = mpsc::unbounded_channel();
let (model_switch_tx, model_switch_rx) = mpsc::unbounded_channel();
let (session_tx, session_rx) = mpsc::unbounded_channel();
let (model_switched_tx, model_switched_rx) = mpsc::unbounded_channel();
let (sessions_updated_tx, sessions_updated_rx) = mpsc::unbounded_channel();
let (soft_detach_tx, soft_detach_rx) = mpsc::unbounded_channel();
let (detach_result_tx, detach_result_rx) = mpsc::unbounded_channel();
let (choice_answered_tx, choice_answered_rx) = mpsc::unbounded_channel();
let handle = tokio::spawn(terminal::run(
term,
events,
input_tx,
display_rx,
opts.model_name,
opts.provider_id,
opts.context_window,
opts.initial_sessions,
load_session_tx,
model_switch_tx,
opts.available_models,
opts.initial_message,
session_rx,
model_switched_rx,
sessions_updated_rx,
soft_detach_tx,
detach_result_rx,
choice_answered_tx,
));
let harness = Self {
emu,
event_tx,
display_tx,
session_tx,
model_switched_tx,
sessions_updated_tx,
detach_result_tx,
input_rx,
load_session_rx,
model_switch_rx,
soft_detach_rx,
choice_answered_rx,
handle,
};
harness.settle().await;
harness
}
pub async fn settle(&self) {
tokio::time::sleep(std::time::Duration::from_millis(1)).await;
}
pub fn display(&self, event: DisplayEvent) {
self.display_tx
.send((None, event))
.expect("bug: UI task dropped display channel");
}
pub fn display_for_thread(&self, thread_id: &str, event: DisplayEvent) {
self.display_tx
.send((Some(thread_id.into()), event))
.expect("bug: UI task dropped display channel");
}
pub fn key(&self, code: KeyCode) {
self.key_with(code, KeyModifiers::NONE);
}
pub fn key_with(&self, code: KeyCode, modifiers: KeyModifiers) {
self.event_tx
.send(Event::Key(KeyEvent::new(code, modifiers)))
.expect("bug: UI task dropped event channel");
}
pub fn type_str(&self, text: &str) {
for ch in text.chars() {
self.key(KeyCode::Char(ch));
}
}
pub fn tick(&self) {
self.event_tx
.send(Event::FocusGained)
.expect("bug: UI task dropped event channel");
}
pub fn resize(&self, cols: u16, rows: u16) {
lock_emu(&self.emu).resize(cols, rows);
self.event_tx
.send(Event::Resize(cols, rows))
.expect("bug: UI task dropped event channel");
}
pub fn screen(&self) -> String {
let emu = lock_emu(&self.emu);
render_screen(&**emu, false)
}
pub fn screen_with_scrollback(&self) -> String {
let emu = lock_emu(&self.emu);
render_screen(&**emu, true)
}
pub fn screen_with_bg(&self) -> String {
let emu = lock_emu(&self.emu);
let mut out = render_screen(&**emu, false);
out.push_str("bg:\n");
out.push_str(&render_bg_frame(&**emu));
out
}
}
pub fn render_screen(emu: &dyn Emulator, with_scrollback: bool) -> String {
let (cols, rows) = emu.size();
let mut out = String::new();
if with_scrollback {
let history = emu.history_len();
for idx in 0..history {
out.push_str("~ ");
out.push_str(emu.history_row_text(idx).trim_end());
out.push('\n');
}
}
out.push('┌');
out.push_str(&"─".repeat(cols as usize));
out.push_str("┐\n");
for row in 0..rows {
out.push('│');
out.push_str(&emu.row_text(row));
out.push_str("│\n");
}
out.push('└');
out.push_str(&"─".repeat(cols as usize));
out.push_str("┘\n");
let (cursor_col, cursor_row) = emu.cursor_position();
out.push_str(&format!(
"cursor: row={cursor_row} col={cursor_col}{}\n",
if emu.cursor_hidden() { " (hidden)" } else { "" }
));
let title = emu.title();
if !title.is_empty() {
out.push_str(&format!("title: {title}\n"));
}
out
}
pub fn render_bg_frame(emu: &dyn Emulator) -> String {
let (cols, rows) = emu.size();
let mut out = String::new();
out.push('┌');
out.push_str(&"─".repeat(cols as usize));
out.push_str("┐\n");
for row in 0..rows {
out.push('│');
out.push_str(&emu.row_bg(row));
out.push_str("│\n");
}
out.push('└');
out.push_str(&"─".repeat(cols as usize));
out.push_str("┘\n");
out
}
pub async fn advance_and_redraw(harness: &TuiHarness, duration: std::time::Duration) {
tokio::time::advance(duration).await;
harness.tick();
harness.settle().await;
}