| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- import assert from "node:assert/strict";
- import { execFile, spawn } from "node:child_process";
- import { once } from "node:events";
- import { cp, mkdir, mkdtemp, readFile, realpath, rm, writeFile } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { setTimeout } from "node:timers/promises";
- import { promisify } from "node:util";
- import { test } from "node:test";
- import { patchNodePtyPrebuilds } from "./zcode-distribution/assets.mjs";
- import { installScriptSource } from "./zcode-distribution/installer.mjs";
- const exec = promisify(execFile);
- const runnerSource = new URL("./zcode-distribution/runner.mjs", import.meta.url);
- async function fixture(t) {
- const directory = await realpath(await mkdtemp(join(tmpdir(), "zcode-distribution-")));
- t.after(() => rm(directory, { recursive: true, force: true }));
- const root = join(directory, "zcode");
- for (const name of ["bin", "agent", "web", "server"]) {
- await mkdir(join(root, name), { recursive: true });
- }
- await cp(runnerSource, join(root, "bin/zcode.mjs"));
- await writeFile(join(root, "package.json"), JSON.stringify({ type: "module", version: "1.2.3" }));
- await writeFile(
- join(root, "agent/zcode.cjs"),
- `
- process.stdout.write(JSON.stringify({ argv: process.argv.slice(2), entry: process.argv[1], cwd: process.cwd() }));
- process.exitCode = Number(process.env.FIXTURE_EXIT_CODE || 0);
- `,
- );
- await writeFile(
- join(root, "server/entry-http.js"),
- `
- import { writeFile } from 'node:fs/promises';
- await writeFile(process.env.FIXTURE_WEB_RESULT, JSON.stringify({
- cwd: process.cwd(), port: process.env.PORT, host: process.env.ZCODE_SERVER_HOST,
- workspace: process.env.ZCODE_SERVER_WORKSPACE, staticRoot: process.env.ZCODE_WEB_STATIC_ROOT,
- command: process.env.ZCODE_AGENT_SERVER_COMMAND, args: JSON.parse(process.env.ZCODE_AGENT_SERVER_ARGS_JSON),
- token: process.env.ZCODE_SERVER_AUTH_TOKEN,
- }));
- process.on('SIGTERM', () => process.exit(0));
- setInterval(() => {}, 1000);
- `,
- );
- return { directory, root, runner: join(root, "bin/zcode.mjs") };
- }
- test("default launch and CLI arguments reuse the Agent entry without starting Web", async (t) => {
- const { runner, root, directory } = await fixture(t);
- for (const args of [[], ["tui"], ["--prompt", "--web"], ["app-server", "--stdio"]]) {
- const result = await exec(process.execPath, [runner, ...args], { cwd: directory });
- assert.deepEqual(JSON.parse(result.stdout), {
- argv: args,
- entry: join(root, "agent/zcode.cjs"),
- cwd: directory,
- });
- }
- await assert.rejects(
- exec(process.execPath, [runner], { env: { ...process.env, FIXTURE_EXIT_CODE: "17" } }),
- (error) => error.code === 17,
- );
- });
- test("help exposes Web mode and preserves CLI help; version uses the distribution", async (t) => {
- const { runner } = await fixture(t);
- assert.equal((await exec(process.execPath, [runner, "--version"])).stdout.trim(), "1.2.3");
- const help = (await exec(process.execPath, [runner, "--help"])).stdout;
- assert.match(help, /zcode --web/);
- assert.match(help, /"argv":\["--help"\]/);
- const webHelp = (await exec(process.execPath, [runner, "--web", "--help"])).stdout;
- assert.match(webHelp, /--workspace/);
- assert.doesNotMatch(webHelp, /"argv"/);
- await assert.rejects(exec(process.execPath, [runner, "--web", "--unknown"]), /Unknown option/);
- await assert.rejects(
- exec(process.execPath, [runner, "--web", "--port", "NaN"]),
- /--port must be an integer/,
- );
- });
- test("Web startup errors preserve a failing exit code", async (t) => {
- const { runner, directory, root } = await fixture(t);
- await assert.rejects(
- exec(process.execPath, [
- runner,
- "--web",
- "--workspace",
- join(directory, "missing"),
- "--no-open",
- ]),
- /Unable to start Web server/,
- );
- await rm(join(root, "server/entry-http.js"));
- await assert.rejects(
- exec(process.execPath, [runner, "--web", "--no-open"]),
- /Missing runtime file/,
- );
- });
- test("Web mode passes its runtime configuration and terminates its child on shutdown", async (t) => {
- const { directory, runner, root } = await fixture(t);
- const resultPath = join(directory, "web-result.json");
- const child = spawn(
- process.execPath,
- [
- runner,
- "--web",
- "--workspace",
- directory,
- "--host",
- "127.0.0.1",
- "--port",
- "3041",
- "--no-open",
- "--no-token",
- ],
- {
- env: {
- ...process.env,
- FIXTURE_WEB_RESULT: resultPath,
- ZCODE_SERVER_AUTH_TOKEN: "stale-token",
- },
- stdio: ["ignore", "pipe", "pipe"],
- },
- );
- t.after(() => {
- if (child.exitCode === null) child.kill();
- });
- let result;
- for (let attempt = 0; attempt < 100; attempt++) {
- result = await readFile(resultPath, "utf8")
- .then(JSON.parse)
- .catch(() => undefined);
- if (result) break;
- await setTimeout(20);
- }
- assert.deepEqual(result, {
- cwd: directory,
- port: "3041",
- host: "127.0.0.1",
- workspace: directory,
- staticRoot: join(root, "web"),
- command: process.execPath,
- args: [join(root, "agent/zcode.cjs"), "app-server", "--stdio"],
- token: "",
- });
- const exited = once(child, "exit");
- child.kill("SIGTERM");
- assert.deepEqual(await exited, [0, null]);
- });
- test("installer creates zcode and the installed command defaults to CLI", async (t) => {
- const { directory, root } = await fixture(t);
- const downloads = join(directory, "downloads");
- const releaseDir = join(downloads, "releases/1.2.3");
- await mkdir(releaseDir, { recursive: true });
- await exec("tar", ["-czf", join(releaseDir, "zcode-1.2.3.tar.gz"), "-C", directory, "zcode"]);
- await writeFile(
- join(downloads, "latest.json"),
- JSON.stringify({ version: "1.2.3", tarball: "zcode-1.2.3.tar.gz" }),
- );
- const installer = join(directory, "install.sh");
- await writeFile(installer, installScriptSource(`file://${downloads}/`));
- const installRoot = join(directory, "installed");
- const bin = join(directory, "bin");
- await exec("sh", [installer], {
- env: {
- ...process.env,
- ZCODE_DIST_BASE_URL: `file://${downloads}/`,
- ZCODE_DIST_HOME: installRoot,
- ZCODE_DIST_BIN_DIR: bin,
- },
- });
- await rm(root, { recursive: true });
- const result = await exec(join(bin, "zcode"), [], { cwd: directory });
- assert.deepEqual(JSON.parse(result.stdout).argv, []);
- const webHelp = await exec(join(bin, "zcode"), ["--web", "--help"]);
- assert.match(webHelp.stdout, /zcode --web/);
- });
- test("node-pty prebuild patch keeps the official native that matches the official JS", async (t) => {
- const directory = await realpath(await mkdtemp(join(tmpdir(), "zcode-distribution-pty-")));
- t.after(() => rm(directory, { recursive: true, force: true }));
- const prebuilds = join(directory, "node_modules/node-pty/prebuilds");
- await mkdir(join(prebuilds, "darwin-arm64"), { recursive: true });
- await writeFile(join(prebuilds, "darwin-arm64/pty.node"), "official-1.1.0");
- await patchNodePtyPrebuilds(directory);
- // Bug 回归:旧补丁用平台包 1.2.0-beta.10 的 darwin native 覆盖官方 1.1.0 native,
- // 官方 JS 只传 3 个 resize 参数,新 native 要 5 个,macOS 终端 resize 必然失败。
- assert.equal(await readFile(join(prebuilds, "darwin-arm64/pty.node"), "utf8"), "official-1.1.0");
- // 官方包缺失的平台仍由平台包补齐。
- assert.ok((await readFile(join(prebuilds, "linux-x64/pty.node"))).length > 0);
- });
|