Elffuss BraidHub

utopia-ia/elffuss-braid public

main · 11.7 KiBRawPermalink
1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 UtopiaIA
3
4//! End-to-end tests of what an agent session that follows the braid section
5//! of a project's AGENTS.md relies on: where `workspace add` puts a
6//! workspace, git refusing to work inside one, the branch it tracks, hints
7//! that repeat the session's flags, and reading a file as a changeset has
8//! it.
9
10use std::fs;
11use std::path::{Path, PathBuf};
12use std::process::{Command, Output};
13
14use serde_json::Value;
15
16/// Git without the user's or the system's configuration.
17const GIT_ENV: [(&str, &str); 6] = [
18 ("GIT_CONFIG_GLOBAL", "/dev/null"),
19 ("GIT_CONFIG_NOSYSTEM", "1"),
20 ("GIT_AUTHOR_NAME", "Ada Lovelace"),
21 ("GIT_AUTHOR_EMAIL", "ada@example.org"),
22 ("GIT_COMMITTER_NAME", "Ada Lovelace"),
23 ("GIT_COMMITTER_EMAIL", "ada@example.org"),
24];
25
26/// Runs braid in `dir`, with an author but no agent from the environment.
27fn braid(dir: &Path, args: &[&str]) -> Output {
28 Command::new(env!("CARGO_BIN_EXE_braid"))
29 .current_dir(dir)
30 .args(args)
31 .envs(GIT_ENV)
32 .env("BRAID_AUTHOR", "Ada <ada@example.org>")
33 .env_remove("BRAID_AGENT")
34 .output()
35 .unwrap()
36}
37
38/// Runs braid expecting exit status `code`, and returns what it printed.
39fn run(dir: &Path, args: &[&str], code: i32) -> String {
40 let out = braid(dir, args);
41 assert_eq!(
42 out.status.code(),
43 Some(code),
44 "braid {args:?}: {}{}",
45 String::from_utf8_lossy(&out.stdout),
46 String::from_utf8_lossy(&out.stderr)
47 );
48 String::from_utf8(out.stdout).unwrap()
49}
50
51fn ok(dir: &Path, args: &[&str]) -> String {
52 run(dir, args, 0)
53}
54
55fn json(dir: &Path, args: &[&str], code: i32) -> Value {
56 let mut all = args.to_vec();
57 all.push("--json");
58 serde_json::from_str(&run(dir, &all, code)).unwrap()
59}
60
61/// Runs braid expecting it to fail, and returns its error.
62fn fails(dir: &Path, args: &[&str]) -> String {
63 let out = braid(dir, args);
64 assert_eq!(out.status.code(), Some(1), "braid {args:?}");
65 assert!(out.stdout.is_empty(), "braid {args:?}");
66 String::from_utf8(out.stderr).unwrap()
67}
68
69fn git(dir: &Path, args: &[&str]) -> Output {
70 Command::new("git")
71 .current_dir(dir)
72 .args(args)
73 .envs(GIT_ENV)
74 .output()
75 .expect("git must be installed to run this test")
76}
77
78fn git_ok(dir: &Path, args: &[&str]) -> String {
79 let out = git(dir, args);
80 assert!(
81 out.status.success(),
82 "git {args:?}: {}",
83 String::from_utf8_lossy(&out.stderr)
84 );
85 String::from_utf8(out.stdout).unwrap()
86}
87
88/// A git project set up for braid as agents.md does it, on `branch`, with
89/// one commit of `notes.txt`.
90fn project(branch: &str) -> (tempfile::TempDir, PathBuf) {
91 let temp = tempfile::tempdir().unwrap();
92 let dir = fs::canonicalize(temp.path()).unwrap();
93 git_ok(&dir, &["init", "--quiet", "-b", branch]);
94 fs::write(dir.join("notes.txt"), "one\ntwo\n").unwrap();
95 git_ok(&dir, &["add", "notes.txt"]);
96 git_ok(&dir, &["commit", "--quiet", "-m", "First"]);
97 ok(&dir, &["init"]);
98 let exclude = git_ok(&dir, &["rev-parse", "--git-path", "info/exclude"]);
99 let exclude = dir.join(exclude.trim());
100 fs::create_dir_all(exclude.parent().unwrap()).unwrap();
101 let mut lines = fs::read_to_string(&exclude).unwrap_or_default();
102 lines.push_str(".braid\n");
103 fs::write(&exclude, lines).unwrap();
104 ok(&dir, &["git", "import", "."]);
105 (temp, dir)
106}
107
108#[test]
109fn a_workspace_goes_in_braid_work_and_git_refuses_to_work_in_it() {
110 let (_temp, dir) = project("main");
111 let deep = dir.join("src/deep");
112 fs::create_dir_all(&deep).unwrap();
113 // From anywhere in the project, the workspace goes in .braid/work.
114 let made = json(&deep, &["workspace", "add", "agent-1"], 0);
115 let workspace = dir.join(".braid/work/agent-1");
116 assert_eq!(made["path"], workspace.to_str().unwrap());
117 assert_eq!(made["files"], 1);
118 assert!(!deep.join("agent-1").exists());
119 let guard = fs::read_to_string(workspace.join(".git")).unwrap();
120 assert!(guard.starts_with("gitdir: "), "{guard}");
121
122 // The user's own edit in the shared checkout, and the agent's in its
123 // workspace.
124 fs::write(dir.join("notes.txt"), "one, the user's\ntwo\n").unwrap();
125 fs::write(workspace.join("notes.txt"), "one\ntwo, the agent's\n").unwrap();
126 for args in [
127 &["stash"][..],
128 &["status"],
129 &["diff"],
130 &["reset", "--hard"],
131 &["checkout", "--", "."],
132 &["clean", "-fdx"],
133 &["add", "-A"],
134 &["rev-parse", "--show-toplevel"],
135 ] {
136 let out = git(&workspace, args);
137 assert!(!out.status.success(), "git {args:?} worked in a workspace");
138 let err = String::from_utf8_lossy(&out.stderr);
139 // Only the refusal is the same in every git: older versions repeat
140 // the guard's text, which names braid, and newer ones say "(null)".
141 assert!(err.contains("not a git repository"), "git {args:?}: {err}");
142 }
143 assert_eq!(
144 fs::read_to_string(dir.join("notes.txt")).unwrap(),
145 "one, the user's\ntwo\n"
146 );
147 assert_eq!(git_ok(&dir, &["stash", "list"]), "");
148
149 // Braid does not see the guard, nor does the shared checkout's git see
150 // the workspace.
151 let status = json(&workspace, &["status"], 0);
152 assert_eq!(status["modified"], serde_json::json!(["notes.txt"]));
153 assert_eq!(status["added"], serde_json::json!([]));
154 assert_eq!(
155 git_ok(&dir, &["status", "--short"]),
156 " M notes.txt\n",
157 "only the user's edit"
158 );
159 ok(&workspace, &["commit", "-m", "Agent's edit"]);
160 ok(&workspace, &["land"]);
161 ok(&dir, &["workspace", "remove", "agent-1"]);
162 assert!(!workspace.exists());
163}
164
165#[test]
166fn outside_a_workspace_the_error_says_how_to_reach_one() {
167 let (_temp, dir) = project("main");
168 let err = fails(&dir, &["status"]);
169 assert!(err.contains("not inside a workspace"), "{err}");
170 assert!(err.contains("--workspace <name>"), "{err}");
171 assert!(err.contains(".braid/work/<name>"), "{err}");
172}
173
174#[test]
175fn a_workspace_tracks_a_branch_the_repository_has() {
176 let (_temp, dir) = project("master");
177 let err = fails(&dir, &["workspace", "add", "s2", "--track", "main"]);
178 assert!(
179 err.contains("the repository has no branch \"main\"; it has master"),
180 "{err}"
181 );
182 assert!(err.contains("--new-branch"), "{err}");
183 assert!(!dir.join(".braid/work/s2").exists());
184 assert_eq!(json(&dir, &["workspace", "list"], 0), serde_json::json!([]));
185 for own in ["braid/git-map", "tags/v1"] {
186 let err = fails(&dir, &["workspace", "add", "s3", "--track", own]);
187 assert!(err.contains("braid's own namespaces"), "{err}");
188 }
189
190 // The branch it has, or a new one on purpose.
191 let made = json(&dir, &["workspace", "add", "s2", "--track", "master"], 0);
192 assert_eq!(made["files"], 1);
193 let made = json(
194 &dir,
195 &["workspace", "add", "s4", "--track", "topic", "--new-branch"],
196 0,
197 );
198 assert_eq!(made["branch"], "topic");
199 assert_eq!(made["base"], Value::Null);
200
201 // A repository with no branch yet takes any name: the first landing
202 // starts it.
203 let temp = tempfile::tempdir().unwrap();
204 let empty = fs::canonicalize(temp.path()).unwrap();
205 ok(&empty, &["init"]);
206 let made = json(&empty, &["workspace", "add", "first"], 0);
207 assert_eq!(made["branch"], "main");
208}
209
210#[test]
211fn hints_repeat_the_flags_the_session_gave() {
212 let (_temp, dir) = project("main");
213 for name in ["agent-1", "agent-2"] {
214 ok(&dir, &["--agent", name, "workspace", "add", name]);
215 }
216 let work = |name: &str| dir.join(".braid/work").join(name);
217 fs::write(work("agent-1").join("notes.txt"), "one, from 1\ntwo\n").unwrap();
218 fs::write(work("agent-2").join("notes.txt"), "one, from 2\ntwo\n").unwrap();
219 let session = |name: &'static str| ["--agent", name, "--workspace", name];
220 for name in ["agent-1", "agent-2"] {
221 let mut args = session(name).to_vec();
222 args.extend(["commit", "-m", name]);
223 ok(&dir, &args);
224 }
225 let mut land = session("agent-1").to_vec();
226 land.push("land");
227 ok(&dir, &land);
228
229 // From the project root, as a shell that does not keep its directory
230 // runs them: the hints name the agent and the workspace.
231 let mut land = session("agent-2").to_vec();
232 land.push("land");
233 let landed = json(&dir, &land, 2);
234 assert_eq!(landed["outcome"], "conflicts");
235 assert_eq!(
236 landed["next"],
237 "braid --agent agent-2 --workspace agent-2 update"
238 );
239 let said = run(&dir, &land, 2);
240 assert!(
241 said.contains("Run `braid --agent agent-2 --workspace agent-2 update`"),
242 "{said}"
243 );
244 let mut update = session("agent-2").to_vec();
245 update.push("update");
246 let updated = json(&dir, &update, 2);
247 assert_eq!(
248 updated["next"],
249 "braid --agent agent-2 --workspace agent-2 commit"
250 );
251 // The same flags after the command name count as well.
252 let status = run(
253 &dir,
254 &["status", "--workspace", "agent-2", "--agent", "agent-2"],
255 0,
256 );
257 assert!(
258 status.contains("then `braid --agent agent-2 --workspace agent-2 commit`"),
259 "{status}"
260 );
261 // Settled, committed and landed as the hints say, the op log names the
262 // session for each step.
263 fs::write(
264 work("agent-2").join("notes.txt"),
265 "one, from 1 and 2\ntwo\n",
266 )
267 .unwrap();
268 let commit = run(
269 &dir,
270 &["--agent", "agent-2", "--workspace", "agent-2", "commit"],
271 0,
272 );
273 assert!(
274 commit.contains("`braid --agent agent-2 --workspace agent-2 land` when ready"),
275 "{commit}"
276 );
277 ok(&dir, &land);
278 let ops = json(&dir, &["op", "log", "-n", "1"], 0);
279 assert_eq!(ops[0]["agent"], "agent-2");
280
281 // agent-1 is behind now. A label that a shell would split is quoted.
282 let behind = run(
283 &dir,
284 &["--agent", "agent one", "--workspace", "agent-1", "status"],
285 0,
286 );
287 assert!(
288 behind.contains("run `braid --agent 'agent one' --workspace agent-1 update`"),
289 "{behind}"
290 );
291 // With no flags, the hints are as plain as ever.
292 let behind = run(&work("agent-1"), &["status"], 0);
293 assert!(behind.contains("run `braid update`"), "{behind}");
294}
295
296#[test]
297fn cat_object_reads_a_file_as_a_changeset_has_it() {
298 let (_temp, dir) = project("main");
299 ok(&dir, &["workspace", "add", "agent-1"]);
300 let workspace = dir.join(".braid/work/agent-1");
301 fs::create_dir_all(workspace.join("src")).unwrap();
302 fs::write(workspace.join("src/lib.rs"), "fn a() {}\n").unwrap();
303 ok(&workspace, &["commit", "-m", "Add src/lib.rs"]);
304 fs::write(workspace.join("notes.txt"), "one, changed\ntwo\n").unwrap();
305 let base = json(&workspace, &["status"], 0)["base"]
306 .as_str()
307 .unwrap()
308 .to_owned();
309
310 // The base's version of an edited file: what reviewing or undoing the
311 // edit needs.
312 assert_eq!(
313 ok(&workspace, &["cat-object", &format!("{base}:notes.txt")]),
314 "one\ntwo\n"
315 );
316 assert_eq!(
317 ok(
318 &workspace,
319 &["cat-object", &format!("{}:src/lib.rs", &base[..12])]
320 ),
321 "fn a() {}\n"
322 );
323 assert_eq!(ok(&dir, &["cat-object", "main:notes.txt"]), "one\ntwo\n");
324 let blob = json(&dir, &["cat-object", "main:./notes.txt"], 0);
325 assert_eq!(blob["kind"], "blob");
326 assert_eq!(blob["size"], 8);
327 let tree = json(&workspace, &["cat-object", &format!("{base}:src")], 0);
328 assert_eq!(tree["kind"], "tree");
329 assert_eq!(json(&dir, &["cat-object", "main:"], 0)["kind"], "tree");
330 let err = fails(&dir, &["cat-object", "main:src/lib.rs"]);
331 assert!(
332 err.contains("no file or directory at \"src/lib.rs\""),
333 "{err}"
334 );
335 fails(&dir, &["cat-object", "nope:notes.txt"]);
336}