providerFamilyConnectionVisibility.test.ts 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. import {
  2. BIGMODEL_PROVIDER_ID,
  3. BUILTIN_MODEL_PROVIDER_IDS,
  4. getModelProviderFamilySpec,
  5. ZAI_PROVIDER_ID,
  6. type UsageEntitlementSnapshot,
  7. } from "@zcode/shared";
  8. import type { ProviderSettingsFormProvider } from "@/lib/providerSettingsFormTypes.js";
  9. import { describe, expect, it } from "vitest";
  10. import {
  11. buildVisibleFamilyConnectionItems,
  12. buildVisibleFamilyConnectionKeys,
  13. resolveCodingPlanEntitlementState,
  14. } from "@/settings/model-provider-section/providerFamilyConnectionVisibility.js";
  15. import {
  16. CODING_PLAN_PROVIDER_SPECS,
  17. type CodingPlanStatus,
  18. type ModelProviderNavGroup,
  19. } from "@/settings/model-provider-section/constants.js";
  20. type ProviderOverrides = Partial<ProviderSettingsFormProvider> & {
  21. id?: string;
  22. name?: string;
  23. apiKey?: string;
  24. models?: string[];
  25. };
  26. it("待生效套餐可查看订阅,不借 entitled 点亮执行资格", () => {
  27. const result = resolveCodingPlanEntitlementState({
  28. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
  29. accountEntitled: false,
  30. accountAvailability: "pending",
  31. modelProvidersLoading: false,
  32. entitlement: {
  33. loading: false,
  34. error: null,
  35. snapshot: {
  36. subscription: {
  37. details: [{ productId: "start", productName: "Start Plan", effectiveAt: 2000 }],
  38. },
  39. } as UsageEntitlementSnapshot,
  40. },
  41. });
  42. expect(result.status).toBe("purchased");
  43. expect(result.currentProductId).toBe("start");
  44. });
  45. it("已登录但服务端明确无个人订阅时显示未开通,而不是未连接", () => {
  46. const result = resolveCodingPlanEntitlementState({
  47. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  48. accountEntitled: false,
  49. accountAvailability: "unavailable",
  50. accountUnavailableReason: "not-entitled",
  51. modelProvidersLoading: false,
  52. });
  53. expect(result.status).toBe("notPurchased");
  54. });
  55. it("凭据失效时显示获取失败,既不显示未连接也不显示未开通", () => {
  56. const result = resolveCodingPlanEntitlementState({
  57. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  58. accountEntitled: false,
  59. accountAvailability: "unavailable",
  60. accountUnavailableReason: "credential-failed",
  61. modelProvidersLoading: false,
  62. });
  63. expect(result.status).toBe("unavailable");
  64. });
  65. it.each(["not-connected", "not-authenticated", undefined] as const)(
  66. "未连接 / 未登录 / 原因缺失时仍显示未连接(%s)",
  67. (accountUnavailableReason) => {
  68. const result = resolveCodingPlanEntitlementState({
  69. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  70. accountEntitled: false,
  71. accountAvailability: "unavailable",
  72. accountUnavailableReason,
  73. modelProvidersLoading: false,
  74. });
  75. expect(result.status).toBe("disconnected");
  76. },
  77. );
  78. it("原因未知时保持未连接,不把网络抖动当成未开通", () => {
  79. const result = resolveCodingPlanEntitlementState({
  80. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  81. accountEntitled: false,
  82. accountAvailability: "unknown",
  83. accountUnavailableReason: "not-entitled",
  84. modelProvidersLoading: false,
  85. });
  86. expect(result.status).toBe("disconnected");
  87. });
  88. it.each([
  89. BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
  90. BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
  91. ] as const)("Start Plan 的明确无权益显示暂无套餐(%s)", (providerId) => {
  92. const result = resolveCodingPlanEntitlementState({
  93. providerId,
  94. accountEntitled: false,
  95. accountAvailability: "unavailable",
  96. accountUnavailableReason: "not-entitled",
  97. modelProvidersLoading: false,
  98. });
  99. expect(result.status).toBe("notPurchased");
  100. });
  101. it("Z.AI 个人套餐与服务端无权益判定对称", () => {
  102. const result = resolveCodingPlanEntitlementState({
  103. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  104. accountEntitled: false,
  105. accountAvailability: "unavailable",
  106. accountUnavailableReason: "not-entitled",
  107. modelProvidersLoading: false,
  108. });
  109. expect(result.status).toBe("notPurchased");
  110. });
  111. function createProvider(overrides: ProviderOverrides): ProviderSettingsFormProvider {
  112. const providerId =
  113. overrides.providerId ?? overrides.id ?? BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan;
  114. const modelIds = overrides.models ?? ["glm-5"];
  115. return {
  116. providerId,
  117. executable: overrides.executable ?? true,
  118. enabled: overrides.enabled ?? true,
  119. hasPersonalConfig: false,
  120. personalConfig: {},
  121. config: {
  122. label: overrides.name ?? "BigModel",
  123. access: { type: "api-key", apiKey: overrides.apiKey ?? "sk-test" },
  124. api: { type: "anthropic-messages", baseUrl: "https://example.com" },
  125. builtinModelIds: modelIds,
  126. enabled: overrides.enabled ?? true,
  127. ...overrides.config,
  128. },
  129. models: modelIds.map((modelId) => ({
  130. kind: "candidate" as const,
  131. modelId,
  132. builtin: true,
  133. personalConfig: {},
  134. config: {},
  135. hasPersonalConfig: false,
  136. executable: true,
  137. selectable: true,
  138. })),
  139. };
  140. }
  141. function createCodingPlanItem(
  142. status: "notPurchased" | "purchased",
  143. ): Extract<ModelProviderNavGroup["items"][number], { type: "codingPlan" }> {
  144. return {
  145. key: `coding-plan:${BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan}`,
  146. type: "codingPlan",
  147. presetId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  148. oauthProviderId: BIGMODEL_PROVIDER_ID,
  149. label: "BigModel - Coding Plan",
  150. providerName: "BigModel",
  151. provider: createProvider({
  152. id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  153. name: "BigModel - Coding Plan",
  154. }),
  155. status,
  156. statusActive: status === "purchased",
  157. subscriptionRenewTime: "2026-09-15T00:00:00.000Z",
  158. subscriptionBillingCycle: "monthly",
  159. };
  160. }
  161. function createStartPlanItem(
  162. status: CodingPlanStatus,
  163. ): Extract<ModelProviderNavGroup["items"][number], { type: "codingPlan" }> {
  164. return {
  165. ...createCodingPlanItem(status === "purchased" ? "purchased" : "notPurchased"),
  166. key: `coding-plan:${BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan}`,
  167. presetId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
  168. label: "BigModel - Start Plan",
  169. provider: createProvider({
  170. id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
  171. name: "BigModel - Start Plan",
  172. }),
  173. status,
  174. statusActive: status === "purchased",
  175. };
  176. }
  177. function createNoTeamQuotaEntitlementSnapshot(): UsageEntitlementSnapshot {
  178. return {
  179. generatedAt: 1,
  180. authenticated: true,
  181. context: {
  182. scope: "team",
  183. organizationId: "org-a",
  184. projectId: "project-a",
  185. productId: "product-team",
  186. displayName: "Org A",
  187. },
  188. provider: {
  189. id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  190. name: "BigModel - Coding Plan",
  191. },
  192. remaining: null,
  193. quota: null,
  194. subscription: null,
  195. unavailableReason: "no_plan",
  196. };
  197. }
  198. describe("buildVisibleFamilyConnectionKeys", () => {
  199. it("Start 与个人 Coding 同时有效时保留两个连接项", () => {
  200. const items = buildVisibleFamilyConnectionItems({
  201. items: [createCodingPlanItem("purchased"), createStartPlanItem("purchased")],
  202. subscribedTeamProducts: [],
  203. showPurchasedTeamPlanFallback: false,
  204. });
  205. expect(items.map((item) => item.key)).toEqual([
  206. `coding-plan:${BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan}`,
  207. `coding-plan:${BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan}`,
  208. ]);
  209. });
  210. it("Start 与 Team Plan 同时有效时仍保留 Start 连接项", () => {
  211. const items = buildVisibleFamilyConnectionItems({
  212. items: [createCodingPlanItem("notPurchased"), createStartPlanItem("purchased")],
  213. subscribedTeamProducts: [
  214. {
  215. productId: "team-pro",
  216. productName: "Team Pro",
  217. productBigTitle: "Team Pro",
  218. originalAmount: 598,
  219. payAmount: 598,
  220. priceUnit: "year",
  221. priceCurrency: "CNY",
  222. productEquityList: [],
  223. hasPreview: true,
  224. enterpriseProduct: {
  225. productId: "team-pro",
  226. tier: "PRO",
  227. subscribeMode: "CONTINUOUS",
  228. subscribePeriod: "YEARLY",
  229. subscribed: true,
  230. organizationId: "org-a",
  231. organizationName: "Org A",
  232. projectId: "project-a",
  233. projectName: "Project A",
  234. },
  235. tier: "PRO",
  236. subscribeMode: "CONTINUOUS",
  237. subscribePeriod: "YEARLY",
  238. purchaseMethodName: "yearly",
  239. subscribed: true,
  240. organizationId: "org-a",
  241. organizationName: "Org A",
  242. projectId: "project-a",
  243. projectName: "Project A",
  244. },
  245. ],
  246. showPurchasedTeamPlanFallback: false,
  247. });
  248. expect(items.map((item) => item.key)).toContain(
  249. `coding-plan:${BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan}`,
  250. );
  251. expect(items.some((item) => item.type === "teamPlan")).toBe(true);
  252. });
  253. it("已选 Start 临时不可用时保留入口,明确无权益时隐藏", () => {
  254. const selectedConnectionKey = `coding-plan:${BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan}`;
  255. const common = {
  256. subscribedTeamProducts: [],
  257. showPurchasedTeamPlanFallback: false,
  258. connectionSelections: { bigmodel: { kind: "start-plan" } } as const,
  259. };
  260. expect(
  261. buildVisibleFamilyConnectionItems({
  262. ...common,
  263. items: [createCodingPlanItem("purchased"), createStartPlanItem("unavailable")],
  264. }).map((item) => item.key),
  265. ).toContain(selectedConnectionKey);
  266. expect(
  267. buildVisibleFamilyConnectionItems({
  268. ...common,
  269. items: [createCodingPlanItem("purchased"), createStartPlanItem("notPurchased")],
  270. }).map((item) => item.key),
  271. ).not.toContain(selectedConnectionKey);
  272. });
  273. it("模型删空后仍按 API Key 和权益保持 Coding Plan 已购状态", () => {
  274. const provider = createProvider({
  275. id: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  276. apiKey: "coding-plan-key",
  277. models: [],
  278. });
  279. expect(
  280. resolveCodingPlanEntitlementState({
  281. providerId: provider.providerId,
  282. accountEntitled: true,
  283. modelProvidersLoading: false,
  284. entitlement: {
  285. loading: false,
  286. error: null,
  287. snapshot: {
  288. generatedAt: 1,
  289. authenticated: true,
  290. provider: {
  291. id: provider.providerId,
  292. name: provider.config.label ?? provider.providerId,
  293. },
  294. remaining: null,
  295. quota: null,
  296. subscription: {
  297. identityType: "email",
  298. identityMasked: "u***@example.com",
  299. details: [
  300. {
  301. productId: "coding-lite",
  302. productName: "GLM Coding Lite",
  303. purchaseTime: null,
  304. beginTime: null,
  305. expireTime: null,
  306. },
  307. ],
  308. },
  309. },
  310. },
  311. }).status,
  312. ).toBe("purchased");
  313. });
  314. it.each(["not_authenticated", "not_configured", "unavailable"] as const)(
  315. "账号已连接时 %s 权益快照不会被误报为未购买",
  316. (unavailableReason) => {
  317. expect(
  318. resolveCodingPlanEntitlementState({
  319. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  320. accountEntitled: true,
  321. modelProvidersLoading: false,
  322. entitlement: {
  323. loading: false,
  324. error: null,
  325. snapshot: {
  326. generatedAt: 1,
  327. authenticated: unavailableReason !== "not_authenticated",
  328. provider: null,
  329. remaining: null,
  330. quota: null,
  331. subscription: null,
  332. unavailableReason,
  333. },
  334. },
  335. }).status,
  336. ).toBe("unavailable");
  337. },
  338. );
  339. it("BigModel 个人套餐管理入口使用当前环境的个人套餐页", () => {
  340. const bigmodelSpecs = CODING_PLAN_PROVIDER_SPECS.filter(
  341. (item) =>
  342. item.id === BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan ||
  343. item.id === BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
  344. );
  345. expect(bigmodelSpecs.map((item) => item.purchaseUrl)).toEqual([
  346. "https://bigmodel.cn/coding-plan/personal/overview",
  347. "https://bigmodel.cn/coding-plan/personal/overview",
  348. ]);
  349. });
  350. it("输入框连接方式保留未购买 Coding Plan", () => {
  351. const keys = buildVisibleFamilyConnectionKeys({
  352. items: [createCodingPlanItem("notPurchased")],
  353. subscribedTeamProducts: [],
  354. showPurchasedTeamPlanFallback: false,
  355. modelProviders: [
  356. createProvider({
  357. id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
  358. apiKey: "",
  359. models: ["glm-api-key"],
  360. }),
  361. ],
  362. });
  363. expect([...keys]).toEqual([
  364. `coding-plan:${BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan}`,
  365. ]);
  366. });
  367. it("已购 Coding Plan 会进入输入框连接方式", () => {
  368. const keys = buildVisibleFamilyConnectionKeys({
  369. items: [createCodingPlanItem("purchased")],
  370. subscribedTeamProducts: [],
  371. showPurchasedTeamPlanFallback: false,
  372. modelProviders: [
  373. createProvider({
  374. id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
  375. apiKey: "sk-api",
  376. models: ["glm-api-key"],
  377. }),
  378. ],
  379. });
  380. expect([...keys]).toEqual([
  381. `coding-plan:${BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan}`,
  382. ]);
  383. });
  384. it("Team Plan 管理入口使用当前环境的团队套餐列表页", () => {
  385. const items = buildVisibleFamilyConnectionItems({
  386. items: [
  387. {
  388. ...createCodingPlanItem("purchased"),
  389. purchaseUrl: "https://bigmodel.cn/coding-plan/personal/overview",
  390. },
  391. ],
  392. subscribedTeamProducts: [
  393. {
  394. productId: "product-team",
  395. productName: "GLM Coding Team",
  396. productBigTitle: "GLM Coding Team",
  397. originalAmount: 598,
  398. payAmount: 598,
  399. priceUnit: "year",
  400. priceCurrency: "CNY",
  401. productEquityList: [],
  402. hasPreview: true,
  403. enterpriseProduct: {
  404. productId: "product-team",
  405. tier: "PRO",
  406. subscribeMode: "CONTINUOUS",
  407. subscribePeriod: "YEARLY",
  408. subscribed: true,
  409. organizationId: "org-a",
  410. organizationName: "Org A",
  411. projectId: "project-a",
  412. projectName: "Project A",
  413. },
  414. tier: "PRO",
  415. subscribeMode: "CONTINUOUS",
  416. subscribePeriod: "YEARLY",
  417. purchaseMethodName: "yearly",
  418. subscribed: true,
  419. organizationId: "org-a",
  420. organizationName: "Org A",
  421. projectId: "project-a",
  422. projectName: "Project A",
  423. },
  424. ],
  425. showPurchasedTeamPlanFallback: false,
  426. });
  427. const teamItem = items.find((item) => item.type === "teamPlan");
  428. expect(teamItem?.purchaseUrl).toBe("https://bigmodel.cn/coding-plan/team/plans");
  429. expect(teamItem?.teamPlanName).toBe("Org A");
  430. expect(teamItem?.planLevel).toBe("Org A");
  431. });
  432. it("缺少组织名称时不生成空白 Team Plan 连接项", () => {
  433. const items = buildVisibleFamilyConnectionItems({
  434. items: [createCodingPlanItem("purchased")],
  435. subscribedTeamProducts: [
  436. {
  437. productId: "product-team",
  438. productName: "GLM Coding Team",
  439. productBigTitle: "GLM Coding Team",
  440. originalAmount: 598,
  441. payAmount: 598,
  442. priceUnit: "year",
  443. priceCurrency: "CNY",
  444. productEquityList: [],
  445. hasPreview: true,
  446. enterpriseProduct: {
  447. productId: "product-team",
  448. tier: "PRO",
  449. subscribeMode: "CONTINUOUS",
  450. subscribePeriod: "YEARLY",
  451. subscribed: true,
  452. organizationId: "org-a",
  453. organizationName: null,
  454. projectId: "project-a",
  455. projectName: "Project A",
  456. },
  457. tier: "PRO",
  458. subscribeMode: "CONTINUOUS",
  459. subscribePeriod: "YEARLY",
  460. purchaseMethodName: "yearly",
  461. subscribed: true,
  462. organizationId: "org-a",
  463. organizationName: null,
  464. projectId: "project-a",
  465. projectName: "Project A",
  466. },
  467. ],
  468. showPurchasedTeamPlanFallback: false,
  469. });
  470. expect(items.some((item) => item.type === "teamPlan")).toBe(false);
  471. });
  472. it("Team Plan 项目 key 不可用时保留连接项但标成不可用", () => {
  473. const items = buildVisibleFamilyConnectionItems({
  474. items: [createCodingPlanItem("purchased")],
  475. subscribedTeamProducts: [
  476. {
  477. productId: "product-team",
  478. productName: "GLM Coding Team",
  479. productBigTitle: "GLM Coding Team",
  480. originalAmount: 598,
  481. payAmount: 598,
  482. priceUnit: "year",
  483. priceCurrency: "CNY",
  484. productEquityList: [],
  485. hasPreview: true,
  486. enterpriseProduct: {
  487. productId: "product-team",
  488. tier: "PRO",
  489. subscribeMode: "CONTINUOUS",
  490. subscribePeriod: "YEARLY",
  491. subscribed: true,
  492. teamProjects: [
  493. {
  494. organizationId: "org-a",
  495. organizationName: "Org A",
  496. projectId: "project-a",
  497. projectName: "Project A",
  498. apiKeyStatus: "unavailable",
  499. apiKeyUnavailableReason: "no_valid_team_plan_authorization",
  500. },
  501. ],
  502. },
  503. tier: "PRO",
  504. subscribeMode: "CONTINUOUS",
  505. subscribePeriod: "YEARLY",
  506. purchaseMethodName: "yearly",
  507. subscribed: true,
  508. teamProjects: [
  509. {
  510. organizationId: "org-a",
  511. organizationName: "Org A",
  512. projectId: "project-a",
  513. projectName: "Project A",
  514. apiKeyStatus: "unavailable",
  515. apiKeyUnavailableReason: "no_valid_team_plan_authorization",
  516. },
  517. ],
  518. },
  519. ],
  520. showPurchasedTeamPlanFallback: false,
  521. });
  522. const teamItem = items.find((item) => item.type === "teamPlan");
  523. expect(teamItem).toMatchObject({
  524. key: `team:bigmodel:product-team:org-a:project-a`,
  525. status: "unavailable",
  526. statusActive: false,
  527. availabilityReason: "credential-unavailable",
  528. statusLabelId: undefined,
  529. inactivePlanTitle: "Org A",
  530. teamPlanName: "Org A",
  531. });
  532. });
  533. it("Team Plan quota 权益失败时不继承个人续费日期且不点亮", () => {
  534. const items = buildVisibleFamilyConnectionItems({
  535. items: [createCodingPlanItem("purchased")],
  536. codingPlanEntitlements: {
  537. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan]: {
  538. loading: false,
  539. error: null,
  540. snapshot: createNoTeamQuotaEntitlementSnapshot(),
  541. refresh: async () => {},
  542. },
  543. },
  544. subscribedTeamProducts: [
  545. {
  546. productId: "product-team",
  547. productName: "GLM Coding Team",
  548. productBigTitle: "GLM Coding Team",
  549. originalAmount: 598,
  550. payAmount: 598,
  551. priceUnit: "year",
  552. priceCurrency: "CNY",
  553. productEquityList: [],
  554. hasPreview: true,
  555. enterpriseProduct: {
  556. productId: "product-team",
  557. tier: "PRO",
  558. subscribeMode: "CONTINUOUS",
  559. subscribePeriod: "YEARLY",
  560. subscribed: true,
  561. teamProjects: [
  562. {
  563. organizationId: "org-a",
  564. organizationName: "Org A",
  565. projectId: "project-a",
  566. projectName: "Project A",
  567. apiKeyStatus: "available",
  568. },
  569. ],
  570. },
  571. tier: "PRO",
  572. subscribeMode: "CONTINUOUS",
  573. subscribePeriod: "YEARLY",
  574. purchaseMethodName: "yearly",
  575. subscribed: true,
  576. teamProjects: [
  577. {
  578. organizationId: "org-a",
  579. organizationName: "Org A",
  580. projectId: "project-a",
  581. projectName: "Project A",
  582. apiKeyStatus: "available",
  583. },
  584. ],
  585. },
  586. ],
  587. showPurchasedTeamPlanFallback: false,
  588. });
  589. const teamItem = items.find((item) => item.type === "teamPlan");
  590. expect(teamItem).toMatchObject({
  591. status: "unavailable",
  592. statusActive: false,
  593. statusLabelId: "settings.modelProvider.codingPlan.status.teamUnavailable",
  594. });
  595. expect(teamItem).toMatchObject({
  596. subscriptionRenewTime: null,
  597. subscriptionBillingCycle: null,
  598. });
  599. });
  600. it("Team Plan quota 仍在加载或请求报错时不提前标成未分配", () => {
  601. for (const entitlement of [
  602. { loading: true, error: null },
  603. { loading: false, error: new Error("network failed") },
  604. ]) {
  605. const items = buildVisibleFamilyConnectionItems({
  606. items: [createCodingPlanItem("purchased")],
  607. codingPlanEntitlements: {
  608. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan]: {
  609. ...entitlement,
  610. snapshot: createNoTeamQuotaEntitlementSnapshot(),
  611. refresh: async () => {},
  612. },
  613. },
  614. subscribedTeamProducts: [
  615. {
  616. productId: "product-team",
  617. productName: "GLM Coding Team",
  618. productBigTitle: "GLM Coding Team",
  619. originalAmount: 598,
  620. payAmount: 598,
  621. priceUnit: "year",
  622. priceCurrency: "CNY",
  623. productEquityList: [],
  624. hasPreview: true,
  625. enterpriseProduct: {
  626. productId: "product-team",
  627. tier: "PRO",
  628. subscribeMode: "CONTINUOUS",
  629. subscribePeriod: "YEARLY",
  630. subscribed: true,
  631. teamProjects: [
  632. {
  633. organizationId: "org-a",
  634. organizationName: "Org A",
  635. projectId: "project-a",
  636. projectName: "Project A",
  637. apiKeyStatus: "available",
  638. },
  639. ],
  640. },
  641. tier: "PRO",
  642. subscribeMode: "CONTINUOUS",
  643. subscribePeriod: "YEARLY",
  644. purchaseMethodName: "yearly",
  645. subscribed: true,
  646. teamProjects: [
  647. {
  648. organizationId: "org-a",
  649. organizationName: "Org A",
  650. projectId: "project-a",
  651. projectName: "Project A",
  652. apiKeyStatus: "available",
  653. },
  654. ],
  655. },
  656. ],
  657. showPurchasedTeamPlanFallback: false,
  658. });
  659. const teamItem = items.find((item) => item.type === "teamPlan");
  660. expect(teamItem?.statusLabelId).not.toBe(
  661. "settings.modelProvider.codingPlan.status.teamUnavailable",
  662. );
  663. }
  664. });
  665. it("优先使用 entitlement snapshot 团队上下文生成 Team Plan 连接项", () => {
  666. const items = buildVisibleFamilyConnectionItems({
  667. items: [createCodingPlanItem("purchased")],
  668. codingPlanEntitlements: {
  669. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan]: {
  670. loading: false,
  671. error: null,
  672. snapshot: {
  673. generatedAt: 1,
  674. authenticated: true,
  675. context: {
  676. scope: "team",
  677. organizationId: "org-snapshot",
  678. projectId: "project-snapshot",
  679. productId: "product-snapshot",
  680. displayName: "Snapshot Team",
  681. },
  682. provider: {
  683. id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  684. name: "BigModel - Coding Plan",
  685. },
  686. remaining: null,
  687. subscription: null,
  688. quota: null,
  689. },
  690. },
  691. },
  692. subscribedTeamProducts: [],
  693. showPurchasedTeamPlanFallback: false,
  694. });
  695. const teamItem = items.find((item) => item.type === "teamPlan");
  696. expect(teamItem?.key).toBe(`team:bigmodel:product-snapshot:org-snapshot:project-snapshot`);
  697. expect(teamItem?.teamPlanName).toBe("Snapshot Team");
  698. expect(teamItem?.organizationId).toBe("org-snapshot");
  699. expect(teamItem?.projectId).toBe("project-snapshot");
  700. });
  701. it("enterprise products 只校正 entitlement 生成的同项目 Team Plan 元数据", () => {
  702. const items = buildVisibleFamilyConnectionItems({
  703. items: [createCodingPlanItem("purchased")],
  704. codingPlanEntitlements: {
  705. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan]: {
  706. loading: false,
  707. error: null,
  708. snapshot: {
  709. generatedAt: 1,
  710. authenticated: true,
  711. context: {
  712. scope: "team",
  713. organizationId: "org-a",
  714. projectId: "project-a",
  715. productId: "product-old",
  716. displayName: "Snapshot Team",
  717. },
  718. provider: {
  719. id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  720. name: "BigModel - Coding Plan",
  721. },
  722. remaining: null,
  723. subscription: null,
  724. quota: null,
  725. },
  726. },
  727. },
  728. subscribedTeamProducts: [
  729. {
  730. productId: "product-new",
  731. productName: "GLM Coding Team",
  732. productBigTitle: "GLM Coding Team",
  733. originalAmount: 598,
  734. payAmount: 598,
  735. priceUnit: "year",
  736. priceCurrency: "CNY",
  737. productEquityList: [],
  738. hasPreview: true,
  739. enterpriseProduct: {
  740. productId: "product-new",
  741. tier: "PRO",
  742. subscribeMode: "CONTINUOUS",
  743. subscribePeriod: "YEARLY",
  744. subscribed: true,
  745. organizationId: "org-a",
  746. organizationName: "Org A",
  747. projectId: "project-a",
  748. projectName: "Corrected Project",
  749. },
  750. tier: "PRO",
  751. subscribeMode: "CONTINUOUS",
  752. subscribePeriod: "YEARLY",
  753. purchaseMethodName: "yearly",
  754. subscribed: true,
  755. organizationId: "org-a",
  756. organizationName: "Org A",
  757. projectId: "project-a",
  758. projectName: "Corrected Project",
  759. },
  760. ],
  761. showPurchasedTeamPlanFallback: false,
  762. });
  763. const teamItems = items.filter((item) => item.type === "teamPlan");
  764. expect(teamItems).toHaveLength(1);
  765. expect(teamItems[0]?.key).toBe(`team:bigmodel:product-new:org-a:project-a`);
  766. expect(teamItems[0]?.teamPlanName).toBe("Org A");
  767. });
  768. it("enterprise pricing 未返回团队项目时按已保存 selected key 展示 Team Plan fallback", () => {
  769. const items = buildVisibleFamilyConnectionItems({
  770. items: [createCodingPlanItem("purchased")],
  771. subscribedTeamProducts: [],
  772. showPurchasedTeamPlanFallback: true,
  773. teamPlanSelections: {
  774. bigmodel: {
  775. kind: "team-coding-plan",
  776. productId: "product-saved",
  777. organizationId: "org-saved",
  778. projectId: "project-saved",
  779. },
  780. },
  781. });
  782. const teamItem = items.find((item) => item.type === "teamPlan");
  783. expect(teamItem).toMatchObject({
  784. key: `team:bigmodel:product-saved:org-saved:project-saved`,
  785. organizationId: "org-saved",
  786. projectId: "project-saved",
  787. status: "purchased",
  788. });
  789. });
  790. });
  791. describe("zai family Team Plan 可见性对称化", () => {
  792. function createZaiCodingPlanItem(
  793. status: "notPurchased" | "purchased",
  794. ): Extract<ModelProviderNavGroup["items"][number], { type: "codingPlan" }> {
  795. return {
  796. key: `coding-plan:${BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan}`,
  797. type: "codingPlan",
  798. presetId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  799. oauthProviderId: ZAI_PROVIDER_ID,
  800. label: "Z.ai - Coding Plan",
  801. providerName: "Z.ai",
  802. provider: createProvider({
  803. id: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  804. name: "Z.ai - Coding Plan",
  805. }),
  806. status,
  807. statusActive: status === "purchased",
  808. };
  809. }
  810. it("zai family 的已订阅 Team Plan 会生成 zai 前缀连接项", () => {
  811. const items = buildVisibleFamilyConnectionItems({
  812. items: [createCodingPlanItem("purchased"), createZaiCodingPlanItem("purchased")],
  813. subscribedTeamProducts: [
  814. {
  815. productId: "zai-team-pro",
  816. productName: "Z.ai Coding Team",
  817. productBigTitle: "Z.ai Coding Team",
  818. originalAmount: 216,
  819. payAmount: 216,
  820. priceUnit: "year",
  821. priceCurrency: "USD",
  822. productEquityList: [],
  823. hasPreview: true,
  824. enterpriseProduct: {
  825. productId: "zai-team-pro",
  826. tier: "PRO",
  827. subscribeMode: "CONTINUOUS",
  828. subscribePeriod: "YEARLY",
  829. subscribed: true,
  830. organizationId: "zai-org",
  831. organizationName: "Zai Org",
  832. projectId: "zai-project",
  833. projectName: "Zai Project",
  834. },
  835. tier: "PRO",
  836. subscribeMode: "CONTINUOUS",
  837. subscribePeriod: "YEARLY",
  838. purchaseMethodName: "yearly",
  839. subscribed: true,
  840. organizationId: "zai-org",
  841. organizationName: "Zai Org",
  842. projectId: "zai-project",
  843. projectName: "Zai Project",
  844. // 关键:标记 family=zai,触发 zai codingPlanItem 派生 + zai key 前缀
  845. family: "zai",
  846. },
  847. ],
  848. showPurchasedTeamPlanFallback: false,
  849. });
  850. const teamItem = items.find((item) => item.type === "teamPlan");
  851. expect(teamItem).toBeDefined();
  852. // 关键:key 前缀必须是 zai-coding-plan,不能误用 bigmodel
  853. expect(teamItem?.key).toBe(`team:zai:zai-team-pro:zai-org:zai-project`);
  854. // providerName 应继承自 zai codingPlanItem
  855. expect(teamItem?.providerName).toBe("Z.ai");
  856. expect(teamItem?.teamPlanName).toBe("Zai Org");
  857. expect(teamItem?.purchaseUrl).toBe(getModelProviderFamilySpec("zai").teamCodingPlanManageUrl);
  858. });
  859. it("未标记 family 的 product 仍走 bigmodel(向后兼容)", () => {
  860. const items = buildVisibleFamilyConnectionItems({
  861. items: [createCodingPlanItem("purchased")],
  862. subscribedTeamProducts: [
  863. {
  864. productId: "product-team",
  865. productName: "GLM Coding Team",
  866. productBigTitle: "GLM Coding Team",
  867. originalAmount: 598,
  868. payAmount: 598,
  869. priceUnit: "year",
  870. priceCurrency: "CNY",
  871. productEquityList: [],
  872. hasPreview: true,
  873. enterpriseProduct: {
  874. productId: "product-team",
  875. tier: "PRO",
  876. subscribeMode: "CONTINUOUS",
  877. subscribePeriod: "YEARLY",
  878. subscribed: true,
  879. organizationId: "org-a",
  880. organizationName: "Org A",
  881. projectId: "project-a",
  882. projectName: "Project A",
  883. },
  884. tier: "PRO",
  885. subscribeMode: "CONTINUOUS",
  886. subscribePeriod: "YEARLY",
  887. purchaseMethodName: "yearly",
  888. subscribed: true,
  889. organizationId: "org-a",
  890. organizationName: "Org A",
  891. projectId: "project-a",
  892. projectName: "Project A",
  893. // 不标记 family
  894. },
  895. ],
  896. showPurchasedTeamPlanFallback: false,
  897. });
  898. const teamItem = items.find((item) => item.type === "teamPlan");
  899. expect(teamItem?.key).toBe(`team:bigmodel:product-team:org-a:project-a`);
  900. });
  901. it("zai 和 bigmodel 同名团队不会互相覆盖(family 维度去重)", () => {
  902. const items = buildVisibleFamilyConnectionItems({
  903. items: [createCodingPlanItem("purchased"), createZaiCodingPlanItem("purchased")],
  904. subscribedTeamProducts: [
  905. {
  906. productId: "shared-product",
  907. productName: "Team",
  908. productBigTitle: "Team",
  909. originalAmount: 100,
  910. payAmount: 100,
  911. priceUnit: "year",
  912. priceCurrency: "CNY",
  913. productEquityList: [],
  914. hasPreview: true,
  915. enterpriseProduct: {
  916. productId: "shared-product",
  917. tier: "PRO",
  918. subscribeMode: "CONTINUOUS",
  919. subscribePeriod: "YEARLY",
  920. subscribed: true,
  921. organizationId: "org-same",
  922. organizationName: "Same Org",
  923. projectId: "proj-same",
  924. projectName: "Same Project",
  925. },
  926. tier: "PRO",
  927. subscribeMode: "CONTINUOUS",
  928. subscribePeriod: "YEARLY",
  929. purchaseMethodName: "yearly",
  930. subscribed: true,
  931. organizationId: "org-same",
  932. organizationName: "Same Org",
  933. projectId: "proj-same",
  934. projectName: "Same Project",
  935. family: "bigmodel",
  936. },
  937. {
  938. productId: "shared-product",
  939. productName: "Team",
  940. productBigTitle: "Team",
  941. originalAmount: 100,
  942. payAmount: 100,
  943. priceUnit: "year",
  944. priceCurrency: "USD",
  945. productEquityList: [],
  946. hasPreview: true,
  947. enterpriseProduct: {
  948. productId: "shared-product",
  949. tier: "PRO",
  950. subscribeMode: "CONTINUOUS",
  951. subscribePeriod: "YEARLY",
  952. subscribed: true,
  953. organizationId: "org-same",
  954. organizationName: "Same Org",
  955. projectId: "proj-same",
  956. projectName: "Same Project",
  957. },
  958. tier: "PRO",
  959. subscribeMode: "CONTINUOUS",
  960. subscribePeriod: "YEARLY",
  961. purchaseMethodName: "yearly",
  962. subscribed: true,
  963. organizationId: "org-same",
  964. organizationName: "Same Org",
  965. projectId: "proj-same",
  966. projectName: "Same Project",
  967. family: "zai",
  968. },
  969. ],
  970. showPurchasedTeamPlanFallback: false,
  971. });
  972. const teamItems = items.filter((item) => item.type === "teamPlan");
  973. // 关键:两个 family 各生成一个,不能因 productId+org+project 相同而互相覆盖
  974. expect(teamItems).toHaveLength(2);
  975. const keys = teamItems.map((item) => item.key);
  976. expect(keys).toContain(`team:bigmodel:shared-product:org-same:proj-same`);
  977. expect(keys).toContain(`team:zai:shared-product:org-same:proj-same`);
  978. });
  979. it("zai-only 视图(无 bigmodel codingPlan item)仍生成 zai Team Plan 连接项", () => {
  980. // 修复原因:appendSubscribedTeamPlanItems 原有 `if (!bigmodelCodingPlanItem) return items`
  981. // 前置守卫。当设置页 providerFamilyDomain === "zai" 时,传入的 codingPlanItems 只含 zai family,
  982. // 没有 bigmodelCodingPlan,守卫直接短路,zai teamPlan item 永远不生成,
  983. // 导致 pickFamilyModeNavigationItem 找不到 saved team item → selectedNavItem=null → Plan Card 永远 loading。
  984. const items = buildVisibleFamilyConnectionItems({
  985. // 关键:只有 zai codingPlan item,没有 bigmodel
  986. items: [createZaiCodingPlanItem("notPurchased")],
  987. subscribedTeamProducts: [],
  988. showPurchasedTeamPlanFallback: true,
  989. // 模拟已落盘的 zai team plan selectedKey(和 setting.json 里的格式一致)
  990. teamPlanSelections: {
  991. zai: {
  992. kind: "team-coding-plan",
  993. productId: "zai-pro",
  994. organizationId: "zai-org-1",
  995. projectId: "zai-proj-1",
  996. },
  997. },
  998. });
  999. const teamItem = items.find((item) => item.type === "teamPlan");
  1000. // 关键:必须生成 zai teamPlan item,不能因为 bigmodelCodingPlanItem 不存在就整体短路
  1001. expect(teamItem).toBeDefined();
  1002. expect(teamItem?.key).toBe(`team:zai:zai-pro:zai-org-1:zai-proj-1`);
  1003. });
  1004. });
  1005. it("Start 查询未知不能显示未登录,查询失败可重试", () => {
  1006. expect(
  1007. resolveCodingPlanEntitlementState({
  1008. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
  1009. accountEntitled: false,
  1010. accountAvailability: "unknown",
  1011. modelProvidersLoading: false,
  1012. }).status,
  1013. ).toBe("unavailable");
  1014. });