{"draft":null,"from":7,"to":318,"files":[{"path":"client/test/launch.test.ts","added":174,"removed":0,"diff":"--- /dev/null\n+++ b/client/test/launch.test.ts\n@@ -0,0 +1,174 @@\n+// client/launch.mjs: which client a run takes, and that a bad bundle never stops a command.\n+// Each test runs the real launch.mjs from a throwaway git checkout, with a stand-in client, and a home of its own.\n+\n+import assert from \"node:assert/strict\";\n+import { execFileSync, spawnSync } from \"node:child_process\";\n+import {\n+\tcopyFileSync,\n+\texistsSync,\n+\tmkdirSync,\n+\tmkdtempSync,\n+\treaddirSync,\n+\treadFileSync,\n+\trmSync,\n+\tutimesSync,\n+\twriteFileSync,\n+} from \"node:fs\";\n+import { tmpdir } from \"node:os\";\n+import { join } from \"node:path\";\n+import { after, describe, it } from \"node:test\";\n+\n+const LAUNCH = new URL(\"../launch.mjs\", import.meta.url);\n+const roots: string[] = [];\n+after(() => {\n+\tfor (const root of roots) rmSync(root, { recursive: true, force: true });\n+});\n+\n+/** A checkout on main with one commit, launch.mjs and a stand-in client/abso.ts that names itself. */\n+function checkout(): { repo: string; home: string; head: string } {\n+\tconst root = mkdtempSync(join(tmpdir(), \"abso-launch-\"));\n+\troots.push(root);\n+\tconst repo = join(root, \"repo\");\n+\tconst home = join(root, \"home\");\n+\tmkdirSync(join(repo, \"client\"), { recursive: true });\n+\tcopyFileSync(LAUNCH, join(repo, \"client\", \"launch.mjs\"));\n+\twriteFileSync(\n+\t\tjoin(repo, \"client\", \"abso.ts\"),\n+\t\t'console.log(\"ts \" + JSON.stringify(process.argv.slice(2)));\\nprocess.exitCode = Number(process.env.FAKE_EXIT ?? 0);\\n',\n+\t);\n+\tconst git = (...a: string[]) => execFileSync(\"git\", [\"-C\", repo, ...a], { encoding: \"utf8\" });\n+\tgit(\"init\", \"-q\", \"-b\", \"main\");\n+\tgit(\"add\", \".\");\n+\tgit(\"-c\", \"user.name=t\", \"-c\", \"user.email=t@example.com\", \"commit\", \"-q\", \"-m\", \"one\");\n+\t// A stamp from now: no background step starts during a test.\n+\tmkdirSync(join(home, \".abso\"), { recursive: true });\n+\twriteFileSync(join(home, \".abso\", \"client-update.stamp\"), \"now\\n\");\n+\treturn { repo, home, head: git(\"rev-parse\", \"HEAD\").trim() };\n+}\n+\n+function bundle(repo: string, text: string, commit: string): void {\n+\tmkdirSync(join(repo, \"client\", \"dist\"), { recursive: true });\n+\twriteFileSync(join(repo, \"client\", \"dist\", \"abso.mjs\"), text);\n+\twriteFileSync(join(repo, \"client\", \"dist\", \"abso.mjs.commit\"), `${commit}\\n`);\n+}\n+\n+function launch(c: { repo: string; home: string }, args: string[], env: Record<string, string> = {}) {\n+\treturn spawnSync(process.execPath, [join(c.repo, \"client\", \"launch.mjs\"), ...args], {\n+\t\tencoding: \"utf8\",\n+\t\tenv: { ...process.env, HOME: c.home, USERPROFILE: c.home, ...env },\n+\t});\n+}\n+\n+const NAMES_ITSELF = 'console.log(\"bundle \" + JSON.stringify(process.argv.slice(1)));\\n';\n+\n+describe(\"client/launch.mjs\", () => {\n+\tit(\"runs client/abso.ts when there's no bundle, with the arguments and exit code passed through\", () => {\n+\t\tconst c = checkout();\n+\t\tconst r = launch(c, [\"issue\", \"42\"], { FAKE_EXIT: \"3\" });\n+\t\tassert.equal(r.stdout.trim(), 'ts [\"issue\",\"42\"]');\n+\t\tassert.equal(r.status, 3);\n+\t});\n+\n+\tit(\"runs the bundle in its own process when it was built from the checkout's commit\", () => {\n+\t\tconst c = checkout();\n+\t\tbundle(c.repo, NAMES_ITSELF, c.head);\n+\t\tconst r = launch(c, [\"--version\"]);\n+\t\tassert.equal(\n+\t\t\tr.stdout.trim(),\n+\t\t\t`bundle ${JSON.stringify([join(c.repo, \"client\", \"dist\", \"abso.mjs\"), \"--version\"])}`,\n+\t\t);\n+\t\tassert.equal(r.status, 0);\n+\t});\n+\n+\tit(\"runs client/abso.ts when the bundle is from another commit\", () => {\n+\t\tconst c = checkout();\n+\t\tbundle(c.repo, NAMES_ITSELF, \"0\".repeat(40));\n+\t\tassert.equal(launch(c, [\"status\"]).stdout.trim(), 'ts [\"status\"]');\n+\t});\n+\n+\tit(\"falls back to client/abso.ts in the same run when the bundle doesn't load, and writes one line\", () => {\n+\t\tconst c = checkout();\n+\t\tbundle(c.repo, \"this is not javascript (\\n\", c.head);\n+\t\tconst r = launch(c, [\"log\"]);\n+\t\tassert.equal(r.stdout.trim(), 'ts [\"log\"]');\n+\t\tassert.equal(r.status, 0);\n+\t\t// The launcher's own notice goes to its log only, never on the command's output.\n+\t\tassert.equal(r.stderr, \"\");\n+\t\tconst log = readFileSync(join(c.home, \".abso\", \"launcher.log\"), \"utf8\")\n+\t\t\t.trim()\n+\t\t\t.split(\"\\n\");\n+\t\tassert.equal(log.length, 1);\n+\t\tassert.match(log[0]!, /the bundle didn't load \\(SyntaxError/);\n+\t});\n+\n+\tit(\"doesn't run a command twice when it fails inside a bundle that loaded\", () => {\n+\t\tconst c = checkout();\n+\t\tbundle(c.repo, 'throw new Error(\"the command failed\");\\n', c.head);\n+\t\tconst r = launch(c, [\"push\"]);\n+\t\tassert.notEqual(r.status, 0);\n+\t\tassert.doesNotMatch(r.stdout, /^ts /m);\n+\t\tassert.equal(existsSync(join(c.home, \".abso\", \"launcher.log\")), false);\n+\t});\n+\n+\tit(\"doesn't run a command twice when it throws a SyntaxError after the bundle started\", () => {\n+\t\tconst c = checkout();\n+\t\tbundle(c.repo, 'globalThis.__absoBundleStarted = true;\\nJSON.parse(\"{\");\\n', c.head);\n+\t\tconst r = launch(c, [\"push\"]);\n+\t\tassert.notEqual(r.status, 0);\n+\t\tassert.doesNotMatch(r.stdout, /^ts /m);\n+\t\tassert.equal(existsSync(join(c.home, \".abso\", \"launcher.log\")), false);\n+\t});\n+\n+\tit(\"passes stdin through to the client, from the bundle and from the .ts\", () => {\n+\t\tconst c = checkout();\n+\t\tconst echo = 'let s = \"\";\\nprocess.stdin.on(\"data\", (d) => (s += d)).on(\"end\", () => console.log(\"got \" + s));\\n';\n+\t\twriteFileSync(join(c.repo, \"client\", \"abso.ts\"), echo);\n+\t\tconst fed = (input: string) =>\n+\t\t\tspawnSync(process.execPath, [join(c.repo, \"client\", \"launch.mjs\")], {\n+\t\t\t\tinput,\n+\t\t\t\tencoding: \"utf8\",\n+\t\t\t\tenv: { ...process.env, HOME: c.home, USERPROFILE: c.home },\n+\t\t\t}).stdout.trim();\n+\t\tassert.equal(fed(\"from the ts\"), \"got from the ts\");\n+\t\tbundle(c.repo, echo, c.head);\n+\t\tassert.equal(fed(\"from the bundle\"), \"got from the bundle\");\n+\t});\n+\n+\tit(\"runs one background step at a time: a second finds the lock taken and leaves it\", () => {\n+\t\tconst c = checkout();\n+\t\tconst lock = join(c.home, \".abso\", \"client-update.lock\");\n+\t\tmkdirSync(lock);\n+\t\tassert.equal(launch(c, [], { ABSO_LAUNCH_UPDATE: \"1\" }).status, 0);\n+\t\tassert.equal(existsSync(lock), true);\n+\t});\n+\n+\tit(\"takes over a lock a dead run left behind, and removes it after\", () => {\n+\t\tconst c = checkout();\n+\t\tconst lock = join(c.home, \".abso\", \"client-update.lock\");\n+\t\tmkdirSync(lock);\n+\t\tconst old = new Date(Date.now() - 11 * 60 * 1000);\n+\t\tutimesSync(lock, old, old);\n+\t\tassert.equal(launch(c, [], { ABSO_LAUNCH_UPDATE: \"1\" }).status, 0);\n+\t\tassert.deepEqual(\n+\t\t\treaddirSync(join(c.home, \".abso\")).filter((f) => f.includes(\"lock\")),\n+\t\t\t[],\n+\t\t);\n+\t});\n+\n+\tit(\"reads the commit in a git worktree, where .git is a file\", () => {\n+\t\tconst c = checkout();\n+\t\tconst wt = join(c.repo, \"..\", \"wt\");\n+\t\texecFileSync(\"git\", [\"-C\", c.repo, \"worktree\", \"add\", \"-q\", \"--detach\", wt, \"HEAD\"]);\n+\t\tbundle(wt, NAMES_ITSELF, c.head);\n+\t\tconst r = launch({ repo: wt, home: c.home }, [\"status\"]);\n+\t\tassert.match(r.stdout, /^bundle /);\n+\t});\n+\n+\tit(\"keeps the background step quiet when it can't fetch or build\", () => {\n+\t\tconst c = checkout();\n+\t\tconst r = launch(c, [], { ABSO_LAUNCH_UPDATE: \"1\" });\n+\t\tassert.equal(r.status, 0);\n+\t\tassert.equal(r.stdout, \"\");\n+\t\tassert.equal(existsSync(join(c.repo, \"client\", \"dist\", \"abso.mjs.commit\")), false);\n+\t});\n+});"}],"unmatched":[]}