149 lines · 4.6 KB · RustRaw
//! What the task reader makes of every shape a `TodoWrite` hook can hand it.use super::*;use serde_json::json;/// A `PostToolUse` payload, the shape Claude Code writes it.fn hook(todos: serde_json::Value) -> Value { json!({ "session_id": "s-1", "transcript_path": "/tmp/t.jsonl", "cwd": "/work", "hook_event_name": "PostToolUse", "tool_name": "TodoWrite", "tool_input": { "todos": todos }, "tool_use_id": "toolu_1", "tool_result": { "type": "text", "text": "Todos have been modified successfully" } })}#[test]fn every_status_the_agent_writes_becomes_cottons_own_word() { let tasks = tasks_from_hook( "s-1", &hook(json!([ {"content": "Read the brief", "status": "completed", "activeForm": "Reading the brief"}, {"content": "Write the hook", "status": "in_progress", "activeForm": "Writing the hook"}, {"content": "Open the PR", "status": "pending", "activeForm": "Opening the PR"} ])), ) .expect("a TodoWrite payload carries a list"); assert_eq!( tasks, vec![ Task { seq: 0, text: "Read the brief".into(), status: TaskStatus::Done }, Task { seq: 1, text: "Write the hook".into(), status: TaskStatus::Active }, Task { seq: 2, text: "Open the PR".into(), status: TaskStatus::Open }, ] );}#[test]fn an_empty_list_is_a_reading_that_clears_the_row_never_no_reading_at_all() { assert_eq!(tasks_from_hook("s-1", &hook(json!([]))), Some(Vec::new()));}#[test]fn a_status_this_host_does_not_know_skips_that_task_and_keeps_the_rest() { let tasks = tasks_from_hook( "s-1", &hook(json!([ {"content": "first", "status": "pending", "activeForm": "firsting"}, {"content": "second", "status": "cancelled", "activeForm": "seconding"}, {"content": "third", "status": "completed", "activeForm": "thirding"} ])), ) .expect("the call is still a task list"); // The skipped task leaves its index behind: seq is the agent's own // position, so `third` is 2 and not 1. assert_eq!( tasks.iter().map(|task| task.seq).collect::<Vec<_>>(), vec![0, 2] ); assert_eq!(tasks[1].text, "third");}#[test]fn a_task_with_no_words_is_skipped_rather_than_mirrored_as_a_blank_line() { let tasks = tasks_from_hook( "s-1", &hook(json!([ {"content": " ", "status": "pending"}, {"content": "real", "status": "pending"} ])), ) .expect("the call is still a task list"); assert_eq!(tasks.len(), 1); assert_eq!(tasks[0].text, "real"); assert_eq!(tasks[0].seq, 1);}#[test]fn a_field_this_host_has_never_heard_of_changes_nothing() { let tasks = tasks_from_hook( "s-1", &hook(json!([ {"content": "onward", "status": "pending", "activeForm": "onwarding", "id": "t-9", "priority": "high"} ])), ) .expect("an extra field is not a reason to refuse the list"); assert_eq!(tasks.len(), 1); assert_eq!(tasks[0].text, "onward");}#[test]fn content_is_trimmed_because_the_phone_renders_it_as_written() { let tasks = tasks_from_hook( "s-1", &hook(json!([{"content": " spaced ", "status": "pending"}])), ) .expect("the call is still a task list"); assert_eq!(tasks[0].text, "spaced");}#[test]fn a_payload_for_another_tool_is_not_a_task_list() { let mut payload = hook(json!([{"content": "x", "status": "pending"}])); payload["tool_name"] = json!("Bash"); assert_eq!(tasks_from_hook("s-1", &payload), None);}#[test]fn a_payload_with_no_todos_at_all_is_not_a_task_list_rather_than_an_empty_one() { for payload in [ json!({"tool_name": "TodoWrite"}), json!({"tool_name": "TodoWrite", "tool_input": {}}), json!({"tool_name": "TodoWrite", "tool_input": {"todos": "not an array"}}), json!({}), ] { assert_eq!( tasks_from_hook("s-1", &payload), None, "a payload with no list must not clear the row: {payload}" ); }}#[test]fn a_task_that_is_not_an_object_is_skipped_never_a_panic() { let tasks = tasks_from_hook( "s-1", &hook(json!(["just a string", 7, null, {"content": "kept", "status": "pending"}])), ) .expect("the call is still a task list"); assert_eq!(tasks.len(), 1); assert_eq!(tasks[0].seq, 3);}