| 1 | //! Real dispatcher regression: nested config edits retain TOML types and |
| 2 | //! unrelated bytes. Only config commands run; no provider, player or OS banner. |
| 3 | use std::{ |
| 4 | fs, |
| 5 | path::Path, |
| 6 | process::{Command, Output}, |
| 7 | }; |
| 8 | |
| 9 | fn run(root: &Path, args: &[&str]) -> Output { |
| 10 | Command::new( |
| 11 | std::env::var_os("CARGO_BIN_EXE_codewhale") |
| 12 | .filter(|value| !value.is_empty()) |
| 13 | .map(std::path::PathBuf::from) |
| 14 | .unwrap_or_else(|| std::path::PathBuf::from(env!("CARGO_BIN_EXE_codewhale"))), |
| 15 | ) |
| 16 | .env_clear() |
| 17 | .env("HOME", root) |
| 18 | .env("USERPROFILE", root) |
| 19 | .env("CODEWHALE_HOME", root.join("state")) |
| 20 | .env("CODEWHALE_SECRET_BACKEND", "file") |
| 21 | .current_dir(root) |
| 22 | .arg("--config") |
| 23 | .arg(root.join("config.toml")) |
| 24 | .arg("config") |
| 25 | .args(args) |
| 26 | .output() |
| 27 | .unwrap() |
| 28 | } |
| 29 | fn ok(root: &Path, args: &[&str]) -> String { |
| 30 | let out = run(root, args); |
| 31 | assert!( |
| 32 | out.status.success(), |
| 33 | "{args:?}: {}", |
| 34 | String::from_utf8_lossy(&out.stderr) |
| 35 | ); |
| 36 | String::from_utf8(out.stdout).unwrap() |
| 37 | } |
| 38 | |
| 39 | #[test] |
| 40 | fn nested_notification_cli_set_get_unset_is_typed_lossless_and_validated() { |
| 41 | let temp = tempfile::tempdir().unwrap(); |
| 42 | let root = temp.path(); |
| 43 | let path = root.join("config.toml"); |
| 44 | fs::write(&path, "# keep operator note\n\"notifications.quiet\" = \"false\"\n[notifications]\nfuture = \"keep\"\n[notifications.events]\ninput-needed = false\n").unwrap(); |
| 45 | for (key, value) in [ |
| 46 | ("sound", "whale"), |
| 47 | ("quiet", "true"), |
| 48 | ("threshold_secs", "17"), |
| 49 | ("events.approval-needed", "false"), |
| 50 | ("event_sound.events", r#"["model-notify", "input-needed"]"#), |
| 51 | ("sound_file", "custom call.wav"), |
| 52 | ] { |
| 53 | ok(root, &["set", &format!("notifications.{key}"), value]); |
| 54 | } |
| 55 | assert_eq!(ok(root, &["get", "notifications.sound"]).trim(), "whale"); |
| 56 | assert_eq!(ok(root, &["get", "notifications.quiet"]).trim(), "true"); |
| 57 | let saved = fs::read_to_string(&path).unwrap(); |
| 58 | assert!(saved.contains("# keep operator note")); |
| 59 | let raw: toml::Value = toml::from_str(&saved).unwrap(); |
| 60 | assert_eq!(raw["notifications"]["quiet"].as_bool(), Some(true)); |
| 61 | assert_eq!( |
| 62 | raw["notifications"]["threshold_secs"].as_integer(), |
| 63 | Some(17) |
| 64 | ); |
| 65 | assert_eq!( |
| 66 | raw["notifications"]["event_sound"]["events"] |
| 67 | .as_array() |
| 68 | .unwrap() |
| 69 | .len(), |
| 70 | 2 |
| 71 | ); |
| 72 | assert_eq!( |
| 73 | raw["notifications"]["events"]["input-needed"].as_bool(), |
| 74 | Some(false) |
| 75 | ); |
| 76 | assert_eq!(raw["notifications"]["future"].as_str(), Some("keep")); |
| 77 | assert!(!raw.as_table().unwrap().contains_key("notifications.quiet")); |
| 78 | for (key, value) in [ |
| 79 | ("notifications", "false"), |
| 80 | ("notifications.quiet", "maybe"), |
| 81 | ("notifications.threshold_secs", "18446744073709551615"), |
| 82 | ("notifications.event_sound.events", r#"["unknown"]"#), |
| 83 | ] { |
| 84 | assert!(!run(root, &["set", key, value]).status.success()); |
| 85 | assert_eq!(fs::read_to_string(&path).unwrap(), saved); |
| 86 | } |
| 87 | ok(root, &["unset", "notifications.quiet"]); |
| 88 | assert_eq!(ok(root, &["get", "notifications.quiet"]).trim(), "false"); |
| 89 | assert_eq!(ok(root, &["get", "notifications.sound"]).trim(), "whale"); |
| 90 | assert!( |
| 91 | ok(root, &["get", "notifications"]) |
| 92 | .contains("notifications.events.approval-needed = false") |
| 93 | ); |
| 94 | } |
| 95 |