providerFamilyConnectionVisibility.test.ts 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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. expect(teamItem?.key).not.toContain("bigmodel");
  855. // providerName 应继承自 zai codingPlanItem
  856. expect(teamItem?.providerName).toBe("Z.ai");
  857. expect(teamItem?.teamPlanName).toBe("Zai Org");
  858. expect(teamItem?.purchaseUrl).toBe(getModelProviderFamilySpec("zai").teamCodingPlanManageUrl);
  859. });
  860. it("未标记 family 的 product 仍走 bigmodel(向后兼容)", () => {
  861. const items = buildVisibleFamilyConnectionItems({
  862. items: [createCodingPlanItem("purchased")],
  863. subscribedTeamProducts: [
  864. {
  865. productId: "product-team",
  866. productName: "GLM Coding Team",
  867. productBigTitle: "GLM Coding Team",
  868. originalAmount: 598,
  869. payAmount: 598,
  870. priceUnit: "year",
  871. priceCurrency: "CNY",
  872. productEquityList: [],
  873. hasPreview: true,
  874. enterpriseProduct: {
  875. productId: "product-team",
  876. tier: "PRO",
  877. subscribeMode: "CONTINUOUS",
  878. subscribePeriod: "YEARLY",
  879. subscribed: true,
  880. organizationId: "org-a",
  881. organizationName: "Org A",
  882. projectId: "project-a",
  883. projectName: "Project A",
  884. },
  885. tier: "PRO",
  886. subscribeMode: "CONTINUOUS",
  887. subscribePeriod: "YEARLY",
  888. purchaseMethodName: "yearly",
  889. subscribed: true,
  890. organizationId: "org-a",
  891. organizationName: "Org A",
  892. projectId: "project-a",
  893. projectName: "Project A",
  894. // 不标记 family
  895. },
  896. ],
  897. showPurchasedTeamPlanFallback: false,
  898. });
  899. const teamItem = items.find((item) => item.type === "teamPlan");
  900. expect(teamItem?.key).toBe(`team:bigmodel:product-team:org-a:project-a`);
  901. });
  902. it("zai 和 bigmodel 同名团队不会互相覆盖(family 维度去重)", () => {
  903. const items = buildVisibleFamilyConnectionItems({
  904. items: [createCodingPlanItem("purchased"), createZaiCodingPlanItem("purchased")],
  905. subscribedTeamProducts: [
  906. {
  907. productId: "shared-product",
  908. productName: "Team",
  909. productBigTitle: "Team",
  910. originalAmount: 100,
  911. payAmount: 100,
  912. priceUnit: "year",
  913. priceCurrency: "CNY",
  914. productEquityList: [],
  915. hasPreview: true,
  916. enterpriseProduct: {
  917. productId: "shared-product",
  918. tier: "PRO",
  919. subscribeMode: "CONTINUOUS",
  920. subscribePeriod: "YEARLY",
  921. subscribed: true,
  922. organizationId: "org-same",
  923. organizationName: "Same Org",
  924. projectId: "proj-same",
  925. projectName: "Same Project",
  926. },
  927. tier: "PRO",
  928. subscribeMode: "CONTINUOUS",
  929. subscribePeriod: "YEARLY",
  930. purchaseMethodName: "yearly",
  931. subscribed: true,
  932. organizationId: "org-same",
  933. organizationName: "Same Org",
  934. projectId: "proj-same",
  935. projectName: "Same Project",
  936. family: "bigmodel",
  937. },
  938. {
  939. productId: "shared-product",
  940. productName: "Team",
  941. productBigTitle: "Team",
  942. originalAmount: 100,
  943. payAmount: 100,
  944. priceUnit: "year",
  945. priceCurrency: "USD",
  946. productEquityList: [],
  947. hasPreview: true,
  948. enterpriseProduct: {
  949. productId: "shared-product",
  950. tier: "PRO",
  951. subscribeMode: "CONTINUOUS",
  952. subscribePeriod: "YEARLY",
  953. subscribed: true,
  954. organizationId: "org-same",
  955. organizationName: "Same Org",
  956. projectId: "proj-same",
  957. projectName: "Same Project",
  958. },
  959. tier: "PRO",
  960. subscribeMode: "CONTINUOUS",
  961. subscribePeriod: "YEARLY",
  962. purchaseMethodName: "yearly",
  963. subscribed: true,
  964. organizationId: "org-same",
  965. organizationName: "Same Org",
  966. projectId: "proj-same",
  967. projectName: "Same Project",
  968. family: "zai",
  969. },
  970. ],
  971. showPurchasedTeamPlanFallback: false,
  972. });
  973. const teamItems = items.filter((item) => item.type === "teamPlan");
  974. // 关键:两个 family 各生成一个,不能因 productId+org+project 相同而互相覆盖
  975. expect(teamItems).toHaveLength(2);
  976. const keys = teamItems.map((item) => item.key);
  977. expect(keys).toContain(`team:bigmodel:shared-product:org-same:proj-same`);
  978. expect(keys).toContain(`team:zai:shared-product:org-same:proj-same`);
  979. });
  980. it("zai-only 视图(无 bigmodel codingPlan item)仍生成 zai Team Plan 连接项", () => {
  981. // 修复原因:appendSubscribedTeamPlanItems 原有 `if (!bigmodelCodingPlanItem) return items`
  982. // 前置守卫。当设置页 providerFamilyDomain === "zai" 时,传入的 codingPlanItems 只含 zai family,
  983. // 没有 bigmodelCodingPlan,守卫直接短路,zai teamPlan item 永远不生成,
  984. // 导致 pickFamilyModeNavigationItem 找不到 saved team item → selectedNavItem=null → Plan Card 永远 loading。
  985. const items = buildVisibleFamilyConnectionItems({
  986. // 关键:只有 zai codingPlan item,没有 bigmodel
  987. items: [createZaiCodingPlanItem("notPurchased")],
  988. subscribedTeamProducts: [],
  989. showPurchasedTeamPlanFallback: true,
  990. // 模拟已落盘的 zai team plan selectedKey(和 setting.json 里的格式一致)
  991. teamPlanSelections: {
  992. zai: {
  993. kind: "team-coding-plan",
  994. productId: "zai-pro",
  995. organizationId: "zai-org-1",
  996. projectId: "zai-proj-1",
  997. },
  998. },
  999. });
  1000. const teamItem = items.find((item) => item.type === "teamPlan");
  1001. // 关键:必须生成 zai teamPlan item,不能因为 bigmodelCodingPlanItem 不存在就整体短路
  1002. expect(teamItem).toBeDefined();
  1003. expect(teamItem?.key).toBe(`team:zai:zai-pro:zai-org-1:zai-proj-1`);
  1004. expect(teamItem?.key).not.toContain("bigmodel");
  1005. });
  1006. });
  1007. it("Start 查询未知不能显示未登录,查询失败可重试", () => {
  1008. expect(
  1009. resolveCodingPlanEntitlementState({
  1010. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
  1011. accountEntitled: false,
  1012. accountAvailability: "unknown",
  1013. modelProvidersLoading: false,
  1014. }).status,
  1015. ).toBe("unavailable");
  1016. });