permissionDialog.test.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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('maxLength="4096"');
  83. });
  84. it("labels an authority-verified Computer Use project grant explicitly", () => {
  85. const html = renderPermissionDialog({
  86. type: "permission_request",
  87. taskId: "task-cua",
  88. traceId: "trace-cua",
  89. requestId: "request-cua",
  90. description: "Control TextEdit",
  91. kind: "mcp__computer-use__left_click",
  92. title: "Computer Use",
  93. options: [
  94. {
  95. optionId: "allow_project",
  96. kind: "allow_always",
  97. name: "Always allow Computer Use in this project",
  98. response: {
  99. decision: "allow",
  100. permissionUpdates: [
  101. {
  102. behavior: "allow",
  103. rules: [{ toolName: OFFICIAL_CUA_PERMISSION_RULE_TOOL_NAME }],
  104. type: "addRules",
  105. },
  106. ],
  107. },
  108. },
  109. ],
  110. raw: { rawInput: { app_ref: { bundle_id: "com.apple.TextEdit" } } },
  111. });
  112. expect(html).toContain("始终允许本项目中的电脑控制");
  113. expect(html).toContain('aria-label="始终允许本项目中的电脑控制"');
  114. expect(html).toContain('aria-label="确认"');
  115. expect(html).toContain("本项目后续官方电脑控制操作不再询问");
  116. expect(html).not.toContain("后续相同权限请求不再询问");
  117. });
  118. it("renders compound prefix scopes inline without an exact-command card", () => {
  119. const html = renderPermissionDialog({
  120. type: "permission_request",
  121. taskId: "task-1",
  122. traceId: "trace-1",
  123. requestId: "request-scopes",
  124. description: "Run commands",
  125. kind: "Bash",
  126. title: "Bash",
  127. options: [
  128. {
  129. optionId: "allow_project",
  130. kind: "allow_always",
  131. name: "Always allow in this project",
  132. response: {
  133. decision: "allow",
  134. permissionUpdates: [
  135. {
  136. behavior: "allow",
  137. rules: [
  138. { ruleContent: "pnpm run lint:*", toolName: "Bash" },
  139. { ruleContent: "rm -rf ./generated", toolName: "Bash" },
  140. ],
  141. type: "addRules",
  142. },
  143. ],
  144. },
  145. },
  146. ],
  147. raw: {
  148. rawInput: { command: "pnpm run lint --fix && rm -rf ./generated" },
  149. },
  150. });
  151. expect(html).toContain('data-permission-rule-scopes="true"');
  152. expect(html).toContain('data-permission-rule-prefixes="true"');
  153. expect(html).toContain('data-permission-option-kind="allowAlways"');
  154. expect(html).toContain('data-permission-rule-scope="prefix"');
  155. expect(html).not.toContain('data-permission-rule-scope="exact"');
  156. expect(html).not.toContain("命令前缀");
  157. expect(html).not.toContain("后续相同命令不再询问");
  158. expect(html).toContain("pnpm run lint …");
  159. expect(html).not.toContain("仅此命令");
  160. expect(html).toContain("rm -rf ./generated");
  161. expect(html).not.toContain("<input");
  162. });
  163. it("does not render exact permission rules inside the allow-always option", () => {
  164. const heredocBody = `HEREDOC_BODY_MUST_NOT_RENDER_${"x".repeat(1000)}`;
  165. const oversizedTail = `${"y".repeat(300)}OVERSIZED_TAIL_MUST_NOT_RENDER`;
  166. const html = renderPermissionDialog({
  167. type: "permission_request",
  168. taskId: "task-1",
  169. traceId: "trace-1",
  170. requestId: "request-long-exact",
  171. description: "Write generated files",
  172. kind: "Bash",
  173. title: "Bash",
  174. options: [
  175. {
  176. optionId: "allow_project",
  177. kind: "allow_always",
  178. name: "Always allow in this project",
  179. response: {
  180. decision: "allow",
  181. permissionUpdates: [
  182. {
  183. behavior: "allow",
  184. rules: [
  185. {
  186. ruleContent: `cat > /tmp/sun.html << 'EOF'\n${heredocBody}\nEOF`,
  187. toolName: "Bash",
  188. },
  189. {
  190. ruleContent: `python -c '${oversizedTail}'`,
  191. toolName: "Bash",
  192. },
  193. ],
  194. type: "addRules",
  195. },
  196. ],
  197. },
  198. },
  199. ],
  200. raw: { rawInput: { command: "write generated files" } },
  201. });
  202. expect(html).not.toContain('data-permission-rule-scopes="true"');
  203. expect(html).not.toContain('data-permission-rule-scope="exact"');
  204. expect(html).not.toContain("cat &gt; /tmp/sun.html");
  205. expect(html).not.toContain("HEREDOC_BODY_MUST_NOT_RENDER");
  206. expect(html).not.toContain("OVERSIZED_TAIL_MUST_NOT_RENDER");
  207. expect(html).not.toContain("仅此命令");
  208. expect(html).toContain("后续相同命令不再询问");
  209. });
  210. it("truncates long permission prefixes without showing exact command grants", () => {
  211. const html = renderPermissionDialog({
  212. type: "permission_request",
  213. taskId: "task-1",
  214. traceId: "trace-1",
  215. requestId: "request-long-prefix",
  216. description: "Run command",
  217. kind: "Bash",
  218. title: "Bash",
  219. options: [
  220. {
  221. optionId: "allow_project",
  222. kind: "allow_always",
  223. name: "Always allow in this project",
  224. response: {
  225. decision: "allow",
  226. permissionUpdates: [
  227. {
  228. behavior: "allow",
  229. rules: [
  230. {
  231. ruleContent: `pnpm run ${"unbroken-prefix-".repeat(20)}:*`,
  232. toolName: "Bash",
  233. },
  234. ],
  235. type: "addRules",
  236. },
  237. ],
  238. },
  239. },
  240. ],
  241. raw: { rawInput: { command: "run long command" } },
  242. });
  243. expect(html).toContain('data-permission-rule-prefixes="true"');
  244. expect(html).toContain('data-permission-rule-scopes="true"');
  245. expect(html).toContain('data-permission-rule-scope-truncated="true"');
  246. expect(html).not.toContain("后续相同命令不再询问");
  247. });
  248. it("shows a subagent source tag for delegated permission requests only", () => {
  249. const baseRequest: ZCodePermissionRequest = {
  250. type: "permission_request",
  251. taskId: "task-1",
  252. traceId: "trace-1",
  253. requestId: "request-1",
  254. description: "Run command",
  255. kind: "Bash",
  256. title: "Bash",
  257. options: [
  258. {
  259. optionId: "allow",
  260. kind: "allow",
  261. name: "Allow",
  262. },
  263. ],
  264. raw: {
  265. toolName: "Bash",
  266. },
  267. };
  268. const mainHtml = renderPermissionDialog(baseRequest);
  269. const subagentHtml = renderPermissionDialog({
  270. ...baseRequest,
  271. origin: SUBAGENT_ORIGIN,
  272. });
  273. expect(mainHtml).not.toContain('data-interaction-origin-badge="subagent"');
  274. expect(subagentHtml).toContain('data-interaction-origin-badge="subagent"');
  275. expect(subagentHtml).toContain("子智能体");
  276. expect(subagentHtml).toContain("来自子智能体:general-purpose");
  277. });
  278. it("names the subagent in the source tag when the origin carries a description", () => {
  279. const html = renderPermissionDialog({
  280. type: "permission_request",
  281. taskId: "task-1",
  282. traceId: "trace-1",
  283. requestId: "request-1",
  284. description: "Run command",
  285. kind: "Bash",
  286. title: "Bash",
  287. options: [{ optionId: "allow", kind: "allow", name: "Allow" }],
  288. raw: { toolName: "Bash" },
  289. // 动态工作流子代理的 origin(docs/dynamic-workflow/launch.md「Permissions inside a run」)。
  290. origin: {
  291. ...SUBAGENT_ORIGIN,
  292. agentType: "zcode-workflow",
  293. description: "reviewer (agent#1@0)",
  294. },
  295. });
  296. expect(html).toContain('data-interaction-origin-badge="subagent"');
  297. expect(html).toContain("子智能体 · reviewer (agent#1@0)");
  298. // 截断时完整文本留在 tooltip 里,且仍带 agentType。
  299. expect(html).toContain("来自子智能体:zcode-workflow · reviewer (agent#1@0)");
  300. });
  301. it("renders a compact implementation plan placeholder for switch mode requests", () => {
  302. const html = renderPermissionDialog({
  303. type: "permission_request",
  304. taskId: "task-1",
  305. traceId: "trace-1",
  306. requestId: "request-1",
  307. description: "Ready to code?",
  308. kind: "switch_mode",
  309. title: "Ready to code?",
  310. options: [
  311. {
  312. optionId: "code",
  313. kind: "allow_always",
  314. name: "是,并自动接受后续操作",
  315. },
  316. ],
  317. raw: {},
  318. });
  319. expect(html).toContain("实施计划");
  320. expect(html).not.toContain("ToolCall");
  321. expect(html).not.toContain("Ready to code?");
  322. });
  323. it("renders a compact implementation plan placeholder for ExitPlanMode requests", () => {
  324. const html = renderPermissionDialog({
  325. type: "permission_request",
  326. taskId: "task-1",
  327. traceId: "trace-1",
  328. requestId: "request-1",
  329. description: "ExitPlanMode changes session mode after user plan approval",
  330. kind: "ExitPlanMode",
  331. title: "ExitPlanMode",
  332. options: [
  333. {
  334. optionId: "allow",
  335. kind: "allow",
  336. name: "Allow",
  337. },
  338. {
  339. optionId: "deny",
  340. kind: "deny",
  341. name: "Deny",
  342. },
  343. ],
  344. raw: {
  345. toolName: "ExitPlanMode",
  346. },
  347. });
  348. expect(html).toContain("实施计划");
  349. expect(html).not.toContain("ToolCall");
  350. expect(html).not.toContain("ExitPlanMode changes session mode");
  351. });
  352. it("prefers custom ZCode Agent option names over generic kind labels", () => {
  353. const html = renderPermissionDialog({
  354. type: "permission_request",
  355. taskId: "task-1",
  356. traceId: "trace-1",
  357. requestId: "request-1",
  358. description: "Ready to code?",
  359. kind: "switch_mode",
  360. title: "Ready to code?",
  361. options: [
  362. {
  363. optionId: "code",
  364. kind: "allow_always",
  365. name: "是,并自动接受后续操作",
  366. },
  367. {
  368. optionId: "ask",
  369. kind: "allow_always",
  370. name: "是,但高风险操作仍逐次确认",
  371. },
  372. {
  373. optionId: "reject",
  374. kind: "reject_once",
  375. name: "否,先继续整理计划",
  376. },
  377. ],
  378. raw: {},
  379. });
  380. expect(html).toContain("是,并自动接受后续操作");
  381. expect(html).toContain("是,但高风险操作仍逐次确认");
  382. expect(html).toContain("否,先继续整理计划");
  383. expect(html).not.toContain("始终允许");
  384. expect(html).not.toContain("拒绝");
  385. });
  386. it("localizes GLM project-scoped generic permission options", () => {
  387. const html = renderPermissionDialog(
  388. {
  389. type: "permission_request",
  390. taskId: "task-1",
  391. traceId: "trace-1",
  392. requestId: "request-1",
  393. description: "Run shell command",
  394. kind: "execute",
  395. title: "Run shell command",
  396. options: [
  397. {
  398. optionId: "allow_project",
  399. kind: "allow_always",
  400. name: "Always allow in this project",
  401. },
  402. ],
  403. raw: {
  404. rawInput: {
  405. command: "printf '102384' >> test.md",
  406. },
  407. },
  408. },
  409. "glm",
  410. );
  411. expect(html).toContain("始终允许此命令");
  412. expect(html).toContain("后续相同命令不再询问");
  413. expect(html).not.toContain("Always allow in this project");
  414. });
  415. it("keeps non-Codex session-scoped permission option names provider-native", () => {
  416. const html = renderPermissionDialog(
  417. {
  418. type: "permission_request",
  419. taskId: "task-1",
  420. traceId: "trace-1",
  421. requestId: "request-1",
  422. description: "Run shell command",
  423. kind: "execute",
  424. title: "Run shell command",
  425. options: [
  426. {
  427. optionId: "allow_for_session",
  428. kind: "allow_always",
  429. name: "Allow for Session",
  430. },
  431. ],
  432. raw: {
  433. rawInput: {
  434. command: "printf '102384' >> test.md",
  435. },
  436. },
  437. },
  438. "claude",
  439. );
  440. expect(html).toContain("Allow for Session");
  441. expect(html).not.toContain("允许本会话");
  442. });
  443. it("renders file change previews with file display for add operations", () => {
  444. const html = renderPermissionDialog({
  445. type: "permission_request",
  446. taskId: "task-1",
  447. traceId: "trace-1",
  448. requestId: "request-1",
  449. description: "Apply patch",
  450. kind: "apply_patch",
  451. title: "Apply patch",
  452. options: [
  453. {
  454. optionId: "allow",
  455. kind: "allow_once",
  456. name: "允许",
  457. },
  458. ],
  459. raw: {
  460. changes: {
  461. "/Users/dev/ZCodeProject/demo/README.md": {
  462. type: "add",
  463. content: "\n",
  464. },
  465. },
  466. },
  467. });
  468. expect(html).toContain("README.md");
  469. expect(html).toContain("等待确认");
  470. expect(html).not.toContain("/Users/dev/ZCodeProject/demo/");
  471. });
  472. it("renders ZCode protocol Write input as an existing file preview", () => {
  473. const html = renderPermissionDialog({
  474. type: "permission_request",
  475. taskId: "task-1",
  476. traceId: "trace-1",
  477. requestId: "request-1",
  478. description: "Tool has side effects and requires approval",
  479. kind: "Write",
  480. title: "Write",
  481. options: [
  482. {
  483. optionId: "allow",
  484. kind: "allow_once",
  485. name: "允许",
  486. },
  487. ],
  488. raw: {
  489. requestId: "request-1",
  490. toolName: "Write",
  491. input: {
  492. file_path: "/Users/dev/ZCodeProject/demo/quicksort.js",
  493. content: "export function quicksort(items) {\n return items;\n}\n",
  494. },
  495. },
  496. });
  497. expect(html).toContain("quicksort.js");
  498. expect(html).toContain("等待确认");
  499. expect(html).toContain("+3");
  500. expect(html).not.toContain("toolName");
  501. expect(html).not.toContain("file_path");
  502. });
  503. it("renders ZCode protocol Edit input as an existing file preview", () => {
  504. const html = renderPermissionDialog({
  505. type: "permission_request",
  506. taskId: "task-1",
  507. traceId: "trace-1",
  508. requestId: "request-1",
  509. description: "Tool has side effects and requires approval",
  510. kind: "Edit",
  511. title: "Edit",
  512. options: [
  513. {
  514. optionId: "allow",
  515. kind: "allow_once",
  516. name: "允许",
  517. },
  518. ],
  519. raw: {
  520. requestId: "request-1",
  521. toolName: "Edit",
  522. input: {
  523. file_path: "/Users/dev/ZCodeProject/demo/quicksort.js",
  524. old_string: "平均时间复杂度 O(n log n)",
  525. new_string: "平均时间复杂度 O(n log n),最坏 O(n^2)",
  526. },
  527. },
  528. });
  529. expect(html).toContain("quicksort.js");
  530. expect(html).toContain("等待确认");
  531. expect(html).toContain("+1");
  532. expect(html).toContain("-1");
  533. expect(html).not.toContain("old_string");
  534. expect(html).not.toContain("new_string");
  535. });
  536. it("renders grouped file changes for multiple updates", () => {
  537. const html = renderPermissionDialog({
  538. type: "permission_request",
  539. taskId: "task-1",
  540. traceId: "trace-1",
  541. requestId: "request-1",
  542. description: "Apply patch",
  543. kind: "apply_patch",
  544. title: "Apply patch",
  545. options: [
  546. {
  547. optionId: "allow",
  548. kind: "allow_once",
  549. name: "允许",
  550. },
  551. ],
  552. raw: {
  553. changes: {
  554. "/Users/dev/ZCodeProject/demo/index.html": {
  555. type: "update",
  556. content: "<html></html>",
  557. },
  558. "/Users/dev/ZCodeProject/demo/styles.css": {
  559. type: "update",
  560. content: "body {}",
  561. },
  562. "/Users/dev/ZCodeProject/demo/script.js": {
  563. type: "update",
  564. content: "console.log('ok')",
  565. },
  566. },
  567. },
  568. });
  569. expect(html).toContain("等待确认");
  570. expect(html).toContain("index.html");
  571. expect(html).toContain("styles.css");
  572. expect(html).toContain("script.js");
  573. expect(html).not.toContain("/Users/dev/ZCodeProject/demo/");
  574. });
  575. it("permission preview displays pending approval instead of execution state", () => {
  576. const html = renderPermissionDialog({
  577. type: "permission_request",
  578. taskId: "task-1",
  579. traceId: "trace-1",
  580. requestId: "request-1",
  581. description: "Run shell command",
  582. kind: "execute",
  583. title: "Run shell command",
  584. options: [
  585. {
  586. optionId: "allow",
  587. kind: "allow_once",
  588. name: "允许",
  589. },
  590. ],
  591. raw: {
  592. rawInput: {
  593. command: "printf ok",
  594. },
  595. },
  596. });
  597. expect(html).toContain("Run shell command");
  598. expect(html).toContain("等待确认");
  599. expect(html).not.toContain("执行中");
  600. });
  601. it("renders Bash input description as the permission reason instead of a running state", () => {
  602. const html = renderPermissionDialog({
  603. type: "permission_request",
  604. taskId: "task-1",
  605. traceId: "trace-1",
  606. requestId: "request-bash-description",
  607. description: "High risk tools require explicit approval",
  608. kind: "Bash",
  609. title: "Bash",
  610. options: [
  611. {
  612. optionId: "allow",
  613. kind: "allow_once",
  614. name: "允许",
  615. },
  616. ],
  617. raw: {
  618. requestId: "request-bash-description",
  619. reason: "High risk tools require explicit approval",
  620. toolName: "Bash",
  621. input: {
  622. description: "Check package.json scripts",
  623. command: 'cat /Users/dev/test/z-m/package.json 2>/dev/null || echo "No package.json"',
  624. },
  625. },
  626. });
  627. expect(html).toContain("Check package.json scripts");
  628. expect(html).toContain("等待确认");
  629. expect(html).toContain("cat /Users/dev/test/z-m/package.json");
  630. expect(html).not.toContain("High risk tools require explicit approval");
  631. expect(html).not.toContain("执行中");
  632. });
  633. it("hides the generic high-risk policy reason when no tool description exists", () => {
  634. const html = renderPermissionDialog({
  635. type: "permission_request",
  636. taskId: "task-1",
  637. traceId: "trace-1",
  638. requestId: "request-high-risk-policy-reason",
  639. description: "High risk tools require explicit approval",
  640. kind: "Bash",
  641. title: "Bash",
  642. options: [
  643. {
  644. optionId: "allow",
  645. kind: "allow_once",
  646. name: "允许",
  647. },
  648. ],
  649. raw: {
  650. requestId: "request-high-risk-policy-reason",
  651. reason: "High risk tools require explicit approval",
  652. riskLevel: "high",
  653. toolName: "Bash",
  654. input: {
  655. command: "rm -rf ./generated",
  656. },
  657. },
  658. });
  659. expect(html).toContain("等待确认");
  660. expect(html).toContain("rm -rf ./generated");
  661. expect(html).not.toContain("High risk tools require explicit approval");
  662. });
  663. it("renders WebFetch permission requests as a URL summary instead of fallback JSON", () => {
  664. const url =
  665. "https://www.zhihu.com/search?q=%E6%99%BA%E8%B0%B1AI%20Agent%20%E9%9D%A2%E8%AF%95%E9%A2%98";
  666. const html = renderPermissionDialog({
  667. type: "permission_request",
  668. taskId: "task-1",
  669. traceId: "trace-1",
  670. requestId: "perm-webfetch",
  671. description: "Tool has side effects and requires approval",
  672. kind: "WebFetch",
  673. title: "WebFetch",
  674. options: [
  675. {
  676. optionId: "allow",
  677. kind: "allow_once",
  678. name: "允许",
  679. },
  680. {
  681. optionId: "deny",
  682. kind: "deny",
  683. name: "拒绝",
  684. },
  685. ],
  686. raw: {
  687. requestId: "perm-webfetch",
  688. toolName: "WebFetch",
  689. input: {
  690. url,
  691. prompt: "List all AI Agent / LLM application development interview questions mentioned",
  692. },
  693. },
  694. });
  695. expect(html).toContain(url);
  696. expect(html).toContain("等待确认");
  697. expect(html).not.toContain("Tool has side effects and requires approval");
  698. expect(html).not.toContain("List all AI Agent");
  699. expect(html).not.toContain("&quot;toolId&quot;");
  700. expect(html.match(/WebFetch/g)?.length ?? 0).toBeLessThanOrEqual(1);
  701. });
  702. it("renders MCP permission requests without the generic policy reason", () => {
  703. const toolName = "mcp__ios-simulator__ios_preflight";
  704. const reason = "Tool has side effects and requires approval";
  705. const html = renderPermissionDialog({
  706. type: "permission_request",
  707. taskId: "task-1",
  708. traceId: "trace-1",
  709. requestId: "perm-mcp",
  710. description: reason,
  711. kind: toolName,
  712. title: toolName,
  713. options: [
  714. {
  715. optionId: "allow",
  716. kind: "allow_once",
  717. name: "允许",
  718. },
  719. {
  720. optionId: "deny",
  721. kind: "deny",
  722. name: "拒绝",
  723. },
  724. ],
  725. raw: {
  726. requestId: "perm-mcp",
  727. toolCallId: "call-mcp",
  728. toolName,
  729. reason,
  730. riskLevel: "medium",
  731. input: {},
  732. },
  733. });
  734. expect(html.match(new RegExp(toolName, "g"))?.length ?? 0).toBe(1);
  735. expect(html).not.toContain(reason);
  736. expect(html).not.toContain("&quot;reason&quot;");
  737. expect(html).not.toContain("&quot;toolCallId&quot;");
  738. });
  739. it("keeps a specific MCP permission reason visible", () => {
  740. const toolName = "mcp__ios-simulator__ios_preflight";
  741. const reason = "The simulator must be booted before installing the app";
  742. const html = renderPermissionDialog({
  743. type: "permission_request",
  744. taskId: "task-1",
  745. traceId: "trace-1",
  746. requestId: "perm-mcp-specific-reason",
  747. description: reason,
  748. kind: toolName,
  749. title: toolName,
  750. options: [
  751. {
  752. optionId: "allow",
  753. kind: "allow_once",
  754. name: "允许",
  755. },
  756. {
  757. optionId: "deny",
  758. kind: "deny",
  759. name: "拒绝",
  760. },
  761. ],
  762. raw: {
  763. requestId: "perm-mcp-specific-reason",
  764. toolCallId: "call-mcp-specific-reason",
  765. toolName,
  766. reason,
  767. riskLevel: "medium",
  768. input: {},
  769. },
  770. });
  771. expect(html).toContain(reason);
  772. });
  773. });