workflowAskSettings.test.ts 25 KB

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