aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-switchboard / tests / doctor.rs
| 1 | //! `switchboard doctor`: reports presence and permissions, never contents. |
| 2 | |
| 3 | mod common; |
| 4 | |
| 5 | use aien_switchboard::*; |
| 6 | use common::*; |
| 7 | use std::fs; |
| 8 | use std::os::unix::fs::PermissionsExt; |
| 9 | |
| 10 | fn probe() -> FakeProbe { |
| 11 | FakeProbe { |
| 12 | answer: Ok("2.1.301 (Claude Code)".into()), |
| 13 | calls: Vec::new(), |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | fn text(r: &Report) -> String { |
| 18 | r.to_string() |
| 19 | } |
| 20 | |
| 21 | #[test] |
| 22 | fn signed_in_profile_reports_presence_without_opening_the_credentials_file() { |
| 23 | let f = Fixture::new(); |
| 24 | f.install_fake_claude(); |
| 25 | let dir = f.profile_dir("anthropic-1"); |
| 26 | f.make_private_dir(&dir); |
| 27 | let cred = dir.join(".credentials.json"); |
| 28 | fs::write(&cred, "{\"token\":\"CRED-SENTINEL\"}").unwrap(); |
| 29 | // Unreadable: if doctor tried to open it, it could not, and the report |
| 30 | // would show an error instead of "present". |
| 31 | fs::set_permissions(&cred, fs::Permissions::from_mode(0o000)).unwrap(); |
| 32 | |
| 33 | let cwd = f.tmp.path().join("work"); |
| 34 | fs::create_dir_all(&cwd).unwrap(); |
| 35 | let r = doctor(&f.rules(""), &f.base_env(), &cwd, &mut probe()); |
| 36 | let t = text(&r); |
| 37 | fs::set_permissions(&cred, fs::Permissions::from_mode(0o600)).unwrap(); |
| 38 | |
| 39 | assert!(!t.contains("CRED-SENTINEL"), "{t}"); |
| 40 | let found = r |
| 41 | .findings |
| 42 | .iter() |
| 43 | .find(|x| x.subject == "anthropic-1" && x.message.contains("credentials file present")) |
| 44 | .unwrap_or_else(|| panic!("no presence finding: {t}")); |
| 45 | assert_eq!(found.level, Level::Warn, "mode 000 is not 0600: {t}"); |
| 46 | assert!(found.message.contains("000"), "{t}"); |
| 47 | } |
| 48 | |
| 49 | #[test] |
| 50 | fn credentials_with_0600_are_ok_and_missing_ones_say_not_signed_in() { |
| 51 | let f = Fixture::new(); |
| 52 | f.install_fake_claude(); |
| 53 | let d1 = f.profile_dir("anthropic-1"); |
| 54 | f.make_private_dir(&d1); |
| 55 | let cred = d1.join(".credentials.json"); |
| 56 | fs::write(&cred, "x").unwrap(); |
| 57 | fs::set_permissions(&cred, fs::Permissions::from_mode(0o600)).unwrap(); |
| 58 | f.make_private_dir(&f.profile_dir("anthropic-2")); |
| 59 | |
| 60 | let r = doctor(&f.rules(""), &f.base_env(), f.tmp.path(), &mut probe()); |
| 61 | let t = text(&r); |
| 62 | assert!( |
| 63 | r.findings.iter().any(|x| x.subject == "anthropic-1" |
| 64 | && x.level == Level::Ok |
| 65 | && x.message.contains("credentials file present")), |
| 66 | "{t}" |
| 67 | ); |
| 68 | assert!( |
| 69 | r.findings.iter().any(|x| x.subject == "anthropic-2" |
| 70 | && x.level == Level::Warn |
| 71 | && x.message.contains("not signed in")), |
| 72 | "{t}" |
| 73 | ); |
| 74 | assert!(!r.failed(), "{t}"); |
| 75 | } |
| 76 | |
| 77 | #[test] |
| 78 | fn executable_and_version_are_reported() { |
| 79 | let f = Fixture::new(); |
| 80 | let exe = f.install_fake_claude(); |
| 81 | let mut p = probe(); |
| 82 | let r = doctor(&f.rules(""), &f.base_env(), f.tmp.path(), &mut p); |
| 83 | let t = text(&r); |
| 84 | assert!(t.contains("2.1.301 (Claude Code)"), "{t}"); |
| 85 | assert!(t.contains(exe.to_str().unwrap()), "{t}"); |
| 86 | assert_eq!(p.calls, vec![exe]); |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn missing_executable_fails_the_doctor() { |
| 91 | let f = Fixture::new(); |
| 92 | fs::create_dir_all(f.bin()).unwrap(); |
| 93 | let mut p = probe(); |
| 94 | let r = doctor(&f.rules(""), &f.base_env(), f.tmp.path(), &mut p); |
| 95 | assert!(r.failed(), "{}", text(&r)); |
| 96 | assert!(r |
| 97 | .findings |
| 98 | .iter() |
| 99 | .any(|x| x.level == Level::Fail && x.message.contains("not found"))); |
| 100 | assert!(p.calls.is_empty()); |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn missing_profile_folder_is_a_warning_and_open_folder_is_a_failure() { |
| 105 | let f = Fixture::new(); |
| 106 | f.install_fake_claude(); |
| 107 | let d2 = f.profile_dir("anthropic-2"); |
| 108 | fs::create_dir_all(&d2).unwrap(); |
| 109 | fs::set_permissions(&d2, fs::Permissions::from_mode(0o750)).unwrap(); |
| 110 | let r = doctor(&f.rules(""), &f.base_env(), f.tmp.path(), &mut probe()); |
| 111 | let t = text(&r); |
| 112 | assert!( |
| 113 | r.findings.iter().any(|x| x.subject == "anthropic-1" |
| 114 | && x.level == Level::Warn |
| 115 | && x.message.contains("folder not created yet")), |
| 116 | "{t}" |
| 117 | ); |
| 118 | assert!( |
| 119 | r.findings.iter().any(|x| x.subject == "anthropic-2" |
| 120 | && x.level == Level::Fail |
| 121 | && x.message.contains("750")), |
| 122 | "{t}" |
| 123 | ); |
| 124 | assert!(r.failed()); |
| 125 | } |
| 126 | |
| 127 | #[test] |
| 128 | fn api_key_helper_and_env_block_in_profile_settings_warn_without_values() { |
| 129 | let f = Fixture::new(); |
| 130 | f.install_fake_claude(); |
| 131 | let d1 = f.profile_dir("anthropic-1"); |
| 132 | f.make_private_dir(&d1); |
| 133 | fs::write( |
| 134 | d1.join("settings.json"), |
| 135 | r#"{"apiKeyHelper":"/bin/echo HELPER-SENTINEL","env":{"ANTHROPIC_API_KEY":"sk-ant-SENTINEL","FOO":"bar"}}"#, |
| 136 | ) |
| 137 | .unwrap(); |
| 138 | let r = doctor(&f.rules(""), &f.base_env(), f.tmp.path(), &mut probe()); |
| 139 | let t = text(&r); |
| 140 | assert!(!t.contains("SENTINEL"), "{t}"); |
| 141 | assert!( |
| 142 | r.findings |
| 143 | .iter() |
| 144 | .any(|x| x.level == Level::Warn && x.message.contains("apiKeyHelper")), |
| 145 | "{t}" |
| 146 | ); |
| 147 | assert!( |
| 148 | r.findings.iter().any(|x| x.level == Level::Warn |
| 149 | && x.message.contains("env block") |
| 150 | && x.message.contains("ANTHROPIC_API_KEY")), |
| 151 | "{t}" |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn repo_settings_files_are_checked() { |
| 157 | let f = Fixture::new(); |
| 158 | f.install_fake_claude(); |
| 159 | let repo = f.tmp.path().join("repo"); |
| 160 | let sub = repo.join("crates").join("x"); |
| 161 | fs::create_dir_all(&sub).unwrap(); |
| 162 | fs::create_dir_all(repo.join(".git")).unwrap(); |
| 163 | fs::create_dir_all(repo.join(".claude")).unwrap(); |
| 164 | fs::write( |
| 165 | repo.join(".claude").join("settings.local.json"), |
| 166 | r#"{"env":{"CLAUDE_CODE_OAUTH_TOKEN":"OAUTH-SENTINEL"}}"#, |
| 167 | ) |
| 168 | .unwrap(); |
| 169 | fs::write( |
| 170 | repo.join(".claude").join("settings.json"), |
| 171 | "{not json OTHER-SENTINEL", |
| 172 | ) |
| 173 | .unwrap(); |
| 174 | let r = doctor(&f.rules(""), &f.base_env(), &sub, &mut probe()); |
| 175 | let t = text(&r); |
| 176 | assert!(!t.contains("SENTINEL"), "{t}"); |
| 177 | assert!( |
| 178 | r.findings |
| 179 | .iter() |
| 180 | .any(|x| x.subject.ends_with("settings.local.json") |
| 181 | && x.message.contains("CLAUDE_CODE_OAUTH_TOKEN")), |
| 182 | "{t}" |
| 183 | ); |
| 184 | assert!( |
| 185 | r.findings |
| 186 | .iter() |
| 187 | .any(|x| x.subject.ends_with("settings.json") && x.message.contains("cannot parse")), |
| 188 | "{t}" |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | #[test] |
| 193 | fn credential_variables_in_the_shell_are_named_not_shown() { |
| 194 | let f = Fixture::new(); |
| 195 | f.install_fake_claude(); |
| 196 | let mut e = f.base_env(); |
| 197 | e.insert("ANTHROPIC_API_KEY".into(), "sk-ant-SENTINEL".into()); |
| 198 | e.insert("AWS_SECRET_ACCESS_KEY".into(), "AWS-SENTINEL".into()); |
| 199 | let r = doctor(&f.rules(""), &e, f.tmp.path(), &mut probe()); |
| 200 | let t = text(&r); |
| 201 | assert!(!t.contains("SENTINEL"), "{t}"); |
| 202 | assert!( |
| 203 | t.contains("ANTHROPIC_API_KEY") && t.contains("AWS_SECRET_ACCESS_KEY"), |
| 204 | "{t}" |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | #[test] |
| 209 | fn anthropic_profile_files_in_home_are_flagged_by_presence() { |
| 210 | let f = Fixture::new(); |
| 211 | f.install_fake_claude(); |
| 212 | let cfg = f.home().join(".config").join("anthropic"); |
| 213 | fs::create_dir_all(&cfg).unwrap(); |
| 214 | fs::write(cfg.join("active_config"), "PROFILE-SENTINEL").unwrap(); |
| 215 | let r = doctor(&f.rules(""), &f.base_env(), f.tmp.path(), &mut probe()); |
| 216 | let t = text(&r); |
| 217 | assert!(!t.contains("PROFILE-SENTINEL"), "{t}"); |
| 218 | assert!( |
| 219 | r.findings |
| 220 | .iter() |
| 221 | .any(|x| x.level == Level::Warn && x.message.contains("Anthropic profile")), |
| 222 | "{t}" |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | #[test] |
| 227 | fn doctor_fails_two_profiles_that_resolve_to_one_folder() { |
| 228 | let f = Fixture::new(); |
| 229 | f.install_fake_claude(); |
| 230 | let real = f.tmp.path().join("real"); |
| 231 | let a1 = real.join("p"); |
| 232 | f.make_private_dir(&a1); |
| 233 | let link = f.tmp.path().join("link"); |
| 234 | std::os::unix::fs::symlink(&real, &link).unwrap(); |
| 235 | let toml = format!( |
| 236 | "[[accounts]]\nid=\"a1\"\nprovider=\"anthropic\"\nprogram=\"claude\"\nsettings_dir=\"{}\"\n\ |
| 237 | [[accounts]]\nid=\"a2\"\nprovider=\"anthropic\"\nprogram=\"claude\"\nsettings_dir=\"{}\"\n\ |
| 238 | [[accounts]]\nid=\"local\"\nprovider=\"local\"\nprogram=\"aien\"\n[routes]\ndefault=[\"local\"]\n", |
| 239 | a1.display(), |
| 240 | link.join("p").display() |
| 241 | ); |
| 242 | let rules = RuleTable::from_toml_str(&toml).unwrap(); |
| 243 | let r = doctor(&rules, &f.base_env(), f.tmp.path(), &mut probe()); |
| 244 | let t = text(&r); |
| 245 | assert!(r.failed(), "{t}"); |
| 246 | assert!( |
| 247 | r.findings |
| 248 | .iter() |
| 249 | .any(|x| x.subject == "a2" && x.level == Level::Fail && x.message.contains("a1")), |
| 250 | "{t}" |
| 251 | ); |
| 252 | } |