@@ -81,7 +81,9 @@ describe('/api/v1/docs-search POST endpoint', () => {
8181 headers : { 'Content-Type' : 'text/plain' } ,
8282 } )
8383 }
84- mockFetch = Object . assign ( fetchImpl , { preconnect : ( ) => { } } ) as typeof fetch
84+ mockFetch = Object . assign ( fetchImpl , {
85+ preconnect : ( ) => { } ,
86+ } ) as typeof fetch
8587 } )
8688
8789 afterEach ( ( ) => {
@@ -106,7 +108,7 @@ describe('/api/v1/docs-search POST endpoint', () => {
106108 expect ( res . status ) . toBe ( 401 )
107109 } )
108110
109- test ( '402 when insufficient credits' , async ( ) => {
111+ test ( '200 when zero-credit docs search user has no credits' , async ( ) => {
110112 mockGetUserUsageData = mock ( async ( ) => ( {
111113 usageThisCycle : 0 ,
112114 balance : {
@@ -133,7 +135,11 @@ describe('/api/v1/docs-search POST endpoint', () => {
133135 consumeCreditsWithFallback : mockConsumeCreditsWithFallback ,
134136 fetch : mockFetch ,
135137 } )
136- expect ( res . status ) . toBe ( 402 )
138+ expect ( res . status ) . toBe ( 200 )
139+ const body = await res . json ( )
140+ expect ( body . creditsUsed ) . toBe ( 0 )
141+ expect ( mockGetUserUsageData ) . not . toHaveBeenCalled ( )
142+ expect ( mockConsumeCreditsWithFallback ) . not . toHaveBeenCalled ( )
137143 } )
138144
139145 test ( '200 on success' , async ( ) => {
@@ -155,26 +161,37 @@ describe('/api/v1/docs-search POST endpoint', () => {
155161 expect ( res . status ) . toBe ( 200 )
156162 const body = await res . json ( )
157163 expect ( body . documentation ) . toContain ( 'Some documentation text' )
164+ expect ( body . creditsUsed ) . toBe ( 0 )
165+ expect ( mockConsumeCreditsWithFallback ) . not . toHaveBeenCalled ( )
158166 } )
159167
160168 test ( '200 for subscriber with 0 a-la-carte credits but active block grant' , async ( ) => {
161- mockGetUserUsageData = mock ( async ( { includeSubscriptionCredits } : { includeSubscriptionCredits ?: boolean } ) => ( {
162- usageThisCycle : 0 ,
163- balance : {
164- totalRemaining : includeSubscriptionCredits ? 350 : 0 ,
165- totalDebt : 0 ,
166- netBalance : includeSubscriptionCredits ? 350 : 0 ,
167- breakdown : { } ,
168- principals : { } ,
169- } ,
170- nextQuotaReset : 'soon' ,
171- } ) )
169+ mockGetUserUsageData = mock (
170+ async ( {
171+ includeSubscriptionCredits,
172+ } : {
173+ includeSubscriptionCredits ?: boolean
174+ } ) => ( {
175+ usageThisCycle : 0 ,
176+ balance : {
177+ totalRemaining : includeSubscriptionCredits ? 350 : 0 ,
178+ totalDebt : 0 ,
179+ netBalance : includeSubscriptionCredits ? 350 : 0 ,
180+ breakdown : { } ,
181+ principals : { } ,
182+ } ,
183+ nextQuotaReset : 'soon' ,
184+ } ) ,
185+ )
172186 const mockEnsureSubscriberBlockGrant = mock ( async ( ) => ( {
173187 grantId : 'grant-1' ,
174188 credits : 350 ,
175189 expiresAt : new Date ( Date . now ( ) + 5 * 60 * 60 * 1000 ) ,
176190 isNew : true ,
177- } ) ) as unknown as ( params : { userId : string ; logger : Logger } ) => Promise < BlockGrantResult | null >
191+ } ) ) as unknown as ( params : {
192+ userId : string
193+ logger : Logger
194+ } ) => Promise < BlockGrantResult | null >
178195
179196 const req = new NextRequest ( 'http://localhost:3000/api/v1/docs-search' , {
180197 method : 'POST' ,
@@ -195,7 +212,7 @@ describe('/api/v1/docs-search POST endpoint', () => {
195212 expect ( res . status ) . toBe ( 200 )
196213 } )
197214
198- test ( '402 for non-subscriber with 0 credits and no block grant' , async ( ) => {
215+ test ( '200 for non-subscriber with 0 credits and no block grant' , async ( ) => {
199216 mockGetUserUsageData = mock ( async ( ) => ( {
200217 usageThisCycle : 0 ,
201218 balance : {
@@ -207,7 +224,12 @@ describe('/api/v1/docs-search POST endpoint', () => {
207224 } ,
208225 nextQuotaReset : 'soon' ,
209226 } ) )
210- const mockEnsureSubscriberBlockGrant = mock ( async ( ) => null ) as unknown as ( params : { userId : string ; logger : Logger } ) => Promise < BlockGrantResult | null >
227+ const mockEnsureSubscriberBlockGrant = mock (
228+ async ( ) => null ,
229+ ) as unknown as ( params : {
230+ userId : string
231+ logger : Logger
232+ } ) => Promise < BlockGrantResult | null >
211233
212234 const req = new NextRequest ( 'http://localhost:3000/api/v1/docs-search' , {
213235 method : 'POST' ,
@@ -225,6 +247,10 @@ describe('/api/v1/docs-search POST endpoint', () => {
225247 fetch : mockFetch ,
226248 ensureSubscriberBlockGrant : mockEnsureSubscriberBlockGrant ,
227249 } )
228- expect ( res . status ) . toBe ( 402 )
250+ expect ( res . status ) . toBe ( 200 )
251+ const body = await res . json ( )
252+ expect ( body . creditsUsed ) . toBe ( 0 )
253+ expect ( mockGetUserUsageData ) . not . toHaveBeenCalled ( )
254+ expect ( mockConsumeCreditsWithFallback ) . not . toHaveBeenCalled ( )
229255 } )
230256} )
0 commit comments