codingPlanOwnedEntryPlans.test.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { describe, expect, it } from "vitest";
  2. import { BUILTIN_MODEL_PROVIDER_IDS, type UsageEntitlementSnapshot } from "@zcode/shared";
  3. import { buildOwnedEntryPlanList } from "@/lib/codingPlanOwnedEntryPlans.js";
  4. function snapshot(
  5. id: string,
  6. productId: string,
  7. productName: string,
  8. expireTime: string | null = null,
  9. ): UsageEntitlementSnapshot {
  10. return {
  11. generatedAt: 1,
  12. authenticated: true,
  13. provider: { id, name: id },
  14. remaining: null,
  15. quota: null,
  16. subscription: {
  17. identityType: "unknown",
  18. identityMasked: null,
  19. details: [{ productId, productName, purchaseTime: null, beginTime: null, expireTime }],
  20. },
  21. };
  22. }
  23. describe("all owned entry plans", () => {
  24. it("combines Start, personal and subscribed team plans across families without relying on the selected card", () => {
  25. const snapshots = [
  26. snapshot(BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan, "start-v3", "Start Plan"),
  27. snapshot(
  28. BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  29. "lite-month",
  30. "GLM Coding Lite",
  31. ),
  32. snapshot(BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan, "lite-month", "GLM Coding Lite"),
  33. ];
  34. const teamProducts = [
  35. { subscribed: true, tier: "standard", productId: "enterprise-x" },
  36. { subscribed: false, tier: "advanced", productId: "enterprise-y" },
  37. ];
  38. const expected = "coding_plan__personal_lite,coding_plan__team_standard,start_plan__start_v3";
  39. expect(buildOwnedEntryPlanList({ snapshots, teamProducts })).toBe(expected);
  40. expect(buildOwnedEntryPlanList({ snapshots: [...snapshots].reverse(), teamProducts })).toBe(
  41. expected,
  42. );
  43. });
  44. it("excludes expired, unauthenticated, no-plan and non-plan sources", () => {
  45. const current = snapshot(BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan, "pro", "Pro");
  46. expect(
  47. buildOwnedEntryPlanList({
  48. snapshots: [
  49. snapshot(BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan, "expired", "Start", "2000-01-01"),
  50. { ...current, authenticated: false },
  51. { ...current, unavailableReason: "no_plan" },
  52. snapshot("custom-provider", "pro", "Pro"),
  53. null,
  54. ],
  55. teamProducts: [],
  56. }),
  57. ).toBe("");
  58. });
  59. });