usageEntitlementAuthority.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { describe, expect, it } from "vitest";
  2. import { BUILTIN_MODEL_PROVIDER_IDS, type UsageEntitlementSnapshot } from "@zcode/shared";
  3. import { resolveUsageEntitlementOutcome } from "@/lib/codingPlanProvider.js";
  4. import { hasActiveCodingPlanSnapshot } from "@/CodingPlanUsageRemainingPanel.js";
  5. import { resolveSidebarCodingPlanUpgradeProviderId } from "@/lib/sidebarCodingPlanUpgrade.js";
  6. import { getUsageEntitlementScheduleState } from "@/lib/usageEntitlementRefreshPolicy.js";
  7. import { buildUsageEntitlementCacheKey } from "@/lib/usageEntitlementCache.js";
  8. const snapshot: UsageEntitlementSnapshot = {
  9. generatedAt: 1,
  10. authenticated: true,
  11. provider: null,
  12. subscription: null,
  13. remaining: { count: 100, isShow: true, nextResetTime: null },
  14. quota: { level: "max", limits: [] },
  15. };
  16. describe("所有入口使用订阅权益", () => {
  17. it("只有额度不能证明有权益", () => {
  18. expect(resolveUsageEntitlementOutcome(snapshot)).toBe("unknown");
  19. expect(getUsageEntitlementScheduleState(snapshot)).toBe("unknown");
  20. expect(
  21. hasActiveCodingPlanSnapshot({ ...snapshot, provider: { id: "p", name: "p" } }, "p"),
  22. ).toBe(false);
  23. });
  24. it("侧栏不能把 quota 当作可升级的已购套餐", () => {
  25. const providerId = BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan;
  26. expect(
  27. resolveSidebarCodingPlanUpgradeProviderId({
  28. modelProvidersLoading: false,
  29. availableProviders: [
  30. {
  31. providerId,
  32. label: "BigModel",
  33. accountAccess: {
  34. type: "zhipu-account",
  35. family: "bigmodel",
  36. planKind: "individual-coding-plan",
  37. },
  38. },
  39. ],
  40. entitlements: [
  41. {
  42. providerId,
  43. loading: false,
  44. snapshot: { ...snapshot, provider: { id: providerId, name: "BigModel" } },
  45. },
  46. ],
  47. }),
  48. ).toBeUndefined();
  49. });
  50. it("明确无订阅不能被 quota 覆盖", () => {
  51. expect(resolveUsageEntitlementOutcome({ ...snapshot, unavailableReason: "no_plan" })).toBe(
  52. "inactive",
  53. );
  54. });
  55. it("订阅存在时不要求 quota 或 remaining", () => {
  56. expect(
  57. resolveUsageEntitlementOutcome({
  58. ...snapshot,
  59. quota: null,
  60. remaining: null,
  61. subscription: {
  62. identityType: "unknown",
  63. identityMasked: null,
  64. details: [
  65. {
  66. productId: "team",
  67. productName: "团队",
  68. purchaseTime: null,
  69. beginTime: null,
  70. renewTime: null,
  71. expireTime: null,
  72. },
  73. ],
  74. },
  75. }),
  76. ).toBe("active");
  77. });
  78. it("升级后缓存 key 不再命中旧版 quota 合成订阅", () => {
  79. expect(
  80. buildUsageEntitlementCacheKey({
  81. providerId: "provider",
  82. providerFingerprint: "account/project",
  83. }),
  84. ).toBe("zcode:usage-entitlement:subscription-v2:provider:account/project");
  85. });
  86. });