| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! Three agents on one repository, end to end: agent-a and agent-b edit |
| 5 | //! the same line of a file, agent-c appends a line at its end, and they |
| 6 | //! land in the order c, a, b. c fast-forwards, a is rebased and merged |
| 7 | //! with c's line, b conflicts with a, brings the conflict in with `braid |
| 8 | //! update`, resolves it, commits and lands; then `braid git sync` pushes |
| 9 | //! main to a git remote. Every report is checked as an agent reads it: |
| 10 | //! the outcomes, the names of the conflict's sides, the paths each |
| 11 | //! working copy got, where each changeset landed, the op log, who made |
| 12 | //! what, and the file everyone ends with. |
| 13 | |
| 14 | use std::path::{Path, PathBuf}; |
| 15 | use std::process::{Command, Output}; |
| 16 | |
| 17 | use serde_json::{Value, json}; |
| 18 | |
| 19 | const SEED: &str = "title\nalpha\nbeta\ngamma\n"; |
| 20 | const FINAL: &str = "title\nalpha, from a and b\nbeta\ngamma\ndelta, from c\n"; |
| 21 | |
| 22 | /// Git without the user's or the system's configuration. |
| 23 | const GIT_ENV: [(&str, &str); 6] = [ |
| 24 | ("GIT_CONFIG_GLOBAL", "/dev/null"), |
| 25 | ("GIT_CONFIG_NOSYSTEM", "1"), |
| 26 | ("GIT_AUTHOR_NAME", "Ada Lovelace"), |
| 27 | ("GIT_AUTHOR_EMAIL", "ada@example.org"), |
| 28 | ("GIT_COMMITTER_NAME", "Ada Lovelace"), |
| 29 | ("GIT_COMMITTER_EMAIL", "ada@example.org"), |
| 30 | ]; |
| 31 | |
| 32 | /// Runs braid in `dir` as `agent`. |
| 33 | fn braid(agent: &str, dir: &Path, args: &[&str]) -> Output { |
| 34 | Command::new(env!("CARGO_BIN_EXE_braid")) |
| 35 | .current_dir(dir) |
| 36 | .args(args) |
| 37 | .envs(GIT_ENV) |
| 38 | .env("BRAID_AGENT", agent) |
| 39 | .env("BRAID_AUTHOR", format!("{agent} <{agent}@example.org>")) |
| 40 | .output() |
| 41 | .unwrap() |
| 42 | } |
| 43 | |
| 44 | /// Runs braid with `--json` as `agent`, expecting exit status `code`. |
| 45 | fn run(agent: &str, dir: &Path, args: &[&str], code: i32) -> Value { |
| 46 | let mut all = args.to_vec(); |
| 47 | all.push("--json"); |
| 48 | let out = braid(agent, dir, &all); |
| 49 | assert_eq!( |
| 50 | out.status.code(), |
| 51 | Some(code), |
| 52 | "{agent}: braid {args:?}: {}{}", |
| 53 | String::from_utf8_lossy(&out.stdout), |
| 54 | String::from_utf8_lossy(&out.stderr) |
| 55 | ); |
| 56 | serde_json::from_slice(&out.stdout).unwrap() |
| 57 | } |
| 58 | |
| 59 | fn read(dir: &Path, name: &str) -> String { |
| 60 | std::fs::read_to_string(dir.join(name)).unwrap() |
| 61 | } |
| 62 | |
| 63 | fn git(dir: &Path, args: &[&str]) -> String { |
| 64 | let out = Command::new("git") |
| 65 | .current_dir(dir) |
| 66 | .args(args) |
| 67 | .envs(GIT_ENV) |
| 68 | .output() |
| 69 | .expect("git must be installed to run this test"); |
| 70 | assert!( |
| 71 | out.status.success(), |
| 72 | "git {args:?}: {}", |
| 73 | String::from_utf8_lossy(&out.stderr) |
| 74 | ); |
| 75 | String::from_utf8(out.stdout).unwrap() |
| 76 | } |
| 77 | |
| 78 | /// The keys of a JSON object, sorted. |
| 79 | fn keys(value: &Value) -> Vec<&str> { |
| 80 | let mut keys: Vec<&str> = value |
| 81 | .as_object() |
| 82 | .unwrap() |
| 83 | .keys() |
| 84 | .map(String::as_str) |
| 85 | .collect(); |
| 86 | keys.sort_unstable(); |
| 87 | keys |
| 88 | } |
| 89 | |
| 90 | fn id(value: &Value) -> String { |
| 91 | value.as_str().unwrap().to_owned() |
| 92 | } |
| 93 | |
| 94 | fn short(id: &str) -> &str { |
| 95 | &id[..12] |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn three_agents_edit_one_file_and_land_in_turn() { |
| 100 | let temp = tempfile::tempdir().unwrap(); |
| 101 | let base = std::fs::canonicalize(temp.path()).unwrap(); |
| 102 | let repo = base.join("repo"); |
| 103 | run("seeder", &base, &["init", repo.to_str().unwrap()], 0); |
| 104 | let seed_file = base.join("seed.txt"); |
| 105 | std::fs::write(&seed_file, SEED).unwrap(); |
| 106 | let set = format!("notes.txt=@{}", seed_file.display()); |
| 107 | let seeded = run( |
| 108 | "seeder", |
| 109 | &repo, |
| 110 | &["change", "create", "--base", "none", "--set", &set] |
| 111 | .into_iter() |
| 112 | .chain(["-m", "Seed the notes", "--land", "main"]) |
| 113 | .collect::<Vec<_>>(), |
| 114 | 0, |
| 115 | ); |
| 116 | let seed = id(&seeded["change"]); |
| 117 | |
| 118 | // Each agent gets a workspace, edits, and commits before anyone lands. |
| 119 | let dirs: Vec<PathBuf> = ["a", "b", "c"] |
| 120 | .iter() |
| 121 | .map(|name| { |
| 122 | let dir = base.join(name); |
| 123 | let made = run( |
| 124 | "seeder", |
| 125 | &repo, |
| 126 | &["workspace", "add", name, dir.to_str().unwrap()], |
| 127 | 0, |
| 128 | ); |
| 129 | assert_eq!(made["base"], seed.as_str()); |
| 130 | dir |
| 131 | }) |
| 132 | .collect(); |
| 133 | let (a, b, c) = (&dirs[0], &dirs[1], &dirs[2]); |
| 134 | std::fs::write(a.join("notes.txt"), SEED.replace("alpha", "alpha, from a")).unwrap(); |
| 135 | std::fs::write(b.join("notes.txt"), SEED.replace("alpha", "alpha, from b")).unwrap(); |
| 136 | std::fs::write(c.join("notes.txt"), format!("{SEED}delta, from c\n")).unwrap(); |
| 137 | let a_change = id(&run("agent-a", a, &["commit", "-m", "a: alpha"], 0)["changeset"]); |
| 138 | let b_change = id(&run("agent-b", b, &["commit", "-m", "b: alpha"], 0)["changeset"]); |
| 139 | let c_change = id(&run("agent-c", c, &["commit", "-m", "c: delta"], 0)["changeset"]); |
| 140 | |
| 141 | // c lands first: a fast-forward to its own changeset. |
| 142 | let landed = run("agent-c", c, &["land"], 0); |
| 143 | assert_eq!(landed["outcome"], "fast-forward"); |
| 144 | assert_eq!(landed["head_before"], seed.as_str()); |
| 145 | assert_eq!(landed["head"], c_change.as_str()); |
| 146 | assert_eq!( |
| 147 | landed["landed"], |
| 148 | json!([{ "from": c_change, "to": c_change }]) |
| 149 | ); |
| 150 | assert_eq!(landed["merged_paths"], json!([])); |
| 151 | assert_eq!(landed["updated_paths"], json!([])); |
| 152 | assert_eq!(landed["next"], Value::Null); |
| 153 | |
| 154 | // a is rebased onto c: its line and c's merge, and a's working copy |
| 155 | // gets c's line. |
| 156 | let landed = run("agent-a", a, &["land"], 0); |
| 157 | assert_eq!(landed["outcome"], "rebased"); |
| 158 | assert_eq!(landed["head_before"], c_change.as_str()); |
| 159 | let a_landed = id(&landed["head"]); |
| 160 | assert_ne!(a_landed, a_change); |
| 161 | assert_eq!( |
| 162 | landed["landed"], |
| 163 | json!([{ "from": a_change, "to": a_landed }]) |
| 164 | ); |
| 165 | assert_eq!(landed["merged_paths"], json!(["notes.txt"])); |
| 166 | assert_eq!(landed["updated_paths"], json!(["notes.txt"])); |
| 167 | assert_eq!(landed["base"], a_landed.as_str()); |
| 168 | assert_eq!( |
| 169 | read(a, "notes.txt"), |
| 170 | "title\nalpha, from a\nbeta\ngamma\ndelta, from c\n" |
| 171 | ); |
| 172 | |
| 173 | // b is behind by both, and conflicts with a: main does not move. |
| 174 | let now = run("agent-b", b, &["status"], 0); |
| 175 | assert_eq!(now["behind"], true); |
| 176 | assert_eq!(now["behind_by"], 2); |
| 177 | assert_eq!(now["unlanded"], json!([b_change])); |
| 178 | let landed = run("agent-b", b, &["land"], 2); |
| 179 | assert_eq!(landed["outcome"], "conflicts"); |
| 180 | assert_eq!(landed["head_before"], a_landed.as_str()); |
| 181 | assert_eq!(landed["head"], a_landed.as_str()); |
| 182 | assert_eq!(landed["operation"], Value::Null); |
| 183 | assert_eq!(landed["change"], b_change.as_str()); |
| 184 | assert_eq!(landed["landed"], json!([{ "from": b_change, "to": null }])); |
| 185 | assert_eq!(landed["updated_paths"], json!([])); |
| 186 | assert_eq!(landed["next"], "braid update"); |
| 187 | let conflict = &landed["conflicts"][0]; |
| 188 | assert_eq!( |
| 189 | keys(conflict), |
| 190 | ["base", "branch", "hunks", "kind", "path", "workspace"] |
| 191 | ); |
| 192 | assert_eq!(conflict["path"], "notes.txt"); |
| 193 | assert_eq!(conflict["kind"], "CONTENT"); |
| 194 | let hunk = &conflict["hunks"][0]; |
| 195 | assert_eq!(keys(hunk), ["base", "branch", "workspace"]); |
| 196 | // Line 1 counted from 0, end excluded: the second line. |
| 197 | assert_eq!( |
| 198 | hunk["base"], |
| 199 | json!({ "start": 1, "end": 2, "text": "alpha\n" }) |
| 200 | ); |
| 201 | assert_eq!( |
| 202 | hunk["branch"], |
| 203 | json!({ "start": 1, "end": 2, "text": "alpha, from a\n" }) |
| 204 | ); |
| 205 | assert_eq!( |
| 206 | hunk["workspace"], |
| 207 | json!({ "start": 1, "end": 2, "text": "alpha, from b\n" }) |
| 208 | ); |
| 209 | |
| 210 | // update brings the conflict in, with c's line merged around it. |
| 211 | let updated = run("agent-b", b, &["update"], 2); |
| 212 | assert_eq!(updated["outcome"], "conflicts"); |
| 213 | assert_eq!(updated["head_before"], a_landed.as_str()); |
| 214 | assert_eq!(updated["head"], a_landed.as_str()); |
| 215 | assert_eq!(updated["base"], a_landed.as_str()); |
| 216 | assert_eq!(updated["landed"], json!([{ "from": b_change, "to": null }])); |
| 217 | assert_eq!(updated["updated_paths"], json!(["notes.txt"])); |
| 218 | assert_eq!(updated["next"], "braid commit"); |
| 219 | assert_eq!( |
| 220 | updated["conflicts"][0]["hunks"][0]["workspace"]["text"], |
| 221 | "alpha, from b\n" |
| 222 | ); |
| 223 | let merged = read(b, "notes.txt"); |
| 224 | assert!(merged.contains("<<<<<<< main"), "{merged}"); |
| 225 | assert!(merged.ends_with("delta, from c\n"), "{merged}"); |
| 226 | let now = run("agent-b", b, &["status"], 0); |
| 227 | assert_eq!(now["unlanded"], json!([b_change])); |
| 228 | assert_eq!(now["behind"], false); |
| 229 | assert_eq!(now["ready_to_commit"], false); |
| 230 | assert_eq!(now["blocking"][0]["reason"], "markers"); |
| 231 | |
| 232 | // b resolves, commits without -m, which keeps its message, and lands. |
| 233 | std::fs::write(b.join("notes.txt"), FINAL).unwrap(); |
| 234 | assert_eq!(run("agent-b", b, &["status"], 0)["ready_to_commit"], true); |
| 235 | let committed = run("agent-b", b, &["commit"], 0); |
| 236 | assert_eq!(committed["message_replaced"], false); |
| 237 | assert_eq!(committed["rebase"]["outcome"], "finished"); |
| 238 | let b_landed = id(&committed["changeset"]); |
| 239 | let landed = run("agent-b", b, &["land"], 0); |
| 240 | assert_eq!(landed["outcome"], "fast-forward"); |
| 241 | assert_eq!( |
| 242 | landed["landed"], |
| 243 | json!([{ "from": b_landed, "to": b_landed }]) |
| 244 | ); |
| 245 | |
| 246 | // The op log says who did what, and how. |
| 247 | let ops = run("seeder", &repo, &["op", "log", "-n", "4"], 0); |
| 248 | let ops: Vec<(String, String)> = ops |
| 249 | .as_array() |
| 250 | .unwrap() |
| 251 | .iter() |
| 252 | .map(|op| (id(&op["agent"]), id(&op["description"]))) |
| 253 | .collect(); |
| 254 | let said = |agent: &str, text: String| (agent.to_owned(), text); |
| 255 | assert_eq!( |
| 256 | ops, |
| 257 | [ |
| 258 | said( |
| 259 | "agent-b", |
| 260 | format!( |
| 261 | "land workspace b on main: fast-forward {} -> {} (0 paths merged)", |
| 262 | short(&b_landed), |
| 263 | short(&b_landed) |
| 264 | ) |
| 265 | ), |
| 266 | said( |
| 267 | "agent-a", |
| 268 | format!( |
| 269 | "land workspace a on main: rebased {} -> {} (1 path merged)", |
| 270 | short(&a_change), |
| 271 | short(&a_landed) |
| 272 | ) |
| 273 | ), |
| 274 | said( |
| 275 | "agent-c", |
| 276 | format!( |
| 277 | "land workspace c on main: fast-forward {} -> {} (0 paths merged)", |
| 278 | short(&c_change), |
| 279 | short(&c_change) |
| 280 | ) |
| 281 | ), |
| 282 | said( |
| 283 | "seeder", |
| 284 | format!( |
| 285 | "land changeset on main: fast-forward {} -> {} (0 paths merged)", |
| 286 | short(&seed), |
| 287 | short(&seed) |
| 288 | ) |
| 289 | ), |
| 290 | ] |
| 291 | ); |
| 292 | |
| 293 | // main's history names the agent that made each change, rebased or not. |
| 294 | let log = run("seeder", &repo, &["log", "main"], 0); |
| 295 | let history: Vec<(String, String, String)> = log |
| 296 | .as_array() |
| 297 | .unwrap() |
| 298 | .iter() |
| 299 | .map(|c| (id(&c["id"]), id(&c["agent"]), id(&c["message"]))) |
| 300 | .collect(); |
| 301 | assert_eq!( |
| 302 | history, |
| 303 | [ |
| 304 | (b_landed.clone(), "agent-b".into(), "b: alpha".into()), |
| 305 | (a_landed.clone(), "agent-a".into(), "a: alpha".into()), |
| 306 | (c_change.clone(), "agent-c".into(), "c: delta".into()), |
| 307 | (seed.clone(), "seeder".into(), "Seed the notes".into()), |
| 308 | ] |
| 309 | ); |
| 310 | let shown = run("seeder", &repo, &["show", short(&a_landed)], 0); |
| 311 | assert_eq!(shown["changes"][0]["status"], "M"); |
| 312 | assert_eq!(shown["author"]["name"], "agent-a"); |
| 313 | |
| 314 | // Everyone ends with the same file. |
| 315 | run("agent-a", a, &["update"], 0); |
| 316 | run("agent-c", c, &["update"], 0); |
| 317 | for dir in [a, b, c] { |
| 318 | assert_eq!(read(dir, "notes.txt"), FINAL, "{}", dir.display()); |
| 319 | } |
| 320 | |
| 321 | // git sync pushes main to a git remote, which has the same file and |
| 322 | // the agent in each commit. |
| 323 | let remote = base.join("remote.git"); |
| 324 | git( |
| 325 | &base, |
| 326 | &["init", "--quiet", "--bare", remote.to_str().unwrap()], |
| 327 | ); |
| 328 | let synced = run( |
| 329 | "agent-b", |
| 330 | &repo, |
| 331 | &["git", "sync", remote.to_str().unwrap()], |
| 332 | 0, |
| 333 | ); |
| 334 | assert_eq!(synced["outcome"], "pushed"); |
| 335 | assert_eq!(git(&remote, &["show", "main:notes.txt"]), FINAL); |
| 336 | let commit = git(&remote, &["cat-file", "commit", "main"]); |
| 337 | assert!(commit.contains("\nagent agent-b\n"), "{commit}"); |
| 338 | assert_eq!(run("seeder", &repo, &["fsck"], 0)["clean"], true); |
| 339 | } |