| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! End-to-end tests of `braid agents init`: the braid section of a project's |
| 5 | //! AGENTS.md, written once and rewritten in place, CLAUDE.md and GEMINI.md, |
| 6 | //! dry runs, JSON, the branch the section names, where it writes, and the |
| 7 | //! files it refuses to write. |
| 8 | |
| 9 | use std::fs; |
| 10 | use std::path::{Path, PathBuf}; |
| 11 | use std::process::{Command, Output}; |
| 12 | |
| 13 | use serde_json::Value; |
| 14 | |
| 15 | const START: &str = "<!-- braid:start -->"; |
| 16 | const END: &str = "<!-- braid:end -->"; |
| 17 | |
| 18 | /// Git without the user's or the system's configuration. |
| 19 | const GIT_ENV: [(&str, &str); 6] = [ |
| 20 | ("GIT_CONFIG_GLOBAL", "/dev/null"), |
| 21 | ("GIT_CONFIG_NOSYSTEM", "1"), |
| 22 | ("GIT_AUTHOR_NAME", "Ada Lovelace"), |
| 23 | ("GIT_AUTHOR_EMAIL", "ada@example.org"), |
| 24 | ("GIT_COMMITTER_NAME", "Ada Lovelace"), |
| 25 | ("GIT_COMMITTER_EMAIL", "ada@example.org"), |
| 26 | ]; |
| 27 | |
| 28 | /// Runs braid in `dir`. |
| 29 | fn braid(dir: &Path, args: &[&str]) -> Output { |
| 30 | Command::new(env!("CARGO_BIN_EXE_braid")) |
| 31 | .current_dir(dir) |
| 32 | .args(args) |
| 33 | .envs(GIT_ENV) |
| 34 | .env("BRAID_AUTHOR", "Ada <ada@example.org>") |
| 35 | .env("BRAID_AGENT", "tester") |
| 36 | .output() |
| 37 | .unwrap() |
| 38 | } |
| 39 | |
| 40 | /// Runs braid in `dir`, expecting success, and returns what it printed. |
| 41 | fn ok(dir: &Path, args: &[&str]) -> String { |
| 42 | let out = braid(dir, args); |
| 43 | assert!( |
| 44 | out.status.success(), |
| 45 | "braid {args:?}: {}", |
| 46 | String::from_utf8_lossy(&out.stderr) |
| 47 | ); |
| 48 | String::from_utf8(out.stdout).unwrap() |
| 49 | } |
| 50 | |
| 51 | /// Runs braid in `dir`, expecting it to fail with exit status 1 and to |
| 52 | /// print nothing on standard output, and returns its error. |
| 53 | fn fails(dir: &Path, args: &[&str]) -> String { |
| 54 | let out = braid(dir, args); |
| 55 | assert_eq!( |
| 56 | out.status.code(), |
| 57 | Some(1), |
| 58 | "braid {args:?}: {}", |
| 59 | String::from_utf8_lossy(&out.stdout) |
| 60 | ); |
| 61 | assert!(out.stdout.is_empty(), "braid {args:?}"); |
| 62 | String::from_utf8(out.stderr).unwrap() |
| 63 | } |
| 64 | |
| 65 | /// Runs `braid agents init` with `--json` and these arguments in `dir`. |
| 66 | fn init_json(dir: &Path, args: &[&str]) -> Value { |
| 67 | let mut all = vec!["agents", "init", "--json"]; |
| 68 | all.extend_from_slice(args); |
| 69 | serde_json::from_str(&ok(dir, &all)).unwrap() |
| 70 | } |
| 71 | |
| 72 | fn names(report: &Value, list: &str) -> Vec<String> { |
| 73 | report[list] |
| 74 | .as_array() |
| 75 | .unwrap_or_else(|| panic!("no {list} in {report}")) |
| 76 | .iter() |
| 77 | .map(|name| name.as_str().unwrap().to_owned()) |
| 78 | .collect() |
| 79 | } |
| 80 | |
| 81 | /// A temporary directory, canonical so that paths compare as braid prints |
| 82 | /// them. |
| 83 | fn temp() -> (tempfile::TempDir, PathBuf) { |
| 84 | let temp = tempfile::tempdir().unwrap(); |
| 85 | let path = fs::canonicalize(temp.path()).unwrap(); |
| 86 | (temp, path) |
| 87 | } |
| 88 | |
| 89 | fn read(path: &Path) -> String { |
| 90 | fs::read_to_string(path).unwrap() |
| 91 | } |
| 92 | |
| 93 | /// The braid section of `text`, markers included. |
| 94 | fn section_of(text: &str) -> &str { |
| 95 | let start = text.find(START).expect("a start marker"); |
| 96 | let end = text.find(END).expect("an end marker") + END.len() + 1; |
| 97 | &text[start..end] |
| 98 | } |
| 99 | |
| 100 | /// A braid repository in `dir` whose only branch is `branch`. |
| 101 | fn repository_with_branch(dir: &Path, branch: &str) { |
| 102 | ok(dir, &["init"]); |
| 103 | fs::write(dir.join("seed.txt"), "seed\n").unwrap(); |
| 104 | ok( |
| 105 | dir, |
| 106 | &[ |
| 107 | "change", |
| 108 | "create", |
| 109 | "--base", |
| 110 | "none", |
| 111 | "--set", |
| 112 | "seed.txt=@seed.txt", |
| 113 | "-m", |
| 114 | "Seed", |
| 115 | "--land", |
| 116 | branch, |
| 117 | ], |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | /// A temporary braid repository whose only branch is `main`. |
| 122 | fn project() -> (tempfile::TempDir, PathBuf) { |
| 123 | let (temp, dir) = temp(); |
| 124 | repository_with_branch(&dir, "main"); |
| 125 | (temp, dir) |
| 126 | } |
| 127 | |
| 128 | fn git(dir: &Path, args: &[&str]) { |
| 129 | let out = Command::new("git") |
| 130 | .current_dir(dir) |
| 131 | .args(args) |
| 132 | .envs(GIT_ENV) |
| 133 | .output() |
| 134 | .expect("git must be installed to run this test"); |
| 135 | assert!( |
| 136 | out.status.success(), |
| 137 | "git {args:?}: {}", |
| 138 | String::from_utf8_lossy(&out.stderr) |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | #[test] |
| 143 | fn without_a_repository_the_branch_must_be_given() { |
| 144 | let (_temp, dir) = temp(); |
| 145 | let err = fails(&dir, &["agents", "init"]); |
| 146 | assert!(err.contains("no braid repository here"), "{err}"); |
| 147 | assert!( |
| 148 | err.contains("`braid init`, then `braid git import .`"), |
| 149 | "{err}" |
| 150 | ); |
| 151 | assert!(err.contains("--branch"), "{err}"); |
| 152 | assert!(!dir.join("AGENTS.md").exists()); |
| 153 | |
| 154 | let report = init_json(&dir, &["--branch", "main"]); |
| 155 | assert_eq!(names(&report, "created"), ["AGENTS.md"]); |
| 156 | assert!(names(&report, "updated").is_empty()); |
| 157 | assert!(names(&report, "unchanged").is_empty()); |
| 158 | assert_eq!(report["branch"], "main"); |
| 159 | assert_eq!(report["previous_branch"], Value::Null); |
| 160 | assert_eq!(report["dry_run"], false); |
| 161 | assert!(report["repository"].is_null()); |
| 162 | assert_eq!(report["dir"], dir.to_str().unwrap()); |
| 163 | assert_eq!(report["paths"]["AGENTS.md"], "AGENTS.md"); |
| 164 | |
| 165 | let text = read(&dir.join("AGENTS.md")); |
| 166 | assert!(text.starts_with(&format!("{START}\n")), "{text}"); |
| 167 | assert!(text.ends_with(&format!("{END}\n")), "{text}"); |
| 168 | assert!(text.contains("braid workspace add agent-1 --track main\n")); |
| 169 | assert!(text.contains("https://braid.elffuss.com/agents.md")); |
| 170 | assert!(!dir.join("CLAUDE.md").exists()); |
| 171 | assert!(!dir.join("GEMINI.md").exists()); |
| 172 | |
| 173 | // The section names the branch now: a second run keeps it. |
| 174 | let said = ok(&dir, &["agents", "init"]); |
| 175 | assert!(said.contains("has this braid section already"), "{said}"); |
| 176 | assert!( |
| 177 | said.contains("`braid init`, then `braid git import .`"), |
| 178 | "{said}" |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | #[test] |
| 183 | fn only_the_section_changes_and_a_second_run_changes_nothing() { |
| 184 | let (_temp, dir) = project(); |
| 185 | let above = "# Our project\n\nTabs, not spaces.\n\n"; |
| 186 | let older = format!("{START}\n## Braid\n\nAn older section.\n{END}\n"); |
| 187 | let below = "\n## Releases\n\nTagged by hand.\n"; |
| 188 | fs::write(dir.join("AGENTS.md"), format!("{above}{older}{below}")).unwrap(); |
| 189 | |
| 190 | let report = init_json(&dir, &[]); |
| 191 | assert_eq!(names(&report, "updated"), ["AGENTS.md"]); |
| 192 | assert_eq!(report["repository"], dir.to_str().unwrap()); |
| 193 | let text = read(&dir.join("AGENTS.md")); |
| 194 | assert!(text.starts_with(above), "{text}"); |
| 195 | assert!(text.ends_with(below), "{text}"); |
| 196 | let section = section_of(&text); |
| 197 | assert_eq!(text, format!("{above}{section}{below}")); |
| 198 | assert!(!section.contains("An older section")); |
| 199 | assert!(section.contains("--track main")); |
| 200 | |
| 201 | let report = init_json(&dir, &[]); |
| 202 | assert_eq!(names(&report, "unchanged"), ["AGENTS.md"]); |
| 203 | assert!(names(&report, "updated").is_empty()); |
| 204 | assert_eq!(read(&dir.join("AGENTS.md")), text); |
| 205 | let said = ok(&dir, &["agents", "init"]); |
| 206 | assert!(said.contains("has this braid section already"), "{said}"); |
| 207 | assert!(!said.contains("braid init"), "{said}"); |
| 208 | } |
| 209 | |
| 210 | #[test] |
| 211 | fn a_file_without_markers_gets_the_section_at_its_end_once() { |
| 212 | let (_temp, dir) = project(); |
| 213 | let ours = "# Rules\n\nRun the tests before you push."; |
| 214 | fs::write(dir.join("AGENTS.md"), ours).unwrap(); |
| 215 | let said = ok(&dir, &["agents", "init"]); |
| 216 | assert!( |
| 217 | said.contains("Added the braid section to AGENTS.md"), |
| 218 | "{said}" |
| 219 | ); |
| 220 | let text = read(&dir.join("AGENTS.md")); |
| 221 | assert_eq!(text, format!("{ours}\n\n{}", section_of(&text))); |
| 222 | assert_eq!( |
| 223 | names(&init_json(&dir, &[]), "unchanged"), |
| 224 | ["AGENTS.md"], |
| 225 | "a second run changes nothing" |
| 226 | ); |
| 227 | assert_eq!(read(&dir.join("AGENTS.md")), text); |
| 228 | } |
| 229 | |
| 230 | #[test] |
| 231 | fn unbalanced_markers_are_refused_and_nothing_is_written() { |
| 232 | let (_temp, dir) = project(); |
| 233 | let claude = "# Claude\n"; |
| 234 | fs::write(dir.join("CLAUDE.md"), claude).unwrap(); |
| 235 | for (text, lines) in [ |
| 236 | (format!("# Rules\n{START}\nhalf a section\n"), "on line 2"), |
| 237 | (format!("# Rules\n{END}\n"), "on no line"), |
| 238 | (format!("{END}\n{START}\n"), "on line 2"), |
| 239 | (format!("{START}\n{START}\n{END}\n"), "on lines 1 and 2"), |
| 240 | ( |
| 241 | format!("{START}\n{END}\n{START}\n{END}\n"), |
| 242 | "on lines 1 and 3", |
| 243 | ), |
| 244 | ] { |
| 245 | fs::write(dir.join("AGENTS.md"), &text).unwrap(); |
| 246 | for args in [ |
| 247 | &["agents", "init"][..], |
| 248 | &["agents", "init", "--json"], |
| 249 | &["agents", "init", "--dry-run"], |
| 250 | ] { |
| 251 | let err = fails(&dir, args); |
| 252 | assert!(err.contains("unbalanced braid markers"), "{err}"); |
| 253 | assert!(err.contains(lines), "{text:?}: {err}"); |
| 254 | assert!(err.contains("nothing was written"), "{err}"); |
| 255 | assert_eq!(read(&dir.join("AGENTS.md")), text); |
| 256 | assert_eq!(read(&dir.join("CLAUDE.md")), claude); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | #[test] |
| 262 | fn markers_in_a_code_block_are_an_example_and_stay_one() { |
| 263 | let (_temp, dir) = project(); |
| 264 | let ours = format!( |
| 265 | "# Rules\n\nWhat braid writes looks like this:\n\n```markdown\n{START}\n(the rules braid writes)\n{END}\n```\n\nMore rules.\n" |
| 266 | ); |
| 267 | fs::write(dir.join("AGENTS.md"), &ours).unwrap(); |
| 268 | let report = init_json(&dir, &[]); |
| 269 | assert_eq!(names(&report, "updated"), ["AGENTS.md"]); |
| 270 | let text = read(&dir.join("AGENTS.md")); |
| 271 | assert!(text.starts_with(&ours), "the example stays: {text}"); |
| 272 | assert_eq!(text, format!("{ours}\n{}", section_of(&text[ours.len()..]))); |
| 273 | // The example next to the real section is not a second pair. |
| 274 | assert_eq!(names(&init_json(&dir, &[]), "unchanged"), ["AGENTS.md"]); |
| 275 | assert_eq!(read(&dir.join("AGENTS.md")), text); |
| 276 | |
| 277 | // A file that ends inside a code block would have the section read as |
| 278 | // code: refused. |
| 279 | let open = "# Rules\n\n```bash\nmake test\n"; |
| 280 | fs::write(dir.join("AGENTS.md"), open).unwrap(); |
| 281 | let err = fails(&dir, &["agents", "init", "--dry-run"]); |
| 282 | assert!(err.contains("opens on line 3 and is never closed"), "{err}"); |
| 283 | assert!(err.contains("nothing was written"), "{err}"); |
| 284 | fails(&dir, &["agents", "init"]); |
| 285 | assert_eq!(read(&dir.join("AGENTS.md")), open); |
| 286 | } |
| 287 | |
| 288 | #[test] |
| 289 | fn a_byte_order_mark_before_the_section_stays_and_hides_nothing() { |
| 290 | let (_temp, dir) = project(); |
| 291 | ok(&dir, &["agents", "init"]); |
| 292 | let written = read(&dir.join("AGENTS.md")); |
| 293 | let marked = format!("\u{feff}{written}"); |
| 294 | fs::write(dir.join("AGENTS.md"), &marked).unwrap(); |
| 295 | assert_eq!(names(&init_json(&dir, &[]), "unchanged"), ["AGENTS.md"]); |
| 296 | assert_eq!(read(&dir.join("AGENTS.md")), marked); |
| 297 | // A section of another branch is rewritten after the mark. |
| 298 | ok(&dir, &["ref", "set", "trunk", "main"]); |
| 299 | assert_eq!( |
| 300 | names(&init_json(&dir, &["--branch", "trunk"]), "updated"), |
| 301 | ["AGENTS.md"] |
| 302 | ); |
| 303 | let text = read(&dir.join("AGENTS.md")); |
| 304 | assert!(text.starts_with(&format!("\u{feff}{START}\n")), "{text:?}"); |
| 305 | assert!(text.contains("--track trunk\n")); |
| 306 | } |
| 307 | |
| 308 | #[test] |
| 309 | fn claude_md_gets_the_include_line_and_only_that() { |
| 310 | let (_temp, dir) = project(); |
| 311 | let claude = "# Notes for Claude\n\nBe brief."; |
| 312 | fs::write(dir.join("CLAUDE.md"), claude).unwrap(); |
| 313 | |
| 314 | let report = init_json(&dir, &["--no-claude"]); |
| 315 | assert_eq!(names(&report, "created"), ["AGENTS.md"]); |
| 316 | assert!(names(&report, "updated").is_empty()); |
| 317 | assert!(names(&report, "unchanged").is_empty()); |
| 318 | assert_eq!(read(&dir.join("CLAUDE.md")), claude); |
| 319 | |
| 320 | let report = init_json(&dir, &[]); |
| 321 | assert_eq!(names(&report, "updated"), ["CLAUDE.md"]); |
| 322 | assert_eq!(names(&report, "unchanged"), ["AGENTS.md"]); |
| 323 | assert_eq!(report["paths"]["CLAUDE.md"], "CLAUDE.md"); |
| 324 | assert_eq!( |
| 325 | read(&dir.join("CLAUDE.md")), |
| 326 | format!("{claude}\n@AGENTS.md\n") |
| 327 | ); |
| 328 | |
| 329 | let report = init_json(&dir, &[]); |
| 330 | assert_eq!(names(&report, "unchanged"), ["AGENTS.md", "CLAUDE.md"]); |
| 331 | for including in ["See @AGENTS.md for the rules.\n", "@./AGENTS.md\n"] { |
| 332 | fs::write(dir.join("CLAUDE.md"), including).unwrap(); |
| 333 | let said = ok(&dir, &["agents", "init"]); |
| 334 | assert!( |
| 335 | said.contains("CLAUDE.md includes AGENTS.md already"), |
| 336 | "{said}" |
| 337 | ); |
| 338 | assert_eq!(read(&dir.join("CLAUDE.md")), including); |
| 339 | } |
| 340 | |
| 341 | // An import shown in a code block imports nothing. |
| 342 | let shown = "# Claude notes\n\nThe import looks like this:\n\n```text\n@AGENTS.md\n```\n"; |
| 343 | fs::write(dir.join("CLAUDE.md"), shown).unwrap(); |
| 344 | assert_eq!(names(&init_json(&dir, &[]), "updated"), ["CLAUDE.md"]); |
| 345 | assert_eq!(read(&dir.join("CLAUDE.md")), format!("{shown}@AGENTS.md\n")); |
| 346 | |
| 347 | // A CLAUDE.md that ends inside a code block would hide the line. |
| 348 | let open = "# Claude\n\n~~~\nan example\n"; |
| 349 | fs::write(dir.join("CLAUDE.md"), open).unwrap(); |
| 350 | let err = fails(&dir, &["agents", "init"]); |
| 351 | assert!( |
| 352 | err.contains("CLAUDE.md ends inside a fenced code block"), |
| 353 | "{err}" |
| 354 | ); |
| 355 | assert_eq!(read(&dir.join("CLAUDE.md")), open); |
| 356 | } |
| 357 | |
| 358 | #[test] |
| 359 | fn gemini_md_gets_the_include_line_too() { |
| 360 | let (_temp, dir) = project(); |
| 361 | fs::write(dir.join("CLAUDE.md"), "# Claude\n").unwrap(); |
| 362 | fs::write(dir.join("GEMINI.md"), "# Gemini\n").unwrap(); |
| 363 | let report = init_json(&dir, &["--no-gemini"]); |
| 364 | assert_eq!(names(&report, "updated"), ["CLAUDE.md"]); |
| 365 | assert_eq!(read(&dir.join("GEMINI.md")), "# Gemini\n"); |
| 366 | let report = init_json(&dir, &[]); |
| 367 | assert_eq!(names(&report, "updated"), ["GEMINI.md"]); |
| 368 | assert_eq!(names(&report, "unchanged"), ["AGENTS.md", "CLAUDE.md"]); |
| 369 | assert_eq!(read(&dir.join("GEMINI.md")), "# Gemini\n@AGENTS.md\n"); |
| 370 | let said = ok(&dir, &["agents", "init"]); |
| 371 | assert!( |
| 372 | said.contains("GEMINI.md includes AGENTS.md already"), |
| 373 | "{said}" |
| 374 | ); |
| 375 | } |
| 376 | |
| 377 | #[cfg(unix)] |
| 378 | #[test] |
| 379 | fn links_inside_the_project_are_followed() { |
| 380 | let (_temp, dir) = project(); |
| 381 | std::os::unix::fs::symlink("AGENTS.md", dir.join("CLAUDE.md")).unwrap(); |
| 382 | let report = init_json(&dir, &[]); |
| 383 | assert_eq!(names(&report, "created"), ["AGENTS.md"]); |
| 384 | assert_eq!(names(&report, "unchanged"), ["CLAUDE.md"]); |
| 385 | assert_eq!(report["paths"]["CLAUDE.md"], "AGENTS.md"); |
| 386 | assert!(!read(&dir.join("AGENTS.md")).contains("@AGENTS.md")); |
| 387 | assert!( |
| 388 | fs::symlink_metadata(dir.join("CLAUDE.md")) |
| 389 | .unwrap() |
| 390 | .file_type() |
| 391 | .is_symlink() |
| 392 | ); |
| 393 | let report = init_json(&dir, &[]); |
| 394 | assert_eq!(names(&report, "unchanged"), ["AGENTS.md", "CLAUDE.md"]); |
| 395 | |
| 396 | // AGENTS.md may be the link, to a file elsewhere in the project: that |
| 397 | // file gets the section, and the JSON names it. |
| 398 | let (_other, linked) = project(); |
| 399 | fs::create_dir(linked.join("docs")).unwrap(); |
| 400 | fs::write(linked.join("docs/rules.md"), "# Shared rules\n").unwrap(); |
| 401 | std::os::unix::fs::symlink("docs/rules.md", linked.join("AGENTS.md")).unwrap(); |
| 402 | let report = init_json(&linked, &[]); |
| 403 | assert_eq!(names(&report, "updated"), ["AGENTS.md"]); |
| 404 | assert_eq!(report["paths"]["AGENTS.md"], "docs/rules.md"); |
| 405 | let link = fs::symlink_metadata(linked.join("AGENTS.md")).unwrap(); |
| 406 | assert!(link.file_type().is_symlink()); |
| 407 | assert!( |
| 408 | read(&linked.join("docs/rules.md")).starts_with("# Shared rules\n\n<!-- braid:start -->\n") |
| 409 | ); |
| 410 | let said = ok(&linked, &["agents", "init"]); |
| 411 | assert!( |
| 412 | said.contains( |
| 413 | "AGENTS.md (docs/rules.md, which it links to) has this braid section already" |
| 414 | ), |
| 415 | "{said}" |
| 416 | ); |
| 417 | let left: Vec<_> = fs::read_dir(linked.join("docs")).unwrap().collect(); |
| 418 | assert_eq!(left.len(), 1, "no temporary file is left"); |
| 419 | |
| 420 | // The layout of projects that started with Claude Code: AGENTS.md a |
| 421 | // link to CLAUDE.md, which holds the section itself. |
| 422 | let (_third, claude) = project(); |
| 423 | fs::write(claude.join("CLAUDE.md"), "# Claude rules\n").unwrap(); |
| 424 | std::os::unix::fs::symlink("CLAUDE.md", claude.join("AGENTS.md")).unwrap(); |
| 425 | let report = init_json(&claude, &[]); |
| 426 | assert_eq!(names(&report, "updated"), ["AGENTS.md"]); |
| 427 | assert_eq!(names(&report, "unchanged"), ["CLAUDE.md"]); |
| 428 | let text = read(&claude.join("CLAUDE.md")); |
| 429 | assert!( |
| 430 | text.starts_with("# Claude rules\n\n<!-- braid:start -->\n"), |
| 431 | "{text}" |
| 432 | ); |
| 433 | assert!(!text.contains("@AGENTS.md"), "{text}"); |
| 434 | assert_eq!( |
| 435 | names(&init_json(&claude, &[]), "unchanged"), |
| 436 | ["AGENTS.md", "CLAUDE.md"] |
| 437 | ); |
| 438 | } |
| 439 | |
| 440 | #[cfg(unix)] |
| 441 | #[test] |
| 442 | fn files_braid_must_not_write_are_refused_in_a_dry_run_too() { |
| 443 | use std::os::unix::fs::{PermissionsExt, symlink}; |
| 444 | |
| 445 | let both = |dir: &Path, said: &str| { |
| 446 | for args in [&["agents", "init", "--dry-run"][..], &["agents", "init"]] { |
| 447 | let err = fails(dir, args); |
| 448 | assert!(err.contains(said), "{args:?}: {err}"); |
| 449 | assert!(err.contains("nothing was written"), "{args:?}: {err}"); |
| 450 | } |
| 451 | }; |
| 452 | |
| 453 | // A link outside the project: other projects may read that file. |
| 454 | let (_shared, shared) = temp(); |
| 455 | fs::write(shared.join("CLAUDE.md"), "# Shared notes\n").unwrap(); |
| 456 | let (_project, dir) = project(); |
| 457 | symlink(shared.join("CLAUDE.md"), dir.join("CLAUDE.md")).unwrap(); |
| 458 | both(&dir, "CLAUDE.md is a link to"); |
| 459 | assert_eq!(read(&shared.join("CLAUDE.md")), "# Shared notes\n"); |
| 460 | assert!(!dir.join("AGENTS.md").exists()); |
| 461 | ok(&dir, &["agents", "init", "--no-claude"]); |
| 462 | fs::remove_file(dir.join("AGENTS.md")).unwrap(); |
| 463 | |
| 464 | // A link to a file that does not exist fails the dry run as it fails |
| 465 | // the real run. |
| 466 | symlink("docs/rules.md", dir.join("AGENTS.md")).unwrap(); |
| 467 | both(&dir, "which does not exist"); |
| 468 | fs::remove_file(dir.join("AGENTS.md")).unwrap(); |
| 469 | |
| 470 | // A FIFO is never opened: the dry run does not wait on it. |
| 471 | let fifo = dir.join("AGENTS.md"); |
| 472 | let made = Command::new("mkfifo").arg(&fifo).status(); |
| 473 | if made.is_ok_and(|status| status.success()) { |
| 474 | both(&dir, "is not a regular file"); |
| 475 | fs::remove_file(&fifo).unwrap(); |
| 476 | } |
| 477 | |
| 478 | // A read-only file is left as it is. |
| 479 | fs::write(dir.join("AGENTS.md"), "# Rules\n").unwrap(); |
| 480 | fs::set_permissions(dir.join("AGENTS.md"), fs::Permissions::from_mode(0o444)).unwrap(); |
| 481 | both(&dir, "AGENTS.md is read-only"); |
| 482 | assert_eq!(read(&dir.join("AGENTS.md")), "# Rules\n"); |
| 483 | fs::set_permissions(dir.join("AGENTS.md"), fs::Permissions::from_mode(0o644)).unwrap(); |
| 484 | |
| 485 | // A file with a hard link elsewhere would change under that name too. |
| 486 | fs::hard_link(dir.join("AGENTS.md"), shared.join("AGENTS.md")).unwrap(); |
| 487 | both(&dir, "has hard links"); |
| 488 | assert_eq!(read(&shared.join("AGENTS.md")), "# Rules\n"); |
| 489 | } |
| 490 | |
| 491 | #[cfg(unix)] |
| 492 | #[test] |
| 493 | fn hard_linked_guides_stay_one_file() { |
| 494 | use std::os::unix::fs::MetadataExt; |
| 495 | |
| 496 | let (_temp, dir) = project(); |
| 497 | fs::write( |
| 498 | dir.join("AGENTS.md"), |
| 499 | "# Shared rules for every agent\n\nRun the tests.\n", |
| 500 | ) |
| 501 | .unwrap(); |
| 502 | fs::hard_link(dir.join("AGENTS.md"), dir.join("CLAUDE.md")).unwrap(); |
| 503 | let report = init_json(&dir, &[]); |
| 504 | assert_eq!(names(&report, "updated"), ["AGENTS.md"]); |
| 505 | assert_eq!(names(&report, "unchanged"), ["CLAUDE.md"]); |
| 506 | let (agents, claude) = ( |
| 507 | fs::metadata(dir.join("AGENTS.md")).unwrap(), |
| 508 | fs::metadata(dir.join("CLAUDE.md")).unwrap(), |
| 509 | ); |
| 510 | assert_eq!(agents.ino(), claude.ino()); |
| 511 | assert_eq!(agents.nlink(), 2); |
| 512 | let text = read(&dir.join("CLAUDE.md")); |
| 513 | assert!(text.contains(START), "{text}"); |
| 514 | assert!(!text.contains("@AGENTS.md"), "{text}"); |
| 515 | assert_eq!( |
| 516 | names(&init_json(&dir, &[]), "unchanged"), |
| 517 | ["AGENTS.md", "CLAUDE.md"] |
| 518 | ); |
| 519 | } |
| 520 | |
| 521 | #[cfg(unix)] |
| 522 | #[test] |
| 523 | fn a_failed_write_leaves_every_file_as_it_was() { |
| 524 | use std::os::unix::fs::{PermissionsExt, symlink}; |
| 525 | |
| 526 | let (_temp, dir) = project(); |
| 527 | let sub = dir.join("sub"); |
| 528 | fs::create_dir(&sub).unwrap(); |
| 529 | fs::write(sub.join("CLAUDE.md"), "# Claude\n").unwrap(); |
| 530 | symlink("sub/CLAUDE.md", dir.join("CLAUDE.md")).unwrap(); |
| 531 | fs::set_permissions(&sub, fs::Permissions::from_mode(0o555)).unwrap(); |
| 532 | // Where permissions do not bind, as for root, there is nothing to test. |
| 533 | let binding = fs::write(sub.join("probe"), "").is_err(); |
| 534 | if binding { |
| 535 | let err = fails(&dir, &["agents", "init", "--json"]); |
| 536 | assert!(err.contains("cannot write sub/CLAUDE.md"), "{err}"); |
| 537 | assert!(err.contains("nothing was written"), "{err}"); |
| 538 | assert!(!dir.join("AGENTS.md").exists()); |
| 539 | assert_eq!(read(&sub.join("CLAUDE.md")), "# Claude\n"); |
| 540 | let left: Vec<_> = fs::read_dir(&dir) |
| 541 | .unwrap() |
| 542 | .map(|entry| entry.unwrap().file_name()) |
| 543 | .filter(|name| name.to_string_lossy().contains(".braid-")) |
| 544 | .collect(); |
| 545 | assert!(left.is_empty(), "{left:?}"); |
| 546 | } |
| 547 | fs::set_permissions(&sub, fs::Permissions::from_mode(0o755)).unwrap(); |
| 548 | } |
| 549 | |
| 550 | #[test] |
| 551 | fn files_that_are_not_utf8_are_refused_and_left_as_they_are() { |
| 552 | let (_temp, dir) = project(); |
| 553 | let latin1 = b"# R\xe8gles\n".to_vec(); |
| 554 | fs::write(dir.join("AGENTS.md"), &latin1).unwrap(); |
| 555 | fs::write(dir.join("CLAUDE.md"), "# Claude\n").unwrap(); |
| 556 | let err = fails(&dir, &["agents", "init"]); |
| 557 | assert!( |
| 558 | err.contains("AGENTS.md is not UTF-8 text: nothing was written"), |
| 559 | "{err}" |
| 560 | ); |
| 561 | assert_eq!(fs::read(dir.join("AGENTS.md")).unwrap(), latin1); |
| 562 | assert_eq!(read(&dir.join("CLAUDE.md")), "# Claude\n"); |
| 563 | |
| 564 | fs::remove_file(dir.join("AGENTS.md")).unwrap(); |
| 565 | fs::write(dir.join("CLAUDE.md"), &latin1).unwrap(); |
| 566 | let err = fails(&dir, &["agents", "init"]); |
| 567 | assert!( |
| 568 | err.contains("CLAUDE.md is not UTF-8 text: nothing was written"), |
| 569 | "{err}" |
| 570 | ); |
| 571 | assert!(!dir.join("AGENTS.md").exists()); |
| 572 | assert_eq!(fs::read(dir.join("CLAUDE.md")).unwrap(), latin1); |
| 573 | } |
| 574 | |
| 575 | #[test] |
| 576 | fn a_dry_run_says_what_would_change_and_writes_nothing() { |
| 577 | let (_temp, dir) = project(); |
| 578 | fs::write(dir.join("CLAUDE.md"), "# Claude\n").unwrap(); |
| 579 | let report = init_json(&dir, &["--dry-run"]); |
| 580 | assert_eq!(report["dry_run"], true); |
| 581 | assert_eq!(names(&report, "created"), ["AGENTS.md"]); |
| 582 | assert_eq!(names(&report, "updated"), ["CLAUDE.md"]); |
| 583 | assert!(!dir.join("AGENTS.md").exists()); |
| 584 | assert_eq!(read(&dir.join("CLAUDE.md")), "# Claude\n"); |
| 585 | |
| 586 | let said = ok(&dir, &["agents", "init", "--dry-run"]); |
| 587 | assert!(said.contains("Would create AGENTS.md"), "{said}"); |
| 588 | assert!(said.contains("Would add @AGENTS.md to CLAUDE.md"), "{said}"); |
| 589 | assert!(said.contains("Nothing was written."), "{said}"); |
| 590 | assert!(said.ends_with(&format!("{END}\n")), "{said}"); |
| 591 | assert!(said.contains(START)); |
| 592 | assert!(!dir.join("AGENTS.md").exists()); |
| 593 | ok(&dir, &["agents", "init"]); |
| 594 | let said = ok(&dir, &["agents", "init", "--dry-run"]); |
| 595 | assert!(!said.contains(START), "{said}"); |
| 596 | } |
| 597 | |
| 598 | #[test] |
| 599 | fn the_section_names_the_branch_agents_land_on() { |
| 600 | let (_temp, dir) = temp(); |
| 601 | repository_with_branch(&dir, "master"); |
| 602 | let report = init_json(&dir, &["--dry-run"]); |
| 603 | assert_eq!(report["branch"], "master"); |
| 604 | assert_eq!(report["repository"], dir.to_str().unwrap()); |
| 605 | |
| 606 | let (_other, trunk) = temp(); |
| 607 | repository_with_branch(&trunk, "trunk"); |
| 608 | let report = init_json(&trunk, &[]); |
| 609 | assert_eq!(report["branch"], "trunk"); |
| 610 | let text = read(&trunk.join("AGENTS.md")); |
| 611 | assert!(text.contains("--track trunk\n"), "{text}"); |
| 612 | assert!(text.contains("--branch trunk\n"), "{text}"); |
| 613 | ok(&trunk, &["ref", "set", "main", "trunk"]); |
| 614 | let report = init_json(&trunk, &[]); |
| 615 | assert_eq!(report["branch"], "trunk"); |
| 616 | assert_eq!(names(&report, "unchanged"), ["AGENTS.md"]); |
| 617 | let report = init_json(&trunk, &["--branch", "main"]); |
| 618 | assert_eq!(report["branch"], "main"); |
| 619 | assert_eq!(report["previous_branch"], "trunk"); |
| 620 | assert_eq!(names(&report, "updated"), ["AGENTS.md"]); |
| 621 | assert!(read(&trunk.join("AGENTS.md")).contains("--track main\n")); |
| 622 | |
| 623 | let (_third, two) = temp(); |
| 624 | repository_with_branch(&two, "trunk"); |
| 625 | ok(&two, &["ref", "set", "develop", "trunk"]); |
| 626 | let err = fails(&two, &["agents", "init"]); |
| 627 | assert!(err.contains("develop, trunk; pass --branch"), "{err}"); |
| 628 | assert!(!two.join("AGENTS.md").exists()); |
| 629 | for bad in ["two words", "tags/v1", "braid/git-map"] { |
| 630 | fails(&two, &["agents", "init", "--branch", bad]); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | #[test] |
| 635 | fn a_branch_the_repository_does_not_have_is_refused() { |
| 636 | let (_temp, dir) = project(); |
| 637 | let err = fails(&dir, &["agents", "init", "--branch", "mian", "--dry-run"]); |
| 638 | assert!(err.contains("has no branch \"mian\"; it has main"), "{err}"); |
| 639 | fails(&dir, &["agents", "init", "--branch", "mian"]); |
| 640 | assert!(!dir.join("AGENTS.md").exists()); |
| 641 | } |
| 642 | |
| 643 | #[test] |
| 644 | fn branch_names_that_shell_commands_would_misread_are_refused() { |
| 645 | let (_temp, dir) = temp(); |
| 646 | repository_with_branch(&dir, "feature/x.y_z-1"); |
| 647 | let report = init_json(&dir, &[]); |
| 648 | assert_eq!(report["branch"], "feature/x.y_z-1"); |
| 649 | let text = read(&dir.join("AGENTS.md")); |
| 650 | assert!(text.contains("--track feature/x.y_z-1\n"), "{text}"); |
| 651 | // It reads back exactly: a second run changes nothing. |
| 652 | assert_eq!(names(&init_json(&dir, &[]), "unchanged"), ["AGENTS.md"]); |
| 653 | |
| 654 | for bad in [ |
| 655 | "feat(x)", |
| 656 | "a;b", |
| 657 | "a;touch${IFS}pwned", |
| 658 | "q$(id)", |
| 659 | "x`id`", |
| 660 | "it's", |
| 661 | "a|b", |
| 662 | "a>b", |
| 663 | "-x", |
| 664 | "--no-claude", |
| 665 | ] { |
| 666 | let flag = format!("--branch={bad}"); |
| 667 | let err = fails(&dir, &["agents", "init", "--dry-run", &flag]); |
| 668 | assert!( |
| 669 | err.contains("cannot name the branch agents land on"), |
| 670 | "{bad}: {err}" |
| 671 | ); |
| 672 | } |
| 673 | assert_eq!(read(&dir.join("AGENTS.md")), text); |
| 674 | } |
| 675 | |
| 676 | #[test] |
| 677 | fn a_branch_named_before_the_project_was_imported_is_chosen_again() { |
| 678 | // A project whose only branch is master, set up out of order. |
| 679 | let (_temp, dir) = temp(); |
| 680 | git(&dir, &["init", "--quiet", "-b", "master"]); |
| 681 | fs::write(dir.join("README.md"), "# Project\n").unwrap(); |
| 682 | git(&dir, &["add", "README.md"]); |
| 683 | git(&dir, &["commit", "--quiet", "-m", "First"]); |
| 684 | |
| 685 | // Before braid is set up, there is no branch to name. |
| 686 | let err = fails(&dir, &["agents", "init"]); |
| 687 | assert!(err.contains("--branch"), "{err}"); |
| 688 | assert!(!dir.join("AGENTS.md").exists()); |
| 689 | ok(&dir, &["init"]); |
| 690 | let err = fails(&dir, &["agents", "init"]); |
| 691 | assert!(err.contains("no branch yet"), "{err}"); |
| 692 | assert!(err.contains("braid git import ."), "{err}"); |
| 693 | // A guess written anyway, before the import... |
| 694 | let report = init_json(&dir, &["--branch", "main"]); |
| 695 | assert_eq!(report["branch"], "main"); |
| 696 | |
| 697 | // ...gives way, once the history is in, to the branch it has. |
| 698 | ok(&dir, &["git", "import", "."]); |
| 699 | let said = ok(&dir, &["agents", "init", "--dry-run"]); |
| 700 | assert!( |
| 701 | said.contains( |
| 702 | "The section named main, which the repository does not have: it would name master now." |
| 703 | ), |
| 704 | "{said}" |
| 705 | ); |
| 706 | let report = init_json(&dir, &[]); |
| 707 | assert_eq!(report["branch"], "master"); |
| 708 | assert_eq!(report["previous_branch"], "main"); |
| 709 | assert_eq!(names(&report, "updated"), ["AGENTS.md"]); |
| 710 | let text = read(&dir.join("AGENTS.md")); |
| 711 | assert!(text.contains("--track master\n"), "{text}"); |
| 712 | assert!(!text.contains("--track main"), "{text}"); |
| 713 | let report = init_json(&dir, &[]); |
| 714 | assert_eq!(names(&report, "unchanged"), ["AGENTS.md"]); |
| 715 | assert_eq!(report["previous_branch"], Value::Null); |
| 716 | } |
| 717 | |
| 718 | #[test] |
| 719 | fn it_writes_at_the_root_of_the_repository_or_of_the_workspace() { |
| 720 | let (_temp, dir) = project(); |
| 721 | let deep = dir.join("src/deep"); |
| 722 | fs::create_dir_all(&deep).unwrap(); |
| 723 | let report = init_json(&deep, &[]); |
| 724 | assert_eq!(report["dir"], dir.to_str().unwrap()); |
| 725 | assert!(dir.join("AGENTS.md").is_file()); |
| 726 | assert!(!deep.join("AGENTS.md").exists()); |
| 727 | |
| 728 | ok( |
| 729 | &dir, |
| 730 | &[ |
| 731 | "workspace", |
| 732 | "add", |
| 733 | "agent-1", |
| 734 | "--track", |
| 735 | "release", |
| 736 | "--new-branch", |
| 737 | ], |
| 738 | ); |
| 739 | let workspace = dir.join(".braid/work/agent-1"); |
| 740 | let inside = workspace.join("docs"); |
| 741 | fs::create_dir_all(&inside).unwrap(); |
| 742 | let report = init_json(&inside, &[]); |
| 743 | assert_eq!(report["dir"], workspace.to_str().unwrap()); |
| 744 | assert_eq!(report["branch"], "release"); |
| 745 | assert_eq!(report["repository"], dir.to_str().unwrap()); |
| 746 | assert!(read(&workspace.join("AGENTS.md")).contains("--track release\n")); |
| 747 | let status: Value = serde_json::from_str(&ok(&workspace, &["status", "--json"])).unwrap(); |
| 748 | assert_eq!(status["added"], serde_json::json!(["AGENTS.md"])); |
| 749 | |
| 750 | let (_other, elsewhere) = temp(); |
| 751 | let report = init_json(&elsewhere, &["--dir", dir.join("src").to_str().unwrap()]); |
| 752 | assert_eq!(report["dir"], dir.join("src").to_str().unwrap()); |
| 753 | assert_eq!(report["branch"], "main"); |
| 754 | assert!(dir.join("src/AGENTS.md").is_file()); |
| 755 | fails(&elsewhere, &["agents", "init", "--dir", "no-such-dir"]); |
| 756 | } |
| 757 | |
| 758 | #[test] |
| 759 | fn workspace_and_repo_say_where_to_write() { |
| 760 | let (_temp, dir) = project(); |
| 761 | ok(&dir, &["workspace", "add", "agent-1"]); |
| 762 | let workspace = dir.join(".braid/work/agent-1"); |
| 763 | |
| 764 | // From the project root, --workspace writes in that workspace, as the |
| 765 | // section's commands run from the root do. |
| 766 | let report = init_json(&dir, &["--workspace", "agent-1"]); |
| 767 | assert_eq!(report["dir"], workspace.to_str().unwrap()); |
| 768 | assert_eq!(report["branch"], "main"); |
| 769 | assert_eq!(names(&report, "created"), ["AGENTS.md"]); |
| 770 | assert!(workspace.join("AGENTS.md").is_file()); |
| 771 | assert!(!dir.join("AGENTS.md").exists()); |
| 772 | let err = fails( |
| 773 | &dir, |
| 774 | &["--workspace", "nope", "agents", "init", "--dry-run"], |
| 775 | ); |
| 776 | assert!(err.contains("no workspace named \"nope\""), "{err}"); |
| 777 | let err = fails( |
| 778 | &dir, |
| 779 | &["--workspace", "agent-1", "agents", "init", "--dir", "."], |
| 780 | ); |
| 781 | assert!(err.contains("give one of them"), "{err}"); |
| 782 | |
| 783 | // From outside the repository, --repo writes at its root. |
| 784 | let (_other, elsewhere) = temp(); |
| 785 | let report = init_json(&elsewhere, &["--repo", dir.to_str().unwrap(), "--dry-run"]); |
| 786 | assert_eq!(report["dir"], dir.to_str().unwrap()); |
| 787 | assert_eq!(report["repository"], dir.to_str().unwrap()); |
| 788 | assert_eq!(names(&report, "created"), ["AGENTS.md"]); |
| 789 | ok( |
| 790 | &elsewhere, |
| 791 | &["--repo", dir.to_str().unwrap(), "agents", "init"], |
| 792 | ); |
| 793 | assert!(dir.join("AGENTS.md").is_file()); |
| 794 | assert!(!elsewhere.join("AGENTS.md").exists()); |
| 795 | } |