devsandbox 0.11.1

Manage devcontainer-style sandboxes on docker, podman, or Apple container — many throwaway instances per repo from one TOML config.
//! Fixtures shared by the `app` submodules' tests.

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)
}

/// `n` synthetic forward rows with ids `0..n`.
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()
}

/// A snapshot with one sandbox `s` holding `n` instances. Its Instances tree
/// is `[Sandbox(0), Instance(0)..Instance(n-1)]` when expanded, i.e. node
/// position of instance `i` is `i + 1`.
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,
    }
}

/// Machine `box` (connected) and `down` (unreachable, no rows), plus a
/// snapshot whose sandbox `s` and instances `inst0`/`inst1` exist both here
/// and on `box` (root `/r/one`), all running. Tree: `[Sandbox(0), inst0,
/// inst1, Sandbox(1) box, box/inst0, box/inst1]`: instance rows 0, 1 local,
/// 2, 3 on `box`. Plus a service `db` on `box`.
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
}

/// Install a small config modal directly, bypassing fs/config reads.
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(),
        // Ten lines so scroll has room to move.
        resolved: (0..10).map(|i| format!("line{i}\n")).collect(),
        showing: Side::Original,
        scroll: 0,
        hash: "deadbeef".into(),
        // Twenty inspect lines so the inspect pane scrolls independently.
        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"),
    }
}

/// A snapshot with no configured sandboxes and one instance whose sandbox is
/// not in config, so it groups under the orphan node:
/// tree = [Orphans, Instance(0)].
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")
}

/// `snapshot_with(n)` with every instance's container reporting `status`.
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())
}

/// `snapshot_with(n)` with every instance at `phase` over a `status` container.
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() }
}

/// Seed the proc cache + expansion for an instance so proc rows are visible
/// without a fetch.
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 });
}

/// Select the single process row of `inst0`, returning the app ready to act.
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
}

/// Instance `key` of `app`'s snapshot expanded with one session row
/// (`args` as `tui::procs::session_rows` writes them), selected.
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)
}

// A frame large enough that the terminal panel is laid out; the panel Rect
// is derived from the same helper the real code uses so tests can't drift.
pub(super) const FRAME: Rect = Rect { x: 0, y: 0, width: 100, height: 40 };

// The config modal is full-screen, so its divider math only reads the width;
// a 100-wide area keeps the pre-Rect tests' column→percent mapping intact.
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()
}

/// A snapshot whose single service `svc` has `containers`.
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,
    }
}

/// Open a test terminal directly (no PTY) and focus it, bypassing the
/// I/O-bound `open_terminal`.
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;
}