workflowNotificationArtifacts.test.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // 终态通知载荷里的**用户面产物** chips(docs/dynamic-workflow/authoring.md「How the user sees them」)。
  2. //
  3. // ⚠ 术语:这里的 artifact 是脚本经 `artifact.*` 发布给用户看的产出,与同一载荷上的 `result`
  4. // (脚本顶层返回值,引擎内部也叫 artifact)是两件不同的东西——见 spec 的「术语」表。
  5. //
  6. // 这份 schema 是三处必须同步的其中一处(另两处:contracts 的 TS 镜像、bootstrap 的
  7. // `transcript-hydration.ts` 读取器)。它不是 `.strict()`——zod 默认**剥离**未声明的键,所以
  8. // 「忘了在这里加字段」的症状不是报错,而是载荷在冷恢复后无声少一半。
  9. import { describe, expect, it } from "vitest";
  10. import {
  11. backgroundResultOriginMetaSchema,
  12. workflowNotificationMetaSchema,
  13. } from "../src/zcode-protocol-v4/rows.js";
  14. const terminal = (overrides: Record<string, unknown> = {}) => ({
  15. kind: "terminal" as const,
  16. status: "completed" as const,
  17. summary: "nightly audit",
  18. ...overrides,
  19. });
  20. describe("workflowNotificationMeta.terminal.artifacts", () => {
  21. it("接受 chips 需要的五个字段,title / contentType 可缺", () => {
  22. const parsed = workflowNotificationMetaSchema.parse(
  23. terminal({
  24. artifacts: [
  25. {
  26. id: "audit",
  27. kind: "file",
  28. title: "审计报告",
  29. version: 2,
  30. contentType: "application/pdf",
  31. },
  32. { id: "perf", kind: "chart", version: 1 },
  33. ],
  34. }),
  35. );
  36. expect(parsed.kind).toBe("terminal");
  37. expect(parsed.kind === "terminal" && parsed.artifacts).toEqual([
  38. { id: "audit", kind: "file", title: "审计报告", version: 2, contentType: "application/pdf" },
  39. { id: "perf", kind: "chart", version: 1 },
  40. ]);
  41. });
  42. it("两个字段一起缺席即「这个 run 没有产物」,不发空数组", () => {
  43. const parsed = workflowNotificationMetaSchema.parse(terminal());
  44. expect(parsed).not.toHaveProperty("artifacts");
  45. expect(parsed).not.toHaveProperty("artifactsTruncated");
  46. });
  47. it("artifactsTruncated 只能是 true(false 是每条载荷都要带的噪音字段)", () => {
  48. expect(
  49. workflowNotificationMetaSchema.parse(terminal({ artifacts: [], artifactsTruncated: true })),
  50. ).toMatchObject({ artifactsTruncated: true });
  51. expect(() =>
  52. workflowNotificationMetaSchema.parse(terminal({ artifactsTruncated: false })),
  53. ).toThrow();
  54. });
  55. it("上界 8 件:发射侧就地截断,超界整行落库时被拒", () => {
  56. const entries = (count: number) =>
  57. Array.from({ length: count }, (_, index) => ({
  58. id: `a${index + 1}`,
  59. kind: "markdown" as const,
  60. version: 1,
  61. }));
  62. expect(workflowNotificationMetaSchema.parse(terminal({ artifacts: entries(8) }))).toMatchObject(
  63. { kind: "terminal" },
  64. );
  65. expect(() =>
  66. workflowNotificationMetaSchema.parse(terminal({ artifacts: entries(9) })),
  67. ).toThrow();
  68. });
  69. it("kind 是闭集:未知种类让整份载荷被拒,所以发射侧必须先过滤", () => {
  70. expect(() =>
  71. workflowNotificationMetaSchema.parse(
  72. terminal({ artifacts: [{ id: "future", kind: "hologram", version: 1 }] }),
  73. ),
  74. ).toThrow();
  75. for (const kind of ["file", "markdown", "chart", "table", "metrics", "board"]) {
  76. expect(() =>
  77. workflowNotificationMetaSchema.parse(
  78. terminal({ artifacts: [{ id: "a", kind, version: 1 }] }),
  79. ),
  80. ).not.toThrow();
  81. }
  82. });
  83. it("id 非空 ≤64、title ≤120(= ARTIFACT_CAPS.maxTitleLength)、version 是正整数", () => {
  84. const one = (entry: Record<string, unknown>) => () =>
  85. workflowNotificationMetaSchema.parse(terminal({ artifacts: [entry] }));
  86. expect(one({ id: "", kind: "file", version: 1 })).toThrow();
  87. expect(one({ id: "x".repeat(65), kind: "file", version: 1 })).toThrow();
  88. expect(one({ id: "x".repeat(64), kind: "file", version: 1 })).not.toThrow();
  89. expect(one({ id: "a", kind: "file", version: 1, title: "t".repeat(121) })).toThrow();
  90. expect(one({ id: "a", kind: "file", version: 1, title: "t".repeat(120) })).not.toThrow();
  91. expect(one({ id: "a", kind: "file", version: 0 })).toThrow();
  92. expect(one({ id: "a", kind: "file", version: 1.5 })).toThrow();
  93. });
  94. // 交付物那一条例外地多带两个键(docs/dynamic-workflow/transcript-and-notifications.md
  95. // 「The deliverable row」):完成卡要把它画成带文字的一行,冷 transcript 上只有这份载荷可读。
  96. it("primary 只能是 true;description 只在交付物上有意义,≤500(= ARTIFACT_CAPS.maxDescriptionLength)", () => {
  97. const one = (entry: Record<string, unknown>) => () =>
  98. workflowNotificationMetaSchema.parse(terminal({ artifacts: [entry] }));
  99. const deliverable = {
  100. id: "report",
  101. kind: "markdown",
  102. title: "审计报告",
  103. version: 3,
  104. primary: true,
  105. description: "本轮审计的结论与修复建议。",
  106. };
  107. const parsed = workflowNotificationMetaSchema.parse(terminal({ artifacts: [deliverable] }));
  108. expect(parsed.kind === "terminal" && parsed.artifacts).toEqual([deliverable]);
  109. // false 是每条都要带的噪音字段:缺席即「不是交付物」。
  110. expect(one({ id: "a", kind: "file", version: 1, primary: false })).toThrow();
  111. expect(one({ id: "a", kind: "file", version: 1, description: "d".repeat(500) })).not.toThrow();
  112. expect(one({ id: "a", kind: "file", version: 1, description: "d".repeat(501) })).toThrow();
  113. });
  114. it("载荷随 originMeta 一起过 schema(chips 的唯一数据源)", () => {
  115. const parsed = backgroundResultOriginMetaSchema.parse({
  116. backgroundSource: "workflow",
  117. workId: "dwfrun-1",
  118. title: "nightly audit",
  119. workflowNotification: terminal({
  120. artifacts: [{ id: "audit", kind: "file", version: 1 }],
  121. artifactsTruncated: true,
  122. }),
  123. });
  124. const meta = parsed.workflowNotification;
  125. expect(meta?.kind === "terminal" && meta.artifacts?.[0]?.id).toBe("audit");
  126. expect(meta?.kind === "terminal" && meta.artifactsTruncated).toBe(true);
  127. });
  128. // escalation 分支是另一支:它播报的是一个还没被满足的义务,与交付物无关。
  129. it("escalation 分支不长出 artifacts", () => {
  130. expect(() =>
  131. workflowNotificationMetaSchema.parse({
  132. kind: "escalation",
  133. qid: "dwfq-1",
  134. actor: "actor#1@1",
  135. question: "which branch?",
  136. artifacts: [{ id: "a", kind: "file", version: 1 }],
  137. }),
  138. ).not.toThrow();
  139. const parsed = workflowNotificationMetaSchema.parse({
  140. kind: "escalation",
  141. qid: "dwfq-1",
  142. actor: "actor#1@1",
  143. question: "which branch?",
  144. artifacts: [{ id: "a", kind: "file", version: 1 }],
  145. });
  146. // 未声明的键被剥离——escalation 载荷永远不带产物。
  147. expect(parsed).not.toHaveProperty("artifacts");
  148. });
  149. });