use crate::tui::data::{ContainerStatus, ServiceRow};
use crate::tui::term::TermSession;
use super::*;
pub(super) fn new_app() -> App {
App::new(PathBuf::from("."))
}
pub(super) fn key(code: KeyCode) -> KeyEvent {
KeyEvent::new(code, KeyModifiers::NONE)
}
pub(super) fn key_mods(code: KeyCode, modifiers: KeyModifiers) -> KeyEvent {
KeyEvent::new(code, modifiers)
}
pub(super) fn port_rows(n: usize) -> Vec<PortRow> {
(0..n)
.map(|i| PortRow {
id: i as u64,
local: format!("127.0.0.1:{}", 3000 + i),
target: format!("api:{}", 3000 + i),
process: None,
state: "active".into(),
conns: 0,
configured: false,
open: false,
machine: None,
})
.collect()
}
pub(super) fn snapshot_with(n: usize) -> Snapshot {
use crate::tui::data::{ContainerStatus, InstanceRow, SandboxRow};
let instances = (0..n)
.map(|i| InstanceRow {
name: format!("inst{i}"),
sandbox: "s".into(),
container: format!("devsandbox-inst{i}"),
status: ContainerStatus::Missing,
uptime_secs: 0,
cpu: None,
mem: None,
folder: "/f".into(),
worktree: false,
services: Vec::new(),
workspace: "/w".into(),
remote_user: None,
remote_env_len: 0,
base_folder: "/f".into(),
drift: false,
instance_id: String::new(),
dispatcher: None,
done: false,
profile: None,
profile_disallowed: false,
browser_sandbox: None, github_repos: Vec::new(), build_notes: Vec::new(),
phase: None,
root: None,
mesh_root: None,
machine: None,
})
.collect();
let sandboxes = vec![SandboxRow {
name: "s".into(),
source: "image x".into(),
folder: None,
services: Vec::new(),
extends: Vec::new(),
config_hash: "hash".into(),
build_hash: String::new(),
issues: Vec::new(),
identity: Vec::new(),
dispatcher: false,
worktree_branch: None,
profiles: Vec::new(),
root: None,
mesh_root: None,
machine: None,
}];
Snapshot {
instances,
sandboxes,
services: Vec::new(),
sandbox_count: 1,
runtime_name: "docker".into(),
runtime_version: None,
collected_at: std::time::Instant::now(),
depth: crate::snapshot::Depth::Full,
error: None,
no_runtime: false,
}
}
pub(super) fn machine_app() -> App {
use crate::serve::snapshots::LinkState;
let mut s = snapshot_with_status(2, running());
let mut remote = snapshot_with_status(2, running());
for r in &mut remote.instances {
r.machine = Some("box".into());
r.root = Some("/r/one".into());
}
for r in &mut remote.sandboxes {
r.machine = Some("box".into());
r.root = Some("/r/one".into());
}
s.instances.extend(remote.instances);
s.sandboxes.extend(remote.sandboxes);
let mut db = service_snapshot(vec![("devsandbox-svc-db".into(), running())]).services.remove(0);
db.name = "db".into();
db.machine = Some("box".into());
db.root = Some("/r/one".into());
s.services.push(db);
let mut app = new_app();
let mut down = crate::tui::remote::tests::sm("down", LinkState::Unreachable);
down.error = Some("Connection refused".into());
app.machines = vec![crate::tui::remote::tests::sm("box", LinkState::Connected), down];
app.set_snapshot(s);
app
}
pub(super) fn open_modal(app: &mut App) {
app.modal = Modal::Config(ConfigView {
title: "s".into(),
kind: ConfigKind::Sandbox,
original: "[sandbox.s]\nimage = \"a\"\n".into(),
resolved: (0..10).map(|i| format!("line{i}\n")).collect(),
showing: Side::Original,
scroll: 0,
hash: "deadbeef".into(),
inspect: (0..20).map(|i| format!("insp{i}\n")).collect(),
inspect_scroll: 0,
inspect_container: "devsandbox-s".into(),
focus: Pane::Config,
split_pct: 50,
});
}
pub(super) fn view(app: &App) -> &ConfigView {
match &app.modal {
Modal::Config(v) => v,
_ => panic!("config modal not open"),
}
}
pub(super) fn orphan_snapshot() -> Snapshot {
use crate::tui::data::{ContainerStatus, InstanceRow};
let instances = vec![InstanceRow {
name: "orphan0".into(),
sandbox: "gone".into(),
container: "devsandbox-orphan0".into(),
status: ContainerStatus::Missing,
uptime_secs: 0,
cpu: None,
mem: None,
folder: "/f".into(),
worktree: false,
services: Vec::new(),
workspace: "/w".into(),
remote_user: None,
remote_env_len: 0,
base_folder: "/f".into(),
drift: false,
instance_id: String::new(),
dispatcher: None,
done: false,
profile: None,
profile_disallowed: false,
browser_sandbox: None, github_repos: Vec::new(), build_notes: Vec::new(),
phase: None,
root: None,
mesh_root: None,
machine: None,
}];
Snapshot {
instances,
sandboxes: Vec::new(),
services: Vec::new(),
sandbox_count: 0,
runtime_name: "docker".into(),
runtime_version: None,
collected_at: std::time::Instant::now(),
depth: crate::snapshot::Depth::Full,
error: None,
no_runtime: false,
}
}
pub(super) fn prompt(app: &App) -> &Prompt {
app.prompt.as_ref().expect("prompt open")
}
pub(super) fn snapshot_with_status(n: usize, status: ContainerStatus) -> Snapshot {
let mut snap = snapshot_with(n);
for row in &mut snap.instances {
row.status = status.clone();
}
snap
}
pub(super) fn running() -> ContainerStatus {
ContainerStatus::Running("Up".into())
}
pub(super) fn snapshot_with_phase(n: usize, status: ContainerStatus, phase: crate::snapshot::PhaseRow) -> Snapshot {
let mut snap = snapshot_with_status(n, status);
for row in &mut snap.instances {
row.phase = Some(phase.clone());
}
snap
}
pub(super) fn in_flight(step: &str) -> crate::snapshot::PhaseRow {
crate::snapshot::PhaseRow::InFlight { step: step.into() }
}
pub(super) fn failed(step: &str) -> crate::snapshot::PhaseRow {
crate::snapshot::PhaseRow::Failed { step: step.into(), error: "exit status 1".into() }
}
pub(super) fn expand_with_rows(app: &mut App, instance: &str, pids: &[&str]) {
use crate::tui::procs::{ProcRow, ProcState};
let rows = pids
.iter()
.map(|p| ProcRow { pid: p.to_string(), depth: 0, args: "x".into() })
.collect();
app.expanded_procs.insert(instance.to_string());
app.procs
.insert(instance.to_string(), ProcState::Rows { rows, signalable: true, sessions: None });
}
pub(super) fn app_on_proc_row(pids: &[&str]) -> App {
let mut app = new_app();
app.set_snapshot(snapshot_with_status(1, running()));
expand_with_rows(&mut app, "inst0", pids);
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Down));
assert_eq!(app.selected_node(), Some(Node::Proc { instance: 0, row: 0 }));
app
}
pub(super) fn select_session_row(mut app: App, key: &str, args: &str) -> App {
use crate::tui::procs::{ProcRow, ProcState, SESSION_PID};
let rows = vec![ProcRow { pid: SESSION_PID.into(), depth: 0, args: args.into() }];
app.expanded_procs.insert(key.into());
app.procs.insert(key.into(), ProcState::Rows { rows, signalable: false, sessions: None });
let snapshot = app.snapshot.as_ref().unwrap();
let i = snapshot.instances.iter().position(|r| crate::tui::remote::instance_key(r) == key).unwrap();
let at = app.visible_nodes().iter().position(|n| *n == Node::Proc { instance: i, row: 0 }).unwrap();
app.selected[Tab::Instances.index()] = at;
app
}
pub(super) fn mouse(kind: MouseEventKind, column: u16) -> MouseEvent {
MouseEvent { kind, column, row: 0, modifiers: KeyModifiers::NONE }
}
pub(super) fn mouse_at(kind: MouseEventKind, column: u16, row: u16) -> MouseEvent {
MouseEvent { kind, column, row, modifiers: KeyModifiers::NONE }
}
pub(super) fn term_sess(title: &str, container: &str) -> TermSession {
TermSession::test_session(title, container, 24, 80)
}
pub(super) const FRAME: Rect = Rect { x: 0, y: 0, width: 100, height: 40 };
pub(super) const MODAL_AREA: Rect = Rect { x: 0, y: 0, width: 100, height: 40 };
pub(super) fn toks(line: &str) -> Vec<String> {
line.split_whitespace().map(str::to_string).collect()
}
pub(super) fn service_snapshot(containers: Vec<(String, ContainerStatus)>) -> Snapshot {
Snapshot {
instances: Vec::new(),
sandboxes: Vec::new(),
services: vec![ServiceRow {
name: "svc".into(),
scope: "isolated".into(),
source: "image x".into(),
ports: Vec::new(),
containers,
used_by: Vec::new(),
env_len: 0,
command: None,
config_hash: "hash".into(),
drift: false,
root: None,
mesh_root: None,
machine: None,
}],
sandbox_count: 0,
runtime_name: "docker".into(),
runtime_version: None,
collected_at: std::time::Instant::now(),
depth: crate::snapshot::Depth::Full,
error: None,
no_runtime: false,
}
}
pub(super) fn open_test_term(app: &mut App, title: &str, container: &str) {
app.terms
.open(TermSession::test_session(title, container, 24, 80));
app.focus = Focus::Terminal;
}