|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockGetUserUsageData, mockGetCreditBalance, mockGetUserUsageLimitInfo } = vi.hoisted( |
| 7 | + () => ({ |
| 8 | + mockGetUserUsageData: vi.fn(), |
| 9 | + mockGetCreditBalance: vi.fn(), |
| 10 | + mockGetUserUsageLimitInfo: vi.fn(), |
| 11 | + }) |
| 12 | +) |
| 13 | + |
| 14 | +vi.mock('@/lib/billing', () => ({ |
| 15 | + getUserUsageData: mockGetUserUsageData, |
| 16 | + getCreditBalance: mockGetCreditBalance, |
| 17 | + getUserUsageLimitInfo: mockGetUserUsageLimitInfo, |
| 18 | +})) |
| 19 | + |
| 20 | +import type { ExecutionContext } from '@/lib/copilot/request/types' |
| 21 | +import { executeGetAccountBilling } from '@/lib/copilot/tools/handlers/account' |
| 22 | + |
| 23 | +const context = { userId: 'user-1' } as ExecutionContext |
| 24 | + |
| 25 | +describe('executeGetAccountBilling', () => { |
| 26 | + beforeEach(() => { |
| 27 | + vi.clearAllMocks() |
| 28 | + }) |
| 29 | + |
| 30 | + it('returns the org-aware plan, usage, and credit snapshot', async () => { |
| 31 | + const periodEnd = new Date('2026-09-01T00:00:00Z') |
| 32 | + mockGetUserUsageData.mockResolvedValue({ |
| 33 | + currentUsage: 18.5, |
| 34 | + limit: 40, |
| 35 | + percentUsed: 46.25, |
| 36 | + isWarning: false, |
| 37 | + isExceeded: false, |
| 38 | + billingPeriodStart: new Date('2026-08-01T00:00:00Z'), |
| 39 | + billingPeriodEnd: periodEnd, |
| 40 | + lastPeriodCost: 31, |
| 41 | + }) |
| 42 | + mockGetCreditBalance.mockResolvedValue({ |
| 43 | + balance: 25, |
| 44 | + entityType: 'organization', |
| 45 | + entityId: 'org-1', |
| 46 | + }) |
| 47 | + mockGetUserUsageLimitInfo.mockResolvedValue({ |
| 48 | + currentLimit: 40, |
| 49 | + canEdit: false, |
| 50 | + minimumLimit: 0, |
| 51 | + plan: 'team', |
| 52 | + updatedAt: null, |
| 53 | + scope: 'organization', |
| 54 | + organizationId: 'org-1', |
| 55 | + }) |
| 56 | + |
| 57 | + const result = await executeGetAccountBilling(context) |
| 58 | + |
| 59 | + expect(mockGetUserUsageData).toHaveBeenCalledWith('user-1') |
| 60 | + expect(mockGetCreditBalance).toHaveBeenCalledWith('user-1') |
| 61 | + expect(mockGetUserUsageLimitInfo).toHaveBeenCalledWith('user-1') |
| 62 | + expect(result).toEqual({ |
| 63 | + success: true, |
| 64 | + output: { |
| 65 | + plan: 'team', |
| 66 | + billingScope: 'organization', |
| 67 | + organizationId: 'org-1', |
| 68 | + usage: { |
| 69 | + currentPeriodCost: 18.5, |
| 70 | + limit: 40, |
| 71 | + remaining: 21.5, |
| 72 | + percentUsed: 46.25, |
| 73 | + isExceeded: false, |
| 74 | + billingPeriodEnd: periodEnd, |
| 75 | + }, |
| 76 | + credits: { balance: 25, scope: 'organization' }, |
| 77 | + }, |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + it('clamps remaining to zero when usage exceeds the limit', async () => { |
| 82 | + mockGetUserUsageData.mockResolvedValue({ |
| 83 | + currentUsage: 45, |
| 84 | + limit: 40, |
| 85 | + percentUsed: 112.5, |
| 86 | + isWarning: false, |
| 87 | + isExceeded: true, |
| 88 | + billingPeriodStart: null, |
| 89 | + billingPeriodEnd: null, |
| 90 | + lastPeriodCost: 0, |
| 91 | + }) |
| 92 | + mockGetCreditBalance.mockResolvedValue({ balance: 0, entityType: 'user', entityId: 'user-1' }) |
| 93 | + mockGetUserUsageLimitInfo.mockResolvedValue({ |
| 94 | + currentLimit: 40, |
| 95 | + canEdit: true, |
| 96 | + minimumLimit: 0, |
| 97 | + plan: 'pro', |
| 98 | + updatedAt: null, |
| 99 | + scope: 'user', |
| 100 | + organizationId: null, |
| 101 | + }) |
| 102 | + |
| 103 | + const result = await executeGetAccountBilling(context) |
| 104 | + |
| 105 | + expect(result.success).toBe(true) |
| 106 | + expect(result.output).toMatchObject({ |
| 107 | + plan: 'pro', |
| 108 | + usage: { remaining: 0, isExceeded: true }, |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + it('surfaces a billing lookup failure as a tool error', async () => { |
| 113 | + mockGetUserUsageData.mockRejectedValue(new Error('stats row missing')) |
| 114 | + mockGetCreditBalance.mockResolvedValue({ balance: 0, entityType: 'user', entityId: 'user-1' }) |
| 115 | + mockGetUserUsageLimitInfo.mockResolvedValue({}) |
| 116 | + |
| 117 | + const result = await executeGetAccountBilling(context) |
| 118 | + |
| 119 | + expect(result).toEqual({ success: false, error: 'stats row missing' }) |
| 120 | + }) |
| 121 | +}) |
0 commit comments