@@ -39,8 +39,9 @@ describe('/api/files/presigned', () => {
3939 const response = await POST ( request )
4040 const data = await response . json ( )
4141
42- expect ( response . status ) . toBe ( 400 )
42+ expect ( response . status ) . toBe ( 500 ) // Changed from 400 to 500 (StorageConfigError )
4343 expect ( data . error ) . toBe ( 'Direct uploads are only available when cloud storage is enabled' )
44+ expect ( data . code ) . toBe ( 'STORAGE_CONFIG_ERROR' )
4445 expect ( data . directUploadSupported ) . toBe ( false )
4546 } )
4647
@@ -64,7 +65,8 @@ describe('/api/files/presigned', () => {
6465 const data = await response . json ( )
6566
6667 expect ( response . status ) . toBe ( 400 )
67- expect ( data . error ) . toBe ( 'Missing fileName or contentType' )
68+ expect ( data . error ) . toBe ( 'fileName is required and cannot be empty' )
69+ expect ( data . code ) . toBe ( 'VALIDATION_ERROR' )
6870 } )
6971
7072 it ( 'should return error when contentType is missing' , async ( ) => {
@@ -87,7 +89,59 @@ describe('/api/files/presigned', () => {
8789 const data = await response . json ( )
8890
8991 expect ( response . status ) . toBe ( 400 )
90- expect ( data . error ) . toBe ( 'Missing fileName or contentType' )
92+ expect ( data . error ) . toBe ( 'contentType is required and cannot be empty' )
93+ expect ( data . code ) . toBe ( 'VALIDATION_ERROR' )
94+ } )
95+
96+ it ( 'should return error when fileSize is invalid' , async ( ) => {
97+ setupFileApiMocks ( {
98+ cloudEnabled : true ,
99+ storageProvider : 's3' ,
100+ } )
101+
102+ const { POST } = await import ( './route' )
103+
104+ const request = new NextRequest ( 'http://localhost:3000/api/files/presigned' , {
105+ method : 'POST' ,
106+ body : JSON . stringify ( {
107+ fileName : 'test.txt' ,
108+ contentType : 'text/plain' ,
109+ fileSize : 0 ,
110+ } ) ,
111+ } )
112+
113+ const response = await POST ( request )
114+ const data = await response . json ( )
115+
116+ expect ( response . status ) . toBe ( 400 )
117+ expect ( data . error ) . toBe ( 'fileSize must be a positive number' )
118+ expect ( data . code ) . toBe ( 'VALIDATION_ERROR' )
119+ } )
120+
121+ it ( 'should return error when file size exceeds limit' , async ( ) => {
122+ setupFileApiMocks ( {
123+ cloudEnabled : true ,
124+ storageProvider : 's3' ,
125+ } )
126+
127+ const { POST } = await import ( './route' )
128+
129+ const largeFileSize = 150 * 1024 * 1024 // 150MB (exceeds 100MB limit)
130+ const request = new NextRequest ( 'http://localhost:3000/api/files/presigned' , {
131+ method : 'POST' ,
132+ body : JSON . stringify ( {
133+ fileName : 'large-file.txt' ,
134+ contentType : 'text/plain' ,
135+ fileSize : largeFileSize ,
136+ } ) ,
137+ } )
138+
139+ const response = await POST ( request )
140+ const data = await response . json ( )
141+
142+ expect ( response . status ) . toBe ( 400 )
143+ expect ( data . error ) . toContain ( 'exceeds maximum allowed size' )
144+ expect ( data . code ) . toBe ( 'VALIDATION_ERROR' )
91145 } )
92146
93147 it ( 'should generate S3 presigned URL successfully' , async ( ) => {
@@ -122,6 +176,34 @@ describe('/api/files/presigned', () => {
122176 expect ( data . directUploadSupported ) . toBe ( true )
123177 } )
124178
179+ it ( 'should generate knowledge-base S3 presigned URL with kb prefix' , async ( ) => {
180+ setupFileApiMocks ( {
181+ cloudEnabled : true ,
182+ storageProvider : 's3' ,
183+ } )
184+
185+ const { POST } = await import ( './route' )
186+
187+ const request = new NextRequest (
188+ 'http://localhost:3000/api/files/presigned?type=knowledge-base' ,
189+ {
190+ method : 'POST' ,
191+ body : JSON . stringify ( {
192+ fileName : 'knowledge-doc.pdf' ,
193+ contentType : 'application/pdf' ,
194+ fileSize : 2048 ,
195+ } ) ,
196+ }
197+ )
198+
199+ const response = await POST ( request )
200+ const data = await response . json ( )
201+
202+ expect ( response . status ) . toBe ( 200 )
203+ expect ( data . fileInfo . key ) . toMatch ( / ^ k b \/ .* k n o w l e d g e - d o c \. p d f $ / )
204+ expect ( data . directUploadSupported ) . toBe ( true )
205+ } )
206+
125207 it ( 'should generate Azure Blob presigned URL successfully' , async ( ) => {
126208 setupFileApiMocks ( {
127209 cloudEnabled : true ,
@@ -182,8 +264,9 @@ describe('/api/files/presigned', () => {
182264 const response = await POST ( request )
183265 const data = await response . json ( )
184266
185- expect ( response . status ) . toBe ( 400 )
186- expect ( data . error ) . toBe ( 'Unknown storage provider' )
267+ expect ( response . status ) . toBe ( 500 ) // Changed from 400 to 500 (StorageConfigError)
268+ expect ( data . error ) . toBe ( 'Unknown storage provider: unknown' ) // Updated error message
269+ expect ( data . code ) . toBe ( 'STORAGE_CONFIG_ERROR' )
187270 expect ( data . directUploadSupported ) . toBe ( false )
188271 } )
189272
@@ -225,8 +308,10 @@ describe('/api/files/presigned', () => {
225308 const data = await response . json ( )
226309
227310 expect ( response . status ) . toBe ( 500 )
228- expect ( data . error ) . toBe ( 'Error' )
229- expect ( data . message ) . toBe ( 'S3 service unavailable' )
311+ expect ( data . error ) . toBe (
312+ 'Failed to generate S3 presigned URL - check AWS credentials and permissions'
313+ ) // Updated error message
314+ expect ( data . code ) . toBe ( 'STORAGE_CONFIG_ERROR' )
230315 } )
231316
232317 it ( 'should handle Azure Blob errors gracefully' , async ( ) => {
@@ -269,8 +354,8 @@ describe('/api/files/presigned', () => {
269354 const data = await response . json ( )
270355
271356 expect ( response . status ) . toBe ( 500 )
272- expect ( data . error ) . toBe ( 'Error' )
273- expect ( data . message ) . toBe ( 'Azure service unavailable ' )
357+ expect ( data . error ) . toBe ( 'Failed to generate Azure Blob presigned URL' ) // Updated error message
358+ expect ( data . code ) . toBe ( 'STORAGE_CONFIG_ERROR ' )
274359 } )
275360
276361 it ( 'should handle malformed JSON gracefully' , async ( ) => {
@@ -289,9 +374,9 @@ describe('/api/files/presigned', () => {
289374 const response = await POST ( request )
290375 const data = await response . json ( )
291376
292- expect ( response . status ) . toBe ( 500 )
293- expect ( data . error ) . toBe ( 'SyntaxError' )
294- expect ( data . message ) . toContain ( 'Unexpected token ')
377+ expect ( response . status ) . toBe ( 400 ) // Changed from 500 to 400 (ValidationError )
378+ expect ( data . error ) . toBe ( 'Invalid JSON in request body' ) // Updated error message
379+ expect ( data . code ) . toBe ( 'VALIDATION_ERROR ')
295380 } )
296381 } )
297382
0 commit comments