Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

55.3 KB
Raw Blame
//! Required account entry. Authentication remains owned by the installed CLI,
//! including cancellation, browser callbacks and phone approval; hn never reads tokens.
use std::time::{Duration, Instant};

use ratatui::buffer::Buffer;
use ratatui::layout::{Position, Rect};
use ratatui::style::Modifier;
use ratatui::text::Line;
use ratatui::widgets::{Paragraph, Widget, Wrap};
use serde_json::Value;
use tokio::sync::mpsc;

use crate::app::App;
use crate::devices::{Reply, Req};
use crate::modal::{Modal, PickerKind};
use crate::picker::{Picker, Row};

#[derive(Clone, Debug, Default, PartialEq)]
pub enum Status {
    #[default] Unknown,
    SignedOut,
    SignedIn { offline: bool, email: Option<String> },
}

#[derive(Clone, Debug, Default, PartialEq)]
enum Phase {
    #[default] Ready,
    Starting,
    Browser(String),
    Phone { url: String, expires: Instant },
    Completing,
    Committed,
    SigningOut,
    Failed(String),
}

#[derive(Clone, Debug, PartialEq)]
enum Driver { Answer(bool), Cancel }

#[derive(Default)]
pub struct State {
    pub status: Status,
    mandatory: bool,
    entry_required: bool,
    phase: Phase,
    checked: Option<Instant>,
    checking: bool,
    generation: u64,
    driver: Option<mpsc::UnboundedSender<Driver>>,
    waiting: Option<String>,
    pub qr_drawn: std::cell::Cell<Option<Rect>>,
    #[cfg(test)]
    started: Vec<Vec<String>>,
}

pub fn entry_required(app: &App) -> bool { app.account.entry_required && !app.headless }

/// Required sign-in with no attempt under way: what may be covered without losing a sign-in.
pub fn entry_idle(app: &App) -> bool {
    entry_required(app) && matches!(app.account.phase, Phase::Ready | Phase::Failed(_))
}

/// Every interactive workspace needs an account, including the OS's bundled TUI.
/// Detached servers keep their sessions running.
pub fn begin_entry(app: &mut App) {
    app.account.mandatory = true;
    app.account.entry_required = true;
    enforce_entry(app);
}

/// The local daemon already read this computer's saved identity during startup. Reuse that
/// answer, including offline sign-ins, rather than putting a network round trip before work.
pub fn daemon_status(app: &mut App, status: &Value) {
    if app.account.mandatory && status["signedIn"] == false && !signing_in(app) && app.account.phase != Phase::SigningOut {
        app.account.status = Status::SignedOut;
        app.account.entry_required = true;
    }
    if entry_required(app) && app.account.status == Status::Unknown && !signing_in(app) {
        match status["signedIn"].as_bool() {
            Some(true) => app.account.status = Status::SignedIn { offline: status["offline"] == true, email: None },
            Some(false) => app.account.status = Status::SignedOut,
            None => {}
        }
    }
    enforce_entry(app);
    refill(app);
}

pub fn enforce_entry(app: &mut App) {
    if app.account.mandatory && app.account.status == Status::SignedOut {
        app.account.entry_required = true;
    }
    if !entry_required(app) { return }
    if matches!(app.account.status, Status::SignedIn { .. }) && !signing_in(app) && app.account.phase != Phase::SigningOut {
        app.account.entry_required = false;
        if visible(app) { app.modal = None; }
        app.redraw_all = true;
        app.maybe_start_shell();
    } else if !visible(app) && crate::os_welcome::entry_wifi(app).is_none() {
        crate::input::picker(app, PickerKind::Account, "Sign in to Harness", "");
    }
}

/// The entry page owns input before pane shortcuts, paste, mouse or OS welcome controls.
/// Esc cancels a sign-in attempt; Ctrl+C leaves hn without stopping its harnesses.
pub fn entry_input(app: &mut App, event: &crossterm::event::Event) -> bool {
    use crossterm::event::{Event, KeyCode, KeyEventKind, KeyModifiers, MouseButton, MouseEventKind};
    if !entry_required(app) { return false }
    enforce_entry(app);
    if !entry_required(app) { return false }
    if let Some(pane) = crate::os_welcome::entry_wifi(app) {
        return crate::input::system_form_input(app, pane, event);
    }
    let mut accept = false;
    match event {
        Event::Key(key) => {
            if key.kind == KeyEventKind::Release { return true }
            let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
            match key.code {
                KeyCode::Char('c') if ctrl => { cancel(app); app.quit = true; }
                KeyCode::Esc => { cancel(app); refill(app); }
                KeyCode::Enter => accept = true,
                _ => if let Some(Modal::Picker { picker, .. }) = &mut app.modal {
                    match key.code {
                        KeyCode::Up | KeyCode::BackTab => picker.move_by(-1),
                        KeyCode::Down | KeyCode::Tab => picker.move_by(1),
                        KeyCode::Char('k') if !ctrl => picker.move_by(-1),
                        KeyCode::Char('j') if !ctrl => picker.move_by(1),
                        _ => {}
                    }
                }
            }
        }
        Event::Paste(_) => {}
        Event::Mouse(mouse) => {
            if !app.mouse { return true }
            if let Some(Modal::Picker { picker, .. }) = &mut app.modal {
                match mouse.kind {
                    MouseEventKind::ScrollUp => picker.move_by(-1),
                    MouseEventKind::ScrollDown => picker.move_by(1),
                    MouseEventKind::Down(MouseButton::Left) if picker.list_area.get().contains(Position::new(mouse.column, mouse.row)) => {
                        let row = picker.scroll + (mouse.row - picker.list_area.get().y) as usize;
                        picker.vset(row as i64, 1);
                        accept = true;
                    }
                    _ => {}
                }
            }
        }
        _ => return false,
    }
    if accept {
        if let Some(Modal::Picker { picker, .. }) = app.modal.take() {
            let id = picker.current_id().unwrap_or_default();
            choose(app, picker, &id);
        }
    }
    enforce_entry(app);
    true
}

pub fn label(app: &App) -> &'static str {
    if app.account.status == Status::SignedOut { "Sign in" } else { "Account" }
}

/// The side bar's account button: whose account it is — the email's name, its domain left off
/// (`tuan.dev` for [email protected]) — once the email is known, else [label].
pub fn button(app: &App) -> String {
    match &app.account.status {
        Status::SignedIn { email: Some(email), .. } => email.split_once('@').map_or(email.as_str(), |(name, _)| name).to_string(),
        _ => label(app).to_string(),
    }
}

pub fn open(app: &mut App) {
    crate::input::picker(app, PickerKind::Account, "Your Harness account", "");
    refresh(app, true);
}

/// A sign-in is under way: the browser or phone asked, or the account it chose being committed.
pub fn signing_in(app: &App) -> bool {
    app.account.driver.is_some() || matches!(app.account.phase, Phase::Starting | Phase::Browser(_) | Phase::Phone { .. } | Phase::Completing | Phase::Committed)
}

fn visible(app: &App) -> bool { matches!(app.modal, Some(Modal::Picker { kind: PickerKind::Account, .. })) }

pub(crate) fn identity_changed(app: &mut App) {
    app.account.checked = None; app.account.checking = false;
    // `harness logout` restarts the daemon before it exits; the sign-out finishes when it does.
    if app.account.driver.is_none() && app.account.phase != Phase::SigningOut { app.account.status = Status::Unknown; app.account.phase = Phase::Ready; }
}

/// Sign out asks first, Cancel chosen, so an Enter that was meant for something else signs
/// nothing out. Signed out, there is nothing to ask: the account page offers sign-in.
/// [from_account]: Cancel goes back to the account page it was asked from.
pub fn ask_sign_out(app: &mut App, from_account: bool) {
    if app.account.driver.is_some() || app.account.phase == Phase::SigningOut || !matches!(app.account.status, Status::SignedIn { .. }) {
        if !visible(app) { open(app); }
        return;
    }
    use crate::workspace_menu as menu;
    let mut notes = vec![menu::note("Sign out of your Harness account on this computer?")];
    if let Status::SignedIn { email: Some(email), .. } = &app.account.status { notes.push(menu::note(&format!("You are signed in as {email}."))); }
    notes.push(menu::note("Harnesses on this computer keep running."));
    let button = |label: &str| crate::buttons::Button { label: label.into(), key: None };
    let row = crate::buttons::Row { buttons: vec![button("Cancel"), button("Sign out")], chosen: 0, hint: crate::buttons::KEYS.into() };
    let cancel = if from_account { "account" } else { "" };
    menu::open_buttons(app, "Sign out", notes, row, vec![cancel.into(), "signout -y".into()]);
}

/// The confirmed Sign out: on the account page, where its progress and any error show.
pub fn sign_out_confirmed(app: &mut App) {
    open(app);
    sign_out(app);
}

pub fn refresh(app: &mut App, force: bool) {
    if app.account.checking || (!force && app.account.checked.is_some_and(|t| t.elapsed() < Duration::from_secs(60))) { return }
    app.account.checking = true;
    app.account.checked = Some(Instant::now());
    let generation = app.account.generation;
    crate::devices::ask(app, Req::Cli { args: vec!["auth".into(), "status".into(), "--json".into()], stdin: None }, None, move |app, reply| {
        app.account.checking = false;
        if app.account.generation != generation { return }
        if let Reply::Cli { ok: true, out, .. } = reply {
            let status = out.lines().rev().find_map(|s| serde_json::from_str::<Value>(s).ok());
            if let Some(status) = status {
                let expected = if status["loggedIn"] == true { status["machineId"].as_str() } else { status["computerId"].as_str() };
                if expected.is_some_and(|id| !id.is_empty() && id != app.fleet.local_id) { reconnect(app); }
                match status["loggedIn"].as_bool() {
                    Some(false) => app.account.status = Status::SignedOut,
                    Some(true) => {
                        let email = match &app.account.status { Status::SignedIn { email, .. } => email.clone(), _ => None };
                        app.account.status = Status::SignedIn { offline: status["offline"] == true, email };
                        load_email(app, generation);
                        crate::models::signed_in(app);
                    }
                    None => {}
                }
            }
        }
        enforce_entry(app);
        refill(app);
    });
}

fn load_email(app: &mut App, generation: u64) {
    crate::devices::ask(app, Req::Http { method: "GET", path: "/api/auth/me".into(), body: None }, None, move |app, reply| {
        if app.account.generation != generation { return }
        if let (Status::SignedIn { email, .. }, Reply::Http(Ok(value))) = (&mut app.account.status, reply) {
            *email = value.pointer("/user/email").and_then(Value::as_str).filter(|s| s.contains('@') && !s.chars().any(char::is_control)).map(str::to_string);
        }
        refill(app);
    });
}

pub fn tick(app: &mut App) {
    enforce_entry(app);
    let open = visible(app);
    if !open && !matches!(app.account.phase, Phase::Committed | Phase::Completing) { cancel(app); }
    if !app.headless && (app.account.mandatory || app.account.checked.is_none() || open) { refresh(app, false); }
    if matches!(app.account.phase, Phase::Phone { expires, .. } if Instant::now() >= expires) {
        cancel(app);
        app.account.phase = Phase::Failed("This code expired. Choose Sign in with phone to get a new one.".into());
        refill(app);
    }
}

pub fn fill(app: &App, picker: &mut Picker) {
    picker.keep_order = true;
    picker.preview = true;
    picker.hints = vec![("↑↓", "choose"), ("enter", "open"), if entry_required(app) { ("^C", "quit") } else { ("esc", "back") }];
    let mut rows = Vec::new();
    if entry_required(app) && app.account.status == Status::Unknown && app.account.phase == Phase::Ready {
        rows.push(Row::new(if app.account.checking || app.account.checked.is_none() { "account:wait" } else { "account:retry" },
            if app.account.checking || app.account.checked.is_none() { "Checking your account…" } else { "Retry account check" }));
    } else {
    match &app.account.phase {
        Phase::Starting => rows.push(Row::new("account:wait", app.account.waiting.as_deref().unwrap_or("Starting sign-in…"))),
        Phase::Browser(_) => {
            rows.push(Row::new("account:browser", "Open sign-in page"));
            rows.push(Row::new("account:copy", "Copy sign-in link"));
            rows.push(Row::new("account:cancel", "Cancel sign-in"));
        }
        Phase::Phone { .. } => {
            rows.push(Row::new("account:copy", "Copy phone sign-in link"));
            rows.push(Row::new("account:cancel", "Cancel sign-in"));
        }
        Phase::Completing => rows.push(Row::new("account:wait", "Finishing sign-in…")),
        Phase::Committed => {
            rows.push(Row::new("account:wait", "Signed in · connecting this computer…"));
        }
        Phase::SigningOut => rows.push(Row::new("account:wait", "Signing out…")),
        Phase::Ready | Phase::Failed(_) => match &app.account.status {
            Status::SignedIn { .. } => {
                rows.push(Row::new("account:machines", "Connect a machine"));
                rows.push(Row::new("account:phone", "Add your phone"));
                rows.push(Row::new("account:signout", "Sign out"));
            }
            _ => {
                rows.push(Row::new("account:google", "Continue with Google"));
                rows.push(Row::new("account:apple", "Continue with Apple"));
                rows.push(Row::new("account:email", "Continue with email"));
                rows.push(Row::new("account:qr", "Continue with your phone"));
            }
        },
    }
    }
    // These are page actions, not a growing search result list.
    picker.rows.clear();
    picker.set_rows(rows);
    picker.busy = matches!(app.account.phase, Phase::Starting | Phase::Completing | Phase::Committed | Phase::SigningOut).then(|| "waiting".into());
}

fn refill(app: &mut App) {
    let Some(Modal::Picker { kind: PickerKind::Account, mut picker }) = app.modal.take_if(|m| matches!(m, Modal::Picker { kind: PickerKind::Account, .. })) else { return };
    fill(app, &mut picker);
    app.modal = Some(Modal::Picker { kind: PickerKind::Account, picker });
}

pub fn preview(app: &App, _: &str) -> Vec<Line<'static>> {
    let mut lines = vec![Line::raw("Your workspace, across your devices"), Line::raw(""),
        Line::raw("Connect your computers and sync your workspace."),
        Line::raw("Use models running on another linked machine."),
        Line::raw("Follow your harnesses from your phone."), Line::raw("")];
    match &app.account.status {
        Status::SignedIn { email, offline } => {
            lines.push(Line::raw(email.as_ref().map(|e| format!("Signed in as {e}")).unwrap_or_else(|| "Signed in".into())));
            if *offline { lines.push(Line::raw("Account is offline. Local work is still available.")); }
        }
        _ => lines.push(Line::raw("Sign in to continue. Your saved tabs and running harnesses stay here.")),
    }
    lines.push(Line::raw(""));
    match &app.account.phase {
        Phase::Browser(_) => {
            lines.push(Line::raw("Finish signing in in your browser."));
            lines.push(Line::raw("Using SSH? Cancel and choose Continue with your phone."));
        }
        Phase::Phone { expires, .. } => {
            lines.clear();
            lines.push(Line::raw("On your phone, open Harness → Settings → Sign in a computer."));
            lines.push(Line::raw(""));
            lines.push(Line::raw(format!("Expires in {}s", expires.saturating_duration_since(Instant::now()).as_secs())));
        }
        Phase::Failed(message) => lines.push(Line::raw(message.clone())),
        Phase::Starting => lines.push(Line::raw(app.account.waiting.clone().unwrap_or_else(|| "Opening sign-in…".into()))),
        Phase::SigningOut => lines.push(Line::raw(SIGNING_OUT)),
        _ => {}
    }
    lines
}

/// Account is a small explanation and a few actions, not a catalog to search. It keeps the
/// benefits visible at 80 columns; a phone code is drawn only when the complete code fits.
pub fn draw(buf: &mut Buffer, app: &App, body: Rect, picker: &mut Picker) -> Option<Position> {
    use crate::settings::{self, put};
    let c = settings::chrome();
    settings::backdrop(buf, body, c.backdrop);
    app.account.qr_drawn.set(None);
    picker.preview_area.set(None);
    picker.list_area.set(Rect::default());
    picker.screen_area.set(body);
    if body.width < 24 || body.height < 8 {
        put(buf, body.x, body.y, body.width, if entry_required(app) { "Sign in · resize or ^C quit" } else { "Account · resize or Esc" }, c.base);
        return None;
    }
    let available = settings::area(body, settings::PanelSize::Large, 0);
    let phone = matches!(app.account.phase, Phase::Phone { .. });
    let (w, h) = (available.width.min(if phone { 108 } else { 76 }), available.height.min(if phone { 30 } else { 18 }));
    let r = Rect::new(body.x + (body.width - w) / 2, body.y + (body.height - h) / 2, w, h);
    settings::fill(buf, r, c.base);
    picker.screen_area.set(r);
    let (x, mut right) = (r.x + 2, r.right().saturating_sub(2));
    let mut needs_room = false;
    if let Phase::Phone { url, .. } = &app.account.phase {
        let cols = r.width.saturating_sub(39);
        let rows = r.height.saturating_sub(2);
        if let Some(modules) = crate::devices::qr_modules(url, cols, rows) {
            let qw = modules.len() as u16;
            let qh = qw.div_ceil(2);
            let at = crate::devices::draw_qr(buf, r.right() - qw - 1, r.y + (r.height - qh) / 2, &modules, false);
            right = at.x.saturating_sub(2);
            app.account.qr_drawn.set(Some(at));
        } else { needs_room = true; }
    }
    let width = right.saturating_sub(x);
    put(buf, x, r.y + 1, width, if entry_required(app) { "Sign in to Harness" } else { "Your Harness account" }, c.base.add_modifier(Modifier::BOLD));
    let mut lines = match &app.account.phase {
        Phase::Ready => match &app.account.status {
            Status::SignedIn { email, offline } => vec![Line::raw(email.clone().unwrap_or_else(|| "Signed in".into())),
                Line::raw(if *offline { "Account is offline. Local work is still available." } else { "Manage your connected computers and phone." })],
            _ if entry_required(app) => vec![Line::raw("All your machines. Your tabs everywhere."), Line::raw(""),
                Line::raw(if app.account.status == Status::Unknown { "Checking this computer's account. Your work stays here." } else { "Your saved tabs stay here. Running harnesses keep running." })],
            _ => vec![Line::raw("Connect computers and sync your workspace."), Line::raw("Use models on a linked machine. Follow work from your phone."),
                Line::raw(""), Line::raw("Sign in to continue.")],
        },
        Phase::Browser(_) => vec![Line::raw("Finish signing in in your browser."), Line::raw("Using SSH? Cancel and choose Continue with your phone.")],
        Phase::Failed(message) => vec![Line::raw(message.clone())],
        Phase::Phone { .. } => preview(app, ""),
        Phase::Starting => vec![Line::raw(app.account.waiting.clone().unwrap_or_else(|| "Opening sign-in…".into()))],
        Phase::Completing => vec![Line::raw("Finishing sign-in…")],
        Phase::Committed => vec![Line::raw("Signed in. Connecting this computer…")],
        Phase::SigningOut => vec![Line::raw(SIGNING_OUT)],
    };
    if needs_room { lines.extend([Line::raw(""), Line::raw("Enlarge the terminal to scan, or copy the phone sign-in link.")]); }
    // Actions always remain reachable, including in a short terminal. The explanation uses
    // the remaining rows and wraps; wheel/arrow navigation still works in a one-row list.
    let compact = r.height < 14;
    // Tight: the actions do not fit beside the explanation, so they take its rows and the
    // bottom padding instead.
    let tight = compact && picker.rows.len() as u16 > r.height.saturating_sub(5);
    let hint_y = r.bottom().saturating_sub(if tight { 1 } else { 2 });
    let bottom = r.bottom().saturating_sub(if tight { 1 } else if compact { 2 } else { 3 });
    let count = picker.rows.len().min(bottom.saturating_sub(r.y + if tight { 2 } else if compact { 3 } else { 4 }) as usize).max(1) as u16;
    let list_top = bottom.saturating_sub(count);
    let description_top = r.y + if compact { 2 } else { 3 };
    if !tight {
        Paragraph::new(lines).style(c.muted).wrap(Wrap { trim: false })
            .render(Rect::new(x, description_top, width, list_top.saturating_sub(description_top + if compact { 0 } else { 1 })), buf);
    }
    settings::list(buf, picker, Rect::new(x, list_top, width, count), &c, false);
    let hint = if entry_required(app) {
        if compact { "↑↓ choose · Enter · ^C quit".into() } else { "↑↓ choose   enter open   esc cancel   ^C quit".into() }
    } else if picker.query.is_empty() { if compact { "↑↓ choose · Enter · Esc back".into() } else { "↑↓ choose   enter open   esc back".into() } } else { format!("Filter: {}", picker.query) };
    put(buf, x, hint_y, width, &hint, c.muted);
    None
}

pub fn choose(app: &mut App, mut picker: Picker, id: &str) {
    // Reattach before callbacks update the view; every completion checks its own attempt.
    let action = id.strip_prefix("account:").unwrap_or("");
    if action == "back" { cancel(app); enforce_entry(app); return }
    if matches!(action, "machines" | "phone") {
        crate::input::run(app, if action == "machines" { "connect-machine" } else { "add-phone" });
        return;
    }
    picker.set_query("");
    app.modal = Some(Modal::Picker { kind: PickerKind::Account, picker });
    match action {
        "google" | "apple" | "email" | "qr" => start(app, action),
        "retry" => { refresh(app, true); refill(app); }
        "signout" => ask_sign_out(app, true),
        "cancel" => { cancel(app); refill(app); }
        "browser" => { if let Phase::Browser(url) = &app.account.phase { open_browser(app, url.clone()); } }
        "copy" => {
            let url = match &app.account.phase { Phase::Browser(url) | Phase::Phone { url, .. } => Some(url.clone()), _ => None };
            if let Some(url) = url { crate::devices::ask(app, Req::Copy(url), None, |_, _| {}); }
        }
        _ => {}
    }
}

/// hn's own sign-in, chosen and finished — what the browser and `harness login` would do.
#[cfg(test)]
pub(crate) fn sign_in_for_test(app: &mut App) {
    start(app, "google");
    let generation = app.account.generation;
    finished(app, generation, true, None);
}

fn cancel(app: &mut App) {
    if matches!(app.account.phase, Phase::Committed | Phase::Completing) { return }
    // A sign-in that a Set up asked for, given up: nothing waits to set grid up behind a later one.
    if (app.account.driver.is_some() || app.account.status == Status::SignedOut) && !app.models_view.set_up_signed_in { app.models_view.set_up_after_sign_in = false }
    if let Some(driver) = app.account.driver.take() {
        let _ = driver.send(Driver::Cancel);
        app.account.generation += 1;
        app.account.phase = Phase::Ready;
        app.account.waiting = None;
    }
}

fn start(app: &mut App, method: &str) {
    if app.account.driver.is_some() || matches!(app.account.status, Status::SignedIn { .. }) { return }
    app.account.generation += 1;
    let generation = app.account.generation;
    let mut args = vec!["login".into(), "--json".into(), "--entry-point=tui".into()];
    // With no provider hint the existing SSO page offers email and a verification code.
    if method != "email" { args.push(format!("--{method}")); }
    if app.account.status == Status::SignedOut { args.push("--force".into()); }
    let (driver, receive) = mpsc::unbounded_channel();
    app.account.driver = Some(driver);
    app.account.phase = Phase::Starting;
    app.account.waiting = None;
    refill(app);
    #[cfg(test)]
    { let _ = receive; app.account.started.push(args); }
    #[cfg(not(test))]
    {
        let sink = app.sink.clone();
        let mut command = cli();
        command.args(args);
        tokio::spawn(drive(command, generation, sink, receive));
    }
    let _ = generation;
}

const SIGNING_OUT: &str = "Signing out. Harnesses on this computer keep running.";

#[cfg(not(test))]
fn cli() -> tokio::process::Command {
    let exe = std::env::var("HARNESS_CLI").unwrap_or_else(|_| "harness".into());
    let script: Vec<String> = std::env::var("HARNESS_CLI_ARGS").ok().and_then(|s| serde_json::from_str(&s).ok()).unwrap_or_default();
    let mut command = tokio::process::Command::new(exe);
    command.args(script);
    command
}

/// Sign out as the desktop app does: `harness logout` clears the sign-in and brings this
/// computer's daemon back signed out. Local harnesses keep running behind required sign-in.
fn sign_out(app: &mut App) {
    if app.account.driver.is_some() || app.account.phase == Phase::SigningOut || !matches!(app.account.status, Status::SignedIn { .. }) { return }
    app.account.generation += 1;
    let generation = app.account.generation;
    app.account.phase = Phase::SigningOut;
    refill(app);
    #[cfg(test)]
    app.account.started.push(vec!["logout".into()]);
    #[cfg(not(test))]
    {
        use std::process::Stdio;
        let mut command = cli();
        // Not piped: the daemon it restarts inherits these, and would hold a pipe open forever.
        command.arg("logout").stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()).kill_on_drop(true);
        app.spawn(async move {
            tokio::time::timeout(Duration::from_secs(60), command.status()).await.is_ok_and(|s| s.is_ok_and(|s| s.success()))
        }, move |app, ok| signed_out(app, generation, ok));
    }
    let _ = generation;
}

fn signed_out(app: &mut App, generation: u64, ok: bool) {
    if generation != app.account.generation || app.account.phase != Phase::SigningOut { return }
    if ok {
        app.account.status = Status::SignedOut;
        app.account.phase = Phase::Ready;
        begin_entry(app);
        reconnect(app);
        refresh(app, true);
    } else {
        app.account.phase = Phase::Failed("Could not sign out. Try again.".into());
        refresh(app, true);
    }
    enforce_entry(app);
    refill(app);
}

fn valid_url(url: &str) -> bool {
    // The CLI gets these links from SSO. They are never shell commands or local file URLs.
    url.starts_with("https://") && url.len() <= 8192 && !url.chars().any(|c| c.is_control() || c.is_whitespace())
}

fn open_browser(app: &mut App, url: String) {
    if !valid_url(&url) { return }
    #[cfg(test)]
    { let _ = (app, url); }
    #[cfg(not(test))]
    app.spawn(async move {
        let mut command = crate::sys::url_opener();
        command
            .arg(url).stdin(std::process::Stdio::null()).stdout(std::process::Stdio::null()).stderr(std::process::Stdio::null())
            .kill_on_drop(true);
        tokio::time::timeout(Duration::from_secs(10), command.status()).await.is_ok_and(|s| s.is_ok_and(|s| s.success()))
    }, |app, opened| {
        if !opened { if let Some(Modal::Picker { kind: PickerKind::Account, picker }) = &mut app.modal {
            picker.say("Could not open a browser. Copy the link, or use Continue with your phone.");
        } }
    });
}

fn event(app: &mut App, generation: u64, value: Value) {
    if generation != app.account.generation || app.account.driver.is_none() { return }
    let success = value["type"] == "result" && value["status"] == "success";
    if !visible(app) && !success && !matches!(app.account.phase, Phase::Committed | Phase::Completing) { cancel(app); return }
    let clean = |k: &str| value[k].as_str().map(|s| s.chars().filter(|c| !c.is_control()).take(1024).collect::<String>()).unwrap_or_default();
    match value["type"].as_str() {
        Some("authorize_url") => if let Some(url) = value["url"].as_str().filter(|u| valid_url(u)) {
            app.account.phase = Phase::Browser(url.into());
            // Over SSH, a loopback browser callback cannot reach the server. Phone sign-in is
            // offered explicitly; never report a browser on the remote host as the user's.
            if std::env::var_os("SSH_CONNECTION").is_none() && std::env::var_os("SSH_TTY").is_none() { open_browser(app, url.into()); }
        },
        Some("qr") => if let Some(url) = value["url"].as_str().filter(|u| valid_url(u)) {
            let seconds = value["expiresIn"].as_u64().unwrap_or(120).clamp(1, 600);
            app.account.phase = Phase::Phone { url: url.into(), expires: Instant::now() + Duration::from_secs(seconds) };
        },
        // The phone that approved is signed in to the account, and its person scanned this code, so
        // there is nothing left to ask here: the CLI is told yes and finishes the sign-in.
        Some("confirm") => {
            let email = clean("email");
            if !email.contains('@') { cancel(app); app.account.phase = Phase::Failed("The phone returned no valid account. Try signing in again.".into()); }
            else {
                if let Some(tx) = &app.account.driver { let _ = tx.send(Driver::Answer(true)); }
                app.account.phase = Phase::Completing;
                app.account.waiting = Some("Finishing sign-in…".into());
            }
        }
        Some("waiting") => app.account.waiting = Some(clean("message")),
        Some("result") if value["status"] == "success" => {
            let email = clean("email");
            app.account.status = Status::SignedIn { offline: false, email: (!email.is_empty()).then_some(email) };
            app.account.phase = Phase::Committed;
        }
        Some("result") => app.account.phase = Phase::Failed({ let text = clean("message"); if text.is_empty() { "Sign-in did not complete. Try again.".into() } else { text } }),
        _ => {}
    }
    refill(app);
}

fn finished(app: &mut App, generation: u64, committed: bool, error: Option<String>) {
    // Cancelling the view can race the browser's final approval. The CLI is allowed to finish
    // a committed identity write; read the actual result once it exits, without reopening UI.
    if generation != app.account.generation {
        if app.account.driver.is_none() {
            if committed { reconnect(app); }
            refresh(app, true);
        }
        return;
    }
    app.account.driver = None;
    if committed {
        app.account.phase = error.map(Phase::Failed).unwrap_or(Phase::Ready);
        // This sign-in is hn's own: a Set up that asked for it goes on (only that one sets grid up).
        crate::models::own_sign_in_done(app);
        // The CLI commits credentials and restarts its daemon before it exits.
        reconnect(app);
        refresh(app, true);
    } else if !matches!(app.account.phase, Phase::Failed(_)) {
        app.account.phase = Phase::Failed(error.unwrap_or_else(|| "Sign-in did not complete. Try again.".into()));
    }
    enforce_entry(app);
    refill(app);
}

fn reconnect(app: &mut App) {
    #[cfg(not(test))]
    app.boot();
    #[cfg(test)]
    let _ = app;
}

async fn drive(mut command: tokio::process::Command, generation: u64, sink: mpsc::UnboundedSender<crate::event::Event>, mut commands: mpsc::UnboundedReceiver<Driver>) {
    use std::process::Stdio;
    use tokio::io::{AsyncBufReadExt, AsyncWriteExt};
    let emit = |value: Value| { let _ = sink.send(crate::event::Event::Apply(Box::new(move |app| event(app, generation, value)))); };
    let done = |committed: bool, error: Option<String>| { let _ = sink.send(crate::event::Event::Apply(Box::new(move |app| finished(app, generation, committed, error)))); };
    let child = command.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::piped()).kill_on_drop(true).spawn();
    let mut child = match child { Ok(child) => child, Err(_) => { done(false, Some("Could not run the Harness CLI. Check that harness is installed.".into())); return } };
    let mut input = child.stdin.take();
    let mut lines = tokio::io::BufReader::new(child.stdout.take().unwrap()).lines();
    let mut stderr = child.stderr.take().unwrap();
    let drain = tokio::spawn(async move { let _ = tokio::io::copy(&mut stderr, &mut tokio::io::sink()).await; });
    let deadline = tokio::time::sleep(Duration::from_secs(6 * 60));
    tokio::pin!(deadline);
    let mut committed = false;
    let mut accepting = true;
    let mut cancelled = false;
    loop {
        tokio::select! {
            line = lines.next_line() => match line {
                Ok(Some(line)) if line.len() <= 65536 => {
                    if let Ok(value) = serde_json::from_str::<Value>(&line) {
                        if value["type"] == "result" && value["status"] == "success" { committed = true; }
                        emit(value);
                    }
                }
                Ok(Some(_)) => {},
                _ => break,
            },
            command = commands.recv(), if accepting => match command {
                Some(Driver::Answer(yes)) if !committed => if let Some(stdin) = &mut input { let _ = stdin.write_all(if yes { b"yes\n" } else { b"no\n" }).await; },
                Some(Driver::Cancel) | None => {
                    if committed { accepting = false; }
                    else {
                        // Closing the driver's stdin lets the CLI revoke a pending QR, yet
                        // finish if browser/phone approval has already committed. SIGTERM in
                        // that small interval would interrupt the session write or restart.
                        input.take();
                        cancelled = true;
                        accepting = false;
                        deadline.as_mut().reset(tokio::time::Instant::now() + Duration::from_secs(120));
                    }
                }
                _ => {},
            },
            _ = &mut deadline => { cancelled = true; break; },
        }
    }
    input.take();
    let exited = match tokio::time::timeout(Duration::from_secs(if committed { 60 } else { 3 }), child.wait()).await {
        Ok(Ok(status)) => status.success(),
        Ok(Err(_)) => false,
        Err(_) => { let _ = child.kill().await; false },
    };
    drain.abort();
    let error = if committed && !exited {
        Some("Signed in, but this computer could not reconnect. Run harness start, then reopen Account.".into())
    } else if cancelled && !committed { Some("Sign-in was cancelled or timed out. Try signing in again.".into()) }
    else { None };
    done(committed, error);
}

#[cfg(test)]
mod tests {
    use super::*;
    use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
    use serde_json::json;
    use std::sync::{Arc, Mutex};

    fn app() -> App {
        let (sink, _) = mpsc::unbounded_channel();
        let mut app = App::new(19789, sink, (100, 30));
        app.account.status = Status::SignedOut;
        app
    }

    fn key(app: &mut App, code: KeyCode) {
        crate::input::handle(app, Event::Key(KeyEvent::new(code, KeyModifiers::NONE)));
    }

    /// The side bar's account button says whose it is: the email's name, not its domain.
    #[test]
    fn the_account_button_says_the_username() {
        let mut app = app();
        assert_eq!(button(&app), "Sign in");
        app.account.status = Status::SignedIn { offline: false, email: None };
        assert_eq!(button(&app), "Account", "until the email is known");
        app.account.status = Status::SignedIn { offline: false, email: Some("[email protected]".into()) };
        assert_eq!(button(&app), "tuan.dev");
    }

    fn select(app: &mut App, id: &str) {
        let Some(Modal::Picker { picker, .. }) = &mut app.modal else { panic!("account is not open") };
        picker.select(id);
        assert_eq!(picker.current_id().as_deref(), Some(id));
        key(app, KeyCode::Enter);
    }

    fn render(app: &mut App, width: u16, height: u16) -> (String, Buffer) {
        let mut term = ratatui::Terminal::new(ratatui::backend::TestBackend::new(width, height)).unwrap();
        term.draw(|f| crate::ui::draw(f, app)).unwrap();
        let buf = term.backend().buffer().clone();
        let text = (0..height).map(|y| (0..width).map(|x| buf[(x, y)].symbol().to_string()).collect::<String>()).collect::<Vec<_>>().join("\n");
        (text, buf)
    }

    #[test]
    fn account_entry_waits_for_an_explicit_provider_choice() {
        let mut app = app();
        let seen = Arc::new(Mutex::new(Vec::new()));
        let log = seen.clone();
        app.devices.runner = Some(Box::new(move |req| {
            log.lock().unwrap().push(req.clone());
            Some(Reply::Cli { ok: true, out: "{\"loggedIn\":false}".into(), err: String::new() })
        }));
        tick(&mut app);
        assert_eq!(label(&app), "Sign in");
        assert!(app.account.started.is_empty());
        open(&mut app);
        let (text, _) = render(&mut app, 80, 24);
        for expected in ["Connect computers", "Sign in to continue", "Continue with email", "Continue with Google"] { assert!(text.contains(expected), "missing {expected}:\n{text}"); }
        assert!(!text.contains("Keep using locally"));
        key(&mut app, KeyCode::Esc);
        tick(&mut app);
        assert!(app.modal.is_none());
        assert!(app.account.started.is_empty());
        assert!(seen.lock().unwrap().iter().all(|r| matches!(r, Req::Cli { args, .. } if args == &["auth", "status", "--json"])));
    }

    #[test]
    fn short_account_panels_keep_all_actions_and_a_clear_exit_visible() {
        let mut app = app();
        for signed_in in [false, true] {
            app.account.status = if signed_in { Status::SignedIn { email:Some("[email protected]".into()), offline:false } } else { Status::SignedOut };
            crate::input::picker(&mut app, PickerKind::Account, "Your Harness account", "");
            let (text, _) = render(&mut app, 40, 12);
            for label in if signed_in { vec!["Connect a machine", "Add your phone", "Sign out"] }
                else { vec!["Continue with Google", "Continue with Apple", "Continue with email", "Continue with your phone"] } {
                assert!(text.contains(label), "{label}\n{text}");
            }
            if signed_in { for gone in ["Models on your machines", "Back to workspace"] { assert!(!text.contains(gone), "{gone} is not offered\n{text}") } }
            assert!(text.contains("Esc back"));
            let Some(Modal::Picker { picker, .. }) = &app.modal else { panic!() };
            assert!(picker.list_area.get().height >= if signed_in { 3 } else { 4 }, "every action stays visible");
        }
    }

    #[test]
    fn required_entry_owns_keys_paste_and_mouse_without_touching_saved_tabs() {
        use crossterm::event::{MouseButton, MouseEvent, MouseEventKind};
        let mut app = app();
        app.devices.runner = Some(Box::new(|_| None));
        app.rename_tab("Saved project");
        app.keymap.bind(crate::keys::Table::Root, crate::keys::of(&KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE)), "new-window".into(), false);
        begin_entry(&mut app);
        let tabs = app.tabs.len();
        let (text, _) = render(&mut app, 80, 24);
        assert!(text.contains("Sign in to Harness") && text.contains("Continue with email"), "{text}");
        assert!(!text.contains("Saved project") && !text.contains("Keep using locally"), "the workspace is covered");
        for event in [Event::Key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)),
            Event::Key(KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE)),
            Event::Paste("new-window\n".into()),
            Event::Mouse(MouseEvent { kind: MouseEventKind::Down(MouseButton::Left), column: 0, row: 0, modifiers: KeyModifiers::NONE })] {
            crate::input::handle(&mut app, event);
            assert!(entry_required(&app) && visible(&app));
        }
        assert_eq!(app.tabs.len(), tabs);
        assert_eq!(app.tab().name, "Saved project");
        assert!(app.account.started.is_empty());
        crate::input::handle(&mut app, Event::Key(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL)));
        assert!(app.quit);
        assert_eq!(app.tabs.len(), tabs, "quit detaches without destroying saved work");
    }

    #[test]
    fn entry_cancel_failure_and_late_approval_keep_the_gate_until_login_finishes() {
        let mut app = app();
        app.devices.runner = Some(Box::new(|_| None));
        begin_entry(&mut app);
        select(&mut app, "account:google");
        let old = app.account.generation;
        key(&mut app, KeyCode::Esc);
        assert!(entry_required(&app) && visible(&app));
        assert!(app.account.driver.is_none());
        event(&mut app, old, json!({"type":"result", "status":"success"}));
        assert_eq!(app.account.status, Status::SignedOut);
        select(&mut app, "account:email");
        assert_eq!(app.account.started.last().unwrap(), &["login", "--json", "--entry-point=tui", "--force"]);
        let generation = app.account.generation;
        finished(&mut app, generation, false, Some("Try again".into()));
        assert!(entry_required(&app));
        select(&mut app, "account:apple");
        let generation = app.account.generation;
        event(&mut app, generation, json!({"type":"result", "status":"success"}));
        enforce_entry(&mut app);
        assert!(entry_required(&app), "wait for the CLI to finish adopting the account");
        finished(&mut app, generation, true, None);
        assert!(!entry_required(&app) && app.modal.is_none());
    }

    #[test]
    fn startup_reuses_a_signed_in_daemon_including_offline_and_expiry_requires_sign_in() {
        let mut app = app();
        app.account.status = Status::Unknown;
        begin_entry(&mut app);
        assert!(entry_required(&app));
        daemon_status(&mut app, &json!({"signedIn":true,"offline":true}));
        assert!(!entry_required(&app) && app.modal.is_none());
        assert_eq!(app.account.status, Status::SignedIn { offline: true, email: None });
        app.devices.runner = Some(Box::new(|req| match req {
            Req::Cli { .. } => Some(Reply::Cli { ok: true, out: "{\"loggedIn\":false}".into(), err: String::new() }),
            _ => None,
        }));
        // Check credentials periodically even while the account panel is closed.
        app.account.checked = None;
        tick(&mut app);
        assert!(entry_required(&app), "runtime expiry requires sign-in again, as Desktop does");
        begin_entry(&mut app);
        assert!(entry_required(&app), "a new launch signed out requires sign-in again");
    }

    #[test]
    fn os_workspaces_require_the_same_account_as_other_terminals() {
        let mut app = app();
        app.os_session = true;
        app.os_live = true;
        begin_entry(&mut app);
        tick(&mut app);
        assert!(entry_required(&app));
        assert!(visible(&app));
        app.os_live = false;
        app.os_first_use = true;
        begin_entry(&mut app);
        assert!(entry_required(&app));
        assert!(visible(&app));
        app.account.status = Status::Unknown;
        daemon_status(&mut app, &json!({"signedIn":true,"offline":true}));
        assert!(!entry_required(&app));
    }

    #[test]
    fn failed_or_missing_account_reads_do_not_admit_a_guest_and_retry_can_recover() {
        let mut app = app();
        app.account.status = Status::Unknown;
        app.devices.runner = Some(Box::new(|_| Some(Reply::Cli { ok: false, out: String::new(), err: "unavailable".into() })));
        begin_entry(&mut app);
        refresh(&mut app, true);
        assert!(entry_required(&app));
        let (text, _) = render(&mut app, 80, 24);
        assert!(text.contains("Retry account check"), "{text}");
        app.devices.runner = Some(Box::new(|_| Some(Reply::Cli { ok: true, out: "{\"loggedIn\":true,\"offline\":true}".into(), err: String::new() })));
        select(&mut app, "account:retry");
        assert!(!entry_required(&app));
    }

    #[test]
    fn sign_out_runs_the_cli_once_and_ends_signed_out_on_this_computer() {
        let mut app = app();
        app.devices.runner = Some(Box::new(|_| None));
        let asking = |app: &App| matches!(&app.modal, Some(Modal::Menu(m)) if m.title == "Sign out" && m.buttons.is_some());
        // Signed out, the command panel's Sign out asks nothing and runs nothing: the account page offers sign-in.
        crate::input::run(&mut app, "signout");
        assert!(visible(&app) && !asking(&app));
        assert_eq!(app.account.started, Vec::<Vec<String>>::new());
        app.modal = None;
        app.account.status = Status::SignedIn { email: Some("[email protected]".into()), offline: false };
        open(&mut app);
        let (text, _) = render(&mut app, 80, 24);
        assert!(text.contains("Sign out"), "{text}");
        // Enter on the row asks first, Cancel chosen: a second Enter goes back to the account page.
        select(&mut app, "account:signout");
        assert!(asking(&app));
        let (text, _) = render(&mut app, 80, 24);
        for expected in ["Sign out of your Harness account on this computer?", "[email protected]", "keep running", "Cancel"] { assert!(text.contains(expected), "missing {expected}:\n{text}"); }
        key(&mut app, KeyCode::Enter);
        assert!(visible(&app), "Cancel returns to the account page");
        assert_eq!(app.account.started, Vec::<Vec<String>>::new());
        // From the command panel, Esc just closes the question.
        crate::input::run(&mut app, "signout");
        assert!(asking(&app));
        key(&mut app, KeyCode::Esc);
        assert!(app.modal.is_none());
        assert!(app.toast.is_none(), "{:?}", app.toast);
        assert_eq!(app.account.started, Vec::<Vec<String>>::new());
        // → to Sign out, then Enter.
        open(&mut app);
        select(&mut app, "account:signout");
        key(&mut app, KeyCode::Right);
        key(&mut app, KeyCode::Enter);
        assert_eq!(app.account.started, vec![vec!["logout"]]);
        assert!(visible(&app), "progress shows on the account page");
        let (text, _) = render(&mut app, 80, 24);
        assert!(text.contains("Signing out") && text.contains("keep running"), "{text}");
        // The daemon restarting signed out is part of the sign-out, not the end of it.
        identity_changed(&mut app);
        assert_eq!(app.account.phase, Phase::SigningOut);
        crate::input::run(&mut app, "signout");
        assert!(!asking(&app));
        crate::commands::execute(&mut app, "signout -y");
        assert_eq!(app.account.started.len(), 1, "one sign-out at a time");

        let generation = app.account.generation;
        signed_out(&mut app, generation, false);
        assert_eq!(app.account.phase, Phase::Failed("Could not sign out. Try again.".into()));
        let (text, _) = render(&mut app, 80, 24);
        assert!(text.contains("Could not sign out") && text.contains("Sign out"), "{text}");

        crate::commands::execute(&mut app, "logout -y");
        assert_eq!(app.account.started.len(), 2);
        let generation = app.account.generation;
        signed_out(&mut app, generation - 1, true);
        assert_eq!(app.account.phase, Phase::SigningOut, "a stale result is ignored");
        signed_out(&mut app, generation, true);
        assert_eq!((app.account.status.clone(), app.account.phase.clone()), (Status::SignedOut, Phase::Ready));
        let (text, _) = render(&mut app, 80, 24);
        assert!(text.contains("Continue with Google") && !text.contains("Sign out"), "{text}");
    }

    #[test]
    fn offline_and_failed_status_reads_do_not_offer_an_account_switch() {
        let mut app = app();
        app.devices.runner = Some(Box::new(|req| Some(match req {
            Req::Cli { .. } => Reply::Cli { ok: true, out: "{\"loggedIn\":true,\"offline\":true}".into(), err: String::new() },
            _ => Reply::Http(Err(("OFFLINE".into(), "no network".into()))),
        })));
        open(&mut app);
        assert_eq!(app.account.status, Status::SignedIn { offline: true, email: None });
        assert_eq!(label(&app), "Account");
        app.devices.runner = Some(Box::new(|_| Some(Reply::Cli { ok: false, out: String::new(), err: "timeout".into() })));
        refresh(&mut app, true);
        assert!(matches!(app.account.status, Status::SignedIn { offline: true, .. }));
        start(&mut app, "google");
        assert!(app.account.started.is_empty());
    }

    #[test]
    fn explicit_provider_choice_runs_once_and_cancellation_rejects_late_callbacks() {
        let mut app = app();
        open(&mut app);
        select(&mut app, "account:apple");
        start(&mut app, "google");
        assert_eq!(app.account.started, vec![vec!["login", "--json", "--entry-point=tui", "--apple", "--force"]]);
        let generation = app.account.generation;
        event(&mut app, generation, json!({ "type": "authorize_url", "url": "file:///do-not-open" }));
        assert_eq!(app.account.phase, Phase::Starting);
        event(&mut app, generation, json!({ "type": "authorize_url", "url": "https://sso.example.test/authorize" }));
        assert!(matches!(app.account.phase, Phase::Browser(_)));
        key(&mut app, KeyCode::Esc);
        tick(&mut app);
        assert!(app.account.driver.is_none());
        event(&mut app, generation, json!({ "type": "result", "status": "success", "email": "[email protected]" }));
        assert_eq!(app.account.status, Status::SignedOut);
        assert!(app.modal.is_none());
    }

    #[test]
    fn phone_approval_signs_in_at_once_and_is_not_repeated() {
        let mut app = app();
        open(&mut app);
        select(&mut app, "account:qr");
        let generation = app.account.generation;
        let (sender, mut receive) = mpsc::unbounded_channel();
        app.account.driver = Some(sender);
        event(&mut app, generation, json!({ "type": "confirm", "email": "[email protected]" }));
        assert_eq!(receive.try_recv().unwrap(), Driver::Answer(true), "the phone's approval is the answer; nothing more is asked here");
        assert_eq!(app.account.phase, Phase::Completing);
        key(&mut app, KeyCode::Enter);
        key(&mut app, KeyCode::Esc);
        tick(&mut app);
        assert_eq!(app.account.phase, Phase::Completing);
        assert!(receive.try_recv().is_err(), "no duplicate approval or cancellation while committing");
        event(&mut app, generation, json!({ "type": "result", "status": "success", "email": "[email protected]" }));
        assert!(matches!(app.account.status, Status::SignedIn { .. }));
        finished(&mut app, generation, true, None);
        assert!(app.account.driver.is_none());
        assert!(app.modal.is_none(), "completion must not reopen a dismissed panel");
    }

    #[test]
    fn phone_code_is_complete_or_omitted_and_expiry_keeps_local_work_available() {
        let mut app = app();
        open(&mut app);
        select(&mut app, "account:qr");
        let generation = app.account.generation;
        let url = "https://harness.autonomous.ai/sign-in?code=hnq_1234567890abcdefghijklmnopqrstuvwxyz";
        event(&mut app, generation, json!({ "type": "qr", "url": url, "expiresIn": 120 }));
        let (_, buf) = render(&mut app, 160, 44);
        let at = app.account.qr_drawn.get().expect("wide panel fits a complete QR");
        let scale = 6;
        let mut image = rqrr::PreparedImage::prepare_from_greyscale(at.width as usize * scale, at.height as usize * 2 * scale, |x, y| {
            let cell = buf[(at.x + (x / scale) as u16, at.y + (y / scale / 2) as u16)].symbol();
            let dark = cell == "█" || if (y / scale) % 2 == 0 { cell == "▀" } else { cell == "▄" };
            if dark { 0 } else { 255 }
        });
        let grids = image.detect_grids();
        assert_eq!(grids.len(), 1);
        assert_eq!(grids[0].decode().unwrap().1, url);
        let (text, _) = render(&mut app, 80, 24);
        assert!(app.account.qr_drawn.get().is_none());
        assert!(text.contains("copy the phone sign-in link"), "{text}");
        for (w, h) in [(45, 14), (24, 8), (1, 1)] { render(&mut app, w, h); }
        app.account.phase = Phase::Phone { url: url.into(), expires: Instant::now() - Duration::from_secs(1) };
        tick(&mut app);
        assert!(matches!(app.account.phase, Phase::Failed(_)));
        assert!(app.account.driver.is_none());
        assert_eq!(app.account.status, Status::SignedOut);
    }

    async fn apply_next(app: &mut App, receive: &mut mpsc::UnboundedReceiver<crate::event::Event>) {
        let event = tokio::time::timeout(Duration::from_secs(5), receive.recv()).await.unwrap().unwrap();
        if let crate::event::Event::Apply(f) = event { f(app); } else { panic!("unexpected event") }
    }

    #[tokio::test]
    async fn cli_driver_keeps_stdin_open_then_answers_the_phone_approval_itself() {
        let mut app = app();
        open(&mut app);
        let (sink, mut events) = mpsc::unbounded_channel();
        let (sender, receive) = mpsc::unbounded_channel();
        app.account.driver = Some(sender.clone());
        let mut command = tokio::process::Command::new(crate::sys::sh());
        command.args(["-c", r#"printf '%s\n' '{"type":"confirm","email":"[email protected]"}'; IFS= read -r answer; [ "$answer" = yes ] || exit 1; printf '%s\n' '{"type":"result","status":"success","email":"[email protected]"}'"#]);
        let task = tokio::spawn(drive(command, app.account.generation, sink, receive));
        apply_next(&mut app, &mut events).await;
        assert_eq!(app.account.phase, Phase::Completing, "no step is shown for an account the phone already approved");
        apply_next(&mut app, &mut events).await;
        assert_eq!(app.account.phase, Phase::Committed);
        apply_next(&mut app, &mut events).await;
        task.await.unwrap();
        assert_eq!(app.account.phase, Phase::Ready);
        assert!(matches!(app.account.status, Status::SignedIn { .. }));
    }

    #[tokio::test]
    async fn cancelling_the_driver_closes_stdin_so_cli_can_finish_its_own_cleanup() {
        let mut app = app();
        open(&mut app);
        let (sink, mut events) = mpsc::unbounded_channel();
        let (sender, receive) = mpsc::unbounded_channel();
        app.account.driver = Some(sender);
        let mut command = tokio::process::Command::new(crate::sys::sh());
        command.args(["-c", r#"printf '%s\n' '{"type":"waiting","message":"waiting for driver"}'; IFS= read -r answer; printf '%s\n' '{"type":"result","status":"error","message":"cleanup finished"}'"#]);
        let task = tokio::spawn(drive(command, app.account.generation, sink, receive));
        apply_next(&mut app, &mut events).await;
        app.account.driver.as_ref().unwrap().send(Driver::Cancel).unwrap();
        apply_next(&mut app, &mut events).await;
        assert_eq!(app.account.phase, Phase::Failed("cleanup finished".into()));
        apply_next(&mut app, &mut events).await;
        tokio::time::timeout(Duration::from_secs(5), task).await.unwrap().unwrap();
        assert!(app.account.driver.is_none());
    }

    #[tokio::test]
    async fn a_failed_daemon_restart_keeps_the_account_and_explains_recovery() {
        let mut app = app();
        open(&mut app);
        let (sink, mut events) = mpsc::unbounded_channel();
        let (sender, receive) = mpsc::unbounded_channel();
        app.account.driver = Some(sender);
        let mut command = tokio::process::Command::new(crate::sys::sh());
        command.args(["-c", r#"printf '%s\n' '{"type":"result","status":"success","email":"[email protected]"}'; exit 1"#]);
        let task = tokio::spawn(drive(command, app.account.generation, sink, receive));
        apply_next(&mut app, &mut events).await;
        apply_next(&mut app, &mut events).await;
        task.await.unwrap();
        assert!(matches!(app.account.status, Status::SignedIn { .. }));
        let (text, _) = render(&mut app, 80, 24);
        assert!(text.contains("could not reconnect") && text.contains("harness start"), "{text}");
        assert!(!text.contains("Continue with Google"));
        assert!(app.account.driver.is_none());
    }
}