|
| 1 | +const mockIsFreeModel = jest.fn(); |
| 2 | +const mockGetModelUserByokProviders = jest.fn(); |
| 3 | +const mockGetUserByokProviderIds = jest.fn(); |
| 4 | +const mockGetOrganizationByokProviderIds = jest.fn(); |
| 5 | + |
| 6 | +jest.mock('@/lib/ai-gateway/is-free-model', () => ({ |
| 7 | + isFreeModel: (...args: unknown[]) => mockIsFreeModel(...args), |
| 8 | +})); |
| 9 | + |
| 10 | +jest.mock('@/lib/ai-gateway/byok', () => ({ |
| 11 | + getModelUserByokProviders: (...args: unknown[]) => mockGetModelUserByokProviders(...args), |
| 12 | + getUserByokProviderIds: (...args: unknown[]) => mockGetUserByokProviderIds(...args), |
| 13 | + getOrganizationByokProviderIds: (...args: unknown[]) => |
| 14 | + mockGetOrganizationByokProviderIds(...args), |
| 15 | +})); |
| 16 | + |
| 17 | +import { computeCloudAgentNextBalanceCheckEligibility } from './balance-check-eligibility'; |
| 18 | + |
| 19 | +const KILO_EXCLUSIVE_MODEL = 'deepseek/deepseek-v4-pro:discounted'; |
| 20 | +const NON_EXCLUSIVE_MODEL = 'anthropic/claude-sonnet-4'; |
| 21 | + |
| 22 | +const fakeDb = {} as never; |
| 23 | +const fakeUser = { id: 'user-1' }; |
| 24 | + |
| 25 | +beforeEach(() => { |
| 26 | + jest.resetAllMocks(); |
| 27 | + mockIsFreeModel.mockResolvedValue(false); |
| 28 | + mockGetModelUserByokProviders.mockResolvedValue([]); |
| 29 | + mockGetUserByokProviderIds.mockResolvedValue([]); |
| 30 | + mockGetOrganizationByokProviderIds.mockResolvedValue([]); |
| 31 | +}); |
| 32 | + |
| 33 | +describe('computeCloudAgentNextBalanceCheckEligibility', () => { |
| 34 | + it('returns isFree and skips BYOK when the model is free', async () => { |
| 35 | + mockIsFreeModel.mockResolvedValueOnce(true); |
| 36 | + |
| 37 | + const result = await computeCloudAgentNextBalanceCheckEligibility({ |
| 38 | + fromDb: fakeDb, |
| 39 | + user: fakeUser, |
| 40 | + modelId: 'kilo/free-model', |
| 41 | + }); |
| 42 | + |
| 43 | + expect(result).toEqual({ isFree: true, hasUserByokAvailable: false }); |
| 44 | + expect(mockGetModelUserByokProviders).not.toHaveBeenCalled(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('returns hasUserByokAvailable: false for a Kilo-exclusive model even when BYOK providers can serve it', async () => { |
| 48 | + const result = await computeCloudAgentNextBalanceCheckEligibility({ |
| 49 | + fromDb: fakeDb, |
| 50 | + user: fakeUser, |
| 51 | + modelId: KILO_EXCLUSIVE_MODEL, |
| 52 | + }); |
| 53 | + |
| 54 | + expect(result).toEqual({ isFree: false, hasUserByokAvailable: false }); |
| 55 | + expect(mockGetModelUserByokProviders).not.toHaveBeenCalled(); |
| 56 | + expect(mockGetUserByokProviderIds).not.toHaveBeenCalled(); |
| 57 | + expect(mockGetOrganizationByokProviderIds).not.toHaveBeenCalled(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('returns hasUserByokAvailable: false for a Kilo-exclusive model even when the user has an enabled matching BYOK provider', async () => { |
| 61 | + mockGetUserByokProviderIds.mockResolvedValueOnce(['openrouter']); |
| 62 | + |
| 63 | + const result = await computeCloudAgentNextBalanceCheckEligibility({ |
| 64 | + fromDb: fakeDb, |
| 65 | + user: fakeUser, |
| 66 | + modelId: KILO_EXCLUSIVE_MODEL, |
| 67 | + }); |
| 68 | + |
| 69 | + expect(result).toEqual({ isFree: false, hasUserByokAvailable: false }); |
| 70 | + expect(mockGetModelUserByokProviders).not.toHaveBeenCalled(); |
| 71 | + expect(mockGetUserByokProviderIds).not.toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('returns hasUserByokAvailable: false for a Kilo-exclusive model in an organization context', async () => { |
| 75 | + mockGetOrganizationByokProviderIds.mockResolvedValueOnce(['openrouter']); |
| 76 | + |
| 77 | + const result = await computeCloudAgentNextBalanceCheckEligibility({ |
| 78 | + fromDb: fakeDb, |
| 79 | + user: fakeUser, |
| 80 | + modelId: KILO_EXCLUSIVE_MODEL, |
| 81 | + organizationId: 'org-1', |
| 82 | + }); |
| 83 | + |
| 84 | + expect(result).toEqual({ isFree: false, hasUserByokAvailable: false }); |
| 85 | + expect(mockGetModelUserByokProviders).not.toHaveBeenCalled(); |
| 86 | + expect(mockGetOrganizationByokProviderIds).not.toHaveBeenCalled(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('returns hasUserByokAvailable: true for a non-Kilo-exclusive paid model with a matching enabled user BYOK provider', async () => { |
| 90 | + mockGetModelUserByokProviders.mockResolvedValueOnce(['openrouter']); |
| 91 | + mockGetUserByokProviderIds.mockResolvedValueOnce(['openrouter']); |
| 92 | + |
| 93 | + const result = await computeCloudAgentNextBalanceCheckEligibility({ |
| 94 | + fromDb: fakeDb, |
| 95 | + user: fakeUser, |
| 96 | + modelId: NON_EXCLUSIVE_MODEL, |
| 97 | + }); |
| 98 | + |
| 99 | + expect(result).toEqual({ isFree: false, hasUserByokAvailable: true }); |
| 100 | + }); |
| 101 | + |
| 102 | + it('returns hasUserByokAvailable: false for a non-Kilo-exclusive paid model with no matching BYOK provider', async () => { |
| 103 | + mockGetModelUserByokProviders.mockResolvedValueOnce(['openrouter']); |
| 104 | + mockGetUserByokProviderIds.mockResolvedValueOnce(['anthropic']); |
| 105 | + |
| 106 | + const result = await computeCloudAgentNextBalanceCheckEligibility({ |
| 107 | + fromDb: fakeDb, |
| 108 | + user: fakeUser, |
| 109 | + modelId: NON_EXCLUSIVE_MODEL, |
| 110 | + }); |
| 111 | + |
| 112 | + expect(result).toEqual({ isFree: false, hasUserByokAvailable: false }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('returns hasUserByokAvailable: false for a non-Kilo-exclusive paid model with no resolvable providers', async () => { |
| 116 | + mockGetModelUserByokProviders.mockResolvedValueOnce([]); |
| 117 | + |
| 118 | + const result = await computeCloudAgentNextBalanceCheckEligibility({ |
| 119 | + fromDb: fakeDb, |
| 120 | + user: fakeUser, |
| 121 | + modelId: NON_EXCLUSIVE_MODEL, |
| 122 | + }); |
| 123 | + |
| 124 | + expect(result).toEqual({ isFree: false, hasUserByokAvailable: false }); |
| 125 | + expect(mockGetUserByokProviderIds).not.toHaveBeenCalled(); |
| 126 | + }); |
| 127 | + |
| 128 | + it('uses organization BYOK providers for a non-Kilo-exclusive paid model when organizationId is provided', async () => { |
| 129 | + mockGetModelUserByokProviders.mockResolvedValueOnce(['openrouter']); |
| 130 | + mockGetOrganizationByokProviderIds.mockResolvedValueOnce(['openrouter']); |
| 131 | + |
| 132 | + const result = await computeCloudAgentNextBalanceCheckEligibility({ |
| 133 | + fromDb: fakeDb, |
| 134 | + user: fakeUser, |
| 135 | + modelId: NON_EXCLUSIVE_MODEL, |
| 136 | + organizationId: 'org-1', |
| 137 | + }); |
| 138 | + |
| 139 | + expect(result).toEqual({ isFree: false, hasUserByokAvailable: true }); |
| 140 | + expect(mockGetOrganizationByokProviderIds).toHaveBeenCalledWith(fakeDb, 'org-1'); |
| 141 | + expect(mockGetUserByokProviderIds).not.toHaveBeenCalled(); |
| 142 | + }); |
| 143 | +}); |
0 commit comments