engine-artifacts.test.ts 24 KB

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