Cotton code
148 lines · 4.6 KB · RustRaw
//! What `list` still answers after the daemon has been restarted.use super::*;fn entry(id: &str) -> IndexEntry {    IndexEntry {        id: id.to_string(),        tool: Tool::Claude,        launch_id: format!("launch-{id}"),        agent_session_id: None,        cwd: "/work/repo".to_string(),        started_at: "2026-09-18T09:00:00Z".to_string(),        ended_at: None,        exit: None,        adopted: false,    }}/// A session this host FOUND rather than spawned.fn adopted_entry(id: &str) -> IndexEntry {    IndexEntry {        adopted: true,        launch_id: String::new(),        ..entry(id)    }}#[test]fn a_restart_does_not_end_an_adopted_session_because_this_host_never_held_it() {    let mut index = Index::default();    index.upsert(entry("hosted"));    index.upsert(adopted_entry("found"));    let ended = index.end_everything_left_running("2026-09-18T10:00:00Z");    assert_eq!(ended, 1, "only the session this host owned a terminal for");    assert_eq!(index.sessions[0].ended_at.as_deref(), Some("2026-09-18T10:00:00Z"));    assert_eq!(        index.sessions[1].ended_at, None,        "its transcript is still being written; the next scan reads the state back out of it"    );}#[test]fn a_host_that_has_never_run_has_no_sessions_and_that_is_a_fact_not_a_failure() {    let dir = tempfile::tempdir().expect("a scratch directory");    let index = Index::load(&dir.path().join("sessions").join("index.json")).expect("loads");    assert_eq!(index, Index::default());}#[test]fn it_round_trips_through_the_file() {    let dir = tempfile::tempdir().expect("a scratch directory");    let path = dir.path().join("sessions").join("index.json");    let mut index = Index::default();    index.upsert(entry("a"));    index.upsert(entry("b"));    index.save(&path).expect("saves");    assert_eq!(Index::load(&path).expect("loads"), index);}#[test]fn an_upsert_replaces_the_entry_with_the_same_id_rather_than_doubling_it() {    let mut index = Index::default();    index.upsert(entry("a"));    let mut moved = entry("a");    moved.cwd = "/work/other".to_string();    index.upsert(moved);    assert_eq!(index.sessions.len(), 1);    assert_eq!(index.sessions[0].cwd, "/work/other");}#[test]fn ending_a_session_stamps_it_and_ending_one_nobody_started_answers_false() {    let mut index = Index::default();    index.upsert(entry("a"));    let exit = Exit {        code: Some(0),        signal: None,    };    assert!(index.end("a", "2026-09-18T10:00:00Z", exit));    assert_eq!(        index.sessions[0].ended_at.as_deref(),        Some("2026-09-18T10:00:00Z")    );    assert_eq!(index.sessions[0].exit, Some(exit));    assert!(!index.end("nobody", "2026-09-18T10:00:00Z", exit));    assert_eq!(index.sessions.len(), 1, "a no-op invents no row");}#[test]fn a_restart_ends_what_the_previous_daemon_left_running_with_an_honest_unknown_exit() {    let mut index = Index::default();    index.upsert(entry("a"));    index.upsert(entry("b"));    index.end(        "b",        "2026-09-18T09:30:00Z",        Exit {            code: Some(0),            signal: None,        },    );    assert_eq!(index.end_everything_left_running("2026-09-18T11:00:00Z"), 1);    assert_eq!(        index.sessions[0].ended_at.as_deref(),        Some("2026-09-18T11:00:00Z")    );    assert_eq!(        index.sessions[0].exit,        Some(Exit {            code: None,            signal: None        }),        "nobody reaped it, so the exit is unknown — never a fabricated 0"    );    assert_eq!(        index.sessions[1].ended_at.as_deref(),        Some("2026-09-18T09:30:00Z"),        "a session that really ended keeps its own stamp"    );}#[test]fn a_file_that_is_not_a_session_index_is_refused_by_name_never_started_over() {    let dir = tempfile::tempdir().expect("a scratch directory");    let path = dir.path().join("index.json");    std::fs::write(&path, b"{\"sessions\": \"not a list\"}").expect("writes");    let error = Index::load(&path).expect_err("it is refused");    assert!(        matches!(error, IndexError::NotAnIndex { .. }),        "a damaged index is named, not silently replaced with an empty one — got {error}"    );}#[test]fn it_knows_which_agent_session_id_belongs_to_which_host_session() {    let mut index = Index::default();    let mut codex = entry("a");    codex.tool = Tool::Codex;    codex.agent_session_id = Some("019a-codex".to_string());    index.upsert(codex);    index.upsert(entry("b"));    assert_eq!(        index.agent_session_ids(),        BTreeMap::from([("a".to_string(), "019a-codex".to_string())])    );}