workflowNotificationToolRow.test.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // 后台 workflow 通知行(docs/dynamic-workflow/transcript-and-notifications.md「The notification row」)——工具卡语法。
  2. //
  3. // 文件名是 `.test.ts`(不是 `.test.tsx`):根 vitest 只收 packages/*/test/**/*.test.ts。
  4. // 照 workflowEscalationToolCards 用 createElement + renderToStaticMarkup;展开态经 forceOpen
  5. // 透传给 ToolLayout(ToolLayout 自管开合,静态渲染默认折叠,forceOpen 才把展开体渲进 DOM)。
  6. import { createElement, type ReactNode } from "react";
  7. import { renderToStaticMarkup } from "react-dom/server";
  8. import { describe, expect, it, vi } from "vitest";
  9. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  10. // CodeBlock 依赖 shiki / CodeViewer 根 provider,静态渲染跑不动;照 submit-result / escalation
  11. // 的先例替换为桩,好让 json result 分支可断言。
  12. vi.mock("@/components/ai-elements/code-block.js", () => ({
  13. CodeBlockHeader: () => null,
  14. CodeBlock: ({ code, language }: { code: string; language?: string; children?: ReactNode }) =>
  15. createElement("pre", { "data-testid": "code-block", "data-language": language }, code),
  16. }));
  17. // eslint-disable-next-line import/first -- 必须在 code-block mock 之后再引入被测组件。
  18. import {
  19. WorkflowNotificationToolRow,
  20. type WorkflowNotificationToolRowProps,
  21. } from "@/v4/WorkflowNotificationToolRow.js";
  22. function render(
  23. props: Partial<WorkflowNotificationToolRowProps> &
  24. Pick<WorkflowNotificationToolRowProps, "notification">,
  25. locale: "en-US" | "zh-CN" = "en-US",
  26. ): string {
  27. return renderToStaticMarkup(
  28. createElement(
  29. ZCodeIntlProvider,
  30. { initialLocale: locale },
  31. createElement(WorkflowNotificationToolRow, {
  32. runName: "flaky-test-triage",
  33. testIdKey: "turn-1:1",
  34. theme: "system",
  35. ...props,
  36. } as WorkflowNotificationToolRowProps),
  37. ),
  38. );
  39. }
  40. const EXPAND_TOGGLE_LABEL = "Expand tool details";
  41. const OPEN_RUN_LABEL = "Open run details";
  42. describe("WorkflowNotificationToolRow — terminal", () => {
  43. it("kindLabel 承载三个终态(工具卡语法,无失败装置)", () => {
  44. expect(
  45. render({ notification: { kind: "terminal", status: "completed", summary: "s" } }),
  46. ).toContain("Workflow completed");
  47. expect(
  48. render({
  49. notification: { kind: "terminal", status: "errored", summary: "s", error: "boom" },
  50. }),
  51. ).toContain("Workflow errored");
  52. expect(
  53. render({ notification: { kind: "terminal", status: "stopped", summary: "s" } }),
  54. ).toContain("Workflow stopped");
  55. });
  56. it("stopped 带原因词:kindLabel 后跟 `· <reason>`(决策 15)", () => {
  57. const html = render({
  58. notification: { kind: "terminal", status: "stopped", stopReason: "provider", summary: "s" },
  59. });
  60. expect(html).toContain("Workflow stopped · model error");
  61. expect(
  62. render({
  63. notification: { kind: "terminal", status: "stopped", stopReason: "user", summary: "s" },
  64. }),
  65. ).toContain("Workflow stopped · by you");
  66. });
  67. it("stopped 而带 error(provider / interrupted)→ 可展开,error 走错误面板", () => {
  68. const html = render({
  69. notification: {
  70. kind: "terminal",
  71. status: "stopped",
  72. stopReason: "provider",
  73. summary: "s",
  74. error: "Sign-in to BigModel expired.",
  75. },
  76. forceOpen: true,
  77. });
  78. expect(html).toContain("Sign-in to BigModel expired.");
  79. expect(html).toContain("border-destructive/40");
  80. });
  81. it("stall 通知:kindLabel「等待模型」+ 沙漏图标,展开体给等待时长 / 原因 / 并发数", () => {
  82. const html = render({
  83. notification: { kind: "stall", sinceMs: 20 * 60_000, reason: "rate_limited", cap: 2 },
  84. forceOpen: true,
  85. });
  86. expect(html).toContain("Workflow waiting on the model");
  87. expect(html).toContain("No model request in this run has succeeded for 20 min.");
  88. expect(html).toContain("rate_limited");
  89. expect(html).toContain("Max concurrency");
  90. expect(html).toContain("workflow-notification-stall");
  91. });
  92. it("primaryText = 分隔符与普通字体 run 名", () => {
  93. const html = render({
  94. notification: { kind: "terminal", status: "completed", summary: "s", result: "r" },
  95. });
  96. expect(html).toContain("flaky-test-triage");
  97. expect(html).not.toContain("font-mono");
  98. // testid 挂在行根,供 turn-group 断言。
  99. expect(html).toContain('data-testid="chat-workflow-notification-row-turn-1:1"');
  100. });
  101. it("展开体:completed 的 prose result 走 <p>(不进 code-block)", () => {
  102. const html = render({
  103. notification: {
  104. kind: "terminal",
  105. status: "completed",
  106. summary: "s",
  107. result: "Confirmed 3 flaky tests; harness race shared by two of them.",
  108. resultForm: "prose",
  109. },
  110. forceOpen: true,
  111. });
  112. expect(html).toContain("Confirmed 3 flaky tests; harness race shared by two of them.");
  113. expect(html).not.toContain('data-testid="code-block"');
  114. expect(html).toContain("max-h-80");
  115. expect(html).not.toContain("border-border bg-panel");
  116. });
  117. it("展开体:json result 进 CodeBlock 桩(language=json)", () => {
  118. const html = render({
  119. notification: {
  120. kind: "terminal",
  121. status: "completed",
  122. summary: "s",
  123. result: '{ "confirmed": 3 }',
  124. resultForm: "json",
  125. },
  126. forceOpen: true,
  127. });
  128. expect(html).toContain('data-testid="code-block"');
  129. expect(html).toContain('data-language="json"');
  130. expect(html).toContain("&quot;confirmed&quot;");
  131. });
  132. it("展开体:errored 的 error 走 border-destructive/40 面板(非行级失败装置)", () => {
  133. const html = render({
  134. notification: {
  135. kind: "terminal",
  136. status: "errored",
  137. summary: "s",
  138. error: "NODE_LIMIT exceeded: run spawned 100 nodes (budget 100).",
  139. },
  140. forceOpen: true,
  141. });
  142. expect(html).toContain("NODE_LIMIT exceeded: run spawned 100 nodes (budget 100).");
  143. expect(html).toContain("border-destructive/40");
  144. });
  145. it("result 与 error 双缺 → 行不可展开(无 chevron)", () => {
  146. const html = render({ notification: { kind: "terminal", status: "completed", summary: "s" } });
  147. expect(html).toContain("Workflow completed");
  148. expect(html).not.toContain(EXPAND_TOGGLE_LABEL);
  149. });
  150. it("errored 但 error 缺席 → 无 chevron", () => {
  151. const html = render({ notification: { kind: "terminal", status: "errored", summary: "s" } });
  152. expect(html).toContain("Workflow errored");
  153. expect(html).not.toContain(EXPAND_TOGGLE_LABEL);
  154. });
  155. it("打开运行详情链接:onOpenRun 在场时渲染,缺席时不渲染", () => {
  156. const withCb = render({
  157. notification: { kind: "terminal", status: "completed", summary: "s", result: "r" },
  158. onOpenRun: () => {},
  159. forceOpen: true,
  160. });
  161. expect(withCb).toContain(OPEN_RUN_LABEL);
  162. const without = render({
  163. notification: { kind: "terminal", status: "completed", summary: "s", result: "r" },
  164. forceOpen: true,
  165. });
  166. expect(without).not.toContain(OPEN_RUN_LABEL);
  167. });
  168. });
  169. const ESCALATION = {
  170. kind: "escalation",
  171. qid: "q_7f2c",
  172. actor: "planner #3",
  173. question:
  174. "Fix the harness once (larger blast radius) or patch each test individually (safe but leaves the race)?",
  175. context: "Harness fix = ~40 lines in test/fixtures/harness.ts.",
  176. askedAt: Date.now() - 5 * 60_000,
  177. } as const;
  178. describe("WorkflowNotificationToolRow — escalation", () => {
  179. it("kindLabel 三态由 pendingQids 活翻转", () => {
  180. expect(render({ notification: ESCALATION, pendingQids: new Set(["q_7f2c"]) })).toContain(
  181. "Subagent is waiting for an answer",
  182. );
  183. expect(render({ notification: ESCALATION, pendingQids: new Set<string>() })).toContain(
  184. "Subagent question answered",
  185. );
  186. expect(render({ notification: ESCALATION })).toContain("Subagent asked a question");
  187. });
  188. it("primaryText = 问题单行概要(折叠态可见,全文不挂载)", () => {
  189. const html = render({ notification: ESCALATION, pendingQids: new Set(["q_7f2c"]) });
  190. // 概要(问题被 160 截断/折行;这里短,整句作为概要出现)。
  191. expect(html).toContain("Fix the harness once (larger blast radius)");
  192. // 折叠态不挂载 context。
  193. expect(html).not.toContain("Harness fix = ~40 lines in test/fixtures/harness.ts.");
  194. });
  195. it("展开体:问题全文 + context;不引用答案原文", () => {
  196. const html = render({
  197. notification: ESCALATION,
  198. pendingQids: new Set(["q_7f2c"]),
  199. forceOpen: true,
  200. });
  201. expect(html).toContain(
  202. "Fix the harness once (larger blast radius) or patch each test individually (safe but leaves the race)?",
  203. );
  204. expect(html).toContain("Harness fix = ~40 lines in test/fixtures/harness.ts.");
  205. });
  206. it("等待时长只在 waiting 态出现", () => {
  207. const waiting = render({
  208. notification: ESCALATION,
  209. pendingQids: new Set(["q_7f2c"]),
  210. forceOpen: true,
  211. });
  212. expect(waiting).toContain("5m ago");
  213. const answered = render({
  214. notification: ESCALATION,
  215. pendingQids: new Set<string>(),
  216. forceOpen: true,
  217. });
  218. expect(answered).not.toContain("5m ago");
  219. });
  220. });