zcode-distribution.test.mjs 7.3 KB

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