返回 CodeWhale
telemetry_kill_switch_dispatch.rs
根目录 / crates / cli / tests / telemetry_kill_switch_dispatch.rs
1 //! The kill switch has to reach the in-process runtime that would emit.
2 //!
3 //! The single `codewhale` binary resolves dispatcher overrides, states the
4 //! telemetry floor in its environment, and then calls `codewhale_tui::run`.
5 //! That runtime re-resolves telemetry before it can arm. These tests drive the
6 //! real binary through the keyless `features list` command and use the local
7 //! dry-run sink as the end-to-end observable: an enabled positive control must
8 //! write session events, while a kill switch must create no telemetry state.
9
10 #![cfg(unix)]
11
12 use std::fs;
13 use std::process::Command;
14
15 use codewhale_config::{SetupState, TELEMETRY_NOTICE_VERSION};
16 use tempfile::TempDir;
17
18 /// `CODEWHALE_TELEMETRY=0` beats `--telemetry true`, end to end.
19 ///
20 /// The positive control proves the runtime is enabled before the kill switch is
21 /// applied, so the zero-state assertion cannot pass vacuously.
22 #[test]
23 fn env_off_beats_cli_on_end_to_end() {
24 // Positive control first: the flag reaches the in-process runtime and
25 // arms its dry-run sink, so the assertion below is about the floor and not
26 // about a command that never crossed the dispatch boundary.
27 let on = dispatch_and_read_telemetry(None);
28 let dry_run = on
29 .dry_run
30 .expect("current consent plus `--telemetry true` must write the dry-run sink");
31 assert!(
32 dry_run.contains("\"event\":\"session_start\"")
33 && dry_run.contains("\"event\":\"session_end\""),
34 "the real in-process runtime must record a complete session: {dry_run}"
35 );
36
37 let off = dispatch_and_read_telemetry(Some("0"));
38 assert!(
39 !off.telemetry_dir_exists && off.dry_run.is_none(),
40 "`CODEWHALE_TELEMETRY=0` must beat `--telemetry true` before the runtime arms"
41 );
42 }
43
44 /// A real endpoint must remain queued locally instead of becoming short-CLI
45 /// network latency. The next interactive session owns delivery.
46 #[test]
47 fn short_cli_exit_persists_without_network_delivery() {
48 let evidence = dispatch_and_read_telemetry_with_endpoint(None, None, None, false);
49 let pending = evidence
50 .pending
51 .expect("short CLI must seal its pending telemetry before process exit");
52 assert!(
53 pending.contains("\"event\":\"session_start\"")
54 && pending.contains("\"event\":\"session_end\""),
55 "the pending buffer must contain the complete short CLI session: {pending}"
56 );
57 assert!(
58 evidence.dry_run.is_none(),
59 "a configured endpoint must stay pending rather than use the dry-run sink"
60 );
61 }
62
63 /// A value the resolver cannot parse resolves to off, rather than falling
64 /// through to the flag.
65 #[test]
66 fn an_unparseable_telemetry_env_value_keeps_the_in_process_runtime_off() {
67 let evidence = dispatch_and_read_telemetry(Some("maybe"));
68 assert!(
69 !evidence.telemetry_dir_exists && evidence.dry_run.is_none(),
70 "a typo in the kill switch must never arm the in-process runtime"
71 );
72 }
73
74 /// An explicit durable enable uses the Settings transition to clear old no.
75 /// The optional versioned compatibility command still rejects old versions.
76 #[test]
77 fn config_set_true_reenables_a_historical_decline_through_settings() {
78 let fixture = TempDir::new().expect("fixture root");
79 let home = fixture.path().join("home");
80 let codewhale_home = fixture.path().join("codewhale-home");
81 let workspace = fixture.path().join("workspace");
82 for dir in [&home, &codewhale_home, &workspace] {
83 fs::create_dir_all(dir).expect("create fixture dir");
84 }
85
86 let mut state = SetupState::default();
87 state.record_telemetry_notice("1", false);
88 let state_path = codewhale_home.join("setup_state.json");
89 state
90 .save_to(&state_path)
91 .expect("write historical decline");
92
93 let config_path = fixture.path().join("config.toml");
94 fs::write(&config_path, "telemetry_endpoint = \"\"\n").expect("write config");
95
96 let command = || {
97 let mut command = Command::new(codewhale_binary());
98 command
99 .current_dir(&workspace)
100 .env_clear()
101 .env("PATH", std::env::var_os("PATH").expect("PATH"))
102 .env("HOME", &home)
103 .env("USERPROFILE", &home)
104 .env("CODEWHALE_HOME", &codewhale_home)
105 .env("CODEWHALE_SECRET_BACKEND", "file")
106 .arg("--config")
107 .arg(&config_path);
108 command
109 };
110 let before = fs::read(&state_path).unwrap();
111 let declined = command()
112 .env("CODEWHALE_TELEMETRY", "true")
113 .args(["--telemetry", "true", "features", "list"])
114 .output()
115 .expect("run with a historical sidecar-only decline");
116 assert!(declined.status.success());
117 assert!(!codewhale_home.join("telemetry").exists());
118 assert_eq!(
119 fs::read(&state_path).unwrap(),
120 before,
121 "default-on and run-scoped on must preserve the old explicit no"
122 );
123 let output = command()
124 .args(["config", "set", "telemetry", "true"])
125 .output()
126 .expect("run config set");
127 assert!(
128 output.status.success(),
129 "config set failed\nstdout:\n{}\nstderr:\n{}",
130 String::from_utf8_lossy(&output.stdout),
131 String::from_utf8_lossy(&output.stderr)
132 );
133 assert!(
134 fs::read_to_string(&config_path)
135 .expect("read config")
136 .contains("telemetry = true")
137 );
138 let state = SetupState::load_from(&state_path).expect("read setup state");
139 assert!(state.telemetry_accepted(TELEMETRY_NOTICE_VERSION));
140 assert!(!state.telemetry_opted_out());
141
142 let outdated = command()
143 .args(["config", "telemetry", "--accept-notice", "3"])
144 .output()
145 .expect("attempt outdated consent");
146 assert!(!outdated.status.success());
147 assert!(
148 !SetupState::load_from(&state_path)
149 .expect("read enabled state")
150 .telemetry_opted_out()
151 );
152
153 let accepted = command()
154 .args([
155 "config",
156 "telemetry",
157 "--accept-notice",
158 TELEMETRY_NOTICE_VERSION,
159 ])
160 .output()
161 .expect("accept current notice");
162 assert!(
163 accepted.status.success(),
164 "current notice acceptance failed: {}",
165 String::from_utf8_lossy(&accepted.stderr)
166 );
167 let state = SetupState::load_from(&state_path).expect("read accepted state");
168 assert!(state.telemetry_accepted(TELEMETRY_NOTICE_VERSION));
169 assert!(!state.telemetry_opted_out());
170 }
171
172 #[test]
173 fn missing_preference_defaults_on_without_inventing_acceptance() {
174 for version in [None, Some("1"), Some("4")] {
175 let evidence = dispatch_and_read_telemetry_with_endpoint(None, Some(""), version, false);
176 let batch: serde_json::Value = serde_json::from_str(
177 evidence
178 .dry_run
179 .as_ref()
180 .expect("default-on writes dry run")
181 .lines()
182 .next()
183 .unwrap(),
184 )
185 .unwrap();
186 assert_eq!(batch["schema_version"], 4);
187 assert_eq!(batch["notice_version"], 6);
188 assert!(batch.get("consent_version").is_none());
189 let state = evidence.setup.expect("disclosure display marker");
190 assert_eq!(
191 state.telemetry_notice_shown_for.as_deref(),
192 Some(TELEMETRY_NOTICE_VERSION)
193 );
194 assert_eq!(state.telemetry_notice_decided_for.as_deref(), version);
195 assert!(!state.telemetry_accepted(TELEMETRY_NOTICE_VERSION));
196 assert!(evidence.stderr.contains("on by default"));
197 assert!(evidence.stderr.contains("Codewhale processes"));
198 assert!(!evidence.stderr.contains("PostHog"));
199 assert!(
200 evidence
201 .stderr
202 .contains("codewhale config set telemetry false")
203 );
204 }
205 }
206
207 struct DispatchEvidence {
208 telemetry_dir_exists: bool,
209 dry_run: Option<String>,
210 pending: Option<String>,
211 setup: Option<SetupState>,
212 stderr: String,
213 }
214
215 /// Run the real dispatcher into a keyless in-process command and report the
216 /// telemetry state it actually left behind.
217 fn dispatch_and_read_telemetry(telemetry_env: Option<&str>) -> DispatchEvidence {
218 dispatch_and_read_telemetry_with_endpoint(
219 telemetry_env,
220 Some(""),
221 Some(TELEMETRY_NOTICE_VERSION),
222 true,
223 )
224 }
225
226 fn dispatch_and_read_telemetry_with_endpoint(
227 telemetry_env: Option<&str>,
228 endpoint: Option<&str>,
229 consent_version: Option<&str>,
230 cli_on: bool,
231 ) -> DispatchEvidence {
232 let fixture = TempDir::new().expect("fixture root");
233 let home = fixture.path().join("home");
234 let codewhale_home = fixture.path().join("codewhale-home");
235 let workspace = fixture.path().join("workspace");
236 for dir in [&home, &codewhale_home, &workspace] {
237 fs::create_dir_all(dir).expect("create fixture dir");
238 }
239
240 if let Some(version) = consent_version {
241 let mut state = SetupState::default();
242 state.record_telemetry_notice(version, true);
243 state
244 .save_to(&codewhale_home.join("setup_state.json"))
245 .expect("write fixture consent");
246 }
247
248 let config_path = fixture.path().join("config.toml");
249 let mut config = String::new();
250 if let Some(endpoint) = endpoint {
251 config.push_str(&format!("telemetry_endpoint = {endpoint:?}\n"));
252 }
253 fs::write(&config_path, config).expect("write config");
254
255 let mut command = Command::new(codewhale_binary());
256 command
257 .current_dir(&workspace)
258 .env_clear()
259 .env("PATH", std::env::var_os("PATH").expect("PATH"))
260 .env("HOME", &home)
261 .env("USERPROFILE", &home)
262 .env("CODEWHALE_HOME", &codewhale_home)
263 .env("CODEWHALE_SECRET_BACKEND", "file")
264 .env(
265 "CODEWHALE_RELEASE_BASE_URL",
266 "https://example.invalid/releases",
267 )
268 .arg("--config")
269 .arg(&config_path);
270 if cli_on {
271 command.args(["--telemetry", "true"]);
272 }
273 command.args(["features", "list"]);
274 if let Some(value) = telemetry_env {
275 command.env("CODEWHALE_TELEMETRY", value);
276 }
277 let output = command.output().expect("run codewhale dispatcher");
278 assert!(
279 output.status.success(),
280 "the in-process feature command must succeed\nstdout:\n{}\nstderr:\n{}",
281 String::from_utf8_lossy(&output.stdout),
282 String::from_utf8_lossy(&output.stderr)
283 );
284 assert!(
285 String::from_utf8_lossy(&output.stdout).contains("feature\tstage\tenabled"),
286 "the real in-process feature command must have run\nstdout:\n{}\nstderr:\n{}",
287 String::from_utf8_lossy(&output.stdout),
288 String::from_utf8_lossy(&output.stderr)
289 );
290
291 let telemetry_dir = codewhale_home.join("telemetry");
292 let dry_run = match fs::read_to_string(telemetry_dir.join("dryrun.jsonl")) {
293 Ok(contents) => Some(contents),
294 Err(error) if error.kind() == std::io::ErrorKind::NotFound => None,
295 Err(error) => panic!("read telemetry dry-run sink: {error}"),
296 };
297 let pending = fs::read_dir(telemetry_dir.join("spool"))
298 .ok()
299 .and_then(|entries| {
300 let bodies: Vec<String> = entries
301 .filter_map(Result::ok)
302 .map(|entry| entry.path())
303 .filter(|path| path.extension().is_some_and(|ext| ext == "jsonl"))
304 .filter_map(|path| fs::read_to_string(path).ok())
305 .collect();
306 (!bodies.is_empty()).then(|| bodies.join("\n"))
307 });
308 DispatchEvidence {
309 telemetry_dir_exists: telemetry_dir.exists(),
310 dry_run,
311 pending,
312 setup: SetupState::load_from(&codewhale_home.join("setup_state.json")),
313 stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
314 }
315 }
316
317 fn codewhale_binary() -> std::path::PathBuf {
318 std::env::var_os("CARGO_BIN_EXE_codewhale")
319 .filter(|value| !value.is_empty())
320 .map(std::path::PathBuf::from)
321 .unwrap_or_else(|| std::path::PathBuf::from(env!("CARGO_BIN_EXE_codewhale")))
322 }
323
323 lines RUST