Elffuss BraidHub

utopia-ia/elffuss-braid public

2dd40509e7e2 · 11.6 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 assert!(err.contains("not a git repository"), "git {args:?}: {err}");
140 assert!(err.contains("braid workspace"), "git {args:?}: {err}");
141 }
142 assert_eq!(
143 fs::read_to_string(dir.join("notes.txt")).unwrap(),
144 "one, the user's\ntwo\n"
145 );
146 assert_eq!(git_ok(&dir, &["stash", "list"]), "");
147
148 // Braid does not see the guard, nor does the shared checkout's git see
149 // the workspace.
150 let status = json(&workspace, &["status"], 0);
151 assert_eq!(status["modified"], serde_json::json!(["notes.txt"]));
152 assert_eq!(status["added"], serde_json::json!([]));
153 assert_eq!(
154 git_ok(&dir, &["status", "--short"]),
155 " M notes.txt\n",
156 "only the user's edit"
157 );
158 ok(&workspace, &["commit", "-m", "Agent's edit"]);
159 ok(&workspace, &["land"]);
160 ok(&dir, &["workspace", "remove", "agent-1"]);
161 assert!(!workspace.exists());
162}
163
164#[test]
165fn outside_a_workspace_the_error_says_how_to_reach_one() {
166 let (_temp, dir) = project("main");
167 let err = fails(&dir, &["status"]);
168 assert!(err.contains("not inside a workspace"), "{err}");
169 assert!(err.contains("--workspace <name>"), "{err}");
170 assert!(err.contains(".braid/work/<name>"), "{err}");
171}
172
173#[test]
174fn a_workspace_tracks_a_branch_the_repository_has() {
175 let (_temp, dir) = project("master");
176 let err = fails(&dir, &["workspace", "add", "s2", "--track", "main"]);
177 assert!(
178 err.contains("the repository has no branch \"main\"; it has master"),
179 "{err}"
180 );
181 assert!(err.contains("--new-branch"), "{err}");
182 assert!(!dir.join(".braid/work/s2").exists());
183 assert_eq!(json(&dir, &["workspace", "list"], 0), serde_json::json!([]));
184 for own in ["braid/git-map", "tags/v1"] {
185 let err = fails(&dir, &["workspace", "add", "s3", "--track", own]);
186 assert!(err.contains("braid's own namespaces"), "{err}");
187 }
188
189 // The branch it has, or a new one on purpose.
190 let made = json(&dir, &["workspace", "add", "s2", "--track", "master"], 0);
191 assert_eq!(made["files"], 1);
192 let made = json(
193 &dir,
194 &["workspace", "add", "s4", "--track", "topic", "--new-branch"],
195 0,
196 );
197 assert_eq!(made["branch"], "topic");
198 assert_eq!(made["base"], Value::Null);
199
200 // A repository with no branch yet takes any name: the first landing
201 // starts it.
202 let temp = tempfile::tempdir().unwrap();
203 let empty = fs::canonicalize(temp.path()).unwrap();
204 ok(&empty, &["init"]);
205 let made = json(&empty, &["workspace", "add", "first"], 0);
206 assert_eq!(made["branch"], "main");
207}
208
209#[test]
210fn hints_repeat_the_flags_the_session_gave() {
211 let (_temp, dir) = project("main");
212 for name in ["agent-1", "agent-2"] {
213 ok(&dir, &["--agent", name, "workspace", "add", name]);
214 }
215 let work = |name: &str| dir.join(".braid/work").join(name);
216 fs::write(work("agent-1").join("notes.txt"), "one, from 1\ntwo\n").unwrap();
217 fs::write(work("agent-2").join("notes.txt"), "one, from 2\ntwo\n").unwrap();
218 let session = |name: &'static str| ["--agent", name, "--workspace", name];
219 for name in ["agent-1", "agent-2"] {
220 let mut args = session(name).to_vec();
221 args.extend(["commit", "-m", name]);
222 ok(&dir, &args);
223 }
224 let mut land = session("agent-1").to_vec();
225 land.push("land");
226 ok(&dir, &land);
227
228 // From the project root, as a shell that does not keep its directory
229 // runs them: the hints name the agent and the workspace.
230 let mut land = session("agent-2").to_vec();
231 land.push("land");
232 let landed = json(&dir, &land, 2);
233 assert_eq!(landed["outcome"], "conflicts");
234 assert_eq!(
235 landed["next"],
236 "braid --agent agent-2 --workspace agent-2 update"
237 );
238 let said = run(&dir, &land, 2);
239 assert!(
240 said.contains("Run `braid --agent agent-2 --workspace agent-2 update`"),
241 "{said}"
242 );
243 let mut update = session("agent-2").to_vec();
244 update.push("update");
245 let updated = json(&dir, &update, 2);
246 assert_eq!(
247 updated["next"],
248 "braid --agent agent-2 --workspace agent-2 commit"
249 );
250 // The same flags after the command name count as well.
251 let status = run(
252 &dir,
253 &["status", "--workspace", "agent-2", "--agent", "agent-2"],
254 0,
255 );
256 assert!(
257 status.contains("then `braid --agent agent-2 --workspace agent-2 commit`"),
258 "{status}"
259 );
260 // Settled, committed and landed as the hints say, the op log names the
261 // session for each step.
262 fs::write(
263 work("agent-2").join("notes.txt"),
264 "one, from 1 and 2\ntwo\n",
265 )
266 .unwrap();
267 let commit = run(
268 &dir,
269 &["--agent", "agent-2", "--workspace", "agent-2", "commit"],
270 0,
271 );
272 assert!(
273 commit.contains("`braid --agent agent-2 --workspace agent-2 land` when ready"),
274 "{commit}"
275 );
276 ok(&dir, &land);
277 let ops = json(&dir, &["op", "log", "-n", "1"], 0);
278 assert_eq!(ops[0]["agent"], "agent-2");
279
280 // agent-1 is behind now. A label that a shell would split is quoted.
281 let behind = run(
282 &dir,
283 &["--agent", "agent one", "--workspace", "agent-1", "status"],
284 0,
285 );
286 assert!(
287 behind.contains("run `braid --agent 'agent one' --workspace agent-1 update`"),
288 "{behind}"
289 );
290 // With no flags, the hints are as plain as ever.
291 let behind = run(&work("agent-1"), &["status"], 0);
292 assert!(behind.contains("run `braid update`"), "{behind}");
293}
294
295#[test]
296fn cat_object_reads_a_file_as_a_changeset_has_it() {
297 let (_temp, dir) = project("main");
298 ok(&dir, &["workspace", "add", "agent-1"]);
299 let workspace = dir.join(".braid/work/agent-1");
300 fs::create_dir_all(workspace.join("src")).unwrap();
301 fs::write(workspace.join("src/lib.rs"), "fn a() {}\n").unwrap();
302 ok(&workspace, &["commit", "-m", "Add src/lib.rs"]);
303 fs::write(workspace.join("notes.txt"), "one, changed\ntwo\n").unwrap();
304 let base = json(&workspace, &["status"], 0)["base"]
305 .as_str()
306 .unwrap()
307 .to_owned();
308
309 // The base's version of an edited file: what reviewing or undoing the
310 // edit needs.
311 assert_eq!(
312 ok(&workspace, &["cat-object", &format!("{base}:notes.txt")]),
313 "one\ntwo\n"
314 );
315 assert_eq!(
316 ok(
317 &workspace,
318 &["cat-object", &format!("{}:src/lib.rs", &base[..12])]
319 ),
320 "fn a() {}\n"
321 );
322 assert_eq!(ok(&dir, &["cat-object", "main:notes.txt"]), "one\ntwo\n");
323 let blob = json(&dir, &["cat-object", "main:./notes.txt"], 0);
324 assert_eq!(blob["kind"], "blob");
325 assert_eq!(blob["size"], 8);
326 let tree = json(&workspace, &["cat-object", &format!("{base}:src")], 0);
327 assert_eq!(tree["kind"], "tree");
328 assert_eq!(json(&dir, &["cat-object", "main:"], 0)["kind"], "tree");
329 let err = fails(&dir, &["cat-object", "main:src/lib.rs"]);
330 assert!(
331 err.contains("no file or directory at \"src/lib.rs\""),
332 "{err}"
333 );
334 fails(&dir, &["cat-object", "nope:notes.txt"]);
335}