zcode-distribution-smoke.mjs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // 在仓库外验证发行包,避免开发机 node_modules 掩盖缺失的 TUI/native/worker 依赖。
  2. import assert from "node:assert/strict";
  3. import { execFile, spawn } from "node:child_process";
  4. import { once } from "node:events";
  5. import { mkdir, mkdtemp, realpath, rm, writeFile } from "node:fs/promises";
  6. import { createRequire } from "node:module";
  7. import { tmpdir } from "node:os";
  8. import { join, resolve } from "node:path";
  9. import { setTimeout } from "node:timers/promises";
  10. import { promisify } from "node:util";
  11. import { pathToFileURL } from "node:url";
  12. const exec = promisify(execFile);
  13. const archive = process.argv[2];
  14. assert.ok(archive, "Usage: node scripts/zcode-distribution-smoke.mjs <archive.tar.gz>");
  15. const directory = await realpath(await mkdtemp(join(tmpdir(), "zcode-release-smoke-")));
  16. const root = join(directory, "zcode");
  17. const runner = join(root, "bin/zcode.mjs");
  18. const workspace = join(directory, "workspace");
  19. const env = {
  20. ...process.env,
  21. ZCODE_DATA_BASE_DIR: join(directory, "data"),
  22. NODE_PATH: "",
  23. NODE_OPTIONS: "",
  24. TERM: "xterm-256color",
  25. };
  26. let web;
  27. let terminal;
  28. try {
  29. await exec("tar", ["-xzf", resolve(archive), "-C", directory]);
  30. await mkdir(workspace);
  31. await exec(process.execPath, [runner, "--help"], { cwd: workspace, env });
  32. const version = (
  33. await exec(process.execPath, [runner, "--version"], { cwd: workspace, env })
  34. ).stdout.trim();
  35. const require = createRequire(join(root, "package.json"));
  36. const pty = require("node-pty");
  37. const runtimeCheck = join(root, "agent/check-tui.mjs");
  38. await writeFile(
  39. runtimeCheck,
  40. 'import { runTui } from "@zcode/tui"; if (typeof runTui !== "function") throw new Error("Missing TUI export"); console.log("tui-runtime-ok");',
  41. );
  42. const imported = await exec(process.execPath, [runtimeCheck], { cwd: workspace, env });
  43. assert.match(imported.stdout, /tui-runtime-ok/);
  44. terminal = pty.spawn(process.execPath, [runner], { cwd: workspace, env, cols: 110, rows: 32 });
  45. let screen = "";
  46. let terminalExit;
  47. terminal.onData((data) => {
  48. screen += data;
  49. });
  50. const tuiExited = new Promise((done) =>
  51. terminal.onExit((event) => {
  52. terminalExit = event;
  53. done(event);
  54. }),
  55. );
  56. await until(
  57. () => /ZCode/.test(screen) && /(?:登录|\/login|输入提示词|Type a prompt)/i.test(screen),
  58. "TUI initialized render",
  59. () => screen,
  60. );
  61. assert.equal(terminalExit, undefined, screen);
  62. assert.doesNotMatch(screen, /Cannot find (?:module|package)|ERR_MODULE_NOT_FOUND/);
  63. // 保留真实键盘退出链路;不发送 prompt,不调用模型。
  64. terminal.write("\u0003");
  65. await setTimeout(200);
  66. if (!terminalExit) terminal.write("\u0003");
  67. const tuiExit = await Promise.race([
  68. tuiExited,
  69. setTimeout(8000).then(() => {
  70. throw new Error("TUI keyboard exit timed out");
  71. }),
  72. ]);
  73. assert.equal(tuiExit.exitCode, 0, screen);
  74. terminal = undefined;
  75. let webOutput = "";
  76. web = spawn(process.execPath, [runner, "--web", "--workspace", workspace, "--no-open"], {
  77. cwd: workspace,
  78. env,
  79. stdio: ["ignore", "pipe", "pipe"],
  80. });
  81. web.stdout.on("data", (data) => {
  82. webOutput += data;
  83. });
  84. web.stderr.on("data", (data) => {
  85. webOutput += data;
  86. });
  87. let base;
  88. await until(
  89. () => {
  90. base = webOutput.match(/Local:\s+(http:\/\/127\.0\.0\.1:\d+\/)/)?.[1];
  91. return Boolean(base);
  92. },
  93. "Web URL",
  94. () => webOutput,
  95. );
  96. let info;
  97. await until(
  98. async () => {
  99. try {
  100. const response = await fetch(new URL("api/server-info", base), {
  101. signal: AbortSignal.timeout(1000),
  102. });
  103. if (!response.ok) return false;
  104. info = await response.json();
  105. return true;
  106. } catch {
  107. return false;
  108. }
  109. },
  110. "Web readiness",
  111. () => webOutput,
  112. );
  113. assert.equal(info.workspaces[0].path, workspace);
  114. const html = await fetch(base);
  115. assert.equal(html.status, 200);
  116. assert.match(await html.text(), /<html/i);
  117. const { default: WebSocket } = await import(pathToFileURL(require.resolve("ws")).href);
  118. const socket = new WebSocket(new URL("ws", base.replace("http:", "ws:")));
  119. await once(socket, "open");
  120. socket.close();
  121. await once(socket, "close");
  122. const exited = once(web, "exit");
  123. web.kill("SIGTERM");
  124. assert.deepEqual(await exited, [0, null]);
  125. web = undefined;
  126. console.log(
  127. JSON.stringify({
  128. version,
  129. platform: process.platform,
  130. arch: process.arch,
  131. tui: "native import, initialized render, keyboard exit passed",
  132. web: "HTML, server-info, workspace, WebSocket, shutdown passed",
  133. isolated: true,
  134. }),
  135. );
  136. } finally {
  137. terminal?.kill();
  138. web?.kill();
  139. await rm(directory, { recursive: true, force: true });
  140. }
  141. async function until(check, label, diagnostic) {
  142. const deadline = Date.now() + 20000;
  143. while (Date.now() < deadline) {
  144. if (await check()) return;
  145. await setTimeout(100);
  146. }
  147. throw new Error(`${label} timed out:\n${diagnostic()}`);
  148. }