providerSettingsFormProjection.test.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import { describe, expect, it } from "vitest";
  2. import type { ProviderSettingsView } from "@zcode/services";
  3. import {
  4. projectProviderSettingsViewToFormProviders,
  5. resolveProviderSettingsFormProviders,
  6. resolveProviderOrdering,
  7. resolvePersonalProviderIds,
  8. } from "@/lib/providerSettingsFormProjection.js";
  9. describe("providerSettingsFormProjection", () => {
  10. it("API 与 Account 的投影只使用 Settings View,不携带旧 Account 运行凭据", () => {
  11. const settings: ProviderSettingsView = {
  12. revision: 7,
  13. addableProviders: [],
  14. providerOrder: ["api-provider"],
  15. providers: [
  16. {
  17. providerId: "api-provider",
  18. enabled: true,
  19. executable: true,
  20. personalConfig: {
  21. access: { type: "api-key", apiKey: "new-key" },
  22. personalModelIds: ["model-a"],
  23. },
  24. effectiveConfig: {
  25. label: "API Provider",
  26. group: "standard-personal",
  27. access: { type: "api-key", apiKey: "new-key" },
  28. api: {
  29. type: "openai-chat-completions",
  30. baseUrl: "https://new.example.com/v1",
  31. },
  32. personalModelIds: ["model-a"],
  33. },
  34. issues: [],
  35. models: [
  36. {
  37. kind: "candidate",
  38. modelId: "model-a",
  39. builtin: false,
  40. personalConfig: { properties: { contextWindow: 128_000 } },
  41. effectiveConfig: {
  42. properties: {
  43. requiresMfjsToolSchema: false,
  44. contextWindow: 128_000,
  45. inputFormat: {
  46. supportsText: true,
  47. supportsImage: true,
  48. supportsVideo: true,
  49. supportsAudio: false,
  50. supportsPdf: false,
  51. },
  52. outputFormat: { supportsText: true },
  53. supportsToolCall: true,
  54. supportsJsonSchemaOutput: true,
  55. supportsNativeWebSearch: false,
  56. supportsMidConversationSystem: false,
  57. },
  58. optionSpecs: {
  59. reasoningLevel: {
  60. values: ["low", "high"],
  61. map: "{'reasoning_effort': reasoningLevel}",
  62. },
  63. maxOutputTokens: {
  64. max: 32_000,
  65. map: "{'max_completion_tokens': maxOutputTokens}",
  66. },
  67. },
  68. },
  69. enabled: true,
  70. executable: true,
  71. selectable: true,
  72. issues: [],
  73. },
  74. ],
  75. },
  76. {
  77. providerId: "account-plan",
  78. enabled: true,
  79. executable: true,
  80. effectiveConfig: {
  81. label: "Account Plan Config",
  82. group: "zai-family",
  83. access: {
  84. type: "zhipu-account",
  85. },
  86. api: {
  87. type: "anthropic-messages",
  88. baseUrl: "https://account.example.com",
  89. },
  90. builtinModelIds: ["account-model"],
  91. },
  92. issues: [],
  93. models: [
  94. {
  95. kind: "candidate",
  96. modelId: "account-model",
  97. builtin: true,
  98. effectiveConfig: {
  99. properties: {
  100. requiresMfjsToolSchema: false,
  101. contextWindow: 200_000,
  102. inputFormat: {
  103. supportsText: true,
  104. supportsImage: false,
  105. supportsVideo: false,
  106. supportsAudio: false,
  107. supportsPdf: false,
  108. },
  109. outputFormat: { supportsText: true },
  110. supportsToolCall: true,
  111. supportsJsonSchemaOutput: true,
  112. supportsNativeWebSearch: false,
  113. supportsMidConversationSystem: false,
  114. },
  115. optionSpecs: {
  116. maxOutputTokens: {
  117. max: 64_000,
  118. },
  119. },
  120. },
  121. enabled: true,
  122. executable: true,
  123. selectable: true,
  124. issues: [],
  125. },
  126. ],
  127. },
  128. ],
  129. };
  130. const result = projectProviderSettingsViewToFormProviders(settings);
  131. expect(result.map((provider) => provider.providerId)).toEqual(["api-provider", "account-plan"]);
  132. expect(result[0]).toMatchObject({
  133. providerId: "api-provider",
  134. executable: true,
  135. config: {
  136. label: "API Provider",
  137. access: { type: "api-key", apiKey: "new-key" },
  138. api: {
  139. type: "openai-chat-completions",
  140. baseUrl: "https://new.example.com/v1",
  141. },
  142. },
  143. });
  144. expect(result[0]?.models[0]).toMatchObject({
  145. modelId: "model-a",
  146. config: {
  147. properties: { contextWindow: 128_000 },
  148. optionSpecs: {
  149. maxOutputTokens: { max: 32_000 },
  150. },
  151. },
  152. });
  153. expect(result[1]).toMatchObject({
  154. providerId: "account-plan",
  155. executable: true,
  156. config: {
  157. label: "Account Plan Config",
  158. api: { baseUrl: "https://account.example.com" },
  159. },
  160. models: [
  161. expect.objectContaining({
  162. modelId: "account-model",
  163. config: expect.objectContaining({
  164. properties: expect.objectContaining({ contextWindow: 200_000 }),
  165. optionSpecs: expect.objectContaining({
  166. maxOutputTokens: expect.objectContaining({ max: 64_000 }),
  167. }),
  168. }),
  169. }),
  170. ],
  171. });
  172. expect(resolvePersonalProviderIds(settings)).toEqual(["api-provider"]);
  173. expect(
  174. resolveProviderOrdering({
  175. view: settings,
  176. providers: result,
  177. }),
  178. ).toEqual({
  179. displayOrder: {
  180. providerIds: ["api-provider", "account-plan"],
  181. },
  182. reorderableProviderIds: new Set(["api-provider"]),
  183. });
  184. });
  185. it("没有旧快照对象时仍投影 Official Account Provider", () => {
  186. const settings: ProviderSettingsView = {
  187. revision: 8,
  188. addableProviders: [],
  189. providerOrder: [],
  190. providers: [
  191. {
  192. providerId: "builtin:account-plan",
  193. enabled: true,
  194. executable: false,
  195. effectiveConfig: {
  196. label: "Account Plan",
  197. group: "zai-family",
  198. access: { type: "zhipu-account" },
  199. api: {
  200. type: "anthropic-messages",
  201. baseUrl: "https://account.example.com",
  202. },
  203. builtinModelIds: ["account-model"],
  204. },
  205. issues: [
  206. {
  207. code: "required",
  208. path: ["providers", "builtin:account-plan", "connection"],
  209. message: "账号连接尚未就绪",
  210. },
  211. ],
  212. models: [],
  213. },
  214. ],
  215. };
  216. expect(projectProviderSettingsViewToFormProviders(settings)).toEqual([
  217. expect.objectContaining({
  218. providerId: "builtin:account-plan",
  219. executable: false,
  220. config: expect.objectContaining({
  221. label: "Account Plan",
  222. access: { type: "zhipu-account" },
  223. api: {
  224. type: "anthropic-messages",
  225. baseUrl: "https://account.example.com",
  226. },
  227. }),
  228. }),
  229. ]);
  230. });
  231. it("Settings View 已发布时不追加旧快照独有的 Provider", () => {
  232. const settings: ProviderSettingsView = {
  233. revision: 9,
  234. providers: [],
  235. };
  236. expect(projectProviderSettingsViewToFormProviders(settings)).toEqual([]);
  237. });
  238. it("Settings View 尚未发布时保持空列表", () => {
  239. expect(
  240. resolveProviderSettingsFormProviders({
  241. view: null,
  242. }),
  243. ).toEqual([]);
  244. });
  245. });