args.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * 运行实参 `args` 经**真实入口**抵达沙箱:runWorkflowScript → ChildPayload → vm context。
  3. *
  4. * 这条链上有四个可以静默断掉的接头(options → payload、payload → sandbox 全局、BOOTSTRAP
  5. * 的 `__host.args`、lowering 的 `args` → `__host.args`),而断掉的表现都一样:脚本读到
  6. * `undefined` 或直接 ReferenceError。所以这里跑的是真子进程,不是桩。
  7. *
  8. * 另一半(实参落 dwf_run.args_json、resume 从那里读回重放)由 run-metadata 的断言与
  9. * bootstrap 的 resume 测试各守一头。
  10. */
  11. import { describe, expect, it } from "vitest";
  12. import { InMemoryJournalStore } from "@zcode/dynamic-workflow";
  13. import { runWorkflowScript } from "../src/index.js";
  14. import { AutoDriver } from "./auto-driver.js";
  15. import { askSpecsFor, TEST_CWD } from "./helpers.js";
  16. const RUN = "run";
  17. function baseOptions(scriptText: string, journal: InMemoryJournalStore) {
  18. const driver = new AutoDriver(journal, {});
  19. return {
  20. scriptText,
  21. cwd: TEST_CWD,
  22. runId: RUN,
  23. caps: { maxConcurrency: 16 },
  24. askSpecs: askSpecsFor(scriptText),
  25. validate: () => [],
  26. makeDriver: (sink: Parameters<typeof driver.attach>[0]) => {
  27. driver.attach(sink);
  28. return driver;
  29. },
  30. timeoutMs: 20000,
  31. };
  32. }
  33. describe("args in the sandbox", () => {
  34. it("delivers the validated bag to the script", async () => {
  35. const journal = new InMemoryJournalStore();
  36. const script = "return [String(args.target), Number(args.depth), args.deep];";
  37. const settlement = await runWorkflowScript({
  38. cwd: TEST_CWD,
  39. ...baseOptions(script, journal),
  40. args: { target: "packages/core", depth: 3, deep: { nested: true } },
  41. });
  42. expect(settlement).toEqual({
  43. status: "completed",
  44. artifact: ["packages/core", 3, { nested: true }],
  45. });
  46. });
  47. // 不变式 7:`args` 恒有定义。内联 run 读 `args.x` 必须是一次合法的属性读,
  48. // 不是一次 ReferenceError——这正是 payload 缺席时编码成 "{}" 那一行守的东西。
  49. it("is an empty object, never undefined, for a run with no arguments", async () => {
  50. const journal = new InMemoryJournalStore();
  51. const script = "return [typeof args, Object.keys(args).length, args.missing === undefined];";
  52. const settlement = await runWorkflowScript(baseOptions(script, journal));
  53. expect(settlement).toEqual({ status: "completed", artifact: ["object", 0, true] });
  54. });
  55. it("journals the args alongside the rest of the run metadata", async () => {
  56. const journal = new InMemoryJournalStore();
  57. await runWorkflowScript({
  58. cwd: TEST_CWD,
  59. ...baseOptions("return String(args.who);", journal),
  60. args: { who: "ada" },
  61. });
  62. // resume 读的就是这一份:run 的身份包含它的实参(不变式 6)。
  63. expect(journal.getRun(RUN)?.args).toEqual({ who: "ada" });
  64. });
  65. it("leaves args off the run record entirely when the run had none", async () => {
  66. const journal = new InMemoryJournalStore();
  67. await runWorkflowScript(baseOptions("return 1;", journal));
  68. // 缺席的键,不是 `{}`:记录层面保持「没有实参」与「实参是空袋」可分辨。
  69. expect(journal.getRun(RUN)?.args).toBeUndefined();
  70. });
  71. // 冻结是**浅**的(child-source 的注释说明了这个界限):顶层重新赋值静默失败(非严格
  72. // 模式的 lowered 体里)或抛错,而嵌套对象仍可改。这条钉住顶层那一半,也钉住"改动不
  73. // 穿透回宿主"——宿主拿到的实参袋不该被脚本改写。
  74. it("freezes the top level of the bag", async () => {
  75. const journal = new InMemoryJournalStore();
  76. const script = [
  77. "const frozen = Object.isFrozen(args);",
  78. "let threw = false;",
  79. "try { (args as Record<string, unknown>).injected = 1; } catch { threw = true; }",
  80. "return [frozen, threw || args.injected === undefined];",
  81. ].join("\n");
  82. const settlement = await runWorkflowScript({
  83. cwd: TEST_CWD,
  84. ...baseOptions(script, journal),
  85. args: { original: 1 },
  86. });
  87. expect(settlement).toEqual({ status: "completed", artifact: [true, true] });
  88. // 宿主侧那一份没有被脚本碰过。
  89. expect(journal.getRun(RUN)?.args).toEqual({ original: 1 });
  90. });
  91. });