permissionDialog.test.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. import {
  2. OFFICIAL_CUA_PERMISSION_RULE_TOOL_NAME,
  3. type ZCodePermissionRequest,
  4. type ZCodeProvider,
  5. } from "@zcode/shared";
  6. import { createElement } from "react";
  7. import type { ComponentType } from "react";
  8. import { renderToStaticMarkup } from "react-dom/server";
  9. import { describe, expect, it } from "vitest";
  10. import { PermissionDialog } from "../src/PermissionDialog.js";
  11. import { ZCodeIntlProvider } from "../src/i18n/IntlProvider.js";
  12. const WORKSPACE_PATH = "/Users/dev/ZCodeProject/demo";
  13. const SUBAGENT_ORIGIN = {
  14. kind: "subagent" as const,
  15. agentId: "general-purpose",
  16. agentType: "general-purpose",
  17. childSessionId: "sess-child",
  18. parentSessionId: "sess-parent",
  19. parentToolCallId: "tool-parent-agent",
  20. };
  21. function renderPermissionDialog(
  22. request: ZCodePermissionRequest,
  23. provider?: ZCodeProvider,
  24. extraProps: Record<string, unknown> = {},
  25. ) {
  26. const Dialog = PermissionDialog as ComponentType<Record<string, unknown>>;
  27. return renderToStaticMarkup(
  28. createElement(
  29. ZCodeIntlProvider,
  30. { initialLocale: "zh-CN" },
  31. createElement(Dialog, {
  32. request,
  33. onRespond: () => {},
  34. workspacePath: WORKSPACE_PATH,
  35. provider,
  36. ...extraProps,
  37. }),
  38. ),
  39. );
  40. }
  41. describe("PermissionDialog", () => {
  42. it("renders the optional feedback input only when V4 enables it", () => {
  43. const withoutFeedback = renderPermissionDialog({
  44. type: "permission_request",
  45. taskId: "task-no-feedback",
  46. traceId: "trace-no-feedback",
  47. requestId: "request-no-feedback",
  48. description: "Run ls",
  49. kind: "Bash",
  50. options: [
  51. {
  52. optionId: "deny",
  53. kind: "deny",
  54. name: "Deny",
  55. response: { decision: "deny" },
  56. },
  57. ],
  58. raw: { command: "ls" },
  59. });
  60. const withFeedback = renderPermissionDialog({
  61. type: "permission_request",
  62. taskId: "task-feedback",
  63. traceId: "trace-feedback",
  64. requestId: "request-feedback",
  65. description: "Run ls",
  66. kind: "Bash",
  67. freeText: true,
  68. options: [
  69. {
  70. optionId: "deny",
  71. kind: "deny",
  72. name: "Deny",
  73. response: { decision: "deny" },
  74. },
  75. ],
  76. raw: { command: "ls" },
  77. });
  78. expect(withoutFeedback).not.toContain("chat.permission.feedback.placeholder");
  79. expect(withFeedback).toContain("告诉模型接下来应该怎么做...");
  80. expect(withFeedback).toContain("<textarea");
  81. expect(withFeedback).toContain('wrap="soft"');
  82. expect(withFeedback).toContain("field-sizing-content");
  83. expect(withFeedback).toContain("max-h-[5lh]");
  84. expect(withFeedback).toContain("overflow-y-auto");
  85. expect(withFeedback).toContain("focus-visible:ring-0");
  86. expect(withFeedback).toContain('maxLength="4096"');
  87. });
  88. it("labels an authority-verified Computer Use project grant explicitly", () => {
  89. const html = renderPermissionDialog({
  90. type: "permission_request",
  91. taskId: "task-cua",
  92. traceId: "trace-cua",
  93. requestId: "request-cua",
  94. description: "Control TextEdit",
  95. kind: "mcp__computer-use__left_click",
  96. title: "Computer Use",
  97. options: [
  98. {
  99. optionId: "allow_project",
  100. kind: "allow_always",
  101. name: "Always allow Computer Use in this project",
  102. response: {
  103. decision: "allow",
  104. permissionUpdates: [
  105. {
  106. behavior: "allow",
  107. rules: [{ toolName: OFFICIAL_CUA_PERMISSION_RULE_TOOL_NAME }],
  108. type: "addRules",
  109. },
  110. ],
  111. },
  112. },
  113. ],
  114. raw: { rawInput: { app_ref: { bundle_id: "com.apple.TextEdit" } } },
  115. });
  116. expect(html).toContain("始终允许本项目中的电脑控制");
  117. expect(html).toContain('aria-label="始终允许本项目中的电脑控制"');
  118. expect(html).toContain('aria-label="确认"');
  119. expect(html).toContain("本项目后续官方电脑控制操作不再询问");
  120. expect(html).not.toContain("后续相同权限请求不再询问");
  121. });
  122. it("renders compound prefix scopes inline without an exact-command card", () => {
  123. const html = renderPermissionDialog({
  124. type: "permission_request",
  125. taskId: "task-1",
  126. traceId: "trace-1",
  127. requestId: "request-scopes",
  128. description: "Run commands",
  129. kind: "Bash",
  130. title: "Bash",
  131. options: [
  132. {
  133. optionId: "allow_project",
  134. kind: "allow_always",
  135. name: "Always allow in this project",
  136. response: {
  137. decision: "allow",
  138. permissionUpdates: [
  139. {
  140. behavior: "allow",
  141. rules: [
  142. { ruleContent: "pnpm run lint:*", toolName: "Bash" },
  143. { ruleContent: "rm -rf ./generated", toolName: "Bash" },
  144. ],
  145. type: "addRules",
  146. },
  147. ],
  148. },
  149. },
  150. ],
  151. raw: {
  152. rawInput: { command: "pnpm run lint --fix && rm -rf ./generated" },
  153. },
  154. });
  155. expect(html).toContain('data-permission-rule-scopes="true"');
  156. expect(html).toContain('data-permission-rule-prefixes="true"');
  157. expect(html).toContain('data-permission-option-kind="allowAlways"');
  158. expect(html).toContain('data-permission-rule-scope="prefix"');
  159. expect(html).not.toContain('data-permission-rule-scope="exact"');
  160. expect(html).not.toContain("命令前缀");
  161. expect(html).not.toContain("后续相同命令不再询问");
  162. expect(html).toContain("pnpm run lint …");
  163. expect(html).not.toContain("仅此命令");
  164. expect(html).toContain("rm -rf ./generated");
  165. expect(html).not.toContain("<input");
  166. });
  167. it("does not render exact permission rules inside the allow-always option", () => {
  168. const heredocBody = `HEREDOC_BODY_MUST_NOT_RENDER_${"x".repeat(1000)}`;
  169. const oversizedTail = `${"y".repeat(300)}OVERSIZED_TAIL_MUST_NOT_RENDER`;
  170. const html = renderPermissionDialog({
  171. type: "permission_request",
  172. taskId: "task-1",
  173. traceId: "trace-1",
  174. requestId: "request-long-exact",
  175. description: "Write generated files",
  176. kind: "Bash",
  177. title: "Bash",
  178. options: [
  179. {
  180. optionId: "allow_project",
  181. kind: "allow_always",
  182. name: "Always allow in this project",
  183. response: {
  184. decision: "allow",
  185. permissionUpdates: [
  186. {
  187. behavior: "allow",
  188. rules: [
  189. {
  190. ruleContent: `cat > /tmp/sun.html << 'EOF'\n${heredocBody}\nEOF`,
  191. toolName: "Bash",
  192. },
  193. {
  194. ruleContent: `python -c '${oversizedTail}'`,
  195. toolName: "Bash",
  196. },
  197. ],
  198. type: "addRules",
  199. },
  200. ],
  201. },
  202. },
  203. ],
  204. raw: { rawInput: { command: "write generated files" } },
  205. });
  206. expect(html).not.toContain('data-permission-rule-scopes="true"');
  207. expect(html).not.toContain('data-permission-rule-scope="exact"');
  208. expect(html).not.toContain("cat &gt; /tmp/sun.html");
  209. expect(html).not.toContain("HEREDOC_BODY_MUST_NOT_RENDER");
  210. expect(html).not.toContain("OVERSIZED_TAIL_MUST_NOT_RENDER");
  211. expect(html).not.toContain("仅此命令");
  212. expect(html).toContain("后续相同命令不再询问");
  213. });
  214. it("wraps a long prefix in the allow-always description position", () => {
  215. const html = renderPermissionDialog({
  216. type: "permission_request",
  217. taskId: "task-1",
  218. traceId: "trace-1",
  219. requestId: "request-long-prefix",
  220. description: "Run command",
  221. kind: "Bash",
  222. title: "Bash",
  223. options: [
  224. {
  225. optionId: "allow_project",
  226. kind: "allow_always",
  227. name: "Always allow in this project",
  228. response: {
  229. decision: "allow",
  230. permissionUpdates: [
  231. {
  232. behavior: "allow",
  233. rules: [
  234. {
  235. ruleContent: `pnpm run ${"unbroken-prefix-".repeat(20)}:*`,
  236. toolName: "Bash",
  237. },
  238. ],
  239. type: "addRules",
  240. },
  241. ],
  242. },
  243. },
  244. ],
  245. raw: { rawInput: { command: "run long command" } },
  246. });
  247. expect(html).toContain('data-permission-rule-prefixes="true"');
  248. expect(html).toContain('data-permission-rule-scopes="true"');
  249. expect(html).toContain("basis-48");
  250. expect(html).toContain("whitespace-pre-wrap");
  251. expect(html).toContain("break-all");
  252. expect(html).toContain('data-permission-rule-scope-truncated="true"');
  253. expect(html).not.toContain("后续相同命令不再询问");
  254. });
  255. it("shows a subagent source tag for delegated permission requests only", () => {
  256. const baseRequest: ZCodePermissionRequest = {
  257. type: "permission_request",
  258. taskId: "task-1",
  259. traceId: "trace-1",
  260. requestId: "request-1",
  261. description: "Run command",
  262. kind: "Bash",
  263. title: "Bash",
  264. options: [
  265. {
  266. optionId: "allow",
  267. kind: "allow",
  268. name: "Allow",
  269. },
  270. ],
  271. raw: {
  272. toolName: "Bash",
  273. },
  274. };
  275. const mainHtml = renderPermissionDialog(baseRequest);
  276. const subagentHtml = renderPermissionDialog({
  277. ...baseRequest,
  278. origin: SUBAGENT_ORIGIN,
  279. });
  280. expect(mainHtml).not.toContain('data-interaction-origin-badge="subagent"');
  281. expect(subagentHtml).toContain('data-interaction-origin-badge="subagent"');
  282. expect(subagentHtml).toContain("子智能体");
  283. expect(subagentHtml).toContain("来自子智能体:general-purpose");
  284. });
  285. it("names the subagent in the source tag when the origin carries a description", () => {
  286. const html = renderPermissionDialog({
  287. type: "permission_request",
  288. taskId: "task-1",
  289. traceId: "trace-1",
  290. requestId: "request-1",
  291. description: "Run command",
  292. kind: "Bash",
  293. title: "Bash",
  294. options: [{ optionId: "allow", kind: "allow", name: "Allow" }],
  295. raw: { toolName: "Bash" },
  296. // 动态工作流子代理的 origin(docs/dynamic-workflow/launch.md「Permissions inside a run」)。
  297. origin: {
  298. ...SUBAGENT_ORIGIN,
  299. agentType: "zcode-workflow",
  300. description: "reviewer (agent#1@0)",
  301. },
  302. });
  303. expect(html).toContain('data-interaction-origin-badge="subagent"');
  304. expect(html).toContain("子智能体 · reviewer (agent#1@0)");
  305. // 截断时完整文本留在 tooltip 里,且仍带 agentType。
  306. expect(html).toContain("来自子智能体:zcode-workflow · reviewer (agent#1@0)");
  307. });
  308. it("renders a compact implementation plan placeholder for switch mode requests", () => {
  309. const html = renderPermissionDialog({
  310. type: "permission_request",
  311. taskId: "task-1",
  312. traceId: "trace-1",
  313. requestId: "request-1",
  314. description: "Ready to code?",
  315. kind: "switch_mode",
  316. title: "Ready to code?",
  317. options: [
  318. {
  319. optionId: "code",
  320. kind: "allow_always",
  321. name: "是,并自动接受后续操作",
  322. },
  323. ],
  324. raw: {},
  325. });
  326. expect(html).toContain("实施计划");
  327. expect(html).not.toContain("ToolCall");
  328. expect(html).not.toContain("Ready to code?");
  329. });
  330. it("renders a compact implementation plan placeholder for ExitPlanMode requests", () => {
  331. const html = renderPermissionDialog({
  332. type: "permission_request",
  333. taskId: "task-1",
  334. traceId: "trace-1",
  335. requestId: "request-1",
  336. description: "ExitPlanMode changes session mode after user plan approval",
  337. kind: "ExitPlanMode",
  338. title: "ExitPlanMode",
  339. options: [
  340. {
  341. optionId: "allow",
  342. kind: "allow",
  343. name: "Allow",
  344. },
  345. {
  346. optionId: "deny",
  347. kind: "deny",
  348. name: "Deny",
  349. },
  350. ],
  351. raw: {
  352. toolName: "ExitPlanMode",
  353. },
  354. });
  355. expect(html).toContain("实施计划");
  356. expect(html).not.toContain("ToolCall");
  357. expect(html).not.toContain("ExitPlanMode changes session mode");
  358. });
  359. it("prefers custom ZCode Agent option names over generic kind labels", () => {
  360. const html = renderPermissionDialog({
  361. type: "permission_request",
  362. taskId: "task-1",
  363. traceId: "trace-1",
  364. requestId: "request-1",
  365. description: "Ready to code?",
  366. kind: "switch_mode",
  367. title: "Ready to code?",
  368. options: [
  369. {
  370. optionId: "code",
  371. kind: "allow_always",
  372. name: "是,并自动接受后续操作",
  373. },
  374. {
  375. optionId: "ask",
  376. kind: "allow_always",
  377. name: "是,但高风险操作仍逐次确认",
  378. },
  379. {
  380. optionId: "reject",
  381. kind: "reject_once",
  382. name: "否,先继续整理计划",
  383. },
  384. ],
  385. raw: {},
  386. });
  387. expect(html).toContain("是,并自动接受后续操作");
  388. expect(html).toContain("是,但高风险操作仍逐次确认");
  389. expect(html).toContain("否,先继续整理计划");
  390. expect(html).not.toContain("始终允许");
  391. expect(html).not.toContain("拒绝");
  392. });
  393. it("localizes GLM project-scoped generic permission options", () => {
  394. const html = renderPermissionDialog(
  395. {
  396. type: "permission_request",
  397. taskId: "task-1",
  398. traceId: "trace-1",
  399. requestId: "request-1",
  400. description: "Run shell command",
  401. kind: "execute",
  402. title: "Run shell command",
  403. options: [
  404. {
  405. optionId: "allow_project",
  406. kind: "allow_always",
  407. name: "Always allow in this project",
  408. },
  409. ],
  410. raw: {
  411. rawInput: {
  412. command: "printf '102384' >> test.md",
  413. },
  414. },
  415. },
  416. "glm",
  417. );
  418. expect(html).toContain("始终允许此命令");
  419. expect(html).toContain("后续相同命令不再询问");
  420. expect(html).not.toContain("Always allow in this project");
  421. });
  422. it("keeps non-Codex session-scoped permission option names provider-native", () => {
  423. const html = renderPermissionDialog(
  424. {
  425. type: "permission_request",
  426. taskId: "task-1",
  427. traceId: "trace-1",
  428. requestId: "request-1",
  429. description: "Run shell command",
  430. kind: "execute",
  431. title: "Run shell command",
  432. options: [
  433. {
  434. optionId: "allow_for_session",
  435. kind: "allow_always",
  436. name: "Allow for Session",
  437. },
  438. ],
  439. raw: {
  440. rawInput: {
  441. command: "printf '102384' >> test.md",
  442. },
  443. },
  444. },
  445. "claude",
  446. );
  447. expect(html).toContain("Allow for Session");
  448. expect(html).not.toContain("允许本会话");
  449. });
  450. it("renders file change previews with file display for add operations", () => {
  451. const html = renderPermissionDialog({
  452. type: "permission_request",
  453. taskId: "task-1",
  454. traceId: "trace-1",
  455. requestId: "request-1",
  456. description: "Apply patch",
  457. kind: "apply_patch",
  458. title: "Apply patch",
  459. options: [
  460. {
  461. optionId: "allow",
  462. kind: "allow_once",
  463. name: "允许",
  464. },
  465. ],
  466. raw: {
  467. changes: {
  468. "/Users/dev/ZCodeProject/demo/README.md": {
  469. type: "add",
  470. content: "\n",
  471. },
  472. },
  473. },
  474. });
  475. expect(html).toContain("README.md");
  476. expect(html).toContain("等待确认");
  477. expect(html).not.toContain("/Users/dev/ZCodeProject/demo/");
  478. });
  479. it("renders ZCode protocol Write input as an existing file preview", () => {
  480. const html = renderPermissionDialog({
  481. type: "permission_request",
  482. taskId: "task-1",
  483. traceId: "trace-1",
  484. requestId: "request-1",
  485. description: "Tool has side effects and requires approval",
  486. kind: "Write",
  487. title: "Write",
  488. options: [
  489. {
  490. optionId: "allow",
  491. kind: "allow_once",
  492. name: "允许",
  493. },
  494. ],
  495. raw: {
  496. requestId: "request-1",
  497. toolName: "Write",
  498. input: {
  499. file_path: "/Users/dev/ZCodeProject/demo/quicksort.js",
  500. content: "export function quicksort(items) {\n return items;\n}\n",
  501. },
  502. },
  503. });
  504. expect(html).toContain("quicksort.js");
  505. expect(html).toContain("等待确认");
  506. expect(html).toContain("+3");
  507. expect(html).not.toContain("toolName");
  508. expect(html).not.toContain("file_path");
  509. });
  510. it("renders ZCode protocol Edit input as an existing file preview", () => {
  511. const html = renderPermissionDialog({
  512. type: "permission_request",
  513. taskId: "task-1",
  514. traceId: "trace-1",
  515. requestId: "request-1",
  516. description: "Tool has side effects and requires approval",
  517. kind: "Edit",
  518. title: "Edit",
  519. options: [
  520. {
  521. optionId: "allow",
  522. kind: "allow_once",
  523. name: "允许",
  524. },
  525. ],
  526. raw: {
  527. requestId: "request-1",
  528. toolName: "Edit",
  529. input: {
  530. file_path: "/Users/dev/ZCodeProject/demo/quicksort.js",
  531. old_string: "平均时间复杂度 O(n log n)",
  532. new_string: "平均时间复杂度 O(n log n),最坏 O(n^2)",
  533. },
  534. },
  535. });
  536. expect(html).toContain("quicksort.js");
  537. expect(html).toContain("等待确认");
  538. expect(html).toContain("+1");
  539. expect(html).toContain("-1");
  540. expect(html).not.toContain("old_string");
  541. expect(html).not.toContain("new_string");
  542. });
  543. it("renders grouped file changes for multiple updates", () => {
  544. const html = renderPermissionDialog({
  545. type: "permission_request",
  546. taskId: "task-1",
  547. traceId: "trace-1",
  548. requestId: "request-1",
  549. description: "Apply patch",
  550. kind: "apply_patch",
  551. title: "Apply patch",
  552. options: [
  553. {
  554. optionId: "allow",
  555. kind: "allow_once",
  556. name: "允许",
  557. },
  558. ],
  559. raw: {
  560. changes: {
  561. "/Users/dev/ZCodeProject/demo/index.html": {
  562. type: "update",
  563. content: "<html></html>",
  564. },
  565. "/Users/dev/ZCodeProject/demo/styles.css": {
  566. type: "update",
  567. content: "body {}",
  568. },
  569. "/Users/dev/ZCodeProject/demo/script.js": {
  570. type: "update",
  571. content: "console.log('ok')",
  572. },
  573. },
  574. },
  575. });
  576. expect(html).toContain("等待确认");
  577. expect(html).toContain("index.html");
  578. expect(html).toContain("styles.css");
  579. expect(html).toContain("script.js");
  580. expect(html).not.toContain("/Users/dev/ZCodeProject/demo/");
  581. });
  582. it("不再承载聊天底部主列宽度和 summary 偏移", () => {
  583. const html = renderPermissionDialog({
  584. type: "permission_request",
  585. taskId: "task-1",
  586. traceId: "trace-1",
  587. requestId: "request-1",
  588. description: "Run shell command",
  589. kind: "execute",
  590. title: "Run shell command",
  591. options: [
  592. {
  593. optionId: "allow",
  594. kind: "allow_once",
  595. name: "允许",
  596. },
  597. ],
  598. raw: {
  599. rawInput: {
  600. command: "printf ok",
  601. },
  602. },
  603. });
  604. expect(html).toContain("Run shell command");
  605. expect(html).toContain("等待确认");
  606. expect(html).not.toContain("执行中");
  607. expect(html).not.toContain("translate-x");
  608. expect(html).not.toContain("max-w-3xl");
  609. expect(html).not.toContain("max-w-2xl");
  610. });
  611. it("renders Bash input description as the permission reason instead of a running state", () => {
  612. const html = renderPermissionDialog({
  613. type: "permission_request",
  614. taskId: "task-1",
  615. traceId: "trace-1",
  616. requestId: "request-bash-description",
  617. description: "High risk tools require explicit approval",
  618. kind: "Bash",
  619. title: "Bash",
  620. options: [
  621. {
  622. optionId: "allow",
  623. kind: "allow_once",
  624. name: "允许",
  625. },
  626. ],
  627. raw: {
  628. requestId: "request-bash-description",
  629. reason: "High risk tools require explicit approval",
  630. toolName: "Bash",
  631. input: {
  632. description: "Check package.json scripts",
  633. command: 'cat /Users/dev/test/z-m/package.json 2>/dev/null || echo "No package.json"',
  634. },
  635. },
  636. });
  637. expect(html).toContain("Check package.json scripts");
  638. expect(html).toContain("等待确认");
  639. expect(html).toContain("cat /Users/dev/test/z-m/package.json");
  640. expect(html).not.toContain("High risk tools require explicit approval");
  641. expect(html).not.toContain("执行中");
  642. });
  643. it("hides the generic high-risk policy reason when no tool description exists", () => {
  644. const html = renderPermissionDialog({
  645. type: "permission_request",
  646. taskId: "task-1",
  647. traceId: "trace-1",
  648. requestId: "request-high-risk-policy-reason",
  649. description: "High risk tools require explicit approval",
  650. kind: "Bash",
  651. title: "Bash",
  652. options: [
  653. {
  654. optionId: "allow",
  655. kind: "allow_once",
  656. name: "允许",
  657. },
  658. ],
  659. raw: {
  660. requestId: "request-high-risk-policy-reason",
  661. reason: "High risk tools require explicit approval",
  662. riskLevel: "high",
  663. toolName: "Bash",
  664. input: {
  665. command: "rm -rf ./generated",
  666. },
  667. },
  668. });
  669. expect(html).toContain("等待确认");
  670. expect(html).toContain("rm -rf ./generated");
  671. expect(html).not.toContain("High risk tools require explicit approval");
  672. });
  673. it("renders WebFetch permission requests as a URL summary instead of fallback JSON", () => {
  674. const url =
  675. "https://www.zhihu.com/search?q=%E6%99%BA%E8%B0%B1AI%20Agent%20%E9%9D%A2%E8%AF%95%E9%A2%98";
  676. const html = renderPermissionDialog({
  677. type: "permission_request",
  678. taskId: "task-1",
  679. traceId: "trace-1",
  680. requestId: "perm-webfetch",
  681. description: "Tool has side effects and requires approval",
  682. kind: "WebFetch",
  683. title: "WebFetch",
  684. options: [
  685. {
  686. optionId: "allow",
  687. kind: "allow_once",
  688. name: "允许",
  689. },
  690. {
  691. optionId: "deny",
  692. kind: "deny",
  693. name: "拒绝",
  694. },
  695. ],
  696. raw: {
  697. requestId: "perm-webfetch",
  698. toolName: "WebFetch",
  699. input: {
  700. url,
  701. prompt: "List all AI Agent / LLM application development interview questions mentioned",
  702. },
  703. },
  704. });
  705. expect(html).toContain(url);
  706. expect(html).toContain("等待确认");
  707. expect(html).not.toContain("Tool has side effects and requires approval");
  708. expect(html).not.toContain("List all AI Agent");
  709. expect(html).not.toContain("&quot;toolId&quot;");
  710. expect(html.match(/WebFetch/g)?.length ?? 0).toBeLessThanOrEqual(1);
  711. });
  712. it("renders MCP permission requests without the generic policy reason", () => {
  713. const toolName = "mcp__ios-simulator__ios_preflight";
  714. const reason = "Tool has side effects and requires approval";
  715. const html = renderPermissionDialog({
  716. type: "permission_request",
  717. taskId: "task-1",
  718. traceId: "trace-1",
  719. requestId: "perm-mcp",
  720. description: reason,
  721. kind: toolName,
  722. title: toolName,
  723. options: [
  724. {
  725. optionId: "allow",
  726. kind: "allow_once",
  727. name: "允许",
  728. },
  729. {
  730. optionId: "deny",
  731. kind: "deny",
  732. name: "拒绝",
  733. },
  734. ],
  735. raw: {
  736. requestId: "perm-mcp",
  737. toolCallId: "call-mcp",
  738. toolName,
  739. reason,
  740. riskLevel: "medium",
  741. input: {},
  742. },
  743. });
  744. expect(html.match(new RegExp(toolName, "g"))?.length ?? 0).toBe(1);
  745. expect(html).not.toContain(reason);
  746. expect(html).not.toContain("&quot;reason&quot;");
  747. expect(html).not.toContain("&quot;toolCallId&quot;");
  748. });
  749. it("keeps a specific MCP permission reason visible", () => {
  750. const toolName = "mcp__ios-simulator__ios_preflight";
  751. const reason = "The simulator must be booted before installing the app";
  752. const html = renderPermissionDialog({
  753. type: "permission_request",
  754. taskId: "task-1",
  755. traceId: "trace-1",
  756. requestId: "perm-mcp-specific-reason",
  757. description: reason,
  758. kind: toolName,
  759. title: toolName,
  760. options: [
  761. {
  762. optionId: "allow",
  763. kind: "allow_once",
  764. name: "允许",
  765. },
  766. {
  767. optionId: "deny",
  768. kind: "deny",
  769. name: "拒绝",
  770. },
  771. ],
  772. raw: {
  773. requestId: "perm-mcp-specific-reason",
  774. toolCallId: "call-mcp-specific-reason",
  775. toolName,
  776. reason,
  777. riskLevel: "medium",
  778. input: {},
  779. },
  780. });
  781. expect(html).toContain(reason);
  782. });
  783. it("keeps CUA macOS permission actions out of tool approval dialogs", () => {
  784. const toolName = "mcp__zcode-cua__request_access";
  785. const html = renderPermissionDialog(
  786. {
  787. type: "permission_request",
  788. taskId: "task-1",
  789. traceId: "trace-1",
  790. requestId: "perm-cua",
  791. description: "Tool has side effects and requires approval",
  792. kind: toolName,
  793. title: toolName,
  794. options: [
  795. {
  796. optionId: "allow",
  797. kind: "allow_once",
  798. name: "允许",
  799. },
  800. {
  801. optionId: "deny",
  802. kind: "deny",
  803. name: "拒绝",
  804. },
  805. ],
  806. raw: {
  807. requestId: "perm-cua",
  808. toolName,
  809. input: {
  810. permissions: ["accessibility"],
  811. },
  812. },
  813. },
  814. undefined,
  815. );
  816. expect(html).not.toContain("打开辅助功能设置");
  817. expect(html).not.toContain("ZCode Computer Use.app");
  818. expect(html).toContain("允许");
  819. });
  820. it("does not offer the Computer Use Helper Accessibility action for unrelated MCP permission requests", () => {
  821. const toolName = "mcp__ios-simulator__ios_preflight";
  822. const html = renderPermissionDialog(
  823. {
  824. type: "permission_request",
  825. taskId: "task-1",
  826. traceId: "trace-1",
  827. requestId: "perm-mcp",
  828. description: "Tool has side effects and requires approval",
  829. kind: toolName,
  830. title: toolName,
  831. options: [
  832. {
  833. optionId: "allow",
  834. kind: "allow_once",
  835. name: "允许",
  836. },
  837. {
  838. optionId: "deny",
  839. kind: "deny",
  840. name: "拒绝",
  841. },
  842. ],
  843. raw: {
  844. requestId: "perm-mcp",
  845. toolName,
  846. input: {},
  847. },
  848. },
  849. undefined,
  850. );
  851. expect(html).not.toContain("打开辅助功能设置");
  852. expect(html).not.toContain("ZCode Computer Use.app");
  853. });
  854. it("does not offer the Computer Use Helper Accessibility action for ordinary zcode-cua tool requests", () => {
  855. const toolName = "mcp__zcode-cua__left_click";
  856. const html = renderPermissionDialog(
  857. {
  858. type: "permission_request",
  859. taskId: "task-1",
  860. traceId: "trace-1",
  861. requestId: "perm-cua-click",
  862. description: "Tool has side effects and requires approval",
  863. kind: toolName,
  864. title: toolName,
  865. options: [
  866. {
  867. optionId: "allow",
  868. kind: "allow_once",
  869. name: "允许",
  870. },
  871. {
  872. optionId: "deny",
  873. kind: "deny",
  874. name: "拒绝",
  875. },
  876. ],
  877. raw: {
  878. requestId: "perm-cua-click",
  879. toolName,
  880. input: {
  881. target: { type: "coordinate", x: 10, y: 10, space: "screen" },
  882. },
  883. },
  884. },
  885. undefined,
  886. );
  887. expect(html).not.toContain("打开辅助功能设置");
  888. expect(html).not.toContain("ZCode Computer Use.app");
  889. });
  890. });