@@ -18,32 +18,25 @@ import type { BlockGrantResult } from '@codebuff/billing/subscription'
1818import type { GetUserPreferencesFn } from '../_post'
1919
2020describe ( '/api/v1/chat/completions POST endpoint' , ( ) => {
21- // Old enough to clear the account-age gate in _post.ts
22- const AGED_ACCOUNT_CREATED_AT = new Date ( '2024-01-01T00:00:00Z' )
23-
2421 const mockUserData : Record <
2522 string ,
26- { id : string ; banned : boolean ; created_at : Date }
23+ { id : string ; banned : boolean }
2724 > = {
2825 'test-api-key-123' : {
2926 id : 'user-123' ,
3027 banned : false ,
31- created_at : AGED_ACCOUNT_CREATED_AT ,
3228 } ,
3329 'test-api-key-no-credits' : {
3430 id : 'user-no-credits' ,
3531 banned : false ,
36- created_at : AGED_ACCOUNT_CREATED_AT ,
3732 } ,
3833 'test-api-key-blocked' : {
3934 id : 'banned-user-id' ,
4035 banned : true ,
41- created_at : AGED_ACCOUNT_CREATED_AT ,
4236 } ,
4337 'test-api-key-new-free' : {
4438 id : 'user-new-free' ,
4539 banned : false ,
46- created_at : new Date ( ) ,
4740 } ,
4841 }
4942
@@ -57,7 +50,6 @@ describe('/api/v1/chat/completions POST endpoint', () => {
5750 return {
5851 id : userData . id ,
5952 banned : userData . banned ,
60- created_at : userData . created_at ,
6153 } as Awaited < ReturnType < GetUserInfoFromApiKeyFn > >
6254 }
6355
@@ -82,15 +74,15 @@ describe('/api/v1/chat/completions POST endpoint', () => {
8274 ) . toISOString ( )
8375
8476 mockLogger = {
85- error : mock ( ( ) => { } ) ,
86- warn : mock ( ( ) => { } ) ,
87- info : mock ( ( ) => { } ) ,
88- debug : mock ( ( ) => { } ) ,
77+ error : mock ( ( ) => { } ) ,
78+ warn : mock ( ( ) => { } ) ,
79+ info : mock ( ( ) => { } ) ,
80+ debug : mock ( ( ) => { } ) ,
8981 }
9082
9183 mockLoggerWithContext = mock ( ( ) => mockLogger )
9284
93- mockTrackEvent = mock ( ( ) => { } )
85+ mockTrackEvent = mock ( ( ) => { } )
9486
9587 mockGetUserUsageData = mock ( async ( { userId } : { userId : string } ) => {
9688 if ( userId === 'user-no-credits' ) {
@@ -101,22 +93,7 @@ describe('/api/v1/chat/completions POST endpoint', () => {
10193 totalDebt : 0 ,
10294 netBalance : 0 ,
10395 breakdown : { } ,
104- // Has purchased credits historically (principals > 0) but 0 remaining
105- // so the paid-plan gate passes and the credit check is what enforces 402.
106- principals : { purchase : 100 } ,
107- } ,
108- nextQuotaReset,
109- }
110- }
111- if ( userId === 'user-new-free' ) {
112- return {
113- usageThisCycle : 0 ,
114- balance : {
115- totalRemaining : 100 ,
116- totalDebt : 0 ,
117- netBalance : 100 ,
118- breakdown : { } as Record < string , number > ,
119- principals : { } as Record < string , number > ,
96+ principals : { } ,
12097 } ,
12198 nextQuotaReset,
12299 }
@@ -128,7 +105,7 @@ describe('/api/v1/chat/completions POST endpoint', () => {
128105 totalDebt : 0 ,
129106 netBalance : 100 ,
130107 breakdown : { } ,
131- principals : { purchase : 100 } ,
108+ principals : { } ,
132109 } ,
133110 nextQuotaReset,
134111 }
@@ -474,7 +451,7 @@ describe('/api/v1/chat/completions POST endpoint', () => {
474451 expect ( body . message ) . not . toContain ( nextQuotaReset )
475452 } )
476453
477- it ( 'returns 403 for a free-tier user with no paid relationship' , async ( ) => {
454+ it ( 'lets a new account with no paid relationship through for non-free mode ' , async ( ) => {
478455 const req = new NextRequest (
479456 'http://localhost:3000/api/v1/chat/completions' ,
480457 {
@@ -504,11 +481,10 @@ describe('/api/v1/chat/completions POST endpoint', () => {
504481 checkSessionAdmissible : mockCheckSessionAdmissibleAllow ,
505482 } )
506483
507- expect ( response . status ) . toBe ( 403 )
508- const body = await response . json ( )
509- expect ( body . error ) . toBe ( 'requires_paid_plan' )
484+ expect ( response . status ) . toBe ( 200 )
510485 } )
511486
487+
512488 it ( 'lets a BYOK free-tier new account through the paid-plan gate' , async ( ) => {
513489 const req = new NextRequest (
514490 'http://localhost:3000/api/v1/chat/completions' ,
0 commit comments