run-identity.test.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * run 身份 `run` 经**真实入口**抵达沙箱:runWorkflowScript → ChildPayload → vm context。
  3. *
  4. * 与 args 同一条链(options → payload、payload → sandbox 全局、BOOTSTRAP 的 `__host.run`、
  5. * lowering 的 `run` → `__host.run`),断掉的表现同样是 `undefined` 或 ReferenceError,所以跑真子进程。
  6. * resume 时前驱 id 从 journal 读回由 bootstrap 的 run service 测试守。
  7. */
  8. import { describe, expect, it } from "vitest";
  9. import { InMemoryJournalStore } from "@zcode/dynamic-workflow";
  10. import { runWorkflowScript } from "../src/index.js";
  11. import { AutoDriver } from "./auto-driver.js";
  12. import { askSpecsFor, TEST_CWD } from "./helpers.js";
  13. function baseOptions(scriptText: string, journal: InMemoryJournalStore, runId: string) {
  14. const driver = new AutoDriver(journal, {});
  15. return {
  16. scriptText,
  17. cwd: TEST_CWD,
  18. runId,
  19. caps: { maxConcurrency: 16 },
  20. askSpecs: askSpecsFor(scriptText),
  21. validate: () => [],
  22. makeDriver: (sink: Parameters<typeof driver.attach>[0]) => {
  23. driver.attach(sink);
  24. return driver;
  25. },
  26. timeoutMs: 20000,
  27. };
  28. }
  29. describe("run identity in the sandbox", () => {
  30. it("gives the script its run id and the run it amends", async () => {
  31. const journal = new InMemoryJournalStore();
  32. const script = "return [run.id, run.resumedFrom];";
  33. const settlement = await runWorkflowScript({
  34. ...baseOptions(script, journal, "dwfrun-b"),
  35. resumedFrom: "dwfrun-a",
  36. });
  37. expect(settlement).toEqual({ status: "completed", artifact: ["dwfrun-b", "dwfrun-a"] });
  38. });
  39. it("leaves resumedFrom absent for a run that amends nothing, and freezes the object", async () => {
  40. const journal = new InMemoryJournalStore();
  41. const script = [
  42. "const before = run.id;",
  43. "try { (run as { id: string }).id = 'changed'; } catch { /* strict-mode TypeError */ }",
  44. 'return [before, run.id, "resumedFrom" in run, Object.isFrozen(run)];',
  45. ].join("\n");
  46. const settlement = await runWorkflowScript(baseOptions(script, journal, "dwfrun-only"));
  47. expect(settlement).toEqual({
  48. status: "completed",
  49. artifact: ["dwfrun-only", "dwfrun-only", false, true],
  50. });
  51. });
  52. });