configSelectModeOptions.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. import { createElement, forwardRef, type ForwardedRef } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { beforeEach, describe, expect, it, vi } from "vitest";
  4. const mockSelectContentProps: Array<{
  5. onCloseAutoFocus?: (event: { preventDefault: () => void }) => void;
  6. }> = [];
  7. vi.mock("@/ControlHintTooltip.js", () => ({
  8. ControlHintTooltip: ({ children }: { children: unknown }) =>
  9. createElement("div", { "data-testid": "control-hint-tooltip" }, children),
  10. }));
  11. vi.mock("@/components/ai-elements/context.js", () => ({
  12. Context: ({ children }: { children: unknown }) => createElement("div", null, children),
  13. ContextContent: ({ children }: { children: unknown }) => createElement("div", null, children),
  14. ContextContentBody: ({ children }: { children: unknown }) => createElement("div", null, children),
  15. ContextContentHeader: () => createElement("div"),
  16. ContextTrigger: () => createElement("button"),
  17. }));
  18. vi.mock("@/components/ui/select.js", () => ({
  19. Select: ({ children }: { children: unknown }) => createElement("div", { "data-testid": "select-root" }, children),
  20. SelectContent: ({
  21. children,
  22. onCloseAutoFocus,
  23. }: {
  24. children: unknown;
  25. onCloseAutoFocus?: (event: { preventDefault: () => void }) => void;
  26. }) => {
  27. mockSelectContentProps.push({ onCloseAutoFocus });
  28. return createElement("div", { "data-testid": "select-content" }, children);
  29. },
  30. SelectItem: forwardRef(function MockSelectItem(
  31. {
  32. children,
  33. value,
  34. className,
  35. }: {
  36. children: unknown;
  37. value: string;
  38. className?: string;
  39. },
  40. ref: ForwardedRef<HTMLDivElement>,
  41. ) {
  42. return createElement(
  43. "div",
  44. {
  45. ref,
  46. "data-slot": "select-item",
  47. "data-value": value,
  48. className,
  49. },
  50. children,
  51. );
  52. }),
  53. SelectTrigger: ({
  54. children,
  55. className,
  56. indicator,
  57. }: {
  58. children: unknown;
  59. className?: string;
  60. indicator?: unknown;
  61. }) =>
  62. createElement(
  63. "button",
  64. {
  65. "data-testid": "select-trigger",
  66. className,
  67. },
  68. children,
  69. indicator,
  70. ),
  71. SelectValue: () => createElement("span", null, "value"),
  72. }));
  73. vi.mock("@/components/ui/tooltip.js", () => ({
  74. TooltipProvider: ({ children }: { children: unknown }) => createElement("div", { "data-testid": "tooltip-provider" }, children),
  75. Tooltip: ({ children }: { children: unknown }) => createElement("div", { "data-testid": "tooltip-root" }, children),
  76. TooltipTrigger: ({ children }: { children: unknown }) => children,
  77. TooltipContent: ({
  78. children,
  79. side,
  80. }: {
  81. children: unknown;
  82. side?: string;
  83. }) =>
  84. createElement(
  85. "div",
  86. {
  87. "data-testid": "tooltip-content",
  88. "data-side": side,
  89. },
  90. children,
  91. ),
  92. }));
  93. import {
  94. ConfigSelect,
  95. isHighPermissionModeValue,
  96. resolveModeOptionIcon,
  97. } from "@/chat-input-toolbar/display.js";
  98. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  99. beforeEach(() => {
  100. mockSelectContentProps.length = 0;
  101. });
  102. function renderConfigSelect(
  103. locale: "zh-CN" | "en-US",
  104. props: Parameters<typeof ConfigSelect>[0],
  105. ) {
  106. return renderToStaticMarkup(
  107. createElement(
  108. ZCodeIntlProvider,
  109. { initialLocale: locale },
  110. createElement(ConfigSelect, props),
  111. ),
  112. );
  113. }
  114. describe("ConfigSelect mode options", () => {
  115. it.each([
  116. ["zh-CN", "自主模式", "在有风险时询问"],
  117. ["en-US", "Autonomous mode", "Ask when there’s risk"],
  118. ] as const)(
  119. "Guarded 在 %s 下使用浅蓝色和 Edit 图标,不复用 YOLO 警示样式",
  120. (locale, label, description) => {
  121. const html = renderConfigSelect(locale, {
  122. provider: "glm",
  123. option: {
  124. id: "mode",
  125. name: "Mode",
  126. category: "mode",
  127. type: "select",
  128. currentValue: "guarded",
  129. options: [{ value: "guarded", name: "Guarded" }],
  130. },
  131. onValueChange: vi.fn(),
  132. tooltipTitle: "Mode",
  133. });
  134. expect(isHighPermissionModeValue("guarded")).toBe(false);
  135. expect(html).toContain(label);
  136. expect(html).toContain(description);
  137. expect(html).toContain('data-value="guarded"');
  138. expect(resolveModeOptionIcon("guarded")).toBe(resolveModeOptionIcon("edit"));
  139. expect(html.match(/lucide-shield-check/g)).toHaveLength(2);
  140. expect(html).toContain("text-icon-blue hover:text-icon-blue aria-expanded:text-icon-blue");
  141. expect(html).not.toContain("text-warning");
  142. expect(html).not.toContain("lucide-shield-alert");
  143. },
  144. );
  145. it.each([ ["zh-CN", "自动编辑"], ["en-US", "Edit automatically"] ] as const)(
  146. "历史 edit 在 %s 下保留本地化回显,但不重新加入候选", (locale, label) => {
  147. const onValueChange = vi.fn();
  148. const html = renderConfigSelect(locale, {
  149. provider: "glm",
  150. option: {
  151. id: "mode", name: "Mode", category: "mode", type: "select", currentValue: "edit",
  152. options: [{ value: "guarded", name: "Guarded" }, { value: "yolo", name: "YOLO" }],
  153. },
  154. onValueChange,
  155. tooltipTitle: "Mode",
  156. });
  157. expect(html).toContain(label);
  158. expect(html).not.toContain('data-value="edit"');
  159. expect(onValueChange).not.toHaveBeenCalled();
  160. },
  161. );
  162. it("允许 composer 在窄宽度隐藏 mode 下拉箭头", () => {
  163. const html = renderConfigSelect("zh-CN", {
  164. provider: "codex",
  165. option: {
  166. id: "mode",
  167. name: "Mode",
  168. category: "mode",
  169. type: "select",
  170. currentValue: "read-only",
  171. options: [{ value: "read-only", name: "read-only" }],
  172. },
  173. onValueChange: () => {},
  174. tooltipTitle: "Mode",
  175. indicatorClassName: "hidden @xl/composer:block",
  176. });
  177. expect(html).toContain("lucide-chevron-down");
  178. expect(html).toContain("hidden @xl/composer:block");
  179. });
  180. it("触控设备关闭 mode 菜单后不会重新聚焦聊天输入框", () => {
  181. const focus = vi.fn();
  182. const preventDefault = vi.fn();
  183. const originalWindowDescriptor = Object.getOwnPropertyDescriptor(
  184. globalThis,
  185. "window",
  186. );
  187. const originalDocumentDescriptor = Object.getOwnPropertyDescriptor(
  188. globalThis,
  189. "document",
  190. );
  191. Object.defineProperty(globalThis, "window", {
  192. configurable: true,
  193. value: {
  194. addEventListener: vi.fn(),
  195. matchMedia: vi.fn(() => ({ matches: true })),
  196. removeEventListener: vi.fn(),
  197. },
  198. });
  199. Object.defineProperty(globalThis, "document", {
  200. configurable: true,
  201. value: {
  202. querySelector: vi.fn(() => ({ focus })),
  203. },
  204. });
  205. try {
  206. renderConfigSelect("zh-CN", {
  207. provider: "codex",
  208. option: {
  209. id: "mode",
  210. name: "Mode",
  211. category: "mode",
  212. type: "select",
  213. currentValue: "read-only",
  214. options: [
  215. {
  216. value: "read-only",
  217. name: "read-only",
  218. },
  219. ],
  220. },
  221. onValueChange: () => {},
  222. tooltipTitle: "Mode",
  223. });
  224. mockSelectContentProps[0]?.onCloseAutoFocus?.({ preventDefault });
  225. expect(preventDefault).toHaveBeenCalledTimes(1);
  226. expect(document.querySelector).not.toHaveBeenCalled();
  227. expect(focus).not.toHaveBeenCalled();
  228. } finally {
  229. if (originalWindowDescriptor) {
  230. Object.defineProperty(globalThis, "window", originalWindowDescriptor);
  231. } else {
  232. Reflect.deleteProperty(globalThis, "window");
  233. }
  234. if (originalDocumentDescriptor) {
  235. Object.defineProperty(globalThis, "document", originalDocumentDescriptor);
  236. } else {
  237. Reflect.deleteProperty(globalThis, "document");
  238. }
  239. }
  240. });
  241. it("中文环境下显示中文标签,并在菜单项中显示 description", () => {
  242. const html = renderConfigSelect("zh-CN", {
  243. provider: "glm",
  244. option: {
  245. id: "mode",
  246. name: "Mode",
  247. category: "mode",
  248. type: "select",
  249. currentValue: "build",
  250. options: [
  251. {
  252. value: "build",
  253. name: "Build",
  254. description: "Require confirmation before edits.",
  255. },
  256. {
  257. value: "edit",
  258. name: "Edit",
  259. },
  260. ],
  261. },
  262. onValueChange: () => {},
  263. tooltipTitle: "Mode",
  264. });
  265. expect(html).toContain("变更前确认");
  266. expect(html).toContain("自动编辑");
  267. expect(html).not.toContain("变更前确认 / Build");
  268. expect(html).toContain("改文件前先问我。");
  269. expect(html).toContain("pr-8");
  270. });
  271. it("mode trigger 使用当前文案而不是 SelectValue 自动回填", () => {
  272. const html = renderConfigSelect("zh-CN", {
  273. provider: "glm",
  274. option: {
  275. id: "mode",
  276. name: "Mode",
  277. category: "mode",
  278. type: "select",
  279. currentValue: "build",
  280. options: [
  281. {
  282. value: "build",
  283. name: "Build",
  284. },
  285. {
  286. value: "edit",
  287. name: "Edit",
  288. },
  289. ],
  290. },
  291. onValueChange: () => {},
  292. tooltipTitle: "Mode",
  293. });
  294. expect(html).toContain("变更前确认");
  295. expect(html).not.toContain(">value<");
  296. });
  297. it("英文环境下显示英文多语言标签", () => {
  298. const html = renderConfigSelect("en-US", {
  299. provider: "glm",
  300. option: {
  301. id: "mode",
  302. name: "Mode",
  303. category: "mode",
  304. type: "select",
  305. currentValue: "build",
  306. options: [
  307. {
  308. value: "build",
  309. name: "Build",
  310. },
  311. {
  312. value: "edit",
  313. name: "Edit",
  314. },
  315. ],
  316. },
  317. onValueChange: () => {},
  318. tooltipTitle: "Mode",
  319. });
  320. expect(html).toContain("Ask before changes");
  321. expect(html).toContain("Edit automatically");
  322. expect(html).not.toContain("默认模式");
  323. });
  324. it("GLM 模式列表只渲染 agent 返回的可用模式", () => {
  325. const html = renderConfigSelect("zh-CN", {
  326. provider: "glm",
  327. option: {
  328. id: "mode",
  329. name: "Mode",
  330. category: "mode",
  331. type: "select",
  332. currentValue: "yolo",
  333. options: [
  334. { value: "build", name: "Build" },
  335. { value: "edit", name: "Edit" },
  336. { value: "plan", name: "Plan" },
  337. { value: "yolo", name: "Yolo" },
  338. ],
  339. },
  340. onValueChange: () => {},
  341. tooltipTitle: "Mode",
  342. });
  343. expect(html).toContain("变更前确认");
  344. expect(html).toContain("自动编辑");
  345. expect(html).toContain("计划模式");
  346. expect(html).toContain("完全访问");
  347. expect(html).toContain("自动编辑文件。");
  348. expect(html).not.toContain("Auto");
  349. });
  350. it("选中 edit 模式时不会显示高权限 warning 色", () => {
  351. const html = renderConfigSelect("zh-CN", {
  352. provider: "glm",
  353. option: {
  354. id: "mode",
  355. name: "Mode",
  356. category: "mode",
  357. type: "select",
  358. currentValue: "edit",
  359. options: [
  360. {
  361. value: "edit",
  362. name: "Edit",
  363. },
  364. ],
  365. },
  366. onValueChange: () => {},
  367. tooltipTitle: "Mode",
  368. triggerClassName: "rounded-full text-ui-base/relaxed pl-3 pr-2",
  369. });
  370. expect(html).toContain('data-testid="select-trigger"');
  371. expect(html).not.toContain("text-warning");
  372. });
  373. it.each([
  374. ["claude", "bypassPermissions"],
  375. ["codex", "full-access"],
  376. ["codex", "agent-full-access"],
  377. ["gemini", "yolo"],
  378. ["glm", "yolo"],
  379. ] as const)(
  380. "选中高权限模式 %s/%s 时,trigger 文本会显示 warning 色",
  381. (provider, currentValue) => {
  382. const html = renderConfigSelect("zh-CN", {
  383. provider,
  384. option: {
  385. id: "mode",
  386. name: "Mode",
  387. category: "mode",
  388. type: "select",
  389. currentValue,
  390. options: [
  391. {
  392. value: currentValue,
  393. name: currentValue,
  394. },
  395. ],
  396. },
  397. onValueChange: () => {},
  398. tooltipTitle: "Mode",
  399. triggerClassName: "rounded-full text-ui-base/relaxed pl-3 pr-2",
  400. });
  401. expect(html).toContain('data-testid="select-trigger"');
  402. expect(html).toContain("text-warning");
  403. },
  404. );
  405. it("restoreFocusSelector 为 null 时保留 Radix 默认回焦(不 preventDefault)", () => {
  406. // Bugfix 回归:Automations 权限选择器没有聊天输入框可恢复;之前 onCloseAutoFocus
  407. // 无条件 preventDefault,null 时把 Radix 默认“回焦到 trigger”一并吃掉,焦点掉 body。
  408. const focus = vi.fn();
  409. const preventDefault = vi.fn();
  410. const originalWindowDescriptor = Object.getOwnPropertyDescriptor(
  411. globalThis,
  412. "window",
  413. );
  414. const originalDocumentDescriptor = Object.getOwnPropertyDescriptor(
  415. globalThis,
  416. "document",
  417. );
  418. Object.defineProperty(globalThis, "window", {
  419. configurable: true,
  420. value: {
  421. addEventListener: vi.fn(),
  422. // 桌面指针环境(非触屏):非 null selector 本应恢复输入框焦点的场景。
  423. matchMedia: vi.fn(() => ({ matches: false })),
  424. removeEventListener: vi.fn(),
  425. },
  426. });
  427. Object.defineProperty(globalThis, "document", {
  428. configurable: true,
  429. value: {
  430. querySelector: vi.fn(() => ({ focus })),
  431. },
  432. });
  433. try {
  434. renderConfigSelect("zh-CN", {
  435. provider: "glm",
  436. option: {
  437. id: "mode",
  438. name: "Mode",
  439. category: "mode",
  440. type: "select",
  441. currentValue: "build",
  442. options: [
  443. {
  444. value: "build",
  445. name: "build",
  446. },
  447. ],
  448. },
  449. onValueChange: () => {},
  450. tooltipTitle: "Mode",
  451. restoreFocusSelector: null,
  452. });
  453. mockSelectContentProps[0]?.onCloseAutoFocus?.({ preventDefault });
  454. expect(preventDefault).not.toHaveBeenCalled();
  455. expect(document.querySelector).not.toHaveBeenCalled();
  456. expect(focus).not.toHaveBeenCalled();
  457. } finally {
  458. if (originalWindowDescriptor) {
  459. Object.defineProperty(globalThis, "window", originalWindowDescriptor);
  460. } else {
  461. Reflect.deleteProperty(globalThis, "window");
  462. }
  463. if (originalDocumentDescriptor) {
  464. Object.defineProperty(globalThis, "document", originalDocumentDescriptor);
  465. } else {
  466. Reflect.deleteProperty(globalThis, "document");
  467. }
  468. }
  469. });
  470. it("不传 provider 时 label/description 回退到预本地化 entry", () => {
  471. // ConfigSelect 仍支持没有 provider 词表的调用方直接提供本地化名称与说明。
  472. const html = renderConfigSelect("zh-CN", {
  473. option: {
  474. id: "mode",
  475. name: "Permission",
  476. category: "mode",
  477. type: "select",
  478. currentValue: "yolo",
  479. options: [
  480. { value: "build", name: "自定义确认", description: "自定义确认说明。" },
  481. { value: "yolo", name: "完全访问", description: "更少确认地执行。" },
  482. ],
  483. },
  484. onValueChange: () => {},
  485. tooltipTitle: "Permission",
  486. });
  487. expect(html).toContain("自定义确认");
  488. expect(html).toContain("完全访问");
  489. expect(html).toContain("自定义确认说明。");
  490. expect(html).toContain("更少确认地执行。");
  491. expect(html).not.toContain("变更前确认");
  492. // 高权限 warning 语义只依赖 entry.value,不依赖 provider。
  493. expect(html).toContain("text-warning");
  494. });
  495. });