workflowAskSettings.test.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. // @vitest-environment jsdom
  2. // 确认窗里可调的两项 run 设置(docs/dynamic-workflow/launch.md「Adjusting the settings in the window」;
  3. // 外观与键盘见 docs/dynamic-workflow/presentation.md「The confirmation window」):纯规则逐条穷举,
  4. // 真实 DOM 上钉两句话、改过的那一行、选项描述、Allow 应答带出去的 content、「不可用」的门与 Shift+Tab。
  5. import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
  6. import { type ZCodePermissionRequest } from "@zcode/shared";
  7. import { TID_CHAT_THOUGHT_LEVEL_SELECT_TRIGGER } from "@zcode/shared/test-ids";
  8. import type { ConversationSnapshot } from "@zcode/shared/zcode-protocol-v4";
  9. import { createElement, type ReactNode } from "react";
  10. import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
  11. vi.mock("@/logger.js", () => ({
  12. logger: { debug: vi.fn(), warn: vi.fn(), info: vi.fn(), error: vi.fn() },
  13. }));
  14. vi.mock("@/components/ai-elements/code-block.js", () => ({
  15. CodeBlock: ({ code }: { code: string }) =>
  16. createElement("pre", { "data-testid": "code-block" }, code),
  17. }));
  18. vi.mock("@/ControlHintTooltip.js", () => ({
  19. ControlHintTooltip: ({ children }: { children?: ReactNode }) => children,
  20. }));
  21. vi.mock("@/hooks/useWorkflowSubagentModelProviderName.js", () => ({
  22. useWorkflowSubagentModelProviderName: () => undefined,
  23. }));
  24. /** 模型清单:一个 provider、两个模型,其中一个有思考档(与「配置」弹层的测试同一份)。 */
  25. const VIEW = {
  26. revision: 1,
  27. providers: [
  28. {
  29. providerId: "zhipu",
  30. providerName: "Zhipu",
  31. config: { api: { type: "openai-chat-completions" } },
  32. models: [
  33. {
  34. modelId: "glm-5.3",
  35. config: { optionSpecs: { reasoningLevel: { values: [], map: "{}" } } },
  36. },
  37. {
  38. modelId: "glm-5.3-flash",
  39. config: { optionSpecs: { reasoningLevel: { values: ["low", "high"], map: "{}" } } },
  40. },
  41. ],
  42. },
  43. ],
  44. };
  45. const modelRead = vi.hoisted(() => ({
  46. state: { status: "ready", view: null as unknown } as { status: string; view?: unknown },
  47. }));
  48. vi.mock("@/hooks/useModelSelectionView.js", () => ({
  49. useModelSelectionView: () => ({ state: modelRead.state, reload: vi.fn() }),
  50. }));
  51. // 模型菜单的选择走真实组件,只把 props 记下来:测试直接调它的 onValueChange,不去驱动 Radix 菜单。
  52. const capturedSelects = vi.hoisted(() => [] as Array<Record<string, unknown>>);
  53. vi.mock("@/ModelConfigSelect.js", async (importOriginal) => {
  54. const actual = await importOriginal<typeof import("@/ModelConfigSelect.js")>();
  55. const react = await import("react");
  56. return {
  57. ...actual,
  58. ModelConfigSelect: (props: Record<string, unknown>) => {
  59. capturedSelects.push(props);
  60. return react.createElement(
  61. actual.ModelConfigSelect as unknown as (p: Record<string, unknown>) => ReactNode,
  62. props,
  63. );
  64. },
  65. };
  66. });
  67. const sendCommand = vi.hoisted(() => vi.fn());
  68. vi.mock("@/v4/V4ConversationContext.js", () => ({
  69. useV4Conversation: () => ({ sendCommand }),
  70. }));
  71. // eslint-disable-next-line import/first -- 必须在 mock 之后再引入被测组件。
  72. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  73. // eslint-disable-next-line import/first
  74. import { PermissionDialog } from "@/PermissionDialog.js";
  75. // eslint-disable-next-line import/first
  76. import {
  77. isWorkflowSettingsCarrier,
  78. workflowAdjustedOptionDescriptionId,
  79. } from "@/permissionWorkflowSettings.js";
  80. // eslint-disable-next-line import/first
  81. import {
  82. initialWorkflowAskSettingsDraft,
  83. workflowAskSettingsChangedLines,
  84. workflowAskSettingsContent,
  85. } from "@/components/workflow-timeline/workflowAskSettings.js";
  86. // eslint-disable-next-line import/first
  87. import { encodeCustomModelValue } from "@/lib/zcodeCustomModelValue.js";
  88. // eslint-disable-next-line import/first
  89. import { V4InteractionDialogs } from "@/v4/V4InteractionDialogs.js";
  90. const SCRIPT = 'const r = await agent("a").ask<string>("do");';
  91. const ALLOW = { optionId: "allowOnce", kind: "allowOnce", name: "Allow" };
  92. const SESSION = {
  93. optionId: "allowSession",
  94. kind: "allowAlways",
  95. name: "Always allow in this session",
  96. };
  97. const DENY = { optionId: "deny", kind: "deny", name: "Deny" };
  98. beforeAll(() => {
  99. globalThis.ResizeObserver ??= class {
  100. observe() {}
  101. unobserve() {}
  102. disconnect() {}
  103. } as unknown as typeof ResizeObserver;
  104. });
  105. beforeEach(() => {
  106. modelRead.state = { status: "ready", view: VIEW };
  107. capturedSelects.length = 0;
  108. sendCommand.mockReset();
  109. });
  110. afterEach(() => {
  111. cleanup();
  112. });
  113. function workflowRequest(
  114. raw: Record<string, unknown>,
  115. overrides: Partial<ZCodePermissionRequest> = {},
  116. ): ZCodePermissionRequest {
  117. return {
  118. type: "permission_request",
  119. taskId: "task-1",
  120. traceId: "trace-1",
  121. requestId: "perm-1",
  122. description: "createWorkflow.runConfirmation",
  123. kind: "CreateWorkflow",
  124. title: "CreateWorkflow",
  125. options: [
  126. { ...ALLOW, response: { decision: "allow" } },
  127. { ...SESSION, response: { decision: "allow" } },
  128. { ...DENY, response: { decision: "deny" } },
  129. ],
  130. raw: { name: "Research", script: SCRIPT, ...raw },
  131. ...overrides,
  132. };
  133. }
  134. const ADJUSTABLE = { adjustable_settings: { subagent_model: true, concurrency_ceiling: 13 } };
  135. function renderDialog(
  136. request: ZCodePermissionRequest,
  137. options: { locale?: "en-US" | "zh-CN"; responding?: boolean } = {},
  138. ) {
  139. const onRespond = vi.fn();
  140. const element = (next: ZCodePermissionRequest) =>
  141. createElement(
  142. ZCodeIntlProvider,
  143. { initialLocale: options.locale ?? "en-US" },
  144. createElement(PermissionDialog, {
  145. request: next,
  146. onRespond,
  147. workspacePath: "/w",
  148. workflowSessionModel: { providerId: "zhipu", modelId: "glm-5.3" },
  149. ...(options.responding === undefined ? {} : { responding: options.responding }),
  150. }),
  151. );
  152. const view = render(element(request));
  153. return { onRespond, rerender: (next: ZCodePermissionRequest) => view.rerender(element(next)) };
  154. }
  155. function modelLine(): HTMLElement {
  156. return screen.getByTestId("workflow-permission-settings-model");
  157. }
  158. function boundLine(): HTMLElement {
  159. return screen.getByTestId("workflow-permission-settings-bound");
  160. }
  161. function boundValue(): HTMLInputElement {
  162. return screen.getByTestId<HTMLInputElement>("workflow-permission-settings-bound-value");
  163. }
  164. function option(name: string): HTMLElement {
  165. return screen.getByRole("option", { name });
  166. }
  167. function pickModel(value: string) {
  168. const onValueChange = capturedSelects.at(-1)?.onValueChange as (value: string) => void;
  169. act(() => onValueChange(value));
  170. }
  171. describe("纯规则:起点、应答 content、改过的行", () => {
  172. it("起点读入参:缺席的模型是会话模型,缺席的上界是默认并发,默认也未知即空", () => {
  173. expect(initialWorkflowAskSettingsDraft({}, 13)).toEqual({
  174. model: { kind: "session" },
  175. bound: 13,
  176. });
  177. expect(
  178. initialWorkflowAskSettingsDraft(
  179. { subagent_model: "zhipu/glm-5.3-flash$high", max_concurrency: 4 },
  180. 13,
  181. ),
  182. ).toEqual({
  183. model: { kind: "model", providerId: "zhipu", modelId: "glm-5.3-flash", level: "high" },
  184. bound: 4,
  185. });
  186. expect(initialWorkflowAskSettingsDraft({}, undefined)).toEqual({
  187. model: { kind: "session" },
  188. bound: null,
  189. });
  190. });
  191. it("只发改过的字段;上界回到默认发 null,换回会话模型发 null,只改思考档也算改了模型", () => {
  192. const initial = initialWorkflowAskSettingsDraft(
  193. { subagent_model: "zhipu/glm-5.3-flash$low", max_concurrency: 4 },
  194. 13,
  195. );
  196. expect(workflowAskSettingsContent(initial, initial, 13)).toBeUndefined();
  197. expect(workflowAskSettingsContent(initial, { ...initial, bound: 13 }, 13)).toEqual({
  198. max_concurrency: null,
  199. });
  200. // 默认是起点不是上限:高于它的数原样发出去。
  201. expect(workflowAskSettingsContent(initial, { ...initial, bound: 40 }, 13)).toEqual({
  202. max_concurrency: 40,
  203. });
  204. expect(
  205. workflowAskSettingsContent(initial, { ...initial, model: { kind: "session" } }, 13),
  206. ).toEqual({
  207. subagent_model: null,
  208. });
  209. const levelOnly = {
  210. ...initial,
  211. model: {
  212. kind: "model" as const,
  213. providerId: "zhipu",
  214. modelId: "glm-5.3-flash",
  215. level: "high",
  216. },
  217. };
  218. expect(workflowAskSettingsContent(initial, levelOnly, 13)).toEqual({
  219. subagent_model: "zhipu/glm-5.3-flash$high",
  220. });
  221. expect(workflowAskSettingsChangedLines(initial, levelOnly, 13)).toEqual({
  222. model: true,
  223. bound: false,
  224. });
  225. });
  226. it("只有放行的两项带设置;改过设置时换描述的也只有它们", () => {
  227. expect(isWorkflowSettingsCarrier({ ...ALLOW, response: { decision: "allow" } })).toBe(true);
  228. expect(isWorkflowSettingsCarrier({ ...SESSION, response: { decision: "allow" } })).toBe(true);
  229. expect(isWorkflowSettingsCarrier({ ...DENY, response: { decision: "deny" } })).toBe(false);
  230. expect(workflowAdjustedOptionDescriptionId("chat.permission.allowOnce.description")).toBe(
  231. "chat.permission.workflow.allowOnce.adjusted",
  232. );
  233. expect(
  234. workflowAdjustedOptionDescriptionId("chat.permission.workflow.allowForSession.description"),
  235. ).toBe("chat.permission.workflow.allowForSession.adjusted");
  236. expect(workflowAdjustedOptionDescriptionId("chat.permission.denyOnce.description")).toBe(
  237. "chat.permission.denyOnce.description",
  238. );
  239. });
  240. });
  241. describe("确认窗里的两句话", () => {
  242. it("没设过的调用也说出将会发生什么:模型在上(会话模型徽标),上界在下(= 默认),没有「原为」", () => {
  243. renderDialog(workflowRequest(ADJUSTABLE));
  244. const block = screen.getByTestId("workflow-permission-settings");
  245. expect(block.textContent?.indexOf("Subagents run on")).toBeLessThan(
  246. block.textContent?.indexOf("At most") ?? -1,
  247. );
  248. expect(modelLine().textContent).toContain("glm-5.3");
  249. expect(modelLine().textContent).toContain("session model");
  250. expect(boundValue().value).toBe("13");
  251. expect(boundLine().textContent).toContain("subagents at once");
  252. expect(boundLine().textContent).toContain("= default");
  253. // 默认不是天花板:停在默认上照样能往上加。
  254. expect(
  255. screen.getByTestId<HTMLButtonElement>("workflow-permission-settings-bound-increase").disabled,
  256. ).toBe(false);
  257. expect(screen.queryByTestId("workflow-permission-settings-model-was")).toBeNull();
  258. expect(screen.queryByTestId("workflow-permission-settings-bound-was")).toBeNull();
  259. // 旧的纯文本行不再出现。
  260. expect(screen.queryByTestId("workflow-permission-max-concurrency")).toBeNull();
  261. expect(option("Allow").textContent).toContain("Allow only this time");
  262. });
  263. it("改上界:那一行加粗并带「· was 13 ↺」,前两个选项换描述,Allow 带 content;↺ 放回去即不带", () => {
  264. const { onRespond } = renderDialog(workflowRequest(ADJUSTABLE));
  265. fireEvent.click(screen.getByTestId("workflow-permission-settings-bound-decrease"));
  266. fireEvent.click(screen.getByTestId("workflow-permission-settings-bound-decrease"));
  267. expect(boundValue().value).toBe("11");
  268. expect(boundLine().getAttribute("data-changed")).toBe("true");
  269. expect(boundValue().className).toContain("font-medium");
  270. expect(boundLine().textContent).toContain("default 13");
  271. expect(screen.getByTestId("workflow-permission-settings-bound-was").textContent).toBe(
  272. "· was 13",
  273. );
  274. expect(option("Allow").textContent).toContain("Run once with the adjusted settings");
  275. expect(option("Always allow in this session").textContent).toContain(
  276. "later runs are not asked and carry their own",
  277. );
  278. fireEvent.click(option("Allow"));
  279. expect(onRespond).toHaveBeenLastCalledWith(
  280. "perm-1",
  281. expect.objectContaining({ optionId: "allowOnce" }),
  282. undefined,
  283. { max_concurrency: 11 },
  284. );
  285. fireEvent.click(screen.getByTestId("workflow-permission-settings-bound-reset"));
  286. expect(boundValue().value).toBe("13");
  287. expect(screen.queryByTestId("workflow-permission-settings-bound-was")).toBeNull();
  288. expect(option("Allow").textContent).toContain("Allow only this time");
  289. fireEvent.click(option("Allow"));
  290. expect(onRespond.mock.lastCall).toEqual([
  291. "perm-1",
  292. expect.objectContaining({ optionId: "allowOnce" }),
  293. undefined,
  294. ]);
  295. });
  296. it("换模型:content 是规范串,「原为」说起点(会话模型);会话免确认同样带着这一次的设置", () => {
  297. const { onRespond } = renderDialog(workflowRequest(ADJUSTABLE));
  298. pickModel(encodeCustomModelValue("zhipu", "glm-5.3-flash"));
  299. expect(modelLine().getAttribute("data-changed")).toBe("true");
  300. expect(screen.getByTestId("workflow-permission-settings-model-was").textContent).toBe(
  301. "· was Zhipu/glm-5.3 · session model",
  302. );
  303. fireEvent.click(option("Always allow in this session"));
  304. fireEvent.click(option("Always allow in this session"));
  305. const content = onRespond.mock.lastCall?.[3] as { subagent_model?: string };
  306. expect(onRespond.mock.lastCall?.[1]).toMatchObject({ optionId: "allowSession" });
  307. expect(content.subagent_model?.startsWith("zhipu/glm-5.3-flash")).toBe(true);
  308. expect(Object.keys(content)).toEqual(["subagent_model"]);
  309. });
  310. it("所选模型有思考档:模型芯片旁边出现思考档芯片;没有档位的模型没有它", () => {
  311. renderDialog(workflowRequest(ADJUSTABLE));
  312. // 起点是会话模型 glm-5.3(夹具里没有档位)。
  313. expect(screen.queryByTestId(TID_CHAT_THOUGHT_LEVEL_SELECT_TRIGGER)).toBeNull();
  314. pickModel(encodeCustomModelValue("zhipu", "glm-5.3-flash"));
  315. expect(screen.getByTestId(TID_CHAT_THOUGHT_LEVEL_SELECT_TRIGGER)).toBeTruthy();
  316. });
  317. it("拒绝不带设置:改过之后点 Deny 也只是普通拒绝", () => {
  318. const { onRespond } = renderDialog(workflowRequest(ADJUSTABLE));
  319. fireEvent.click(screen.getByTestId("workflow-permission-settings-bound-decrease"));
  320. fireEvent.click(option("Deny"));
  321. fireEvent.click(option("Deny"));
  322. expect(onRespond.mock.lastCall).toEqual([
  323. "perm-1",
  324. expect.objectContaining({ optionId: "deny" }),
  325. undefined,
  326. ]);
  327. });
  328. it("入参里的模型已不在清单:「不可用」徽标,Allow 与确认键等用户换一个,Deny 照常", async () => {
  329. const { onRespond } = renderDialog(
  330. workflowRequest({ ...ADJUSTABLE, subagent_model: "gone/model-x" }),
  331. );
  332. expect(screen.getByTestId("workflow-permission-settings-model-unavailable").textContent).toBe(
  333. "unavailable",
  334. );
  335. await waitFor(() => expect(option("Allow").getAttribute("aria-disabled")).toBe("true"));
  336. fireEvent.click(option("Allow"));
  337. expect(onRespond).not.toHaveBeenCalled();
  338. expect(screen.getByRole<HTMLButtonElement>("button", { name: "Confirm" }).disabled).toBe(true);
  339. pickModel(encodeCustomModelValue("zhipu", "glm-5.3"));
  340. await waitFor(() => expect(option("Allow").getAttribute("aria-disabled")).toBeNull());
  341. fireEvent.click(option("Allow"));
  342. expect(onRespond.mock.lastCall?.[3]).toEqual({ subagent_model: "zhipu/glm-5.3" });
  343. });
  344. it("agent 没有模型目录:模型那一行是一句话,上界照样可调", () => {
  345. const { onRespond } = renderDialog(
  346. workflowRequest({ adjustable_settings: { subagent_model: false, concurrency_ceiling: 8 } }),
  347. );
  348. expect(modelLine().textContent).toBe(
  349. "Subagents stay on the session model· this agent has no model catalog",
  350. );
  351. expect(screen.queryByTestId("workflow-permission-settings-model-trigger")).toBeNull();
  352. fireEvent.click(screen.getByTestId("workflow-permission-settings-bound-decrease"));
  353. fireEvent.click(option("Allow"));
  354. expect(onRespond.mock.lastCall?.[3]).toEqual({ max_concurrency: 7 });
  355. });
  356. it("选不了模型、入参却带着一个(沿用来的):照实说它,不说「沿用会话模型」", () => {
  357. renderDialog(
  358. workflowRequest({
  359. adjustable_settings: { subagent_model: false, concurrency_ceiling: 8 },
  360. subagent_model: "zhipu/glm-5.3-flash",
  361. }),
  362. );
  363. // 夹具的 provider 不是内置家族:名字是「provider 名/模型 id」,与菜单同一条拼名规则。
  364. expect(modelLine().textContent).toBe("Subagents run on Zhipu/glm-5.3-flash");
  365. });
  366. it("天花板未知、调用也没设上界:步进器留空,句尾「不设上限」;敲一个数就成了调整", () => {
  367. const { onRespond } = renderDialog(
  368. workflowRequest({ adjustable_settings: { subagent_model: true } }),
  369. );
  370. expect(boundValue().value).toBe("");
  371. expect(screen.getByTestId("workflow-permission-settings-bound-tail").textContent).toBe(
  372. "· no limit of its own",
  373. );
  374. fireEvent.change(boundValue(), { target: { value: "5" } });
  375. expect(screen.queryByTestId("workflow-permission-settings-bound-tail")).toBeNull();
  376. fireEvent.click(option("Allow"));
  377. expect(onRespond.mock.lastCall?.[3]).toEqual({ max_concurrency: 5 });
  378. });
  379. it("应答在途:控件与选项一起禁用", () => {
  380. renderDialog(workflowRequest(ADJUSTABLE), { responding: true });
  381. expect(boundValue().disabled).toBe(true);
  382. expect(
  383. screen.getByTestId<HTMLButtonElement>("workflow-permission-settings-bound-decrease").disabled,
  384. ).toBe(true);
  385. expect(
  386. screen.getByTestId<HTMLButtonElement>("workflow-permission-settings-model-trigger").disabled,
  387. ).toBe(true);
  388. });
  389. it("zh-CN:两句与「原为」", () => {
  390. renderDialog(workflowRequest(ADJUSTABLE), { locale: "zh-CN" });
  391. expect(modelLine().textContent).toContain("子代理运行在");
  392. expect(boundLine().textContent).toContain("最多");
  393. expect(boundLine().textContent).toContain("个子代理同时运行");
  394. expect(boundLine().textContent).toContain("= 默认");
  395. fireEvent.click(screen.getByTestId("workflow-permission-settings-bound-decrease"));
  396. expect(boundLine().textContent).toContain("默认 13");
  397. expect(screen.getByTestId("workflow-permission-settings-bound-was").textContent).toBe(
  398. "· 原为 13",
  399. );
  400. expect(
  401. screen.getByTestId("workflow-permission-settings-bound-reset").getAttribute("title"),
  402. ).toBe("恢复原值");
  403. expect(option("允许").textContent).toContain("按调整后的设置运行一次");
  404. });
  405. it("换请求:设置回到新入参的起点,上一个窗的改动不带进下一个", () => {
  406. const { onRespond, rerender } = renderDialog(workflowRequest(ADJUSTABLE));
  407. fireEvent.click(screen.getByTestId("workflow-permission-settings-bound-decrease"));
  408. rerender(workflowRequest(ADJUSTABLE, { requestId: "perm-2" }));
  409. expect(boundValue().value).toBe("13");
  410. expect(option("Allow").textContent).toContain("Allow only this time");
  411. fireEvent.click(option("Allow"));
  412. expect(onRespond.mock.lastCall).toEqual([
  413. "perm-2",
  414. expect.objectContaining({ optionId: "allowOnce" }),
  415. undefined,
  416. ]);
  417. });
  418. });
  419. // 脚本给某些子代理点名了模型(docs/dynamic-workflow/launch.md「The window」):窗不逐名列出——那是
  420. // 作者模型的选择——只把模型那一句改口「默认」;应答里也永远没有改它们的字段。
  421. describe("脚本点名了模型", () => {
  422. const BINDINGS = {
  423. model_bindings: { "GLM-5.3-Flash": "zhipu/glm-5.3-flash$low", judge: "zhipu/glm-5.3" },
  424. };
  425. it("不逐名列出,模型那一句说「默认」;只有一个模型菜单", () => {
  426. renderDialog(workflowRequest({ ...ADJUSTABLE, ...BINDINGS }));
  427. const text = screen.getByTestId("workflow-permission-settings").textContent ?? "";
  428. expect(modelLine().textContent).toContain("By default, subagents run on");
  429. expect(text).not.toContain("judge");
  430. expect(text).not.toContain("glm-5.3-flash");
  431. expect(screen.getAllByTestId("workflow-permission-settings-model-trigger")).toHaveLength(1);
  432. });
  433. it("改设置后 Allow 只带那两项,不带 model_bindings", () => {
  434. const { onRespond } = renderDialog(workflowRequest({ ...ADJUSTABLE, ...BINDINGS }));
  435. fireEvent.click(screen.getByTestId("workflow-permission-settings-bound-decrease"));
  436. fireEvent.click(option("Allow"));
  437. expect(onRespond.mock.lastCall?.[3]).toEqual({ max_concurrency: 12 });
  438. });
  439. it("没点名模型的脚本照旧说「子代理运行在」", () => {
  440. renderDialog(workflowRequest({ ...ADJUSTABLE, model_bindings: {} }));
  441. expect(modelLine().textContent).toContain("Subagents run on");
  442. expect(modelLine().textContent).not.toContain("By default");
  443. });
  444. it("选不了模型:「子代理默认沿用会话模型」", () => {
  445. renderDialog(
  446. workflowRequest({
  447. adjustable_settings: { subagent_model: false, concurrency_ceiling: 8 },
  448. ...BINDINGS,
  449. }),
  450. );
  451. expect(modelLine().textContent).toBe(
  452. "By default, subagents stay on the session model· this agent has no model catalog",
  453. );
  454. });
  455. it("zh-CN:「子代理默认运行在」", () => {
  456. renderDialog(workflowRequest({ ...ADJUSTABLE, ...BINDINGS }), { locale: "zh-CN" });
  457. expect(modelLine().textContent).toContain("子代理默认运行在");
  458. });
  459. it("旧 agent(纯文本):同样不列名字,模型那一句说「默认」", () => {
  460. renderDialog(
  461. workflowRequest({ max_concurrency: 2, subagent_model: "zhipu/glm-5.3", ...BINDINGS }),
  462. );
  463. expect(screen.getByTestId("workflow-permission-subagent-model").textContent).toBe(
  464. "By default, subagents run on glm-5.3",
  465. );
  466. const blockText =
  467. document.querySelector('[data-workflow-permission-block="true"]')?.textContent ?? "";
  468. expect(blockText).not.toContain("judge");
  469. });
  470. });
  471. describe("键盘", () => {
  472. it("第一个选项上的 Shift+Tab 离开列表、落到设置块的最后一个控件;其余选项上照旧在列表里循环", async () => {
  473. renderDialog(workflowRequest(ADJUSTABLE));
  474. const allow = option("Allow");
  475. allow.focus();
  476. fireEvent.keyDown(allow, { key: "Tab", shiftKey: true });
  477. // 默认不是天花板:停在默认上加号照样可按,所以最后一个可聚焦的控件是加号。
  478. expect(document.activeElement).toBe(
  479. screen.getByTestId("workflow-permission-settings-bound-increase"),
  480. );
  481. const session = option("Always allow in this session");
  482. session.focus();
  483. fireEvent.keyDown(session, { key: "Tab", shiftKey: true });
  484. await waitFor(() => expect(document.activeElement).toBe(option("Allow")));
  485. });
  486. it("在数字框里敲的键留在数字框:选项的数字快捷键不生效", () => {
  487. const { onRespond } = renderDialog(workflowRequest(ADJUSTABLE));
  488. fireEvent.keyDown(boundValue(), { key: "1" });
  489. fireEvent.keyDown(boundValue(), { key: "Enter" });
  490. expect(onRespond).not.toHaveBeenCalled();
  491. });
  492. });
  493. describe("旧 agent(入参没有 adjustable_settings)", () => {
  494. it("只画调用设了的字段,纯文本,模型在上、上界在下;没有控件", () => {
  495. renderDialog(workflowRequest({ max_concurrency: 2, subagent_model: "zhipu/glm-5.3-flash" }));
  496. expect(screen.queryByTestId("workflow-permission-settings")).toBeNull();
  497. const blockText =
  498. document.querySelector('[data-workflow-permission-block="true"]')?.textContent ?? "";
  499. expect(blockText.indexOf("Subagents run on glm-5.3-flash")).toBeLessThan(
  500. blockText.indexOf("At most 2 subagents at once"),
  501. );
  502. });
  503. });
  504. describe("V4 宿主", () => {
  505. it("把改过的设置放进 resolveInteraction.answer.content", async () => {
  506. sendCommand.mockResolvedValue({ status: "accepted", commandId: "c", revisionAtDecision: 0 });
  507. const snapshot = {
  508. sessionId: "sess-1",
  509. config: { provider: "zhipu", model: "glm-5.3" },
  510. pendingInteractions: [
  511. {
  512. interactionId: "ask-1",
  513. kind: "permission",
  514. anchorRowId: 3,
  515. createdAt: 1,
  516. payload: {
  517. kind: "permission",
  518. toolName: "CreateWorkflow",
  519. toolCallId: "tool-1",
  520. summary: "createWorkflow.runConfirmation",
  521. options: [
  522. { optionId: "allowOnce", kind: "allowOnce", label: "Allow" },
  523. { optionId: "deny", kind: "deny", label: "Deny" },
  524. ],
  525. detail: { name: "Research", script: SCRIPT, ...ADJUSTABLE },
  526. },
  527. },
  528. ],
  529. } as unknown as ConversationSnapshot;
  530. render(
  531. createElement(
  532. ZCodeIntlProvider,
  533. { initialLocale: "en-US" },
  534. createElement(V4InteractionDialogs, {
  535. sessionId: "sess-1",
  536. workspacePath: "/w",
  537. snapshot,
  538. }),
  539. ),
  540. );
  541. fireEvent.click(screen.getByTestId("workflow-permission-settings-bound-decrease"));
  542. fireEvent.click(option("Allow"));
  543. await waitFor(() => expect(sendCommand).toHaveBeenCalled());
  544. const envelope = sendCommand.mock.calls
  545. .map(([sent]) => sent as { type: string; payload: { answer?: unknown } })
  546. .find((sent) => sent.type === "resolveInteraction");
  547. expect(envelope?.payload.answer).toEqual({
  548. optionId: "allowOnce",
  549. content: { max_concurrency: 12 },
  550. });
  551. });
  552. });