configSelectModeOptions.test.ts 14 KB

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