zcode-distribution-smoke.mjs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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, stripVTControlCharacters } 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. () => {
  58. // 修复:TUI 渲染器会在相邻字符之间插入 SGR/光标定位序列,原始 PTY 输出里的提示词不连续;
  59. // 先去掉终端控制序列再匹配可见文本,避免界面已就绪却被判定为超时。
  60. const visible = stripVTControlCharacters(screen);
  61. return /ZCode/.test(visible) && /(?:登录|\/login|输入提示词|Type a prompt)/i.test(visible);
  62. },
  63. "TUI initialized render",
  64. () => stripVTControlCharacters(screen),
  65. );
  66. assert.equal(terminalExit, undefined, screen);
  67. assert.doesNotMatch(screen, /Cannot find (?:module|package)|ERR_MODULE_NOT_FOUND/);
  68. // 保留真实键盘退出链路;不发送 prompt,不调用模型。
  69. terminal.write("\u0003");
  70. await setTimeout(200);
  71. if (!terminalExit) terminal.write("\u0003");
  72. const tuiExit = await Promise.race([
  73. tuiExited,
  74. setTimeout(8000).then(() => {
  75. throw new Error("TUI keyboard exit timed out");
  76. }),
  77. ]);
  78. assert.equal(tuiExit.exitCode, 0, screen);
  79. terminal = undefined;
  80. let webOutput = "";
  81. web = spawn(process.execPath, [runner, "--web", "--workspace", workspace, "--no-open"], {
  82. cwd: workspace,
  83. env,
  84. stdio: ["ignore", "pipe", "pipe"],
  85. });
  86. web.stdout.on("data", (data) => {
  87. webOutput += data;
  88. });
  89. web.stderr.on("data", (data) => {
  90. webOutput += data;
  91. });
  92. let base;
  93. await until(
  94. () => {
  95. base = webOutput.match(/Local:\s+(http:\/\/127\.0\.0\.1:\d+\/)/)?.[1];
  96. return Boolean(base);
  97. },
  98. "Web URL",
  99. () => webOutput,
  100. );
  101. let info;
  102. await until(
  103. async () => {
  104. try {
  105. const response = await fetch(new URL("api/server-info", base), {
  106. signal: AbortSignal.timeout(1000),
  107. });
  108. if (!response.ok) return false;
  109. info = await response.json();
  110. return true;
  111. } catch {
  112. return false;
  113. }
  114. },
  115. "Web readiness",
  116. () => webOutput,
  117. );
  118. assert.equal(info.workspaces[0].path, workspace);
  119. const html = await fetch(base);
  120. assert.equal(html.status, 200);
  121. assert.match(await html.text(), /<html/i);
  122. const { default: WebSocket } = await import(pathToFileURL(require.resolve("ws")).href);
  123. const socket = new WebSocket(new URL("ws", base.replace("http:", "ws:")));
  124. await once(socket, "open");
  125. socket.close();
  126. await once(socket, "close");
  127. const exited = once(web, "exit");
  128. web.kill("SIGTERM");
  129. assert.deepEqual(await exited, [0, null]);
  130. web = undefined;
  131. console.log(
  132. JSON.stringify({
  133. version,
  134. platform: process.platform,
  135. arch: process.arch,
  136. tui: "native import, initialized render, keyboard exit passed",
  137. web: "HTML, server-info, workspace, WebSocket, shutdown passed",
  138. isolated: true,
  139. }),
  140. );
  141. } finally {
  142. terminal?.kill();
  143. web?.kill();
  144. await rm(directory, { recursive: true, force: true });
  145. }
  146. async function until(check, label, diagnostic) {
  147. const deadline = Date.now() + 20000;
  148. while (Date.now() < deadline) {
  149. if (await check()) return;
  150. await setTimeout(100);
  151. }
  152. throw new Error(`${label} timed out:\n${diagnostic()}`);
  153. }