savedWorkflowLaunchDialog.test.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // @vitest-environment jsdom
  2. // 实参窗(docs/dynamic-workflow/launch.md「The launch dialog」):按类型出控件、默认值回填、一次收齐错误。
  3. import { createElement } from "react";
  4. import { cleanup, fireEvent, render, screen } from "@testing-library/react";
  5. import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
  6. import {
  7. TID_WORKFLOW_LAUNCH_ARG,
  8. TID_WORKFLOW_LAUNCH_DIALOG,
  9. TID_WORKFLOW_LAUNCH_ERROR,
  10. TID_WORKFLOW_LAUNCH_SUBMIT,
  11. TID_WORKFLOW_LAUNCH_TARGET,
  12. testId,
  13. type ZCodeSavedWorkflowEntry,
  14. } from "@zcode/shared";
  15. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  16. import {
  17. resolveAutomationWorkspaceSelectionKey,
  18. type AutomationWorkspaceOption,
  19. } from "@/settings/automationWorkspaceOptions.js";
  20. import { SavedWorkflowLaunchDialog } from "@/settings/saved-workflows/SavedWorkflowLaunchDialog.js";
  21. import type { SavedWorkflowLaunchError } from "@/settings/saved-workflows/useSavedWorkflowLauncher.js";
  22. const ENTRY: ZCodeSavedWorkflowEntry = {
  23. name: "release-check",
  24. description: "发布前检查",
  25. scope: "project",
  26. path: "/repo/.zcode/workflows/release-check.dwf.ts",
  27. args: {
  28. branch: { type: "string", required: true, description: "目标分支" },
  29. count: { type: "number", default: 3 },
  30. dry: { type: "boolean", default: true },
  31. extra: { type: "json" },
  32. },
  33. };
  34. const GLOBAL_ENTRY: ZCodeSavedWorkflowEntry = {
  35. name: "deep-research",
  36. description: "深度调研",
  37. scope: "global",
  38. path: "/home/.zcode/workflows/deep-research.dwf.ts",
  39. };
  40. const ALPHA: AutomationWorkspaceOption = { workspacePath: "/repo/alpha", label: "Alpha" };
  41. const BETA: AutomationWorkspaceOption = { workspacePath: "/repo/beta", label: "Beta" };
  42. const TARGETS: readonly AutomationWorkspaceOption[] = [ALPHA, BETA];
  43. // Radix Dialog 在 jsdom 下需要 ResizeObserver;这里给个空实现。
  44. beforeAll(() => {
  45. (globalThis as { ResizeObserver?: unknown }).ResizeObserver ??= class {
  46. observe() {}
  47. unobserve() {}
  48. disconnect() {}
  49. };
  50. });
  51. afterEach(() => {
  52. cleanup();
  53. });
  54. function mount(
  55. entry: ZCodeSavedWorkflowEntry | null,
  56. extra: {
  57. targets?: readonly AutomationWorkspaceOption[];
  58. defaultTargetKey?: string | null;
  59. scope?: "project" | "global";
  60. projectLabel?: string;
  61. pending?: boolean;
  62. error?: SavedWorkflowLaunchError | null;
  63. } = {},
  64. onSubmit = vi.fn(),
  65. onOpenChange = vi.fn(),
  66. ) {
  67. const { scope, projectLabel, ...rest } = extra;
  68. render(
  69. createElement(
  70. ZCodeIntlProvider,
  71. { initialLocale: "zh-CN" },
  72. createElement(SavedWorkflowLaunchDialog, {
  73. entry,
  74. scope: scope ?? entry?.scope ?? "project",
  75. projectLabel: projectLabel ?? "Beta",
  76. onOpenChange,
  77. onSubmit,
  78. ...rest,
  79. }),
  80. ),
  81. );
  82. return { onSubmit, onOpenChange };
  83. }
  84. describe("SavedWorkflowLaunchDialog", () => {
  85. it("entry 为 null 时不渲染", () => {
  86. mount(null);
  87. expect(screen.queryByTestId(TID_WORKFLOW_LAUNCH_DIALOG)).toBeNull();
  88. });
  89. it("按类型出控件并回填默认值;required 标注;说明可见", () => {
  90. mount(ENTRY);
  91. expect(screen.getByTestId(TID_WORKFLOW_LAUNCH_DIALOG)).toBeTruthy();
  92. // 头部 = 名字(mono)+ 作用域徽标 + 说明;不再是「运行 {name}」标题。
  93. expect(screen.getByText("release-check")).toBeTruthy();
  94. expect(screen.getByText("项目")).toBeTruthy();
  95. expect(screen.getByText("发布前检查")).toBeTruthy();
  96. const branch = screen.getByTestId(
  97. testId(TID_WORKFLOW_LAUNCH_ARG, "branch"),
  98. ) as HTMLInputElement;
  99. expect(branch.type).toBe("text");
  100. expect(branch.value).toBe("");
  101. expect(screen.getByText("必填")).toBeTruthy();
  102. expect(screen.getByText("目标分支")).toBeTruthy();
  103. const count = screen.getByTestId(testId(TID_WORKFLOW_LAUNCH_ARG, "count")) as HTMLInputElement;
  104. expect(count.type).toBe("number");
  105. expect(count.value).toBe("3");
  106. const dry = screen.getByTestId(testId(TID_WORKFLOW_LAUNCH_ARG, "dry"));
  107. expect(dry.getAttribute("role")).toBe("switch");
  108. expect(dry.getAttribute("aria-checked")).toBe("true");
  109. const extra = screen.getByTestId(testId(TID_WORKFLOW_LAUNCH_ARG, "extra"));
  110. expect(extra.tagName).toBe("TEXTAREA");
  111. });
  112. it("提交前一次收齐 required 与 json 错误;修正后提交拿到按类型解析的实参", () => {
  113. const { onSubmit } = mount(ENTRY);
  114. fireEvent.change(screen.getByTestId(testId(TID_WORKFLOW_LAUNCH_ARG, "extra")), {
  115. target: { value: "{oops" },
  116. });
  117. fireEvent.click(screen.getByTestId(TID_WORKFLOW_LAUNCH_SUBMIT));
  118. expect(onSubmit).not.toHaveBeenCalled();
  119. // 徽标「必填」与错误「必填」同文案:错误出现后应有两处。
  120. expect(screen.getAllByText("必填")).toHaveLength(2);
  121. expect(screen.getByText("不是合法的 JSON")).toBeTruthy();
  122. fireEvent.change(screen.getByTestId(testId(TID_WORKFLOW_LAUNCH_ARG, "branch")), {
  123. target: { value: "main" },
  124. });
  125. fireEvent.change(screen.getByTestId(testId(TID_WORKFLOW_LAUNCH_ARG, "extra")), {
  126. target: { value: '{"a":1}' },
  127. });
  128. // 编辑即清掉该字段的错误提示。
  129. expect(screen.getAllByText("必填")).toHaveLength(1);
  130. expect(screen.queryByText("不是合法的 JSON")).toBeNull();
  131. fireEvent.change(screen.getByTestId(testId(TID_WORKFLOW_LAUNCH_ARG, "count")), {
  132. target: { value: "" },
  133. });
  134. fireEvent.click(screen.getByTestId(TID_WORKFLOW_LAUNCH_SUBMIT));
  135. expect(onSubmit).toHaveBeenCalledTimes(1);
  136. // count 留空 → 不传(服务端按 default 回填);dry 是 boolean 永远有值。
  137. expect(onSubmit.mock.calls[0]?.[1]).toEqual({ branch: "main", dry: true, extra: { a: 1 } });
  138. });
  139. it("取消按钮关闭窗口", () => {
  140. const { onOpenChange, onSubmit } = mount(ENTRY);
  141. fireEvent.click(screen.getByText("取消"));
  142. expect(onOpenChange).toHaveBeenCalledWith(false);
  143. expect(onSubmit).not.toHaveBeenCalled();
  144. });
  145. it("不传 targets 时不渲染「运行于」选择器(项目档路径)", () => {
  146. mount(ENTRY);
  147. expect(screen.queryByTestId(TID_WORKFLOW_LAUNCH_TARGET)).toBeNull();
  148. expect(screen.queryByText("运行于")).toBeNull();
  149. });
  150. it("传 targets 时渲染选择器,默认选中 defaultTargetKey", () => {
  151. mount(GLOBAL_ENTRY, {
  152. targets: TARGETS,
  153. defaultTargetKey: resolveAutomationWorkspaceSelectionKey(BETA),
  154. });
  155. expect(screen.getByText("运行于")).toBeTruthy();
  156. const trigger = screen.getByTestId(TID_WORKFLOW_LAUNCH_TARGET);
  157. expect(trigger.textContent).toContain("Beta");
  158. });
  159. it("defaultTargetKey 不在候选里时回落到首个候选", () => {
  160. mount(GLOBAL_ENTRY, { targets: TARGETS, defaultTargetKey: "no-such-key" });
  161. expect(screen.getByTestId(TID_WORKFLOW_LAUNCH_TARGET).textContent).toContain("Alpha");
  162. });
  163. it("候选为空时提示并禁用提交", () => {
  164. mount(GLOBAL_ENTRY, { targets: [] });
  165. expect(screen.queryByTestId(TID_WORKFLOW_LAUNCH_TARGET)).toBeNull();
  166. expect(screen.getByText("打开一个本地项目以运行")).toBeTruthy();
  167. expect((screen.getByTestId(TID_WORKFLOW_LAUNCH_SUBMIT) as HTMLButtonElement).disabled).toBe(
  168. true,
  169. );
  170. });
  171. it("无实参的全局工作流也弹窗,提交回带选中的项目", () => {
  172. const { onSubmit } = mount(GLOBAL_ENTRY, {
  173. targets: TARGETS,
  174. defaultTargetKey: resolveAutomationWorkspaceSelectionKey(ALPHA),
  175. });
  176. expect(screen.getByTestId(TID_WORKFLOW_LAUNCH_DIALOG)).toBeTruthy();
  177. fireEvent.click(screen.getByTestId(TID_WORKFLOW_LAUNCH_SUBMIT));
  178. expect(onSubmit).toHaveBeenCalledTimes(1);
  179. expect(onSubmit.mock.calls[0]?.[1]).toEqual({});
  180. expect(onSubmit.mock.calls[0]?.[2]).toEqual(ALPHA);
  181. });
  182. it("全局档头部渲染「全局」徽标", () => {
  183. mount(GLOBAL_ENTRY, { targets: TARGETS, scope: "global" });
  184. expect(screen.getByText("deep-research")).toBeTruthy();
  185. expect(screen.getByText("全局")).toBeTruthy();
  186. });
  187. it("说明文案带项目名:项目档用所属项目名", () => {
  188. mount(ENTRY, { projectLabel: "Beta" });
  189. expect(screen.getByText("将立即在 Beta 的新会话中运行")).toBeTruthy();
  190. });
  191. it("说明文案带项目名:全局档用选中的「运行于」项目名", () => {
  192. mount(GLOBAL_ENTRY, {
  193. targets: TARGETS,
  194. scope: "global",
  195. defaultTargetKey: resolveAutomationWorkspaceSelectionKey(ALPHA),
  196. });
  197. expect(screen.getByText("将立即在 Alpha 的新会话中运行")).toBeTruthy();
  198. });
  199. it("pending 时禁用提交按钮,且点击不再发起", () => {
  200. const { onSubmit } = mount(GLOBAL_ENTRY, {
  201. targets: TARGETS,
  202. scope: "global",
  203. defaultTargetKey: resolveAutomationWorkspaceSelectionKey(ALPHA),
  204. pending: true,
  205. });
  206. const submit = screen.getByTestId(TID_WORKFLOW_LAUNCH_SUBMIT) as HTMLButtonElement;
  207. expect(submit.disabled).toBe(true);
  208. fireEvent.click(submit);
  209. expect(onSubmit).not.toHaveBeenCalled();
  210. });
  211. it("行内错误区按 reason 出标题,并把服务端 message 放进 mono 块", () => {
  212. const error: SavedWorkflowLaunchError = {
  213. reason: "compile_failed",
  214. code: "fault.command.savedWorkflowStartRejected.compile_failed",
  215. message: "line 3: boom",
  216. };
  217. mount(GLOBAL_ENTRY, { targets: TARGETS, scope: "global", error });
  218. const region = screen.getByTestId(TID_WORKFLOW_LAUNCH_ERROR);
  219. expect(region.textContent).toContain("工作流脚本编译失败");
  220. expect(region.textContent).toContain("line 3: boom");
  221. });
  222. it("能力缺席错误映射到 unsupported 文案;无 message 时不渲染 mono 块", () => {
  223. const error: SavedWorkflowLaunchError = {
  224. reason: "unsupported",
  225. code: "fault.command.capabilityUnsupported",
  226. };
  227. mount(GLOBAL_ENTRY, { targets: TARGETS, scope: "global", error });
  228. const region = screen.getByTestId(TID_WORKFLOW_LAUNCH_ERROR);
  229. expect(region.textContent).toContain("当前 agent 不支持直接启动工作流");
  230. expect(region.querySelector("pre")).toBeNull();
  231. });
  232. });