| 1 | use super::*; |
| 2 | use tempfile::TempDir; |
| 3 | |
| 4 | fn write_flat(root: &Path, name: &str, body: &[u8]) -> PathBuf { |
| 5 | let dir = root.join(name); |
| 6 | fs::create_dir_all(&dir).unwrap(); |
| 7 | let file = dir.join("SKILL.md"); |
| 8 | fs::write(&file, body).unwrap(); |
| 9 | file |
| 10 | } |
| 11 | |
| 12 | #[test] |
| 13 | fn fresh_install_has_six_verified_bodies_and_one_fingerprint() { |
| 14 | let tmp = TempDir::new().unwrap(); |
| 15 | let root = tmp.path().join("skills"); |
| 16 | install_system_skills(&root, &Default::default()).unwrap(); |
| 17 | let pack = root.join(".system"); |
| 18 | assert_eq!(fs::read_dir(&pack).unwrap().count(), 7); |
| 19 | for skill in BUNDLED_SKILLS { |
| 20 | let entry = pack.join(skill.name).join("SKILL.md"); |
| 21 | assert_eq!(fs::read_to_string(&entry).unwrap(), skill.body); |
| 22 | assert!(is_bundled_copy(&entry, skill.name, &root)); |
| 23 | assert!(!root.join(skill.name).exists()); |
| 24 | } |
| 25 | let marker = pack.join(".fingerprint"); |
| 26 | let original = fs::read(&marker).unwrap(); |
| 27 | let time = fs::metadata(&marker).unwrap().modified().unwrap(); |
| 28 | assert_eq!( |
| 29 | starter::installed_fingerprint(&root) |
| 30 | .unwrap() |
| 31 | .unwrap() |
| 32 | .len(), |
| 33 | 64 |
| 34 | ); |
| 35 | install_system_skills(&root, &Default::default()).unwrap(); |
| 36 | assert_eq!(fs::read(&marker).unwrap(), original); |
| 37 | assert_eq!(fs::metadata(marker).unwrap().modified().unwrap(), time); |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn install_policy_controls_missing_files_without_replacing_personal_edits() { |
| 42 | let tmp = TempDir::new().unwrap(); |
| 43 | let root = tmp.path().join("skills"); |
| 44 | let mut policy = BundledSkillsConfig { |
| 45 | install: Some(BundledSkillsInstall::None), |
| 46 | ..Default::default() |
| 47 | }; |
| 48 | install_system_skills(&root, &policy).unwrap(); |
| 49 | assert!(!root.exists()); |
| 50 | policy.install = Some(BundledSkillsInstall::Auto); |
| 51 | policy.selected = Some(vec!["review".into(), "help".into()]); |
| 52 | policy.disabled = Some(vec!["help".into()]); |
| 53 | install_system_skills(&root, &policy).unwrap(); |
| 54 | let pack = root.join(".system"); |
| 55 | let review = pack.join("review/SKILL.md"); |
| 56 | assert!(review.exists()); |
| 57 | assert!(!pack.join("help").exists()); |
| 58 | fs::remove_file(&review).unwrap(); |
| 59 | policy.disabled = None; |
| 60 | install_system_skills(&root, &policy).unwrap(); |
| 61 | assert!(pack.join("help/SKILL.md").exists()); |
| 62 | assert!(!review.exists()); |
| 63 | policy.install = Some(BundledSkillsInstall::All); |
| 64 | install_system_skills(&root, &policy).unwrap(); |
| 65 | assert_eq!(fs::read_to_string(&review).unwrap(), REVIEW_BODY); |
| 66 | fs::write(&review, b"personal edits").unwrap(); |
| 67 | install_system_skills(&root, &policy).unwrap(); |
| 68 | assert_eq!(fs::read(&review).unwrap(), b"personal edits"); |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn migration_requires_a_legacy_marker_and_an_unmodified_complete_tree() { |
| 73 | for marker in [None, Some("0"), Some("26"), Some("future"), Some("28")] { |
| 74 | let tmp = TempDir::new().unwrap(); |
| 75 | let root = tmp.path(); |
| 76 | if let Some(value) = marker { |
| 77 | fs::write(root.join(".system-installed-version"), value).unwrap(); |
| 78 | } |
| 79 | let shipped = write_flat(root, "review", REVIEW_BODY.as_bytes()); |
| 80 | let edited = write_flat(root, "help", b"personal help"); |
| 81 | let manual = write_flat(root, "forget", FORGET_BODY.as_bytes()); |
| 82 | fs::write(root.join("forget/.installed-from"), "marketplace").unwrap(); |
| 83 | let extra = write_flat(root, "skill-creator", SKILL_CREATOR_BODY.as_bytes()); |
| 84 | fs::write(root.join("skill-creator/local.md"), "personal").unwrap(); |
| 85 | let binary = write_flat(root, "plugin-creator", &[0xff, 0xfe]); |
| 86 | install_system_skills(root, &Default::default()).unwrap(); |
| 87 | assert_eq!(shipped.is_file(), marker != Some("26")); |
| 88 | assert_eq!(fs::read(edited).unwrap(), b"personal help"); |
| 89 | assert_eq!(fs::read(binary).unwrap(), [0xff, 0xfe]); |
| 90 | assert!(manual.is_file()); |
| 91 | assert!(extra.is_file()); |
| 92 | assert!(root.join(".system/review/SKILL.md").exists()); |
| 93 | assert_eq!( |
| 94 | root.join(".system/skill-installer/SKILL.md").exists(), |
| 95 | marker != Some("26") |
| 96 | ); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | #[test] |
| 101 | fn migration_preserves_new_skills_and_intentional_removals_from_old_installs() { |
| 102 | let tmp = TempDir::new().unwrap(); |
| 103 | fs::write(tmp.path().join(".system-installed-version"), "3").unwrap(); |
| 104 | install_system_skills(tmp.path(), &Default::default()).unwrap(); |
| 105 | let pack = tmp.path().join(".system"); |
| 106 | assert!(!pack.join("plugin-creator/SKILL.md").exists()); |
| 107 | assert!(pack.join("forget/SKILL.md").exists()); |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn interrupted_starter_replacement_recovers_a_selected_missing_body() { |
| 112 | for file in ["SKILL.md", ".updating-SKILL"] { |
| 113 | let tmp = TempDir::new().unwrap(); |
| 114 | let root = tmp.path(); |
| 115 | let dir = root.join(".system/debug"); |
| 116 | fs::create_dir_all(&dir).unwrap(); |
| 117 | fs::write( |
| 118 | dir.join(file), |
| 119 | include_str!("../../assets/skills/debug/SKILL.md"), |
| 120 | ) |
| 121 | .unwrap(); |
| 122 | let updated = BundledSkill { |
| 123 | name: "debug", |
| 124 | body: "Updated body", |
| 125 | introduced_in: 5, |
| 126 | }; |
| 127 | starter::seed(root, &updated, false).unwrap(); |
| 128 | assert_eq!( |
| 129 | fs::read_to_string(dir.join("SKILL.md")).unwrap(), |
| 130 | updated.body |
| 131 | ); |
| 132 | assert!(!dir.join(".updating-SKILL").exists()); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn interrupted_upgrade_restores_edits_to_the_active_skill() { |
| 138 | for edited in [b"personal guide".as_slice(), &[0xff]] { |
| 139 | let tmp = TempDir::new().unwrap(); |
| 140 | let root = tmp.path(); |
| 141 | let dir = root.join(".system/review"); |
| 142 | fs::create_dir_all(&dir).unwrap(); |
| 143 | fs::write(dir.join(".updating-SKILL"), edited).unwrap(); |
| 144 | let skill = BUNDLED_SKILLS |
| 145 | .iter() |
| 146 | .find(|skill| skill.name == "review") |
| 147 | .unwrap(); |
| 148 | starter::seed(root, skill, false).unwrap(); |
| 149 | assert_eq!(fs::read(dir.join("SKILL.md")).unwrap(), edited); |
| 150 | assert!(!dir.join(".updating-SKILL").exists()); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn migration_preserves_copies_from_before_their_first_shipped_generation() { |
| 156 | let tmp = TempDir::new().unwrap(); |
| 157 | let root = tmp.path(); |
| 158 | fs::write(root.join(".system-installed-version"), "1").unwrap(); |
| 159 | let review = write_flat(root, "review", REVIEW_BODY.as_bytes()); |
| 160 | install_system_skills(root, &Default::default()).unwrap(); |
| 161 | assert_eq!(fs::read_to_string(review).unwrap(), REVIEW_BODY); |
| 162 | } |
| 163 | |
| 164 | #[test] |
| 165 | fn migration_finishes_deleting_a_verified_partial_tree() { |
| 166 | let tmp = TempDir::new().unwrap(); |
| 167 | let root = tmp.path(); |
| 168 | let id = uuid::Uuid::new_v4().to_string(); |
| 169 | let staging = root.join(format!(".system-retiring-{id}")); |
| 170 | let partial = staging.join(".deleting-cordis-plugin-development"); |
| 171 | fs::create_dir_all(&partial).unwrap(); |
| 172 | fs::write(partial.join("LICENSE"), "remaining file").unwrap(); |
| 173 | fs::create_dir(root.join(".system")).unwrap(); |
| 174 | fs::write(root.join(".system/.retirement"), id).unwrap(); |
| 175 | fs::write(root.join(".system-installed-version"), "26").unwrap(); |
| 176 | install_system_skills(root, &Default::default()).unwrap(); |
| 177 | assert!(!staging.exists()); |
| 178 | assert!(!root.join("cordis-plugin-development").exists()); |
| 179 | } |
| 180 | |
| 181 | #[cfg(unix)] |
| 182 | #[test] |
| 183 | fn migration_preserves_hard_linked_flat_skills() { |
| 184 | let tmp = TempDir::new().unwrap(); |
| 185 | let root = tmp.path(); |
| 186 | fs::write(root.join(".system-installed-version"), "26").unwrap(); |
| 187 | let flat = write_flat(root, "review", REVIEW_BODY.as_bytes()); |
| 188 | fs::hard_link(&flat, root.join("my-review")).unwrap(); |
| 189 | install_system_skills(root, &Default::default()).unwrap(); |
| 190 | assert_eq!(fs::read_to_string(flat).unwrap(), REVIEW_BODY); |
| 191 | assert!(starter::installed_fingerprint(root).unwrap().is_some()); |
| 192 | } |
| 193 | |
| 194 | #[test] |
| 195 | fn corrupt_fingerprint_does_not_reclassify_flat_user_skills() { |
| 196 | let tmp = TempDir::new().unwrap(); |
| 197 | let root = tmp.path(); |
| 198 | let flat = write_flat(root, "review", REVIEW_BODY.as_bytes()); |
| 199 | fs::create_dir(root.join(".system")).unwrap(); |
| 200 | fs::write(root.join(".system/.fingerprint"), "broken").unwrap(); |
| 201 | fs::write(root.join(".system-installed-version"), "26").unwrap(); |
| 202 | install_system_skills(root, &Default::default()).unwrap(); |
| 203 | assert!(flat.is_file()); |
| 204 | assert!(starter::installed_fingerprint(root).unwrap().is_some()); |
| 205 | } |
| 206 | |
| 207 | #[cfg(unix)] |
| 208 | #[test] |
| 209 | fn migration_never_follows_a_linked_legacy_entry() { |
| 210 | let tmp = TempDir::new().unwrap(); |
| 211 | let root = tmp.path(); |
| 212 | let outside = TempDir::new().unwrap(); |
| 213 | let original = write_flat(outside.path(), "review", REVIEW_BODY.as_bytes()); |
| 214 | std::os::unix::fs::symlink(outside.path().join("review"), root.join("review")).unwrap(); |
| 215 | fs::write(root.join(".system-installed-version"), "26").unwrap(); |
| 216 | install_system_skills(root, &Default::default()).unwrap(); |
| 217 | assert_eq!(fs::read_to_string(original).unwrap(), REVIEW_BODY); |
| 218 | assert!( |
| 219 | fs::symlink_metadata(root.join("review")) |
| 220 | .unwrap() |
| 221 | .is_symlink() |
| 222 | ); |
| 223 | } |
| 224 | |
| 225 | #[test] |
| 226 | fn contributor_onboarding_is_repo_local_and_keeps_its_refusals() { |
| 227 | assert!( |
| 228 | !is_bundled_skill_name("contributor-onboarding"), |
| 229 | "contributor-onboarding must not ship to every user anymore" |
| 230 | ); |
| 231 | |
| 232 | let body = include_str!("../../../../docs/skills/contributor-onboarding/SKILL.md"); |
| 233 | assert!(body.contains("invocation: explicit-only")); |
| 234 | assert!(body.contains("Do not run `git fetch`, `git pull`, `git rebase`")); |
| 235 | assert!(body.contains("Do not stash, discard, reset, or commit a dirty tree")); |
| 236 | assert!(body.contains("cargo clippy --workspace --all-targets --all-features --locked")); |
| 237 | assert!(body.contains(".github/workflows/ci.yml")); |
| 238 | assert!(body.contains("Do not call a model provider")); |
| 239 | assert!(body.contains("./target/release/codewhale exec --help")); |
| 240 | assert!(body.contains("Never select a provider for them")); |
| 241 | assert!(body.contains("@JayBeest")); |
| 242 | } |
| 243 | |
| 244 | #[test] |
| 245 | fn shipped_body_hashes_cover_superseded_generations_and_reject_current_bodies() { |
| 246 | for line in SHIPPED_BODY_HASHES.lines() { |
| 247 | let (name, digest) = line.split_once(' ').expect("name and digest"); |
| 248 | assert!( |
| 249 | is_bundled_skill_name(name) || retired_bundled_skill_name(name).is_some(), |
| 250 | "{name} is neither bundled nor retired" |
| 251 | ); |
| 252 | assert_eq!(digest.len(), 64, "{name}"); |
| 253 | assert!(digest.bytes().all(|b| b.is_ascii_hexdigit()), "{name}"); |
| 254 | } |
| 255 | for (name, digest) in [ |
| 256 | ( |
| 257 | "forget", |
| 258 | "12ffaf4afc0aefb7a024d0b8dcf673fbf79cb5489a5872329e5eaf2b5cf0f5ad", |
| 259 | ), |
| 260 | ( |
| 261 | "gmail", |
| 262 | "f6c242008c9b6c71c5e9ba4ea4fe3800268f9c9d443a6fb061d17418b5ee2b0e", |
| 263 | ), |
| 264 | ( |
| 265 | "google-calendar", |
| 266 | "c05737fe2714eeda4f2f203c3066623d767489f354d9c9e4307a0d0a6967a3a0", |
| 267 | ), |
| 268 | ( |
| 269 | "help", |
| 270 | "3e03eda8e9581682093eeb2c37fc18478b51ee2b014638f8927198e36ea613ef", |
| 271 | ), |
| 272 | ( |
| 273 | "mcp-discovery", |
| 274 | "b7ecdccf9dc95ea82c562ea59f2fc940f2b43002b558185b89df4b07323d821b", |
| 275 | ), |
| 276 | ( |
| 277 | "pdf", |
| 278 | "14d37e1c04d744f8994ead97771875f010b3c1d7eb00ecdd328f6c8fee70fdca", |
| 279 | ), |
| 280 | ( |
| 281 | "photos", |
| 282 | "5f565864b2b42fadcb02e73647fdd086b36bd86e6705894b74b5ed17beafa5e5", |
| 283 | ), |
| 284 | ( |
| 285 | "plugin-creator", |
| 286 | "8bc868fa92cf192a42fe150696df1b2248b7b2a8acc723b79937c81b10daac06", |
| 287 | ), |
| 288 | ( |
| 289 | "plugin-creator", |
| 290 | "32139a0bdcf5da00cc0ce49144feb90540496023dd7121b8abc2f7db0debfe1b", |
| 291 | ), |
| 292 | ( |
| 293 | "plugin-creator", |
| 294 | "e91aa6dfde62abd9548dda7a43bc097a82aa19385335117e86cb204edc9e294d", |
| 295 | ), |
| 296 | ( |
| 297 | "plugin-creator", |
| 298 | "c761d6774b751a985ffed2894f595d8f871430b0a05a8e94bb7245399d811de7", |
| 299 | ), |
| 300 | ( |
| 301 | "plugin-creator", |
| 302 | "2e71c88ea1d3c5838034185ad07ee7db8dc195c46136f2d25f816a6a4bd5a768", |
| 303 | ), |
| 304 | ( |
| 305 | "spotify", |
| 306 | "5bf2d29160a4e650d0c4ffb77fdf6f5d0c4a6667d9068af713262e27f0719840", |
| 307 | ), |
| 308 | ] { |
| 309 | let entry = format!("{name} {digest}"); |
| 310 | assert!( |
| 311 | SHIPPED_BODY_HASHES.lines().any(|line| line == entry), |
| 312 | "missing superseded digest entry: {entry}" |
| 313 | ); |
| 314 | } |
| 315 | for skill in BUNDLED_SKILLS { |
| 316 | assert!( |
| 317 | !is_superseded_shipped_body(skill.name, skill.body), |
| 318 | "current body must not be listed as superseded: {}", |
| 319 | skill.name |
| 320 | ); |
| 321 | } |
| 322 | let plugin_creator = BUNDLED_SKILLS |
| 323 | .iter() |
| 324 | .find(|skill| skill.name == "plugin-creator") |
| 325 | .unwrap(); |
| 326 | assert!(plugin_creator.body.contains("Create `plugin.json`")); |
| 327 | assert!(plugin_creator.body.contains("cordis-plugin-development")); |
| 328 | assert!( |
| 329 | !plugin_creator |
| 330 | .body |
| 331 | .contains("never automate the trust token") |
| 332 | ); |
| 333 | } |
| 334 |