workflowNotificationToolRow.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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("blocked 提醒:kindLabel「等待主代理」+ run 名,展开体一行计数与等待时长,再加 run 链接", () => {
  93. const html = render({
  94. notification: { kind: "blocked", questions: 2, holes: 1, sinceMs: 17 * 60_000 },
  95. forceOpen: true,
  96. onOpenRun: () => {},
  97. });
  98. expect(html).toContain("Workflow waiting on the agent");
  99. expect(html).toContain("flaky-test-triage");
  100. expect(html).toContain("workflow-notification-blocked");
  101. expect(html).toContain("2 questions · 1 hole · open for 17 min");
  102. expect(html).toContain(OPEN_RUN_LABEL);
  103. // 不是失败、也不是在干活:没有失败装置。
  104. expect(html).not.toContain("border-destructive");
  105. });
  106. it("blocked 提醒:零的那一种不出现;不足一分钟按 1 分钟;中文走同一组词", () => {
  107. const onlyHoles = render({
  108. notification: { kind: "blocked", questions: 0, holes: 2, sinceMs: 20_000 },
  109. forceOpen: true,
  110. });
  111. expect(onlyHoles).toContain("2 holes · open for 1 min");
  112. expect(onlyHoles).not.toContain("question");
  113. const zh = render(
  114. {
  115. notification: { kind: "blocked", questions: 1, holes: 0, sinceMs: 45 * 60_000 },
  116. forceOpen: true,
  117. },
  118. "zh-CN",
  119. );
  120. expect(zh).toContain("工作流等待主代理");
  121. expect(zh).toContain("1 个待答问题 · 已等待 45 分钟");
  122. expect(zh).not.toContain("留白");
  123. });
  124. it("blocked 提醒折叠时只有头部:计数在展开体里", () => {
  125. const html = render({
  126. notification: { kind: "blocked", questions: 2, holes: 1, sinceMs: 17 * 60_000 },
  127. });
  128. expect(html).toContain("Workflow waiting on the agent");
  129. expect(html).toContain(EXPAND_TOGGLE_LABEL);
  130. expect(html).not.toContain("open for 17 min");
  131. });
  132. it("primaryText = 分隔符与普通字体 run 名", () => {
  133. const html = render({
  134. notification: { kind: "terminal", status: "completed", summary: "s", result: "r" },
  135. });
  136. expect(html).toContain("flaky-test-triage");
  137. expect(html).not.toContain("font-mono");
  138. // testid 挂在行根,供 turn-group 断言。
  139. expect(html).toContain('data-testid="chat-workflow-notification-row-turn-1:1"');
  140. });
  141. it("展开体:completed 的 prose result 走 <p>(不进 code-block)", () => {
  142. const html = render({
  143. notification: {
  144. kind: "terminal",
  145. status: "completed",
  146. summary: "s",
  147. result: "Confirmed 3 flaky tests; harness race shared by two of them.",
  148. resultForm: "prose",
  149. },
  150. forceOpen: true,
  151. });
  152. expect(html).toContain("Confirmed 3 flaky tests; harness race shared by two of them.");
  153. expect(html).not.toContain('data-testid="code-block"');
  154. expect(html).toContain("max-h-80");
  155. expect(html).not.toContain("border-border bg-panel");
  156. });
  157. it("展开体:json result 进 CodeBlock 桩(language=json)", () => {
  158. const html = render({
  159. notification: {
  160. kind: "terminal",
  161. status: "completed",
  162. summary: "s",
  163. result: '{ "confirmed": 3 }',
  164. resultForm: "json",
  165. },
  166. forceOpen: true,
  167. });
  168. expect(html).toContain('data-testid="code-block"');
  169. expect(html).toContain('data-language="json"');
  170. expect(html).toContain("&quot;confirmed&quot;");
  171. });
  172. it("展开体:errored 的 error 走 border-destructive/40 面板(非行级失败装置)", () => {
  173. const html = render({
  174. notification: {
  175. kind: "terminal",
  176. status: "errored",
  177. summary: "s",
  178. error: "NODE_LIMIT exceeded: run spawned 100 nodes (budget 100).",
  179. },
  180. forceOpen: true,
  181. });
  182. expect(html).toContain("NODE_LIMIT exceeded: run spawned 100 nodes (budget 100).");
  183. expect(html).toContain("border-destructive/40");
  184. });
  185. it("result 与 error 双缺 → 行不可展开(无 chevron)", () => {
  186. const html = render({ notification: { kind: "terminal", status: "completed", summary: "s" } });
  187. expect(html).toContain("Workflow completed");
  188. expect(html).not.toContain(EXPAND_TOGGLE_LABEL);
  189. });
  190. it("errored 但 error 缺席 → 无 chevron", () => {
  191. const html = render({ notification: { kind: "terminal", status: "errored", summary: "s" } });
  192. expect(html).toContain("Workflow errored");
  193. expect(html).not.toContain(EXPAND_TOGGLE_LABEL);
  194. });
  195. it("打开运行详情链接:onOpenRun 在场时渲染,缺席时不渲染", () => {
  196. const withCb = render({
  197. notification: { kind: "terminal", status: "completed", summary: "s", result: "r" },
  198. onOpenRun: () => {},
  199. forceOpen: true,
  200. });
  201. expect(withCb).toContain(OPEN_RUN_LABEL);
  202. const without = render({
  203. notification: { kind: "terminal", status: "completed", summary: "s", result: "r" },
  204. forceOpen: true,
  205. });
  206. expect(without).not.toContain(OPEN_RUN_LABEL);
  207. });
  208. });
  209. const ESCALATION = {
  210. kind: "escalation",
  211. qid: "q_7f2c",
  212. actor: "planner #3",
  213. question:
  214. "Fix the harness once (larger blast radius) or patch each test individually (safe but leaves the race)?",
  215. context: "Harness fix = ~40 lines in test/fixtures/harness.ts.",
  216. askedAt: Date.now() - 5 * 60_000,
  217. } as const;
  218. describe("WorkflowNotificationToolRow — escalation", () => {
  219. it("kindLabel 三态由 pendingQids 活翻转", () => {
  220. expect(render({ notification: ESCALATION, pendingQids: new Set(["q_7f2c"]) })).toContain(
  221. "Subagent is waiting for an answer",
  222. );
  223. expect(render({ notification: ESCALATION, pendingQids: new Set<string>() })).toContain(
  224. "Subagent question answered",
  225. );
  226. expect(render({ notification: ESCALATION })).toContain("Subagent asked a question");
  227. });
  228. it("primaryText = 问题单行概要(折叠态可见,全文不挂载)", () => {
  229. const html = render({ notification: ESCALATION, pendingQids: new Set(["q_7f2c"]) });
  230. // 概要(问题被 160 截断/折行;这里短,整句作为概要出现)。
  231. expect(html).toContain("Fix the harness once (larger blast radius)");
  232. // 折叠态不挂载 context。
  233. expect(html).not.toContain("Harness fix = ~40 lines in test/fixtures/harness.ts.");
  234. });
  235. it("展开体:问题全文 + context;不引用答案原文", () => {
  236. const html = render({
  237. notification: ESCALATION,
  238. pendingQids: new Set(["q_7f2c"]),
  239. forceOpen: true,
  240. });
  241. expect(html).toContain(
  242. "Fix the harness once (larger blast radius) or patch each test individually (safe but leaves the race)?",
  243. );
  244. expect(html).toContain("Harness fix = ~40 lines in test/fixtures/harness.ts.");
  245. });
  246. it("等待时长只在 waiting 态出现", () => {
  247. const waiting = render({
  248. notification: ESCALATION,
  249. pendingQids: new Set(["q_7f2c"]),
  250. forceOpen: true,
  251. });
  252. expect(waiting).toContain("5m ago");
  253. const answered = render({
  254. notification: ESCALATION,
  255. pendingQids: new Set<string>(),
  256. forceOpen: true,
  257. });
  258. expect(answered).not.toContain("5m ago");
  259. });
  260. });