savedWorkflowLaunchDialog.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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_FIELDS,
  11. TID_WORKFLOW_LAUNCH_SUBMIT,
  12. TID_WORKFLOW_LAUNCH_TARGET,
  13. type ZCodeSavedWorkflowArgsDeclaration,
  14. type ZCodeSavedWorkflowEntry,
  15. } from "@zcode/shared";
  16. import { testId } from "@zcode/shared/test-ids";
  17. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  18. import {
  19. resolveAutomationWorkspaceSelectionKey,
  20. type AutomationWorkspaceOption,
  21. } from "@/settings/automationWorkspaceOptions.js";
  22. import { SavedWorkflowLaunchDialog } from "@/settings/saved-workflows/SavedWorkflowLaunchDialog.js";
  23. import type { SavedWorkflowLaunchError } from "@/settings/saved-workflows/useSavedWorkflowLauncher.js";
  24. const ENTRY: ZCodeSavedWorkflowEntry = {
  25. name: "release-check",
  26. description: "发布前检查",
  27. scope: "project",
  28. path: "/repo/.zcode/workflows/release-check.dwf.ts",
  29. args: {
  30. branch: { type: "string", required: true, description: "目标分支" },
  31. count: { type: "number", default: 3 },
  32. dry: { type: "boolean", default: true },
  33. extra: { type: "json" },
  34. },
  35. };
  36. const GLOBAL_ENTRY: ZCodeSavedWorkflowEntry = {
  37. name: "deep-research",
  38. description: "深度调研",
  39. scope: "global",
  40. path: "/home/.zcode/workflows/deep-research.dwf.ts",
  41. };
  42. // 实参很多的声明(40 个):第 13 个是无默认值的必填项,第 31 个是 json,其余都有默认值。
  43. // 真实窗口里这份表单远高于视口;jsdom 不排版,这里只能断言结构与焦点,高度由桌面 e2e 断言。
  44. const MANY_ARGS_REQUIRED = "arg12";
  45. const MANY_ARGS_JSON = "arg30";
  46. const MANY_ARGS: ZCodeSavedWorkflowArgsDeclaration = {};
  47. for (let index = 0; index < 40; index += 1) {
  48. const name = `arg${String(index).padStart(2, "0")}`;
  49. MANY_ARGS[name] =
  50. name === MANY_ARGS_REQUIRED
  51. ? { type: "string", required: true }
  52. : name === MANY_ARGS_JSON
  53. ? { type: "json" }
  54. : { type: "string", default: `v${index}` };
  55. }
  56. const MANY_ARGS_ENTRY: ZCodeSavedWorkflowEntry = {
  57. name: "arg-overflow",
  58. description: "实参很多",
  59. scope: "global",
  60. path: "/home/.zcode/workflows/arg-overflow.dwf.ts",
  61. args: MANY_ARGS,
  62. };
  63. const ALPHA: AutomationWorkspaceOption = { workspacePath: "/repo/alpha", label: "Alpha" };
  64. const BETA: AutomationWorkspaceOption = { workspacePath: "/repo/beta", label: "Beta" };
  65. const TARGETS: readonly AutomationWorkspaceOption[] = [ALPHA, BETA];
  66. // jsdom 没有实现 scrollIntoView;校验失败时实参窗会调它,这里记下是谁被滚进了视野。
  67. const scrollIntoView = vi.fn();
  68. // Radix Dialog 在 jsdom 下需要 ResizeObserver;这里给个空实现。
  69. beforeAll(() => {
  70. (globalThis as { ResizeObserver?: unknown }).ResizeObserver ??= class {
  71. observe() {}
  72. unobserve() {}
  73. disconnect() {}
  74. };
  75. Element.prototype.scrollIntoView = scrollIntoView;
  76. });
  77. afterEach(() => {
  78. cleanup();
  79. scrollIntoView.mockClear();
  80. });
  81. function mount(
  82. entry: ZCodeSavedWorkflowEntry | null,
  83. extra: {
  84. targets?: readonly AutomationWorkspaceOption[];
  85. defaultTargetKey?: string | null;
  86. scope?: "project" | "global";
  87. projectLabel?: string;
  88. pending?: boolean;
  89. error?: SavedWorkflowLaunchError | null;
  90. } = {},
  91. onSubmit = vi.fn(),
  92. onOpenChange = vi.fn(),
  93. ) {
  94. const { scope, projectLabel, ...rest } = extra;
  95. render(
  96. createElement(
  97. ZCodeIntlProvider,
  98. { initialLocale: "zh-CN" },
  99. createElement(SavedWorkflowLaunchDialog, {
  100. entry,
  101. scope: scope ?? entry?.scope ?? "project",
  102. projectLabel: projectLabel ?? "Beta",
  103. onOpenChange,
  104. onSubmit,
  105. ...rest,
  106. }),
  107. ),
  108. );
  109. return { onSubmit, onOpenChange };
  110. }
  111. describe("SavedWorkflowLaunchDialog", () => {
  112. it("entry 为 null 时不渲染", () => {
  113. mount(null);
  114. expect(screen.queryByTestId(TID_WORKFLOW_LAUNCH_DIALOG)).toBeNull();
  115. });
  116. it("按类型出控件并回填默认值;required 标注;说明可见", () => {
  117. mount(ENTRY);
  118. expect(screen.getByTestId(TID_WORKFLOW_LAUNCH_DIALOG)).toBeTruthy();
  119. // 头部 = 名字(mono)+ 作用域徽标 + 说明;不再是「运行 {name}」标题。
  120. expect(screen.getByText("release-check")).toBeTruthy();
  121. expect(screen.getByText("项目")).toBeTruthy();
  122. expect(screen.getByText("发布前检查")).toBeTruthy();
  123. const branch = screen.getByTestId(
  124. testId(TID_WORKFLOW_LAUNCH_ARG, "branch"),
  125. ) as HTMLInputElement;
  126. expect(branch.type).toBe("text");
  127. expect(branch.value).toBe("");
  128. expect(screen.getByText("必填")).toBeTruthy();
  129. expect(screen.getByText("目标分支")).toBeTruthy();
  130. const count = screen.getByTestId(testId(TID_WORKFLOW_LAUNCH_ARG, "count")) as HTMLInputElement;
  131. expect(count.type).toBe("number");
  132. expect(count.value).toBe("3");
  133. const dry = screen.getByTestId(testId(TID_WORKFLOW_LAUNCH_ARG, "dry"));
  134. expect(dry.getAttribute("role")).toBe("switch");
  135. expect(dry.getAttribute("aria-checked")).toBe("true");
  136. const extra = screen.getByTestId(testId(TID_WORKFLOW_LAUNCH_ARG, "extra"));
  137. expect(extra.tagName).toBe("TEXTAREA");
  138. });
  139. it("提交前一次收齐 required 与 json 错误;修正后提交拿到按类型解析的实参", () => {
  140. const { onSubmit } = mount(ENTRY);
  141. fireEvent.change(screen.getByTestId(testId(TID_WORKFLOW_LAUNCH_ARG, "extra")), {
  142. target: { value: "{oops" },
  143. });
  144. fireEvent.click(screen.getByTestId(TID_WORKFLOW_LAUNCH_SUBMIT));
  145. expect(onSubmit).not.toHaveBeenCalled();
  146. // 徽标「必填」与错误「必填」同文案:错误出现后应有两处。
  147. expect(screen.getAllByText("必填")).toHaveLength(2);
  148. expect(screen.getByText("不是合法的 JSON")).toBeTruthy();
  149. fireEvent.change(screen.getByTestId(testId(TID_WORKFLOW_LAUNCH_ARG, "branch")), {
  150. target: { value: "main" },
  151. });
  152. fireEvent.change(screen.getByTestId(testId(TID_WORKFLOW_LAUNCH_ARG, "extra")), {
  153. target: { value: '{"a":1}' },
  154. });
  155. // 编辑即清掉该字段的错误提示。
  156. expect(screen.getAllByText("必填")).toHaveLength(1);
  157. expect(screen.queryByText("不是合法的 JSON")).toBeNull();
  158. fireEvent.change(screen.getByTestId(testId(TID_WORKFLOW_LAUNCH_ARG, "count")), {
  159. target: { value: "" },
  160. });
  161. fireEvent.click(screen.getByTestId(TID_WORKFLOW_LAUNCH_SUBMIT));
  162. expect(onSubmit).toHaveBeenCalledTimes(1);
  163. // count 留空 → 不传(服务端按 default 回填);dry 是 boolean 永远有值。
  164. expect(onSubmit.mock.calls[0]?.[1]).toEqual({ branch: "main", dry: true, extra: { a: 1 } });
  165. });
  166. it("取消按钮关闭窗口", () => {
  167. const { onOpenChange, onSubmit } = mount(ENTRY);
  168. fireEvent.click(screen.getByText("取消"));
  169. expect(onOpenChange).toHaveBeenCalledWith(false);
  170. expect(onSubmit).not.toHaveBeenCalled();
  171. });
  172. it("不传 targets 时不渲染「运行于」选择器(项目档路径)", () => {
  173. mount(ENTRY);
  174. expect(screen.queryByTestId(TID_WORKFLOW_LAUNCH_TARGET)).toBeNull();
  175. expect(screen.queryByText("运行于")).toBeNull();
  176. });
  177. it("传 targets 时渲染选择器,默认选中 defaultTargetKey", () => {
  178. mount(GLOBAL_ENTRY, {
  179. targets: TARGETS,
  180. defaultTargetKey: resolveAutomationWorkspaceSelectionKey(BETA),
  181. });
  182. expect(screen.getByText("运行于")).toBeTruthy();
  183. const trigger = screen.getByTestId(TID_WORKFLOW_LAUNCH_TARGET);
  184. expect(trigger.textContent).toContain("Beta");
  185. });
  186. it("defaultTargetKey 不在候选里时回落到首个候选", () => {
  187. mount(GLOBAL_ENTRY, { targets: TARGETS, defaultTargetKey: "no-such-key" });
  188. expect(screen.getByTestId(TID_WORKFLOW_LAUNCH_TARGET).textContent).toContain("Alpha");
  189. });
  190. it("候选为空时提示并禁用提交", () => {
  191. mount(GLOBAL_ENTRY, { targets: [] });
  192. expect(screen.queryByTestId(TID_WORKFLOW_LAUNCH_TARGET)).toBeNull();
  193. expect(screen.getByText("打开一个本地项目以运行")).toBeTruthy();
  194. expect((screen.getByTestId(TID_WORKFLOW_LAUNCH_SUBMIT) as HTMLButtonElement).disabled).toBe(
  195. true,
  196. );
  197. });
  198. it("无实参的全局工作流也弹窗,提交回带选中的项目", () => {
  199. const { onSubmit } = mount(GLOBAL_ENTRY, {
  200. targets: TARGETS,
  201. defaultTargetKey: resolveAutomationWorkspaceSelectionKey(ALPHA),
  202. });
  203. expect(screen.getByTestId(TID_WORKFLOW_LAUNCH_DIALOG)).toBeTruthy();
  204. fireEvent.click(screen.getByTestId(TID_WORKFLOW_LAUNCH_SUBMIT));
  205. expect(onSubmit).toHaveBeenCalledTimes(1);
  206. expect(onSubmit.mock.calls[0]?.[1]).toEqual({});
  207. expect(onSubmit.mock.calls[0]?.[2]).toEqual(ALPHA);
  208. });
  209. it("全局档头部渲染「全局」徽标", () => {
  210. mount(GLOBAL_ENTRY, { targets: TARGETS, scope: "global" });
  211. expect(screen.getByText("deep-research")).toBeTruthy();
  212. expect(screen.getByText("全局")).toBeTruthy();
  213. });
  214. it("说明文案带项目名:项目档用所属项目名", () => {
  215. mount(ENTRY, { projectLabel: "Beta" });
  216. expect(screen.getByText("将立即在 Beta 的新会话中运行")).toBeTruthy();
  217. });
  218. it("说明文案带项目名:全局档用选中的「运行于」项目名", () => {
  219. mount(GLOBAL_ENTRY, {
  220. targets: TARGETS,
  221. scope: "global",
  222. defaultTargetKey: resolveAutomationWorkspaceSelectionKey(ALPHA),
  223. });
  224. expect(screen.getByText("将立即在 Alpha 的新会话中运行")).toBeTruthy();
  225. });
  226. it("pending 时禁用提交按钮,且点击不再发起", () => {
  227. const { onSubmit } = mount(GLOBAL_ENTRY, {
  228. targets: TARGETS,
  229. scope: "global",
  230. defaultTargetKey: resolveAutomationWorkspaceSelectionKey(ALPHA),
  231. pending: true,
  232. });
  233. const submit = screen.getByTestId(TID_WORKFLOW_LAUNCH_SUBMIT) as HTMLButtonElement;
  234. expect(submit.disabled).toBe(true);
  235. fireEvent.click(submit);
  236. expect(onSubmit).not.toHaveBeenCalled();
  237. });
  238. it("行内错误区按 reason 出标题,并把服务端 message 放进 mono 块", () => {
  239. const error: SavedWorkflowLaunchError = {
  240. reason: "compile_failed",
  241. code: "fault.command.savedWorkflowStartRejected.compile_failed",
  242. message: "line 3: boom",
  243. };
  244. mount(GLOBAL_ENTRY, { targets: TARGETS, scope: "global", error });
  245. const region = screen.getByTestId(TID_WORKFLOW_LAUNCH_ERROR);
  246. expect(region.textContent).toContain("工作流脚本编译失败");
  247. expect(region.textContent).toContain("line 3: boom");
  248. });
  249. it("能力缺席错误映射到 unsupported 文案;无 message 时不渲染 mono 块", () => {
  250. const error: SavedWorkflowLaunchError = {
  251. reason: "unsupported",
  252. code: "fault.command.capabilityUnsupported",
  253. };
  254. mount(GLOBAL_ENTRY, { targets: TARGETS, scope: "global", error });
  255. const region = screen.getByTestId(TID_WORKFLOW_LAUNCH_ERROR);
  256. expect(region.textContent).toContain("当前 agent 不支持直接启动工作流");
  257. expect(region.querySelector("pre")).toBeNull();
  258. });
  259. it("只有「运行于」与实参字段在滚动区内;说明、错误区、按钮钉在滚动区外", () => {
  260. const error: SavedWorkflowLaunchError = {
  261. reason: "compile_failed",
  262. code: "fault.command.savedWorkflowStartRejected.compile_failed",
  263. message: "line 3: boom",
  264. };
  265. mount(MANY_ARGS_ENTRY, { targets: TARGETS, scope: "global", projectLabel: "Alpha", error });
  266. const dialog = screen.getByTestId(TID_WORKFLOW_LAUNCH_DIALOG);
  267. const fields = screen.getByTestId(TID_WORKFLOW_LAUNCH_FIELDS);
  268. expect(dialog.contains(fields)).toBe(true);
  269. // 滚动区内:「运行于」与全部 40 个实参字段。
  270. expect(fields.contains(screen.getByTestId(TID_WORKFLOW_LAUNCH_TARGET))).toBe(true);
  271. for (const name of Object.keys(MANY_ARGS)) {
  272. expect(fields.contains(screen.getByTestId(testId(TID_WORKFLOW_LAUNCH_ARG, name)))).toBe(true);
  273. }
  274. // 滚动区外、窗口内:名字、说明文案、错误区、取消与提交——实参再多它们也不会被顶出视口。
  275. const pinned = [
  276. screen.getByText("arg-overflow"),
  277. screen.getByText("将立即在 Alpha 的新会话中运行"),
  278. screen.getByTestId(TID_WORKFLOW_LAUNCH_ERROR),
  279. screen.getByText("取消"),
  280. screen.getByTestId(TID_WORKFLOW_LAUNCH_SUBMIT),
  281. ];
  282. for (const element of pinned) {
  283. expect(dialog.contains(element)).toBe(true);
  284. expect(fields.contains(element)).toBe(false);
  285. }
  286. });
  287. it("没有「运行于」也没有实参时不渲染滚动区", () => {
  288. mount({ ...GLOBAL_ENTRY, scope: "project" });
  289. expect(screen.getByTestId(TID_WORKFLOW_LAUNCH_DIALOG)).toBeTruthy();
  290. expect(screen.queryByTestId(TID_WORKFLOW_LAUNCH_FIELDS)).toBeNull();
  291. });
  292. it("校验失败:第一个出错字段被滚进视野并拿到焦点", () => {
  293. const { onSubmit } = mount(MANY_ARGS_ENTRY, { targets: TARGETS, scope: "global" });
  294. const required = screen.getByTestId(testId(TID_WORKFLOW_LAUNCH_ARG, MANY_ARGS_REQUIRED));
  295. const json = screen.getByTestId(testId(TID_WORKFLOW_LAUNCH_ARG, MANY_ARGS_JSON));
  296. fireEvent.change(json, { target: { value: "{oops" } });
  297. fireEvent.click(screen.getByTestId(TID_WORKFLOW_LAUNCH_SUBMIT));
  298. expect(onSubmit).not.toHaveBeenCalled();
  299. // 两个字段都出错:按声明顺序取第一个(必填项),而不是后面的 json。
  300. expect(document.activeElement).toBe(required);
  301. expect(scrollIntoView).toHaveBeenCalledTimes(1);
  302. const revealed = scrollIntoView.mock.contexts[0] as HTMLElement;
  303. // 滚进视野的是整行(标签 + 控件 + 错误文案),不只是输入框本身。
  304. expect(revealed.contains(required)).toBe(true);
  305. expect(revealed.textContent).toContain("必填");
  306. expect(revealed.contains(json)).toBe(false);
  307. // 修掉第一个后再提交:轮到下一个出错字段。
  308. fireEvent.change(required, { target: { value: "prod" } });
  309. fireEvent.click(screen.getByTestId(TID_WORKFLOW_LAUNCH_SUBMIT));
  310. expect(onSubmit).not.toHaveBeenCalled();
  311. expect(document.activeElement).toBe(json);
  312. expect(scrollIntoView).toHaveBeenCalledTimes(2);
  313. });
  314. it("校验通过时不滚动", () => {
  315. const { onSubmit } = mount(ENTRY);
  316. fireEvent.change(screen.getByTestId(testId(TID_WORKFLOW_LAUNCH_ARG, "branch")), {
  317. target: { value: "main" },
  318. });
  319. fireEvent.click(screen.getByTestId(TID_WORKFLOW_LAUNCH_SUBMIT));
  320. expect(onSubmit).toHaveBeenCalledTimes(1);
  321. expect(scrollIntoView).not.toHaveBeenCalled();
  322. });
  323. });