zcode-distribution.test.mjs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import assert from "node:assert/strict";
  2. import { execFile, spawn } from "node:child_process";
  3. import { once } from "node:events";
  4. import { cp, mkdir, mkdtemp, readFile, realpath, rm, writeFile } from "node:fs/promises";
  5. import { tmpdir } from "node:os";
  6. import { join } from "node:path";
  7. import { setTimeout } from "node:timers/promises";
  8. import { promisify } from "node:util";
  9. import { test } from "node:test";
  10. import { installScriptSource } from "./zcode-distribution/installer.mjs";
  11. const exec = promisify(execFile);
  12. const runnerSource = new URL("./zcode-distribution/runner.mjs", import.meta.url);
  13. async function fixture(t) {
  14. const directory = await realpath(await mkdtemp(join(tmpdir(), "zcode-distribution-")));
  15. t.after(() => rm(directory, { recursive: true, force: true }));
  16. const root = join(directory, "zcode");
  17. for (const name of ["bin", "agent", "web", "server"]) {
  18. await mkdir(join(root, name), { recursive: true });
  19. }
  20. await cp(runnerSource, join(root, "bin/zcode.mjs"));
  21. await writeFile(join(root, "package.json"), JSON.stringify({ type: "module", version: "1.2.3" }));
  22. await writeFile(
  23. join(root, "agent/zcode.cjs"),
  24. `
  25. process.stdout.write(JSON.stringify({ argv: process.argv.slice(2), entry: process.argv[1], cwd: process.cwd() }));
  26. process.exitCode = Number(process.env.FIXTURE_EXIT_CODE || 0);
  27. `,
  28. );
  29. await writeFile(
  30. join(root, "server/entry-http.js"),
  31. `
  32. import { writeFile } from 'node:fs/promises';
  33. await writeFile(process.env.FIXTURE_WEB_RESULT, JSON.stringify({
  34. cwd: process.cwd(), port: process.env.PORT, host: process.env.ZCODE_SERVER_HOST,
  35. workspace: process.env.ZCODE_SERVER_WORKSPACE, staticRoot: process.env.ZCODE_WEB_STATIC_ROOT,
  36. command: process.env.ZCODE_AGENT_SERVER_COMMAND, args: JSON.parse(process.env.ZCODE_AGENT_SERVER_ARGS_JSON),
  37. token: process.env.ZCODE_SERVER_AUTH_TOKEN,
  38. }));
  39. process.on('SIGTERM', () => process.exit(0));
  40. setInterval(() => {}, 1000);
  41. `,
  42. );
  43. return { directory, root, runner: join(root, "bin/zcode.mjs") };
  44. }
  45. test("default launch and CLI arguments reuse the Agent entry without starting Web", async (t) => {
  46. const { runner, root, directory } = await fixture(t);
  47. for (const args of [[], ["tui"], ["--prompt", "--web"], ["app-server", "--stdio"]]) {
  48. const result = await exec(process.execPath, [runner, ...args], { cwd: directory });
  49. assert.deepEqual(JSON.parse(result.stdout), {
  50. argv: args,
  51. entry: join(root, "agent/zcode.cjs"),
  52. cwd: directory,
  53. });
  54. }
  55. await assert.rejects(
  56. exec(process.execPath, [runner], { env: { ...process.env, FIXTURE_EXIT_CODE: "17" } }),
  57. (error) => error.code === 17,
  58. );
  59. });
  60. test("help exposes Web mode and preserves CLI help; version uses the distribution", async (t) => {
  61. const { runner } = await fixture(t);
  62. assert.equal((await exec(process.execPath, [runner, "--version"])).stdout.trim(), "1.2.3");
  63. const help = (await exec(process.execPath, [runner, "--help"])).stdout;
  64. assert.match(help, /zcode --web/);
  65. assert.match(help, /"argv":\["--help"\]/);
  66. const webHelp = (await exec(process.execPath, [runner, "--web", "--help"])).stdout;
  67. assert.match(webHelp, /--workspace/);
  68. assert.doesNotMatch(webHelp, /"argv"/);
  69. await assert.rejects(exec(process.execPath, [runner, "--web", "--unknown"]), /Unknown option/);
  70. await assert.rejects(
  71. exec(process.execPath, [runner, "--web", "--port", "NaN"]),
  72. /--port must be an integer/,
  73. );
  74. });
  75. test("Web startup errors preserve a failing exit code", async (t) => {
  76. const { runner, directory, root } = await fixture(t);
  77. await assert.rejects(
  78. exec(process.execPath, [
  79. runner,
  80. "--web",
  81. "--workspace",
  82. join(directory, "missing"),
  83. "--no-open",
  84. ]),
  85. /Unable to start Web server/,
  86. );
  87. await rm(join(root, "server/entry-http.js"));
  88. await assert.rejects(
  89. exec(process.execPath, [runner, "--web", "--no-open"]),
  90. /Missing runtime file/,
  91. );
  92. });
  93. test("Web mode passes its runtime configuration and terminates its child on shutdown", async (t) => {
  94. const { directory, runner, root } = await fixture(t);
  95. const resultPath = join(directory, "web-result.json");
  96. const child = spawn(
  97. process.execPath,
  98. [
  99. runner,
  100. "--web",
  101. "--workspace",
  102. directory,
  103. "--host",
  104. "127.0.0.1",
  105. "--port",
  106. "3041",
  107. "--no-open",
  108. "--no-token",
  109. ],
  110. {
  111. env: {
  112. ...process.env,
  113. FIXTURE_WEB_RESULT: resultPath,
  114. ZCODE_SERVER_AUTH_TOKEN: "stale-token",
  115. },
  116. stdio: ["ignore", "pipe", "pipe"],
  117. },
  118. );
  119. t.after(() => {
  120. if (child.exitCode === null) child.kill();
  121. });
  122. let result;
  123. for (let attempt = 0; attempt < 100; attempt++) {
  124. result = await readFile(resultPath, "utf8")
  125. .then(JSON.parse)
  126. .catch(() => undefined);
  127. if (result) break;
  128. await setTimeout(20);
  129. }
  130. assert.deepEqual(result, {
  131. cwd: directory,
  132. port: "3041",
  133. host: "127.0.0.1",
  134. workspace: directory,
  135. staticRoot: join(root, "web"),
  136. command: process.execPath,
  137. args: [join(root, "agent/zcode.cjs"), "app-server", "--stdio"],
  138. token: "",
  139. });
  140. const exited = once(child, "exit");
  141. child.kill("SIGTERM");
  142. assert.deepEqual(await exited, [0, null]);
  143. });
  144. test("installer creates zcode and the installed command defaults to CLI", async (t) => {
  145. const { directory, root } = await fixture(t);
  146. const downloads = join(directory, "downloads");
  147. const releaseDir = join(downloads, "releases/1.2.3");
  148. await mkdir(releaseDir, { recursive: true });
  149. await exec("tar", ["-czf", join(releaseDir, "zcode-1.2.3.tar.gz"), "-C", directory, "zcode"]);
  150. await writeFile(
  151. join(downloads, "latest.json"),
  152. JSON.stringify({ version: "1.2.3", tarball: "zcode-1.2.3.tar.gz" }),
  153. );
  154. const installer = join(directory, "install.sh");
  155. await writeFile(installer, installScriptSource(`file://${downloads}/`));
  156. const installRoot = join(directory, "installed");
  157. const bin = join(directory, "bin");
  158. await exec("sh", [installer], {
  159. env: {
  160. ...process.env,
  161. ZCODE_DIST_BASE_URL: `file://${downloads}/`,
  162. ZCODE_DIST_HOME: installRoot,
  163. ZCODE_DIST_BIN_DIR: bin,
  164. },
  165. });
  166. await rm(root, { recursive: true });
  167. const result = await exec(join(bin, "zcode"), [], { cwd: directory });
  168. assert.deepEqual(JSON.parse(result.stdout).argv, []);
  169. const webHelp = await exec(join(bin, "zcode"), ["--web", "--help"]);
  170. assert.match(webHelp.stdout, /zcode --web/);
  171. });