Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed

Versions 2 to 318

Compare

+174 −0 in 1 place
new file, 174 lines
1+// client/launch.mjs: which client a run takes, and that a bad bundle never stops a command.
2+// Each test runs the real launch.mjs from a throwaway git checkout, with a stand-in client, and a home of its own.
3+
4+import assert from "node:assert/strict";
5+import { execFileSync, spawnSync } from "node:child_process";
6+import {
7+ copyFileSync,
8+ existsSync,
9+ mkdirSync,
10+ mkdtempSync,
11+ readdirSync,
12+ readFileSync,
13+ rmSync,
14+ utimesSync,
15+ writeFileSync,
16+} from "node:fs";
17+import { tmpdir } from "node:os";
18+import { join } from "node:path";
19+import { after, describe, it } from "node:test";
21+const LAUNCH = new URL("../launch.mjs", import.meta.url);
22+const roots: string[] = [];
23+after(() => {
24+ for (const root of roots) rmSync(root, { recursive: true, force: true });
25+});
27+/** A checkout on main with one commit, launch.mjs and a stand-in client/abso.ts that names itself. */
28+function checkout(): { repo: string; home: string; head: string } {
29+ const root = mkdtempSync(join(tmpdir(), "abso-launch-"));
30+ roots.push(root);
31+ const repo = join(root, "repo");
32+ const home = join(root, "home");
33+ mkdirSync(join(repo, "client"), { recursive: true });
34+ copyFileSync(LAUNCH, join(repo, "client", "launch.mjs"));
35+ writeFileSync(
36+ join(repo, "client", "abso.ts"),
37+ 'console.log("ts " + JSON.stringify(process.argv.slice(2)));\nprocess.exitCode = Number(process.env.FAKE_EXIT ?? 0);\n',
38+ );
39+ const git = (...a: string[]) => execFileSync("git", ["-C", repo, ...a], { encoding: "utf8" });
40+ git("init", "-q", "-b", "main");
41+ git("add", ".");
42+ git("-c", "user.name=t", "-c", "user.email=t@example.com", "commit", "-q", "-m", "one");
43+ // A stamp from now: no background step starts during a test.
44+ mkdirSync(join(home, ".abso"), { recursive: true });
45+ writeFileSync(join(home, ".abso", "client-update.stamp"), "now\n");
46+ return { repo, home, head: git("rev-parse", "HEAD").trim() };
47+}
49+function bundle(repo: string, text: string, commit: string): void {
50+ mkdirSync(join(repo, "client", "dist"), { recursive: true });
51+ writeFileSync(join(repo, "client", "dist", "abso.mjs"), text);
52+ writeFileSync(join(repo, "client", "dist", "abso.mjs.commit"), `${commit}\n`);
53+}
55+function launch(c: { repo: string; home: string }, args: string[], env: Record<string, string> = {}) {
56+ return spawnSync(process.execPath, [join(c.repo, "client", "launch.mjs"), ...args], {
57+ encoding: "utf8",
58+ env: { ...process.env, HOME: c.home, USERPROFILE: c.home, ...env },
59+ });
60+}
62+const NAMES_ITSELF = 'console.log("bundle " + JSON.stringify(process.argv.slice(1)));\n';
64+describe("client/launch.mjs", () => {
65+ it("runs client/abso.ts when there's no bundle, with the arguments and exit code passed through", () => {
66+ const c = checkout();
67+ const r = launch(c, ["issue", "42"], { FAKE_EXIT: "3" });
68+ assert.equal(r.stdout.trim(), 'ts ["issue","42"]');
69+ assert.equal(r.status, 3);
70+ });
72+ it("runs the bundle in its own process when it was built from the checkout's commit", () => {
73+ const c = checkout();
74+ bundle(c.repo, NAMES_ITSELF, c.head);
75+ const r = launch(c, ["--version"]);
76+ assert.equal(
77+ r.stdout.trim(),
78+ `bundle ${JSON.stringify([join(c.repo, "client", "dist", "abso.mjs"), "--version"])}`,
79+ );
80+ assert.equal(r.status, 0);
81+ });
83+ it("runs client/abso.ts when the bundle is from another commit", () => {
84+ const c = checkout();
85+ bundle(c.repo, NAMES_ITSELF, "0".repeat(40));
86+ assert.equal(launch(c, ["status"]).stdout.trim(), 'ts ["status"]');
87+ });
89+ it("falls back to client/abso.ts in the same run when the bundle doesn't load, and writes one line", () => {
90+ const c = checkout();
91+ bundle(c.repo, "this is not javascript (\n", c.head);
92+ const r = launch(c, ["log"]);
93+ assert.equal(r.stdout.trim(), 'ts ["log"]');
94+ assert.equal(r.status, 0);
95+ // The launcher's own notice goes to its log only, never on the command's output.
96+ assert.equal(r.stderr, "");
97+ const log = readFileSync(join(c.home, ".abso", "launcher.log"), "utf8")
98+ .trim()
99+ .split("\n");
100+ assert.equal(log.length, 1);
101+ assert.match(log[0]!, /the bundle didn't load \(SyntaxError/);
102+ });
104+ it("doesn't run a command twice when it fails inside a bundle that loaded", () => {
105+ const c = checkout();
106+ bundle(c.repo, 'throw new Error("the command failed");\n', c.head);
107+ const r = launch(c, ["push"]);
108+ assert.notEqual(r.status, 0);
109+ assert.doesNotMatch(r.stdout, /^ts /m);
110+ assert.equal(existsSync(join(c.home, ".abso", "launcher.log")), false);
111+ });
113+ it("doesn't run a command twice when it throws a SyntaxError after the bundle started", () => {
114+ const c = checkout();
115+ bundle(c.repo, 'globalThis.__absoBundleStarted = true;\nJSON.parse("{");\n', c.head);
116+ const r = launch(c, ["push"]);
117+ assert.notEqual(r.status, 0);
118+ assert.doesNotMatch(r.stdout, /^ts /m);
119+ assert.equal(existsSync(join(c.home, ".abso", "launcher.log")), false);
120+ });
122+ it("passes stdin through to the client, from the bundle and from the .ts", () => {
123+ const c = checkout();
124+ const echo = 'let s = "";\nprocess.stdin.on("data", (d) => (s += d)).on("end", () => console.log("got " + s));\n';
125+ writeFileSync(join(c.repo, "client", "abso.ts"), echo);
126+ const fed = (input: string) =>
127+ spawnSync(process.execPath, [join(c.repo, "client", "launch.mjs")], {
128+ input,
129+ encoding: "utf8",
130+ env: { ...process.env, HOME: c.home, USERPROFILE: c.home },
131+ }).stdout.trim();
132+ assert.equal(fed("from the ts"), "got from the ts");
133+ bundle(c.repo, echo, c.head);
134+ assert.equal(fed("from the bundle"), "got from the bundle");
135+ });
137+ it("runs one background step at a time: a second finds the lock taken and leaves it", () => {
138+ const c = checkout();
139+ const lock = join(c.home, ".abso", "client-update.lock");
140+ mkdirSync(lock);
141+ assert.equal(launch(c, [], { ABSO_LAUNCH_UPDATE: "1" }).status, 0);
142+ assert.equal(existsSync(lock), true);
143+ });
145+ it("takes over a lock a dead run left behind, and removes it after", () => {
146+ const c = checkout();
147+ const lock = join(c.home, ".abso", "client-update.lock");
148+ mkdirSync(lock);
149+ const old = new Date(Date.now() - 11 * 60 * 1000);
150+ utimesSync(lock, old, old);
151+ assert.equal(launch(c, [], { ABSO_LAUNCH_UPDATE: "1" }).status, 0);
152+ assert.deepEqual(
153+ readdirSync(join(c.home, ".abso")).filter((f) => f.includes("lock")),
154+ [],
155+ );
156+ });
158+ it("reads the commit in a git worktree, where .git is a file", () => {
159+ const c = checkout();
160+ const wt = join(c.repo, "..", "wt");
161+ execFileSync("git", ["-C", c.repo, "worktree", "add", "-q", "--detach", wt, "HEAD"]);
162+ bundle(wt, NAMES_ITSELF, c.head);
163+ const r = launch({ repo: wt, home: c.home }, ["status"]);
164+ assert.match(r.stdout, /^bundle /);
165+ });
167+ it("keeps the background step quiet when it can't fetch or build", () => {
168+ const c = checkout();
169+ const r = launch(c, [], { ABSO_LAUNCH_UPDATE: "1" });
170+ assert.equal(r.status, 0);
171+ assert.equal(r.stdout, "");
172+ assert.equal(existsSync(join(c.repo, "client", "dist", "abso.mjs.commit")), false);
173+ });
174+});
MCP
compare on this site's /mcp
CLI
abso diff 2 318 client/test/launch.test.ts --json
JSON
/christopher/abso-core/compare/2..318?file=client%2Ftest%2Flaunch.test.ts&format=json