- MCP
- blame on this site's /mcp
- CLI
- abso blame client/test/launch.test.ts --draft login-timeout --json
- JSON
- /christopher/abso-core/blame/client/test/launch.test.ts?draft=login-timeout&format=json
174 linesspeed 100%Last change speed in version 42,
174 lines from 7 changes in 2 versions.
speed38client/launch.mjs: run the client from a git checkout and keep it current. It runs a plain-JS bundle of the client in the same process when the bundle matches the checkout's commit, and client/abso.ts otherwise. A background step, one at a time under a lock, fast-forwards main, installs when package-lock.json changed, and builds the bundle (temporary file, checked, then renamed into place). stdin, stdout, stderr, arguments and exit codes pass through; a bundle that fails to load falls back to the .ts in the same run, and a command that started is never run twice. Notices go only to ~/.abso/launcher.log. Tests run it against a throwaway checkout and home.Compare1// 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.
4import assert from "node:assert/strict";
5import { execFileSync, spawnSync } from "node:child_process";
speed42client/launch.mjs follow-ups from review: reads the commit in a git worktree too (.git as a file, following gitdir and commondir; info/exclude in the shared folder); a stale lock is renamed aside before it's removed, and put back if it turns out to be live, so two runs can't both take it over; the README says to point it at a checkout nobody edits in; the tests remove their temporary folders.Compare6import {
7 copyFileSync,
8 existsSync,
9 mkdirSync,
10 mkdtempSync,
11 readdirSync,
12 readFileSync,
13 rmSync,
14 utimesSync,
15 writeFileSync,
16} from "node:fs";
speed38client/launch.mjs: run the client from a git checkout and keep it current. It runs a plain-JS bundle of the client in the same process when the bundle matches the checkout's commit, and client/abso.ts otherwise. A background step, one at a time under a lock, fast-forwards main, installs when package-lock.json changed, and builds the bundle (temporary file, checked, then renamed into place). stdin, stdout, stderr, arguments and exit codes pass through; a bundle that fails to load falls back to the .ts in the same run, and a command that started is never run twice. Notices go only to ~/.abso/launcher.log. Tests run it against a throwaway checkout and home.Compare17import { tmpdir } from "node:os";
18import { join } from "node:path";
speed42client/launch.mjs follow-ups from review: reads the commit in a git worktree too (.git as a file, following gitdir and commondir; info/exclude in the shared folder); a stale lock is renamed aside before it's removed, and put back if it turns out to be live, so two runs can't both take it over; the README says to point it at a checkout nobody edits in; the tests remove their temporary folders.Compare19import { after, describe, it } from "node:test";
speed38client/launch.mjs: run the client from a git checkout and keep it current. It runs a plain-JS bundle of the client in the same process when the bundle matches the checkout's commit, and client/abso.ts otherwise. A background step, one at a time under a lock, fast-forwards main, installs when package-lock.json changed, and builds the bundle (temporary file, checked, then renamed into place). stdin, stdout, stderr, arguments and exit codes pass through; a bundle that fails to load falls back to the .ts in the same run, and a command that started is never run twice. Notices go only to ~/.abso/launcher.log. Tests run it against a throwaway checkout and home.Compare20
21const LAUNCH = new URL("../launch.mjs", import.meta.url);
speed42client/launch.mjs follow-ups from review: reads the commit in a git worktree too (.git as a file, following gitdir and commondir; info/exclude in the shared folder); a stale lock is renamed aside before it's removed, and put back if it turns out to be live, so two runs can't both take it over; the README says to point it at a checkout nobody edits in; the tests remove their temporary folders.Compare22const roots: string[] = [];
23after(() => {
24 for (const root of roots) rmSync(root, { recursive: true, force: true });
25});
speed38client/launch.mjs: run the client from a git checkout and keep it current. It runs a plain-JS bundle of the client in the same process when the bundle matches the checkout's commit, and client/abso.ts otherwise. A background step, one at a time under a lock, fast-forwards main, installs when package-lock.json changed, and builds the bundle (temporary file, checked, then renamed into place). stdin, stdout, stderr, arguments and exit codes pass through; a bundle that fails to load falls back to the .ts in the same run, and a command that started is never run twice. Notices go only to ~/.abso/launcher.log. Tests run it against a throwaway checkout and home.Compare26
27/** A checkout on main with one commit, launch.mjs and a stand-in client/abso.ts that names itself. */
28function checkout(): { repo: string; home: string; head: string } {
29 const root = mkdtempSync(join(tmpdir(), "abso-launch-"));
speed42client/launch.mjs follow-ups from review: reads the commit in a git worktree too (.git as a file, following gitdir and commondir; info/exclude in the shared folder); a stale lock is renamed aside before it's removed, and put back if it turns out to be live, so two runs can't both take it over; the README says to point it at a checkout nobody edits in; the tests remove their temporary folders.Compare30 roots.push(root);
speed38client/launch.mjs: run the client from a git checkout and keep it current. It runs a plain-JS bundle of the client in the same process when the bundle matches the checkout's commit, and client/abso.ts otherwise. A background step, one at a time under a lock, fast-forwards main, installs when package-lock.json changed, and builds the bundle (temporary file, checked, then renamed into place). stdin, stdout, stderr, arguments and exit codes pass through; a bundle that fails to load falls back to the .ts in the same run, and a command that started is never run twice. Notices go only to ~/.abso/launcher.log. Tests run it against a throwaway checkout and home.Compare31 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}
49function 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}
55function 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}
62const NAMES_ITSELF = 'console.log("bundle " + JSON.stringify(process.argv.slice(1)));\n';
64describe("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, ["publish"]);
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, ["publish"]);
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 });
speed42client/launch.mjs follow-ups from review: reads the commit in a git worktree too (.git as a file, following gitdir and commondir; info/exclude in the shared folder); a stale lock is renamed aside before it's removed, and put back if it turns out to be live, so two runs can't both take it over; the README says to point it at a checkout nobody edits in; the tests remove their temporary folders.Compare145 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 });
speed38client/launch.mjs: run the client from a git checkout and keep it current. It runs a plain-JS bundle of the client in the same process when the bundle matches the checkout's commit, and client/abso.ts otherwise. A background step, one at a time under a lock, fast-forwards main, installs when package-lock.json changed, and builds the bundle (temporary file, checked, then renamed into place). stdin, stdout, stderr, arguments and exit codes pass through; a bundle that fails to load falls back to the .ts in the same run, and a command that started is never run twice. Notices go only to ~/.abso/launcher.log. Tests run it against a throwaway checkout and home.Compare167 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});