engine-artifacts.test.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /**
  2. * 引擎侧的用户面产物(docs/dynamic-workflow/authoring.md「Journal rows and events」两行 + 错误码表)。
  3. * 编译侧在 tests/artifacts.test.ts。
  4. *
  5. * ⚠ 术语:本文件的 artifact 一律是**用户面产物**(脚本发布给用户看的产出),不是
  6. * `RunSettlement.artifact`(脚本的顶层返回值,那个在别的测试里叫 artifact 也很正常)。
  7. */
  8. import { describe, expect, it } from "vitest";
  9. import {
  10. InMemoryJournalStore,
  11. WorkflowEngine,
  12. WorkflowError,
  13. type ArtifactVersionRecord,
  14. type Caps,
  15. } from "../../src/engine/index.js";
  16. import { ARTIFACT_CAPS } from "../../src/facade/artifact-caps.js";
  17. import { FakeDriver, type FakeDriverOptions } from "./fake-driver.js";
  18. const RUN = "run";
  19. const CHART = { x: { field: "round" }, y: { field: "ms" } };
  20. function setup(opts: { journal?: InMemoryJournalStore; caps?: Caps } & FakeDriverOptions = {}) {
  21. const { journal: given, caps, ...driverOptions } = opts;
  22. const journal = given ?? new InMemoryJournalStore();
  23. const driver = new FakeDriver(journal, driverOptions);
  24. const engine = new WorkflowEngine({
  25. runId: RUN,
  26. driver,
  27. caps: caps ?? { maxConcurrency: 16 },
  28. askSpecs: new Map(),
  29. validate: () => [],
  30. });
  31. return { journal, driver, engine };
  32. }
  33. /** 某类事件的全部载荷(断言事件流用)。 */
  34. const published = (driver: FakeDriver): ArtifactVersionRecord[] =>
  35. driver.eventsOfType("artifact-published").map((event) => event.artifact);
  36. /** 捕获一次 run 级失败(failRun 后 settled 兑现为 errored)。 */
  37. async function failure(engine: WorkflowEngine): Promise<WorkflowError> {
  38. const settlement = await engine.settled;
  39. if (settlement.status !== "errored")
  40. throw new Error(`expected errored, got ${settlement.status}`);
  41. return settlement.error;
  42. }
  43. describe("publishArtifact — 内容成员", () => {
  44. it("经 driver 发布、落 completed 行、发 artifact-published,并把 ref 交给脚本", async () => {
  45. const { engine, driver, journal } = setup();
  46. const ref = await engine.publishArtifact("artifact#1", "file", [
  47. "book",
  48. "out/book.pdf",
  49. { title: "Book" },
  50. ]);
  51. expect(ref).toEqual({ id: "book", version: 1 });
  52. // driver 收到的请求形状:版本号由引擎算好传下去(store 的 toolCallId 里要带它)。
  53. expect(driver.artifactPublishes).toEqual([
  54. {
  55. runId: RUN,
  56. siteId: "artifact#1",
  57. ordinal: 1,
  58. op: "file",
  59. id: "book",
  60. version: 1,
  61. path: "out/book.pdf",
  62. opts: { title: "Book" },
  63. },
  64. ]);
  65. const node = journal.getNode(RUN, "artifact#1", 1);
  66. expect(node?.kind).toBe("artifact");
  67. expect(node?.status).toBe("completed");
  68. expect(node?.artifactId).toBe("book");
  69. expect((node?.result as ArtifactVersionRecord | undefined)?.uri).toBe(
  70. "zcode-artifact://fake/book/1",
  71. );
  72. expect(published(driver).map((r) => [r.id, r.version])).toEqual([["book", 1]]);
  73. });
  74. it("同 id 再发布是新版本,版本号按已 completed 的行数派生", async () => {
  75. const { engine } = setup();
  76. const first = await engine.publishArtifact("artifact#1", "file", ["book", "a.pdf"]);
  77. const second = await engine.publishArtifact("artifact#1", "file", ["book", "b.pdf"]);
  78. expect([first.version, second.version]).toEqual([1, 2]);
  79. });
  80. it("markdown 走 content 而不是 path", async () => {
  81. const { engine, driver } = setup();
  82. await engine.publishArtifact("artifact#1", "markdown", ["notes", "# hi"]);
  83. expect(driver.artifactPublishes[0]?.content).toBe("# hi");
  84. expect(driver.artifactPublishes[0]?.path).toBeUndefined();
  85. });
  86. it("journal 命中即短路:不调 driver、不重发事件", async () => {
  87. const journal = new InMemoryJournalStore();
  88. const first = setup({ journal });
  89. await first.engine.publishArtifact("artifact#1", "file", ["book", "a.pdf"]);
  90. first.engine.complete(undefined);
  91. const resumed = setup({ journal });
  92. const ref = await resumed.engine.publishArtifact("artifact#1", "file", ["book", "a.pdf"]);
  93. expect(ref).toEqual({ id: "book", version: 1 });
  94. expect(resumed.driver.artifactPublishes).toEqual([]);
  95. expect(published(resumed.driver)).toEqual([]);
  96. });
  97. it("命中一条失败记录即按记录重新拒绝(resume 是崩溃恢复,不是复验)", async () => {
  98. const journal = new InMemoryJournalStore();
  99. const first = setup({ journal, withoutArtifactStore: true });
  100. await expect(
  101. first.engine.publishArtifact("artifact#1", "file", ["book", "a.pdf"]),
  102. ).rejects.toMatchObject({ code: "ArtifactStoreUnavailable" });
  103. first.engine.complete(undefined);
  104. // 第二次装配**有** store:记录里的失败仍然是失败,driver 不会被调到。
  105. const resumed = setup({ journal });
  106. await expect(
  107. resumed.engine.publishArtifact("artifact#1", "file", ["book", "a.pdf"]),
  108. ).rejects.toMatchObject({ code: "ArtifactStoreUnavailable" });
  109. expect(resumed.driver.artifactPublishes).toEqual([]);
  110. });
  111. it("版本号跨 resume 连续", async () => {
  112. const journal = new InMemoryJournalStore();
  113. const first = setup({ journal });
  114. await first.engine.publishArtifact("artifact#1", "file", ["book", "a.pdf"]);
  115. first.engine.complete(undefined);
  116. const resumed = setup({ journal });
  117. // 第一次调用重放命中(v1),第二次是新 ordinal:若版本计数没从 journal 恢复,它会是 1。
  118. await resumed.engine.publishArtifact("artifact#1", "file", ["book", "a.pdf"]);
  119. const next = await resumed.engine.publishArtifact("artifact#1", "file", ["book", "b.pdf"]);
  120. expect(next.version).toBe(2);
  121. });
  122. it("inputHash 失配即整个 run 失败(绝不静默偏移)", async () => {
  123. const journal = new InMemoryJournalStore();
  124. const first = setup({ journal });
  125. await first.engine.publishArtifact("artifact#1", "file", ["book", "a.pdf"]);
  126. first.engine.complete(undefined);
  127. const resumed = setup({ journal });
  128. await expect(
  129. resumed.engine.publishArtifact("artifact#1", "file", ["book", "OTHER.pdf"]),
  130. ).rejects.toMatchObject({ code: "InputHashMismatch" });
  131. expect((await failure(resumed.engine)).code).toBe("InputHashMismatch");
  132. });
  133. it("第 17 版被拒(节点级,脚本可 catch),run 继续活着", async () => {
  134. const { engine } = setup();
  135. for (let i = 1; i <= ARTIFACT_CAPS.maxVersionsPerArtifact; i++) {
  136. await engine.publishArtifact("artifact#1", "file", ["book", `v${i}.pdf`]);
  137. }
  138. await expect(
  139. engine.publishArtifact("artifact#1", "file", ["book", "v17.pdf"]),
  140. ).rejects.toMatchObject({ code: "ArtifactVersionCapExceeded" });
  141. expect(engine.status()).toBe("running");
  142. });
  143. it("第 33 个 id 被拒", async () => {
  144. const { engine } = setup();
  145. for (let i = 1; i <= ARTIFACT_CAPS.maxArtifactsPerRun; i++) {
  146. await engine.publishArtifact("artifact#1", "markdown", [`doc${i}`, "x"]);
  147. }
  148. await expect(
  149. engine.publishArtifact("artifact#1", "markdown", ["overflow", "x"]),
  150. ).rejects.toMatchObject({ code: "ArtifactCapExceeded" });
  151. });
  152. it("跨成员种类复用一个 id 是节点级拒绝", async () => {
  153. const { engine } = setup();
  154. await engine.publishArtifact("artifact#1", "file", ["x", "a.pdf"]);
  155. await expect(
  156. engine.publishArtifact("artifact#2", "markdown", ["x", "hi"]),
  157. ).rejects.toMatchObject({ code: "ArtifactKindMismatch" });
  158. expect(engine.status()).toBe("running");
  159. });
  160. it("装配没有产物存储时大声拒绝,并把失败落 journal", async () => {
  161. const { engine, journal, driver } = setup({ withoutArtifactStore: true });
  162. await expect(
  163. engine.publishArtifact("artifact#1", "file", ["book", "a.pdf"]),
  164. ).rejects.toMatchObject({ code: "ArtifactStoreUnavailable" });
  165. const node = journal.getNode(RUN, "artifact#1", 1);
  166. expect(node?.status).toBe("failed");
  167. expect(node?.error?.code).toBe("ArtifactStoreUnavailable");
  168. expect(node?.artifactId).toBe("book");
  169. // 失败发自己的事件,带着渲染一张失败卡要的东西。
  170. expect(published(driver)).toEqual([]);
  171. expect(driver.eventsOfType("artifact-failed")).toEqual([
  172. {
  173. type: "artifact-failed",
  174. instance: { siteId: "artifact#1", ordinal: 1 },
  175. id: "book",
  176. op: "file",
  177. error: node?.error,
  178. },
  179. ]);
  180. });
  181. // 2026-09-04 裁决:产物站点不进任何图,所以一条 node-settled 落在它上面会在 run 面板里
  182. // 变成未知节点(投影把 node-settled 归约进 nodes[],图层再按 site id 找不到它)。
  183. it("失败**不**发 node-settled——产物站点在图里没有节点可结算", async () => {
  184. const { engine, driver } = setup({ withoutArtifactStore: true });
  185. await expect(
  186. engine.publishArtifact("artifact#1", "file", ["book", "a.pdf"]),
  187. ).rejects.toMatchObject({ code: "ArtifactStoreUnavailable" });
  188. expect(driver.eventsOfType("node-settled")).toEqual([]);
  189. expect(driver.eventsOfType("node-queued")).toEqual([]);
  190. expect(driver.eventsOfType("node-dispatched")).toEqual([]);
  191. });
  192. // 成功那侧的对偶(与 report 的 parity 是有意的):只有 artifact-published,没有节点生命周期。
  193. it("成功也只发 artifact-published,不发任何节点生命周期事件", async () => {
  194. const { engine, driver } = setup();
  195. await engine.publishArtifact("artifact#1", "file", ["book", "a.pdf"]);
  196. expect(driver.eventsOfType("artifact-published")).toHaveLength(1);
  197. for (const type of ["node-queued", "node-dispatched", "node-settled"] as const) {
  198. expect(driver.eventsOfType(type)).toEqual([]);
  199. }
  200. });
  201. it("replay 命中一条失败记录不重发事件", async () => {
  202. const journal = new InMemoryJournalStore();
  203. const first = setup({ journal, withoutArtifactStore: true });
  204. await expect(
  205. first.engine.publishArtifact("artifact#1", "file", ["book", "a.pdf"]),
  206. ).rejects.toMatchObject({ code: "ArtifactStoreUnavailable" });
  207. first.engine.complete(undefined);
  208. const resumed = setup({ journal });
  209. await expect(
  210. resumed.engine.publishArtifact("artifact#1", "file", ["book", "a.pdf"]),
  211. ).rejects.toMatchObject({ code: "ArtifactStoreUnavailable" });
  212. expect(resumed.driver.eventsOfType("artifact-failed")).toEqual([]);
  213. expect(resumed.driver.eventsOfType("artifact-published")).toEqual([]);
  214. });
  215. // 实参形状的护栏是**确定性**的拒绝:结论只由 args 决定,没有 driver 往返,所以既不落行
  216. // 也不发事件——没有 id 可写,编一个出来会让读者以为真有那么一个产物。
  217. it("实参形状错误直接拒绝,不落行也不发事件", async () => {
  218. const { engine, journal, driver } = setup();
  219. await expect(
  220. engine.publishArtifact("artifact#1", "file", [42, "a.pdf"]),
  221. ).rejects.toMatchObject({ code: "DriverError" });
  222. await expect(
  223. engine.publishArtifact("artifact#2", "markdown", ["notes", 7]),
  224. ).rejects.toMatchObject({ code: "DriverError" });
  225. expect(
  226. journal
  227. .listNodes(RUN, { kinds: "all", withResult: true })
  228. .filter((n) => n.kind === "artifact"),
  229. ).toEqual([]);
  230. expect(driver.eventsOfType("artifact-failed")).toEqual([]);
  231. });
  232. it("driver 拒绝即节点 failed + 可 catch 的结构化错误", async () => {
  233. const { engine, journal, driver } = setup({ deferArtifactPublishes: true });
  234. const promise = engine.publishArtifact("artifact#1", "file", ["book", "missing.pdf"]);
  235. driver.artifactDeferrals[0]?.reject(
  236. new WorkflowError("ArtifactSourceMissing", "out/book.pdf 不存在"),
  237. );
  238. await expect(promise).rejects.toMatchObject({ code: "ArtifactSourceMissing" });
  239. expect(journal.getNode(RUN, "artifact#1", 1)?.status).toBe("failed");
  240. expect(driver.eventsOfType("artifact-failed").map((e) => [e.id, e.op, e.error.code])).toEqual([
  241. ["book", "file", "ArtifactSourceMissing"],
  242. ]);
  243. expect(engine.status()).toBe("running");
  244. });
  245. it("一个失败的发布不占用 id 名额,重试仍是第 1 版", async () => {
  246. const { engine, driver } = setup({ deferArtifactPublishes: true });
  247. const first = engine.publishArtifact("artifact#1", "file", ["book", "missing.pdf"]);
  248. driver.artifactDeferrals[0]?.reject(new WorkflowError("ArtifactSourceMissing", "no"));
  249. await expect(first).rejects.toMatchObject({ code: "ArtifactSourceMissing" });
  250. const second = engine.publishArtifact("artifact#1", "file", ["book", "now-there.pdf"]);
  251. driver.artifactDeferrals[1]?.resolve({ id: "book", kind: "file", version: 1 });
  252. expect(await second).toEqual({ id: "book", version: 1 });
  253. });
  254. });
  255. describe("primary(docs/dynamic-workflow/authoring.md「Ids, tags and versions」)", () => {
  256. it("引擎把旗子盖到记录上,事件与 journal 行都带着它", async () => {
  257. const { engine, driver, journal } = setup();
  258. await engine.publishArtifact("artifact#1", "file", ["book", "a.pdf", { primary: true }]);
  259. expect(published(driver)[0]?.primary).toBe(true);
  260. expect(journal.getNode(RUN, "artifact#1", 1)?.result).toMatchObject({ primary: true });
  261. // 没标的记录**没有**这个键(缺席不是 false)。
  262. await engine.publishArtifact("artifact#2", "file", ["notes", "n.pdf"]);
  263. expect("primary" in (published(driver)[1] ?? {})).toBe(false);
  264. });
  265. it("按 id 粘着:下一版没再写 primary 也还是 primary", async () => {
  266. const { engine, driver } = setup();
  267. await engine.publishArtifact("artifact#1", "file", ["book", "a.pdf", { primary: true }]);
  268. await engine.publishArtifact("artifact#1", "file", ["book", "b.pdf"]);
  269. expect(published(driver).map((r) => [r.version, r.primary])).toEqual([
  270. [1, true],
  271. [2, true],
  272. ]);
  273. });
  274. it("第二个 id 想当 primary 是节点级拒绝:失败行 + artifact-failed,run 继续活着", async () => {
  275. const { engine, driver, journal } = setup();
  276. await engine.publishArtifact("artifact#1", "file", ["book", "a.pdf", { primary: true }]);
  277. await expect(
  278. engine.publishArtifact("artifact#2", "markdown", ["notes", "hi", { primary: true }]),
  279. ).rejects.toMatchObject({ code: "ArtifactPrimaryConflict" });
  280. expect(journal.getNode(RUN, "artifact#2", 1)?.status).toBe("failed");
  281. expect(driver.eventsOfType("artifact-failed")).toHaveLength(1);
  282. expect(engine.status()).toBe("running");
  283. // 输的那个没占 id:之后不带旗子再发,是它的第 1 版。
  284. const ref = await engine.publishArtifact("artifact#2", "markdown", ["notes", "hi"]);
  285. expect(ref.version).toBe(1);
  286. });
  287. it("并发的两个 primary 发布都过了准入,结算时后到的那个走失败路径", async () => {
  288. const { engine, driver } = setup();
  289. const results = await Promise.allSettled([
  290. engine.publishArtifact("artifact#1", "file", ["a", "a.pdf", { primary: true }]),
  291. engine.publishArtifact("artifact#2", "file", ["b", "b.pdf", { primary: true }]),
  292. ]);
  293. expect(results.map((r) => r.status)).toEqual(["fulfilled", "rejected"]);
  294. expect(published(driver).map((r) => r.id)).toEqual(["a"]);
  295. expect(driver.eventsOfType("artifact-failed")[0]?.error.code).toBe("ArtifactPrimaryConflict");
  296. });
  297. it("失败记录 replay 照旧拒绝,且不认领旗子", async () => {
  298. const journal = new InMemoryJournalStore();
  299. const first = setup({ journal });
  300. await first.engine.publishArtifact("artifact#1", "file", ["book", "a.pdf", { primary: true }]);
  301. await expect(
  302. first.engine.publishArtifact("artifact#2", "file", ["other", "o.pdf", { primary: true }]),
  303. ).rejects.toMatchObject({ code: "ArtifactPrimaryConflict" });
  304. first.engine.complete(undefined);
  305. const resumed = setup({ journal });
  306. await resumed.engine.publishArtifact("artifact#1", "file", ["book", "a.pdf", { primary: true }]);
  307. await expect(
  308. resumed.engine.publishArtifact("artifact#2", "file", ["other", "o.pdf", { primary: true }]),
  309. ).rejects.toMatchObject({ code: "ArtifactPrimaryConflict" });
  310. // resume 重建:book 仍是 primary,所以第三个 id 也被拒。
  311. await expect(
  312. resumed.engine.publishArtifact("artifact#3", "file", ["third", "t.pdf", { primary: true }]),
  313. ).rejects.toMatchObject({ code: "ArtifactPrimaryConflict" });
  314. expect(resumed.driver.artifactPublishes).toEqual([]);
  315. });
  316. it("非布尔的 primary 是确定性护栏:DriverError,不落行、不发事件", async () => {
  317. const { engine, driver, journal } = setup();
  318. await expect(
  319. engine.publishArtifact("artifact#1", "file", ["book", "a.pdf", { primary: "yes" }]),
  320. ).rejects.toMatchObject({ code: "DriverError" });
  321. expect(journal.getNode(RUN, "artifact#1", 1)).toBeUndefined();
  322. expect(driver.eventsOfType("artifact-failed")).toEqual([]);
  323. });
  324. it("预置也能当 primary:记录带旗子;第二个 id 再标是 failRun", async () => {
  325. const { engine, driver } = setup();
  326. engine.declareArtifact("artifact#1", "board", [
  327. "triage",
  328. { key: "id", status: "s", columns: ["x"], primary: true },
  329. ]);
  330. expect(published(driver)[0]?.primary).toBe(true);
  331. engine.declareArtifact("artifact#2", "chart", ["perf", { ...CHART, primary: true }]);
  332. expect((await failure(engine)).code).toBe("ArtifactPrimaryConflict");
  333. });
  334. it("内容 primary 在前,预置再标 primary 也是 failRun", async () => {
  335. const { engine } = setup();
  336. await engine.publishArtifact("artifact#1", "file", ["book", "a.pdf", { primary: true }]);
  337. engine.declareArtifact("artifact#2", "chart", ["perf", { ...CHART, primary: true }]);
  338. expect((await failure(engine)).code).toBe("ArtifactPrimaryConflict");
  339. });
  340. it("spec 里非布尔的 primary 是 ArtifactSpecInvalid", async () => {
  341. const { engine } = setup();
  342. engine.declareArtifact("artifact#1", "chart", ["perf", { ...CHART, primary: 1 }]);
  343. const err = await failure(engine);
  344. expect(err.code).toBe("ArtifactSpecInvalid");
  345. expect(err.message).toContain("primary must be a boolean");
  346. });
  347. });
  348. describe("declareArtifact — 预置成员", () => {
  349. it("落一行 completed、发 artifact-published,spec 与 title 都在记录里", () => {
  350. const { engine, driver, journal } = setup();
  351. engine.declareArtifact("artifact#1", "chart", ["perf", { ...CHART, title: "性能" }]);
  352. const node = journal.getNode(RUN, "artifact#1", 1);
  353. expect(node?.kind).toBe("artifact");
  354. expect(node?.status).toBe("completed");
  355. expect(node?.artifactId).toBe("perf");
  356. const record = node?.result as ArtifactVersionRecord;
  357. expect(record).toMatchObject({ id: "perf", kind: "chart", version: 1, title: "性能" });
  358. expect(record.spec).toEqual({ ...CHART, title: "性能" });
  359. expect(published(driver)).toHaveLength(1);
  360. });
  361. it("不经 driver", () => {
  362. const { engine, driver } = setup();
  363. engine.declareArtifact("artifact#1", "table", ["rows", { columns: [{ field: "n" }] }]);
  364. expect(driver.artifactPublishes).toEqual([]);
  365. });
  366. it("同 id 同 spec 重复声明是幂等 no-op:不落新行、不发事件", () => {
  367. const { engine, driver, journal } = setup();
  368. engine.declareArtifact("artifact#1", "chart", ["perf", CHART]);
  369. engine.declareArtifact("artifact#2", "chart", ["perf", { ...CHART }]);
  370. expect(published(driver)).toHaveLength(1);
  371. expect(journal.getNode(RUN, "artifact#2", 1)).toBeUndefined();
  372. expect(
  373. journal
  374. .listNodes(RUN, { kinds: "all", withResult: true })
  375. .filter((n) => n.kind === "artifact"),
  376. ).toHaveLength(1);
  377. });
  378. it("同 id 异 spec 让整个 run 失败(void 返回没有拒绝通道)", async () => {
  379. const { engine } = setup();
  380. engine.declareArtifact("artifact#1", "chart", ["perf", CHART]);
  381. engine.declareArtifact("artifact#2", "chart", [
  382. "perf",
  383. { x: { field: "round" }, y: { field: "OTHER" } },
  384. ]);
  385. expect((await failure(engine)).code).toBe("ArtifactRedeclared");
  386. });
  387. it("spec 形状不合法即 failRun,消息说得出哪里不对", async () => {
  388. const { engine } = setup();
  389. engine.declareArtifact("artifact#1", "chart", ["perf", { x: { field: "" }, y: CHART.y }]);
  390. const error = await failure(engine);
  391. expect(error.code).toBe("ArtifactSpecInvalid");
  392. expect(error.message).toContain("x.field");
  393. });
  394. it("每一种预置都按自己的必填字段校验", async () => {
  395. const cases: Array<[Parameters<WorkflowEngine["declareArtifact"]>[1], unknown]> = [
  396. ["chart", { x: { field: "r" } }],
  397. ["table", { columns: [] }],
  398. ["metrics", { metrics: "not a list" }],
  399. ["board", { key: "id", status: "s", columns: [] }],
  400. ];
  401. for (const [op, spec] of cases) {
  402. const { engine } = setup();
  403. engine.declareArtifact("artifact#1", op, ["x", spec]);
  404. expect((await failure(engine)).code).toBe("ArtifactSpecInvalid");
  405. }
  406. });
  407. it("board 的两个标题各自校验:title 是板子的名字,cardTitle 是字段名", async () => {
  408. const good = setup();
  409. good.engine.declareArtifact("artifact#1", "board", [
  410. "b",
  411. { title: "内存回归", key: "id", status: "state", columns: ["todo"], cardTitle: "name" },
  412. ]);
  413. expect(good.engine.status()).toBe("running");
  414. const bad = setup();
  415. bad.engine.declareArtifact("artifact#1", "board", [
  416. "b",
  417. { key: "id", status: "state", columns: ["todo"], cardTitle: "" },
  418. ]);
  419. const error = await failure(bad.engine);
  420. expect(error.code).toBe("ArtifactSpecInvalid");
  421. expect(error.message).toContain("cardTitle");
  422. });
  423. it("超长 title 与超大 spec 都算 spec 不合法", async () => {
  424. const long = setup();
  425. long.engine.declareArtifact("artifact#1", "chart", [
  426. "perf",
  427. { ...CHART, title: "x".repeat(ARTIFACT_CAPS.maxTitleLength + 1) },
  428. ]);
  429. expect((await failure(long.engine)).code).toBe("ArtifactSpecInvalid");
  430. const big = setup();
  431. big.engine.declareArtifact("artifact#1", "table", [
  432. "rows",
  433. { columns: [{ field: "n", label: "y".repeat(ARTIFACT_CAPS.maxSpecSerializedBytes + 1) }] },
  434. ]);
  435. expect((await failure(big.engine)).code).toBe("ArtifactSpecInvalid");
  436. });
  437. it("预置与内容共用一个 id 是 failRun(预置族没有拒绝通道)", async () => {
  438. const { engine } = setup();
  439. await engine.publishArtifact("artifact#1", "markdown", ["x", "hi"]);
  440. engine.declareArtifact("artifact#2", "chart", ["x", CHART]);
  441. expect((await failure(engine)).code).toBe("ArtifactKindMismatch");
  442. });
  443. it("第 33 个 id 让 run 失败", async () => {
  444. const { engine } = setup();
  445. for (let i = 1; i <= ARTIFACT_CAPS.maxArtifactsPerRun; i++) {
  446. engine.declareArtifact(`artifact#${i}`, "chart", [`c${i}`, CHART]);
  447. }
  448. engine.declareArtifact("artifact#33", "chart", ["overflow", CHART]);
  449. expect((await failure(engine)).code).toBe("ArtifactCapExceeded");
  450. });
  451. it("replay 命中即静默跳过", () => {
  452. const journal = new InMemoryJournalStore();
  453. const first = setup({ journal });
  454. first.engine.declareArtifact("artifact#1", "chart", ["perf", CHART]);
  455. first.engine.complete(undefined);
  456. const resumed = setup({ journal });
  457. resumed.engine.declareArtifact("artifact#1", "chart", ["perf", CHART]);
  458. expect(published(resumed.driver)).toEqual([]);
  459. expect(
  460. journal
  461. .listNodes(RUN, { kinds: "all", withResult: true })
  462. .filter((n) => n.kind === "artifact"),
  463. ).toHaveLength(1);
  464. });
  465. it("resume 之后仍然认得这个 id 已被声明(幂等与标签都靠它)", async () => {
  466. const journal = new InMemoryJournalStore();
  467. const first = setup({ journal });
  468. first.engine.declareArtifact("artifact#1", "chart", ["perf", CHART]);
  469. first.engine.complete(undefined);
  470. const resumed = setup({ journal });
  471. resumed.engine.report("report#1", { round: 1 }, "perf");
  472. expect(resumed.driver.eventsOfType("report")[0]?.artifactId).toBe("perf");
  473. expect(resumed.engine.status()).toBe("running");
  474. });
  475. });
  476. describe("report 的产物标签", () => {
  477. it("标签落在节点行与事件上", () => {
  478. const { engine, driver, journal } = setup();
  479. engine.declareArtifact("artifact#1", "chart", ["perf", CHART]);
  480. engine.report("report#1", { round: 1, ms: 12 }, "perf");
  481. expect(journal.getNode(RUN, "report#1", 1)?.artifactId).toBe("perf");
  482. const event = driver.eventsOfType("report")[0];
  483. expect(event?.artifactId).toBe("perf");
  484. expect(event?.item).toEqual({ round: 1, ms: 12 });
  485. });
  486. it("无标签的 report 不长出 artifactId 键", () => {
  487. const { engine, driver, journal } = setup();
  488. engine.report("report#1", { a: 1 });
  489. expect(journal.getNode(RUN, "report#1", 1)?.artifactId).toBeUndefined();
  490. expect(driver.eventsOfType("report")[0]?.artifactId).toBeUndefined();
  491. });
  492. it("标签指向未声明的 id 即 failRun,消息列出已声明的预置", async () => {
  493. const { engine } = setup();
  494. engine.declareArtifact("artifact#1", "chart", ["perf", CHART]);
  495. engine.report("report#1", { a: 1 }, "nope");
  496. const error = await failure(engine);
  497. expect(error.code).toBe("ArtifactUndeclared");
  498. expect(error.message).toContain("perf");
  499. });
  500. it("标签指向内容产物同样是 ArtifactUndeclared(它不是预置)", async () => {
  501. const { engine } = setup();
  502. await engine.publishArtifact("artifact#1", "markdown", ["doc", "hi"]);
  503. engine.report("report#1", { a: 1 }, "doc");
  504. expect((await failure(engine)).code).toBe("ArtifactUndeclared");
  505. });
  506. it("标签不进 inputHash:既有 journal 里的无标签 report 照旧命中", () => {
  507. const journal = new InMemoryJournalStore();
  508. const first = setup({ journal });
  509. first.engine.report("report#1", { a: 1 });
  510. first.engine.complete(undefined);
  511. const resumed = setup({ journal });
  512. resumed.engine.report("report#1", { a: 1 });
  513. expect(resumed.driver.eventsOfType("report")).toEqual([]);
  514. expect(resumed.engine.status()).toBe("running");
  515. });
  516. });