modelConfigSelectRenderStability.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. import { act, createElement, type ReactNode } from "react";
  2. import { createRoot, type Root } from "react-dom/client";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import type { ModelSelectGroup } from "@/ModelConfigSelect.js";
  5. const {
  6. dropdownContentCalls,
  7. dropdownItemCalls,
  8. radioItemCalls,
  9. subContentCalls,
  10. subTriggerCalls,
  11. } = vi.hoisted(() => ({
  12. dropdownContentCalls: [] as Array<Record<string, unknown>>,
  13. dropdownItemCalls: [] as Array<Record<string, unknown>>,
  14. radioItemCalls: [] as Array<Record<string, unknown>>,
  15. subContentCalls: [] as Array<Record<string, unknown>>,
  16. subTriggerCalls: [] as Array<Record<string, unknown>>,
  17. }));
  18. vi.mock("lucide-react", () => {
  19. const createIcon = (name: string) => (props: Record<string, unknown>) =>
  20. createElement("svg", { "data-icon": name, ...props });
  21. return {
  22. AlertCircle: createIcon("alert-circle"),
  23. CheckIcon: createIcon("check"),
  24. ChevronDownIcon: createIcon("chevron-down"),
  25. LoaderIcon: createIcon("loader"),
  26. // Bugfix: ModelConfigSelect 的 trigger 会无条件渲染 PackageIcon;封闭 mock 漏掉它时,
  27. // render stability 用例会在进入菜单延迟渲染断言前直接崩溃。
  28. PackageIcon: createIcon("package"),
  29. };
  30. });
  31. vi.mock("@/components/ui/button.js", () => ({
  32. Button: ({ children, ...props }: { children?: ReactNode }) =>
  33. createElement("button", props, children),
  34. }));
  35. vi.mock("@/ControlHintTooltip.js", () => ({
  36. ControlHintTooltip: ({ children }: { children?: ReactNode }) =>
  37. createElement("span", null, children),
  38. }));
  39. vi.mock("@/chat-input-toolbar/RollingToolbarLabel.js", () => ({
  40. RollingToolbarLabel: ({ label }: { label: string }) =>
  41. createElement("span", null, label),
  42. }));
  43. vi.mock("@/components/ui/dropdown-menu.js", () => ({
  44. DropdownMenu: ({ children }: { children?: ReactNode }) =>
  45. createElement("div", null, children),
  46. DropdownMenuContent: ({ children, ...props }: { children?: ReactNode }) => {
  47. dropdownContentCalls.push(props);
  48. return createElement("div", props, children);
  49. },
  50. DropdownMenuItem: ({ children, ...props }: { children?: ReactNode }) => {
  51. dropdownItemCalls.push(props);
  52. return createElement("div", props, children);
  53. },
  54. DropdownMenuRadioGroup: ({ children }: { children?: ReactNode }) =>
  55. createElement("div", null, children),
  56. DropdownMenuRadioItem: ({ children, ...props }: { children?: ReactNode }) => {
  57. radioItemCalls.push(props);
  58. return createElement("div", props, children);
  59. },
  60. DropdownMenuSeparator: () => createElement("hr"),
  61. DropdownMenuSub: ({ children }: { children?: ReactNode }) =>
  62. createElement("div", null, children),
  63. DropdownMenuSubContent: ({ children, ...props }: { children?: ReactNode }) => {
  64. subContentCalls.push(props);
  65. return createElement("div", props, children);
  66. },
  67. DropdownMenuSubTrigger: ({
  68. children,
  69. ...props
  70. }: {
  71. children?: ReactNode;
  72. }) => {
  73. subTriggerCalls.push(props);
  74. return createElement("div", props, children);
  75. },
  76. DropdownMenuTrigger: ({ children }: { children?: ReactNode }) =>
  77. createElement("span", null, children),
  78. }));
  79. vi.mock("@/components/ui/tooltip.js", () => ({
  80. Tooltip: ({ children }: { children?: ReactNode }) =>
  81. createElement("span", null, children),
  82. TooltipContent: ({ children }: { children?: ReactNode }) =>
  83. createElement("span", null, children),
  84. TooltipProvider: ({ children }: { children?: ReactNode }) =>
  85. createElement("span", null, children),
  86. TooltipTrigger: ({ children }: { children?: ReactNode }) =>
  87. createElement("span", null, children),
  88. }));
  89. function createMinimalElement(ownerDocument: Document, tagName = "div") {
  90. const element = {
  91. addEventListener: () => {},
  92. appendChild: (child: { parentNode?: unknown }) => {
  93. child.parentNode = element;
  94. return child;
  95. },
  96. childNodes: [] as unknown[],
  97. getAttribute: () => null,
  98. insertBefore: (child: { parentNode?: unknown }) => {
  99. child.parentNode = element;
  100. return child;
  101. },
  102. nodeName: tagName.toUpperCase(),
  103. nodeType: 1,
  104. ownerDocument,
  105. parentNode: null as unknown,
  106. removeAttribute: () => {},
  107. removeChild: (child: { parentNode?: unknown }) => {
  108. child.parentNode = null;
  109. return child;
  110. },
  111. removeEventListener: () => {},
  112. setAttribute: () => {},
  113. style: {},
  114. tagName: tagName.toUpperCase(),
  115. };
  116. return element as unknown as Element;
  117. }
  118. function installMinimalDom() {
  119. const documentMock = {
  120. addEventListener: () => {},
  121. createElement: (tagName: string) =>
  122. createMinimalElement(documentMock as unknown as Document, tagName),
  123. createElementNS: (_namespace: string, tagName: string) =>
  124. createMinimalElement(documentMock as unknown as Document, tagName),
  125. createTextNode: (nodeValue: string) => ({
  126. nodeType: 3,
  127. nodeValue,
  128. ownerDocument: documentMock,
  129. parentNode: null,
  130. }),
  131. nodeType: 9,
  132. querySelector: () => null,
  133. removeEventListener: () => {},
  134. } as unknown as Document;
  135. const windowMock = {
  136. addEventListener: () => {},
  137. document: documentMock,
  138. HTMLIFrameElement: function HTMLIFrameElement() {},
  139. HTMLElement: function HTMLElement() {},
  140. Node: function Node() {},
  141. removeEventListener: () => {},
  142. };
  143. Object.defineProperty(globalThis, "document", {
  144. configurable: true,
  145. value: documentMock,
  146. });
  147. Object.defineProperty(globalThis, "window", {
  148. configurable: true,
  149. value: windowMock,
  150. });
  151. Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
  152. configurable: true,
  153. value: true,
  154. });
  155. return createMinimalElement(documentMock);
  156. }
  157. const modelGroups: ModelSelectGroup[] = [
  158. {
  159. key: "codex",
  160. label: "Codex",
  161. items: [
  162. { key: "gpt-5", value: "gpt-5", name: "GPT-5" },
  163. { key: "gpt-5-mini", value: "gpt-5-mini", name: "GPT-5 Mini" },
  164. ],
  165. },
  166. ];
  167. describe("ModelConfigSelect render stability", () => {
  168. afterEach(() => {
  169. dropdownContentCalls.length = 0;
  170. dropdownItemCalls.length = 0;
  171. radioItemCalls.length = 0;
  172. subContentCalls.length = 0;
  173. subTriggerCalls.length = 0;
  174. delete (globalThis as { document?: unknown }).document;
  175. delete (globalThis as { window?: unknown }).window;
  176. delete (globalThis as { IS_REACT_ACT_ENVIRONMENT?: unknown })
  177. .IS_REACT_ACT_ENVIRONMENT;
  178. });
  179. it("defers model menu item rendering until the dropdown is open", async () => {
  180. const { ModelConfigSelect } = await import("@/ModelConfigSelect.js");
  181. const root: Root = createRoot(installMinimalDom());
  182. const baseProps = {
  183. focusSelectorOnClose: null,
  184. isItemLocked: () => false,
  185. lockReasonMessage: "locked",
  186. modelGroups,
  187. normalizedValue: "gpt-5",
  188. onValueChange: vi.fn(),
  189. showManageModelsAction: false,
  190. tooltipTitle: "model",
  191. triggerLabel: "GPT-5",
  192. };
  193. act(() => {
  194. root.render(createElement(ModelConfigSelect, baseProps));
  195. });
  196. expect(radioItemCalls).toHaveLength(0);
  197. await act(async () => {
  198. root.render(
  199. createElement(ModelConfigSelect, {
  200. ...baseProps,
  201. openRequestKey: 1,
  202. }),
  203. );
  204. });
  205. expect(radioItemCalls).toHaveLength(2);
  206. act(() => {
  207. root.unmount();
  208. });
  209. });
  210. it("renders a flat model allowlist without a provider submenu", async () => {
  211. const { ModelConfigSelect } = await import("@/ModelConfigSelect.js");
  212. const root: Root = createRoot(installMinimalDom());
  213. const baseProps = {
  214. focusSelectorOnClose: null,
  215. isItemLocked: () => false,
  216. lockReasonMessage: "",
  217. modelGroups,
  218. normalizedValue: "gpt-5",
  219. onValueChange: vi.fn(),
  220. showManageModelsAction: false,
  221. showProviderLevel: false,
  222. tooltipTitle: "model",
  223. triggerLabel: "GPT-5",
  224. };
  225. act(() => {
  226. root.render(createElement(ModelConfigSelect, baseProps));
  227. });
  228. await act(async () => {
  229. root.render(
  230. createElement(ModelConfigSelect, {
  231. ...baseProps,
  232. openRequestKey: 1,
  233. }),
  234. );
  235. });
  236. expect(radioItemCalls).toHaveLength(2);
  237. expect(subTriggerCalls).toHaveLength(0);
  238. const onCloseAutoFocus = dropdownContentCalls.at(-1)?.onCloseAutoFocus;
  239. expect(onCloseAutoFocus).toBeTypeOf("function");
  240. const preventDefault = vi.fn();
  241. (onCloseAutoFocus as (event: { preventDefault: () => void }) => void)({
  242. preventDefault,
  243. });
  244. expect(preventDefault).not.toHaveBeenCalled();
  245. act(() => {
  246. root.unmount();
  247. });
  248. });
  249. it("keeps the provider submenu interaction and accepts a narrower mobile width", async () => {
  250. const { ModelConfigSelect } = await import("@/ModelConfigSelect.js");
  251. const root: Root = createRoot(installMinimalDom());
  252. const baseProps = {
  253. focusSelectorOnClose: null,
  254. isItemLocked: () => false,
  255. lockReasonMessage: "",
  256. modelGroups,
  257. normalizedValue: "gpt-5",
  258. onValueChange: vi.fn(),
  259. providerSubmenuClassName:
  260. "w-40 min-w-0 max-w-(--radix-dropdown-menu-content-available-width)",
  261. showManageModelsAction: false,
  262. tooltipTitle: "model",
  263. triggerLabel: "GPT-5",
  264. };
  265. act(() => {
  266. root.render(createElement(ModelConfigSelect, baseProps));
  267. });
  268. await act(async () => {
  269. root.render(
  270. createElement(ModelConfigSelect, {
  271. ...baseProps,
  272. openRequestKey: 1,
  273. }),
  274. );
  275. });
  276. expect(radioItemCalls).toHaveLength(2);
  277. expect(subTriggerCalls).toHaveLength(1);
  278. expect(subContentCalls.at(-1)?.className).toContain("w-40");
  279. expect(subContentCalls.at(-1)?.className).toContain("min-w-0");
  280. expect(subContentCalls.at(-1)?.className).toContain(
  281. "max-w-(--radix-dropdown-menu-content-available-width)",
  282. );
  283. expect(subContentCalls.at(-1)?.className).not.toContain("w-48");
  284. act(() => {
  285. root.unmount();
  286. });
  287. });
  288. it("renders selected footer actions alongside manage models", async () => {
  289. const { ModelConfigSelect } = await import("@/ModelConfigSelect.js");
  290. const root: Root = createRoot(installMinimalDom());
  291. const onInherit = vi.fn();
  292. const onManageModels = vi.fn();
  293. act(() => {
  294. root.render(
  295. createElement(ModelConfigSelect, {
  296. focusSelectorOnClose: null,
  297. footerActions: [
  298. {
  299. key: "inherit",
  300. label: "Inherit default",
  301. onSelect: onInherit,
  302. selected: true,
  303. },
  304. ],
  305. isItemLocked: () => false,
  306. lockReasonMessage: "locked",
  307. manageModelsLabel: "Manage models",
  308. modelGroups,
  309. normalizedValue: "gpt-5",
  310. onManageModels,
  311. onValueChange: vi.fn(),
  312. showManageModelsAction: true,
  313. tooltipTitle: "model",
  314. triggerLabel: "GPT-5",
  315. }),
  316. );
  317. });
  318. await act(async () => {
  319. root.render(
  320. createElement(ModelConfigSelect, {
  321. focusSelectorOnClose: null,
  322. footerActions: [
  323. {
  324. key: "inherit",
  325. label: "Inherit default",
  326. onSelect: onInherit,
  327. selected: true,
  328. },
  329. ],
  330. isItemLocked: () => false,
  331. lockReasonMessage: "locked",
  332. manageModelsLabel: "Manage models",
  333. modelGroups,
  334. normalizedValue: "gpt-5",
  335. onManageModels,
  336. onValueChange: vi.fn(),
  337. openRequestKey: 1,
  338. showManageModelsAction: true,
  339. tooltipTitle: "model",
  340. triggerLabel: "GPT-5",
  341. }),
  342. );
  343. });
  344. expect(dropdownItemCalls).toEqual([
  345. expect.objectContaining({
  346. "data-model-footer-action": "inherit",
  347. "data-model-footer-action-selected": "true",
  348. }),
  349. expect.objectContaining({
  350. "data-model-footer-action": "manage-models",
  351. }),
  352. ]);
  353. act(() => {
  354. root.unmount();
  355. });
  356. });
  357. });