accountProviderConnectionResolver.test.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. import {
  2. ProviderConfig,
  3. ProviderConfigMap,
  4. ZhipuAccountAccessConfig,
  5. createAccountProviderConfigResolver,
  6. } from "@zcode/provider";
  7. import {
  8. BUILTIN_MODEL_PROVIDER_IDS,
  9. migrateLegacyModelProviderId,
  10. zcodeProviderUpdateAccountConfigParamsSchema,
  11. type ProviderFamilyDomain,
  12. } from "@zcode/shared";
  13. import { describe, expect, it, vi } from "vitest";
  14. import {
  15. createAccountProviderConnectionResolver,
  16. createCodingPlanFamilyAvailabilityResolver,
  17. resolveCurrentAccountAccess,
  18. type AccountProviderFamilyAvailabilityResolver,
  19. } from "../src/model-provider/accountProviderConnectionResolver.js";
  20. import { createAccountProviderConfig } from "./providerConfigFixtures.js";
  21. function configuredProviders(options: { bigmodelTeam?: boolean } = {}): ProviderConfigMap {
  22. return new ProviderConfigMap([
  23. [
  24. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  25. createAccountProviderConfig({
  26. accountType: "zai",
  27. mode: "individual-coding-plan",
  28. models: ["zai-coding"],
  29. }),
  30. ],
  31. [
  32. BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
  33. createAccountProviderConfig({
  34. accountType: "zai",
  35. mode: "start-plan",
  36. models: ["zai-start"],
  37. }),
  38. ],
  39. [
  40. BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  41. createAccountProviderConfig({
  42. accountType: "bigmodel",
  43. mode: "individual-coding-plan",
  44. models: ["bigmodel-coding"],
  45. }),
  46. ],
  47. ...(options.bigmodelTeam
  48. ? ([
  49. [
  50. BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  51. createAccountProviderConfig({
  52. accountType: "bigmodel",
  53. mode: "team-coding-plan",
  54. models: ["bigmodel-team"],
  55. }),
  56. ],
  57. ] as const)
  58. : []),
  59. [
  60. BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
  61. createAccountProviderConfig({
  62. accountType: "bigmodel",
  63. mode: "start-plan",
  64. models: ["bigmodel-start"],
  65. }),
  66. ],
  67. ]);
  68. }
  69. describe("createAccountProviderConnectionResolver", () => {
  70. it("Start 与付费连接并存,切团队不改变 Start 身份,退出登录则失效", async () => {
  71. let identity: string | null = "account-a";
  72. let selections: import("@zcode/shared").ProviderFamilyConnectionSelectionSettings = {
  73. bigmodel: { kind: "individual-coding-plan" },
  74. };
  75. const resolve = createAccountProviderConnectionResolver({
  76. readSettings: async () => ({ providerFamilyDomain: "bigmodel", selections }),
  77. loadAccountIdentity: async (family) => (family === "bigmodel" ? identity : null),
  78. loadCodingPlanApiKey: async () => null,
  79. resolveFamilyAvailability: async () => ({
  80. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan]: { kind: "available", models: [] },
  81. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan]: { kind: "available" },
  82. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan]: { kind: "available" },
  83. }),
  84. });
  85. const input = {
  86. configRevision: "config",
  87. configuredProviders: configuredProviders({ bigmodelTeam: true }),
  88. };
  89. const first = await resolve(input);
  90. expect(first.filter((p) => p.current).map((p) => p.providerId)).toEqual([
  91. BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  92. BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
  93. ]);
  94. selections = {
  95. bigmodel: {
  96. kind: "team-coding-plan",
  97. organizationId: "org",
  98. projectId: "project",
  99. productId: "product",
  100. },
  101. };
  102. const second = await resolve(input);
  103. const start = (values: typeof first) =>
  104. values.find((p) => p.providerId === BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan)!;
  105. expect(start(second).current).toBe(true);
  106. expect(start(second).connectionKey).toBe(start(first).connectionKey);
  107. expect(start(second).resetPrevious).toBeUndefined();
  108. selections = {};
  109. expect(start(await resolve(input)).current).toBe(true);
  110. identity = null;
  111. expect(start(await resolve(input))).toMatchObject({
  112. current: false,
  113. status: "unavailable",
  114. resetPrevious: true,
  115. });
  116. });
  117. it.each([
  118. undefined,
  119. { kind: "individual-coding-plan" as const },
  120. { kind: "start-plan" as const },
  121. ])("Start 请求不依赖套餐选择 %j,仍要求当前品牌与登录身份", async (selection) => {
  122. const input = {
  123. access: {
  124. type: "zhipu-account" as const,
  125. accountType: "bigmodel" as const,
  126. mode: "start-plan" as const,
  127. },
  128. readSettings: async () => ({
  129. providerFamilyDomain: "bigmodel" as const,
  130. selections: selection ? { bigmodel: selection } : {},
  131. }),
  132. loadAccountIdentity: async () => "account-a",
  133. };
  134. await expect(resolveCurrentAccountAccess(input)).resolves.toEqual({
  135. type: "zhipu-account",
  136. family: "bigmodel",
  137. planKind: "start-plan",
  138. });
  139. await expect(
  140. resolveCurrentAccountAccess({ ...input, loadAccountIdentity: async () => null }),
  141. ).resolves.toBeNull();
  142. await expect(
  143. resolveCurrentAccountAccess({
  144. ...input,
  145. readSettings: async () => ({ providerFamilyDomain: "zai", selections: {} }),
  146. }),
  147. ).resolves.toBeNull();
  148. });
  149. it("真实账号缺 Key 保持权益未知,且不阻断另一个可用套餐", async () => {
  150. const configured = configuredProviders();
  151. const resolve = createAccountProviderConfigResolver(
  152. createAccountProviderConnectionResolver({
  153. readSettings: async () => ({
  154. providerFamilyDomain: "bigmodel",
  155. selections: { bigmodel: { kind: "individual-coding-plan" } },
  156. }),
  157. loadAccountIdentity: async () => "account",
  158. loadCodingPlanApiKey: async () => null,
  159. resolveFamilyAvailability: createCodingPlanFamilyAvailabilityResolver({
  160. credentialService: { load: async () => "local-login-info" },
  161. apiClient: {
  162. request: async () =>
  163. new Response(
  164. JSON.stringify({
  165. code: 0,
  166. success: true,
  167. data: {
  168. plans: [{ plan_id: "zcode-v3-start-plan", status: "active" }],
  169. balances: [],
  170. },
  171. }),
  172. { status: 200 },
  173. ),
  174. },
  175. }),
  176. }),
  177. );
  178. const result = await resolve({
  179. configRevision: "builtin-1",
  180. configuredProviders: configured,
  181. previousProviders: new ProviderConfigMap(),
  182. });
  183. const parsed = zcodeProviderUpdateAccountConfigParamsSchema.parse({
  184. revision: "account-1",
  185. basedOnZCodeBuiltinRevision: "builtin-1",
  186. providers: {},
  187. states: result.states,
  188. });
  189. for (const id of [
  190. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  191. BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  192. ]) {
  193. expect(parsed.states[id]).toMatchObject({
  194. availability: "unknown",
  195. });
  196. }
  197. expect(parsed.states[BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan]).toMatchObject({
  198. availability: "available",
  199. entitled: true,
  200. });
  201. });
  202. it.each(["connection", "identity"] as const)(
  203. "查询期间变更 %s 时丢弃过期结果,不污染上次成功的 scope",
  204. async (change) => {
  205. let identity = "account-a";
  206. let settings: import("../src/model-provider/accountProviderConnectionResolver.js").AccountProviderConnectionSettings =
  207. {
  208. providerFamilyDomain: "bigmodel",
  209. selections: { bigmodel: { kind: "individual-coding-plan" } },
  210. };
  211. let release!: () => void;
  212. let entered!: () => void;
  213. const enteredGate = new Promise<void>((resolve) => {
  214. entered = resolve;
  215. });
  216. let wait = false;
  217. const resolve = createAccountProviderConnectionResolver({
  218. readSettings: async () => settings,
  219. loadAccountIdentity: async (family) => (family === "bigmodel" ? identity : null),
  220. loadCodingPlanApiKey: async () => null,
  221. resolveFamilyAvailability: async () => {
  222. if (wait) {
  223. entered();
  224. await new Promise<void>((resolve) => {
  225. release = resolve;
  226. });
  227. }
  228. return {};
  229. },
  230. });
  231. const input = {
  232. configRevision: "config",
  233. configuredProviders: configuredProviders({ bigmodelTeam: true }),
  234. };
  235. await resolve(input);
  236. // 未发布的 B 查询不能把成功作用域 A 推进成 B。
  237. identity = "account-b";
  238. wait = true;
  239. const pending = resolve(input);
  240. await enteredGate;
  241. if (change === "identity") identity = "account-c";
  242. else
  243. settings = {
  244. ...settings,
  245. selections: {
  246. bigmodel: {
  247. kind: "team-coding-plan",
  248. organizationId: "org",
  249. productId: "product",
  250. projectId: "project",
  251. },
  252. },
  253. };
  254. release();
  255. await expect(pending).rejects.toThrow("账号查询期间");
  256. wait = false;
  257. identity = "account-b";
  258. const next = await resolve(input);
  259. expect(
  260. next.find(
  261. (entry) => entry.providerId === BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  262. )?.resetPrevious,
  263. ).toBe(true);
  264. },
  265. );
  266. it("旧团队待补全只限制付费访问,Start 与其他 Family 继续查询", async () => {
  267. const availability = vi.fn<AccountProviderFamilyAvailabilityResolver>(async () => ({
  268. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan]: { kind: "available", models: ["glm-5"] },
  269. }));
  270. const resolve = createAccountProviderConnectionResolver({
  271. readSettings: async () => ({
  272. providerFamilyDomain: "bigmodel",
  273. selections: {},
  274. unresolvedFamilies: ["bigmodel"],
  275. }),
  276. loadAccountIdentity: async () => "account",
  277. loadCodingPlanApiKey: async () => "key",
  278. resolveFamilyAvailability: availability,
  279. });
  280. const results = await resolve({
  281. configRevision: "config",
  282. configuredProviders: configuredProviders({ bigmodelTeam: true }),
  283. });
  284. expect(availability).toHaveBeenCalledTimes(2);
  285. expect(availability.mock.calls[1]?.[0].providers.map((p) => p.providerId)).toEqual([
  286. BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
  287. ]);
  288. expect(availability.mock.calls[0]?.[0]).toMatchObject({ family: "zai" });
  289. for (const result of results.filter((entry) =>
  290. entry.providerId.startsWith("account:bigmodel-"),
  291. )) {
  292. expect(result).toMatchObject({
  293. status:
  294. result.providerId === BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan
  295. ? "available"
  296. : "unknown",
  297. current: result.providerId === BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
  298. });
  299. }
  300. });
  301. it("切换账号后只清理该账号的旧权益,不把网络 unknown 当旧账号可用", async () => {
  302. let identity = "account-a";
  303. const resolve = createAccountProviderConnectionResolver({
  304. readSettings: async () => ({
  305. providerFamilyDomain: "bigmodel",
  306. selections: {},
  307. }),
  308. loadAccountIdentity: async (family) => (family === "bigmodel" ? identity : null),
  309. loadCodingPlanApiKey: async () => null,
  310. resolveFamilyAvailability: async () => ({}),
  311. });
  312. const input = {
  313. configRevision: "config",
  314. configuredProviders: configuredProviders(),
  315. };
  316. await resolve(input);
  317. identity = "account-b";
  318. expect(await resolve(input)).toContainEqual({
  319. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
  320. status: "unknown",
  321. connectionKey: expect.any(String),
  322. current: true,
  323. resetPrevious: true,
  324. });
  325. expect((await resolve(input)).some((entry) => entry.resetPrevious)).toBe(false);
  326. });
  327. it("把不可用原因随连接结果发布,供 UI 区分未开通与未连接", async () => {
  328. const resolve = createAccountProviderConnectionResolver({
  329. readSettings: async () => ({
  330. providerFamilyDomain: "bigmodel",
  331. selections: {},
  332. }),
  333. loadAccountIdentity: async (family) => (family === "bigmodel" ? "account" : null),
  334. loadCodingPlanApiKey: async () => null,
  335. resolveFamilyAvailability: async () => ({
  336. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan]: {
  337. kind: "unavailable" as const,
  338. reason: "coding_plan_not_entitled" as const,
  339. },
  340. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan]: {
  341. kind: "unavailable" as const,
  342. reason: "coding_plan_auth_failed" as const,
  343. },
  344. }),
  345. });
  346. const result = await resolve({
  347. configRevision: "config",
  348. configuredProviders: configuredProviders(),
  349. });
  350. // 修复原因:个人套餐"服务端明确无订阅"的原因曾在此丢失,UI 只能统一显示"未连接"。
  351. expect(result).toContainEqual({
  352. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  353. status: "unavailable",
  354. unavailableReason: "not-entitled",
  355. connectionKey: expect.any(String),
  356. current: false,
  357. });
  358. expect(result).toContainEqual({
  359. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
  360. status: "unavailable",
  361. unavailableReason: "credential-failed",
  362. connectionKey: expect.any(String),
  363. current: true,
  364. });
  365. });
  366. it("可用结果不携带不可用原因", async () => {
  367. const resolve = createAccountProviderConnectionResolver({
  368. readSettings: async () => ({
  369. providerFamilyDomain: "bigmodel",
  370. selections: {},
  371. }),
  372. loadAccountIdentity: async (family) => (family === "bigmodel" ? "account" : null),
  373. loadCodingPlanApiKey: async () => null,
  374. resolveFamilyAvailability: async () => ({
  375. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan]: {
  376. kind: "available" as const,
  377. models: [],
  378. },
  379. }),
  380. });
  381. const result = await resolve({
  382. configRevision: "config",
  383. configuredProviders: configuredProviders(),
  384. });
  385. const individual = result.find(
  386. (entry) => entry.providerId === BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  387. );
  388. expect(individual?.status).toBe("available");
  389. expect(individual).not.toHaveProperty("unavailableReason");
  390. });
  391. it("没有保存连接选择也查询已登录 Family 的 Start 权益;未选中不改写 availability", async () => {
  392. const resolveFamilyAvailability = vi.fn(async () => ({
  393. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan]: {
  394. kind: "available" as const,
  395. models: [],
  396. },
  397. }));
  398. const resolve = createAccountProviderConnectionResolver({
  399. readSettings: async () => ({
  400. providerFamilyDomain: "bigmodel",
  401. selections: {},
  402. }),
  403. loadAccountIdentity: async (family) => (family === "bigmodel" ? "account" : null),
  404. loadCodingPlanApiKey: async () => null,
  405. resolveFamilyAvailability,
  406. });
  407. const result = await resolve({
  408. configRevision: "config",
  409. configuredProviders: configuredProviders(),
  410. });
  411. expect(resolveFamilyAvailability).toHaveBeenCalledOnce();
  412. expect(result).toContainEqual({
  413. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
  414. status: "available",
  415. models: [],
  416. connectionKey: expect.any(String),
  417. current: true,
  418. });
  419. });
  420. it.each(["bigmodel", "zai"] as const)(
  421. "%s 登录连接仍单独解析,但不决定旧 Selection 迁移落点",
  422. async (family) => {
  423. const settings = {
  424. providerFamilyDomain: family,
  425. selections: {
  426. [family]: {
  427. kind: "team-coding-plan" as const,
  428. productId: "product",
  429. organizationId: "org",
  430. projectId: "project",
  431. },
  432. },
  433. };
  434. const input = {
  435. access: {
  436. type: "zhipu-account" as const,
  437. accountType: family,
  438. mode: "team-coding-plan" as const,
  439. },
  440. readSettings: async () => settings,
  441. };
  442. const connected = await resolveCurrentAccountAccess({
  443. ...input,
  444. loadAccountIdentity: async () => "account",
  445. });
  446. expect(connected?.planKind).toBe("team-coding-plan");
  447. expect(migrateLegacyModelProviderId(`builtin:${family}-coding-plan`)).toBe(
  448. `account:${family}-individual-coding-plan`,
  449. );
  450. const loggedOut = await resolveCurrentAccountAccess({
  451. ...input,
  452. loadAccountIdentity: async () => null,
  453. });
  454. expect(loggedOut).toBeNull();
  455. expect(migrateLegacyModelProviderId(`builtin:${family}-coding-plan`)).toBe(
  456. `account:${family}-individual-coding-plan`,
  457. );
  458. },
  459. );
  460. it("请求期从当前 Team 连接解析动态 scope", async () => {
  461. await expect(
  462. resolveCurrentAccountAccess({
  463. access: {
  464. type: "zhipu-account",
  465. accountType: "bigmodel",
  466. mode: "team-coding-plan",
  467. },
  468. readSettings: async () => ({
  469. providerFamilyDomain: "bigmodel",
  470. selections: {
  471. bigmodel: {
  472. kind: "team-coding-plan",
  473. productId: "product",
  474. organizationId: "org",
  475. projectId: "project",
  476. },
  477. },
  478. }),
  479. loadAccountIdentity: async () => "account-a",
  480. }),
  481. ).resolves.toEqual({
  482. type: "zhipu-account",
  483. family: "bigmodel",
  484. planKind: "team-coding-plan",
  485. productId: "product",
  486. organizationId: "org",
  487. projectId: "project",
  488. });
  489. });
  490. it("当前 Family 或 Plan 不兼容时不返回请求凭据范围", async () => {
  491. await expect(
  492. resolveCurrentAccountAccess({
  493. access: {
  494. type: "zhipu-account",
  495. accountType: "zai",
  496. mode: "individual-coding-plan",
  497. },
  498. readSettings: async () => ({
  499. providerFamilyDomain: "bigmodel",
  500. selections: { zai: { kind: "individual-coding-plan" } },
  501. }),
  502. loadAccountIdentity: async () => "account-a",
  503. }),
  504. ).resolves.toBeNull();
  505. });
  506. it("只启用当前 Family 已连接 Coding Plan 对应的 Off-Peak Provider", async () => {
  507. const resolve = createAccountProviderConnectionResolver({
  508. readSettings: async () => ({
  509. providerFamilyDomain: "zai",
  510. selections: {
  511. zai: { kind: "individual-coding-plan" },
  512. bigmodel: { kind: "individual-coding-plan" },
  513. },
  514. }),
  515. loadCodingPlanApiKey: async () => "personal-key",
  516. loadAccountIdentity: async () => "account-a",
  517. resolveFamilyAvailability: async ({ family }) =>
  518. family === "zai"
  519. ? {
  520. [BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan]: {
  521. kind: "available",
  522. },
  523. }
  524. : {
  525. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan]: {
  526. kind: "available",
  527. },
  528. },
  529. });
  530. const providers = configuredProviders()
  531. .set(
  532. "account:zai-offpeak-idle-plan",
  533. new ProviderConfig({
  534. group: "zai-family",
  535. access: new ZhipuAccountAccessConfig({
  536. accountType: "zai",
  537. mode: "off-peak",
  538. }),
  539. }),
  540. )
  541. .set(
  542. "account:bigmodel-offpeak-idle-plan",
  543. new ProviderConfig({
  544. group: "bigmodel-family",
  545. access: new ZhipuAccountAccessConfig({
  546. accountType: "bigmodel",
  547. mode: "off-peak",
  548. }),
  549. }),
  550. );
  551. await expect(
  552. resolve({ configRevision: "config-1", configuredProviders: providers }),
  553. ).resolves.toEqual(
  554. expect.arrayContaining([
  555. { providerId: "account:zai-offpeak-idle-plan", status: "available" },
  556. {
  557. providerId: "account:bigmodel-offpeak-idle-plan",
  558. status: "unavailable",
  559. },
  560. ]),
  561. );
  562. });
  563. it("把当前 Family 的 Start/Coding 权益映射为可用状态", async () => {
  564. const resolveFamilyAvailability = vi.fn<AccountProviderFamilyAvailabilityResolver>(
  565. async ({ family }) =>
  566. family === "zai"
  567. ? {
  568. [BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan]: {
  569. kind: "available",
  570. },
  571. [BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan]: {
  572. kind: "unavailable",
  573. reason: "coding_plan_not_entitled",
  574. },
  575. }
  576. : {},
  577. );
  578. const resolve = createAccountProviderConnectionResolver({
  579. readSettings: async () => ({
  580. providerFamilyDomain: "zai",
  581. selections: { zai: { kind: "individual-coding-plan" } },
  582. }),
  583. loadCodingPlanApiKey: async () => "personal-key",
  584. loadAccountIdentity: async () => "account-a",
  585. resolveFamilyAvailability,
  586. });
  587. await expect(
  588. resolve({
  589. configRevision: "config-1",
  590. configuredProviders: configuredProviders(),
  591. }),
  592. ).resolves.toEqual([
  593. {
  594. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  595. status: "available",
  596. connectionKey: expect.any(String),
  597. current: true,
  598. },
  599. {
  600. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
  601. status: "unavailable",
  602. unavailableReason: "not-entitled",
  603. connectionKey: expect.any(String),
  604. current: true,
  605. },
  606. {
  607. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  608. status: "unknown",
  609. connectionKey: expect.any(String),
  610. current: false,
  611. },
  612. {
  613. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
  614. status: "unknown",
  615. connectionKey: expect.any(String),
  616. current: false,
  617. },
  618. ]);
  619. expect(resolveFamilyAvailability).toHaveBeenCalledTimes(2);
  620. });
  621. it("Coding Plan 选中 Team 连接时只投影可用状态", async () => {
  622. const resolve = createAccountProviderConnectionResolver({
  623. readSettings: async () => ({
  624. providerFamilyDomain: "bigmodel",
  625. selections: {
  626. bigmodel: {
  627. kind: "team-coding-plan",
  628. productId: "product",
  629. organizationId: "org",
  630. projectId: "project",
  631. },
  632. },
  633. }),
  634. loadCodingPlanApiKey: async () => null,
  635. loadAccountIdentity: async () => "account-a",
  636. resolveFamilyAvailability: async ({ family }) =>
  637. family === "bigmodel"
  638. ? {
  639. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan]: {
  640. kind: "available",
  641. },
  642. }
  643. : {},
  644. });
  645. const result = await resolve({
  646. configRevision: "config-1",
  647. configuredProviders: configuredProviders({ bigmodelTeam: true }),
  648. });
  649. expect(result).toContainEqual({
  650. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  651. status: "available",
  652. connectionKey: expect.any(String),
  653. current: true,
  654. });
  655. });
  656. it("禁用 Account Provider 仍解析连接,供设置与 Usage 保留账号事实", async () => {
  657. const resolveFamilyAvailability = vi.fn<AccountProviderFamilyAvailabilityResolver>(
  658. async () => ({
  659. [BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan]: {
  660. kind: "available",
  661. },
  662. }),
  663. );
  664. const resolve = createAccountProviderConnectionResolver({
  665. readSettings: async () => ({
  666. providerFamilyDomain: "zai",
  667. selections: { zai: { kind: "individual-coding-plan" } },
  668. }),
  669. loadCodingPlanApiKey: async () => "personal-key",
  670. loadAccountIdentity: async () => "account-a",
  671. resolveFamilyAvailability,
  672. });
  673. await expect(
  674. resolve({
  675. configRevision: "config-1",
  676. configuredProviders: new ProviderConfigMap([
  677. [
  678. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  679. createAccountProviderConfig({
  680. enabled: false,
  681. models: ["zai-coding"],
  682. }),
  683. ],
  684. ]),
  685. }),
  686. ).resolves.toEqual([
  687. {
  688. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  689. status: "available",
  690. connectionKey: expect.any(String),
  691. current: true,
  692. },
  693. ]);
  694. expect(resolveFamilyAvailability).toHaveBeenCalledOnce();
  695. });
  696. it("保留单个 Provider 的 unknown,并按 Config 顺序返回", async () => {
  697. const requestedFamilies: ProviderFamilyDomain[] = [];
  698. const resolve = createAccountProviderConnectionResolver({
  699. readSettings: async () => ({
  700. providerFamilyDomain: null,
  701. selections: {
  702. zai: { kind: "start-plan" },
  703. bigmodel: { kind: "individual-coding-plan" },
  704. },
  705. }),
  706. loadCodingPlanApiKey: async () => null,
  707. loadAccountIdentity: async () => "account-a",
  708. resolveFamilyAvailability: async ({ family }) => {
  709. requestedFamilies.push(family);
  710. return family === "zai"
  711. ? {
  712. [BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan]: {
  713. kind: "available",
  714. models: ["remote-start-model"],
  715. },
  716. }
  717. : {};
  718. },
  719. });
  720. const result = await resolve({
  721. configRevision: "config-1",
  722. configuredProviders: configuredProviders(),
  723. });
  724. expect(requestedFamilies).toEqual(["zai", "bigmodel"]);
  725. expect(result).toEqual([
  726. {
  727. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  728. status: "unknown",
  729. connectionKey: expect.any(String),
  730. current: false,
  731. },
  732. {
  733. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
  734. status: "available",
  735. models: ["remote-start-model"],
  736. connectionKey: expect.any(String),
  737. current: false,
  738. },
  739. {
  740. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  741. status: "unknown",
  742. connectionKey: expect.any(String),
  743. current: false,
  744. },
  745. {
  746. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
  747. status: "unknown",
  748. connectionKey: expect.any(String),
  749. current: false,
  750. },
  751. ]);
  752. });
  753. it("连接身份稳定且随账号改变,不暴露明文账号或凭据", async () => {
  754. let accountIdentity = "account-a";
  755. const resolve = createAccountProviderConnectionResolver({
  756. readSettings: async () => ({
  757. providerFamilyDomain: "zai",
  758. selections: { zai: { kind: "individual-coding-plan" } },
  759. }),
  760. loadCodingPlanApiKey: async () => "rotating-key",
  761. loadAccountIdentity: async () => accountIdentity,
  762. resolveFamilyAvailability: async () => ({
  763. [BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan]: {
  764. kind: "available",
  765. },
  766. }),
  767. });
  768. const input = {
  769. configRevision: "config-1",
  770. configuredProviders: new ProviderConfigMap([
  771. [
  772. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  773. createAccountProviderConfig({ models: ["zai-coding"] }),
  774. ],
  775. ]),
  776. };
  777. const first = await resolve(input);
  778. expect((await resolve(input))[0]?.connectionKey).toBe(first[0]?.connectionKey);
  779. expect(JSON.stringify(first)).not.toContain("account-a");
  780. expect(JSON.stringify(first)).not.toContain("rotating-key");
  781. accountIdentity = "account-b";
  782. expect((await resolve(input))[0]?.connectionKey).not.toBe(first[0]?.connectionKey);
  783. await expect(resolve(input)).resolves.toEqual([
  784. {
  785. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  786. status: "available",
  787. connectionKey: expect.any(String),
  788. current: true,
  789. },
  790. ]);
  791. });
  792. it("缺少账号身份时不查询权益并关闭 Account Provider", async () => {
  793. const resolveFamilyAvailability = vi.fn(async () => ({
  794. [BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan]: {
  795. kind: "available" as const,
  796. },
  797. }));
  798. const resolve = createAccountProviderConnectionResolver({
  799. readSettings: async () => ({
  800. providerFamilyDomain: "zai",
  801. selections: { zai: { kind: "individual-coding-plan" } },
  802. }),
  803. loadCodingPlanApiKey: async () => "personal-key",
  804. loadAccountIdentity: async () => null,
  805. resolveFamilyAvailability,
  806. });
  807. await expect(
  808. resolve({
  809. configRevision: "config-1",
  810. configuredProviders: new ProviderConfigMap([
  811. [
  812. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  813. createAccountProviderConfig({ models: ["zai-coding"] }),
  814. ],
  815. ]),
  816. }),
  817. ).resolves.toEqual([
  818. {
  819. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  820. status: "unavailable",
  821. unavailableReason: "not-connected",
  822. connectionKey: expect.any(String),
  823. current: true,
  824. },
  825. ]);
  826. expect(resolveFamilyAvailability).not.toHaveBeenCalled();
  827. });
  828. });