@@ -114,12 +114,15 @@ vi.mock('@/lib/core/telemetry', () => ({
114114
115115vi . mock ( '@/lib/posthog/server' , ( ) => ( { captureServerEvent : mocks . capture } ) )
116116
117+ import { OrchestrationError } from '@/lib/core/orchestration/types'
118+ import { KnowledgeUsageLimitExceededError } from '@/lib/knowledge/application/billing'
117119import {
118120 GET as listConnectorDocuments ,
119121 PATCH as updateConnectorDocuments ,
120122} from '@/app/api/knowledge/[id]/connectors/[connectorId]/documents/route'
121123import { PUT as updateDocument } from '@/app/api/knowledge/[id]/documents/[documentId]/route'
122124import {
125+ PATCH as bulkDocuments ,
123126 POST as createDocuments ,
124127 GET as listDocuments ,
125128} from '@/app/api/knowledge/[id]/documents/route'
@@ -360,6 +363,46 @@ describe('migrated internal Knowledge routes', () => {
360363 )
361364 } )
362365
366+ it ( 'preserves payment-required for document usage admission' , async ( ) => {
367+ mocks . createDocuments . mockRejectedValueOnce (
368+ new KnowledgeUsageLimitExceededError ( 'Usage limit exceeded' )
369+ )
370+
371+ const response = await createDocuments (
372+ createMockRequest ( 'POST' , {
373+ bulk : false ,
374+ filename : document . filename ,
375+ fileUrl : document . fileUrl ,
376+ fileSize : document . fileSize ,
377+ mimeType : document . mimeType ,
378+ } ) ,
379+ { params : Promise . resolve ( { id : 'knowledge-1' } ) }
380+ )
381+
382+ expect ( response . status ) . toBe ( 402 )
383+ await expect ( response . json ( ) ) . resolves . toEqual ( { error : 'Usage limit exceeded' } )
384+ expect ( mocks . capture ) . not . toHaveBeenCalled ( )
385+ } )
386+
387+ it ( 'returns not found when a bulk document selection has no active matches' , async ( ) => {
388+ mocks . bulkDocuments . mockRejectedValueOnce (
389+ new OrchestrationError ( 'not_found' , 'No valid documents found to update' )
390+ )
391+
392+ const response = await bulkDocuments (
393+ createMockRequest ( 'PATCH' , {
394+ operation : 'disable' ,
395+ documentIds : [ 'document-1' ] ,
396+ } ) ,
397+ { params : Promise . resolve ( { id : 'knowledge-1' } ) }
398+ )
399+
400+ expect ( response . status ) . toBe ( 404 )
401+ await expect ( response . json ( ) ) . resolves . toEqual ( {
402+ error : 'No valid documents found to update' ,
403+ } )
404+ } )
405+
363406 it ( 'rejects oversized document-create arrays at the contract boundary' , async ( ) => {
364407 const response = await createDocuments (
365408 createMockRequest ( 'POST' , {
0 commit comments