providerModelMetadataDialog.test.ts 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. import { createElement, type ReactNode } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import {
  5. ProviderModelMetadataDialog,
  6. selectFocusedInputText,
  7. } from "@/settings/model-provider-section/ProviderModelMetadataDialog.js";
  8. function extractInputTag(html: string, needle: string) {
  9. return html.match(/<input[^>]*>/g)?.find((tag) => tag.includes(needle)) ?? "";
  10. }
  11. function extractButtonOpeningTag(html: string, label: string) {
  12. return html.match(new RegExp(`<button[^>]*>${label}</button>`))?.[0].split(">", 1)[0] ?? "";
  13. }
  14. const MODEL_FORMAT_DRAFT = {
  15. reasoningLevelValuesValue: ["disabled"] as readonly string[],
  16. reasoningLevelMapValue: "",
  17. inputFormatValue: {
  18. supportsText: true,
  19. supportsImage: false,
  20. supportsVideo: false,
  21. supportsAudio: false,
  22. supportsPdf: false,
  23. },
  24. };
  25. vi.mock("@/components/ui/dialog.js", () => ({
  26. Dialog: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  27. DialogContent: ({
  28. children,
  29. className,
  30. ...props
  31. }: {
  32. children: ReactNode;
  33. className?: string;
  34. [key: string]: unknown;
  35. }) => createElement("section", { className, ...props }, children),
  36. DialogDescription: ({ children, className }: { children: ReactNode; className?: string }) =>
  37. createElement("p", { className }, children),
  38. DialogFooter: ({ children, className }: { children: ReactNode; className?: string }) =>
  39. createElement("footer", { className }, children),
  40. DialogHeader: ({ children, className }: { children: ReactNode; className?: string }) =>
  41. createElement("header", { className }, children),
  42. DialogTitle: ({ children, className }: { children: ReactNode; className?: string }) =>
  43. createElement("h2", { className }, children),
  44. DialogTrigger: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  45. }));
  46. vi.mock("@/components/ui/popover.js", () => ({
  47. Popover: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  48. PopoverContent: ({ children, className }: { children: ReactNode; className?: string }) =>
  49. createElement("div", { className }, children),
  50. PopoverTrigger: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  51. }));
  52. vi.mock("@/i18n/IntlProvider.js", () => ({
  53. useZCodeIntl: () => ({
  54. intl: {
  55. formatMessage: ({ id }: { id: string }) => {
  56. const messages: Record<string, string> = {
  57. "common.cancel": "取消",
  58. "common.loading": "加载中...",
  59. "common.save": "保存",
  60. "settings.modelProvider.resetForm": "重置表单",
  61. "settings.modelProvider.followRecommendedConfig": "跟随推荐配置",
  62. "settings.modelProvider.addModel": "添加模型",
  63. "settings.modelProvider.apiFormat": "API 格式",
  64. "settings.modelProvider.apiFormat.anthropicMessages":
  65. "Anthropic Messages (/anthropic/v1/messages)",
  66. "settings.modelProvider.apiFormat.chatCompletions":
  67. "Chat Completions (/v1/chat/completions)",
  68. "settings.modelProvider.apiFormat.responses": "Responses (/responses)",
  69. "settings.modelProvider.apiFormat.short.anthropicMessages": "Anthropic",
  70. "settings.modelProvider.apiFormat.short.chatCompletions": "Chat",
  71. "settings.modelProvider.apiFormat.short.responses": "Responses",
  72. "settings.modelProvider.apiFormat.title.anthropicMessages": "Anthropic Messages",
  73. "settings.modelProvider.apiFormat.title.chatCompletions": "Chat Completions",
  74. "settings.modelProvider.apiFormat.title.responses": "Responses",
  75. "settings.modelProvider.contextWindow": "上下文窗口",
  76. "settings.modelProvider.capabilities": "模型能力",
  77. "settings.modelProvider.enableModel": "启用",
  78. "settings.modelProvider.advancedConfig": "高级配置",
  79. "settings.modelProvider.reasoning": "推理设置",
  80. "settings.modelProvider.editModel": "编辑模型配置",
  81. "settings.modelProvider.editModelDescription": "编辑模型配置描述",
  82. "settings.modelProvider.maxOutputTokens": "最大输出 Token",
  83. "settings.modelProvider.modelId": "模型 ID",
  84. "settings.modelProvider.inputModalities": "输入类型",
  85. "settings.modelProvider.outputModalities": "输出类型",
  86. "settings.modelProvider.otherSettings": "其他设置",
  87. "settings.modelProvider.modality.text": "Text",
  88. "settings.modelProvider.modality.image": "Image",
  89. "settings.modelProvider.modality.video": "Video",
  90. "settings.modelProvider.modality.audio": "Audio",
  91. "settings.modelProvider.modality.pdf": "PDF",
  92. "settings.modelProvider.modelMetadata.invalid.id": "模型 ID 不能为空",
  93. "settings.modelProvider.modelMetadata.invalid.kinds": "至少选择一种 API 格式",
  94. };
  95. return messages[id] ?? id;
  96. },
  97. },
  98. }),
  99. }));
  100. describe("ProviderModelMetadataDialog", () => {
  101. it.each([true, false])("Todo134:模型 ID 只读权限不随布局改变:%s", (modelIdReadOnly) => {
  102. const html = renderToStaticMarkup(
  103. createElement(ProviderModelMetadataDialog, {
  104. open: true,
  105. modelIdReadOnly,
  106. draft: {
  107. ...MODEL_FORMAT_DRAFT,
  108. idValue: "builtin-model",
  109. contextWindowValue: "",
  110. maxOutputTokensValue: "",
  111. },
  112. draftErrorMessage: null,
  113. onOpenChange: vi.fn(),
  114. onDraftChange: vi.fn(),
  115. onCommit: () => true,
  116. }),
  117. );
  118. expect(extractInputTag(html, 'value="builtin-model"').includes('readOnly=""')).toBe(
  119. modelIdReadOnly,
  120. );
  121. expect(html).toContain('aria-expanded="false"');
  122. });
  123. it.each(["", "unknown"])("Todo130:ID=%s 无推荐/解析中/失败都能切换,只有保存禁用", (idValue) => {
  124. for (const pending of [false, true]) {
  125. for (const saving of [false, true]) {
  126. const html = renderToStaticMarkup(
  127. createElement(ProviderModelMetadataDialog, {
  128. mode: "add",
  129. open: true,
  130. draft: {
  131. ...MODEL_FORMAT_DRAFT,
  132. idValue,
  133. contextWindowValue: "",
  134. maxOutputTokensValue: "",
  135. },
  136. draftErrorMessage: "failed",
  137. modelConfigResolutionPending: pending,
  138. saving,
  139. onOpenChange: vi.fn(),
  140. onDraftChange: vi.fn(),
  141. onCommit: vi.fn(() => true),
  142. }),
  143. );
  144. const toggle = html.match(/<button[^>]*role="switch"[^>]*>/)?.[0];
  145. expect(toggle).toBeDefined();
  146. expect(toggle?.includes("disabled=")).toBe(saving);
  147. }
  148. }
  149. });
  150. it("Todo134:添加与编辑共用 footer 智能配置,布尔选项使用复选框", () => {
  151. const html = renderToStaticMarkup(
  152. createElement(ProviderModelMetadataDialog, {
  153. mode: "add",
  154. open: true,
  155. draft: {
  156. ...MODEL_FORMAT_DRAFT,
  157. idValue: "model",
  158. contextWindowValue: "",
  159. maxOutputTokensValue: "",
  160. },
  161. inheritedConfig: { properties: { contextWindow: 100000 } },
  162. draftErrorMessage: null,
  163. onOpenChange: () => {},
  164. onDraftChange: () => {},
  165. onCommit: () => true,
  166. }),
  167. );
  168. expect(html).toContain('data-model-recommended-config="true"');
  169. expect(html).toContain("data-model-option-checkbox");
  170. expect(html).not.toContain(">推理设置<");
  171. });
  172. it("Todo92:无效字段不影响独立覆盖标记,固定模式全部关闭标记", () => {
  173. const props = {
  174. open: true,
  175. draft: {
  176. ...MODEL_FORMAT_DRAFT,
  177. idValue: "model",
  178. contextWindowValue: "bad",
  179. maxOutputTokensValue: "",
  180. reasoningLevelMapValue: "bad (",
  181. },
  182. overrideFields: new Set([
  183. "contextWindowValue",
  184. "reasoningLevelValuesValue",
  185. "reasoningLevelMapValue",
  186. ]),
  187. draftErrorMessage: "invalid",
  188. onOpenChange: () => {},
  189. onDraftChange: () => {},
  190. onCommit: () => true,
  191. };
  192. const html = renderToStaticMarkup(createElement(ProviderModelMetadataDialog, props));
  193. expect(html).toContain('data-model-reasoning-chip="true" data-personal-override="true"');
  194. expect(extractInputTag(html, 'value="bad"')).toContain('data-personal-override="true"');
  195. const fixed = renderToStaticMarkup(
  196. createElement(ProviderModelMetadataDialog, {
  197. ...props,
  198. draft: { ...props.draft, useRecommendedConfigValue: false },
  199. }),
  200. );
  201. expect(fixed).not.toContain('data-personal-override="true"');
  202. });
  203. it("继承值只作为输入 placeholder,Reasoning 使用紧凑有序标签", () => {
  204. const html = renderToStaticMarkup(
  205. createElement(ProviderModelMetadataDialog, {
  206. open: true,
  207. draft: {
  208. ...MODEL_FORMAT_DRAFT,
  209. idValue: "deepseek-v4-flash",
  210. nameValue: "",
  211. kindsValue: ["openai-compatible"],
  212. contextWindowValue: "",
  213. maxOutputTokensValue: "",
  214. reasoningLevelValuesValue: ["low", "high"],
  215. },
  216. draftErrorMessage: null,
  217. personalConfig: {
  218. properties: {
  219. contextWindow: 1_200_000,
  220. supportsToolCall: false,
  221. inputFormat: { supportsImage: true },
  222. },
  223. },
  224. inheritedConfig: {
  225. properties: {
  226. contextWindow: 128_000,
  227. supportsToolCall: true,
  228. inputFormat: { supportsImage: false },
  229. },
  230. optionSpecs: {
  231. reasoningLevel: {
  232. values: ["low", "high"],
  233. map: "reasoningLevel == 'low'\n ? {'reasoning_effort': 'low'}\n : {'reasoning_effort': 'high'}",
  234. },
  235. },
  236. },
  237. onOpenChange: vi.fn(),
  238. onDraftChange: vi.fn(),
  239. onCommit: vi.fn(() => true),
  240. }),
  241. );
  242. expect(html).toContain('inputMode="numeric"');
  243. expect(html).not.toContain('type="number"');
  244. expect(html).toContain("<textarea");
  245. expect(html).toContain('data-model-json-slot="true"');
  246. expect(html).toContain(
  247. 'placeholder="reasoningLevel == &#x27;low&#x27;\n ? {&#x27;reasoning_effort&#x27;: &#x27;low&#x27;}\n : {&#x27;reasoning_effort&#x27;: &#x27;high&#x27;}"',
  248. );
  249. expect(html).toContain('data-model-reasoning-level-editor="true"');
  250. const reasoningLevelAddButton =
  251. html.match(/<button[^>]*data-model-reasoning-level-add="true"[^>]*>/)?.[0] ?? "";
  252. expect(reasoningLevelAddButton).toContain('data-variant="outline"');
  253. expect(reasoningLevelAddButton).toContain('data-size="icon-lg"');
  254. expect(html).toContain(">low<");
  255. expect(html).toContain(">high<");
  256. expect(html).toContain("高级配置");
  257. const reasoningGroup =
  258. html.match(/<section[^>]*data-model-settings-group="reasoning"[\s\S]*?<\/section>/)?.[0] ??
  259. "";
  260. const advancedGroup = html.slice(html.indexOf('data-model-advanced="true"'));
  261. expect(reasoningGroup).not.toContain(">推理设置<");
  262. expect(reasoningGroup).toContain('data-model-reasoning-level-editor="true"');
  263. expect(reasoningGroup).toContain('data-model-reasoning-level-map-editor="true"');
  264. expect(advancedGroup).toContain('data-model-reasoning-level-map-editor="true"');
  265. expect(reasoningGroup).not.toContain("settings.modelProvider.requiresMfjsToolSchema");
  266. expect(advancedGroup).not.toContain("settings.modelProvider.requiresMfjsToolSchema");
  267. expect(html).not.toContain('data-model-settings-advanced-trigger="true"');
  268. expect(html).toContain("settings.modelProvider.reasoningLevelsOrdered");
  269. expect(html).not.toContain("settings.modelProvider.requiresMfjsToolSchema");
  270. expect(html).not.toContain("settings.modelProvider.maxOutputTokensOptionSpecJson");
  271. expect(html).not.toContain("data-effective-json-preview");
  272. expect(html).not.toContain("settings.modelProvider.effectiveJsonPreview");
  273. expect(html).toContain('placeholder="128000"');
  274. expect(html).toContain('data-personal-override="true"');
  275. expect(html).not.toContain('aria-label="settings.modelProvider.restoreInheritedValue"');
  276. expect(html).toContain("max-w-2xl");
  277. expect(extractInputTag(html, 'value="deepseek-v4-flash"')).toContain('spellCheck="false"');
  278. });
  279. it("编辑弹窗在 Token 分组展示最大输出并提供跟随推荐配置开关", () => {
  280. const html = renderToStaticMarkup(
  281. createElement(ProviderModelMetadataDialog, {
  282. mode: "edit",
  283. open: true,
  284. draft: {
  285. ...MODEL_FORMAT_DRAFT,
  286. idValue: "demo-model",
  287. contextWindowValue: "",
  288. maxOutputTokensValue: "",
  289. },
  290. draftErrorMessage: null,
  291. inheritedConfig: {
  292. properties: { contextWindow: 128_000 },
  293. optionSpecs: { maxOutputTokens: { max: 32_000 } },
  294. },
  295. personalConfig: { enabled: false },
  296. onOpenChange: vi.fn(),
  297. onDraftChange: vi.fn(),
  298. onCommit: vi.fn(() => true),
  299. }),
  300. );
  301. expect(html).toContain("高级配置");
  302. expect(html).not.toContain('data-model-settings-advanced-trigger="true"');
  303. expect(html).toContain('data-model-advanced="true"');
  304. expect(html).toContain('data-model-reasoning-level-editor="true"');
  305. expect(html).toContain('data-model-settings-scroll="true"');
  306. expect(html).toContain("-mr-3");
  307. expect(html).toContain("pr-4");
  308. expect(extractInputTag(html, 'aria-label="最大输出 Token"')).toContain('placeholder="32000"');
  309. expect(html).toContain('data-model-recommended-config="true"');
  310. expect(html).toContain(">跟随推荐配置<");
  311. const footer = html.slice(html.indexOf('data-model-settings-footer="true"'));
  312. expect(footer).not.toContain('data-model-recommended-config="true"');
  313. expect(footer).not.toContain('role="switch"');
  314. expect(footer).toContain(">重置表单<");
  315. expect(footer.indexOf(">取消<")).toBeLessThan(footer.indexOf(">保存<"));
  316. expect(html.indexOf('data-model-max-output="true"')).toBeLessThan(
  317. html.indexOf('data-model-advanced="true"'),
  318. );
  319. expect(footer).toContain(">保存<");
  320. expect(footer).toContain(">取消<");
  321. });
  322. it("跟随推荐配置开关始终展示,且不存在旧的恢复默认按钮", () => {
  323. const html = renderToStaticMarkup(
  324. createElement(ProviderModelMetadataDialog, {
  325. mode: "edit",
  326. open: true,
  327. draft: {
  328. ...MODEL_FORMAT_DRAFT,
  329. idValue: "personal-model",
  330. contextWindowValue: "",
  331. maxOutputTokensValue: "",
  332. },
  333. draftErrorMessage: null,
  334. inheritedConfig: {
  335. properties: { contextWindow: 128_000 },
  336. optionSpecs: { maxOutputTokens: { max: 32_000 } },
  337. },
  338. personalConfig: {},
  339. onOpenChange: vi.fn(),
  340. onDraftChange: vi.fn(),
  341. onCommit: vi.fn(() => true),
  342. }),
  343. );
  344. expect(html).toContain('data-model-recommended-config="true"');
  345. expect(html).not.toContain("全部恢复默认");
  346. expect(html).not.toContain("settings.modelProvider.restoreInheritedValue");
  347. });
  348. it("模态与可编辑模型能力使用复选框选项,工具调用不再作为设置项", () => {
  349. const html = renderToStaticMarkup(
  350. createElement(ProviderModelMetadataDialog, {
  351. mode: "edit",
  352. open: true,
  353. draft: {
  354. ...MODEL_FORMAT_DRAFT,
  355. idValue: "demo-model",
  356. contextWindowValue: "",
  357. maxOutputTokensValue: "",
  358. },
  359. draftErrorMessage: null,
  360. personalConfig: {
  361. properties: {
  362. inputFormat: { supportsImage: false },
  363. supportsToolCall: true,
  364. },
  365. },
  366. inheritedConfig: {
  367. properties: {
  368. contextWindow: 128_000,
  369. inputFormat: { supportsImage: false },
  370. },
  371. },
  372. onOpenChange: vi.fn(),
  373. onDraftChange: vi.fn(),
  374. onCommit: vi.fn(() => true),
  375. }),
  376. );
  377. const options = html.match(/<button[^>]*data-model-boolean-option="true"[^>]*>/g) ?? [];
  378. expect(options).toHaveLength(3);
  379. const capabilityOptions =
  380. html.match(/<button[^>]*data-model-capability-option="true"[^>]*>/g) ?? [];
  381. expect(capabilityOptions).toHaveLength(3);
  382. expect(capabilityOptions.every((option) => option.includes('role="checkbox"'))).toBe(true);
  383. expect(capabilityOptions.every((option) => option.includes("border-border"))).toBe(true);
  384. expect(capabilityOptions.every((option) => option.includes("bg-transparent"))).toBe(true);
  385. expect(
  386. capabilityOptions.every(
  387. (option) => option.includes("hover:bg-hover") && option.includes("focus-visible:bg-hover"),
  388. ),
  389. ).toBe(true);
  390. expect(html).not.toContain('data-model-capability-icon="true"');
  391. expect(html).not.toContain("settings.modelProvider.supportsToolCall");
  392. expect(html.match(/data-model-option-checkbox="true"/g)).toHaveLength(7);
  393. expect(html).toContain("data-model-option-checkbox");
  394. const overriddenModality =
  395. html.match(
  396. /<button[^>]*data-personal-override="true"[^>]*data-model-input-modality="image"[^>]*>/,
  397. )?.[0] ?? "";
  398. expect(overriddenModality).not.toBe("");
  399. expect(overriddenModality).toContain("border-primary/35");
  400. const selectedModalities =
  401. html.match(
  402. /<button[^>]*data-selected="true"[^>]*data-model-(?:input|output)-modality[^>]*>/g,
  403. ) ?? [];
  404. expect(selectedModalities).toHaveLength(1);
  405. expect(selectedModalities.every((option) => option.includes("bg-foreground/15"))).toBe(true);
  406. expect(html).not.toContain(">默认<");
  407. expect(html).not.toContain(">已修改<");
  408. });
  409. it("按任务顺序分组排列,常用分组不显示标题", () => {
  410. const html = renderToStaticMarkup(
  411. createElement(ProviderModelMetadataDialog, {
  412. mode: "edit",
  413. open: true,
  414. draft: {
  415. ...MODEL_FORMAT_DRAFT,
  416. idValue: "demo-model",
  417. contextWindowValue: "",
  418. maxOutputTokensValue: "",
  419. },
  420. draftErrorMessage: null,
  421. onOpenChange: vi.fn(),
  422. onDraftChange: vi.fn(),
  423. onCommit: vi.fn(() => true),
  424. }),
  425. );
  426. expect(html).toContain('data-model-identity-row="true"');
  427. expect(html).not.toContain('data-model-enabled-row="true"');
  428. expect(html).not.toContain("启用");
  429. expect(html).not.toContain('data-model-identity-enabled-layout="true"');
  430. expect(html).toContain('data-no-model-drag="true"');
  431. expect(html).toContain("max-h-[min(48rem,calc(100vh-4rem))]");
  432. const orderedGroups = ["basic", "tokens", "modalities", "capabilities"];
  433. const indexes = orderedGroups.map((group) =>
  434. html.indexOf(`data-model-settings-group="${group}"`),
  435. );
  436. expect(indexes.every((index) => index >= 0)).toBe(true);
  437. expect(indexes).toEqual([...indexes].sort((left, right) => left - right));
  438. expect(html).not.toContain("基本信息");
  439. expect(html).not.toContain("Token 限制");
  440. expect(html).not.toContain("输入与输出");
  441. expect(html).toContain("模型能力");
  442. const capabilitiesLabel =
  443. html.match(/<div[^>]*data-model-capabilities-label="true"[^>]*>/)?.[0] ?? "";
  444. expect(capabilitiesLabel).toContain("mb-1");
  445. expect(html).toContain('data-model-capabilities-options="true"');
  446. expect(html).toContain("高级配置");
  447. });
  448. it("模型后缀不会覆盖传入的上下文配置", () => {
  449. const html = renderToStaticMarkup(
  450. createElement(ProviderModelMetadataDialog, {
  451. open: true,
  452. draft: {
  453. ...MODEL_FORMAT_DRAFT,
  454. idValue: "deepseek-v4-pro[1m]",
  455. nameValue: "",
  456. kindsValue: ["openai-compatible"],
  457. contextWindowValue: "64000",
  458. maxOutputTokensValue: "",
  459. },
  460. draftErrorMessage: null,
  461. onOpenChange: vi.fn(),
  462. onDraftChange: vi.fn(),
  463. onCommit: vi.fn(() => true),
  464. }),
  465. );
  466. const contextWindowInput = extractInputTag(html, 'inputMode="numeric"');
  467. expect(contextWindowInput).not.toContain("readOnly");
  468. expect(contextWindowInput).toContain('value="64000"');
  469. });
  470. it("编辑带后缀模型时仍聚焦上下文窗口", () => {
  471. const html = renderToStaticMarkup(
  472. createElement(ProviderModelMetadataDialog, {
  473. mode: "edit",
  474. open: true,
  475. draft: {
  476. ...MODEL_FORMAT_DRAFT,
  477. idValue: "deepseek-v4-pro[1m]",
  478. nameValue: "",
  479. kindsValue: ["openai-compatible"],
  480. contextWindowValue: "64000",
  481. maxOutputTokensValue: "",
  482. },
  483. draftErrorMessage: null,
  484. onOpenChange: vi.fn(),
  485. onDraftChange: vi.fn(),
  486. onCommit: vi.fn(() => true),
  487. }),
  488. );
  489. const modelIdInput = extractInputTag(html, 'placeholder="模型 ID"');
  490. const contextWindowInput = extractInputTag(html, 'inputMode="numeric"');
  491. expect(modelIdInput).not.toContain("autofocus");
  492. expect(contextWindowInput).toContain('autofocus=""');
  493. });
  494. it("打开弹窗默认聚焦上下文窗口", () => {
  495. const html = renderToStaticMarkup(
  496. createElement(ProviderModelMetadataDialog, {
  497. mode: "edit",
  498. open: true,
  499. draft: {
  500. ...MODEL_FORMAT_DRAFT,
  501. idValue: "deepseek-v4-flash",
  502. nameValue: "",
  503. kindsValue: ["openai-compatible"],
  504. contextWindowValue: "128000",
  505. maxOutputTokensValue: "",
  506. },
  507. draftErrorMessage: null,
  508. onOpenChange: vi.fn(),
  509. onDraftChange: vi.fn(),
  510. onCommit: vi.fn(() => true),
  511. }),
  512. );
  513. expect(html).not.toMatch(/placeholder="模型 ID"[^>]*autofocus/);
  514. expect(html).toMatch(/<input[^>]*autofocus[^>]*inputMode="numeric"[^>]*>/);
  515. });
  516. it("上下文窗口聚焦时会全选当前输入内容", () => {
  517. const select = vi.fn();
  518. selectFocusedInputText({
  519. currentTarget: {
  520. select,
  521. },
  522. });
  523. expect(select).toHaveBeenCalledTimes(1);
  524. });
  525. it("添加模型弹窗展示推理与高级分组、禁用必选 Text 并使用复选框模态选项", () => {
  526. const html = renderToStaticMarkup(
  527. createElement(ProviderModelMetadataDialog, {
  528. mode: "add",
  529. open: true,
  530. draft: {
  531. ...MODEL_FORMAT_DRAFT,
  532. idValue: "",
  533. nameValue: "",
  534. kindsValue: ["anthropic"],
  535. contextWindowValue: "1000000",
  536. maxOutputTokensValue: "128000",
  537. inputFormatValue: {
  538. supportsText: true,
  539. supportsImage: false,
  540. supportsVideo: false,
  541. supportsAudio: false,
  542. supportsPdf: false,
  543. },
  544. modalitiesTouched: true,
  545. },
  546. draftErrorMessage: null,
  547. onOpenChange: vi.fn(),
  548. onDraftChange: vi.fn(),
  549. onCommit: vi.fn(() => true),
  550. }),
  551. );
  552. expect(html).toContain("添加模型");
  553. expect(html).toContain("模型 ID");
  554. expect(html).not.toContain("API 格式");
  555. expect(html).not.toContain(">/anthropic/v1/messages<");
  556. expect(html).not.toContain(">/v1/chat/completions<");
  557. expect(html).not.toContain("Responses (/responses)");
  558. expect(html).toContain("上下文窗口");
  559. expect(html).toContain("最大输出 Token");
  560. expect(html).toContain("输入类型");
  561. expect(html).not.toContain('data-model-input-modalities-description="true"');
  562. expect(html).not.toContain("暂不支持作为附件");
  563. expect(html).not.toContain("输出类型");
  564. expect(html).toContain("高级配置");
  565. expect(html).not.toContain('data-model-settings-advanced-trigger="true"');
  566. expect(html).toContain('data-model-advanced="true"');
  567. expect(html).toContain('data-model-reasoning-level-editor="true"');
  568. expect(html).toContain('data-model-input-modality="text"');
  569. expect(html).toContain('data-model-input-modality="image"');
  570. expect(html).toContain('data-model-input-modality="video"');
  571. expect(html).toContain('data-model-input-modality="pdf"');
  572. expect(html).not.toContain('data-model-input-modality="audio"');
  573. expect(html).not.toContain('data-model-output-modality="text"');
  574. expect(html).not.toContain('data-model-modality-icon="true"');
  575. expect(html.match(/data-model-modality-lock="true"/g)).toHaveLength(1);
  576. const modalityButtons =
  577. html.match(/<button[^>]*data-model-(?:input|output)-modality=[^>]*>/g) ?? [];
  578. expect(modalityButtons).toHaveLength(4);
  579. for (const button of modalityButtons) {
  580. expect(button).toContain('data-variant="outline"');
  581. expect(button).toContain('data-size="lg"');
  582. expect(button).toContain("px-3");
  583. }
  584. expect(
  585. modalityButtons.find((button) => button.includes('data-model-input-modality="image"')),
  586. ).toContain('aria-pressed="false"');
  587. expect(
  588. modalityButtons.find((button) => button.includes('data-model-input-modality="text"')),
  589. ).toContain('aria-pressed="true"');
  590. expect(
  591. modalityButtons.find((button) => button.includes('data-model-input-modality="text"')),
  592. ).toContain("bg-foreground/15");
  593. expect(
  594. modalityButtons.find((button) => button.includes('data-model-input-modality="text"')),
  595. ).toContain("border-border");
  596. expect(
  597. modalityButtons.find((button) => button.includes('data-model-input-modality="text"')),
  598. ).toContain("bg-clip-border");
  599. expect(
  600. modalityButtons.find((button) => button.includes('data-model-input-modality="text"')),
  601. ).toContain("hover:bg-foreground/20");
  602. expect(
  603. modalityButtons.find((button) => button.includes('data-model-input-modality="image"')),
  604. ).toContain("bg-transparent");
  605. expect(
  606. modalityButtons.find((button) => button.includes('data-model-input-modality="image"')),
  607. ).toContain("hover:bg-hover");
  608. expect(
  609. modalityButtons.find((button) => button.includes('data-model-input-modality="text"')),
  610. ).toContain('disabled=""');
  611. expect(html.match(/lucide-lock-keyhole/g)).toHaveLength(1);
  612. expect(html).not.toContain("border-brand");
  613. expect(html).not.toContain("bg-brand/10");
  614. expect(html).toContain('value="1000000"');
  615. expect(html).not.toContain("settings.modelProvider.reasoningLevelOptionSpecJson");
  616. expect(html).toContain('data-model-settings-footer="true"');
  617. expect(html).toContain(">取消<");
  618. expect(html).toContain(">保存<");
  619. const saveButton = extractButtonOpeningTag(html, "保存");
  620. const cancelButton = extractButtonOpeningTag(html, "取消");
  621. expect(saveButton).toContain('data-variant="default"');
  622. expect(saveButton).toContain('data-size="lg"');
  623. expect(cancelButton).toContain('data-variant="ghost"');
  624. expect(cancelButton).toContain('data-size="lg"');
  625. expect(html.lastIndexOf(">取消<")).toBeLessThan(html.lastIndexOf(">保存<"));
  626. expect(html.match(/添加模型/g)).toHaveLength(1);
  627. });
  628. it("解析模型配置时展示 loading,并允许保存按钮主动等待当前解析", () => {
  629. const html = renderToStaticMarkup(
  630. createElement(ProviderModelMetadataDialog, {
  631. mode: "add",
  632. open: true,
  633. modelConfigResolutionPending: true,
  634. draft: {
  635. ...MODEL_FORMAT_DRAFT,
  636. idValue: "glm-5",
  637. nameValue: "",
  638. kindsValue: ["anthropic"],
  639. contextWindowValue: "1000000",
  640. maxOutputTokensValue: "128000",
  641. },
  642. draftErrorMessage: null,
  643. onOpenChange: vi.fn(),
  644. onDraftChange: vi.fn(),
  645. onCommit: vi.fn(() => true),
  646. }),
  647. );
  648. expect(html).toContain("加载中...");
  649. expect(html).toContain("animate-spin");
  650. expect(extractInputTag(html, 'aria-label="最大输出 Token"')).not.toMatch(
  651. /\sdisabled(?:=|\s|\/|>)/,
  652. );
  653. expect(extractButtonOpeningTag(html, "保存")).not.toContain(' disabled=""');
  654. });
  655. it("添加模型弹窗默认聚焦模型 ID,不自动聚焦上下文窗口", () => {
  656. const html = renderToStaticMarkup(
  657. createElement(ProviderModelMetadataDialog, {
  658. mode: "add",
  659. open: true,
  660. draft: {
  661. ...MODEL_FORMAT_DRAFT,
  662. idValue: "",
  663. nameValue: "",
  664. kindsValue: ["anthropic"],
  665. contextWindowValue: "128000",
  666. maxOutputTokensValue: "",
  667. },
  668. draftErrorMessage: null,
  669. onOpenChange: vi.fn(),
  670. onDraftChange: vi.fn(),
  671. onCommit: vi.fn(() => true),
  672. }),
  673. );
  674. const modelIdInput = extractInputTag(html, 'placeholder="模型 ID"');
  675. const contextWindowInput = extractInputTag(html, 'inputMode="numeric"');
  676. expect(modelIdInput).toContain('autofocus=""');
  677. expect(contextWindowInput).not.toContain("autofocus");
  678. });
  679. it("编辑模型弹窗 footer 展示取消和保存", () => {
  680. const html = renderToStaticMarkup(
  681. createElement(ProviderModelMetadataDialog, {
  682. mode: "edit",
  683. open: true,
  684. draft: {
  685. ...MODEL_FORMAT_DRAFT,
  686. idValue: "deepseek-v4-flash",
  687. nameValue: "",
  688. kindsValue: ["openai-compatible"],
  689. contextWindowValue: "128000",
  690. maxOutputTokensValue: "",
  691. },
  692. draftErrorMessage: null,
  693. onOpenChange: vi.fn(),
  694. onDraftChange: vi.fn(),
  695. onCommit: vi.fn(() => true),
  696. }),
  697. );
  698. expect(html).toContain("编辑模型配置");
  699. expect(html).toContain('data-model-settings-footer="true"');
  700. expect(html).toContain(">取消<");
  701. expect(html).toContain(">保存<");
  702. });
  703. it("模型弹窗不展示 API 格式选项", () => {
  704. const html = renderToStaticMarkup(
  705. createElement(ProviderModelMetadataDialog, {
  706. mode: "add",
  707. open: true,
  708. apiFormatOptions: ["anthropic-messages"],
  709. draft: {
  710. ...MODEL_FORMAT_DRAFT,
  711. idValue: "",
  712. nameValue: "",
  713. kindsValue: ["anthropic", "openai-compatible", "openai"],
  714. contextWindowValue: "128000",
  715. maxOutputTokensValue: "",
  716. },
  717. draftErrorMessage: null,
  718. onOpenChange: vi.fn(),
  719. onDraftChange: vi.fn(),
  720. onCommit: vi.fn(() => true),
  721. }),
  722. );
  723. expect(html).not.toContain("API 格式");
  724. expect(html).not.toContain('data-api-format-selected-tag="anthropic"');
  725. expect(html).not.toContain('data-api-format-option="anthropic"');
  726. expect(html).not.toContain(">/anthropic/v1/messages<");
  727. expect(html).not.toContain(">/v1/chat/completions<");
  728. expect(html).not.toContain("Responses (/responses)");
  729. });
  730. it("模型弹窗忽略供应商 API 格式 path 配置", () => {
  731. const html = renderToStaticMarkup(
  732. createElement(ProviderModelMetadataDialog, {
  733. mode: "add",
  734. open: true,
  735. apiFormatOptions: ["openai-chat-completions"],
  736. endpointPaths: {
  737. "openai-compatible": "/custom/chat",
  738. },
  739. draft: {
  740. ...MODEL_FORMAT_DRAFT,
  741. idValue: "",
  742. nameValue: "",
  743. kindsValue: ["openai-compatible"],
  744. contextWindowValue: "128000",
  745. maxOutputTokensValue: "",
  746. },
  747. draftErrorMessage: null,
  748. onOpenChange: vi.fn(),
  749. onDraftChange: vi.fn(),
  750. onCommit: vi.fn(() => true),
  751. }),
  752. );
  753. expect(html).not.toContain("API 格式");
  754. expect(html).not.toContain(">/custom/chat<");
  755. expect(html).not.toContain(">/v1/chat/completions<");
  756. });
  757. it("模型弹窗不展示 API 格式多选触发器和弹层选项", () => {
  758. const html = renderToStaticMarkup(
  759. createElement(ProviderModelMetadataDialog, {
  760. mode: "add",
  761. open: true,
  762. draft: {
  763. ...MODEL_FORMAT_DRAFT,
  764. idValue: "",
  765. nameValue: "",
  766. kindsValue: ["anthropic", "openai-compatible"],
  767. contextWindowValue: "128000",
  768. maxOutputTokensValue: "",
  769. },
  770. draftErrorMessage: null,
  771. onOpenChange: vi.fn(),
  772. onDraftChange: vi.fn(),
  773. onCommit: vi.fn(() => true),
  774. }),
  775. );
  776. expect(html).not.toContain('data-api-format-selected-tag="anthropic"');
  777. expect(html).not.toContain('data-api-format-selected-tag="openai-compatible"');
  778. expect(html).not.toContain(">Anthropic<");
  779. expect(html).not.toContain(">Chat<");
  780. expect(html).not.toContain('data-api-format-option="anthropic"');
  781. expect(html).not.toContain(
  782. "Anthropic Messages (/anthropic/v1/messages), Chat Completions (/v1/chat/completions)",
  783. );
  784. });
  785. });