providerModelMetadataDialog.test.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. import { createElement, type ReactNode } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import { ProviderModelMetadataDialog } from "@/settings/model-provider-section/ProviderModelMetadataDialog.js";
  5. function extractInputTag(html: string, needle: string) {
  6. return html.match(/<input[^>]*>/g)?.find((tag) => tag.includes(needle)) ?? "";
  7. }
  8. function extractButtonOpeningTag(html: string, label: string) {
  9. return html.match(new RegExp(`<button[^>]*>${label}</button>`))?.[0].split(">", 1)[0] ?? "";
  10. }
  11. const MODEL_FORMAT_DRAFT = {
  12. reasoningLevelValuesValue: ["disabled"] as readonly string[],
  13. reasoningLevelMapValue: "",
  14. inputFormatValue: {
  15. supportsText: true,
  16. supportsImage: false,
  17. supportsVideo: false,
  18. supportsAudio: false,
  19. supportsPdf: false,
  20. },
  21. };
  22. vi.mock("@/components/ui/dialog.js", () => ({
  23. Dialog: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  24. DialogContent: ({
  25. children,
  26. className,
  27. ...props
  28. }: {
  29. children: ReactNode;
  30. className?: string;
  31. [key: string]: unknown;
  32. }) => createElement("section", { className, ...props }, children),
  33. DialogDescription: ({ children, className }: { children: ReactNode; className?: string }) =>
  34. createElement("p", { className }, children),
  35. DialogFooter: ({ children, className }: { children: ReactNode; className?: string }) =>
  36. createElement("footer", { className }, children),
  37. DialogHeader: ({ children, className }: { children: ReactNode; className?: string }) =>
  38. createElement("header", { className }, children),
  39. DialogTitle: ({ children, className }: { children: ReactNode; className?: string }) =>
  40. createElement("h2", { className }, children),
  41. DialogTrigger: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  42. }));
  43. vi.mock("@/components/ui/popover.js", () => ({
  44. Popover: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  45. PopoverContent: ({ children, className }: { children: ReactNode; className?: string }) =>
  46. createElement("div", { className }, children),
  47. PopoverTrigger: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  48. }));
  49. vi.mock("@/i18n/IntlProvider.js", () => ({
  50. useZCodeIntl: () => ({
  51. intl: {
  52. formatMessage: ({ id }: { id: string }) => {
  53. const messages: Record<string, string> = {
  54. "common.cancel": "取消",
  55. "common.loading": "加载中...",
  56. "common.save": "保存",
  57. "settings.modelProvider.resetForm": "重置表单",
  58. "settings.modelProvider.followRecommendedConfig": "跟随推荐配置",
  59. "settings.modelProvider.addModel": "添加模型",
  60. "settings.modelProvider.apiFormat": "API 格式",
  61. "settings.modelProvider.apiFormat.anthropicMessages":
  62. "Anthropic Messages (/anthropic/v1/messages)",
  63. "settings.modelProvider.apiFormat.chatCompletions":
  64. "Chat Completions (/v1/chat/completions)",
  65. "settings.modelProvider.apiFormat.responses": "Responses (/responses)",
  66. "settings.modelProvider.apiFormat.short.anthropicMessages": "Anthropic",
  67. "settings.modelProvider.apiFormat.short.chatCompletions": "Chat",
  68. "settings.modelProvider.apiFormat.short.responses": "Responses",
  69. "settings.modelProvider.apiFormat.title.anthropicMessages": "Anthropic Messages",
  70. "settings.modelProvider.apiFormat.title.chatCompletions": "Chat Completions",
  71. "settings.modelProvider.apiFormat.title.responses": "Responses",
  72. "settings.modelProvider.contextWindow": "上下文窗口",
  73. "settings.modelProvider.capabilities": "模型能力",
  74. "settings.modelProvider.enableModel": "启用",
  75. "settings.modelProvider.advancedConfig": "高级配置",
  76. "settings.modelProvider.reasoning": "推理设置",
  77. "settings.modelProvider.editModel": "编辑模型配置",
  78. "settings.modelProvider.editModelDescription": "编辑模型配置描述",
  79. "settings.modelProvider.maxOutputTokens": "最大输出 Token",
  80. "settings.modelProvider.modelId": "模型 ID",
  81. "settings.modelProvider.inputModalities": "输入类型",
  82. "settings.modelProvider.outputModalities": "输出类型",
  83. "settings.modelProvider.otherSettings": "其他设置",
  84. "settings.modelProvider.modality.text": "Text",
  85. "settings.modelProvider.modality.image": "Image",
  86. "settings.modelProvider.modality.video": "Video",
  87. "settings.modelProvider.modality.audio": "Audio",
  88. "settings.modelProvider.modality.pdf": "PDF",
  89. "settings.modelProvider.modelMetadata.invalid.id": "模型 ID 不能为空",
  90. "settings.modelProvider.modelMetadata.invalid.kinds": "至少选择一种 API 格式",
  91. };
  92. return messages[id] ?? id;
  93. },
  94. },
  95. }),
  96. }));
  97. describe("ProviderModelMetadataDialog", () => {
  98. it.each([true, false])("Todo134:模型 ID 只读权限不随布局改变:%s", (modelIdReadOnly) => {
  99. const html = renderToStaticMarkup(
  100. createElement(ProviderModelMetadataDialog, {
  101. open: true,
  102. modelIdReadOnly,
  103. draft: {
  104. ...MODEL_FORMAT_DRAFT,
  105. idValue: "builtin-model",
  106. contextWindowValue: "",
  107. maxOutputTokensValue: "",
  108. },
  109. draftErrorMessage: null,
  110. onOpenChange: vi.fn(),
  111. onDraftChange: vi.fn(),
  112. onCommit: () => true,
  113. }),
  114. );
  115. expect(extractInputTag(html, 'value="builtin-model"').includes('readOnly=""')).toBe(
  116. modelIdReadOnly,
  117. );
  118. expect(html).toContain('aria-expanded="false"');
  119. });
  120. it.each(["", "unknown"])("Todo130:ID=%s 无推荐/解析中/失败都能切换,只有保存禁用", (idValue) => {
  121. for (const pending of [false, true]) {
  122. for (const saving of [false, true]) {
  123. const html = renderToStaticMarkup(
  124. createElement(ProviderModelMetadataDialog, {
  125. mode: "add",
  126. open: true,
  127. draft: {
  128. ...MODEL_FORMAT_DRAFT,
  129. idValue,
  130. contextWindowValue: "",
  131. maxOutputTokensValue: "",
  132. },
  133. draftErrorMessage: "failed",
  134. modelConfigResolutionPending: pending,
  135. saving,
  136. onOpenChange: vi.fn(),
  137. onDraftChange: vi.fn(),
  138. onCommit: vi.fn(() => true),
  139. }),
  140. );
  141. const toggle = html.match(/<button[^>]*role="switch"[^>]*>/)?.[0];
  142. expect(toggle).toBeDefined();
  143. expect(toggle?.includes("disabled=")).toBe(saving);
  144. }
  145. }
  146. });
  147. it("Todo92:无效字段不影响独立覆盖标记,固定模式全部关闭标记", () => {
  148. const props = {
  149. open: true,
  150. draft: {
  151. ...MODEL_FORMAT_DRAFT,
  152. idValue: "model",
  153. contextWindowValue: "bad",
  154. maxOutputTokensValue: "",
  155. reasoningLevelMapValue: "bad (",
  156. },
  157. overrideFields: new Set([
  158. "contextWindowValue",
  159. "reasoningLevelValuesValue",
  160. "reasoningLevelMapValue",
  161. ]),
  162. draftErrorMessage: "invalid",
  163. onOpenChange: () => {},
  164. onDraftChange: () => {},
  165. onCommit: () => true,
  166. };
  167. const html = renderToStaticMarkup(createElement(ProviderModelMetadataDialog, props));
  168. expect(html).toContain('data-model-reasoning-chip="true" data-personal-override="true"');
  169. expect(extractInputTag(html, 'value="bad"')).toContain('data-personal-override="true"');
  170. const fixed = renderToStaticMarkup(
  171. createElement(ProviderModelMetadataDialog, {
  172. ...props,
  173. draft: { ...props.draft, useRecommendedConfigValue: false },
  174. }),
  175. );
  176. expect(fixed).not.toContain('data-personal-override="true"');
  177. });
  178. it("继承值只作为输入 placeholder,Reasoning 使用紧凑有序标签", () => {
  179. const html = renderToStaticMarkup(
  180. createElement(ProviderModelMetadataDialog, {
  181. open: true,
  182. draft: {
  183. ...MODEL_FORMAT_DRAFT,
  184. idValue: "deepseek-v4-flash",
  185. nameValue: "",
  186. kindsValue: ["openai-compatible"],
  187. contextWindowValue: "",
  188. maxOutputTokensValue: "",
  189. reasoningLevelValuesValue: ["low", "high"],
  190. },
  191. draftErrorMessage: null,
  192. personalConfig: {
  193. properties: {
  194. contextWindow: 1_200_000,
  195. supportsToolCall: false,
  196. inputFormat: { supportsImage: true },
  197. },
  198. },
  199. inheritedConfig: {
  200. properties: {
  201. contextWindow: 128_000,
  202. supportsToolCall: true,
  203. inputFormat: { supportsImage: false },
  204. },
  205. optionSpecs: {
  206. reasoningLevel: {
  207. values: ["low", "high"],
  208. map: "reasoningLevel == 'low'\n ? {'reasoning_effort': 'low'}\n : {'reasoning_effort': 'high'}",
  209. },
  210. },
  211. },
  212. onOpenChange: vi.fn(),
  213. onDraftChange: vi.fn(),
  214. onCommit: vi.fn(() => true),
  215. }),
  216. );
  217. expect(html).toContain('inputMode="numeric"');
  218. expect(html).not.toContain('type="number"');
  219. expect(html).toContain("<textarea");
  220. expect(html).toContain('data-model-json-slot="true"');
  221. expect(html).toContain(
  222. 'placeholder="reasoningLevel == &#x27;low&#x27;\n ? {&#x27;reasoning_effort&#x27;: &#x27;low&#x27;}\n : {&#x27;reasoning_effort&#x27;: &#x27;high&#x27;}"',
  223. );
  224. expect(html).toContain('data-model-reasoning-level-editor="true"');
  225. expect(html).toContain(">low<");
  226. expect(html).toContain(">high<");
  227. expect(html).toContain("高级配置");
  228. const reasoningGroup =
  229. html.match(/<section[^>]*data-model-settings-group="reasoning"[\s\S]*?<\/section>/)?.[0] ??
  230. "";
  231. const advancedGroup = html.slice(html.indexOf('data-model-advanced="true"'));
  232. expect(reasoningGroup).not.toContain(">推理设置<");
  233. expect(reasoningGroup).toContain('data-model-reasoning-level-editor="true"');
  234. expect(reasoningGroup).toContain('data-model-reasoning-level-map-editor="true"');
  235. expect(advancedGroup).toContain('data-model-reasoning-level-map-editor="true"');
  236. expect(reasoningGroup).not.toContain("settings.modelProvider.requiresMfjsToolSchema");
  237. expect(advancedGroup).not.toContain("settings.modelProvider.requiresMfjsToolSchema");
  238. expect(html).not.toContain('data-model-settings-advanced-trigger="true"');
  239. expect(html).toContain("settings.modelProvider.reasoningLevelsOrdered");
  240. expect(html).not.toContain("settings.modelProvider.requiresMfjsToolSchema");
  241. expect(html).not.toContain("settings.modelProvider.maxOutputTokensOptionSpecJson");
  242. expect(html).not.toContain("data-effective-json-preview");
  243. expect(html).not.toContain("settings.modelProvider.effectiveJsonPreview");
  244. expect(html).toContain('placeholder="128000"');
  245. expect(html).toContain('data-personal-override="true"');
  246. expect(html).not.toContain('aria-label="settings.modelProvider.restoreInheritedValue"');
  247. expect(extractInputTag(html, 'value="deepseek-v4-flash"')).toContain('spellCheck="false"');
  248. });
  249. it("编辑弹窗展示继承的最大输出、推荐配置与重置控件", () => {
  250. const html = renderToStaticMarkup(
  251. createElement(ProviderModelMetadataDialog, {
  252. mode: "edit",
  253. open: true,
  254. draft: {
  255. ...MODEL_FORMAT_DRAFT,
  256. idValue: "demo-model",
  257. contextWindowValue: "",
  258. maxOutputTokensValue: "",
  259. },
  260. draftErrorMessage: null,
  261. inheritedConfig: {
  262. properties: { contextWindow: 128_000 },
  263. optionSpecs: { maxOutputTokens: { max: 32_000 } },
  264. },
  265. personalConfig: { enabled: false },
  266. onOpenChange: vi.fn(),
  267. onDraftChange: vi.fn(),
  268. onCommit: vi.fn(() => true),
  269. }),
  270. );
  271. expect(html).toContain("高级配置");
  272. expect(html).not.toContain('data-model-settings-advanced-trigger="true"');
  273. expect(html).toContain('data-model-advanced="true"');
  274. expect(html).toContain('data-model-reasoning-level-editor="true"');
  275. expect(html).toContain('data-model-settings-scroll="true"');
  276. expect(extractInputTag(html, 'aria-label="最大输出 Token"')).toContain('placeholder="32000"');
  277. expect(html).toContain('data-model-recommended-config="true"');
  278. expect(html).toContain(">跟随推荐配置<");
  279. const footer = html.slice(html.indexOf('data-model-settings-footer="true"'));
  280. expect(footer).toContain(">重置表单<");
  281. expect(footer).toContain(">保存<");
  282. expect(footer).toContain(">取消<");
  283. });
  284. it("模态与可编辑模型能力使用复选框选项,工具调用不再作为设置项", () => {
  285. const html = renderToStaticMarkup(
  286. createElement(ProviderModelMetadataDialog, {
  287. mode: "edit",
  288. open: true,
  289. draft: {
  290. ...MODEL_FORMAT_DRAFT,
  291. idValue: "demo-model",
  292. contextWindowValue: "",
  293. maxOutputTokensValue: "",
  294. },
  295. draftErrorMessage: null,
  296. personalConfig: {
  297. properties: {
  298. inputFormat: { supportsImage: false },
  299. supportsToolCall: true,
  300. },
  301. },
  302. inheritedConfig: {
  303. properties: {
  304. contextWindow: 128_000,
  305. inputFormat: { supportsImage: false },
  306. },
  307. },
  308. onOpenChange: vi.fn(),
  309. onDraftChange: vi.fn(),
  310. onCommit: vi.fn(() => true),
  311. }),
  312. );
  313. const options = html.match(/<button[^>]*data-model-boolean-option="true"[^>]*>/g) ?? [];
  314. expect(options).toHaveLength(3);
  315. const capabilityOptions =
  316. html.match(/<button[^>]*data-model-capability-option="true"[^>]*>/g) ?? [];
  317. expect(capabilityOptions).toHaveLength(3);
  318. expect(capabilityOptions.every((option) => option.includes('role="checkbox"'))).toBe(true);
  319. expect(capabilityOptions.every((option) => option.includes("border-border"))).toBe(true);
  320. expect(capabilityOptions.every((option) => option.includes("bg-transparent"))).toBe(true);
  321. expect(
  322. capabilityOptions.every(
  323. (option) => option.includes("hover:bg-hover") && option.includes("focus-visible:bg-hover"),
  324. ),
  325. ).toBe(true);
  326. expect(html).not.toContain('data-model-capability-icon="true"');
  327. expect(html).not.toContain("settings.modelProvider.supportsToolCall");
  328. expect(html.match(/data-model-option-checkbox="true"/g)).toHaveLength(7);
  329. expect(html).toContain("data-model-option-checkbox");
  330. const overriddenModality =
  331. html.match(
  332. /<button[^>]*data-personal-override="true"[^>]*data-model-input-modality="image"[^>]*>/,
  333. )?.[0] ?? "";
  334. expect(overriddenModality).not.toBe("");
  335. expect(overriddenModality).toContain("border-primary/35");
  336. const selectedModalities =
  337. html.match(
  338. /<button[^>]*data-selected="true"[^>]*data-model-(?:input|output)-modality[^>]*>/g,
  339. ) ?? [];
  340. expect(selectedModalities).toHaveLength(1);
  341. expect(selectedModalities.every((option) => option.includes("bg-foreground/15"))).toBe(true);
  342. expect(html).not.toContain(">默认<");
  343. expect(html).not.toContain(">已修改<");
  344. });
  345. it("模型后缀不会覆盖传入的上下文配置", () => {
  346. const html = renderToStaticMarkup(
  347. createElement(ProviderModelMetadataDialog, {
  348. open: true,
  349. draft: {
  350. ...MODEL_FORMAT_DRAFT,
  351. idValue: "deepseek-v4-pro[1m]",
  352. nameValue: "",
  353. kindsValue: ["openai-compatible"],
  354. contextWindowValue: "64000",
  355. maxOutputTokensValue: "",
  356. },
  357. draftErrorMessage: null,
  358. onOpenChange: vi.fn(),
  359. onDraftChange: vi.fn(),
  360. onCommit: vi.fn(() => true),
  361. }),
  362. );
  363. const contextWindowInput = extractInputTag(html, 'inputMode="numeric"');
  364. expect(contextWindowInput).not.toContain("readOnly");
  365. expect(contextWindowInput).toContain('value="64000"');
  366. });
  367. it("编辑带后缀模型时仍聚焦上下文窗口", () => {
  368. const html = renderToStaticMarkup(
  369. createElement(ProviderModelMetadataDialog, {
  370. mode: "edit",
  371. open: true,
  372. draft: {
  373. ...MODEL_FORMAT_DRAFT,
  374. idValue: "deepseek-v4-pro[1m]",
  375. nameValue: "",
  376. kindsValue: ["openai-compatible"],
  377. contextWindowValue: "64000",
  378. maxOutputTokensValue: "",
  379. },
  380. draftErrorMessage: null,
  381. onOpenChange: vi.fn(),
  382. onDraftChange: vi.fn(),
  383. onCommit: vi.fn(() => true),
  384. }),
  385. );
  386. const modelIdInput = extractInputTag(html, 'placeholder="模型 ID"');
  387. const contextWindowInput = extractInputTag(html, 'inputMode="numeric"');
  388. expect(modelIdInput).not.toContain("autofocus");
  389. expect(contextWindowInput).toContain('autofocus=""');
  390. });
  391. it("打开弹窗默认聚焦上下文窗口", () => {
  392. const html = renderToStaticMarkup(
  393. createElement(ProviderModelMetadataDialog, {
  394. mode: "edit",
  395. open: true,
  396. draft: {
  397. ...MODEL_FORMAT_DRAFT,
  398. idValue: "deepseek-v4-flash",
  399. nameValue: "",
  400. kindsValue: ["openai-compatible"],
  401. contextWindowValue: "128000",
  402. maxOutputTokensValue: "",
  403. },
  404. draftErrorMessage: null,
  405. onOpenChange: vi.fn(),
  406. onDraftChange: vi.fn(),
  407. onCommit: vi.fn(() => true),
  408. }),
  409. );
  410. expect(html).not.toMatch(/placeholder="模型 ID"[^>]*autofocus/);
  411. expect(html).toMatch(/<input[^>]*autofocus[^>]*inputMode="numeric"[^>]*>/);
  412. });
  413. it("添加模型弹窗展示推理与高级分组、禁用必选 Text 并使用复选框模态选项", () => {
  414. const html = renderToStaticMarkup(
  415. createElement(ProviderModelMetadataDialog, {
  416. mode: "add",
  417. open: true,
  418. draft: {
  419. ...MODEL_FORMAT_DRAFT,
  420. idValue: "",
  421. nameValue: "",
  422. kindsValue: ["anthropic"],
  423. contextWindowValue: "1000000",
  424. maxOutputTokensValue: "128000",
  425. inputFormatValue: {
  426. supportsText: true,
  427. supportsImage: false,
  428. supportsVideo: false,
  429. supportsAudio: false,
  430. supportsPdf: false,
  431. },
  432. modalitiesTouched: true,
  433. },
  434. draftErrorMessage: null,
  435. onOpenChange: vi.fn(),
  436. onDraftChange: vi.fn(),
  437. onCommit: vi.fn(() => true),
  438. }),
  439. );
  440. expect(html).toContain("添加模型");
  441. expect(html).toContain("模型 ID");
  442. expect(html).not.toContain("API 格式");
  443. expect(html).not.toContain(">/anthropic/v1/messages<");
  444. expect(html).not.toContain(">/v1/chat/completions<");
  445. expect(html).not.toContain("Responses (/responses)");
  446. expect(html).toContain("上下文窗口");
  447. expect(html).toContain("最大输出 Token");
  448. expect(html).toContain("输入类型");
  449. expect(html).not.toContain('data-model-input-modalities-description="true"');
  450. expect(html).not.toContain("暂不支持作为附件");
  451. expect(html).not.toContain("输出类型");
  452. expect(html).toContain("高级配置");
  453. expect(html).not.toContain('data-model-settings-advanced-trigger="true"');
  454. expect(html).toContain('data-model-advanced="true"');
  455. expect(html).toContain('data-model-reasoning-level-editor="true"');
  456. expect(html).toContain('data-model-input-modality="text"');
  457. expect(html).toContain('data-model-input-modality="image"');
  458. expect(html).toContain('data-model-input-modality="video"');
  459. expect(html).toContain('data-model-input-modality="pdf"');
  460. expect(html).not.toContain('data-model-input-modality="audio"');
  461. expect(html).not.toContain('data-model-output-modality="text"');
  462. expect(html).not.toContain('data-model-modality-icon="true"');
  463. expect(html.match(/data-model-modality-lock="true"/g)).toHaveLength(1);
  464. const modalityButtons =
  465. html.match(/<button[^>]*data-model-(?:input|output)-modality=[^>]*>/g) ?? [];
  466. expect(modalityButtons).toHaveLength(4);
  467. expect(
  468. modalityButtons.find((button) => button.includes('data-model-input-modality="image"')),
  469. ).toContain('aria-pressed="false"');
  470. expect(
  471. modalityButtons.find((button) => button.includes('data-model-input-modality="text"')),
  472. ).toContain('aria-pressed="true"');
  473. expect(
  474. modalityButtons.find((button) => button.includes('data-model-input-modality="text"')),
  475. ).toContain("bg-foreground/15");
  476. expect(
  477. modalityButtons.find((button) => button.includes('data-model-input-modality="text"')),
  478. ).toContain("border-border");
  479. expect(
  480. modalityButtons.find((button) => button.includes('data-model-input-modality="text"')),
  481. ).toContain("bg-clip-border");
  482. expect(
  483. modalityButtons.find((button) => button.includes('data-model-input-modality="text"')),
  484. ).toContain("hover:bg-foreground/20");
  485. expect(
  486. modalityButtons.find((button) => button.includes('data-model-input-modality="image"')),
  487. ).toContain("bg-transparent");
  488. expect(
  489. modalityButtons.find((button) => button.includes('data-model-input-modality="image"')),
  490. ).toContain("hover:bg-hover");
  491. expect(
  492. modalityButtons.find((button) => button.includes('data-model-input-modality="text"')),
  493. ).toContain('disabled=""');
  494. expect(html.match(/lucide-lock-keyhole/g)).toHaveLength(1);
  495. expect(html).toContain('value="1000000"');
  496. expect(html).not.toContain("settings.modelProvider.reasoningLevelOptionSpecJson");
  497. expect(html).toContain('data-model-settings-footer="true"');
  498. expect(html).toContain(">取消<");
  499. expect(html).toContain(">保存<");
  500. expect(html.match(/添加模型/g)).toHaveLength(1);
  501. });
  502. it("解析模型配置时展示 loading,并允许保存按钮主动等待当前解析", () => {
  503. const html = renderToStaticMarkup(
  504. createElement(ProviderModelMetadataDialog, {
  505. mode: "add",
  506. open: true,
  507. modelConfigResolutionPending: true,
  508. draft: {
  509. ...MODEL_FORMAT_DRAFT,
  510. idValue: "glm-5",
  511. nameValue: "",
  512. kindsValue: ["anthropic"],
  513. contextWindowValue: "1000000",
  514. maxOutputTokensValue: "128000",
  515. },
  516. draftErrorMessage: null,
  517. onOpenChange: vi.fn(),
  518. onDraftChange: vi.fn(),
  519. onCommit: vi.fn(() => true),
  520. }),
  521. );
  522. expect(html).toContain("加载中...");
  523. expect(html).toContain("animate-spin");
  524. expect(extractInputTag(html, 'aria-label="最大输出 Token"')).not.toMatch(
  525. /\sdisabled(?:=|\s|\/|>)/,
  526. );
  527. expect(extractButtonOpeningTag(html, "保存")).not.toContain(' disabled=""');
  528. });
  529. it("添加模型弹窗默认聚焦模型 ID,不自动聚焦上下文窗口", () => {
  530. const html = renderToStaticMarkup(
  531. createElement(ProviderModelMetadataDialog, {
  532. mode: "add",
  533. open: true,
  534. draft: {
  535. ...MODEL_FORMAT_DRAFT,
  536. idValue: "",
  537. nameValue: "",
  538. kindsValue: ["anthropic"],
  539. contextWindowValue: "128000",
  540. maxOutputTokensValue: "",
  541. },
  542. draftErrorMessage: null,
  543. onOpenChange: vi.fn(),
  544. onDraftChange: vi.fn(),
  545. onCommit: vi.fn(() => true),
  546. }),
  547. );
  548. const modelIdInput = extractInputTag(html, 'placeholder="模型 ID"');
  549. const contextWindowInput = extractInputTag(html, 'inputMode="numeric"');
  550. expect(modelIdInput).toContain('autofocus=""');
  551. expect(contextWindowInput).not.toContain("autofocus");
  552. });
  553. });