|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { sleep } from '@sim/utils/helpers' |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { mockCheck } = vi.hoisted(() => ({ mockCheck: vi.fn() })) |
| 8 | + |
| 9 | +vi.mock('@/lib/billing/core/billing-attribution', () => ({ |
| 10 | + checkAttributedUsageLimits: mockCheck, |
| 11 | +})) |
| 12 | + |
| 13 | +import type { BillingAttributionSnapshot } from '@/lib/billing/core/billing-attribution' |
| 14 | +import { |
| 15 | + checkIngestionUsageLimits, |
| 16 | + INGESTION_USAGE_GATE_TTL_MS, |
| 17 | + resetIngestionUsageGateCache, |
| 18 | +} from '@/lib/billing/core/ingestion-usage-gate' |
| 19 | + |
| 20 | +const ATTRIBUTION: BillingAttributionSnapshot = { |
| 21 | + actorUserId: 'member-1', |
| 22 | + workspaceId: null, |
| 23 | + organizationId: 'org-1', |
| 24 | + billedAccountUserId: 'owner-1', |
| 25 | + billingEntity: { type: 'organization', id: 'org-1' }, |
| 26 | + billingPeriod: { |
| 27 | + start: '2026-09-01T00:00:00.000Z', |
| 28 | + end: '2026-10-01T00:00:00.000Z', |
| 29 | + source: 'stripe', |
| 30 | + }, |
| 31 | + payerSubscription: null, |
| 32 | +} |
| 33 | + |
| 34 | +describe('checkIngestionUsageLimits', () => { |
| 35 | + beforeEach(() => { |
| 36 | + resetIngestionUsageGateCache() |
| 37 | + mockCheck.mockReset().mockResolvedValue({ isExceeded: false }) |
| 38 | + }) |
| 39 | + afterEach(() => vi.restoreAllMocks()) |
| 40 | + |
| 41 | + it('reads the ledger once per payer, period and actor within the TTL', async () => { |
| 42 | + await checkIngestionUsageLimits(ATTRIBUTION) |
| 43 | + await checkIngestionUsageLimits(ATTRIBUTION) |
| 44 | + await checkIngestionUsageLimits({ ...ATTRIBUTION, workspaceId: 'workspace-9' }) |
| 45 | + expect(mockCheck).toHaveBeenCalledTimes(1) |
| 46 | + }) |
| 47 | + |
| 48 | + it('collapses concurrent misses onto one ledger read', async () => { |
| 49 | + let resolve!: (value: { isExceeded: boolean }) => void |
| 50 | + mockCheck.mockReturnValueOnce(new Promise((r) => (resolve = r))) |
| 51 | + const pending = Promise.all([ |
| 52 | + checkIngestionUsageLimits(ATTRIBUTION), |
| 53 | + checkIngestionUsageLimits(ATTRIBUTION), |
| 54 | + checkIngestionUsageLimits(ATTRIBUTION), |
| 55 | + ]) |
| 56 | + await sleep(0) |
| 57 | + resolve({ isExceeded: false }) |
| 58 | + const results = await pending |
| 59 | + expect(results.every((result) => result.isExceeded === false)).toBe(true) |
| 60 | + expect(mockCheck).toHaveBeenCalledTimes(1) |
| 61 | + }) |
| 62 | + |
| 63 | + it('keeps a refusal for the same bounded window, then reads fresh', async () => { |
| 64 | + mockCheck.mockResolvedValueOnce({ isExceeded: true, scope: 'payer', message: 'over' }) |
| 65 | + expect((await checkIngestionUsageLimits(ATTRIBUTION)).isExceeded).toBe(true) |
| 66 | + expect((await checkIngestionUsageLimits(ATTRIBUTION)).isExceeded).toBe(true) |
| 67 | + expect(mockCheck).toHaveBeenCalledTimes(1) |
| 68 | + |
| 69 | + /** `lru-cache` reads `performance.now()` and debounces it behind a real 1 ms timer. */ |
| 70 | + const start = performance.now() |
| 71 | + vi.spyOn(performance, 'now').mockReturnValue(start + INGESTION_USAGE_GATE_TTL_MS + 1) |
| 72 | + await sleep(5) |
| 73 | + expect((await checkIngestionUsageLimits(ATTRIBUTION)).isExceeded).toBe(false) |
| 74 | + expect(mockCheck).toHaveBeenCalledTimes(2) |
| 75 | + }) |
| 76 | + |
| 77 | + it('separates answers by actor, period and payer', async () => { |
| 78 | + await checkIngestionUsageLimits(ATTRIBUTION) |
| 79 | + await checkIngestionUsageLimits({ ...ATTRIBUTION, actorUserId: 'member-2' }) |
| 80 | + await checkIngestionUsageLimits({ |
| 81 | + ...ATTRIBUTION, |
| 82 | + billingPeriod: { ...ATTRIBUTION.billingPeriod, start: '2026-10-01T00:00:00.000Z' }, |
| 83 | + }) |
| 84 | + await checkIngestionUsageLimits({ |
| 85 | + ...ATTRIBUTION, |
| 86 | + billedAccountUserId: 'owner-2', |
| 87 | + billingEntity: { type: 'user', id: 'owner-2' }, |
| 88 | + }) |
| 89 | + expect(mockCheck).toHaveBeenCalledTimes(4) |
| 90 | + }) |
| 91 | + |
| 92 | + it('does not cache a failed read', async () => { |
| 93 | + mockCheck.mockRejectedValueOnce(new Error('ledger unavailable')) |
| 94 | + await expect(checkIngestionUsageLimits(ATTRIBUTION)).rejects.toThrow('ledger unavailable') |
| 95 | + await checkIngestionUsageLimits(ATTRIBUTION) |
| 96 | + expect(mockCheck).toHaveBeenCalledTimes(2) |
| 97 | + }) |
| 98 | +}) |
0 commit comments