|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + deriveUsageLimitCause, |
| 4 | + seatEraLimitContent, |
| 5 | + usageBasedLimitContent, |
| 6 | +} from "./usageLimitContent"; |
| 7 | + |
| 8 | +describe("deriveUsageLimitCause", () => { |
| 9 | + it.each([ |
| 10 | + ["model_gate", "burst", "model_gate"], |
| 11 | + [null, "burst", "user_daily_limit"], |
| 12 | + [null, "sustained", "user_monthly_limit"], |
| 13 | + // No cause and no bucket (e.g. an upstream provider's own rate limit): |
| 14 | + // stay generic instead of blaming the org's billing. |
| 15 | + [null, null, null], |
| 16 | + ] as const)("cause %s + bucket %s -> %s", (cause, bucket, expected) => { |
| 17 | + expect(deriveUsageLimitCause(cause, bucket)).toBe(expected); |
| 18 | + }); |
| 19 | +}); |
| 20 | + |
| 21 | +describe("usageBasedLimitContent", () => { |
| 22 | + it("names the gated model and offers a payment method", () => { |
| 23 | + const content = usageBasedLimitContent({ |
| 24 | + cause: "model_gate", |
| 25 | + model: "Claude Opus 4.8", |
| 26 | + resetLabel: null, |
| 27 | + billed: false, |
| 28 | + }); |
| 29 | + expect(content.title).toBe("Unlock premium models"); |
| 30 | + expect(content.description).toContain("Claude Opus 4.8 isn't"); |
| 31 | + expect(content.actionLabel).toBe("Add payment method"); |
| 32 | + }); |
| 33 | + |
| 34 | + it("falls back to generic wording when the gated model is unknown", () => { |
| 35 | + const content = usageBasedLimitContent({ |
| 36 | + cause: "model_gate", |
| 37 | + model: null, |
| 38 | + resetLabel: null, |
| 39 | + billed: undefined, |
| 40 | + }); |
| 41 | + expect(content.description).toContain("This model isn't"); |
| 42 | + }); |
| 43 | + |
| 44 | + it.each([ |
| 45 | + // Confirmed-free org: allocation used up, the fix is adding a card. |
| 46 | + [false, "Free usage used up", "Add payment method"], |
| 47 | + // Billed org: the fix is raising the spend limit. |
| 48 | + [true, "Organization usage limit reached", "Manage billing"], |
| 49 | + // Unknown billed state must not read as free. |
| 50 | + [undefined, "Organization usage limit reached", "Manage billing"], |
| 51 | + ] as const)( |
| 52 | + "org_limit with billed=%s -> %s / %s", |
| 53 | + (billed, title, actionLabel) => { |
| 54 | + const content = usageBasedLimitContent({ |
| 55 | + cause: "org_limit", |
| 56 | + model: null, |
| 57 | + resetLabel: null, |
| 58 | + billed, |
| 59 | + }); |
| 60 | + expect(content.title).toBe(title); |
| 61 | + expect(content.actionLabel).toBe(actionLabel); |
| 62 | + }, |
| 63 | + ); |
| 64 | + |
| 65 | + it.each([ |
| 66 | + ["user_daily_limit", "Free daily limit reached"], |
| 67 | + ["user_monthly_limit", "Free monthly limit reached"], |
| 68 | + ] as const)("%s carries the reset hint", (cause, title) => { |
| 69 | + const content = usageBasedLimitContent({ |
| 70 | + cause, |
| 71 | + model: null, |
| 72 | + resetLabel: "Resets in 2h", |
| 73 | + billed: false, |
| 74 | + }); |
| 75 | + expect(content.title).toBe(title); |
| 76 | + expect(content.description).toContain("Resets in 2h"); |
| 77 | + expect(content.actionLabel).toBe("Add payment method"); |
| 78 | + }); |
| 79 | + |
| 80 | + it("renders generic copy without a billing CTA when the cause is unknown", () => { |
| 81 | + const content = usageBasedLimitContent({ |
| 82 | + cause: null, |
| 83 | + model: null, |
| 84 | + resetLabel: null, |
| 85 | + billed: true, |
| 86 | + }); |
| 87 | + expect(content.title).toBe("Usage limit reached"); |
| 88 | + expect(content.actionLabel).toBeNull(); |
| 89 | + expect(content.dismissLabel).toBe("Got it"); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +describe("seatEraLimitContent", () => { |
| 94 | + it("keeps the Pro cap copy with no upgrade action", () => { |
| 95 | + const content = seatEraLimitContent({ |
| 96 | + bucket: "sustained", |
| 97 | + isPro: true, |
| 98 | + resetLabel: "Resets in 3d", |
| 99 | + }); |
| 100 | + expect(content.title).toBe("Monthly limit reached"); |
| 101 | + expect(content.description).toContain("monthly usage cap"); |
| 102 | + expect(content.actionLabel).toBeNull(); |
| 103 | + expect(content.dismissLabel).toBe("Got it"); |
| 104 | + }); |
| 105 | + |
| 106 | + it("keeps the free-plan upgrade pitch", () => { |
| 107 | + const content = seatEraLimitContent({ |
| 108 | + bucket: "burst", |
| 109 | + isPro: false, |
| 110 | + resetLabel: null, |
| 111 | + }); |
| 112 | + expect(content.title).toBe("Daily limit reached"); |
| 113 | + expect(content.description).toContain("Upgrade to Pro for 40×"); |
| 114 | + expect(content.actionLabel).toBe("See Pro"); |
| 115 | + }); |
| 116 | +}); |
0 commit comments