- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/test/launch.test.ts?version=41&format=json
137 linesspeed 100%Last change speed in version 38, 1 version changed it
You're reading version 41. The newest is 318, and 2 versions since then changed this file.
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.
4import assert from "node:assert/strict";
5import { execFileSync, spawnSync } from "node:child_process";
6import { copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
7import { tmpdir } from "node:os";
8import { join } from "node:path";
9import { describe, it } from "node:test";
11const LAUNCH = new URL("../launch.mjs", import.meta.url);
13/** A checkout on main with one commit, launch.mjs and a stand-in client/abso.ts that names itself. */
14function checkout(): { repo: string; home: string; head: string } {
15 const root = mkdtempSync(join(tmpdir(), "abso-launch-"));
16 const repo = join(root, "repo");
17 const home = join(root, "home");
18 mkdirSync(join(repo, "client"), { recursive: true });
19 copyFileSync(LAUNCH, join(repo, "client", "launch.mjs"));
20 writeFileSync(
21 join(repo, "client", "abso.ts"),
22 'console.log("ts " + JSON.stringify(process.argv.slice(2)));\nprocess.exitCode = Number(process.env.FAKE_EXIT ?? 0);\n',
23 );
24 const git = (...a: string[]) => execFileSync("git", ["-C", repo, ...a], { encoding: "utf8" });
25 git("init", "-q", "-b", "main");
26 git("add", ".");
27 git("-c", "user.name=t", "-c", "user.email=t@example.com", "commit", "-q", "-m", "one");
28 // A stamp from now: no background step starts during a test.
29 mkdirSync(join(home, ".abso"), { recursive: true });
30 writeFileSync(join(home, ".abso", "client-update.stamp"), "now\n");
31 return { repo, home, head: git("rev-parse", "HEAD").trim() };
32}
34function bundle(repo: string, text: string, commit: string): void {
35 mkdirSync(join(repo, "client", "dist"), { recursive: true });
36 writeFileSync(join(repo, "client", "dist", "abso.mjs"), text);
37 writeFileSync(join(repo, "client", "dist", "abso.mjs.commit"), `${commit}\n`);
38}
40function launch(c: { repo: string; home: string }, args: string[], env: Record<string, string> = {}) {
41 return spawnSync(process.execPath, [join(c.repo, "client", "launch.mjs"), ...args], {
42 encoding: "utf8",
43 env: { ...process.env, HOME: c.home, USERPROFILE: c.home, ...env },
44 });
45}
47const NAMES_ITSELF = 'console.log("bundle " + JSON.stringify(process.argv.slice(1)));\n';
49describe("client/launch.mjs", () => {
50 it("runs client/abso.ts when there's no bundle, with the arguments and exit code passed through", () => {
51 const c = checkout();
52 const r = launch(c, ["issue", "42"], { FAKE_EXIT: "3" });
53 assert.equal(r.stdout.trim(), 'ts ["issue","42"]');
54 assert.equal(r.status, 3);
55 });
57 it("runs the bundle in its own process when it was built from the checkout's commit", () => {
58 const c = checkout();
59 bundle(c.repo, NAMES_ITSELF, c.head);
60 const r = launch(c, ["--version"]);
61 assert.equal(
62 r.stdout.trim(),
63 `bundle ${JSON.stringify([join(c.repo, "client", "dist", "abso.mjs"), "--version"])}`,
64 );
65 assert.equal(r.status, 0);
66 });
68 it("runs client/abso.ts when the bundle is from another commit", () => {
69 const c = checkout();
70 bundle(c.repo, NAMES_ITSELF, "0".repeat(40));
71 assert.equal(launch(c, ["status"]).stdout.trim(), 'ts ["status"]');
72 });
74 it("falls back to client/abso.ts in the same run when the bundle doesn't load, and writes one line", () => {
75 const c = checkout();
76 bundle(c.repo, "this is not javascript (\n", c.head);
77 const r = launch(c, ["log"]);
78 assert.equal(r.stdout.trim(), 'ts ["log"]');
79 assert.equal(r.status, 0);
80 // The launcher's own notice goes to its log only, never on the command's output.
81 assert.equal(r.stderr, "");
82 const log = readFileSync(join(c.home, ".abso", "launcher.log"), "utf8")
83 .trim()
84 .split("\n");
85 assert.equal(log.length, 1);
86 assert.match(log[0]!, /the bundle didn't load \(SyntaxError/);
87 });
89 it("doesn't run a command twice when it fails inside a bundle that loaded", () => {
90 const c = checkout();
91 bundle(c.repo, 'throw new Error("the command failed");\n', c.head);
92 const r = launch(c, ["publish"]);
93 assert.notEqual(r.status, 0);
94 assert.doesNotMatch(r.stdout, /^ts /m);
95 assert.equal(existsSync(join(c.home, ".abso", "launcher.log")), false);
96 });
98 it("doesn't run a command twice when it throws a SyntaxError after the bundle started", () => {
99 const c = checkout();
100 bundle(c.repo, 'globalThis.__absoBundleStarted = true;\nJSON.parse("{");\n', c.head);
101 const r = launch(c, ["publish"]);
102 assert.notEqual(r.status, 0);
103 assert.doesNotMatch(r.stdout, /^ts /m);
104 assert.equal(existsSync(join(c.home, ".abso", "launcher.log")), false);
105 });
107 it("passes stdin through to the client, from the bundle and from the .ts", () => {
108 const c = checkout();
109 const echo = 'let s = "";\nprocess.stdin.on("data", (d) => (s += d)).on("end", () => console.log("got " + s));\n';
110 writeFileSync(join(c.repo, "client", "abso.ts"), echo);
111 const fed = (input: string) =>
112 spawnSync(process.execPath, [join(c.repo, "client", "launch.mjs")], {
113 input,
114 encoding: "utf8",
115 env: { ...process.env, HOME: c.home, USERPROFILE: c.home },
116 }).stdout.trim();
117 assert.equal(fed("from the ts"), "got from the ts");
118 bundle(c.repo, echo, c.head);
119 assert.equal(fed("from the bundle"), "got from the bundle");
120 });
122 it("runs one background step at a time: a second finds the lock taken and leaves it", () => {
123 const c = checkout();
124 const lock = join(c.home, ".abso", "client-update.lock");
125 mkdirSync(lock);
126 assert.equal(launch(c, [], { ABSO_LAUNCH_UPDATE: "1" }).status, 0);
127 assert.equal(existsSync(lock), true);
128 });
130 it("keeps the background step quiet when it can't fetch or build", () => {
131 const c = checkout();
132 const r = launch(c, [], { ABSO_LAUNCH_UPDATE: "1" });
133 assert.equal(r.status, 0);
134 assert.equal(r.stdout, "");
135 assert.equal(existsSync(join(c.repo, "client", "dist", "abso.mjs.commit")), false);
136 });
137});