workflowWorkspaceTranscript.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // 脚本 transcript 的纯模型(docs/dynamic-workflow/transcript-and-notifications.md「The panel」):
  2. // journal 行 → 卡片素材的派生规则钉在这里,面板测试只钉「接到了 DOM 上」。
  3. import { describe, expect, it } from "vitest";
  4. import type { WorkflowRunState, WorkflowRunWorkspaceNode } from "@zcode/shared/zcode-protocol-v4";
  5. import type { WorkflowCausalityGraphData } from "@/components/workflow-graph/types.js";
  6. import {
  7. buildWorkspaceCards,
  8. firstCardIndexOfPhase,
  9. formatCommandLine,
  10. formatWorkspaceBytes,
  11. formatWorkspaceDuration,
  12. isTimeoutError,
  13. workspaceCardKindOf,
  14. workspaceCardStatus,
  15. } from "@/app-shell/workflowWorkspaceTranscript.js";
  16. const GRAPH: WorkflowCausalityGraphData = {
  17. steps: [
  18. { id: "ask#1", kind: "ask", label: "plan", lane: "actor#1", phase: "phase#a" },
  19. {
  20. id: "world-read#1",
  21. kind: "world-read",
  22. label: "glob src/**",
  23. lane: "workspace",
  24. phase: "phase#a",
  25. },
  26. {
  27. id: "world-read#2",
  28. kind: "world-read",
  29. label: "run pnpm",
  30. lane: "workspace",
  31. phase: "phase#b",
  32. },
  33. ],
  34. lanes: [{ id: "actor#1", name: "planner" }, { id: "workspace" }],
  35. participants: [
  36. { id: "phase#a:actor#1", phase: "phase#a", lane: "actor#1", steps: ["ask#1"] },
  37. { id: "phase#a:workspace", phase: "phase#a", lane: "workspace", steps: ["world-read#1"] },
  38. { id: "phase#b:workspace", phase: "phase#b", lane: "workspace", steps: ["world-read#2"] },
  39. ],
  40. handoffs: [],
  41. phases: [
  42. { id: "phase#a", name: "plan" },
  43. { id: "phase#b", name: "verify" },
  44. ],
  45. phaseEdges: [{ from: "phase#a", to: "phase#b" }],
  46. exits: ["phase#b"],
  47. };
  48. function node(overrides: Partial<WorkflowRunWorkspaceNode>): WorkflowRunWorkspaceNode {
  49. return {
  50. siteId: "world-read#1",
  51. ordinal: 1,
  52. kind: "world-read",
  53. status: "completed",
  54. createdAt: 1_000,
  55. updatedAt: 2_300,
  56. ...overrides,
  57. };
  58. }
  59. describe("workspaceCardKindOf", () => {
  60. it("按 op 分派四个动词;没有 op 的历史行是通用步骤", () => {
  61. expect(workspaceCardKindOf("read")).toBe("read");
  62. expect(workspaceCardKindOf("glob")).toBe("search");
  63. expect(workspaceCardKindOf("grep")).toBe("search");
  64. expect(workspaceCardKindOf("git-diff")).toBe("git");
  65. expect(workspaceCardKindOf("git-changed-files")).toBe("git");
  66. expect(workspaceCardKindOf("run")).toBe("terminal");
  67. expect(workspaceCardKindOf(undefined)).toBe("step");
  68. });
  69. });
  70. describe("buildWorkspaceCards", () => {
  71. it("Read:路径;Search:pattern + 范围;Git:`git <子命令> 实参`;Terminal:整条命令行", () => {
  72. const cards = buildWorkspaceCards(
  73. [
  74. node({ op: "read", args: ["src/a.ts"] }),
  75. node({ siteId: "world-read#1", ordinal: 2, op: "grep", args: ["TODO", "src/**/*.ts"] }),
  76. node({ siteId: "world-read#2", op: "git-diff", args: ["src/"] }),
  77. node({
  78. siteId: "world-read#2",
  79. ordinal: 2,
  80. kind: "world-run",
  81. op: "run",
  82. args: ["pnpm", ["vitest", "run", "a b"], { timeoutMs: 1 }],
  83. }),
  84. ],
  85. GRAPH,
  86. );
  87. expect(cards.map((card) => card.kind)).toEqual(["read", "search", "git", "terminal"]);
  88. expect(cards[0]).toMatchObject({
  89. primary: "src/a.ts",
  90. round: 1,
  91. phase: { id: "phase#a", name: "plan" },
  92. });
  93. expect(cards[1]).toMatchObject({ primary: "TODO", secondary: "src/**/*.ts", round: 2 });
  94. expect(cards[2]).toMatchObject({
  95. primary: "git diff src/",
  96. secondary: "src/",
  97. phase: { id: "phase#b" },
  98. });
  99. expect(cards[3]).toMatchObject({
  100. primary: 'pnpm vitest run "a b"',
  101. command: 'pnpm vitest run "a b"',
  102. round: 2,
  103. });
  104. expect(cards.map((card) => card.key)).toEqual([
  105. "world-read#1@1",
  106. "world-read#1@2",
  107. "world-read#2@1",
  108. "world-read#2@2",
  109. ]);
  110. });
  111. it("升级前的历史行(无 op)退回静态图上的步标签;图不可得时退回站点 id、没有阶段", () => {
  112. const [withGraph] = buildWorkspaceCards([node({})], GRAPH);
  113. expect(withGraph).toMatchObject({
  114. kind: "step",
  115. primary: "glob src/**",
  116. phase: { id: "phase#a" },
  117. });
  118. const [noGraph] = buildWorkspaceCards([node({ op: "read", args: ["x"] })], undefined);
  119. expect(noGraph?.primary).toBe("x");
  120. expect(noGraph?.phase).toBeUndefined();
  121. const [unknownSite] = buildWorkspaceCards([node({ siteId: "world-read#9" })], GRAPH);
  122. expect(unknownSite?.primary).toBe("world-read#9");
  123. });
  124. it("截断模式下的数组实参是 JSON 预览文本:命令行原样带上它", () => {
  125. const [card] = buildWorkspaceCards(
  126. [node({ kind: "world-run", op: "run", args: ["node", '["-e","…'], inputTruncated: true })],
  127. undefined,
  128. );
  129. expect(card?.command).toBe('node "[\\"-e\\",\\"…"');
  130. });
  131. });
  132. describe("workspaceCardStatus", () => {
  133. it("状态以 journal 行为准;活投影只叠 cached(replayed 芯片)", () => {
  134. const run = {
  135. runId: "r",
  136. status: "running",
  137. usage: { spentTokens: 0, nodesUsed: 0 },
  138. actors: [],
  139. nodes: [
  140. { siteId: "world-read#1", ordinal: 1, phase: "settled", outcome: "ok", cached: true },
  141. ],
  142. lastEventSequence: 3,
  143. } as unknown as WorkflowRunState;
  144. expect(workspaceCardStatus(node({ status: "running" }), run)).toEqual({
  145. status: "running",
  146. replayed: true,
  147. });
  148. expect(workspaceCardStatus(node({ ordinal: 2 }), run)).toEqual({
  149. status: "completed",
  150. replayed: false,
  151. });
  152. expect(workspaceCardStatus(node({}), undefined).replayed).toBe(false);
  153. });
  154. });
  155. describe("firstCardIndexOfPhase", () => {
  156. it("落点是该阶段的第一张卡;阶段还没到 → -1", () => {
  157. const cards = buildWorkspaceCards(
  158. [
  159. node({ op: "read", args: ["a"] }),
  160. node({ siteId: "world-read#2", op: "git-status", args: [] }),
  161. ],
  162. GRAPH,
  163. );
  164. expect(firstCardIndexOfPhase(cards, "phase#a")).toBe(0);
  165. expect(firstCardIndexOfPhase(cards, "phase#b")).toBe(1);
  166. expect(firstCardIndexOfPhase(cards, "phase#z")).toBe(-1);
  167. });
  168. });
  169. describe("formatters", () => {
  170. it("命令行:带空格的实参加引号", () => {
  171. expect(formatCommandLine("git", ["log", "-n", "5"])).toBe("git log -n 5");
  172. expect(formatCommandLine("echo", ["hello world"])).toBe('echo "hello world"');
  173. });
  174. it("耗时与字节数的三档", () => {
  175. expect(formatWorkspaceDuration(840)).toBe("840ms");
  176. expect(formatWorkspaceDuration(1_300)).toBe("1.3s");
  177. expect(formatWorkspaceDuration(125_000)).toBe("2m 05s");
  178. expect(formatWorkspaceBytes(312)).toBe("312 B");
  179. expect(formatWorkspaceBytes(4_200)).toBe("4.1 KB");
  180. expect(formatWorkspaceBytes(1_300_000)).toBe("1.2 MB");
  181. });
  182. it("超时的判据:code 或 message 说 timeout", () => {
  183. expect(
  184. isTimeoutError({ code: "DriverError", message: "command timed out after 300000ms" }),
  185. ).toBe(true);
  186. expect(isTimeoutError({ code: "WorldRunTimeout", message: "x" })).toBe(true);
  187. expect(isTimeoutError({ code: "DriverError", message: "spawn ENOENT" })).toBe(false);
  188. expect(isTimeoutError(undefined)).toBe(false);
  189. });
  190. });