| 1 | use std::time::Duration; |
| 2 | |
| 3 | use agents::{OutputFormat, OutputParser, RunEvent, parse_line}; |
| 4 | |
| 5 | fn parse_fixture(format: OutputFormat, name: &str) -> Vec<RunEvent> { |
| 6 | let path = format!("{}/tests/fixtures/{name}", env!("CARGO_MANIFEST_DIR")); |
| 7 | let content = std::fs::read_to_string(path).unwrap(); |
| 8 | let mut parser = OutputParser::new(format); |
| 9 | let mut events: Vec<RunEvent> = content.lines().flat_map(|line| parser.feed(line)).collect(); |
| 10 | events.extend(parser.finish()); |
| 11 | events |
| 12 | } |
| 13 | |
| 14 | #[test] |
| 15 | fn claude_fixture() { |
| 16 | let events = parse_fixture(OutputFormat::ClaudeStreamJson, "claude-stream.jsonl"); |
| 17 | assert!(events.contains(&RunEvent::Step("Reading hostname".to_string()))); |
| 18 | assert!(events.contains(&RunEvent::SessionId( |
| 19 | "3f6a2c1e-6f53-4c8e-9a51-7c2b5d0e9a11".to_string() |
| 20 | ))); |
| 21 | assert_eq!( |
| 22 | events.last(), |
| 23 | Some(&RunEvent::Final( |
| 24 | "The first word in `/etc/hostname` is `HOST01`.".to_string() |
| 25 | )) |
| 26 | ); |
| 27 | assert!(events.contains(&RunEvent::Text( |
| 28 | "The first word in `/etc/hostname` is `HOST01`.".to_string() |
| 29 | ))); |
| 30 | } |
| 31 | |
| 32 | #[test] |
| 33 | fn copilot_fixture() { |
| 34 | let events = parse_fixture(OutputFormat::CopilotJsonl, "copilot.jsonl"); |
| 35 | assert!(events.iter().any( |
| 36 | |event| matches!(event, RunEvent::Step(step) if step.contains("Viewing file") && step.contains("/etc/hostname")) |
| 37 | )); |
| 38 | assert!(events.contains(&RunEvent::SessionId( |
| 39 | "7d1e9b2a-3c4f-4a5b-8e6d-1f2a3b4c5d6e".to_string() |
| 40 | ))); |
| 41 | assert!( |
| 42 | matches!(events.last(), Some(RunEvent::Final(text)) if text.starts_with("I couldn't read")) |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | #[test] |
| 47 | fn copilot_failure_without_text() { |
| 48 | let events = parse_line( |
| 49 | OutputFormat::CopilotJsonl, |
| 50 | r#"{"type":"result","sessionId":"s","exitCode":1}"#, |
| 51 | ); |
| 52 | assert!(matches!(events.last(), Some(RunEvent::Failed(_)))); |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn claude_tool_labels_and_failures() { |
| 57 | let label = |name: &str, input: &str| { |
| 58 | let line = format!( |
| 59 | r#"{{"type":"assistant","message":{{"content":[{{"type":"tool_use","name":"{name}","input":{input}}}]}}}}"# |
| 60 | ); |
| 61 | parse_line(OutputFormat::ClaudeStreamJson, &line) |
| 62 | }; |
| 63 | assert_eq!( |
| 64 | label("WebFetch", r#"{"url":"https://example.com/a/b"}"#), |
| 65 | vec![RunEvent::Step("Reading example.com".to_string())] |
| 66 | ); |
| 67 | assert_eq!( |
| 68 | label("Grep", r#"{"pattern":"foo"}"#), |
| 69 | vec![RunEvent::Step("Searching \"foo\"".to_string())] |
| 70 | ); |
| 71 | assert_eq!( |
| 72 | label("Bash", r#"{"command":"ls"}"#), |
| 73 | vec![RunEvent::Step("Running ls".to_string())] |
| 74 | ); |
| 75 | assert_eq!( |
| 76 | label("Mystery", "{}"), |
| 77 | vec![RunEvent::Step("Mystery".to_string())] |
| 78 | ); |
| 79 | assert_eq!( |
| 80 | parse_line( |
| 81 | OutputFormat::ClaudeStreamJson, |
| 82 | r#"{"type":"result","subtype":"error_max_turns","is_error":true}"# |
| 83 | ), |
| 84 | vec![RunEvent::Failed("error_max_turns".to_string())] |
| 85 | ); |
| 86 | assert!(parse_line(OutputFormat::ClaudeStreamJson, "not json").is_empty()); |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn text_format_accumulates() { |
| 91 | let mut parser = OutputParser::new(OutputFormat::Text); |
| 92 | assert_eq!( |
| 93 | parser.feed("hello"), |
| 94 | vec![RunEvent::Step("hello".to_string())] |
| 95 | ); |
| 96 | assert!(parser.feed("").is_empty()); |
| 97 | parser.feed("world"); |
| 98 | assert_eq!( |
| 99 | parser.finish(), |
| 100 | vec![RunEvent::Final("hello\n\nworld".to_string())] |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | #[test] |
| 105 | fn claude_api_error_becomes_an_api_retry() { |
| 106 | let line = r#"{"type":"system","subtype":"api_error","error":{"message":"Connection error.","formatted":"No response from API","connection":{"code":"StreamNoResponse"},"noResponse":{"waitedMs":190000,"retryWaitMs":599000}},"session_id":"abc"}"#; |
| 107 | let events = parse_line(OutputFormat::ClaudeStreamJson, line); |
| 108 | assert_eq!( |
| 109 | events, |
| 110 | vec![RunEvent::ApiRetry { |
| 111 | message: "No response from API".to_string(), |
| 112 | retry_in: Some(Duration::from_millis(599_000)), |
| 113 | }] |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn claude_api_error_without_a_delay_has_no_retry_time() { |
| 119 | let line = r#"{"type":"system","subtype":"api_error","error":"Connection error."}"#; |
| 120 | let events = parse_line(OutputFormat::ClaudeStreamJson, line); |
| 121 | assert_eq!( |
| 122 | events, |
| 123 | vec![RunEvent::ApiRetry { |
| 124 | message: "Connection error.".to_string(), |
| 125 | retry_in: None, |
| 126 | }] |
| 127 | ); |
| 128 | } |