use super::*; use serde_json::json; /// 2026-09-21T12:04:30Z — floored to 12:04 by `session_words`. const NOW: i64 = 1_789_992_270_000; fn ms(iso: &str) -> i64 { parse_date_ms(iso).expect("a literal instant") } fn row(value: Value) -> Map { value.as_object().cloned().expect("an object") } /// RULE: A session's card cuts it into needs-you, running, quiet, from-the-terminal or ended by the app's own rule, dropping a withdrawn row nobody here holds and anything quieter than the window, so every list agrees on where a session sits. #[test] fn a_card_cuts_a_session_into_its_section() { let now = ms("2026-09-21T12:04:00.000Z"); let window = Some(SESSIONS_WINDOW_MS); let section = |session: Value| card(&row(session), window, now).map(|card| card.section); let ours = |state: &str| json!({ "id": "s", "hostOwned": true, "state": state, "lastActiveAt": "2026-09-21T11:30:00.000Z" }); assert_eq!(section(ours("waiting")), Some(Section::NeedsYou)); assert_eq!(section(ours("running")), Some(Section::Running)); assert_eq!(section(ours("queued")), Some(Section::Running)); assert_eq!(section(ours("idle")), Some(Section::Running), "idle within the hour is running"); assert_eq!(section(json!({ "id": "s", "hostOwned": true, "state": "idle", "lastActiveAt": "2026-09-21T09:00:00.000Z" })), Some(Section::Quiet)); assert_eq!(section(ours("ended")), Some(Section::Done)); assert_eq!(section(json!({ "id": "s", "state": "running", "lastActiveAt": "2026-09-21T11:30:00.000Z" })), Some(Section::FromTerminal)); assert_eq!(section(json!({ "id": "s", "state": "withdrawn" })), None, "a withdrawn row nobody here holds is never drawn"); assert_eq!(section(json!({ "id": "s", "state": "ended", "hostOwned": true, "endedAt": "2026-09-01T09:00:00.000Z" })), None); assert_eq!(section(json!({ "id": "s", "state": "ended", "hostOwned": true })), None, "nothing datable is nothing to cut by"); // The history card draws both rows the queue drops, as finished. assert_eq!(history_card(&row(json!({ "id": "s", "state": "withdrawn" })), now).map(|card| card.section), Some(Section::Done)); assert_eq!( history_card(&row(json!({ "id": "s", "state": "ended", "hostOwned": true, "exitCode": 0, "endedAt": "2026-01-01T09:00:00.000Z" })), now).map(|card| card.section), Some(Section::Done) ); } /// RULE: A dispatch hold reason and an engine failure preserve the supplied explanation, so session rendering never substitutes its own diagnosis. #[test] fn session_state_preserves_supplied_reasons() { let now = ms("2026-09-21T12:04:00.000Z"); let reason = "synthetic dispatch explanation"; let state = card(&row(json!({"id":"s", "hostOwned":true, "state":"queued", "holdReason":reason})), Some(SESSIONS_WINDOW_MS), now).unwrap().state; assert_eq!(state, reason); assert_eq!(state_line(&row(json!({"state":"failed", "error":reason}))), reason); } /// RULE: Duration rendering floors within its unit and changes across unit boundaries, so elapsed time cannot inflate a session's age. #[test] fn elapsed_time_floors_without_pinning_unit_words() { assert_eq!(span(12 * 60), span(12 * 60 + 59)); assert_eq!(span(2 * 3600), span(2 * 3600 + 59)); assert_eq!(span(3 * 86_400), span(3 * 86_400 + 86_399)); assert_ne!(span(59), span(60)); assert_ne!(span(3599), span(3600)); assert_ne!(span(86_399), span(86_400)); } /// RULE: A session list derives state and task totals from its row without loading page-only questions or tasks, so list reads stay bounded. #[test] fn list_session_facts_come_from_the_row_without_page_data() { let session = row(json!({ "id": "s-1", "tool": "claude-code", "model": "opus", "account": "owner@example.com", "machine": "mac-b", "repoUrl": "git.example.com/team/field-notes.git", "branch": "main", "state": "running", "hostOwned": true, "startedAt": "2026-09-21T10:00:00.000Z", "lastActiveAt": "2026-09-21T12:00:00.000Z", "tasksDone": 4, "tasksTotal": 6, "project": "field-notes" })); let words = session_words(&session, None, Cut::Window(SESSIONS_WINDOW_MS), NOW); assert_eq!(words["section"], "running"); assert_eq!(words["project_slug"], "field-notes"); assert_eq!(counts(&words["tasks_count"]), vec![4, 6]); assert_eq!(words["active_at"], "2026-09-21T12:00:00.000Z"); assert_eq!(words["can_resume"], false); assert!(words.get("notice").is_none() && words.get("pending_decision").is_none() && words.get("tasks").is_none(), "{words:?}"); let conversation = row(json!({ "id": "c", "kind": "conversation", "projectName": "Field notes", "state": "idle", "lastLine": "Done." })); let words = session_words(&conversation, None, Cut::Window(SESSIONS_WINDOW_MS), NOW); assert_eq!(words["line"], "Done."); } /// RULE: A session page sorts nonblank tasks by the agent's own sequence and counts the done ones. #[test] fn session_pages_sort_their_tasks() { let session = row(json!({ "id": "s-1", "state": "waiting", "waitingKind": "permission", "waitingRef": "p-2", "hostOwned": true })); let page = PageFacts { tasks: vec![ json!({ "seq": 1, "text": "Write the test", "state": "done" }), json!({ "seq": 0, "text": "Read the code", "state": "active" }), json!({ "seq": 2, "text": " ", "state": "open" }), json!({ "seq": 3, "text": "Ship", "state": "later" }), ], }; let words = session_words(&session, Some(&page), Cut::Window(SESSIONS_WINDOW_MS), NOW); assert!(words.get("pending_decision").is_none() && words.get("answering").is_none() && words.get("open_questions").is_none(), "the session inbox is gone: {words:?}"); let tasks = words["tasks"].as_str().unwrap(); let read = tasks.find("Read the code").unwrap(); let write = tasks.find("Write the test").unwrap(); let ship = tasks.find("Ship").unwrap(); assert!(read < write && write < ship, "task sequence wins over input order"); assert_eq!(counts(&words["tasks_count"]), vec![1, 3]); } /// RULE: A project page draws a session as running exactly where its card says needs-you or running for a session Cotton holds that was active inside the window, because the page and the list read one rule. #[test] fn drawn_as_running_is_the_cards_section() { let now = ms("2026-09-21T12:04:00.000Z"); let running = |session: Value| drawn_as_running(&row(session), now); assert!(running(json!({ "id": "s", "hostOwned": true, "state": "waiting", "lastActiveAt": "2026-09-21T11:00:00.000Z" }))); assert!(running(json!({ "id": "s", "hostOwned": true, "state": "idle", "lastActiveAt": "2026-09-21T11:30:00.000Z" }))); assert!(!running(json!({ "id": "s", "hostOwned": true, "state": "idle", "lastActiveAt": "2026-09-21T09:00:00.000Z" }))); assert!(!running(json!({ "id": "s", "state": "running", "lastActiveAt": "2026-09-21T11:30:00.000Z" })), "a terminal's session is never running here"); assert!(!running(json!({ "id": "s", "hostOwned": true, "state": "running", "startedAt": "2026-09-21T11:30:00.000Z" })), "no last activity, not running"); assert!(!running(json!({ "id": "s", "hostOwned": true, "state": "waiting", "lastActiveAt": "2026-09-01T11:00:00.000Z" }))); } /// RULE: Session facts normalize repository paths, redact account domains and preserve supplied links, so rendering loses no source identity while hiding the full account. #[test] fn session_facts_normalize_source_values_and_redact_accounts() { assert_eq!(repo_name(Some("git.example.com/team/project.git")).as_deref(), Some("project")); assert_eq!(repo_name(Some("git.example.com/team/project/")).as_deref(), Some("project")); assert_eq!(repo_name(Some("")), None); let account = short_account("owner@example.com"); assert!(account.contains("owner") && !account.contains("example.com")); assert_eq!(short_account("@example.com"), "@example.com"); assert_eq!(agent_name(&row(json!({ "tool": "kimi" }))).as_deref(), Some("kimi")); assert_eq!(shipped_lines(&json!("[\"https://a.example\",\"https://b.example\"]")).as_deref(), Some("https://a.example\nhttps://b.example")); assert_eq!(shipped_lines(&json!([])), None); assert_eq!(minute_floor(NOW), ms("2026-09-21T12:04:00.000Z")); } fn counts(value: &Value) -> Vec { regex::Regex::new(r"[0-9]+").unwrap().find_iter(value.as_str().unwrap()).map(|number| number.as_str().parse().unwrap()).collect() } /// RULE: Code list lines are plain previews and their facts never use a raw model identifier or a fixture's model marker as a label. #[test] fn session_list_lines_are_plain_and_omit_raw_model_metadata() { let session = row(json!({"tool":"codex","hostOwned":true,"state":"running","model":"synthetic","lastLine":"May I run `npm install`?"})); let words = session_words(&session, None, Cut::Window(SESSIONS_WINDOW_MS), NOW); assert_eq!(words["line"], "May I run npm install?"); assert!(!words["facts"].as_str().unwrap().contains("synthetic")); }