accountConnectionLossSuggestion.test.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { describe, expect, it, vi } from "vitest";
  2. import { prepareAccountConnectionSwitch } from "@/root/accountConnectionLossSuggestion.js";
  3. function fixture() {
  4. let valid = true;
  5. let settings = {
  6. providerFamilyDomain: "bigmodel",
  7. providerFamilyConnectionSelections: {
  8. bigmodel: {
  9. kind: "team-coding-plan",
  10. organizationId: "old-org",
  11. projectId: "old-project",
  12. productId: "old-product",
  13. },
  14. },
  15. };
  16. const view = {
  17. revision: 1,
  18. providers: [
  19. {
  20. providerId: "account:bigmodel-team-coding-plan",
  21. accountState: {
  22. current: true,
  23. connectionKey: "account/start",
  24. availability: "unavailable",
  25. },
  26. },
  27. {
  28. providerId: "account:bigmodel-individual-coding-plan",
  29. accountState: { current: false, availability: "available" },
  30. },
  31. ],
  32. };
  33. const services = {
  34. settingService: {
  35. get: vi.fn(async () => structuredClone(settings)),
  36. update: vi.fn(async (patch) => {
  37. settings = { ...settings, ...patch };
  38. }),
  39. },
  40. providerSettingsService: { getView: vi.fn(async () => view), refresh: vi.fn(async () => view) },
  41. codingPlanSubscriptionService: {
  42. getEnterprisePricing: vi.fn(async () => ({ productList: [] })),
  43. },
  44. usageStatsService: { getEntitlementSnapshot: vi.fn() },
  45. };
  46. const event = {
  47. providerId: "account:bigmodel-team-coding-plan",
  48. connectionKey: "account/start",
  49. isCurrent: () => valid,
  50. };
  51. return {
  52. services,
  53. event,
  54. view,
  55. invalidate: () => {
  56. valid = false;
  57. },
  58. };
  59. }
  60. describe("套餐切换建议", () => {
  61. it("付费失效时不推荐把全局连接改成 Start", async () => {
  62. const { services, event, view } = fixture();
  63. view.providers[1]!.providerId = "account:bigmodel-start-plan";
  64. expect(await prepareAccountConnectionSwitch(services as never, event)).toBeNull();
  65. expect(services.settingService.update).not.toHaveBeenCalled();
  66. });
  67. it("Team 建议校验具体项目,点击不改成别的 Team", async () => {
  68. const { services, event, view } = fixture();
  69. view.providers[1]!.accountState.availability = "unavailable";
  70. services.codingPlanSubscriptionService.getEnterprisePricing.mockResolvedValue({
  71. productList: [
  72. {
  73. productId: "product",
  74. subscribed: true,
  75. teamProjects: [
  76. { organizationId: "org-a", projectId: "project-a", organizationName: "团队 A" },
  77. { organizationId: "org-b", projectId: "project-b", organizationName: "团队 B" },
  78. ],
  79. },
  80. ],
  81. } as never);
  82. services.usageStatsService.getEntitlementSnapshot.mockImplementation(
  83. async ({ accountAccess }) =>
  84. accountAccess.projectId === "project-b"
  85. ? {
  86. subscription: {
  87. identityType: "unknown",
  88. identityMasked: null,
  89. details: [{ productId: "product", productName: "团队 B" }],
  90. },
  91. }
  92. : { unavailableReason: "no_plan" },
  93. );
  94. const suggestion = await prepareAccountConnectionSwitch(services as never, event);
  95. expect(suggestion?.label).toBe("团队 B");
  96. expect(suggestion?.selection).toEqual({
  97. kind: "team-coding-plan",
  98. productId: "product",
  99. organizationId: "org-b",
  100. projectId: "project-b",
  101. });
  102. services.usageStatsService.getEntitlementSnapshot.mockResolvedValue({
  103. unavailableReason: "no_plan",
  104. });
  105. expect(await suggestion?.apply()).toBe("stale");
  106. expect(services.settingService.update).not.toHaveBeenCalled();
  107. });
  108. it("相同身份重选产生新版操作后,旧失效建议不能提交", async () => {
  109. const { services, event } = fixture();
  110. const suggestion = await prepareAccountConnectionSwitch(services as never, event);
  111. services.settingService.update.mockImplementationOnce(async (_patch, expected) => {
  112. if (expected?.providerFamilyDomainUpdatedAt !== 1)
  113. throw new Error("Account connection settings changed");
  114. });
  115. await expect(suggestion?.apply()).rejects.toThrow("Account connection settings changed");
  116. expect(services.settingService.update).toHaveBeenCalledWith(
  117. expect.anything(),
  118. expect.objectContaining({ providerFamilyDomainUpdatedAt: 0 }),
  119. );
  120. });
  121. it("写入成功而刷新失败仍返回已保存", async () => {
  122. const { services, event, view } = fixture();
  123. const suggestion = await prepareAccountConnectionSwitch(services as never, event);
  124. services.providerSettingsService.refresh
  125. .mockResolvedValueOnce(view)
  126. .mockRejectedValueOnce(new Error("offline"));
  127. expect(await suggestion?.apply()).toBe("switched");
  128. expect(services.settingService.update).toHaveBeenCalledOnce();
  129. });
  130. it("点击等待刷新期间切换意图或重复点击,不提交旧建议", async () => {
  131. const { services, event, view, invalidate } = fixture();
  132. const suggestion = await prepareAccountConnectionSwitch(services as never, event);
  133. let finish!: (value: typeof view) => void;
  134. services.providerSettingsService.refresh.mockImplementationOnce(
  135. () =>
  136. new Promise((resolve) => {
  137. finish = resolve;
  138. }),
  139. );
  140. const pending = suggestion!.apply();
  141. expect(await suggestion!.apply()).toBe("stale");
  142. invalidate();
  143. finish(view);
  144. expect(await pending).toBe("stale");
  145. expect(services.settingService.update).not.toHaveBeenCalled();
  146. });
  147. it("计算不写入,只有点击才条件保存,重复点击不重复写", async () => {
  148. const { services, event } = fixture();
  149. const suggestion = await prepareAccountConnectionSwitch(services as never, event);
  150. expect(suggestion?.selection).toEqual({ kind: "individual-coding-plan" });
  151. expect(services.settingService.update).not.toHaveBeenCalled();
  152. expect(await suggestion?.apply()).toBe("switched");
  153. expect(services.settingService.update).toHaveBeenCalledWith(
  154. expect.objectContaining({
  155. providerFamilyConnectionSelections: { bigmodel: { kind: "individual-coding-plan" } },
  156. }),
  157. expect.objectContaining({
  158. providerFamilyConnectionSelections: {
  159. bigmodel: {
  160. kind: "team-coding-plan",
  161. organizationId: "old-org",
  162. projectId: "old-project",
  163. productId: "old-product",
  164. },
  165. },
  166. }),
  167. );
  168. await suggestion?.apply();
  169. expect(services.settingService.update).toHaveBeenCalledTimes(1);
  170. });
  171. it.each(["intent", "recovered", "target"])("%s 改变后旧按钮不再提交", async (kind) => {
  172. const { services, event, view, invalidate } = fixture();
  173. const suggestion = await prepareAccountConnectionSwitch(services as never, event);
  174. if (kind === "intent") invalidate();
  175. if (kind === "recovered") view.providers[0]!.accountState.availability = "available";
  176. if (kind === "target") view.providers[1]!.accountState.availability = "unavailable";
  177. expect(await suggestion?.apply()).toBe("stale");
  178. expect(services.settingService.update).not.toHaveBeenCalled();
  179. });
  180. it("没有可用目标不把购买入口当替代", async () => {
  181. const { services, event, view } = fixture();
  182. view.providers[1]!.accountState.availability = "unavailable";
  183. expect(await prepareAccountConnectionSwitch(services as never, event)).toBeNull();
  184. expect(services.settingService.update).not.toHaveBeenCalled();
  185. });
  186. it("保存失败抛出操作错误而不是宣称成功", async () => {
  187. const { services, event } = fixture();
  188. services.settingService.update.mockRejectedValueOnce(new Error("disk"));
  189. const suggestion = await prepareAccountConnectionSwitch(services as never, event);
  190. await expect(suggestion?.apply()).rejects.toThrow("disk");
  191. });
  192. });