dwf-child-command.test.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * 隐藏子命令 `__zcode-dwf-child` 的分派与执行。
  3. *
  4. * Bug 根因(docs/dynamic-workflow/launch.md「Single-executable builds」):SEA 单文件二进制不解释 Node
  5. * CLI 旗标,harness 缺省的 `node --max-old-space-size=… <entry>` 在 SEA 下会把旗标交给严格
  6. * parseArgs,子进程立刻报错退出——SEA 下每一个 dwf run 必然失败。修法与 official plugin host
  7. * 同款:自 re-exec 本二进制 + 在 parseArgs **之前**分派。
  8. *
  9. * 2026-09-09 追记之后 argv 末位是入口文件路径(payload 不再过命令行)。两件事必须钉住:
  10. * 1. 分派发生在 parseArgs 之前——任何进了 parseArgs 的形态都会把路径当未知参数报错;
  11. * 2. 子进程真的跑起来:子命令 `import()` 入口文件、注入真实 vm/readline/stdio 调它的 `start`,
  12. * 把 NDJSON 写回 stdout。入口文件由 harness 同一个 `renderChildEntry` 渲染,与生产同形。
  13. */
  14. import assert from "node:assert/strict";
  15. import { tmpdir } from "node:os";
  16. import { join } from "node:path";
  17. import { Readable } from "node:stream";
  18. import test from "node:test";
  19. import { ZCODE_DWF_CHILD_COMMAND } from "@zcode/contracts";
  20. import type { RunContext } from "@zcode/shared-types";
  21. import { isDwfChildInvocation } from "../src/dwf-child-command.js";
  22. import { run } from "../src/run.js";
  23. const createCapturedStdout = () => {
  24. let output = "";
  25. return {
  26. output: () => output,
  27. stream: {
  28. isTTY: false,
  29. write: (chunk: string | Uint8Array): boolean => {
  30. output += typeof chunk === "string" ? chunk : chunk.toString();
  31. return true;
  32. },
  33. } as unknown as NodeJS.WriteStream,
  34. };
  35. };
  36. /**
  37. * 子进程语境的 RunContext:stdin 必须是**真** Readable(childMain 拿它建 readline,
  38. * cli.unit.test.ts 里那个 `{ isTTY: false }` 假 stdin 会直接让 readline 抛错)。
  39. * 这里给一条立即 EOF 的空流:本用例的脚本不发 ask,父进程也就没有 response 要喂。
  40. */
  41. const createChildContext = (
  42. argv: string[],
  43. ): { ctx: RunContext; stdout: () => string; stderr: () => string } => {
  44. const stdout = createCapturedStdout();
  45. const stderr = createCapturedStdout();
  46. return {
  47. ctx: {
  48. argv,
  49. stderr: stderr.stream,
  50. stdin: Readable.from([]) as unknown as NodeJS.ReadStream,
  51. stdout: stdout.stream,
  52. },
  53. stdout: stdout.output,
  54. stderr: stderr.output,
  55. };
  56. };
  57. test("recognises the dwf child subcommand only as argv[0]", () => {
  58. assert.equal(isDwfChildInvocation([ZCODE_DWF_CHILD_COMMAND, "/tmp/run.mjs"]), true);
  59. assert.equal(isDwfChildInvocation([ZCODE_DWF_CHILD_COMMAND]), true);
  60. assert.equal(isDwfChildInvocation([]), false);
  61. assert.equal(isDwfChildInvocation(["--help"]), false);
  62. // 出现在别处不算:否则一个普通 prompt 里提到这个词就能改变 CLI 的行为。
  63. assert.equal(isDwfChildInvocation(["-p", ZCODE_DWF_CHILD_COMMAND]), false);
  64. });
  65. test("fails with usage when the entry path argument is missing", async () => {
  66. const { ctx, stdout, stderr } = createChildContext([ZCODE_DWF_CHILD_COMMAND]);
  67. const exitCode = await run(ctx);
  68. assert.equal(exitCode, 1);
  69. // stdout 是父进程的 NDJSON 通道,接线错误只许走 stderr。
  70. assert.equal(stdout(), "");
  71. assert.match(stderr(), new RegExp(`Usage: ${ZCODE_DWF_CHILD_COMMAND}`));
  72. });
  73. test("fails on stderr when the entry file does not exist", async () => {
  74. const { ctx, stdout, stderr } = createChildContext([
  75. ZCODE_DWF_CHILD_COMMAND,
  76. join(tmpdir(), "dwf-child-cmd-missing", "nope.mjs"),
  77. ]);
  78. const exitCode = await run(ctx);
  79. assert.equal(exitCode, 1);
  80. assert.equal(stdout(), "");
  81. assert.match(stderr(), /Workflow child failed: /);
  82. });