| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! End-to-end tests of the `braid` binary. |
| 5 | |
| 6 | use std::io::Write; |
| 7 | use std::process::{Command, Stdio}; |
| 8 | |
| 9 | use braid_format::{Kind, ObjectId}; |
| 10 | |
| 11 | fn braid() -> Command { |
| 12 | Command::new(env!("CARGO_BIN_EXE_braid")) |
| 13 | } |
| 14 | |
| 15 | #[test] |
| 16 | fn version_prints_name_and_version() { |
| 17 | let out = braid().arg("--version").output().unwrap(); |
| 18 | assert!(out.status.success()); |
| 19 | let text = String::from_utf8(out.stdout).unwrap(); |
| 20 | assert_eq!(text.trim(), format!("braid {}", env!("CARGO_PKG_VERSION"))); |
| 21 | } |
| 22 | |
| 23 | #[test] |
| 24 | fn hash_object_matches_the_library() { |
| 25 | let dir = tempfile::tempdir().unwrap(); |
| 26 | let path = dir.path().join("hello.txt"); |
| 27 | std::fs::write(&path, b"hello\n").unwrap(); |
| 28 | |
| 29 | let out = braid().arg("hash-object").arg(&path).output().unwrap(); |
| 30 | assert!(out.status.success()); |
| 31 | let text = String::from_utf8(out.stdout).unwrap(); |
| 32 | let expected = ObjectId::hash(Kind::Blob, b"hello\n"); |
| 33 | assert_eq!(text, format!("{expected} {}\n", path.display())); |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn hash_object_json() { |
| 38 | let dir = tempfile::tempdir().unwrap(); |
| 39 | let path = dir.path().join("big.bin"); |
| 40 | let payload: Vec<u8> = (0..1_000_000u32).map(|i| (i % 253) as u8).collect(); |
| 41 | std::fs::write(&path, &payload).unwrap(); |
| 42 | |
| 43 | let out = braid() |
| 44 | .args(["hash-object", "--json"]) |
| 45 | .arg(&path) |
| 46 | .output() |
| 47 | .unwrap(); |
| 48 | assert!(out.status.success()); |
| 49 | let value: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap(); |
| 50 | let item = &value[0]; |
| 51 | assert_eq!(item["kind"], "blob"); |
| 52 | assert_eq!(item["size"], payload.len() as u64); |
| 53 | assert_eq!( |
| 54 | item["id"], |
| 55 | ObjectId::hash(Kind::Blob, &payload).to_hex().as_str() |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn hash_object_reads_standard_input() { |
| 61 | let mut child = braid() |
| 62 | .args(["hash-object", "-"]) |
| 63 | .stdin(Stdio::piped()) |
| 64 | .stdout(Stdio::piped()) |
| 65 | .spawn() |
| 66 | .unwrap(); |
| 67 | child.stdin.take().unwrap().write_all(b"hello\n").unwrap(); |
| 68 | let out = child.wait_with_output().unwrap(); |
| 69 | assert!(out.status.success()); |
| 70 | let text = String::from_utf8(out.stdout).unwrap(); |
| 71 | let expected = ObjectId::hash(Kind::Blob, b"hello\n"); |
| 72 | assert_eq!(text, format!("{expected} -\n")); |
| 73 | } |
| 74 | |
| 75 | #[test] |
| 76 | fn missing_file_is_an_error() { |
| 77 | let dir = tempfile::tempdir().unwrap(); |
| 78 | let out = braid() |
| 79 | .arg("hash-object") |
| 80 | .arg(dir.path().join("does-not-exist")) |
| 81 | .output() |
| 82 | .unwrap(); |
| 83 | assert!(!out.status.success()); |
| 84 | let err = String::from_utf8(out.stderr).unwrap(); |
| 85 | assert!(err.starts_with("braid: error: cannot open"), "{err}"); |
| 86 | } |
| 87 | |
| 88 | fn braid_in(dir: &std::path::Path) -> Command { |
| 89 | let mut command = braid(); |
| 90 | command.arg("--repo").arg(dir).env("BRAID_AGENT", "tester"); |
| 91 | command |
| 92 | } |
| 93 | |
| 94 | fn run_json(command: &mut Command) -> serde_json::Value { |
| 95 | let out = command.arg("--json").output().unwrap(); |
| 96 | assert!( |
| 97 | out.status.success(), |
| 98 | "{}", |
| 99 | String::from_utf8_lossy(&out.stderr) |
| 100 | ); |
| 101 | serde_json::from_slice(&out.stdout).unwrap() |
| 102 | } |
| 103 | |
| 104 | #[test] |
| 105 | fn repository_round_trip() { |
| 106 | let dir = tempfile::tempdir().unwrap(); |
| 107 | let init = braid().arg("init").arg(dir.path()).output().unwrap(); |
| 108 | assert!(init.status.success()); |
| 109 | // A second init in the same place is refused. |
| 110 | assert!( |
| 111 | !braid() |
| 112 | .arg("init") |
| 113 | .arg(dir.path()) |
| 114 | .output() |
| 115 | .unwrap() |
| 116 | .status |
| 117 | .success() |
| 118 | ); |
| 119 | |
| 120 | let file = dir.path().join("note.txt"); |
| 121 | std::fs::write(&file, b"hello\n").unwrap(); |
| 122 | let written = run_json(braid_in(dir.path()).args(["hash-object", "-w"]).arg(&file)); |
| 123 | let id = written[0]["id"].as_str().unwrap().to_owned(); |
| 124 | assert_eq!(id, ObjectId::hash(Kind::Blob, b"hello\n").to_hex()); |
| 125 | |
| 126 | let cat = braid_in(dir.path()) |
| 127 | .args(["cat-object", &id]) |
| 128 | .output() |
| 129 | .unwrap(); |
| 130 | assert_eq!(cat.stdout, b"hello\n"); |
| 131 | |
| 132 | // Refs: create with a compare-and-swap, refuse a stale expectation. |
| 133 | let set = run_json(braid_in(dir.path()).args(["ref", "set", "main", &id, "--expect", "none"])); |
| 134 | assert_eq!(set["agent"], "tester"); |
| 135 | assert_eq!(set["updates"][0]["new"], id.as_str()); |
| 136 | let stale = braid_in(dir.path()) |
| 137 | .args(["ref", "set", "main", &id, "--expect", "none"]) |
| 138 | .output() |
| 139 | .unwrap(); |
| 140 | assert!(!stale.status.success()); |
| 141 | |
| 142 | let refs = run_json(braid_in(dir.path()).arg("refs")); |
| 143 | assert_eq!(refs["refs"]["main"], id.as_str()); |
| 144 | let by_name = braid_in(dir.path()) |
| 145 | .args(["cat-object", "main"]) |
| 146 | .output() |
| 147 | .unwrap(); |
| 148 | assert_eq!(by_name.stdout, b"hello\n"); |
| 149 | |
| 150 | // The op log shows it, undo reverts it, and the store stays clean. |
| 151 | let log = run_json(braid_in(dir.path()).args(["op", "log"])); |
| 152 | assert_eq!(log.as_array().unwrap().len(), 1); |
| 153 | let undone = run_json(braid_in(dir.path()).arg("undo")); |
| 154 | assert_eq!(undone["undoes"], 1); |
| 155 | let refs = run_json(braid_in(dir.path()).arg("refs")); |
| 156 | assert!(refs["refs"].as_object().unwrap().is_empty()); |
| 157 | let nothing = braid_in(dir.path()).arg("undo").output().unwrap(); |
| 158 | assert!(!nothing.status.success()); |
| 159 | |
| 160 | let fsck = run_json(braid_in(dir.path()).arg("fsck")); |
| 161 | assert_eq!(fsck["clean"], true); |
| 162 | assert_eq!(fsck["objects"], 1); |
| 163 | } |
| 164 | |
| 165 | /// Two blob contents whose ids start with the same six hex digits or more. |
| 166 | fn blob_twins() -> (Vec<u8>, Vec<u8>) { |
| 167 | let mut seen = std::collections::HashMap::new(); |
| 168 | for n in 0u64.. { |
| 169 | let content = format!("twin {n}\n").into_bytes(); |
| 170 | let id = ObjectId::hash(Kind::Blob, &content); |
| 171 | let start = id.to_hex()[..6].to_owned(); |
| 172 | if let Some(other) = seen.insert(start, content.clone()) { |
| 173 | return (other, content); |
| 174 | } |
| 175 | } |
| 176 | unreachable!("the search ends") |
| 177 | } |
| 178 | |
| 179 | #[test] |
| 180 | fn short_ids_and_an_ambiguous_one() { |
| 181 | let dir = tempfile::tempdir().unwrap(); |
| 182 | assert!( |
| 183 | braid() |
| 184 | .arg("init") |
| 185 | .arg(dir.path()) |
| 186 | .output() |
| 187 | .unwrap() |
| 188 | .status |
| 189 | .success() |
| 190 | ); |
| 191 | let (one, two) = blob_twins(); |
| 192 | let files = [dir.path().join("one"), dir.path().join("two")]; |
| 193 | std::fs::write(&files[0], &one).unwrap(); |
| 194 | std::fs::write(&files[1], &two).unwrap(); |
| 195 | let written = run_json( |
| 196 | braid_in(dir.path()) |
| 197 | .args(["hash-object", "-w"]) |
| 198 | .args(&files), |
| 199 | ); |
| 200 | let ids: Vec<String> = (0..2) |
| 201 | .map(|i| written[i]["id"].as_str().unwrap().to_owned()) |
| 202 | .collect(); |
| 203 | let shared = ids[0] |
| 204 | .bytes() |
| 205 | .zip(ids[1].bytes()) |
| 206 | .take_while(|(a, b)| a == b) |
| 207 | .count(); |
| 208 | assert!(shared >= 6); |
| 209 | |
| 210 | // Six digits name both: an error that lists them, and nothing printed. |
| 211 | let out = braid_in(dir.path()) |
| 212 | .args(["cat-object", &ids[0][..6]]) |
| 213 | .output() |
| 214 | .unwrap(); |
| 215 | assert_eq!(out.status.code(), Some(1)); |
| 216 | assert!(out.stdout.is_empty()); |
| 217 | let err = String::from_utf8(out.stderr).unwrap(); |
| 218 | assert!(err.contains("ambiguous"), "{err}"); |
| 219 | for id in &ids { |
| 220 | assert!(err.contains(&id[..12]), "{err}"); |
| 221 | } |
| 222 | |
| 223 | // One digit more names one of them, wherever a revision is taken. |
| 224 | let unique: Vec<&str> = ids.iter().map(|id| &id[..shared + 1]).collect(); |
| 225 | let cat = braid_in(dir.path()) |
| 226 | .args(["cat-object", unique[1]]) |
| 227 | .output() |
| 228 | .unwrap(); |
| 229 | assert_eq!(cat.stdout, two); |
| 230 | let cat = run_json(braid_in(dir.path()).args(["cat-object", unique[0]])); |
| 231 | assert_eq!(cat["id"], ids[0].as_str()); |
| 232 | let set = run_json(braid_in(dir.path()).args(["ref", "set", "pinned", unique[0]])); |
| 233 | assert_eq!(set["updates"][0]["new"], ids[0].as_str()); |
| 234 | // A ref spelled like a short id wins over it. |
| 235 | run_json(braid_in(dir.path()).args(["ref", "set", &ids[1][..6], unique[1]])); |
| 236 | let cat = braid_in(dir.path()) |
| 237 | .args(["cat-object", &ids[1][..6]]) |
| 238 | .output() |
| 239 | .unwrap(); |
| 240 | assert_eq!(cat.stdout, two); |
| 241 | // A short id is no changeset: show says what it is. |
| 242 | let out = braid_in(dir.path()) |
| 243 | .args(["show", unique[0]]) |
| 244 | .output() |
| 245 | .unwrap(); |
| 246 | let err = String::from_utf8(out.stderr).unwrap(); |
| 247 | assert!(err.contains("is a blob, not a changeset"), "{err}"); |
| 248 | } |
| 249 | |
| 250 | #[test] |
| 251 | fn signatures_carry_the_local_offset() { |
| 252 | let dir = tempfile::tempdir().unwrap(); |
| 253 | assert!( |
| 254 | braid() |
| 255 | .arg("init") |
| 256 | .arg(dir.path()) |
| 257 | .output() |
| 258 | .unwrap() |
| 259 | .status |
| 260 | .success() |
| 261 | ); |
| 262 | let file = dir.path().join("a.txt"); |
| 263 | std::fs::write(&file, b"a\n").unwrap(); |
| 264 | let set = format!("a.txt=@{}", file.display()); |
| 265 | // POSIX rules, UTC, and a zone that cannot be found, which reads as UTC. |
| 266 | for (zone, minutes, shown) in [ |
| 267 | ("<+0530>-05:30", 330, "+0530"), |
| 268 | ("<-03>3", -180, "-0300"), |
| 269 | ("UTC0", 0, "+0000"), |
| 270 | ("Nowhere/Nothing", 0, "+0000"), |
| 271 | ] { |
| 272 | let made = run_json( |
| 273 | braid_in(dir.path()) |
| 274 | .env("TZ", zone) |
| 275 | .env("BRAID_AUTHOR", "Ada <ada@example.org>") |
| 276 | .args(["change", "create", "--base", "none", "--set", &set]) |
| 277 | .args(["-m", zone]), |
| 278 | ); |
| 279 | let id = made["change"].as_str().unwrap(); |
| 280 | // JSON keeps the seconds and the offset. |
| 281 | let show = run_json(braid_in(dir.path()).args(["show", id])); |
| 282 | for who in ["author", "committer"] { |
| 283 | assert_eq!(show[who]["offset_minutes"], minutes, "{zone}"); |
| 284 | assert!(show[who]["seconds"].as_i64().unwrap() > 1_700_000_000); |
| 285 | let time = show[who]["time"].as_str().unwrap(); |
| 286 | assert!(time.ends_with(shown), "{zone}: {time}"); |
| 287 | } |
| 288 | // The log shows minutes, in the signature's own offset. |
| 289 | let log = braid_in(dir.path()) |
| 290 | .args(["log", id, "-n", "1"]) |
| 291 | .output() |
| 292 | .unwrap(); |
| 293 | let text = String::from_utf8(log.stdout).unwrap(); |
| 294 | let time = text.split(" ").nth(1).unwrap(); |
| 295 | assert_eq!(time.len(), "2026-10-15 21:41 +0530".len(), "{text}"); |
| 296 | assert!(time.ends_with(shown), "{zone}: {text}"); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | #[test] |
| 301 | fn commands_outside_a_repository_fail_clearly() { |
| 302 | let dir = tempfile::tempdir().unwrap(); |
| 303 | let out = braid() |
| 304 | .arg("--repo") |
| 305 | .arg(dir.path()) |
| 306 | .arg("refs") |
| 307 | .output() |
| 308 | .unwrap(); |
| 309 | assert!(!out.status.success()); |
| 310 | let err = String::from_utf8(out.stderr).unwrap(); |
| 311 | assert!(err.contains("not inside a braid repository"), "{err}"); |
| 312 | } |
| 313 | |
| 314 | /// Runs git with a fixed identity and dates, ignoring the user's config. |
| 315 | fn git(dir: &std::path::Path, args: &[&str]) -> String { |
| 316 | let out = Command::new("git") |
| 317 | .current_dir(dir) |
| 318 | .args(args) |
| 319 | .env("GIT_CONFIG_GLOBAL", "/dev/null") |
| 320 | .env("GIT_CONFIG_NOSYSTEM", "1") |
| 321 | .env("GIT_AUTHOR_NAME", "Ada") |
| 322 | .env("GIT_AUTHOR_EMAIL", "ada@example.org") |
| 323 | .env("GIT_COMMITTER_NAME", "Ada") |
| 324 | .env("GIT_COMMITTER_EMAIL", "ada@example.org") |
| 325 | .env("GIT_AUTHOR_DATE", "1700000000 +0100") |
| 326 | .env("GIT_COMMITTER_DATE", "1700000000 +0100") |
| 327 | .output() |
| 328 | .unwrap(); |
| 329 | assert!( |
| 330 | out.status.success(), |
| 331 | "git {args:?}: {}", |
| 332 | String::from_utf8_lossy(&out.stderr) |
| 333 | ); |
| 334 | String::from_utf8(out.stdout).unwrap() |
| 335 | } |
| 336 | |
| 337 | #[test] |
| 338 | fn git_import_log_show_export() { |
| 339 | let work = tempfile::tempdir().unwrap(); |
| 340 | let src = work.path().join("src"); |
| 341 | std::fs::create_dir(&src).unwrap(); |
| 342 | git(&src, &["init", "-q", "-b", "main"]); |
| 343 | std::fs::write(src.join("README.md"), "hello\n").unwrap(); |
| 344 | git(&src, &["add", "README.md"]); |
| 345 | git(&src, &["commit", "-q", "-m", "First"]); |
| 346 | std::fs::create_dir(src.join("src")).unwrap(); |
| 347 | std::fs::write(src.join("src/lib.rs"), "pub fn f() {}\n").unwrap(); |
| 348 | git(&src, &["add", "src/lib.rs"]); |
| 349 | git(&src, &["commit", "-q", "-m", "Second\n\nWith a body."]); |
| 350 | git(&src, &["tag", "-a", "v1", "-m", "Version 1"]); |
| 351 | |
| 352 | let repo = work.path().join("braid"); |
| 353 | assert!( |
| 354 | braid() |
| 355 | .arg("init") |
| 356 | .arg(&repo) |
| 357 | .output() |
| 358 | .unwrap() |
| 359 | .status |
| 360 | .success() |
| 361 | ); |
| 362 | let import = run_json(braid_in(&repo).args(["git", "import"]).arg(&src)); |
| 363 | assert_eq!(import["commits"], 2); |
| 364 | assert_eq!(import["tags"], 1); |
| 365 | // Importing again finds nothing new and moves no ref. |
| 366 | let again = run_json(braid_in(&repo).args(["git", "import"]).arg(&src)); |
| 367 | assert_eq!(again["commits"], 0); |
| 368 | assert!(again["operation"].is_null()); |
| 369 | |
| 370 | let log = run_json(braid_in(&repo).args(["log", "main"])); |
| 371 | let log = log.as_array().unwrap(); |
| 372 | assert_eq!(log.len(), 2); |
| 373 | assert_eq!(log[0]["message"], "Second\n\nWith a body.\n"); |
| 374 | assert_eq!(log[0]["author"]["email"], "ada@example.org"); |
| 375 | assert_eq!(log[0]["author"]["offset_minutes"], 60); |
| 376 | |
| 377 | let show = run_json(braid_in(&repo).args(["show", "main"])); |
| 378 | assert_eq!(show["changes"][0]["path"], "src/lib.rs"); |
| 379 | assert_eq!(show["changes"][0]["op"], "file"); |
| 380 | assert!(show["git_origin"].is_string()); |
| 381 | |
| 382 | // Export into an empty repository: the same commit and tag ids. |
| 383 | let mirror = work.path().join("mirror.git"); |
| 384 | std::fs::create_dir(&mirror).unwrap(); |
| 385 | git(&mirror, &["init", "-q", "--bare"]); |
| 386 | run_json(braid_in(&repo).args(["git", "export"]).arg(&mirror)); |
| 387 | for name in ["refs/heads/main", "refs/tags/v1"] { |
| 388 | assert_eq!( |
| 389 | git(&src, &["rev-parse", name]), |
| 390 | git(&mirror, &["rev-parse", name]), |
| 391 | "{name}" |
| 392 | ); |
| 393 | } |
| 394 | |
| 395 | // The import is one operation: undo takes every ref back. |
| 396 | run_json(braid_in(&repo).arg("undo")); |
| 397 | let refs = run_json(braid_in(&repo).arg("refs")); |
| 398 | assert!(refs["refs"].as_object().unwrap().is_empty()); |
| 399 | assert_eq!(run_json(braid_in(&repo).arg("fsck"))["clean"], true); |
| 400 | } |