use std::cell::Cell;
use std::cmp::Reverse;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind};
use ratatui::layout::Rect;
use crate::devsbd::notify::Level;
use crate::inbox::{feed, is_url, Inbox, ItemKind, Kind, Marker, Op, State, Thread};
use crate::tui::textarea::TextArea;
use super::forms::{self, FormEdit};
use super::view::{divider_pct, point_in};
use super::{App, Modal, Tab};
use crate::tui::ui;
pub use crate::inbox::View;
pub use crate::inbox::clock::{short_age, stamp};
impl View {
pub fn title(self) -> &'static str {
match self {
View::NeedsYou => "Needs you",
View::Active => "Active",
View::Done => "Done",
View::All => "All",
}
}
fn step(self, step: isize) -> View {
let i = View::ALL.iter().position(|v| *v == self).unwrap_or(0) as isize;
View::ALL[(i + step).clamp(0, View::ALL.len() as isize - 1) as usize]
}
fn shows_notify(self) -> bool {
matches!(self, View::NeedsYou | View::All)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum InboxFocus {
#[default]
List,
Thread,
Form,
Input,
}
const DEFAULT_SPLIT_PCT: u16 = 40;
pub struct InboxView {
content: Inbox,
pub view: View,
pub focus: InboxFocus,
pub split_pct: u16,
pub(super) dragging: bool,
shown: Option<u64>,
pub scroll: u16,
pane_max: Cell<u16>,
list_offset: Cell<usize>,
pub reply: Option<ReplyBox>,
pub form: Option<FormEdit>,
forms_scroll: Cell<usize>,
forms_max: Cell<usize>,
pub(super) forms_follow: Cell<bool>,
pub raw: bool,
}
impl Default for InboxView {
fn default() -> Self {
Self {
content: Inbox::default(),
view: View::default(),
focus: InboxFocus::default(),
split_pct: DEFAULT_SPLIT_PCT,
dragging: false,
shown: None,
scroll: 0,
pane_max: Cell::new(0),
list_offset: Cell::new(0),
reply: None,
form: None,
forms_scroll: Cell::new(0),
forms_max: Cell::new(0),
forms_follow: Cell::new(false),
raw: false,
}
}
}
pub struct ReplyBox {
pub thread: u64,
pub line: TextArea,
}
impl InboxView {
pub fn threads(&self) -> &[Thread] {
&self.content.threads
}
pub fn needs_you(&self) -> usize {
self.content.needs_you()
}
pub fn needs_you_for(&self, owner: &str) -> usize {
self.content.needs_you_for(owner)
}
pub fn count(&self, view: View) -> usize {
self.threads().iter().filter(|t| view.shows(t)).count()
}
pub fn rows(&self) -> Vec<usize> {
let threads = self.threads();
let listed = |t: &Thread| self.view.shows(t) || Some(t.id) == self.shown;
let mut rows: Vec<usize> = (0..threads.len()).filter(|&i| listed(&threads[i])).collect();
rows.sort_by_key(|&i| Reverse(threads[i].changed_at()));
rows
}
pub fn set_pane_max(&self, max: u16) {
self.pane_max.set(max);
}
pub(super) fn scroll_pane(&mut self, delta: i16) -> u16 {
let max = self.pane_max.get();
self.scroll = (self.scroll.min(max) as i32 + delta as i32).clamp(0, max as i32) as u16;
self.scroll
}
pub fn list_offset(&self) -> usize {
self.list_offset.get()
}
pub fn set_list_offset(&self, offset: usize) {
self.list_offset.set(offset);
}
pub fn forms_scroll(&self) -> usize {
self.forms_scroll.get()
}
pub fn take_forms_follow(&self) -> bool {
self.forms_follow.replace(false)
}
pub fn set_forms_scroll(&self, scroll: usize, max: usize) {
self.forms_scroll.set(scroll);
self.forms_max.set(max);
}
#[cfg(test)]
pub(super) fn content_clone(&self) -> Inbox {
self.content.clone()
}
}
pub fn title_of(t: &Thread) -> String {
match t.kind {
Kind::Thread => t.title.clone(),
Kind::Notify => t.head().map_or("", |r| r.msg.lines().next().unwrap_or("")).to_string(),
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ChildInfo {
pub key: String,
pub name: Option<String>,
pub status: Option<String>,
pub stopped: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Tone {
Plain,
Dim,
Bold,
Link,
State(State),
Level(Level),
Text,
Title,
Markdown,
Cursor,
}
pub type PaneLine = Vec<(Tone, String)>;
fn line(tone: Tone, text: impl Into<String>) -> PaneLine {
vec![(tone, text.into())]
}
fn field_rows(items: &[feed::Field]) -> Vec<PaneLine> {
let cols = |s: &str| ratatui::text::Span::raw(s).width();
let width = items.iter().map(|f| cols(&f.label)).max().unwrap_or(0);
items
.iter()
.map(|f| {
let pad = " ".repeat(width - cols(&f.label) + 2);
vec![(Tone::Dim, format!("{}{pad}", f.label)), (Tone::Plain, f.value.clone())]
})
.collect()
}
pub fn chip(t: &Thread) -> PaneLine {
match t.kind {
Kind::Thread => {
let state = t.state.unwrap_or(State::Active);
let (marker, name) = match state {
State::NeedsYou => ("●", "needs you"),
State::Active => ("○", "active"),
State::Done => ("✓", "done"),
};
match t.status.as_deref().filter(|s| !s.trim().is_empty()) {
Some(status) => vec![(Tone::State(state), format!("{marker} ")), (Tone::Text, status.to_string())],
None => line(Tone::State(state), format!("{marker} {name}")),
}
}
Kind::Notify => {
let level = t.head().map_or(Level::Info, |r| r.level);
let marker = match level {
Level::Error => "✖",
Level::Warn => "▲",
Level::Info => "·",
};
line(Tone::Level(level), format!("{marker} {}", level.as_str()))
}
}
}
fn one_line(text: &str) -> String {
text.split_whitespace().collect::<Vec<_>>().join(" ")
}
fn state_name(state: State) -> &'static str {
match state {
State::NeedsYou => "needs you",
State::Active => "active",
State::Done => "done",
}
}
fn marker_text(m: &Marker) -> String {
match m {
Marker::Done { .. } => "done".into(),
Marker::Reopen { .. } => "reopened".into(),
Marker::State { from, to } => format!("{} → {}", state_name(*from), state_name(*to)),
Marker::Status { from: None, to: Some(to) } => format!("status: {}", one_line(to)),
Marker::Status { from, to } => format!(
"status: {} → {}",
from.as_deref().map_or("–".into(), one_line),
to.as_deref().map_or("–".into(), one_line)
),
}
}
fn push_entry(out: &mut Vec<PaneLine>, after_block: &mut bool, block: bool, lines: Vec<PaneLine>) {
if !out.is_empty() && (block || *after_block) {
out.push(Vec::new());
}
*after_block = block;
out.extend(lines);
}
fn feed_lines(t: &Thread, utc_offset: i64) -> Vec<PaneLine> {
let mut out = Vec::new();
let mut after_block = false;
for item in t.feed.iter().rev() {
let at = stamp(item.at, utc_offset);
let block = matches!(item.kind, ItemKind::Message { .. } | ItemKind::Reply { .. } | ItemKind::Submission { .. });
let mut lines = Vec::new();
match &item.kind {
ItemKind::Message { id, blocks, edited, withdrawn, form } => {
let tag = match (edited, withdrawn) {
(_, true) => " (withdrawn)",
(true, false) => " (edited)",
_ => "",
};
lines.push(line(Tone::Dim, format!("{at} {}{tag}", t.owner_name)));
if !withdrawn {
for block in blocks {
match block {
feed::Block::Markdown { text } => lines.push(line(Tone::Markdown, text.clone())),
feed::Block::Fields { items } => lines.extend(field_rows(items)),
feed::Block::Form(f) => {
let answered_above = t.feed.iter().any(
|i| matches!(&i.kind, ItemKind::Submission { message, .. } if message == id),
);
lines.extend(forms::feed_form_lines(f, form.as_ref(), answered_above, utc_offset))
}
}
}
let has_form = blocks.iter().any(|b| matches!(b, feed::Block::Form(_)));
if !has_form && form.as_ref().is_some_and(|r| r.state == crate::inbox::FormState::Withdrawn) {
lines.push(line(Tone::Dim, "form withdrawn"));
}
}
}
ItemKind::Reply { text, .. } => {
lines.push(line(Tone::Dim, format!("{at} you")));
lines.push(line(Tone::Markdown, text.clone()));
}
ItemKind::Action { label, .. } => {
lines.push(vec![(Tone::Dim, format!("{at} ")), (Tone::Text, format!("you: {label}"))]);
}
ItemKind::Submission { message, answers, .. } => {
lines.extend(forms::submission_lines(feed::form_of(&t.feed, message).map(|(f, _)| f), answers, &at));
}
ItemKind::Marker(m) => lines.push(line(Tone::Dim, format!("{at} · {}", marker_text(m)))),
}
push_entry(&mut out, &mut after_block, block, lines);
}
out
}
fn note_lines(t: &Thread, utc_offset: i64) -> Vec<PaneLine> {
let mut out = Vec::new();
let mut after_block = false;
for n in &t.notes {
let r = &n.record;
let lines = vec![
vec![(Tone::Dim, format!("{} ", stamp(r.at, utc_offset))), (Tone::Level(r.level), r.level.as_str().to_string())],
line(Tone::Markdown, r.msg.as_str()),
];
push_entry(&mut out, &mut after_block, true, lines);
}
out
}
fn pending_line(t: &Thread) -> Option<String> {
match t.events.len() {
0 => None,
1 => Some(format!("1 event waiting for {}", t.owner_name)),
n => Some(format!("{n} events waiting for {}", t.owner_name)),
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum HeaderRow {
Line { left: PaneLine, right: Option<String> },
Buttons(Vec<PaneLine>),
}
const TARGET_KEYS: [(char, &str, bool); 4] =
[('o', "VS Code", true), ('t', "Terminal", true), ('l', "Logs", false), ('p', "Port", true)];
const STOPPED: &str = " (stopped)";
pub fn pane_header(t: &Thread, child: Option<&ChildInfo>, target_stopped: bool, utc_offset: i64) -> Vec<HeaderRow> {
let mut title = line(Tone::Title, title_of(t));
if t.link.is_some() || t.head().is_some_and(|r| r.link.is_some()) {
title.push((Tone::Link, " ↗".into()));
}
let mut rows = vec![HeaderRow::Line { left: title, right: None }];
let mut head = chip(t);
head.push((Tone::Dim, format!(" · {}", stamp(t.changed_at(), utc_offset))));
let from = match &t.key {
Some(key) => format!("{} · {key}", t.owner_name),
None => t.owner_name.clone(),
};
rows.push(HeaderRow::Line { left: head, right: Some(from) });
if let Some(c) = child {
let mut row = vec![(Tone::Dim, "child ".to_string())];
match &c.name {
Some(name) => {
row.push((Tone::Plain, name.clone()));
let status = match c.status.as_deref() {
_ if c.stopped => "(stopped)",
Some(status) => status,
None => "not in snapshot",
};
row.push((Tone::Dim, format!(" {status}")));
}
None => row.push((Tone::Dim, format!("{} (no such child)", c.key))),
}
rows.push(HeaderRow::Line { left: row, right: None });
}
if t.archived {
rows.push(HeaderRow::Line { left: line(Tone::Dim, "(archived: instance removed)"), right: None });
}
let mut buttons: Vec<PaneLine> = t
.actions
.iter()
.enumerate()
.map(|(i, a)| {
let mut b = vec![(Tone::Bold, format!("[{}] ", i + 1)), (Tone::Plain, a.label.clone())];
if target_stopped && a.host.as_ref().is_some_and(super::thread_actions::starts_target) {
b.push((Tone::Dim, STOPPED.into()));
}
if a.host.is_some() {
b.push((Tone::Dim, " ⌂".into()));
}
if a.done {
b.push((Tone::Dim, " ✓".into()));
}
b
})
.collect();
let target = t.kind == Kind::Thread && !t.archived && child.is_none_or(|c| c.name.is_some());
if target {
buttons.extend(TARGET_KEYS.iter().map(|(k, label, starts)| {
let mut b = vec![(Tone::Bold, format!("[{k}] ")), (Tone::Plain, label.to_string())];
if target_stopped && *starts {
b.push((Tone::Dim, STOPPED.into()));
}
b
}));
}
if !buttons.is_empty() {
rows.push(HeaderRow::Buttons(buttons));
}
rows
}
pub fn pane_feed(t: &Thread, utc_offset: i64) -> Vec<PaneLine> {
let mut out = Vec::new();
if let Some(pending) = pending_line(t) {
out.push(line(Tone::Dim, pending));
out.push(Vec::new());
}
out.extend(match t.kind {
Kind::Thread => feed_lines(t, utc_offset),
Kind::Notify => note_lines(t, utc_offset),
});
out
}
impl App {
pub fn set_inbox(&mut self, inbox: Inbox) {
let selected = match (self.selected[Tab::Inbox.index()], self.inbox.focus) {
(0, InboxFocus::List) => None,
_ => self.selected_inbox_id(),
};
self.inbox.content = inbox;
self.reselect_inbox(selected);
self.sync_inbox_selection();
self.check_form_edit();
}
pub(super) fn request_inbox(&mut self, op: Op) {
let selected = self.selected_inbox_id();
if !op.enqueues_event() {
self.inbox.content.apply(&op, 0, "tui");
}
self.pending_inbox.push(op);
self.reselect_inbox(selected);
}
pub fn take_pending_inbox(&mut self) -> Vec<Op> {
std::mem::take(&mut self.pending_inbox)
}
pub(super) fn set_tab(&mut self, tab: Tab) {
self.clear_selection();
if self.tab == Tab::Inbox && tab != Tab::Inbox {
self.close_form_edit();
self.inbox.focus = InboxFocus::List;
self.inbox.reply = None;
let unread_notify = self.inbox.threads().iter().any(|t| t.kind == Kind::Notify && t.unread);
if self.inbox.view.shows_notify() && unread_notify {
self.request_inbox(Op::MarkNotifyRead);
}
let selected = self.selected_inbox_id();
self.inbox.shown = None;
self.reselect_inbox(selected);
}
if tab == Tab::Instances && self.tab != tab {
self.needs_proc_fetch = true;
}
self.tab = tab;
self.sync_inbox_selection();
}
pub fn tab_title(&self, tab: Tab) -> String {
match (tab, self.inbox.needs_you()) {
(Tab::Inbox, n) if n > 0 => format!("{} ({n})", tab.title()),
_ => tab.title().to_string(),
}
}
pub fn selected_inbox_thread(&self) -> Option<&Thread> {
let i = *self.inbox.rows().get(self.selected[Tab::Inbox.index()])?;
self.inbox.threads().get(i)
}
fn selected_inbox_id(&self) -> Option<u64> {
self.selected_inbox_thread().map(|t| t.id)
}
fn reselect_inbox(&mut self, id: Option<u64>) {
if let Some(id) = id {
let threads = self.inbox.threads();
if let Some(pos) = self.inbox.rows().iter().position(|&i| threads[i].id == id) {
self.selected[Tab::Inbox.index()] = pos;
}
}
self.clamp_selection();
}
pub(super) fn sync_inbox_selection(&mut self) {
if self.tab != Tab::Inbox {
return;
}
let selected = self.selected_inbox_thread().map(|t| (t.id, t.unread));
let id = selected.map(|(id, _)| id);
if id != self.inbox.shown {
self.clear_selection();
self.close_form_edit();
self.inbox.shown = id;
self.inbox.scroll = 0;
self.inbox.set_forms_scroll(0, 0);
self.inbox.reply = None;
self.inbox.focus = match (id, self.inbox.focus) {
(None, _) => InboxFocus::List,
(Some(_), InboxFocus::Input | InboxFocus::Form) => InboxFocus::Thread,
(Some(_), focus) => focus,
};
self.reselect_inbox(id);
}
if let Some((id, true)) = selected {
self.request_inbox(Op::MarkRead(id));
}
}
pub(super) fn step_inbox_view(&mut self, step: isize) {
let selected = self.selected_inbox_id();
self.inbox.shown = None;
self.inbox.view = self.inbox.view.step(step);
let threads = self.inbox.threads();
let pos = selected.and_then(|id| self.inbox.rows().iter().position(|&i| threads[i].id == id));
if pos.is_none() {
self.inbox.set_list_offset(0);
}
self.selected[Tab::Inbox.index()] = pos.unwrap_or(0);
self.clamp_selection();
}
pub(super) fn focus_inbox_thread(&mut self) {
if self.selected_inbox_thread().is_some() {
self.inbox.focus = InboxFocus::Thread;
}
}
pub(super) fn focus_inbox_input(&mut self) {
if let Some(t) = self.selected_inbox_thread().cloned() {
self.open_reply(&t);
}
}
pub(super) fn selected_inbox_owned(&self) -> Option<Thread> {
self.selected_inbox_thread().cloned()
}
pub(super) fn on_inbox_thread_key(&mut self, key: KeyCode) -> bool {
if !matches!(key, KeyCode::Char('o' | 't' | 'l' | 'p' | 'd' | 'u')) {
return false;
}
let Some(t) = self.selected_inbox_owned() else { return true };
match key {
KeyCode::Char('o') => self.thread_code(&t),
KeyCode::Char('t') => self.thread_terminal(&t),
KeyCode::Char('l') => self.thread_logs(&t),
KeyCode::Char('p') => self.thread_port_prompt(&t),
KeyCode::Char('d') => self.dismiss_selected_notification(),
KeyCode::Char('u') if t.kind == Kind::Thread => self.reopen_thread(&t),
_ => {}
}
true
}
pub fn thread_child(&self, t: &Thread) -> Option<ChildInfo> {
let key = t.child.clone()?;
let name = self.thread_children.get(&t.owner).and_then(|keys| keys.get(&key)).cloned();
let row = name.as_ref().and_then(|name| {
let snapshot = self.snapshot.as_ref()?;
snapshot.instances.iter().find(|r| &r.name == name)
});
let status = row.map(|r| r.status_label().to_string());
let stopped = row.is_some_and(|r| {
r.phase.is_none()
&& matches!(r.status, crate::snapshot::ContainerStatus::Exited(_) | crate::snapshot::ContainerStatus::Missing)
});
Some(ChildInfo { key, name, status, stopped })
}
pub(super) fn on_key_inbox_pane(&mut self, key: KeyEvent) {
if self.inbox.focus == InboxFocus::Input {
self.on_key_reply(key);
return;
}
if self.inbox.focus == InboxFocus::Form {
self.on_key_form(key);
return;
}
self.status = None;
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
let Some(t) = self.selected_inbox_owned() else {
self.inbox.focus = InboxFocus::List;
return;
};
if self.on_inbox_thread_key(key.code) {
return;
}
let link = t.link.clone().or_else(|| t.head().and_then(|r| r.link.clone()));
match key.code {
KeyCode::Esc => self.inbox.focus = InboxFocus::List,
KeyCode::Char('q') => self.should_quit = true,
KeyCode::Char('c') if ctrl => self.should_quit = true,
KeyCode::Char(':') => self.open_prompt(),
KeyCode::Char('?') => self.open_help(),
KeyCode::Enter => match link {
Some(link) => self.request_open_link(link),
None => self.status = Some("no link on this thread".into()),
},
KeyCode::Char(c @ '1'..='9') => self.run_thread_action(&t, c as usize - '1' as usize),
KeyCode::Char('r' | 'i') => self.open_reply(&t),
KeyCode::Tab => {
if !self.focus_inbox_form(None) {
self.open_reply(&t);
}
}
KeyCode::Char('m') => {
self.clear_selection();
self.inbox.raw = !self.inbox.raw;
}
_ => {
let lines = self.inbox.pane_max.get().saturating_add(1);
let mut scroll = self.inbox.scroll;
super::view::scroll_key(&mut scroll, lines, key);
self.inbox.scroll = scroll;
}
}
}
pub(super) fn dismiss_selected_notification(&mut self) {
let Some(t) = self.selected_inbox_owned() else {
return;
};
match t.kind {
Kind::Notify => self.request_inbox(Op::RemoveThread(t.id)),
Kind::Thread => self.mark_thread_done(&t),
}
}
pub(super) fn refuse_user_op(&mut self, t: &Thread) -> bool {
if t.archived {
self.status = Some(format!("archived: `{}` was removed", t.owner_name));
}
t.archived
}
pub(super) fn mark_thread_done(&mut self, t: &Thread) {
if self.refuse_user_op(t) {
return;
}
if t.state == Some(State::Done) {
self.status = Some("already done".into());
return;
}
self.request_inbox(Op::MarkDone(t.id));
let child = self.mark_thread_child(t, true).map(|c| format!(" · {c}")).unwrap_or_default();
self.status = Some(format!("marked done{child} · event queued for {}", t.owner_name));
}
fn reopen_thread(&mut self, t: &Thread) {
if self.refuse_user_op(t) {
return;
}
if t.state != Some(State::Done) {
self.status = Some("not done: nothing to reopen".into());
return;
}
self.request_inbox(Op::Reopen(t.id));
let child = self.mark_thread_child(t, false).map(|c| format!(" · {c}")).unwrap_or_default();
self.status = Some(format!("reopened{child} · event queued for {}", t.owner_name));
}
pub(super) fn open_reply(&mut self, t: &Thread) {
if self.refuse_user_op(t) {
return;
}
if t.compose.is_none() {
self.status = Some("this thread takes no replies".into());
return;
}
self.inbox.reply = Some(ReplyBox { thread: t.id, line: TextArea::new() });
self.inbox.focus = InboxFocus::Input;
}
fn on_key_reply(&mut self, key: KeyEvent) {
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
let newline = key.modifiers.intersects(KeyModifiers::ALT | KeyModifiers::SHIFT);
let Some(rb) = &mut self.inbox.reply else {
self.inbox.focus = InboxFocus::Thread;
return;
};
let line = &mut rb.line;
match key.code {
KeyCode::Esc => self.leave_reply(),
KeyCode::Char('c') if ctrl => self.leave_reply(),
KeyCode::Enter if newline => line.newline(),
KeyCode::Enter => {
let text = line.input().trim().to_string();
if text.is_empty() {
return;
}
line.clear();
let thread = rb.thread;
let owner = self.inbox.threads().iter().find(|t| t.id == thread).map(|t| t.owner_name.clone());
self.request_inbox(Op::Reply { thread, text });
self.status = Some(format!("reply queued for {}", owner.unwrap_or_default()));
}
_ => edit_text(line, key),
}
}
fn leave_reply(&mut self) {
self.inbox.reply = None;
self.inbox.focus = InboxFocus::Thread;
}
pub(super) fn clear_notifications(&mut self) {
self.request_inbox(Op::ClearNotify);
}
pub(super) fn request_open_link(&mut self, link: String) {
if is_url(&link) {
self.status = Some(format!("opening {link}"));
self.pending_open = Some(link);
} else {
self.status = Some(format!("not a URL, not opening: {link}"));
}
}
pub fn take_pending_open(&mut self) -> Option<String> {
self.pending_open.take()
}
pub(super) fn inbox_mouse(&mut self, ev: &MouseEvent, area: Rect) {
if self.tab != Tab::Inbox || self.prompt.is_some() || !matches!(self.modal, Modal::None) {
self.inbox.dragging = false;
return;
}
let rect = ui::inbox_split_rect(area, false, !self.terms.is_empty());
let divider = ui::inbox_divider_col(rect, self.inbox.split_pct);
let pct = divider_pct(ev.column.saturating_sub(rect.x), rect.width);
match ev.kind {
MouseEventKind::Down(MouseButton::Left) => {
if point_in(rect, ev.column, ev.row) && (divider.saturating_sub(1)..=divider).contains(&ev.column) {
self.inbox.dragging = true;
self.inbox.split_pct = pct;
} else {
self.inbox_click(ui::inbox_hit(self, area, ev.column, ev.row));
}
}
MouseEventKind::Drag(MouseButton::Left) if self.inbox.dragging => {
self.inbox.split_pct = pct;
}
MouseEventKind::Up(MouseButton::Left) => self.inbox.dragging = false,
MouseEventKind::ScrollUp => self.inbox_wheel(ui::inbox_hit(self, area, ev.column, ev.row), true),
MouseEventKind::ScrollDown => self.inbox_wheel(ui::inbox_hit(self, area, ev.column, ev.row), false),
_ => {}
}
}
fn inbox_click(&mut self, hit: Option<ui::InboxHit>) {
use ui::{InboxHit, StripHit};
match hit {
Some(InboxHit::Card(pos)) => {
self.selected[Tab::Inbox.index()] = pos;
self.focus_inbox_list();
}
Some(InboxHit::Strip(strip)) => {
let at = |v: View| View::ALL.iter().position(|w| *w == v).unwrap_or(0) as isize;
let step = match strip {
StripHit::Prev => -1,
StripHit::Next => 1,
StripHit::View(v) => at(v) - at(self.inbox.view),
};
if step != 0 {
self.step_inbox_view(step);
}
self.focus_inbox_list();
}
Some(InboxHit::Thread | InboxHit::Hint) if self.inbox.focus == InboxFocus::Input => self.leave_reply(),
Some(InboxHit::Thread | InboxHit::Hint) => {
self.close_form_edit();
self.focus_inbox_thread();
}
Some(InboxHit::Input) if self.inbox.focus == InboxFocus::Input => {}
Some(InboxHit::Input) => {
self.close_form_edit();
self.focus_inbox_input();
}
Some(InboxHit::Form(None)) if self.inbox.focus == InboxFocus::Form => {}
Some(InboxHit::Form(spot)) => {
if self.inbox.focus == InboxFocus::Input {
self.leave_reply();
}
self.focus_inbox_form(spot);
}
Some(InboxHit::ListBlank) | None => {}
}
}
fn inbox_wheel(&mut self, hit: Option<ui::InboxHit>, up: bool) {
use ui::InboxHit;
match hit {
Some(InboxHit::Card(_) | InboxHit::ListBlank) => {
if up {
self.select_up();
} else {
self.select_down();
}
}
Some(InboxHit::Thread) => {
let lines = INBOX_WHEEL_LINES as i16;
self.inbox.scroll_pane(if up { -lines } else { lines });
}
Some(InboxHit::Form(_)) => {
let (scroll, max) = (self.inbox.forms_scroll.get(), self.inbox.forms_max.get());
let lines = INBOX_WHEEL_LINES as usize;
let scroll = if up { scroll.saturating_sub(lines) } else { (scroll + lines).min(max) };
self.inbox.set_forms_scroll(scroll, max);
self.inbox.forms_follow.set(false);
}
_ => {}
}
}
fn focus_inbox_list(&mut self) {
self.close_form_edit();
self.inbox.focus = InboxFocus::List;
self.inbox.reply = None;
}
}
pub(super) fn edit_text(line: &mut TextArea, key: KeyEvent) {
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
match key.code {
KeyCode::Left => line.left(),
KeyCode::Right => line.right(),
KeyCode::Up => line.up(),
KeyCode::Down => line.down(),
KeyCode::Home => line.home(),
KeyCode::End => line.end(),
KeyCode::Backspace => line.backspace(),
KeyCode::Delete => line.delete(),
KeyCode::Char('u') if ctrl => line.clear(),
KeyCode::Char('w') if ctrl => line.delete_word(),
KeyCode::Char(c) if !ctrl => line.insert_char(c),
_ => {}
}
}
const INBOX_WHEEL_LINES: u16 = 3;
#[cfg(test)]
mod tests {
use super::super::test_support::*;
use super::*;
use crate::devsbd::notify::Record;
use crate::inbox::{Action, Compose, ThreadPut};
use crate::snapshot::ContainerStatus;
#[test]
fn fields_render_as_aligned_rows() {
let f = |label: &str, value: &str| feed::Field { label: label.into(), value: value.into() };
let rows = field_rows(&[f("Head", "36b13d41"), f("CI", "green"), f("界面", "wide")]);
let text: Vec<String> = rows.iter().map(|r| r.iter().map(|(_, s)| s.as_str()).collect()).collect();
assert_eq!(text, ["Head 36b13d41", "CI green", "界面 wide"]);
assert_eq!(rows[0], vec![(Tone::Dim, "Head ".to_string()), (Tone::Plain, "36b13d41".to_string())]);
}
fn rec(msg: &str, key: Option<&str>, link: Option<&str>) -> Record {
Record {
level: Level::Info,
key: key.map(str::to_string),
link: link.map(str::to_string),
msg: msg.into(),
at: 0,
}
}
fn push(app: &mut App, name: &str, record: Record) {
let mut inbox = app.inbox.content.clone();
inbox.push(format!("{name}-id"), name.into(), record, true);
app.set_inbox(inbox);
}
fn body(key: &str, state: State) -> ThreadPut {
ThreadPut { key: key.into(), title: key.into(), state, ..ThreadPut::default() }
}
fn put(app: &mut App, name: &str, at: u64, put: ThreadPut) {
let mut inbox = app.inbox.content.clone();
inbox.put(&format!("{name}-id"), name, at, put);
app.set_inbox(inbox);
}
fn rows(app: &App) -> Vec<String> {
app.inbox.rows().iter().map(|&i| title_of(&app.inbox.threads()[i])).collect()
}
fn selected(app: &App) -> Option<String> {
app.selected_inbox_thread().map(title_of)
}
fn thread<'a>(app: &'a App, title: &str) -> &'a Thread {
app.inbox.threads().iter().find(|t| title_of(t) == title).unwrap()
}
fn inbox_tab(app: &mut App) {
app.on_key(key(KeyCode::Char('4')));
}
fn mixed(app: &mut App) {
put(app, "d", 10, body("asks", State::NeedsYou));
put(app, "d", 20, body("busy", State::Active));
put(app, "d", 30, body("over", State::Done));
push(app, "a", Record { at: 40, ..rec("unread note", None, None) });
push(app, "a", Record { at: 50, ..rec("read note", None, None) });
let id = thread(app, "read note").id;
let mut inbox = app.inbox.content.clone();
inbox.apply(&Op::MarkRead(id), 0, "tui");
app.set_inbox(inbox);
}
#[test]
fn view_membership() {
let mut app = new_app();
mixed(&mut app);
put(&mut app, "gone", 60, body("orphan", State::NeedsYou));
let mut inbox = app.inbox.content.clone();
inbox.archive_owner("gone-id");
app.set_inbox(inbox);
let titles = |view: View| -> Vec<String> {
let mut t: Vec<String> =
app.inbox.threads().iter().filter(|t| view.shows(t)).map(title_of).collect();
t.sort();
t
};
assert_eq!(titles(View::NeedsYou), ["asks", "unread note"]);
assert_eq!(titles(View::Active), ["busy"]);
assert_eq!(titles(View::Done), ["over"]);
assert_eq!(titles(View::All).len(), 6);
assert_eq!(app.inbox.count(View::NeedsYou), 2);
assert_eq!(app.inbox.count(View::All), 6);
}
#[test]
fn rows_sort_by_last_change() {
let mut app = new_app();
mixed(&mut app);
app.inbox.view = View::All;
assert_eq!(rows(&app), ["read note", "unread note", "over", "busy", "asks"]);
put(&mut app, "d", 70, ThreadPut { status: Some("ci".into()), ..body("asks", State::NeedsYou) });
assert_eq!(rows(&app)[0], "asks");
}
#[test]
fn badges_count_needs_you_and_unread_notify() {
let mut app = new_app();
mixed(&mut app);
assert_eq!(app.tab_title(Tab::Inbox), "Inbox (2)");
assert_eq!(app.tab_title(Tab::Ports), "Ports");
assert_eq!(app.inbox.needs_you_for("d-id"), 1);
assert_eq!(app.inbox.needs_you_for("a-id"), 1);
assert_eq!(app.inbox.needs_you_for("x-id"), 0);
put(&mut app, "d", 80, ThreadPut { status: Some("new".into()), ..body("busy", State::Active) });
assert_eq!(app.inbox.needs_you(), 2);
}
#[test]
fn arrows_step_views_clamped_and_selection_follows_the_thread() {
let mut app = new_app();
mixed(&mut app);
inbox_tab(&mut app);
assert_eq!(app.inbox.view, View::NeedsYou);
assert_eq!(rows(&app), ["unread note", "asks"]);
app.on_key(key(KeyCode::Left));
assert_eq!(app.inbox.view, View::NeedsYou, "clamped at the left end");
for want in [View::Active, View::Done, View::All] {
app.on_key(key(KeyCode::Right));
assert_eq!(app.inbox.view, want);
}
app.on_key(key(KeyCode::Right));
assert_eq!(app.inbox.view, View::All, "clamped at the right end");
for _ in 0..4 {
app.on_key(key(KeyCode::Down));
}
assert_eq!(selected(&app).as_deref(), Some("asks"));
assert_eq!(app.selected(), 4);
app.on_key(key(KeyCode::Char('v')));
assert_eq!(app.inbox.view, View::All, "`v` no longer switches views");
app.on_key(key(KeyCode::Left));
assert_eq!(app.selected(), 0);
assert_eq!(selected(&app).as_deref(), Some("over"));
for _ in 0..2 {
app.on_key(key(KeyCode::Right));
}
assert_eq!(app.inbox.view, View::All);
assert_eq!(selected(&app).as_deref(), Some("over"));
assert_eq!(app.selected(), 2);
}
#[test]
fn reload_keeps_selection_by_id_and_clamps() {
let mut app = new_app();
mixed(&mut app);
inbox_tab(&mut app);
app.on_key(key(KeyCode::Right));
app.on_key(key(KeyCode::Right));
app.on_key(key(KeyCode::Right));
assert_eq!(selected(&app).as_deref(), Some("over"));
assert_eq!(app.selected(), 2);
put(&mut app, "d", 90, body("newer", State::Active));
assert_eq!(selected(&app).as_deref(), Some("over"));
assert_eq!(app.selected(), 3);
let mut inbox = app.inbox.content.clone();
let ids: Vec<u64> = inbox.threads.iter().map(|t| t.id).collect();
for id in &ids[1..] {
inbox.apply(&Op::RemoveThread(*id), 0, "tui");
}
app.set_inbox(inbox);
assert_eq!(app.selected(), 0);
assert_eq!(rows(&app).len(), 1);
}
#[test]
fn a_focused_thread_keeps_the_cursor_through_new_arrivals() {
let mut app = new_app();
put(&mut app, "d", 10, ThreadPut { compose: Some(Compose::default()), ..body("asks", State::NeedsYou) });
inbox_tab(&mut app);
app.on_key(key(KeyCode::Char('r')));
typed(&mut app, "half");
put(&mut app, "d", 20, body("newer", State::NeedsYou));
assert_eq!(selected(&app).as_deref(), Some("asks"));
assert_eq!(app.inbox.focus, InboxFocus::Input);
assert_eq!(app.inbox.reply.as_ref().unwrap().line.input(), "half");
app.on_key(key(KeyCode::Esc));
app.on_key(key(KeyCode::Esc));
put(&mut app, "d", 30, body("newest", State::NeedsYou));
assert_eq!(selected(&app).as_deref(), Some("asks"), "moved down, so it stays");
app.on_key(key(KeyCode::Up));
app.on_key(key(KeyCode::Up));
put(&mut app, "d", 40, body("latest", State::NeedsYou));
assert_eq!(selected(&app).as_deref(), Some("latest"), "the top row stays on top");
}
#[test]
fn enter_and_esc_step_between_the_zones() {
let mut app = new_app();
mixed(&mut app);
inbox_tab(&mut app);
assert_eq!(app.inbox.focus, InboxFocus::List);
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Enter));
assert_eq!(app.inbox.focus, InboxFocus::Thread);
assert_eq!(selected(&app).as_deref(), Some("asks"));
assert_eq!(app.take_pending_open(), None, "enter on the list doesn't open the link");
app.on_key(key(KeyCode::Esc));
assert_eq!(app.inbox.focus, InboxFocus::List);
assert_eq!(app.tab, Tab::Inbox);
app.on_key(key(KeyCode::Right));
app.on_key(key(KeyCode::Right));
let mut inbox = app.inbox.content.clone();
inbox.apply(&Op::RemoveThread(thread(&app, "over").id), 0, "tui");
app.set_inbox(inbox);
assert_eq!(selected(&app), None);
app.on_key(key(KeyCode::Enter));
assert_eq!(app.inbox.focus, InboxFocus::List);
}
#[test]
fn number_keys_switch_tabs_in_the_list_and_run_actions_in_the_thread() {
let mut app = new_app();
let actions = vec![Action { id: "post".into(), label: "Post replies".into(), ..Action::default() }];
put(&mut app, "d", 10, ThreadPut { actions, ..body("asks", State::NeedsYou) });
let id = thread(&app, "asks").id;
inbox_tab(&mut app);
app.take_pending_inbox();
app.on_key(key(KeyCode::Char('1')));
assert_eq!(app.tab, Tab::Instances);
assert_eq!(app.take_pending_inbox(), []);
inbox_tab(&mut app);
app.on_key(key(KeyCode::Enter));
app.on_key(key(KeyCode::Char('1')));
assert_eq!(app.tab, Tab::Inbox);
assert_eq!(app.take_pending_inbox(), [Op::Act { thread: id, action: "post".into() }]);
}
#[test]
fn the_thread_shadows_dashboard_keys() {
let mut app = new_app();
push(&mut app, "a", rec("note", None, Some("https://x/1")));
put(
&mut app,
"d",
10,
ThreadPut {
link: Some("https://x/pr/1".into()),
actions: vec![Action { id: "post".into(), label: "Post replies".into(), ..Action::default() }],
..body("asks", State::NeedsYou)
},
);
inbox_tab(&mut app);
app.on_key(key(KeyCode::Enter)); assert_eq!(app.inbox.focus, InboxFocus::Thread);
app.take_pending_inbox();
for c in ['2', '3', '4', 'D', 'v', 'T', 'x'] {
app.on_key(key(KeyCode::Char(c)));
}
app.on_key(key(KeyCode::Tab));
app.on_key(key(KeyCode::Right));
assert_eq!(app.tab, Tab::Inbox, "tab keys are shadowed");
assert_eq!(app.inbox.focus, InboxFocus::Thread);
assert_eq!(app.inbox.view, View::NeedsYou, "`→` didn't reach the list");
assert!(app.terms.is_empty(), "`T` opened no terminal");
assert_eq!(app.take_pending_inbox(), [], "`D` dismissed nothing");
assert_eq!(app.inbox.threads().len(), 2);
app.on_key(key(KeyCode::Char('1')));
assert!(app.status.as_deref().unwrap().contains("Post replies"), "{:?}", app.status);
app.on_key(key(KeyCode::Enter));
assert_eq!(app.take_pending_open().as_deref(), Some("https://x/pr/1"));
app.on_key(key(KeyCode::Char('q')));
assert!(app.should_quit);
}
#[test]
fn pane_enter_refuses_non_url_links() {
let mut app = new_app();
push(&mut app, "a", rec("x", None, Some("--help")));
inbox_tab(&mut app);
app.on_key(key(KeyCode::Enter));
app.on_key(key(KeyCode::Enter));
assert_eq!(app.take_pending_open(), None);
assert!(app.status.as_deref().unwrap().starts_with("not a URL"));
}
#[test]
fn pane_scrolls_within_the_rendered_bound_and_resets_on_selection() {
let mut app = new_app();
put(&mut app, "d", 10, body("asks", State::NeedsYou));
put(&mut app, "d", 20, body("other", State::NeedsYou));
inbox_tab(&mut app);
app.on_key(key(KeyCode::Enter));
app.inbox.set_pane_max(3);
for _ in 0..5 {
app.on_key(key(KeyCode::Char('j')));
}
assert_eq!(app.inbox.scroll, 3);
app.on_key(key(KeyCode::PageUp));
assert_eq!(app.inbox.scroll, 0);
app.on_key(key(KeyCode::Char('G')));
assert_eq!(app.inbox.scroll, 3);
app.on_key(key(KeyCode::Esc));
app.on_key(key(KeyCode::Down));
assert_eq!(app.inbox.scroll, 0);
}
#[test]
fn selecting_reads_and_a_change_while_selected_reads_again() {
let mut app = new_app();
put(&mut app, "d", 10, body("asks", State::NeedsYou));
put(&mut app, "d", 20, body("other", State::NeedsYou));
assert_eq!(app.take_pending_inbox(), [], "off the Inbox nothing is read");
let (asks, other) = (thread(&app, "asks").id, thread(&app, "other").id);
inbox_tab(&mut app);
assert_eq!(app.take_pending_inbox(), [Op::MarkRead(other)]);
assert!(thread(&app, "asks").unread);
app.on_key(key(KeyCode::Down));
assert_eq!(app.take_pending_inbox(), [Op::MarkRead(asks)]);
assert!(!thread(&app, "asks").unread);
put(&mut app, "d", 30, ThreadPut { status: Some("ci".into()), ..body("asks", State::NeedsYou) });
assert_eq!(selected(&app).as_deref(), Some("asks"));
assert_eq!(app.take_pending_inbox(), [Op::MarkRead(asks)]);
assert!(!thread(&app, "asks").unread);
put(&mut app, "d", 5, ThreadPut { status: Some("x".into()), ..body("other", State::NeedsYou) });
assert_eq!(app.take_pending_inbox(), []);
assert!(thread(&app, "other").unread);
app.on_key(key(KeyCode::Enter));
let mut inbox = app.inbox.content.clone();
inbox.apply(&Op::RemoveThread(asks), 0, "tui");
app.set_inbox(inbox);
assert_eq!(selected(&app).as_deref(), Some("other"));
assert_eq!(app.take_pending_inbox(), [Op::MarkRead(other)]);
assert_eq!(app.inbox.focus, InboxFocus::Thread);
}
#[test]
fn a_read_notify_record_stays_listed_while_selected() {
let mut app = new_app();
push(&mut app, "a", Record { at: 10, ..rec("n1", None, None) });
push(&mut app, "a", Record { at: 20, ..rec("n2", None, None) });
push(&mut app, "a", Record { at: 30, ..rec("n3", None, None) });
inbox_tab(&mut app);
assert_eq!(app.take_pending_inbox(), [Op::MarkRead(thread(&app, "n3").id)]);
assert_eq!(rows(&app), ["n3", "n2", "n1"]);
assert_eq!(app.inbox.count(View::NeedsYou), 2);
app.on_key(key(KeyCode::Down));
assert_eq!(app.take_pending_inbox(), [Op::MarkRead(thread(&app, "n2").id)]);
assert_eq!(rows(&app), ["n2", "n1"]);
assert_eq!(selected(&app).as_deref(), Some("n2"));
assert!(thread(&app, "n1").unread);
}
#[test]
fn leaving_the_inbox_reads_the_notify_records_it_showed() {
let mut app = new_app();
push(&mut app, "a", rec("one", None, None));
put(&mut app, "d", 10, body("asks", State::NeedsYou));
app.on_key(key(KeyCode::Char('2')));
assert_eq!(app.take_pending_inbox(), []);
inbox_tab(&mut app);
assert_eq!(rows(&app), ["asks", "one"]);
assert_eq!(app.take_pending_inbox(), [Op::MarkRead(thread(&app, "asks").id)]);
app.on_key(key(KeyCode::Char('1')));
assert_eq!(app.take_pending_inbox(), [Op::MarkNotifyRead]);
assert_eq!(app.inbox.needs_you(), 1, "the dispatcher's thread still asks");
push(&mut app, "a", rec("two", None, None));
inbox_tab(&mut app);
app.on_key(key(KeyCode::Right));
app.take_pending_inbox();
app.on_key(key(KeyCode::Char('1')));
assert_eq!(app.take_pending_inbox(), []);
assert!(thread(&app, "two").unread);
}
#[test]
fn leaving_the_tab_resets_focus_and_drops_the_reply_box() {
let mut app = new_app();
open_asks(&mut app, Some(Compose::default()));
app.on_key(key(KeyCode::Char('r')));
typed(&mut app, "half");
app.set_tab(Tab::Instances);
assert_eq!(app.inbox.focus, InboxFocus::List);
assert!(app.inbox.reply.is_none());
inbox_tab(&mut app);
assert_eq!(app.inbox.focus, InboxFocus::List);
app.on_key(key(KeyCode::Enter));
app.next_tab();
assert_eq!(app.inbox.focus, InboxFocus::List);
}
#[test]
fn d_dismisses_notify_only_and_capital_d_clears_notify_only() {
let mut app = new_app();
put(&mut app, "d", 10, body("asks", State::NeedsYou));
push(&mut app, "a", Record { at: 20, ..rec("n1", None, None) });
push(&mut app, "a", Record { at: 30, ..rec("n2", None, None) });
app.inbox.view = View::All;
inbox_tab(&mut app);
assert_eq!(rows(&app), ["n2", "n1", "asks"]);
let (n1, n2) = (thread(&app, "n1").id, thread(&app, "n2").id);
app.take_pending_inbox();
app.on_key(key(KeyCode::Char('d')));
assert_eq!(rows(&app), ["n1", "asks"]);
assert_eq!(app.take_pending_inbox(), [Op::RemoveThread(n2), Op::MarkRead(n1)]);
app.on_key(key(KeyCode::Down));
let asks = thread(&app, "asks").id;
assert_eq!(app.take_pending_inbox(), [Op::MarkRead(asks)]);
app.on_key(key(KeyCode::Char('d')));
assert_eq!(app.take_pending_inbox(), [Op::MarkDone(asks)]);
assert_eq!(app.status.as_deref(), Some("marked done · event queued for d"));
assert_eq!(rows(&app), ["n1", "asks"], "applied by the store, not here");
app.on_key(key(KeyCode::Char('D')));
assert_eq!(app.take_pending_inbox(), [Op::ClearNotify]);
assert_eq!(rows(&app), ["asks"]);
assert_eq!(app.selected(), 0, "re-clamped");
}
#[test]
fn list_keys_act_on_the_selected_thread() {
let mut app = new_app();
put(&mut app, "d", 10, body("asks", State::NeedsYou));
put(&mut app, "d", 20, body("other", State::NeedsYou));
inbox_tab(&mut app);
app.on_key(key(KeyCode::Down));
app.take_pending_inbox();
app.on_key(key(KeyCode::Char('o')));
assert_eq!(app.pending_action, None);
assert_eq!(app.status.as_deref(), Some("`d` not found"));
assert_eq!(app.inbox.focus, InboxFocus::List);
app.on_key(key(KeyCode::Char('d')));
assert_eq!(app.take_pending_inbox(), [Op::MarkDone(thread(&app, "asks").id)]);
app.on_key(key(KeyCode::Char('u')));
assert_eq!(app.status.as_deref(), Some("not done: nothing to reopen"));
}
#[test]
fn d_off_the_inbox_does_not_dismiss() {
let mut app = new_app();
push(&mut app, "a", rec("1", None, None));
app.on_key(key(KeyCode::Char('d')));
app.on_key(key(KeyCode::Char('D')));
assert_eq!(app.inbox.threads().len(), 1);
assert_eq!(app.take_pending_inbox(), []);
}
fn texts(lines: &[PaneLine]) -> Vec<String> {
lines.iter().map(|l| l.iter().map(|(_, s)| s.as_str()).collect()).collect()
}
fn header_texts(rows: &[HeaderRow]) -> Vec<String> {
rows.iter()
.map(|r| match r {
HeaderRow::Line { left, right: None } => texts(std::slice::from_ref(left)).remove(0),
HeaderRow::Line { left, right: Some(right) } => {
format!("{} | {right}", texts(std::slice::from_ref(left)).remove(0))
}
HeaderRow::Buttons(b) => format!("buttons: {}", texts(b).join(" ")),
})
.collect()
}
fn header_actions() -> Vec<Action> {
vec![
Action {
id: "open".into(),
label: "Open draft".into(),
host: Some(crate::inbox::thread::HostVerb::Terminal(Default::default())),
..Action::default()
},
Action { id: "post".into(), label: "Post replies".into(), ..Action::default() },
Action { id: "done".into(), label: "Done".into(), done: true, ..Action::default() },
]
}
#[test]
fn pane_header_with_a_child_link_and_actions() {
let mut app = new_app();
let full = ThreadPut {
link: Some("https://x/pr/1".into()),
status: Some("review".into()),
child: Some("pr-1".into()),
actions: header_actions(),
compose: Some(Compose { placeholder: Some("next run".into()), hint: Some("starts a run".into()) }),
..body("asks", State::NeedsYou)
};
put(&mut app, "d", 10, full);
app.thread_children.insert("d-id".into(), [("pr-1".to_string(), "inst0".to_string())].into());
app.set_snapshot(snapshot_with_status(1, running()));
let t = thread(&app, "asks").clone();
let child = app.thread_child(&t).unwrap();
assert_eq!((child.name.as_deref(), child.status.as_deref(), child.stopped), (Some("inst0"), Some("running"), false));
let rows = pane_header(&t, Some(&child), false, 0);
assert_eq!(header_texts(&rows), [
"asks ↗".to_string(),
format!("● review · {} | d · asks", stamp(10, 0)),
"child inst0 running".into(),
"buttons: [1] Open draft ⌂ [2] Post replies [3] Done ✓ [o] VS Code [t] Terminal [l] Logs [p] Port"
.into(),
]);
let HeaderRow::Line { left: title, .. } = &rows[0] else { panic!() };
assert_eq!(title[..], [(Tone::Title, "asks".into()), (Tone::Link, " ↗".into())]);
let HeaderRow::Buttons(buttons) = &rows[3] else { panic!() };
for b in [&buttons[0], &buttons[4]] {
assert_eq!(b[..2].iter().map(|(t, _)| *t).collect::<Vec<_>>(), [Tone::Bold, Tone::Plain]);
}
let all = header_texts(&rows).join("\n");
for gone in ["next run", "starts a run", "waiting for", "https://x"] {
assert!(!all.contains(gone), "{gone} in {all}");
}
app.set_snapshot(snapshot_with_status(1, ContainerStatus::Exited("Exited (0)".into())));
let child = app.thread_child(&t).unwrap();
assert!(child.stopped);
assert!(app.thread_target_stopped(&t));
let text = header_texts(&pane_header(&t, Some(&child), app.thread_target_stopped(&t), 0));
assert_eq!(text[2], "child inst0 (stopped)");
assert_eq!(
text[3],
"buttons: [1] Open draft (stopped) ⌂ [2] Post replies [3] Done ✓ [o] VS Code (stopped) \
[t] Terminal (stopped) [l] Logs [p] Port (stopped)"
);
for (phase, label) in [(in_flight("building"), "building"), (failed("postCreateCommand"), "failed")] {
app.set_snapshot(snapshot_with_phase(1, ContainerStatus::Missing, phase));
let child = app.thread_child(&t).unwrap();
assert!(!child.stopped && !app.thread_target_stopped(&t));
let text = header_texts(&pane_header(&t, Some(&child), false, 0));
assert_eq!(text[2], format!("child inst0 {label}"));
}
app.set_snapshot(snapshot_with_phase(1, ContainerStatus::Missing, in_flight("building")));
app.thread_terminal(&t);
assert_eq!(app.status.as_deref(), Some("`inst0` is still building"));
assert_eq!(app.take_pending_action(), None);
app.set_snapshot(snapshot_with_phase(1, ContainerStatus::Exited("Exited (1)".into()), failed("postStartCommand")));
app.thread_terminal(&t);
assert_eq!(app.status.as_deref(), Some("`inst0` failed at postStartCommand: rebuild it first (`s` on its row)"));
assert_eq!(app.take_pending_action(), None);
app.thread_children.clear();
let child = app.thread_child(&t).unwrap();
assert_eq!(child.name, None);
let text = header_texts(&pane_header(&t, Some(&child), false, 0));
assert_eq!(text[2], "child pr-1 (no such child)");
assert_eq!(text[3], "buttons: [1] Open draft ⌂ [2] Post replies [3] Done ✓");
}
#[test]
fn pane_header_without_a_child_archived_and_bare() {
let mut app = new_app();
put(&mut app, "d", 10, body("asks", State::Active));
let t = thread(&app, "asks").clone();
assert_eq!(header_texts(&pane_header(&t, None, false, 0)), [
"asks".to_string(),
format!("○ active · {} | d · asks", stamp(10, 0)),
"buttons: [o] VS Code [t] Terminal [l] Logs [p] Port".into(),
]);
assert!(!app.thread_target_stopped(&t), "not in a snapshot: unknown, not stopped");
let mut snap = snapshot_with_status(1, ContainerStatus::Exited("Exited (0)".into()));
snap.instances[0].instance_id = "d-id".into();
app.set_snapshot(snap);
assert!(app.thread_target_stopped(&t));
assert_eq!(
header_texts(&pane_header(&t, None, true, 0))[2],
"buttons: [o] VS Code (stopped) [t] Terminal (stopped) [l] Logs [p] Port (stopped)"
);
let mut inbox = app.inbox.content.clone();
inbox.archive_owner("d-id");
app.set_inbox(inbox);
let t = thread(&app, "asks").clone();
assert_eq!(header_texts(&pane_header(&t, None, false, 0)), [
"asks".to_string(),
format!("○ active · {} | d · asks", stamp(10, 0)),
"(archived: instance removed)".into(),
]);
}
#[test]
fn pane_header_of_a_notify_thread_is_title_and_level() {
let mut app = new_app();
push(&mut app, "a", Record { level: Level::Warn, at: 5, ..rec("disk low\nmore", Some("k"), Some("https://x")) });
let t = thread(&app, "disk low").clone();
assert_eq!(header_texts(&pane_header(&t, None, false, 0)), [
"disk low ↗".to_string(),
format!("▲ warn · {} | a · k", stamp(5, 0)),
]);
push(&mut app, "a", rec("unkeyed", None, None));
let t = thread(&app, "unkeyed").clone();
assert_eq!(header_texts(&pane_header(&t, None, false, 0))[1], format!("· info · {} | a", stamp(0, 0)));
}
#[test]
fn pane_feed_is_newest_first() {
let mut app = new_app();
let header = |state: State, status: &str| ThreadPut {
status: Some(status.into()),
compose: Some(Compose::default()),
actions: vec![Action { id: "post".into(), label: "Post replies".into(), ..Action::default() }],
..body("asks", state)
};
let send = |text: &str| crate::inbox::MessageSend {
thread: "asks".into(),
id: "m".into(),
blocks: vec![feed::Block::Markdown { text: text.into() }],
};
put(&mut app, "d", 10, header(State::Active, "running"));
let id = thread(&app, "asks").id;
let mut inbox = app.inbox.content.clone();
inbox.send("d-id", 10, send("one"));
inbox.apply(&Op::Act { thread: id, action: "post".into() }, 20, "tui");
inbox.apply(&Op::MarkDone(id), 20, "tui");
inbox.apply(&Op::Reopen(id), 20, "tui");
inbox.put("d-id", "d", 30, header(State::NeedsYou, "review"));
inbox.send("d-id", 30, send("two"));
inbox.apply(&Op::Reply { thread: id, text: "ok".into() }, 40, "tui");
let fields = vec![feed::Block::Fields { items: vec![feed::Field { label: "CI".into(), value: "green".into() }] }];
let t = inbox.threads.iter_mut().find(|t| t.id == id).unwrap();
let seq = t.feed.iter().map(|i| i.seq).max().unwrap_or(0) + 1;
t.feed.push(feed::FeedItem {
seq,
at: 50,
kind: ItemKind::Message { id: "f".into(), blocks: fields, edited: false, withdrawn: false, form: None },
});
app.set_inbox(inbox);
let (s10, s20, s30, s40, s50) = (stamp(10, 0), stamp(20, 0), stamp(30, 0), stamp(40, 0), stamp(50, 0));
let lines = pane_feed(thread(&app, "asks"), 0);
let text = texts(&lines);
assert_eq!(text, [
"4 events waiting for d".to_string(),
String::new(),
format!("{s50} d"),
"CI green".into(),
String::new(),
format!("{s40} you"),
"ok".into(),
String::new(),
format!("{s30} · status: running → review"),
format!("{s30} · active → needs you"),
format!("{s20} · reopened"),
format!("{s20} · done"),
format!("{s20} you: Post replies"),
String::new(),
format!("{s10} d (edited)"),
"two".into(),
], "{text:#?}");
assert_eq!(lines[15], [(Tone::Markdown, "two".to_string())]);
assert_eq!(lines[11], [(Tone::Dim, format!("{s20} · done"))]);
let mut inbox = app.inbox.content.clone();
inbox.withdraw("d-id", "asks", "m");
app.set_inbox(inbox);
let text = texts(&pane_feed(thread(&app, "asks"), 0));
assert_eq!(text.last(), Some(&format!("{s10} d (withdrawn)")), "{text:#?}");
assert!(!text.iter().any(|l| l == "two"), "{text:#?}");
}
#[test]
fn notify_feed_is_the_records_newest_first() {
let mut app = new_app();
push(&mut app, "a", Record { at: 1, ..rec("v1", Some("k"), None) });
push(&mut app, "a", Record { at: 2, level: Level::Error, ..rec("v2", Some("k"), None) });
push(&mut app, "a", Record { at: 3, ..rec("v3\n\n- body", Some("k"), None) });
let lines = pane_feed(thread(&app, "v3"), 0);
assert_eq!(texts(&lines), [
format!("{} info", stamp(3, 0)),
"v3\n\n- body".into(),
String::new(),
format!("{} error", stamp(2, 0)),
"v2".into(),
String::new(),
format!("{} info", stamp(1, 0)),
"v1".into(),
]);
assert_eq!(lines[3][1], (Tone::Level(Level::Error), "error".into()));
assert_eq!(lines[1], [(Tone::Markdown, "v3\n\n- body".to_string())]);
}
fn flush(app: &mut App, now: u64) -> Vec<Op> {
let ops = app.take_pending_inbox();
let mut inbox = app.inbox.content.clone();
ops.iter().for_each(|op| inbox.apply(op, now, "tui"));
app.set_inbox(inbox);
ops
}
fn typed(app: &mut App, text: &str) {
for c in text.chars() {
app.on_key(key(KeyCode::Char(c)));
}
}
fn open_asks(app: &mut App, compose: Option<Compose>) -> u64 {
put(app, "d", 10, ThreadPut { compose, ..body("asks", State::NeedsYou) });
inbox_tab(app);
app.on_key(key(KeyCode::Enter));
flush(app, 10);
thread(app, "asks").id
}
#[test]
fn r_focuses_the_input_which_sends_replies_and_stays() {
let mut app = new_app();
let id = open_asks(&mut app, Some(Compose { placeholder: Some("next run".into()), hint: None }));
app.on_key(key(KeyCode::Char('r')));
assert_eq!(app.inbox.focus, InboxFocus::Input);
let rb = app.inbox.reply.as_ref().expect("reply box open");
assert_eq!((rb.thread, rb.line.input()), (id, ""));
typed(&mut app, "do q1x");
app.on_key(key(KeyCode::Backspace));
app.on_key(key(KeyCode::Home));
app.on_key(key(KeyCode::Char('>')));
assert!(!app.should_quit);
assert_eq!(app.inbox.reply.as_ref().unwrap().line.input(), ">do q1");
assert_eq!(app.take_pending_inbox(), []);
app.on_key(key(KeyCode::Enter));
assert_eq!(app.inbox.focus, InboxFocus::Input, "still in the input");
assert_eq!(app.inbox.reply.as_ref().unwrap().line.input(), "", "cleared for the next one");
assert_eq!(app.status.as_deref(), Some("reply queued for d"));
assert_eq!(flush(&mut app, 20), [Op::Reply { thread: id, text: ">do q1".into() }]);
typed(&mut app, "more");
app.on_key(key(KeyCode::Enter));
assert_eq!(app.take_pending_inbox(), [Op::Reply { thread: id, text: "more".into() }]);
assert_eq!(app.inbox.focus, InboxFocus::Input);
app.on_key(key(KeyCode::Esc));
assert_eq!(app.inbox.focus, InboxFocus::Thread);
app.on_key(key(KeyCode::Char('i')));
assert_eq!(app.inbox.focus, InboxFocus::Input, "`i` focuses it too");
let text = texts(&pane_feed(thread(&app, "asks"), 0));
assert!(text.windows(2).any(|w| w[0].ends_with(" you") && w[1] == ">do q1"), "{text:#?}");
assert!(text.contains(&"1 event waiting for d".to_string()), "{text:#?}");
}
#[test]
fn pane_head_is_the_chip_then_the_stamp() {
let mut app = new_app();
put(&mut app, "d", 10, ThreadPut { status: Some("review `drafts`".into()), ..body("asks", State::NeedsYou) });
put(&mut app, "d", 10, body("idle", State::Active));
put(&mut app, "d", 10, ThreadPut { status: Some("merged".into()), ..body("fin", State::Done) });
push(&mut app, "a", Record { level: Level::Error, ..rec("boom", None, None) });
let head = |title: &str| match pane_header(thread(&app, title), None, false, 0).swap_remove(1) {
HeaderRow::Line { left, .. } => left,
row => panic!("{row:?}"),
};
let at10 = (Tone::Dim, format!(" · {}", stamp(10, 0)));
assert_eq!(head("asks"), [
(Tone::State(State::NeedsYou), "● ".into()),
(Tone::Text, "review `drafts`".into()),
at10.clone()
]);
assert_eq!(head("idle"), [(Tone::State(State::Active), "○ active".into()), at10.clone()]);
assert_eq!(head("fin"), [(Tone::State(State::Done), "✓ ".into()), (Tone::Text, "merged".into()), at10]);
assert_eq!(head("boom")[..2], [
(Tone::Level(Level::Error), "✖ error".into()),
(Tone::Dim, format!(" · {}", stamp(thread(&app, "boom").changed_at(), 0)))
]);
for title in ["asks", "idle", "fin", "boom"] {
assert!(head(title).starts_with(&chip(thread(&app, title))), "pane head starts with the chip: {title}");
}
}
#[test]
fn a_multi_line_reply_keeps_its_lines_as_markdown() {
let mut app = new_app();
let id = open_asks(&mut app, Some(Compose::default()));
let mut inbox = app.inbox.content.clone();
inbox.apply(&Op::Reply { thread: id, text: "first line\n\n- a\n- b".into() }, 20, "tui");
app.set_inbox(inbox);
let lines = pane_feed(thread(&app, "asks"), 0);
let at = lines.iter().position(|l| l.len() == 1 && l[0].0 == Tone::Dim && l[0].1.ends_with(" you")).unwrap();
assert_eq!(lines[at][0].1, format!("{} you", stamp(20, 0)));
assert_eq!(lines[at + 1], [(Tone::Markdown, "first line\n\n- a\n- b".to_string())]);
}
#[test]
fn the_reply_box_cancels_and_ignores_empty() {
let mut app = new_app();
open_asks(&mut app, Some(Compose::default()));
app.on_key(key(KeyCode::Char('r')));
typed(&mut app, " ");
app.on_key(key(KeyCode::Enter));
assert!(app.inbox.reply.is_some());
typed(&mut app, "half");
app.on_key(key(KeyCode::Esc));
assert!(app.inbox.reply.is_none());
assert_eq!(app.inbox.focus, InboxFocus::Thread, "esc steps back to the thread");
assert_eq!(app.take_pending_inbox(), []);
app.on_key(key(KeyCode::Esc));
app.on_key(key(KeyCode::Char('r')));
assert_eq!(app.inbox.focus, InboxFocus::Input);
assert!(app.inbox.reply.is_some());
}
#[test]
fn a_selection_change_drops_the_reply_box() {
let mut app = new_app();
open_asks(&mut app, Some(Compose::default()));
put(&mut app, "d", 20, body("other", State::NeedsYou));
app.on_key(key(KeyCode::Char('r')));
typed(&mut app, "half");
let mut inbox = app.inbox.content.clone();
inbox.apply(&Op::RemoveThread(thread(&app, "asks").id), 0, "tui");
app.set_inbox(inbox);
assert_eq!(selected(&app).as_deref(), Some("other"));
assert!(app.inbox.reply.is_none());
assert_eq!(app.inbox.focus, InboxFocus::Thread);
}
#[test]
fn m_in_the_thread_toggles_raw_for_every_thread() {
let mut app = new_app();
open_asks(&mut app, Some(Compose::default()));
put(&mut app, "d", 20, body("other", State::NeedsYou));
app.on_key(key(KeyCode::Char('m')));
assert!(app.inbox.raw);
app.on_key(key(KeyCode::Esc));
app.on_key(key(KeyCode::Up));
assert_eq!(selected(&app).as_deref(), Some("other"));
assert!(app.inbox.raw);
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('r')));
app.on_key(key(KeyCode::Char('m')));
assert_eq!(app.inbox.reply.as_ref().unwrap().line.input(), "m");
assert!(app.inbox.raw);
app.on_key(key(KeyCode::Esc));
app.on_key(key(KeyCode::Char('m')));
assert!(!app.inbox.raw);
}
#[test]
fn r_without_a_reply_box_is_a_hint() {
let mut app = new_app();
open_asks(&mut app, None);
app.on_key(key(KeyCode::Char('r')));
assert!(app.inbox.reply.is_none());
assert_eq!(app.status.as_deref(), Some("this thread takes no replies"));
assert_eq!(app.inbox.focus, InboxFocus::Thread, "focus stays");
app.on_key(key(KeyCode::Esc));
app.on_key(key(KeyCode::Char('i')));
assert_eq!(app.status.as_deref(), Some("this thread takes no replies"));
assert_eq!(app.inbox.focus, InboxFocus::List, "focus stays");
assert_eq!(app.take_pending_inbox(), []);
}
#[test]
fn modified_thread_keys_do_nothing() {
let mut app = new_app();
let id = open_asks(&mut app, None);
app.take_pending_inbox();
for c in ['d', 'u', '1', 'm', 'q'] {
app.on_key(key_mods(KeyCode::Char(c), KeyModifiers::ALT));
app.on_key(key_mods(KeyCode::Char(c), KeyModifiers::CONTROL));
}
assert_eq!(app.take_pending_inbox(), []);
assert!(!app.inbox.raw && !app.should_quit);
assert_eq!(app.inbox.focus, InboxFocus::Thread);
app.on_key(key(KeyCode::Esc));
app.on_key(key_mods(KeyCode::Char('d'), KeyModifiers::CONTROL));
app.on_key(key_mods(KeyCode::Char('D'), KeyModifiers::ALT | KeyModifiers::SHIFT));
assert_eq!(app.take_pending_inbox(), []);
app.on_key(key(KeyCode::Char('d')));
assert_eq!(app.take_pending_inbox(), [Op::MarkDone(id)]);
}
#[test]
fn pane_d_marks_done_and_u_reopens() {
let mut app = new_app();
let id = open_asks(&mut app, None);
app.on_key(key(KeyCode::Char('u')));
assert_eq!(app.status.as_deref(), Some("not done: nothing to reopen"));
app.on_key(key(KeyCode::Char('d')));
assert_eq!(flush(&mut app, 20), [Op::MarkDone(id)]);
assert_eq!(thread(&app, "asks").state, Some(State::Done));
assert_eq!(app.inbox.focus, InboxFocus::Thread, "the pane stays on the thread");
app.on_key(key(KeyCode::Char('d')));
assert_eq!(app.status.as_deref(), Some("already done"));
assert_eq!(app.take_pending_inbox(), []);
app.on_key(key(KeyCode::Char('u')));
assert_eq!(app.status.as_deref(), Some("reopened · event queued for d"));
assert_eq!(flush(&mut app, 30), [Op::Reopen(id)]);
let t = thread(&app, "asks");
assert_eq!(t.state, Some(State::Active));
assert_eq!(t.events.len(), 2);
let text = texts(&pane_feed(t, 0));
assert!(text.contains(&format!("{} · done", stamp(20, 0))), "{text:#?}");
assert!(text.contains(&format!("{} · reopened", stamp(30, 0))), "{text:#?}");
assert!(text.contains(&"2 events waiting for d".to_string()), "{text:#?}");
}
#[test]
fn archived_threads_take_no_user_ops() {
let mut app = new_app();
put(&mut app, "d", 10, ThreadPut { compose: Some(Compose::default()), ..body("asks", State::NeedsYou) });
let mut inbox = app.inbox.content.clone();
inbox.archive_owner("d-id");
app.set_inbox(inbox);
inbox_tab(&mut app);
app.inbox.view = View::All;
app.on_key(key(KeyCode::Char('d')));
app.on_key(key(KeyCode::Enter));
flush(&mut app, 10);
for c in ['r', 'd', 'u'] {
app.on_key(key(KeyCode::Char(c)));
assert!(app.status.as_deref().unwrap().starts_with("archived"), "{c}: {:?}", app.status);
}
assert!(app.inbox.reply.is_none());
assert_eq!(app.take_pending_inbox(), []);
}
#[test]
fn url_check() {
assert!(is_url("https://github.com/o/r/pull/1"));
assert!(is_url("HTTP://example.com"));
assert!(!is_url("vscode://file/x"));
assert!(!is_url("file:///etc/passwd"));
assert!(!is_url("https://"));
assert!(!is_url("-x"));
assert!(!is_url("/etc/passwd"));
assert!(!is_url("1http://x"));
assert!(!is_url("http:"));
assert!(!is_url("-a:b"));
}
#[test]
fn view_change_keeps_the_thread_or_goes_to_the_top() {
let mut app = new_app();
mixed(&mut app);
inbox_tab(&mut app);
for _ in 0..3 {
app.on_key(key(KeyCode::Right));
}
let pos = rows(&app).iter().position(|r| r == "over").unwrap();
assert_ne!(pos, 0);
app.selected[Tab::Inbox.index()] = pos;
app.sync_inbox_selection();
app.inbox.set_list_offset(3);
app.on_key(key(KeyCode::Left));
assert_eq!(app.inbox.view, View::Done);
assert_eq!((app.selected(), selected(&app).as_deref()), (0, Some("over")), "kept by id");
assert_eq!(app.inbox.list_offset(), 3, "offset left to the renderer");
app.on_key(key(KeyCode::Left));
app.on_key(key(KeyCode::Left));
put(&mut app, "d", 15, body("busy2", State::Active));
put(&mut app, "d", 12, body("asks2", State::NeedsYou));
assert_eq!(rows(&app), ["asks2", "asks"]);
app.selected[Tab::Inbox.index()] = 1;
app.sync_inbox_selection();
assert_eq!(selected(&app).as_deref(), Some("asks"));
app.inbox.set_list_offset(2);
app.on_key(key(KeyCode::Right)); assert_eq!(app.inbox.view, View::Active);
assert_eq!(rows(&app), ["busy", "busy2"]);
assert_eq!(app.selected(), 0, "top of the new view, not row 1");
assert_eq!(selected(&app).as_deref(), Some("busy"));
assert_eq!(app.inbox.list_offset(), 0, "offset reset");
}
const ROW: u16 = 10;
#[test]
fn inbox_mouse_drag_resizes_divider() {
let mut app = new_app();
inbox_tab(&mut app);
assert_eq!(app.inbox.split_pct, 40);
app.on_mouse(&mouse_at(MouseEventKind::Down(MouseButton::Left), 39, ROW), FRAME);
assert!(app.inbox.dragging);
app.on_mouse(&mouse_at(MouseEventKind::Drag(MouseButton::Left), 30, ROW), FRAME);
assert_eq!(app.inbox.split_pct, 30);
app.on_mouse(&mouse_at(MouseEventKind::Drag(MouseButton::Left), 2, ROW), FRAME);
assert_eq!(app.inbox.split_pct, 20);
app.on_mouse(&mouse_at(MouseEventKind::Drag(MouseButton::Left), 99, ROW), FRAME);
assert_eq!(app.inbox.split_pct, 80);
app.on_mouse(&mouse_at(MouseEventKind::Up(MouseButton::Left), 99, ROW), FRAME);
assert!(!app.inbox.dragging);
app.on_mouse(&mouse_at(MouseEventKind::Drag(MouseButton::Left), 50, ROW), FRAME);
assert_eq!(app.inbox.split_pct, 80);
}
#[test]
fn inbox_mouse_press_off_the_divider_ignored() {
let mut app = new_app();
inbox_tab(&mut app);
for (col, row) in [(37, ROW), (40, 0), (40, 39)] {
app.on_mouse(&mouse_at(MouseEventKind::Down(MouseButton::Left), col, row), FRAME);
app.on_mouse(&mouse_at(MouseEventKind::Drag(MouseButton::Left), 60, row), FRAME);
assert_eq!(app.inbox.split_pct, 40, "press at ({col}, {row})");
}
}
#[test]
fn inbox_mouse_only_on_the_inbox_tab_unobstructed() {
let mut app = new_app();
app.on_mouse(&mouse_at(MouseEventKind::Down(MouseButton::Left), 40, ROW), FRAME);
app.on_mouse(&mouse_at(MouseEventKind::Drag(MouseButton::Left), 60, ROW), FRAME);
assert_eq!(app.inbox.split_pct, 40);
inbox_tab(&mut app);
app.on_key(key(KeyCode::Char(':')));
assert!(app.prompt.is_some());
app.on_mouse(&mouse_at(MouseEventKind::Down(MouseButton::Left), 40, ROW), FRAME);
app.on_mouse(&mouse_at(MouseEventKind::Drag(MouseButton::Left), 60, ROW), FRAME);
assert_eq!(app.inbox.split_pct, 40);
}
#[test]
fn inbox_mouse_with_terminals_stays_above_the_panel() {
let mut app = new_app();
inbox_tab(&mut app);
open_test_term(&mut app, "web-1", "devsandbox-web-1");
let panel = crate::tui::ui::terminal_panel_rect(FRAME, false);
app.focus = super::super::Focus::Dashboard;
app.on_mouse(&mouse_at(MouseEventKind::Down(MouseButton::Left), 40, panel.y + 1), FRAME);
app.on_mouse(&mouse_at(MouseEventKind::Drag(MouseButton::Left), 60, panel.y + 1), FRAME);
assert_eq!(app.inbox.split_pct, 40);
assert_eq!(app.focus, super::super::Focus::Terminal);
app.on_mouse(&mouse_at(MouseEventKind::Down(MouseButton::Left), 40, panel.y - 1), FRAME);
app.on_mouse(&mouse_at(MouseEventKind::Drag(MouseButton::Left), 60, panel.y - 1), FRAME);
assert_eq!(app.inbox.split_pct, 60);
}
fn click(app: &mut App, col: u16, row: u16) {
app.on_mouse(&mouse_at(MouseEventKind::Down(MouseButton::Left), col, row), FRAME);
app.on_mouse(&mouse_at(MouseEventKind::Up(MouseButton::Left), col, row), FRAME);
}
#[test]
fn click_card_selects_reads_and_focuses_the_list() {
let mut app = new_app();
put(&mut app, "d", 10, body("asks", State::NeedsYou));
put(&mut app, "d", 20, body("other", State::NeedsYou));
inbox_tab(&mut app);
let asks = thread(&app, "asks").id;
app.take_pending_inbox();
app.on_key(key(KeyCode::Enter));
assert_eq!(app.inbox.focus, InboxFocus::Thread);
click(&mut app, 10, 6);
assert_eq!(selected(&app).as_deref(), Some("asks"));
assert_eq!(app.inbox.focus, InboxFocus::List);
assert_eq!(app.take_pending_inbox(), [Op::MarkRead(asks)]);
click(&mut app, 10, 5);
click(&mut app, 10, 9);
assert_eq!(selected(&app).as_deref(), Some("asks"));
app.inbox.set_list_offset(1);
click(&mut app, 10, 3);
assert_eq!(selected(&app).as_deref(), Some("asks"));
app.inbox.set_list_offset(0);
click(&mut app, 10, 3);
assert_eq!(selected(&app).as_deref(), Some("other"));
app.on_key(key(KeyCode::Enter));
app.on_mouse(&mouse_at(MouseEventKind::Down(MouseButton::Left), 40, 6), FRAME);
assert!(app.inbox.dragging);
assert_eq!(app.inbox.focus, InboxFocus::Thread);
}
#[test]
fn click_pane_and_input_focus_them() {
let mut app = new_app();
put(&mut app, "d", 10, ThreadPut { compose: Some(Compose::default()), ..body("asks", State::NeedsYou) });
inbox_tab(&mut app);
click(&mut app, 60, 10);
assert_eq!(app.inbox.focus, InboxFocus::Thread);
click(&mut app, 60, 36);
assert_eq!(app.inbox.focus, InboxFocus::Input);
typed(&mut app, "half");
click(&mut app, 60, 35);
assert_eq!(app.inbox.reply.as_ref().unwrap().line.input(), "half");
click(&mut app, 60, 10);
assert_eq!(app.inbox.focus, InboxFocus::Thread);
assert!(app.inbox.reply.is_none());
click(&mut app, 10, 3);
assert_eq!(app.inbox.focus, InboxFocus::List);
}
#[test]
fn click_where_a_no_reply_thread_has_its_hint_focuses_the_thread() {
let mut app = new_app();
put(&mut app, "d", 10, body("asks", State::NeedsYou));
inbox_tab(&mut app);
for row in [36, 37] {
app.inbox.focus = InboxFocus::List;
click(&mut app, 60, row);
assert_eq!(app.inbox.focus, InboxFocus::Thread, "row {row}");
assert!(app.inbox.reply.is_none());
}
}
#[test]
fn click_strip_switches_views() {
let mut app = new_app();
put(&mut app, "d", 10, body("asks", State::NeedsYou));
put(&mut app, "d", 20, body("busy", State::Active));
inbox_tab(&mut app);
click(&mut app, 16, 1);
assert_eq!(app.inbox.view, View::Active);
assert_eq!(selected(&app).as_deref(), Some("busy"));
click(&mut app, 35, 1); assert_eq!(app.inbox.view, View::Done);
click(&mut app, 2, 1); assert_eq!(app.inbox.view, View::Active);
click(&mut app, 32, 1);
assert_eq!(app.inbox.view, View::All);
click(&mut app, 14, 1); assert_eq!(app.inbox.view, View::All);
}
#[test]
fn inbox_wheel_moves_the_selection_and_scrolls_the_pane() {
let mut app = new_app();
put(&mut app, "d", 10, body("asks", State::NeedsYou));
put(&mut app, "d", 20, body("other", State::NeedsYou));
inbox_tab(&mut app);
app.on_mouse(&mouse_at(MouseEventKind::ScrollDown, 10, 10), FRAME);
assert_eq!(selected(&app).as_deref(), Some("asks"));
app.on_mouse(&mouse_at(MouseEventKind::ScrollUp, 10, 10), FRAME);
assert_eq!(selected(&app).as_deref(), Some("other"));
app.inbox.set_pane_max(4);
app.on_mouse(&mouse_at(MouseEventKind::ScrollDown, 60, 10), FRAME);
assert_eq!(app.inbox.scroll, 3);
app.on_mouse(&mouse_at(MouseEventKind::ScrollDown, 60, 10), FRAME);
assert_eq!(app.inbox.scroll, 4, "clamped to the drawn bound");
app.on_mouse(&mouse_at(MouseEventKind::ScrollUp, 60, 10), FRAME);
assert_eq!(app.inbox.scroll, 1);
assert_eq!(selected(&app).as_deref(), Some("other"));
}
#[test]
fn inbox_clicks_ignored_under_a_modal_or_the_prompt() {
let mut app = new_app();
put(&mut app, "d", 10, body("asks", State::NeedsYou));
put(&mut app, "d", 20, body("other", State::NeedsYou));
inbox_tab(&mut app);
app.on_key(key(KeyCode::Char(':')));
click(&mut app, 10, 6);
click(&mut app, 60, 10);
assert_eq!((selected(&app).as_deref(), app.inbox.focus), (Some("other"), InboxFocus::List));
app.on_key(key(KeyCode::Esc));
assert!(app.prompt.is_none());
app.on_key(key(KeyCode::Char('?')));
assert!(!matches!(app.modal, Modal::None));
click(&mut app, 10, 6);
click(&mut app, 60, 10);
assert_eq!((selected(&app).as_deref(), app.inbox.focus), (Some("other"), InboxFocus::List));
}
#[test]
fn click_thread_while_a_terminal_has_focus_moves_focus_there() {
let mut app = new_app();
put(&mut app, "d", 10, body("asks", State::NeedsYou));
inbox_tab(&mut app);
open_test_term(&mut app, "web-1", "devsandbox-web-1");
assert_eq!(app.focus, super::super::Focus::Terminal);
click(&mut app, 60, 5);
assert_eq!(app.focus, super::super::Focus::Dashboard);
assert_eq!(app.inbox.focus, InboxFocus::Thread);
}
#[test]
fn click_tab_leaving_the_inbox_reads_its_notify_records() {
let mut app = new_app();
push(&mut app, "a", rec("note", None, None));
push(&mut app, "b", rec("other note", None, None));
inbox_tab(&mut app); app.take_pending_inbox();
let tabs = crate::tui::ui::tab_spans(&app);
let services = tabs.iter().find(|(t, _, _)| *t == Tab::Services).unwrap().2.start;
click(&mut app, services, 0);
assert_eq!(app.tab, Tab::Services);
assert_eq!(app.take_pending_inbox(), [Op::MarkNotifyRead]);
}
}