Cotton code
125 lines · 4.1 KB · RustRaw
//! What counts as live, and the one wording — with no socket in sight.use super::*;use crate::protocol::{SessionState, Tool};fn summary(id: &str, state: SessionState, host_owned: bool) -> SessionSummary {    SessionSummary {        session_id: id.to_string(),        launch_id: String::new(),        tool: Tool::Claude,        agent_session_id: None,        cwd: "/Users/someone/Code/something".to_string(),        started_at: "2026-09-24T08:40:00Z".to_string(),        ended_at: None,        state,        waiting_kind: None,        request_id: None,        exit_code: None,        exit_signal: None,        host_owned,    }}#[test]fn every_state_but_ended_is_live_while_the_host_owns_it() {    for state in [        SessionState::Starting,        SessionState::Running,        SessionState::Idle,        SessionState::Waiting,    ] {        assert!(is_live(&summary("a", state, true)), "{state:?}");    }    assert!(!is_live(&summary("a", SessionState::Ended, true)));}#[test]fn an_adopted_session_is_never_live_here() {    // It is somebody's own terminal this host merely watches. Ending it is not    // this command's business, and holding an update for it would hold one for    // ever.    for state in [        SessionState::Starting,        SessionState::Running,        SessionState::Idle,        SessionState::Waiting,    ] {        assert!(!is_live(&summary("a", state, false)), "{state:?}");    }}#[test]fn live_keeps_the_order_list_gave_and_drops_the_rest() {    let sessions = vec![        summary("one", SessionState::Running, true),        summary("adopted", SessionState::Running, false),        summary("gone", SessionState::Ended, true),        summary("two", SessionState::Waiting, true),    ];    let live = live(&sessions);    assert_eq!(        live.iter().map(|s| s.session_id.as_str()).collect::<Vec<_>>(),        ["one", "two"]    );}#[test]fn the_sentence_counts_what_is_running() {    let mut report = Report::default();    assert_eq!(sentence(&report), "no sessions are running on this Mac");    report.live = vec![summary("one", SessionState::Running, true)];    assert_eq!(sentence(&report), "1 session(s) running on this Mac");    report        .live        .push(summary("two", SessionState::Waiting, true));    assert_eq!(sentence(&report), "2 session(s) running on this Mac");}#[test]fn a_helper_that_could_not_be_asked_says_why_and_holds_nothing_up() {    let report = Report {        said: Some("/x/session-host.sock — no such file".to_string()),        ..Report::default()    };    assert!(sentence(&report).contains("could not be asked what is running"));    assert!(sentence(&report).contains("no such file"));    // The DECISION is the empty list: a helper nothing is listening for owns    // no pseudo-terminal, so there is no turn an update could cut short.    assert_eq!(report.live.len(), 0);    let json: serde_json::Value = serde_json::from_str(&answer(&report)).unwrap();    assert_eq!(json["live"], 0);    assert_eq!(json["reachable"], false);}#[test]fn the_sentence_says_what_end_ended() {    let report = Report {        live: vec![summary("one", SessionState::Running, true)],        ended: vec!["one".to_string()],        said: None,    };    assert_eq!(sentence(&report), "1 session(s) ended on this Mac");}#[test]fn the_answer_is_one_json_line_both_readers_can_read() {    let report = Report {        live: vec![            summary("one", SessionState::Running, true),            summary("two", SessionState::Waiting, true),        ],        ended: Vec::new(),        said: None,    };    let text = answer(&report);    assert!(!text.contains('\n'), "{text}");    // The install script greps for exactly this spelling.    assert!(text.contains("\"live\":2"), "{text}");    let json: serde_json::Value = serde_json::from_str(&text).unwrap();    assert_eq!(json["reachable"], true);    assert_eq!(json["sessions"][0]["sessionId"], "one");    assert_eq!(json["sessions"][0]["tool"], "claude");    assert_eq!(json["sessions"][1]["state"], "waiting");    assert_eq!(json["line"], "2 session(s) running on this Mac");}