accountConnectionLossSuggestion.test.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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, view } = fixture();
  110. const suggestion = await prepareAccountConnectionSwitch(services as never, event);
  111. services.providerSettingsService.refresh
  112. .mockResolvedValueOnce(view)
  113. .mockRejectedValueOnce(new Error("offline"));
  114. expect(await suggestion?.apply()).toBe("switched");
  115. expect(services.settingService.update).toHaveBeenCalledOnce();
  116. });
  117. it("点击等待刷新期间切换意图或重复点击,不提交旧建议", async () => {
  118. const { services, event, view, invalidate } = fixture();
  119. const suggestion = await prepareAccountConnectionSwitch(services as never, event);
  120. let finish!: (value: typeof view) => void;
  121. services.providerSettingsService.refresh.mockImplementationOnce(
  122. () =>
  123. new Promise((resolve) => {
  124. finish = resolve;
  125. }),
  126. );
  127. const pending = suggestion!.apply();
  128. expect(await suggestion!.apply()).toBe("stale");
  129. invalidate();
  130. finish(view);
  131. expect(await pending).toBe("stale");
  132. expect(services.settingService.update).not.toHaveBeenCalled();
  133. });
  134. it("计算不写入,只有点击才条件保存,重复点击不重复写", async () => {
  135. const { services, event } = fixture();
  136. const suggestion = await prepareAccountConnectionSwitch(services as never, event);
  137. expect(suggestion?.selection).toEqual({ kind: "individual-coding-plan" });
  138. expect(services.settingService.update).not.toHaveBeenCalled();
  139. expect(await suggestion?.apply()).toBe("switched");
  140. expect(services.settingService.update).toHaveBeenCalledWith(
  141. expect.objectContaining({
  142. providerFamilyConnectionSelections: { bigmodel: { kind: "individual-coding-plan" } },
  143. }),
  144. expect.objectContaining({
  145. providerFamilyConnectionSelections: {
  146. bigmodel: {
  147. kind: "team-coding-plan",
  148. organizationId: "old-org",
  149. projectId: "old-project",
  150. productId: "old-product",
  151. },
  152. },
  153. }),
  154. );
  155. await suggestion?.apply();
  156. expect(services.settingService.update).toHaveBeenCalledTimes(1);
  157. });
  158. it.each(["intent", "recovered", "target"])("%s 改变后旧按钮不再提交", async (kind) => {
  159. const { services, event, view, invalidate } = fixture();
  160. const suggestion = await prepareAccountConnectionSwitch(services as never, event);
  161. if (kind === "intent") invalidate();
  162. if (kind === "recovered") view.providers[0]!.accountState.availability = "available";
  163. if (kind === "target") view.providers[1]!.accountState.availability = "unavailable";
  164. expect(await suggestion?.apply()).toBe("stale");
  165. expect(services.settingService.update).not.toHaveBeenCalled();
  166. });
  167. it("没有可用目标不把购买入口当替代", async () => {
  168. const { services, event, view } = fixture();
  169. view.providers[1]!.accountState.availability = "unavailable";
  170. expect(await prepareAccountConnectionSwitch(services as never, event)).toBeNull();
  171. expect(services.settingService.update).not.toHaveBeenCalled();
  172. });
  173. it("保存失败抛出操作错误而不是宣称成功", async () => {
  174. const { services, event } = fixture();
  175. services.settingService.update.mockRejectedValueOnce(new Error("disk"));
  176. const suggestion = await prepareAccountConnectionSwitch(services as never, event);
  177. await expect(suggestion?.apply()).rejects.toThrow("disk");
  178. });
  179. });