test: add opt-in panel e2e harness
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
const EVENT_PATH_ENV: &str = "YOI_TUI_TEST_EVENTS";
|
||||
|
||||
static EVENT_WRITER: OnceLock<Option<Mutex<File>>> = OnceLock::new();
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct EventEnvelope<'a, T> {
|
||||
ts_ms: u128,
|
||||
surface: &'a str,
|
||||
event: &'a str,
|
||||
data: T,
|
||||
}
|
||||
|
||||
pub(crate) fn emit<T>(surface: &'static str, event: &'static str, data: T)
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
let Some(writer) = EVENT_WRITER.get_or_init(open_event_writer).as_ref() else {
|
||||
return;
|
||||
};
|
||||
let Ok(mut writer) = writer.lock() else {
|
||||
return;
|
||||
};
|
||||
let envelope = EventEnvelope {
|
||||
ts_ms: SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|duration| duration.as_millis())
|
||||
.unwrap_or_default(),
|
||||
surface,
|
||||
event,
|
||||
data,
|
||||
};
|
||||
if serde_json::to_writer(&mut *writer, &envelope).is_ok() {
|
||||
let _ = writer.write_all(b"\n");
|
||||
let _ = writer.flush();
|
||||
}
|
||||
}
|
||||
|
||||
fn open_event_writer() -> Option<Mutex<File>> {
|
||||
let path = std::env::var_os(EVENT_PATH_ENV).map(PathBuf::from)?;
|
||||
if let Some(parent) = path.parent() {
|
||||
let _ = std::fs::create_dir_all(parent);
|
||||
}
|
||||
OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(path)
|
||||
.ok()
|
||||
.map(Mutex::new)
|
||||
}
|
||||
@@ -4,6 +4,7 @@ mod cache;
|
||||
mod command;
|
||||
mod composer_history;
|
||||
mod composer_keys;
|
||||
mod e2e_observer;
|
||||
mod input;
|
||||
pub mod keys;
|
||||
mod markdown;
|
||||
@@ -108,6 +109,7 @@ pub async fn launch(options: LaunchOptions) -> ExitCode {
|
||||
// Always restore the terminal first so any pending eprintln below
|
||||
// shows up cleanly in scrollback rather than inside an active
|
||||
// alternate-screen buffer.
|
||||
e2e_observer::emit("tui", "terminal_cleanup_started", serde_json::json!({}));
|
||||
let mut stdout = io::stdout();
|
||||
let _ = execute!(
|
||||
stdout,
|
||||
@@ -117,9 +119,13 @@ pub async fn launch(options: LaunchOptions) -> ExitCode {
|
||||
);
|
||||
let _ = disable_raw_mode();
|
||||
let _ = execute!(stdout, crossterm::cursor::Show);
|
||||
e2e_observer::emit("tui", "terminal_cleanup_finished", serde_json::json!({}));
|
||||
|
||||
match result {
|
||||
Ok(()) => ExitCode::SUCCESS,
|
||||
Ok(()) => {
|
||||
e2e_observer::emit("tui", "exit", serde_json::json!({ "status": "success" }));
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
Err(e) => {
|
||||
// SpawnError has already been painted into the inline
|
||||
// viewport's final frame, so it's already visible in the
|
||||
@@ -129,6 +135,7 @@ pub async fn launch(options: LaunchOptions) -> ExitCode {
|
||||
if e.downcast_ref::<spawn::SpawnError>().is_none() {
|
||||
eprintln!("yoi: {e}");
|
||||
}
|
||||
e2e_observer::emit("tui", "exit", serde_json::json!({ "status": "failure" }));
|
||||
ExitCode::FAILURE
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,6 +133,7 @@ pub(crate) async fn run(
|
||||
}
|
||||
}
|
||||
let mut next_poll = Instant::now() + MULTI_POD_POLL_INTERVAL;
|
||||
let mut emitted_panel_ready = false;
|
||||
|
||||
loop {
|
||||
if let Some(result) = pending_queue_attention_notice.finish_if_ready().await {
|
||||
@@ -146,6 +147,11 @@ pub(crate) async fn run(
|
||||
}
|
||||
|
||||
terminal.draw(|f| draw(f, app))?;
|
||||
if !emitted_panel_ready {
|
||||
crate::e2e_observer::emit("panel", "panel_ready", serde_json::json!({}));
|
||||
emitted_panel_ready = true;
|
||||
}
|
||||
app.emit_rows_rendered();
|
||||
|
||||
let now = Instant::now();
|
||||
if now >= next_poll {
|
||||
@@ -163,6 +169,7 @@ pub(crate) async fn run(
|
||||
TermEvent::Key(key) => match app.handle_key(key) {
|
||||
MultiPodAction::None => {}
|
||||
MultiPodAction::Quit => {
|
||||
crate::e2e_observer::emit("panel", "quit_requested", serde_json::json!({}));
|
||||
abort_panel_background_work_for_quit(
|
||||
&mut pending_reload,
|
||||
&mut pending_queue_attention_notice,
|
||||
@@ -170,12 +177,22 @@ pub(crate) async fn run(
|
||||
return Ok(MultiPodOutcome::Quit);
|
||||
}
|
||||
MultiPodAction::Open => {
|
||||
crate::e2e_observer::emit(
|
||||
"panel",
|
||||
"action_requested",
|
||||
serde_json::json!({ "action": "open" }),
|
||||
);
|
||||
if let Some(request) = app.prepare_open() {
|
||||
terminal.draw(|f| draw(f, app))?;
|
||||
return Ok(MultiPodOutcome::Open(request));
|
||||
}
|
||||
}
|
||||
MultiPodAction::DispatchTicketAction(request) => {
|
||||
crate::e2e_observer::emit(
|
||||
"panel",
|
||||
"action_requested",
|
||||
serde_json::json!({ "action": "ticket_action" }),
|
||||
);
|
||||
pending_reload.abort();
|
||||
pending_queue_attention_notice.abort();
|
||||
terminal.draw(|f| draw(f, app))?;
|
||||
@@ -187,6 +204,11 @@ pub(crate) async fn run(
|
||||
next_poll = Instant::now() + MULTI_POD_POLL_INTERVAL;
|
||||
}
|
||||
MultiPodAction::LaunchIntake(request) => {
|
||||
crate::e2e_observer::emit(
|
||||
"panel",
|
||||
"action_requested",
|
||||
serde_json::json!({ "action": "launch_intake" }),
|
||||
);
|
||||
pending_reload.abort();
|
||||
pending_queue_attention_notice.abort();
|
||||
terminal.draw(|f| draw(f, app))?;
|
||||
@@ -198,6 +220,11 @@ pub(crate) async fn run(
|
||||
next_poll = Instant::now() + MULTI_POD_POLL_INTERVAL;
|
||||
}
|
||||
MultiPodAction::SendCompanion(request) => {
|
||||
crate::e2e_observer::emit(
|
||||
"panel",
|
||||
"action_requested",
|
||||
serde_json::json!({ "action": "send_companion" }),
|
||||
);
|
||||
pending_reload.abort();
|
||||
pending_queue_attention_notice.abort();
|
||||
terminal.draw(|f| draw(f, app))?;
|
||||
@@ -228,6 +255,14 @@ impl PendingReload {
|
||||
if self.handle.is_some() {
|
||||
return false;
|
||||
}
|
||||
crate::e2e_observer::emit(
|
||||
"panel",
|
||||
"background_task_started",
|
||||
serde_json::json!({
|
||||
"task": "reload",
|
||||
"lifecycle_mode": format!("{lifecycle_mode:?}"),
|
||||
}),
|
||||
);
|
||||
self.handle = Some(tokio::spawn(async move {
|
||||
load_multi_pod_snapshot(None, lifecycle_mode).await
|
||||
}));
|
||||
@@ -252,6 +287,11 @@ impl PendingReload {
|
||||
return None;
|
||||
}
|
||||
let handle = self.handle.take()?;
|
||||
crate::e2e_observer::emit(
|
||||
"panel",
|
||||
"background_task_finished",
|
||||
serde_json::json!({ "task": "reload" }),
|
||||
);
|
||||
Some(match handle.await {
|
||||
Ok(result) => result,
|
||||
Err(e) => Err(MultiPodError::Io(io::Error::other(format!(
|
||||
@@ -262,6 +302,11 @@ impl PendingReload {
|
||||
|
||||
fn abort(&mut self) {
|
||||
if let Some(handle) = self.handle.take() {
|
||||
crate::e2e_observer::emit(
|
||||
"panel",
|
||||
"background_task_aborted",
|
||||
serde_json::json!({ "task": "reload" }),
|
||||
);
|
||||
handle.abort();
|
||||
}
|
||||
}
|
||||
@@ -753,6 +798,57 @@ impl PanelRowHitBox {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct PanelE2eRowKey {
|
||||
kind: &'static str,
|
||||
id: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct PanelE2eRect {
|
||||
x: u16,
|
||||
y: u16,
|
||||
width: u16,
|
||||
height: u16,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct PanelE2eRenderedRow {
|
||||
key: PanelE2eRowKey,
|
||||
title: String,
|
||||
status: Option<String>,
|
||||
action: Option<&'static str>,
|
||||
rect: PanelE2eRect,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct PanelE2eRowsRendered {
|
||||
selected: Option<PanelE2eRowKey>,
|
||||
rows: Vec<PanelE2eRenderedRow>,
|
||||
}
|
||||
|
||||
fn panel_e2e_row_key(key: &PanelRowKey) -> PanelE2eRowKey {
|
||||
match key {
|
||||
PanelRowKey::Ticket(id) => PanelE2eRowKey {
|
||||
kind: "ticket",
|
||||
id: id.clone(),
|
||||
},
|
||||
PanelRowKey::Pod(name) => PanelE2eRowKey {
|
||||
kind: "pod",
|
||||
id: name.clone(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn panel_e2e_rect(rect: Rect) -> PanelE2eRect {
|
||||
PanelE2eRect {
|
||||
x: rect.x,
|
||||
y: rect.y,
|
||||
width: rect.width,
|
||||
height: rect.height,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct MultiPodApp {
|
||||
pub(crate) list: PodList,
|
||||
pub(crate) panel: WorkspacePanelViewModel,
|
||||
@@ -1069,6 +1165,15 @@ impl MultiPodApp {
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
crate::e2e_observer::emit(
|
||||
"panel",
|
||||
"mouse_click",
|
||||
serde_json::json!({
|
||||
"column": event.column,
|
||||
"row": event.row,
|
||||
"target": panel_e2e_row_key(&key),
|
||||
}),
|
||||
);
|
||||
self.select_panel_key(key);
|
||||
true
|
||||
}
|
||||
@@ -1077,6 +1182,42 @@ impl MultiPodApp {
|
||||
self.row_hit_boxes = row_hit_boxes(rows, area);
|
||||
}
|
||||
|
||||
fn emit_rows_rendered(&self) {
|
||||
let rows = self
|
||||
.row_hit_boxes
|
||||
.iter()
|
||||
.map(|hit| {
|
||||
let panel_row = self.panel.row(&hit.key);
|
||||
let (title, status, action) = match panel_row {
|
||||
Some(row) => (
|
||||
row.title.clone(),
|
||||
Some(row.status.clone()),
|
||||
row.next_action.map(NextUserAction::label),
|
||||
),
|
||||
None => match &hit.key {
|
||||
PanelRowKey::Pod(name) => (name.clone(), None, None),
|
||||
PanelRowKey::Ticket(id) => (id.clone(), None, None),
|
||||
},
|
||||
};
|
||||
PanelE2eRenderedRow {
|
||||
key: panel_e2e_row_key(&hit.key),
|
||||
title,
|
||||
status,
|
||||
action,
|
||||
rect: panel_e2e_rect(hit.rect),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
crate::e2e_observer::emit(
|
||||
"panel",
|
||||
"rows_rendered",
|
||||
PanelE2eRowsRendered {
|
||||
selected: self.selected_row.as_ref().map(panel_e2e_row_key),
|
||||
rows,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
fn ensure_selection_visible(&mut self) {
|
||||
let visible = visible_panel_keys(&self.panel, &self.list);
|
||||
if visible.is_empty() {
|
||||
@@ -1127,12 +1268,23 @@ impl MultiPodApp {
|
||||
if let PanelRowKey::Pod(name) = &key {
|
||||
self.list.selected_name = Some(name.clone());
|
||||
}
|
||||
let selected_key = key.clone();
|
||||
self.selected_row = Some(key);
|
||||
crate::e2e_observer::emit(
|
||||
"panel",
|
||||
"selection_changed",
|
||||
serde_json::json!({ "selected": panel_e2e_row_key(&selected_key) }),
|
||||
);
|
||||
}
|
||||
|
||||
fn clear_panel_selection(&mut self) {
|
||||
self.selected_row = None;
|
||||
self.list.selected_name = None;
|
||||
crate::e2e_observer::emit(
|
||||
"panel",
|
||||
"selection_changed",
|
||||
serde_json::json!({ "selected": serde_json::Value::Null }),
|
||||
);
|
||||
}
|
||||
|
||||
fn ensure_composer_target_available(&mut self) {
|
||||
|
||||
Reference in New Issue
Block a user