workflowTurnCompletion.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // 完成卡的解析(docs/dynamic-workflow/transcript-and-notifications.md「When the card appears」):只有 completed 的
  2. // 终态工作流通知轮解析出卡;errored / stopped / 升级 / 载荷缺席 / bash 通知都不。
  3. import { describe, expect, it } from "vitest";
  4. import type { TurnHeaderRow } from "@zcode/shared/zcode-protocol-v4";
  5. import type { WorkflowRunCardSummary } from "@/ToolCallBlocks/shared.js";
  6. import { resolveWorkflowTurnCompletion } from "@/v4/workflowTurnCompletion.js";
  7. function header(overrides: Partial<TurnHeaderRow> = {}): TurnHeaderRow {
  8. return {
  9. rowId: 1,
  10. turnId: "turn",
  11. createdAt: 1_700_000_000_000,
  12. createdAtSeq: 1,
  13. kind: "turnHeader",
  14. origin: "backgroundResult",
  15. state: "completedSuccess",
  16. startedAt: 1_700_000_000_000,
  17. ...overrides,
  18. };
  19. }
  20. function terminal(
  21. status: "completed" | "errored" | "stopped",
  22. extra: Record<string, unknown> = {},
  23. ) {
  24. return header({
  25. originMeta: {
  26. backgroundSource: "workflow",
  27. title: " deep-research ",
  28. workId: "run-1",
  29. workflowNotification: {
  30. kind: "terminal",
  31. status,
  32. summary: "done",
  33. durationMs: 708_000,
  34. artifacts: [{ id: "brief", kind: "markdown", title: "research-brief.md", version: 1 }],
  35. ...extra,
  36. },
  37. },
  38. });
  39. }
  40. const summary: WorkflowRunCardSummary = {
  41. runId: "run-1",
  42. toolCallId: "tool-1",
  43. status: "completed",
  44. nodesSettled: 16,
  45. nodesTotal: 16,
  46. };
  47. describe("resolveWorkflowTurnCompletion", () => {
  48. it("completed 的终态通知轮解析出卡:名字去空白、时间、产物、联接到的 run", () => {
  49. const completion = resolveWorkflowTurnCompletion(terminal("completed"), {
  50. byRunId: new Map([["run-1", summary]]),
  51. });
  52. expect(completion).toMatchObject({
  53. runId: "run-1",
  54. name: "deep-research",
  55. durationMs: 708_000,
  56. artifactsTruncated: false,
  57. summary: { toolCallId: "tool-1" },
  58. });
  59. expect(completion?.artifacts).toHaveLength(1);
  60. });
  61. it("联接不到 run(冷恢复)仍解析出卡,只是 summary 缺席;砍过的载荷置 truncated", () => {
  62. const completion = resolveWorkflowTurnCompletion(
  63. terminal("completed", { artifactsTruncated: true, durationMs: undefined }),
  64. {},
  65. );
  66. expect(completion?.summary).toBeUndefined();
  67. expect(completion?.artifactsTruncated).toBe(true);
  68. expect(completion?.durationMs).toBeUndefined();
  69. });
  70. it("产物载荷缺席 ⇒ 空清单(卡仍画:run 完成了、有代价)", () => {
  71. const completion = resolveWorkflowTurnCompletion(
  72. terminal("completed", { artifacts: undefined }),
  73. {},
  74. );
  75. expect(completion?.artifacts).toEqual([]);
  76. });
  77. it.each(["errored", "stopped"] as const)("%s 不画卡", (status) => {
  78. expect(resolveWorkflowTurnCompletion(terminal(status), {})).toBeUndefined();
  79. });
  80. it("升级通知、载荷缺席、bash / subagent 通知、普通用户轮都不是", () => {
  81. expect(
  82. resolveWorkflowTurnCompletion(
  83. header({
  84. originMeta: {
  85. backgroundSource: "workflow",
  86. title: "x",
  87. workId: "run-1",
  88. workflowNotification: { kind: "escalation", qid: "q", actor: "a", question: "?" },
  89. },
  90. }),
  91. {},
  92. ),
  93. ).toBeUndefined();
  94. expect(
  95. resolveWorkflowTurnCompletion(
  96. header({ originMeta: { backgroundSource: "workflow", title: "x", workId: "run-1" } }),
  97. {},
  98. ),
  99. ).toBeUndefined();
  100. expect(
  101. resolveWorkflowTurnCompletion(
  102. header({ originMeta: { backgroundSource: "bash", title: "x", workId: "b" } }),
  103. {},
  104. ),
  105. ).toBeUndefined();
  106. expect(resolveWorkflowTurnCompletion(header({ origin: "userInput" }), {})).toBeUndefined();
  107. expect(resolveWorkflowTurnCompletion(undefined, {})).toBeUndefined();
  108. });
  109. });