modelProviderFamilyConnectionSelection.test.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. BUILTIN_MODEL_PROVIDER_IDS,
  4. type ProviderFamilyConnectionSelection,
  5. type EnterpriseCodingPlanPricingProduct,
  6. type UsageEntitlementSnapshot,
  7. } from "@zcode/shared";
  8. import {
  9. resolveAutomaticModelProviderFamilyConnectionSelection,
  10. resolveModelProviderFamilyConnectionProviderId,
  11. } from "@/lib/modelProviderFamilyConnectionSelection.js";
  12. function createEntitlementSnapshot(providerId: string): UsageEntitlementSnapshot {
  13. return {
  14. generatedAt: 1,
  15. authenticated: true,
  16. provider: {
  17. id: providerId,
  18. name: providerId,
  19. },
  20. remaining: null,
  21. subscription: {
  22. identityType: "unknown",
  23. identityMasked: null,
  24. details: [
  25. {
  26. productId: "pro",
  27. productName: "GLM Coding Pro",
  28. purchaseTime: null,
  29. beginTime: null,
  30. expireTime: null,
  31. },
  32. ],
  33. },
  34. quota: null,
  35. };
  36. }
  37. describe("resolveAutomaticModelProviderFamilyConnectionSelection", () => {
  38. it.each([
  39. ["start-plan", BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan],
  40. ["individual-coding-plan", BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan],
  41. ["team-coding-plan", BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan],
  42. ] as const)("把 BigModel %s 连接映射到对应 Account Provider", (kind, providerId) => {
  43. const selection: ProviderFamilyConnectionSelection =
  44. kind === "team-coding-plan"
  45. ? {
  46. kind,
  47. productId: "team-pro",
  48. organizationId: "org-1",
  49. projectId: "project-1",
  50. }
  51. : { kind };
  52. expect(
  53. resolveModelProviderFamilyConnectionProviderId({
  54. providerFamilyDomain: "bigmodel",
  55. selection,
  56. }),
  57. ).toBe(providerId);
  58. });
  59. function resolveBigModelSelection(params: {
  60. hasStart: boolean;
  61. hasPersonal: boolean;
  62. teamProducts?: EnterpriseCodingPlanPricingProduct[];
  63. }) {
  64. return resolveAutomaticModelProviderFamilyConnectionSelection({
  65. providerFamilyDomain: "bigmodel",
  66. codingPlanEntitlement: params.hasPersonal
  67. ? createEntitlementSnapshot(BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan)
  68. : null,
  69. startPlanEntitlement: params.hasStart
  70. ? createEntitlementSnapshot(BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan)
  71. : null,
  72. teamProducts: params.teamProducts ?? [],
  73. });
  74. }
  75. const subscribedTeamProduct = {
  76. productId: "team-pro",
  77. tier: "PRO",
  78. subscribeMode: "CONTINUOUS",
  79. subscribePeriod: "MONTHLY",
  80. subscribed: true,
  81. teamProjects: [
  82. {
  83. organizationId: "org-1",
  84. projectId: "project-1",
  85. },
  86. ],
  87. } satisfies EnterpriseCodingPlanPricingProduct;
  88. it.each([
  89. {
  90. name: "有 Start、有个人、有团队时选个人 Coding",
  91. hasStart: true,
  92. hasPersonal: true,
  93. teamProducts: [subscribedTeamProduct],
  94. expectedSelection: { kind: "individual-coding-plan" },
  95. },
  96. {
  97. name: "有 Start、有个人、无团队时选个人 Coding",
  98. hasStart: true,
  99. hasPersonal: true,
  100. teamProducts: [],
  101. expectedSelection: { kind: "individual-coding-plan" },
  102. },
  103. {
  104. name: "有 Start、无个人、有团队时选团队 Coding",
  105. hasStart: true,
  106. hasPersonal: false,
  107. teamProducts: [subscribedTeamProduct],
  108. expectedSelection: {
  109. kind: "team-coding-plan",
  110. productId: "team-pro",
  111. organizationId: "org-1",
  112. projectId: "project-1",
  113. },
  114. },
  115. {
  116. name: "只有 Start 时保留独立 Start,付费连接落到个人购买入口",
  117. hasStart: true,
  118. hasPersonal: false,
  119. teamProducts: [],
  120. expectedSelection: { kind: "individual-coding-plan" },
  121. },
  122. {
  123. name: "无 Start、有个人、有团队时选个人 Coding",
  124. hasStart: false,
  125. hasPersonal: true,
  126. teamProducts: [subscribedTeamProduct],
  127. expectedSelection: { kind: "individual-coding-plan" },
  128. },
  129. {
  130. name: "无 Start、有个人、无团队时选个人 Coding",
  131. hasStart: false,
  132. hasPersonal: true,
  133. teamProducts: [],
  134. expectedSelection: { kind: "individual-coding-plan" },
  135. },
  136. {
  137. name: "无 Start、无个人、有团队时选团队 Coding",
  138. hasStart: false,
  139. hasPersonal: false,
  140. teamProducts: [subscribedTeamProduct],
  141. expectedSelection: {
  142. kind: "team-coding-plan",
  143. productId: "team-pro",
  144. organizationId: "org-1",
  145. projectId: "project-1",
  146. },
  147. },
  148. {
  149. name: "无 Start、无个人、无团队时兜底个人 Coding",
  150. hasStart: false,
  151. hasPersonal: false,
  152. teamProducts: [],
  153. expectedSelection: { kind: "individual-coding-plan" },
  154. },
  155. ] satisfies Array<{
  156. name: string;
  157. hasStart: boolean;
  158. hasPersonal: boolean;
  159. teamProducts: EnterpriseCodingPlanPricingProduct[];
  160. expectedSelection: ProviderFamilyConnectionSelection;
  161. }>)("$name", ({ hasStart, hasPersonal, teamProducts, expectedSelection }) => {
  162. expect(resolveBigModelSelection({ hasStart, hasPersonal, teamProducts })).toEqual(
  163. expectedSelection,
  164. );
  165. });
  166. it("多个团队时选择后端返回顺序里的第一个已订阅团队项目", () => {
  167. const selection = resolveAutomaticModelProviderFamilyConnectionSelection({
  168. providerFamilyDomain: "bigmodel",
  169. codingPlanEntitlement: null,
  170. startPlanEntitlement: null,
  171. teamProducts: [
  172. {
  173. productId: "team-a",
  174. tier: "PRO",
  175. subscribeMode: "CONTINUOUS",
  176. subscribePeriod: "MONTHLY",
  177. subscribed: true,
  178. teamProjects: [
  179. {
  180. organizationId: "org-a",
  181. projectId: "project-a",
  182. },
  183. {
  184. organizationId: "org-a",
  185. projectId: "project-a-2",
  186. },
  187. ],
  188. } satisfies EnterpriseCodingPlanPricingProduct,
  189. {
  190. productId: "team-b",
  191. tier: "PRO",
  192. subscribeMode: "CONTINUOUS",
  193. subscribePeriod: "MONTHLY",
  194. subscribed: true,
  195. teamProjects: [
  196. {
  197. organizationId: "org-b",
  198. projectId: "project-b",
  199. },
  200. ],
  201. } satisfies EnterpriseCodingPlanPricingProduct,
  202. ],
  203. });
  204. expect(selection).toEqual({
  205. kind: "team-coding-plan",
  206. productId: "team-a",
  207. organizationId: "org-a",
  208. projectId: "project-a",
  209. });
  210. });
  211. it("自动选择 Team Plan 时跳过 key 不可用的团队项目", () => {
  212. const selection = resolveAutomaticModelProviderFamilyConnectionSelection({
  213. providerFamilyDomain: "bigmodel",
  214. codingPlanEntitlement: null,
  215. startPlanEntitlement: null,
  216. teamProducts: [
  217. {
  218. productId: "team-a",
  219. tier: "PRO",
  220. subscribeMode: "CONTINUOUS",
  221. subscribePeriod: "MONTHLY",
  222. subscribed: true,
  223. teamProjects: [
  224. {
  225. organizationId: "org-a",
  226. projectId: "project-a",
  227. apiKeyStatus: "unavailable",
  228. },
  229. {
  230. organizationId: "org-a",
  231. projectId: "project-a-2",
  232. apiKeyStatus: "available",
  233. },
  234. ],
  235. } satisfies EnterpriseCodingPlanPricingProduct,
  236. ],
  237. });
  238. expect(selection).toEqual({
  239. kind: "team-coding-plan",
  240. productId: "team-a",
  241. organizationId: "org-a",
  242. projectId: "project-a-2",
  243. });
  244. });
  245. it("团队项目未返回时不选 Team Plan,避免缺少团队身份边界", () => {
  246. const selection = resolveAutomaticModelProviderFamilyConnectionSelection({
  247. providerFamilyDomain: "bigmodel",
  248. codingPlanEntitlement: null,
  249. startPlanEntitlement: null,
  250. teamProducts: [
  251. {
  252. productId: "team-pro",
  253. tier: "PRO",
  254. subscribeMode: "CONTINUOUS",
  255. subscribePeriod: "MONTHLY",
  256. subscribed: true,
  257. teamProjects: [],
  258. } satisfies EnterpriseCodingPlanPricingProduct,
  259. ],
  260. });
  261. expect(selection).toEqual({
  262. kind: "individual-coding-plan",
  263. });
  264. });
  265. });