|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | 2 | import type { UsageOutput } from "../usage/schemas"; |
3 | 3 | import { |
| 4 | + codeOrgSpendLimitUsd, |
4 | 5 | codeUsageMeter, |
5 | 6 | formatResetTime, |
| 7 | + formatUsageBreakdown, |
6 | 8 | formatUsdAmount, |
7 | 9 | isCodeUsageFreeTier, |
8 | 10 | isUsageExceeded, |
@@ -92,9 +94,32 @@ describe("codeUsageMeter", () => { |
92 | 94 | percent: 25, |
93 | 95 | exceeded: false, |
94 | 96 | resetAt: "2026-06-01T00:00:00.000Z", |
| 97 | + breakdown: { includedUsd: 20, spendLimitUsd: 30 }, |
95 | 98 | }); |
96 | 99 | }); |
97 | 100 |
|
| 101 | + it("splits a default-settings subscribed limit into $20 included + $50 spend limit", () => { |
| 102 | + const meter = codeUsageMeter({ |
| 103 | + ...makeUsage(), |
| 104 | + code_usage_subscribed: true, |
| 105 | + ai_credits: { exhausted: false, used_usd: 5, limit_usd: 70 }, |
| 106 | + }); |
| 107 | + expect(meter).toMatchObject({ |
| 108 | + kind: "dollars", |
| 109 | + limitUsd: 70, |
| 110 | + breakdown: { includedUsd: 20, spendLimitUsd: 50 }, |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it("keeps a free org's dollars meter breakdown-free — its limit is just the allowance", () => { |
| 115 | + const meter = codeUsageMeter({ |
| 116 | + ...makeUsage(), |
| 117 | + code_usage_subscribed: false, |
| 118 | + ai_credits: { exhausted: false, used_usd: 5, limit_usd: 20 }, |
| 119 | + }); |
| 120 | + expect(meter).toMatchObject({ kind: "dollars", breakdown: null }); |
| 121 | + }); |
| 122 | + |
98 | 123 | it("marks the dollars meter exceeded from the org bucket and falls back to the sustained reset", () => { |
99 | 124 | const meter = codeUsageMeter({ |
100 | 125 | ...makeUsage(), |
@@ -134,6 +159,59 @@ describe("codeUsageMeter", () => { |
134 | 159 | }); |
135 | 160 | }); |
136 | 161 |
|
| 162 | +describe("codeOrgSpendLimitUsd", () => { |
| 163 | + const subscribedWithLimit = (limitUsd: number | null) => ({ |
| 164 | + code_usage_subscribed: true, |
| 165 | + ai_credits: { exhausted: false, used_usd: 0, limit_usd: limitUsd }, |
| 166 | + }); |
| 167 | + |
| 168 | + it.each([ |
| 169 | + ["default settings", 70, 50], |
| 170 | + ["custom limit", 120.5, 100.5], |
| 171 | + ["a $0 spend limit is a real answer", 20, 0], |
| 172 | + ])("recovers the configured limit with %s", (_name, limitUsd, expected) => { |
| 173 | + expect(codeOrgSpendLimitUsd(subscribedWithLimit(limitUsd))).toBe(expected); |
| 174 | + }); |
| 175 | + |
| 176 | + it("returns null when the merged limit is below the allowance", () => { |
| 177 | + expect(codeOrgSpendLimitUsd(subscribedWithLimit(15))).toBeNull(); |
| 178 | + }); |
| 179 | + |
| 180 | + it("returns null without a limit number", () => { |
| 181 | + expect(codeOrgSpendLimitUsd(subscribedWithLimit(null))).toBeNull(); |
| 182 | + expect( |
| 183 | + codeOrgSpendLimitUsd({ |
| 184 | + code_usage_subscribed: true, |
| 185 | + ai_credits: undefined, |
| 186 | + }), |
| 187 | + ).toBeNull(); |
| 188 | + }); |
| 189 | + |
| 190 | + it("returns null for free, unknown, or missing orgs", () => { |
| 191 | + expect( |
| 192 | + codeOrgSpendLimitUsd({ |
| 193 | + code_usage_subscribed: false, |
| 194 | + ai_credits: { exhausted: false, used_usd: 0, limit_usd: 20 }, |
| 195 | + }), |
| 196 | + ).toBeNull(); |
| 197 | + expect( |
| 198 | + codeOrgSpendLimitUsd({ |
| 199 | + code_usage_subscribed: undefined, |
| 200 | + ai_credits: { exhausted: false, used_usd: 0, limit_usd: 70 }, |
| 201 | + }), |
| 202 | + ).toBeNull(); |
| 203 | + expect(codeOrgSpendLimitUsd(null)).toBeNull(); |
| 204 | + }); |
| 205 | +}); |
| 206 | + |
| 207 | +describe("formatUsageBreakdown", () => { |
| 208 | + it("phrases the merged limit as included + spend limit", () => { |
| 209 | + expect(formatUsageBreakdown({ includedUsd: 20, spendLimitUsd: 50 })).toBe( |
| 210 | + "$20 included + $50 org spend limit", |
| 211 | + ); |
| 212 | + }); |
| 213 | +}); |
| 214 | + |
137 | 215 | describe("formatUsdAmount", () => { |
138 | 216 | it.each([ |
139 | 217 | [50, "$50"], |
|
0 commit comments