artifacts.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * 用户面产物(docs/dynamic-workflow/authoring.md)穿过整条管线:真实脚本 → lowering → 沙箱
  3. * 子进程 → NDJSON 线协议 → 引擎核心 → driver。本文件钉的是**线协议那一段**:两族成员各
  4. * 走哪条通道、标签能不能过界、拒绝能不能被脚本 catch。
  5. *
  6. * ⚠ 术语:这里的 artifact 是用户面产物;`settlement.artifact` 是脚本的顶层返回值——同一个
  7. * 词的两个意思在一个文件里同时出现,正是 spec 的「术语」表存在的理由。
  8. */
  9. import { describe, expect, it } from "vitest";
  10. import { WorkflowError, type ArtifactVersionRecord } from "@zcode/dynamic-workflow";
  11. import { runScript } from "./helpers.js";
  12. const RUN = "run";
  13. const CHART = '{ x: { field: "round" }, y: { field: "ms" } }';
  14. describe("e2e — 预置产物与打标签的 report", () => {
  15. it("声明经事件通道先行到达,随后每条标签 report 都落到那个 id 上", async () => {
  16. const script = [
  17. `artifact.chart("perf", ${CHART});`,
  18. "for (const round of [1, 2, 3]) {",
  19. " report({ round, ms: round * 10 }, \"perf\");",
  20. "}",
  21. "return { rounds: 3 };",
  22. ].join("\n");
  23. const { settlement, journal, driver } = await runScript(script);
  24. expect(settlement.status).toBe("completed");
  25. // 声明落一行 artifact 节点,带 artifact_id 与 spec。
  26. const declaration = journal.getNode(RUN, "artifact#1", 1);
  27. expect(declaration?.kind).toBe("artifact");
  28. expect(declaration?.artifactId).toBe("perf");
  29. expect((declaration?.result as ArtifactVersionRecord | undefined)?.kind).toBe("chart");
  30. // 三条 report 行都带同一个 artifact_id——「看板 = journal 的投影」的落点。
  31. const reports = journal
  32. .listNodes(RUN, { kinds: "all", withResult: true })
  33. .filter((node) => node.kind === "report");
  34. expect(reports.map((node) => node.artifactId)).toEqual(["perf", "perf", "perf"]);
  35. expect(driver.eventsOfType("report").map((event) => event.artifactId)).toEqual([
  36. "perf",
  37. "perf",
  38. "perf",
  39. ]);
  40. // 事件顺序:artifact-published 必须早于第一条 report(同一条 FIFO 事件通道)。
  41. const types = driver.events.map((event) => event.type);
  42. expect(types.indexOf("artifact-published")).toBeLessThan(types.indexOf("report"));
  43. });
  44. it("同一份 spec 重复声明是幂等 no-op,run 照常完成", async () => {
  45. const script = [
  46. `artifact.chart("perf", ${CHART});`,
  47. `artifact.chart("perf", ${CHART});`,
  48. "return 1;",
  49. ].join("\n");
  50. const { settlement, driver, journal } = await runScript(script);
  51. expect(settlement.status).toBe("completed");
  52. expect(driver.eventsOfType("artifact-published")).toHaveLength(1);
  53. expect(
  54. journal
  55. .listNodes(RUN, { kinds: "all", withResult: true })
  56. .filter((node) => node.kind === "artifact"),
  57. ).toHaveLength(1);
  58. });
  59. it("标签指向未声明的 id 让整个 run 失败(void 返回没有拒绝通道)", async () => {
  60. const { settlement } = await runScript(`report({ a: 1 }, "perf");\nreturn 1;`);
  61. expect(settlement.status).toBe("errored");
  62. if (settlement.status !== "errored") return;
  63. expect(settlement.error.code).toBe("ArtifactUndeclared");
  64. });
  65. });
  66. describe("e2e — 内容产物", () => {
  67. it("经 request/response 过界,实参按位置透传,ref 回到脚本", async () => {
  68. const script = [
  69. 'const ref = await artifact.file("book", "out/book.pdf", { title: "Book" });',
  70. 'const notes = await artifact.markdown("notes", "# hi");',
  71. "return { id: ref.id, version: ref.version, notes: notes.id };",
  72. ].join("\n");
  73. const { settlement, driver, journal } = await runScript(script);
  74. expect(settlement).toEqual({
  75. status: "completed",
  76. artifact: { id: "book", version: 1, notes: "notes" },
  77. });
  78. expect(driver.artifactPublishes.map((r) => [r.op, r.id, r.version, r.path, r.content])).toEqual([
  79. ["file", "book", 1, "out/book.pdf", undefined],
  80. ["markdown", "notes", 1, undefined, "# hi"],
  81. ]);
  82. expect(driver.artifactPublishes[0]?.opts).toEqual({ title: "Book" });
  83. expect(journal.getNode(RUN, "artifact#1", 1)?.status).toBe("completed");
  84. expect(driver.eventsOfType("artifact-published").map((e) => e.artifact.id)).toEqual([
  85. "book",
  86. "notes",
  87. ]);
  88. });
  89. it("同 id 再发布是新版本", async () => {
  90. const script = [
  91. 'const a = await artifact.markdown("notes", "one");',
  92. 'const b = await artifact.markdown("notes", "two");',
  93. "return [a.version, b.version];",
  94. ].join("\n");
  95. const { settlement } = await runScript(script);
  96. expect(settlement).toEqual({ status: "completed", artifact: [1, 2] });
  97. });
  98. it("driver 的拒绝以结构化错误过界,脚本能 catch 并继续", async () => {
  99. const script = [
  100. "let code = \"none\";",
  101. "try {",
  102. ' await artifact.file("book", "out/missing.pdf");',
  103. "} catch (e) {",
  104. " code = (e as { code?: string }).code ?? \"no-code\";",
  105. "}",
  106. "return code;",
  107. ].join("\n");
  108. const { settlement, journal, driver } = await runScript(script, {
  109. artifacts: () => {
  110. throw new WorkflowError("ArtifactSourceMissing", "out/missing.pdf 不存在");
  111. },
  112. });
  113. // 门控习语跑通:脚本拿到结构化 code,run 正常完成。
  114. expect(settlement).toEqual({ status: "completed", artifact: "ArtifactSourceMissing" });
  115. expect(journal.getNode(RUN, "artifact#1", 1)?.status).toBe("failed");
  116. expect(journal.getNode(RUN, "artifact#1", 1)?.error?.code).toBe("ArtifactSourceMissing");
  117. // 失败走 artifact-failed,不冒充一个节点结算(产物站点在图里没有节点)。
  118. expect(driver.eventsOfType("artifact-failed").map((e) => [e.id, e.op, e.error.code])).toEqual([
  119. ["book", "file", "ArtifactSourceMissing"],
  120. ]);
  121. expect(driver.eventsOfType("node-settled")).toEqual([]);
  122. });
  123. it("与 ask 混跑:子代理写出的路径由脚本交出去", async () => {
  124. const script = [
  125. 'const writer = agent("writer");',
  126. 'const path = await writer.ask("write the book");',
  127. 'const ref = await artifact.file("book", path, { title: "Book" });',
  128. "return ref.version;",
  129. ].join("\n");
  130. const { settlement, driver } = await runScript(script, {
  131. asks: { "ask#1": () => ({ type: "text", finalText: "out/book.pdf" }) },
  132. });
  133. expect(settlement).toEqual({ status: "completed", artifact: 1 });
  134. expect(driver.artifactPublishes[0]?.path).toBe("out/book.pdf");
  135. });
  136. });