use crate::commands::vscode::Goto;
use crate::snapshot::{InstanceRow, PhaseRow};
use crate::tui::data::ContainerStatus;
use crate::tui::prompt::PromptAction;
use crate::tui::remote::{self, instance_key};
use super::view::{profile_lines, Confirmed, ConfirmModal};
use super::{App, Modal, PortRequest};
fn busy(row: &InstanceRow) -> Option<String> {
match &row.phase {
Some(PhaseRow::InFlight { step }) => {
Some(format!("`{}` is still {}", row.name, crate::commands::doing(step)))
}
_ => None,
}
}
pub fn not_yet_code(machine: &str, name: &str) -> String {
format!("code {machine}/{name}: VS Code on a machine isn't supported yet")
}
#[derive(Debug, PartialEq, Eq)]
enum ProfileSwitch {
To(String),
Pick,
Nothing,
}
fn profile_switch(current: Option<&str>, allowed: &[String]) -> ProfileSwitch {
if allowed.len() > 2 {
return ProfileSwitch::Pick;
}
match allowed.iter().find(|p| Some(p.as_str()) != current) {
Some(p) => ProfileSwitch::To(p.clone()),
None => ProfileSwitch::Nothing,
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum VerbEnd {
Done { status: String, failed: bool },
Confirm(String),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PendingDone {
pub instance: String,
pub done: bool,
pub report: bool,
}
impl App {
pub(super) fn remove_instance(&mut self) {
let Some(i) = self.selected_instance_index() else {
return;
};
let Some(row) = self.snapshot.as_ref().and_then(|s| s.instances.get(i)) else {
return;
};
let key = instance_key(row);
if busy(row).is_some() {
self.confirm_cancel_build(key);
return;
}
self.pending_action = Some(PromptAction::Rm { instance: key, force: false, delete_branch: None });
}
pub(super) fn switch_profile(&mut self) {
let Some(i) = self.selected_instance_index() else {
return;
};
let Some(s) = &self.snapshot else {
return;
};
let Some(row) = s.instances.get(i) else {
return;
};
let key = instance_key(row);
let Some(sandbox) = remote::instance_sandbox(s, row) else {
self.status = Some(format!("{key}: sandbox `{}` isn't listed, so its profiles aren't known", row.sandbox));
return;
};
let (profiles, current) = (sandbox.profiles.clone(), row.profile.clone());
match profile_switch(current.as_deref(), &profiles) {
ProfileSwitch::To(profile) => self.pending_action = Some(PromptAction::Profile { instance: key, profile }),
ProfileSwitch::Pick => {
let cursor = current.as_ref().and_then(|c| profiles.iter().position(|p| p == c)).unwrap_or(0);
let body = profile_lines(&profiles, current.as_deref(), cursor);
let question = format!("{key}: switch to profile");
let action = Confirmed::Profile { instance: key, profiles, current, cursor };
self.modal = Modal::Confirm(ConfirmModal::new(question, body, action));
}
ProfileSwitch::Nothing => self.status = Some(format!("{key}: no other profile to switch to")),
}
}
pub(super) fn request_child_done(&mut self, instance: String, done: bool) {
self.pending_done.push(PendingDone { instance, done, report: false });
}
pub fn take_pending_done(&mut self) -> Vec<PendingDone> {
std::mem::take(&mut self.pending_done)
}
pub(super) fn stop_selected_forward(&mut self) {
let Some(row) = self.ports.get(self.selected()) else {
return;
};
self.status = Some(format!("stopping {}", row.local));
self.pending_unport = Some(row.id);
}
pub(super) fn toggle_selected_agent(&mut self) {
let Some(m) = self.machines.get(self.selected()) else {
return;
};
let agent = !m.agent;
self.status = Some(format!("{}: agent {}…", m.name, if agent { "on" } else { "off" }));
self.pending_machine_set = Some((m.name.clone(), agent));
}
pub fn take_pending_machine_set(&mut self) -> Option<(String, bool)> {
self.pending_machine_set.take()
}
pub fn take_pending_port(&mut self) -> Option<PortRequest> {
self.pending_port.take()
}
pub fn take_pending_unport(&mut self) -> Option<u64> {
self.pending_unport.take()
}
pub(super) fn attach_code(&mut self) {
let Some(i) = self.selected_instance_index() else {
return;
};
let Some(snapshot) = &self.snapshot else {
return;
};
let Some(row) = snapshot.instances.get(i) else {
return;
};
if let Some(m) = &row.machine {
self.status = Some(not_yet_code(m, &row.name));
return;
}
self.pending_action = Some(PromptAction::Code { instance: row.name.clone(), goto: None, allow_credential_leak: false });
}
pub fn code_refused(&mut self, instance: String, goto: Option<Goto>, note: Option<String>, exposed: bool) {
let question = format!(
"`{instance}` has credential claims: VS Code's attach gives it your host's git logins and ssh-agent. Open anyway?"
);
let body = if exposed {
vec!["another VS Code window is running with your logins: close VS Code first to open it safely".into()]
} else {
Vec::new()
};
self.modal = Modal::Confirm(ConfirmModal::new(question, body, Confirmed::CodeLeak { instance, goto, note }));
}
pub(super) fn stop_or_start_instance(&mut self) {
let Some(i) = self.selected_instance_index() else {
return;
};
let Some(snapshot) = &self.snapshot else {
return;
};
let Some(row) = snapshot.instances.get(i) else {
return;
};
let name = instance_key(row);
if self.stopping.contains(&name) || self.starting.contains(&name) {
return;
}
if let Some(why) = busy(row) {
self.status = Some(why);
return;
}
if let Some(PhaseRow::Failed { .. }) = row.phase {
self.pending_action = Some(PromptAction::Rebuild { instance: name, force: false });
return;
}
match row.status {
ContainerStatus::Running(_) => {
self.status = Some(format!("stopping {name}…"));
self.stopping.insert(name.clone());
self.pending_action = Some(PromptAction::Stop { instance: name });
}
ContainerStatus::Exited(_) if row.drift => {
self.pending_action =
Some(PromptAction::Rebuild { instance: name, force: false });
}
ContainerStatus::Exited(_) => {
self.status = Some(format!("starting {name}…"));
self.starting.insert(name.clone());
self.pending_action = Some(PromptAction::Start { instance: name });
}
ContainerStatus::Unknown => {
self.status = Some(format!("{name}: status still loading"));
}
ContainerStatus::Missing => {
self.pending_action =
Some(PromptAction::Rebuild { instance: name, force: false });
}
}
}
pub fn run_rename_hint(&self) -> &'static str {
if self.selected_instance_index().is_some() {
"rename"
} else {
"run"
}
}
pub fn stop_start_hint(&self) -> &'static str {
let row = self.selected_instance_index().and_then(|i| self.snapshot.as_ref()?.instances.get(i));
match row {
Some(InstanceRow { phase: Some(PhaseRow::InFlight { .. }), .. }) => "wait",
Some(InstanceRow { phase: Some(PhaseRow::Failed { .. }), .. }) => "rebuild",
Some(row) if matches!(row.status, ContainerStatus::Exited(_) | ContainerStatus::Missing) => "start",
_ => "stop",
}
}
pub fn remove_hint(&self) -> &'static str {
let row = self.selected_instance_index().and_then(|i| self.snapshot.as_ref()?.instances.get(i));
match row {
Some(row) if busy(row).is_some() => "cancel",
_ => "rm",
}
}
pub fn verb_answered(&mut self, action: &PromptAction, end: VerbEnd) {
match (action, end) {
(PromptAction::Rm { instance, force, .. }, VerbEnd::Confirm(question)) => {
self.status = None;
self.confirm_rm_branch(instance.clone(), *force, question);
}
(_, VerbEnd::Confirm(question)) => self.status = Some(question),
(action, VerbEnd::Done { status, failed }) => {
self.status = Some(status);
match action {
PromptAction::Stop { instance } => {
self.stopping.remove(instance);
}
PromptAction::Start { instance } => self.start_answered(instance, failed),
_ => {}
}
}
}
}
}
#[cfg(test)]
mod tests {
use crate::tui::app::test_support::*;
use crate::tui::app::*;
use super::{profile_switch, ProfileSwitch};
#[test]
fn d_on_ports_sets_pending_unport() {
let mut app = new_app();
app.tab = Tab::Ports;
app.set_ports(port_rows(3));
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('d')));
assert_eq!(app.take_pending_unport(), Some(1));
assert_eq!(app.status.as_deref(), Some("stopping 127.0.0.1:3001"));
}
#[test]
fn a_on_machines_queues_the_flipped_agent() {
use crate::serve::snapshots::LinkState;
use crate::tui::remote::tests::sm;
let mut app = new_app();
let mut on = sm("b", LinkState::Unreachable);
on.agent = true;
app.set_machines(vec![sm("a", LinkState::Connected), on]);
app.on_key(key(KeyCode::Char('a')));
assert_eq!(app.take_pending_machine_set(), None);
app.on_key(key(KeyCode::Char('5')));
app.on_key(key(KeyCode::Char('a')));
assert_eq!(app.take_pending_machine_set(), Some(("a".into(), true)));
assert_eq!(app.status.as_deref(), Some("a: agent on…"));
assert!(!app.machines[0].agent);
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('a')));
assert_eq!(app.take_pending_machine_set(), Some(("b".into(), false)));
assert_eq!(app.status.as_deref(), Some("b: agent off…"));
let lap = crate::serve::snapshots::SnapshotMachine {
kind: crate::serve::machines::MachineKind::Inbound,
target: None,
..sm("lap", LinkState::Connected)
};
let x = crate::serve::snapshots::SnapshotMachine {
kind: crate::serve::machines::MachineKind::Routed,
via: Some("k".into()),
target: None,
agent: true,
..sm("x", LinkState::Connected)
};
app.set_machines(vec![lap, x]);
app.on_key(key(KeyCode::Up));
app.on_key(key(KeyCode::Char('a')));
assert_eq!(app.take_pending_machine_set(), Some(("lap".into(), true)));
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('a')));
assert_eq!(app.take_pending_machine_set(), Some(("x".into(), false)));
assert_eq!(app.status.as_deref(), Some("x: agent off…"));
}
#[test]
fn a_with_no_machine_row_queues_nothing() {
let mut app = new_app();
app.tab = Tab::Machines;
app.on_key(key(KeyCode::Char('a')));
assert_eq!(app.take_pending_machine_set(), None);
assert_eq!(app.status, None);
}
#[test]
fn d_on_an_instance_queues_rm_without_a_prompt() {
let mut app = new_app();
app.set_snapshot(snapshot_with(1));
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('d')));
assert_eq!(
app.take_pending_action(),
Some(PromptAction::Rm { instance: "inst0".into(), force: false, delete_branch: None })
);
assert!(app.prompt.is_none());
assert_eq!(app.take_pending_done(), []);
}
#[test]
fn modified_d_on_an_instance_queues_nothing() {
let mut app = new_app();
app.set_snapshot(snapshot_with(1));
app.on_key(key(KeyCode::Down));
app.take_pending_identity();
let ctrl_shift = KeyModifiers::CONTROL | KeyModifiers::SHIFT;
for (c, mods) in [('d', KeyModifiers::CONTROL), ('d', KeyModifiers::ALT), ('D', ctrl_shift), ('d', KeyModifiers::SUPER)] {
app.on_key(key_mods(KeyCode::Char(c), mods));
assert_eq!(app.take_pending_action(), None, "{mods:?}-{c}");
assert!(matches!(app.modal, Modal::None));
}
for c in ['s', 'r', 'o', 'P', 'U'] {
app.on_key(key_mods(KeyCode::Char(c), KeyModifiers::CONTROL));
}
assert_eq!(app.take_pending_action(), None);
assert!(app.prompt.is_none());
assert!(app.take_pending_identity().is_none());
assert!(!app.should_quit);
app.on_key(key(KeyCode::Char('d')));
assert!(matches!(app.take_pending_action(), Some(PromptAction::Rm { .. })));
}
#[test]
fn chords_fire_only_the_ctrl_bindings() {
let mut app = new_app();
app.tab = Tab::Ports;
app.set_ports(port_rows(2));
app.on_key(key_mods(KeyCode::Char('d'), KeyModifiers::CONTROL));
app.on_key(key_mods(KeyCode::Char('q'), KeyModifiers::ALT));
app.on_key(key_mods(KeyCode::Char('2'), KeyModifiers::ALT));
assert_eq!(app.take_pending_unport(), None);
assert_eq!(app.tab, Tab::Ports);
assert!(!app.should_quit);
app.on_key(key_mods(KeyCode::Char('?'), KeyModifiers::SHIFT));
assert!(matches!(app.modal, Modal::Help(_)));
app.on_key(key(KeyCode::Esc));
app.on_key(key_mods(KeyCode::Char(']'), KeyModifiers::CONTROL));
assert_eq!(app.status.as_deref(), Some("no terminal open — press t"));
app.on_key(key_mods(KeyCode::Char('c'), KeyModifiers::CONTROL));
assert!(app.should_quit);
}
#[test]
fn modified_y_does_not_answer_a_confirm() {
let mut app = new_app();
app.set_snapshot(snapshot_with_phase(1, ContainerStatus::Missing, in_flight("building")));
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('d')));
app.on_key(key_mods(KeyCode::Char('y'), KeyModifiers::ALT));
app.on_key(key_mods(KeyCode::Char('c'), KeyModifiers::CONTROL));
assert!(matches!(app.modal, Modal::Confirm(_)));
assert!(!app.should_quit);
app.on_key(key(KeyCode::Char('y')));
assert_eq!(app.take_pending_cancel(), Some("inst0".into()));
}
#[test]
fn d_and_u_off_an_instance_queue_nothing() {
let mut app = app_on_proc_row(&["10"]);
app.on_key(key(KeyCode::Char('d')));
app.on_key(key(KeyCode::Char('u')));
assert_eq!(app.take_pending_action(), None);
let mut app = new_app();
app.set_snapshot(snapshot_with(1));
app.on_key(key(KeyCode::Char('d')));
assert_eq!(app.take_pending_action(), None);
app.on_key(key(KeyCode::Down));
app.tab = Tab::Services;
app.on_key(key(KeyCode::Char('d')));
assert_eq!(app.take_pending_action(), None);
}
#[test]
fn u_on_an_instance_does_nothing() {
let mut app = new_app();
let mut snap = snapshot_with(1);
snap.instances[0].done = true;
app.set_snapshot(snap);
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('u')));
assert_eq!(app.take_pending_done(), []);
assert_eq!(app.take_pending_action(), None);
}
#[test]
fn d_elsewhere_does_nothing_new() {
let mut app = new_app();
app.set_snapshot(snapshot_with(1));
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('d')));
assert_eq!(app.take_pending_unport(), None);
}
#[test]
fn o_on_instance_sets_pending_code() {
let mut app = new_app();
app.set_snapshot(snapshot_with(1)); app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('o')));
assert_eq!(
app.take_pending_action(),
Some(PromptAction::Code { instance: "inst0".into(), goto: None, allow_credential_leak: false }),
);
}
#[test]
fn o_on_orphan_instance_sets_pending_code() {
let mut app = new_app();
app.set_snapshot(orphan_snapshot()); app.on_key(key(KeyCode::Down));
assert_eq!(app.selected_node(), Some(Node::Instance(0)));
app.on_key(key(KeyCode::Char('o')));
assert_eq!(
app.take_pending_action(),
Some(PromptAction::Code { instance: "orphan0".into(), goto: None, allow_credential_leak: false }),
);
}
#[test]
fn o_on_sandbox_is_noop() {
let mut app = new_app();
app.set_snapshot(snapshot_with(1)); app.on_key(key(KeyCode::Char('o')));
assert_eq!(app.take_pending_action(), None);
}
#[test]
fn a_leak_refused_code_asks_and_y_resends_it_allowed() {
let mut app = new_app();
app.set_snapshot(snapshot_with(1));
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('o')));
assert_eq!(
app.take_pending_action(),
Some(PromptAction::Code { instance: "inst0".into(), goto: None, allow_credential_leak: false }),
);
let goto = super::Goto::parse("a.rs:3").unwrap();
app.code_refused("inst0".into(), Some(goto.clone()), Some("note".into()), true);
let Modal::Confirm(view) = &app.modal else { panic!("no modal") };
assert!(view.question.starts_with("`inst0` has credential claims"), "{}", view.question);
assert_eq!(view.body.len(), 1);
app.on_key(key(KeyCode::Char('y')));
assert!(matches!(app.modal, Modal::None));
assert_eq!(
app.take_pending_action(),
Some(PromptAction::Code { instance: "inst0".into(), goto: Some(goto), allow_credential_leak: true }),
);
assert_eq!(app.code_note.take().as_deref(), Some("note"));
}
#[test]
fn a_leak_refused_code_declined_queues_nothing() {
for answer in [KeyCode::Char('n'), KeyCode::Esc] {
let mut app = new_app();
app.code_refused("inst0".into(), None, Some("note".into()), false);
let Modal::Confirm(view) = &app.modal else { panic!("no modal") };
assert!(view.body.is_empty());
app.on_key(key(answer));
assert!(matches!(app.modal, Modal::None), "{answer:?}");
assert_eq!(app.take_pending_action(), None, "{answer:?}");
assert_eq!(app.code_note, None, "{answer:?}");
}
}
#[test]
fn s_on_running_instance_sets_pending_stop() {
let mut app = new_app();
app.set_snapshot(snapshot_with_status(1, running())); app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('s')));
assert!(app.stopping.contains("inst0"));
assert_eq!(app.status.as_deref(), Some("stopping inst0…"));
assert_eq!(app.take_pending_action(), Some(PromptAction::Stop { instance: "inst0".into() }));
assert_eq!(app.take_pending_action(), None);
}
#[test]
fn actions_on_a_machines_row_queue_its_key() {
let mut app = machine_app();
app.selected[Tab::Instances.index()] = 5;
app.on_key(key(KeyCode::Char('s')));
assert_eq!(app.status.as_deref(), Some("stopping box/inst1…"));
assert!(app.stopping.contains("box/inst1") && !app.stopping.contains("inst1"));
assert_eq!(app.take_pending_action(), Some(PromptAction::Stop { instance: "box/inst1".into() }));
app.selected[Tab::Instances.index()] = 2;
app.on_key(key(KeyCode::Char('s')));
assert_eq!(app.take_pending_action(), Some(PromptAction::Stop { instance: "inst1".into() }));
app.selected[Tab::Instances.index()] = 4;
app.on_key(key(KeyCode::Char('d')));
assert_eq!(
app.take_pending_action(),
Some(PromptAction::Rm { instance: "box/inst0".into(), force: false, delete_branch: None })
);
app.on_key(key(KeyCode::Char('o')));
assert_eq!(app.take_pending_action(), None);
assert_eq!(app.status.as_deref(), Some("code box/inst0: VS Code on a machine isn't supported yet"));
app.on_key(key(KeyCode::Char('p')));
assert_eq!(prompt(&app).input(), "port box/inst0 ");
app.prompt = None;
app.on_key(key(KeyCode::Char('l')));
assert_eq!(app.take_pending_fetch(), [Fetch::Logs { container: "box/devsandbox-inst0".into() }]);
app.modal = Modal::None;
app.on_key(key(KeyCode::Char('e')));
assert_eq!(
app.take_pending_fetch(),
[
Fetch::Config { kind: ConfigKind::Sandbox, name: "box/s".into() },
Fetch::Inspect { container: "box/devsandbox-inst0".into() },
]
);
}
#[test]
fn s_on_exited_instance_sets_pending_start() {
let mut app = new_app();
let status = ContainerStatus::Exited("Exited (0)".into());
app.set_snapshot(snapshot_with_status(1, status));
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('s')));
assert!(app.starting.contains("inst0"));
assert_eq!(app.status.as_deref(), Some("starting inst0…"));
assert_eq!(app.take_pending_action(), Some(PromptAction::Start { instance: "inst0".into() }));
assert_eq!(app.take_pending_action(), None);
}
#[test]
fn s_on_unknown_instance_does_nothing() {
let mut app = new_app();
app.set_snapshot(snapshot_with_status(1, ContainerStatus::Unknown));
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('s')));
assert_eq!(app.status.as_deref(), Some("inst0: status still loading"));
assert_eq!(app.take_pending_action(), None);
}
#[test]
fn s_on_exited_drifted_instance_queues_rebuild() {
let mut app = new_app();
let status = ContainerStatus::Exited("Exited (0)".into());
let mut snap = snapshot_with_status(1, status);
snap.instances[0].drift = true;
app.set_snapshot(snap);
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('s')));
assert_eq!(
app.take_pending_action(),
Some(PromptAction::Rebuild { instance: "inst0".into(), force: false })
);
assert_eq!(app.take_pending_action(), None);
assert!(app.starting.is_empty());
assert_eq!(app.take_pending_action(), None);
}
#[test]
fn s_on_exited_undrifted_instance_still_starts() {
let mut app = new_app();
let status = ContainerStatus::Exited("Exited (0)".into());
let snap = snapshot_with_status(1, status);
app.set_snapshot(snap);
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('s')));
assert!(app.starting.contains("inst0"));
assert_eq!(app.take_pending_action(), Some(PromptAction::Start { instance: "inst0".into() }));
assert_eq!(app.take_pending_action(), None);
}
#[test]
fn s_on_running_drifted_instance_still_stops() {
let mut app = new_app();
let mut snap = snapshot_with_status(1, running());
snap.instances[0].drift = true;
app.set_snapshot(snap);
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('s')));
assert!(app.stopping.contains("inst0"));
assert_eq!(app.take_pending_action(), Some(PromptAction::Stop { instance: "inst0".into() }));
assert_eq!(app.take_pending_action(), None);
}
#[test]
fn s_on_missing_container_queues_rebuild() {
let mut app = new_app();
app.set_snapshot(snapshot_with(1)); app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('s')));
assert_eq!(
app.take_pending_action(),
Some(PromptAction::Rebuild { instance: "inst0".into(), force: false })
);
assert_eq!(app.take_pending_action(), None);
assert!(app.stopping.is_empty());
assert!(app.starting.is_empty());
}
#[test]
fn s_on_orphan_instance_sets_pending_stop() {
let mut app = new_app();
let mut snap = orphan_snapshot(); snap.instances[0].status = running();
app.set_snapshot(snap);
app.on_key(key(KeyCode::Down));
assert_eq!(app.selected_node(), Some(Node::Instance(0)));
app.on_key(key(KeyCode::Char('s')));
assert_eq!(app.take_pending_action(), Some(PromptAction::Stop { instance: "orphan0".into() }));
}
#[test]
fn s_on_sandbox_is_noop() {
let mut app = new_app();
app.set_snapshot(snapshot_with(1)); app.on_key(key(KeyCode::Char('s')));
assert_eq!(app.take_pending_action(), None);
assert!(app.stopping.is_empty());
}
#[test]
fn s_dedupes_while_stop_in_flight() {
let mut app = new_app();
app.set_snapshot(snapshot_with_status(1, running()));
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('s')));
assert_eq!(app.take_pending_action(), Some(PromptAction::Stop { instance: "inst0".into() }));
app.on_key(key(KeyCode::Down)); app.on_key(key(KeyCode::Char('s')));
assert_eq!(app.take_pending_action(), None);
}
#[test]
fn s_dedupes_while_start_in_flight() {
let mut app = new_app();
let status = ContainerStatus::Exited("Exited (0)".into());
app.set_snapshot(snapshot_with_status(1, status));
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('s')));
assert_eq!(app.take_pending_action(), Some(PromptAction::Start { instance: "inst0".into() }));
app.on_key(key(KeyCode::Down)); app.on_key(key(KeyCode::Char('s')));
assert_eq!(app.take_pending_action(), None);
}
#[test]
fn s_guards_clear_when_the_answer_arrives() {
let mut app = new_app();
app.set_snapshot(snapshot_with_status(1, running()));
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('s')));
let stop = app.take_pending_action().unwrap();
app.verb_answered(&stop, VerbEnd::Done { status: "stopped inst0".into(), failed: false });
assert!(app.stopping.is_empty());
assert_eq!(app.status.as_deref(), Some("stopped inst0"));
app.set_snapshot(snapshot_with_status(1, ContainerStatus::Exited("Exited (0)".into())));
app.on_key(key(KeyCode::Char('s')));
let start = app.take_pending_action().unwrap();
assert_eq!(start, PromptAction::Start { instance: "inst0".into() });
app.verb_answered(&start, VerbEnd::Done { status: "start inst0 failed: boom".into(), failed: true });
assert!(app.starting.is_empty());
app.on_key(key(KeyCode::Char('s')));
assert_eq!(app.take_pending_action(), Some(start));
}
#[test]
fn rms_question_is_a_three_way_confirm() {
let q = "delete branch `b`? it has commits not on its remote";
let ask = |app: &mut App, force| {
let rm = PromptAction::Rm { instance: "inst0".into(), force, delete_branch: None };
app.verb_answered(&rm, VerbEnd::Confirm(q.into()));
match &app.modal {
Modal::Confirm(c) => {
assert_eq!(c.question, q);
assert_eq!(c.keys_hint(), "y/enter yes · n no · esc cancel");
}
_ => panic!("no confirm"),
}
};
let resent = |force, delete| Some(PromptAction::Rm { instance: "inst0".into(), force, delete_branch: Some(delete) });
let mut app = new_app();
for (code, force, want) in [
(KeyCode::Char('y'), false, resent(false, true)),
(KeyCode::Enter, true, resent(true, true)),
(KeyCode::Char('n'), true, resent(true, false)),
(KeyCode::Esc, false, None),
(KeyCode::Char('q'), false, None),
] {
ask(&mut app, force);
app.on_key(key(KeyCode::Char('s')));
assert!(matches!(app.modal, Modal::Confirm(_)));
app.on_key(key(code));
assert!(matches!(app.modal, Modal::None), "{code:?}");
assert_eq!(app.take_pending_action(), want, "{code:?}");
assert_eq!(app.take_pending_cancel(), None);
}
assert_eq!(app.status.as_deref(), Some("rm inst0: cancelled"));
}
#[test]
fn s_ignored_on_services_tab() {
let mut app = new_app();
app.set_snapshot(snapshot_with(1));
app.tab = Tab::Services;
app.on_key(key(KeyCode::Char('s')));
assert_eq!(app.take_pending_action(), None);
}
#[test]
fn stop_start_hint_tracks_selection_status() {
let mut app = new_app();
assert_eq!(app.stop_start_hint(), "stop");
app.set_snapshot(snapshot_with_status(
1,
ContainerStatus::Exited("Exited (0)".into()),
));
app.on_key(key(KeyCode::Down));
assert_eq!(app.stop_start_hint(), "start");
app.set_snapshot(snapshot_with_status(1, running()));
assert_eq!(app.stop_start_hint(), "stop");
app.set_snapshot(snapshot_with_status(1, ContainerStatus::Missing));
assert_eq!(app.stop_start_hint(), "start");
}
#[test]
fn s_and_d_on_an_in_flight_instance_queue_nothing() {
for status in [ContainerStatus::Missing, running(), ContainerStatus::Exited("Exited (1)".into())] {
let mut app = new_app();
app.set_snapshot(snapshot_with_phase(1, status, in_flight("postCreateCommand")));
app.on_key(key(KeyCode::Down));
assert_eq!(app.stop_start_hint(), "wait");
app.on_key(key(KeyCode::Char('s')));
assert_eq!(app.status.as_deref(), Some("`inst0` is still running postCreateCommand"));
app.on_key(key(KeyCode::Char('d')));
assert!(matches!(app.modal, Modal::Confirm(_)));
app.on_key(key(KeyCode::Esc));
assert_eq!(app.take_pending_action(), None);
assert_eq!(app.take_pending_cancel(), None);
}
}
#[test]
fn d_on_an_in_flight_instance_confirms_a_cancel() {
let mut app = new_app();
app.set_snapshot(snapshot_with_phase(1, ContainerStatus::Missing, in_flight("building")));
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('d')));
match &app.modal {
Modal::Confirm(c) => assert_eq!(c.question, "cancel inst0's build?"),
_ => panic!("no confirm"),
}
app.on_key(key(KeyCode::Char('s')));
assert!(matches!(app.modal, Modal::Confirm(_)));
app.on_key(key(KeyCode::Char('n')));
assert!(matches!(app.modal, Modal::None));
assert_eq!(app.take_pending_cancel(), None);
app.on_key(key(KeyCode::Char('d')));
app.on_key(key(KeyCode::Char('y')));
assert!(matches!(app.modal, Modal::None));
assert_eq!(app.take_pending_cancel(), Some("inst0".into()));
assert_eq!(app.status.as_deref(), Some("cancelling inst0…"));
assert_eq!(app.take_pending_action(), None);
app.on_key(key(KeyCode::Char('d')));
app.on_key(key(KeyCode::Enter));
assert_eq!(app.take_pending_cancel(), Some("inst0".into()));
}
#[test]
fn s_rebuilds_and_d_removes_a_failed_instance() {
for status in [ContainerStatus::Missing, running(), ContainerStatus::Exited("Exited (1)".into())] {
let mut app = new_app();
app.set_snapshot(snapshot_with_phase(1, status, failed("postCreateCommand")));
app.on_key(key(KeyCode::Down));
assert_eq!(app.stop_start_hint(), "rebuild");
app.on_key(key(KeyCode::Char('s')));
assert_eq!(
app.take_pending_action(),
Some(PromptAction::Rebuild { instance: "inst0".into(), force: false })
);
assert_eq!(app.take_pending_action(), None);
app.on_key(key(KeyCode::Char('d')));
assert_eq!(app.take_pending_action(), Some(PromptAction::Rm { instance: "inst0".into(), force: false, delete_branch: None }));
}
}
#[test]
fn run_rename_hint_tracks_selection() {
let mut app = new_app();
assert_eq!(app.run_rename_hint(), "run");
app.set_snapshot(snapshot_with(2)); assert_eq!(app.run_rename_hint(), "run");
app.on_key(key(KeyCode::Down));
assert_eq!(app.run_rename_hint(), "rename");
}
fn strings(names: &[&str]) -> Vec<String> {
names.iter().map(|s| s.to_string()).collect()
}
#[test]
fn profile_switch_picks_the_other_or_asks() {
let two = strings(&["personal", "work"]);
assert_eq!(profile_switch(Some("work"), &two), ProfileSwitch::To("personal".into()));
assert_eq!(profile_switch(Some("personal"), &two), ProfileSwitch::To("work".into()));
assert_eq!(profile_switch(None, &two), ProfileSwitch::To("personal".into()), "none: the first");
assert_eq!(profile_switch(Some("gone"), &two), ProfileSwitch::To("personal".into()));
assert_eq!(profile_switch(Some("work"), &strings(&["work"])), ProfileSwitch::Nothing);
assert_eq!(profile_switch(Some("gone"), &strings(&["work"])), ProfileSwitch::To("work".into()), "out of a disallowed one");
assert_eq!(profile_switch(None, &[]), ProfileSwitch::Nothing);
assert_eq!(profile_switch(Some("a"), &strings(&["a", "b", "c"])), ProfileSwitch::Pick);
}
fn profile_app(current: Option<&str>, allowed: &[&str]) -> App {
let mut s = snapshot_with(1);
s.instances[0].profile = current.map(String::from);
s.sandboxes[0].profiles = strings(allowed);
let mut app = new_app();
app.set_snapshot(s);
app.on_key(key(KeyCode::Down));
app
}
#[test]
fn p_with_two_allowed_switches_to_the_other() {
let mut app = profile_app(Some("work"), &["personal", "work"]);
app.on_key(key(KeyCode::Char('P')));
assert_eq!(app.take_pending_action(), Some(PromptAction::Profile { instance: "inst0".into(), profile: "personal".into() }));
assert!(matches!(app.modal, Modal::None));
}
#[test]
fn p_with_one_allowed_says_so() {
let mut app = profile_app(Some("work"), &["work"]);
app.on_key(key(KeyCode::Char('P')));
assert_eq!(app.take_pending_action(), None);
assert_eq!(app.status.as_deref(), Some("inst0: no other profile to switch to"));
}
#[test]
fn p_with_more_opens_the_picker_and_enter_switches() {
let mut app = profile_app(Some("b"), &["a", "b", "c"]);
app.on_key(key(KeyCode::Char('P')));
let Modal::Confirm(m) = &app.modal else { panic!("picker not open") };
assert_eq!(m.question, "inst0: switch to profile");
assert_eq!(m.body, [" a", "› b (current)", " c"]);
app.on_key(key(KeyCode::Down));
let Modal::Confirm(m) = &app.modal else { panic!("picker closed") };
assert_eq!(m.body, [" a", " b (current)", "› c"]);
assert_eq!(app.take_pending_action(), None);
app.on_key(key(KeyCode::Enter));
assert!(matches!(app.modal, Modal::None));
assert_eq!(app.take_pending_action(), Some(PromptAction::Profile { instance: "inst0".into(), profile: "c".into() }));
app.on_key(key(KeyCode::Char('P')));
app.on_key(key(KeyCode::Esc));
assert!(matches!(app.modal, Modal::None));
assert_eq!(app.take_pending_action(), None);
}
#[test]
fn p_on_a_machines_row_uses_its_sandbox_row() {
let mut app = machine_app();
let s = app.snapshot.as_mut().unwrap();
s.sandboxes[1].profiles = strings(&["home", "office"]);
s.instances[2].profile = Some("home".into());
app.selected[Tab::Instances.index()] = 4;
app.on_key(key(KeyCode::Char('P')));
assert_eq!(app.take_pending_action(), Some(PromptAction::Profile { instance: "box/inst0".into(), profile: "office".into() }));
app.selected[Tab::Instances.index()] = 1;
app.on_key(key(KeyCode::Char('P')));
assert_eq!(app.status.as_deref(), Some("inst0: no other profile to switch to"));
}
#[test]
fn p_on_an_orphan_says_its_profiles_are_unknown() {
let mut app = new_app();
app.set_snapshot(orphan_snapshot());
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('P')));
assert_eq!(app.status.as_deref(), Some("orphan0: sandbox `gone` isn't listed, so its profiles aren't known"));
}
}