| 1 | use super::*; |
| 2 | use clap::Parser; |
| 3 | use codewhale_telemetry::{SessionSource, Surface}; |
| 4 | |
| 5 | fn command_of(args: &[&str]) -> Option<Commands> { |
| 6 | Cli::try_parse_from(args) |
| 7 | .expect("CLI args should parse") |
| 8 | .command |
| 9 | } |
| 10 | |
| 11 | #[test] |
| 12 | fn every_surface_is_named_by_the_subcommand_not_the_executable() { |
| 13 | assert_eq!(telemetry_surface(None), Surface::Tui); |
| 14 | assert_eq!( |
| 15 | telemetry_surface(command_of(&["codewhale-tui", "resume", "--last"]).as_ref()), |
| 16 | Surface::Tui |
| 17 | ); |
| 18 | assert_eq!( |
| 19 | telemetry_surface(command_of(&["codewhale-tui", "fork", "--last"]).as_ref()), |
| 20 | Surface::Tui |
| 21 | ); |
| 22 | assert_eq!( |
| 23 | telemetry_surface(command_of(&["codewhale-tui", "exec", "hello"]).as_ref()), |
| 24 | Surface::Exec |
| 25 | ); |
| 26 | assert_eq!( |
| 27 | telemetry_surface(command_of(&["codewhale-tui", "serve", "--http"]).as_ref()), |
| 28 | Surface::Serve |
| 29 | ); |
| 30 | assert_eq!( |
| 31 | telemetry_surface(command_of(&["codewhale-tui", "serve", "--mcp"]).as_ref()), |
| 32 | Surface::McpServer |
| 33 | ); |
| 34 | assert_eq!( |
| 35 | telemetry_surface(command_of(&["codewhale-tui", "doctor"]).as_ref()), |
| 36 | Surface::Cli |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn an_embedder_may_name_the_server_it_started() { |
| 42 | // The one surface a third party legitimately starts, and the one they may |
| 43 | // therefore declare. The editor extension's case. |
| 44 | assert_eq!( |
| 45 | embedded_surface_override_from(Some("vscode-extension".to_string())), |
| 46 | Some(Surface::VscodeExtension) |
| 47 | ); |
| 48 | assert_eq!( |
| 49 | embedded_surface_override_from(Some("desktop".to_string())), |
| 50 | Some(Surface::Desktop) |
| 51 | ); |
| 52 | // Surrounding whitespace is a shell artifact, not a different name. |
| 53 | assert_eq!( |
| 54 | embedded_surface_override_from(Some(" vscode-extension\n".to_string())), |
| 55 | Some(Surface::VscodeExtension) |
| 56 | ); |
| 57 | assert_eq!( |
| 58 | embedded_surface_override_from(None), |
| 59 | None, |
| 60 | "an unset variable declares nothing" |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | #[test] |
| 65 | fn the_interactive_surface_can_never_be_declared_by_an_embedder() { |
| 66 | // Nothing starts the terminal UI on a user's behalf: a server reporting |
| 67 | // itself as `tui` would land in the one surface whose numbers are the |
| 68 | // terminal's. |
| 69 | assert_eq!( |
| 70 | embedded_surface_override_from(Some("tui".to_string())), |
| 71 | None |
| 72 | ); |
| 73 | assert_eq!( |
| 74 | embedded_surface_override_from(Some(" tui ".to_string())), |
| 75 | None |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | #[test] |
| 80 | fn an_unknown_surface_name_is_dropped_rather_than_guessed_at() { |
| 81 | // Not an error and not a repair: a name outside the closed set falls back |
| 82 | // to the caller's default. `vscode` and `ide` are the likely typos and are |
| 83 | // deliberately not aliases of anything. |
| 84 | for name in [ |
| 85 | "", |
| 86 | " ", |
| 87 | "vscode", |
| 88 | "ide", |
| 89 | "TUI", |
| 90 | "VSCODE-EXTENSION", |
| 91 | "unknown", |
| 92 | ] { |
| 93 | assert_eq!( |
| 94 | embedded_surface_override_from(Some(name.to_string())), |
| 95 | None, |
| 96 | "{name:?} must not resolve to a surface" |
| 97 | ); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | #[test] |
| 102 | fn the_declared_surface_is_consulted_for_the_server_and_nowhere_else() { |
| 103 | // The variable is inherited by descendants, and an agent's shell command |
| 104 | // runs codewhale descendants with this environment. Only the branch that |
| 105 | // *is* an embedder's server may consult it, so a nested `exec` cannot |
| 106 | // report itself as the embedder. |
| 107 | let declared = Some(Surface::VscodeExtension); |
| 108 | |
| 109 | assert_eq!(telemetry_surface_with(None, declared), Surface::Tui); |
| 110 | assert_eq!( |
| 111 | telemetry_surface_with( |
| 112 | command_of(&["codewhale-tui", "exec", "hello"]).as_ref(), |
| 113 | declared |
| 114 | ), |
| 115 | Surface::Exec |
| 116 | ); |
| 117 | assert_eq!( |
| 118 | telemetry_surface_with(command_of(&["codewhale-tui", "doctor"]).as_ref(), declared), |
| 119 | Surface::Cli |
| 120 | ); |
| 121 | // An MCP server still says so; it is decided before the declaration. |
| 122 | assert_eq!( |
| 123 | telemetry_surface_with( |
| 124 | command_of(&["codewhale-tui", "serve", "--mcp"]).as_ref(), |
| 125 | declared |
| 126 | ), |
| 127 | Surface::McpServer |
| 128 | ); |
| 129 | // Only the plain server picks the declared name up… |
| 130 | assert_eq!( |
| 131 | telemetry_surface_with( |
| 132 | command_of(&["codewhale-tui", "serve", "--http"]).as_ref(), |
| 133 | declared |
| 134 | ), |
| 135 | Surface::VscodeExtension |
| 136 | ); |
| 137 | // …and with nothing declared it is an anonymous server, exactly as before. |
| 138 | assert_eq!( |
| 139 | telemetry_surface_with( |
| 140 | command_of(&["codewhale-tui", "serve", "--http"]).as_ref(), |
| 141 | None |
| 142 | ), |
| 143 | Surface::Serve |
| 144 | ); |
| 145 | } |
| 146 | |
| 147 | #[test] |
| 148 | fn read_only_commands_never_arm_usage_counting() { |
| 149 | for args in [ |
| 150 | vec!["codewhale-tui", "doctor"], |
| 151 | vec!["codewhale-tui", "doctor", "--json"], |
| 152 | vec!["codewhale-tui", "sessions"], |
| 153 | vec!["codewhale-tui", "setup", "--status"], |
| 154 | ] { |
| 155 | let command = command_of(&args); |
| 156 | assert!( |
| 157 | telemetry_command_is_read_only(command.as_ref()), |
| 158 | "{args:?} must remain state-free" |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | for args in [ |
| 163 | vec!["codewhale-tui", "exec", "hello"], |
| 164 | vec!["codewhale-tui", "setup", "--skills"], |
| 165 | ] { |
| 166 | let command = command_of(&args); |
| 167 | assert!( |
| 168 | !telemetry_command_is_read_only(command.as_ref()), |
| 169 | "{args:?} is not a read-only command" |
| 170 | ); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | #[test] |
| 175 | fn windows_resume_session_listing_cannot_consume_the_process_telemetry_arm() { |
| 176 | // Bare `codewhale resume` on Windows invokes `sessions` in-process before |
| 177 | // starting the selected resumed TUI. The listing must remain state-free so |
| 178 | // that the process-global telemetry initializer is still available to the |
| 179 | // resumed TUI after its native privacy decision. |
| 180 | let command = command_of(&["codewhale-tui", "sessions"]); |
| 181 | assert_eq!(telemetry_surface(command.as_ref()), Surface::Cli); |
| 182 | assert!(telemetry_command_is_read_only(command.as_ref())); |
| 183 | } |
| 184 | |
| 185 | #[test] |
| 186 | fn unreadable_existing_setup_state_suppresses_telemetry_instead_of_defaulting_on() { |
| 187 | let dir = tempfile::tempdir().expect("tempdir"); |
| 188 | let state_path = dir.path().join("setup_state.json"); |
| 189 | std::fs::write(&state_path, "not-json").expect("seed corrupt state"); |
| 190 | |
| 191 | assert!(codewhale_telemetry::load_setup_state_for_decision_at(&state_path).is_none()); |
| 192 | assert_eq!( |
| 193 | std::fs::read_to_string(state_path).expect("corrupt state remains untouched"), |
| 194 | "not-json" |
| 195 | ); |
| 196 | } |
| 197 | |
| 198 | #[test] |
| 199 | fn the_session_source_distinguishes_resume_and_fork_from_a_fresh_launch() { |
| 200 | assert_eq!(telemetry_session_source(None), SessionSource::Interactive); |
| 201 | assert_eq!( |
| 202 | telemetry_session_source(command_of(&["codewhale-tui", "resume", "--last"]).as_ref()), |
| 203 | SessionSource::Resume |
| 204 | ); |
| 205 | assert_eq!( |
| 206 | telemetry_session_source(command_of(&["codewhale-tui", "fork", "--last"]).as_ref()), |
| 207 | SessionSource::Fork |
| 208 | ); |
| 209 | assert_eq!( |
| 210 | telemetry_session_source(command_of(&["codewhale-tui", "serve", "--http"]).as_ref()), |
| 211 | SessionSource::Api |
| 212 | ); |
| 213 | assert_eq!( |
| 214 | telemetry_session_source(command_of(&["codewhale-tui", "doctor"]).as_ref()), |
| 215 | SessionSource::Unknown |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | #[test] |
| 220 | fn a_session_end_built_without_arming_writes_nothing() { |
| 221 | let event = codewhale_telemetry::session_end_event(); |
| 222 | assert!(matches!( |
| 223 | event, |
| 224 | codewhale_telemetry::Event::SessionEnd { .. } |
| 225 | )); |
| 226 | codewhale_telemetry::record_blocking(event); |
| 227 | assert!(!codewhale_telemetry::is_armed()); |
| 228 | } |
| 229 | |
| 230 | #[test] |
| 231 | fn canceled_run_reports_exit_class_error_not_signal() { |
| 232 | use codewhale_engine::core::termination::RunTerminationReason; |
| 233 | assert_eq!( |
| 234 | codewhale_telemetry::ExitClass::Signal.as_str(), |
| 235 | "signal", |
| 236 | "the SIGINT path's class is a distinct value, not a synonym for error" |
| 237 | ); |
| 238 | assert!(!RunTerminationReason::Canceled.is_success()); |
| 239 | assert!(RunTerminationReason::Resolved.is_success()); |
| 240 | assert!(!codewhale_telemetry::is_armed()); |
| 241 | codewhale_telemetry::set_exit_class(codewhale_telemetry::ExitClass::Error); |
| 242 | assert_eq!( |
| 243 | codewhale_telemetry::exit_class(), |
| 244 | codewhale_telemetry::ExitClass::Clean |
| 245 | ); |
| 246 | } |
| 247 |