v4ConversationBackgroundWorkTrigger.test.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import { createElement, type ReactNode } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import type { BackgroundWorkSummary } from "@zcode/shared/zcode-protocol-v4";
  5. vi.mock("@/ControlHintTooltip.js", () => ({
  6. ControlHintTooltip: ({ children, title }: { children: ReactNode; title: string }) =>
  7. createElement("div", { "data-tooltip": title }, children),
  8. }));
  9. vi.mock("@/i18n/IntlProvider.js", () => ({
  10. useZCodeIntl: () => ({
  11. intl: {
  12. formatMessage: (descriptor: { id: string }, values?: Record<string, string>) =>
  13. values
  14. ? `${descriptor.id} ${values.bashCount}/${values.workflowCount}/${values.subagentCount}/${values.count}`
  15. : descriptor.id,
  16. },
  17. }),
  18. }));
  19. function backgroundWork(overrides: Partial<BackgroundWorkSummary> = {}): BackgroundWorkSummary {
  20. return {
  21. workId: "work-1",
  22. kind: "bash",
  23. title: "Run checks",
  24. status: "running",
  25. startedAt: 1_700_000_000_000,
  26. anchorRowId: null,
  27. ...overrides,
  28. };
  29. }
  30. async function renderTrigger(
  31. backgroundWorks: readonly BackgroundWorkSummary[],
  32. runningSubagentCount = 0,
  33. onOpen: (() => void) | undefined = vi.fn(),
  34. openTarget?: "panel" | "workflow-run",
  35. ) {
  36. const { ConversationBackgroundWorkTrigger } =
  37. await import("@/v4/composer/ConversationBackgroundWorkTrigger.js");
  38. return renderToStaticMarkup(
  39. createElement(ConversationBackgroundWorkTrigger, {
  40. backgroundWorks,
  41. runningSubagentCount,
  42. onOpen,
  43. ...(openTarget ? { openTarget } : {}),
  44. }),
  45. );
  46. }
  47. describe("ConversationBackgroundWorkTrigger", () => {
  48. it("counts running Bash work and the authoritative running Agent projection", async () => {
  49. const { getComposerBackgroundWorkCounts } =
  50. await import("@/v4/composer/ConversationBackgroundWorkTrigger.js");
  51. const works = [
  52. backgroundWork(),
  53. backgroundWork({ workId: "bash-2" }),
  54. backgroundWork({ workId: "agent-1", kind: "subagent" }),
  55. backgroundWork({ workId: "pending", status: "resultPending" }),
  56. backgroundWork({ workId: "failed", status: "failed" }),
  57. backgroundWork({ workId: "cancelled", status: "cancelled" }),
  58. ];
  59. expect(getComposerBackgroundWorkCounts(works, 1)).toEqual({
  60. bashCount: 2,
  61. workflowCount: 0,
  62. subagentCount: 1,
  63. totalCount: 3,
  64. });
  65. });
  66. it("renders one accessible button with typed wide counts and a compact total", async () => {
  67. const html = await renderTrigger([backgroundWork()], 2);
  68. expect(html).toContain('data-testid="v4-composer-background-work-trigger"');
  69. expect(html).toContain('data-background-bash-count="1"');
  70. expect(html).toContain('data-background-workflow-count="0"');
  71. expect(html).toContain('data-background-subagent-count="2"');
  72. expect(html).toContain('data-background-total-count="3"');
  73. expect(html).toContain('data-composer-background-layout="typed"');
  74. expect(html).toContain("@max-[480px]/composer:hidden");
  75. expect(html).toContain('data-composer-background-layout="compact"');
  76. expect(html).toContain("@max-[480px]/composer:inline-flex");
  77. expect(html).toContain('type="button"');
  78. expect(html).toContain('aria-label="chat.composer.backgroundWorks.ariaLabel 1/0/2/3"');
  79. expect(html).toContain('data-tooltip="chat.composer.backgroundWorks.tooltipMixed"');
  80. });
  81. it("uses a type-specific tooltip, omits zero segments, and hides after the last work", async () => {
  82. const bashOnly = await renderTrigger([
  83. backgroundWork(),
  84. backgroundWork({ workId: "agent-terminal", kind: "subagent", status: "failed" }),
  85. ]);
  86. expect(bashOnly).toContain('data-background-bash-count="1"');
  87. expect(bashOnly).toContain('data-background-subagent-count="0"');
  88. expect(bashOnly).not.toContain("lucide-bot");
  89. expect(bashOnly).toContain('data-tooltip="chat.composer.backgroundWorks.tooltipTerminal"');
  90. const agentOnly = await renderTrigger([], 1);
  91. expect(agentOnly).toContain('data-background-bash-count="0"');
  92. expect(agentOnly).not.toContain("lucide-square-terminal");
  93. expect(agentOnly).toContain('data-tooltip="chat.composer.backgroundWorks.tooltipAgent"');
  94. const terminalOnly = await renderTrigger([
  95. backgroundWork({ status: "resultPending" }),
  96. backgroundWork({ workId: "failed", kind: "subagent", status: "failed" }),
  97. ]);
  98. expect(terminalOnly).toBe("");
  99. expect(await renderTrigger([], 0, undefined)).toBe("");
  100. });
  101. });
  102. // workflow 从 bashCount 拆成自己的一类(docs/dynamic-workflow/presentation.md「Other places a run appears」item 6)。
  103. // 拆分不能顺手把入口也拆掉:**只有 workflow 在跑时 badge 仍必须出现**,因为面板里才有停止
  104. // 按钮(取消的三个入口之一)——这正是拆分前把它塞进 bashCount 所保住的那件事。
  105. describe("getComposerBackgroundWorkCounts with dwf workflow runs", () => {
  106. it("counts running workflow runs as their own type instead of Bash", async () => {
  107. const { getComposerBackgroundWorkCounts } = await import(
  108. "@/v4/composer/ConversationBackgroundWorkTrigger.js"
  109. );
  110. expect(
  111. getComposerBackgroundWorkCounts(
  112. [
  113. backgroundWork({ workId: "bash-1", kind: "bash" }),
  114. backgroundWork({ workId: "dwfrun-1", kind: "workflow" }),
  115. backgroundWork({ workId: "dwfrun-2", kind: "workflow" }),
  116. ],
  117. 1,
  118. ),
  119. ).toEqual({ bashCount: 1, workflowCount: 2, subagentCount: 1, totalCount: 4 });
  120. });
  121. it("ignores settled workflow runs", async () => {
  122. const { getComposerBackgroundWorkCounts } = await import(
  123. "@/v4/composer/ConversationBackgroundWorkTrigger.js"
  124. );
  125. expect(
  126. getComposerBackgroundWorkCounts([
  127. backgroundWork({ workId: "dwfrun-1", kind: "workflow", status: "failed" }),
  128. backgroundWork({ workId: "dwfrun-2", kind: "workflow", status: "resultPending" }),
  129. ]),
  130. ).toEqual({ bashCount: 0, workflowCount: 0, subagentCount: 0, totalCount: 0 });
  131. });
  132. it("keeps the badge and its own icon when only a workflow runs", async () => {
  133. const html = await renderTrigger([backgroundWork({ workId: "dwfrun-1", kind: "workflow" })]);
  134. expect(html).toContain('data-testid="v4-composer-background-work-trigger"');
  135. expect(html).toContain('data-background-workflow-count="1"');
  136. expect(html).toContain('data-background-bash-count="0"');
  137. expect(html).toContain('data-background-total-count="1"');
  138. expect(html).toContain("lucide-workflow");
  139. expect(html).not.toContain("lucide-square-terminal");
  140. expect(html).not.toContain("lucide-bot");
  141. expect(html).toContain('data-tooltip="chat.composer.backgroundWorks.tooltipWorkflow"');
  142. expect(html).toContain('aria-label="chat.composer.backgroundWorks.ariaLabel 0/1/0/1"');
  143. });
  144. it("falls back to the mixed tooltip once a second type joins the workflow", async () => {
  145. const html = await renderTrigger([
  146. backgroundWork({ workId: "dwfrun-1", kind: "workflow" }),
  147. backgroundWork({ workId: "bash-1", kind: "bash" }),
  148. ]);
  149. expect(html).toContain('data-tooltip="chat.composer.backgroundWorks.tooltipMixed"');
  150. expect(html).toContain("lucide-workflow");
  151. expect(html).toContain("lucide-square-terminal");
  152. });
  153. // 直达(docs/dynamic-workflow/presentation.md):宿主判定唯一在跑的工作流可直开详情页时,
  154. // 徽标换 tooltip 并把落点暴露成结构化属性;缺省仍是展开胶囊。
  155. it("announces the direct run-details target when the host resolves one", async () => {
  156. const direct = await renderTrigger(
  157. [backgroundWork({ workId: "dwfrun-1", kind: "workflow" })],
  158. 0,
  159. vi.fn(),
  160. "workflow-run",
  161. );
  162. expect(direct).toContain('data-background-open-target="workflow-run"');
  163. expect(direct).toContain('data-tooltip="chat.composer.backgroundWorks.tooltipWorkflowDetails"');
  164. expect(direct).toContain('data-background-workflow-count="1"');
  165. const panel = await renderTrigger([backgroundWork({ workId: "dwfrun-1", kind: "workflow" })]);
  166. expect(panel).toContain('data-background-open-target="panel"');
  167. expect(panel).toContain('data-tooltip="chat.composer.backgroundWorks.tooltipWorkflow"');
  168. });
  169. it("orders the typed segments terminal-first, then workflow, then agent", async () => {
  170. const html = await renderTrigger(
  171. [
  172. backgroundWork({ workId: "dwfrun-1", kind: "workflow" }),
  173. backgroundWork({ workId: "bash-1", kind: "bash" }),
  174. ],
  175. 1,
  176. );
  177. const order = ["lucide-square-terminal", "lucide-workflow", "lucide-bot"].map((icon) =>
  178. html.indexOf(icon),
  179. );
  180. expect(order.every((index) => index >= 0)).toBe(true);
  181. expect(order).toEqual([...order].sort((left, right) => left - right));
  182. });
  183. });