Skip to content

Commit fd4f198

Browse files
committed
fix(storage): fixed the vector tests
1 parent ea92e50 commit fd4f198

File tree

8 files changed

+80
-32
lines changed

8 files changed

+80
-32
lines changed
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { version } from './version'
22
export const DEFAULT_HEADERS = {
33
'X-Client-Info': `storage-js/${version}`,
4-
'Content-Type': 'application/json',
54
}

packages/core/storage-js/src/lib/vectors/VectorBucketApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DEFAULT_HEADERS } from '../constants'
1+
import { DEFAULT_HEADERS } from './constants'
22
import { isStorageVectorsError } from './errors'
33
import { Fetch, post } from './fetch'
44
import { resolveFetch } from './helpers'

packages/core/storage-js/src/lib/vectors/VectorDataApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DEFAULT_HEADERS } from '../constants'
1+
import { DEFAULT_HEADERS } from './constants'
22
import { isStorageVectorsError } from './errors'
33
import { Fetch, post } from './fetch'
44
import { resolveFetch } from './helpers'

packages/core/storage-js/src/lib/vectors/VectorIndexApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DEFAULT_HEADERS } from '../constants'
1+
import { DEFAULT_HEADERS } from './constants'
22
import { isStorageVectorsError } from './errors'
33
import { Fetch, post } from './fetch'
44
import { resolveFetch } from './helpers'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { version } from '../version'
2+
export const DEFAULT_HEADERS = {
3+
'X-Client-Info': `storage-js/${version}`,
4+
'Content-Type': 'application/json',
5+
}

packages/core/storage-js/src/lib/vectors/fetch.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,39 @@ const handleError = async (
4040
reject: (reason?: any) => void,
4141
options?: FetchOptions
4242
) => {
43-
const Res = await resolveResponse()
44-
45-
if (error instanceof Res && !options?.noResolveJson) {
46-
error
47-
.json()
48-
.then((err: any) => {
49-
const status = error.status || 500
50-
const statusCode = err?.statusCode || err?.code || status + ''
51-
reject(new StorageVectorsApiError(_getErrorMessage(err), status, statusCode))
52-
})
53-
.catch((err: any) => {
54-
reject(new StorageVectorsUnknownError(_getErrorMessage(err), err))
55-
})
43+
// Check if error is a Response-like object (has status and ok properties)
44+
// This is more reliable than instanceof which can fail across realms
45+
const isResponseLike =
46+
error &&
47+
typeof error === 'object' &&
48+
'status' in error &&
49+
'ok' in error &&
50+
typeof (error as any).status === 'number'
51+
52+
if (isResponseLike && !options?.noResolveJson) {
53+
const status = (error as any).status || 500
54+
const responseError = error as any
55+
56+
// Try to parse JSON body if available
57+
if (typeof responseError.json === 'function') {
58+
responseError
59+
.json()
60+
.then((err: any) => {
61+
const statusCode = err?.statusCode || err?.code || status + ''
62+
reject(new StorageVectorsApiError(_getErrorMessage(err), status, statusCode))
63+
})
64+
.catch(() => {
65+
// If JSON parsing fails, create an ApiError with the HTTP status code
66+
const statusCode = status + ''
67+
const message = responseError.statusText || `HTTP ${status} error`
68+
reject(new StorageVectorsApiError(message, status, statusCode))
69+
})
70+
} else {
71+
// No json() method available, create error from status
72+
const statusCode = status + ''
73+
const message = responseError.statusText || `HTTP ${status} error`
74+
reject(new StorageVectorsApiError(message, status, statusCode))
75+
}
5676
} else {
5777
reject(new StorageVectorsUnknownError(_getErrorMessage(error), error))
5878
}

packages/core/storage-js/test/bucket-api.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ describe('VectorBucketApi Integration Tests', () => {
184184
const response = await client.deleteVectorBucket('non-existent-bucket')
185185

186186
const error = assertErrorResponse(response)
187-
assertErrorCode(error, 409)
187+
assertErrorCode(error, 404)
188188
})
189189

190190
it('should return error when bucket is not empty', async () => {
@@ -205,7 +205,7 @@ describe('VectorBucketApi Integration Tests', () => {
205205
const response = await client.deleteVectorBucket(bucketName)
206206

207207
const error = assertErrorResponse(response)
208-
assertErrorCode(error, 409)
208+
assertErrorCode(error, 400)
209209
expect(error.message).toContain('not empty')
210210
})
211211

packages/core/storage-js/test/mock-server.ts

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,13 @@ const storage = new MockStorage()
172172
*/
173173
export function createMockFetch(): Fetch {
174174
return async (input: string | URL | Request, init?: RequestInit): Promise<Response> => {
175-
const url = input instanceof Request ? input.url : input
175+
// Handle different input types safely without assuming Request constructor exists
176+
const url =
177+
typeof input === 'string'
178+
? input
179+
: input instanceof URL
180+
? input.toString()
181+
: (input as any).url || String(input)
176182
const urlStr = url.toString()
177183
const endpoint = urlStr.split('/').pop() || ''
178184
const body = init?.body ? JSON.parse(init.body as string) : {}
@@ -195,12 +201,30 @@ export function createMockFetch(): Fetch {
195201

196202
// Create mock Response object
197203
const responseBody = JSON.stringify(response.error || response.data || {})
198-
return new Response(responseBody, {
204+
205+
// Check if Response constructor is available (Node 18+, modern browsers)
206+
if (typeof Response !== 'undefined') {
207+
return new Response(responseBody, {
208+
status: response.status,
209+
headers: {
210+
'Content-Type': 'application/json',
211+
},
212+
}) as any
213+
}
214+
215+
// Fallback: Create a minimal Response-like object for older environments
216+
const mockResponse: any = {
217+
ok: response.status >= 200 && response.status < 300,
199218
status: response.status,
219+
statusText: response.status === 200 ? 'OK' : 'Error',
200220
headers: {
201-
'Content-Type': 'application/json',
221+
get: (key: string) => (key.toLowerCase() === 'content-type' ? 'application/json' : null),
202222
},
203-
}) as any
223+
json: async () => JSON.parse(responseBody),
224+
text: async () => responseBody,
225+
}
226+
227+
return mockResponse as Response
204228
}
205229
}
206230

@@ -324,7 +348,7 @@ function handleDeleteBucket(body: any): MockResponse {
324348
return {
325349
status: 404,
326350
error: {
327-
statusCode: 409,
351+
statusCode: 404,
328352
error: 'Not Found',
329353
message: `Bucket '${vectorBucketName}' not found`,
330354
},
@@ -336,7 +360,7 @@ function handleDeleteBucket(body: any): MockResponse {
336360
return {
337361
status: 400,
338362
error: {
339-
statusCode: 409,
363+
statusCode: 400,
340364
error: 'Bad Request',
341365
message: `Bucket '${vectorBucketName}' is not empty`,
342366
},
@@ -355,7 +379,7 @@ function handleCreateIndex(body: any): MockResponse {
355379
return {
356380
status: 404,
357381
error: {
358-
statusCode: 409,
382+
statusCode: 404,
359383
error: 'Not Found',
360384
message: `Bucket '${vectorBucketName}' not found`,
361385
},
@@ -388,7 +412,7 @@ function handleGetIndex(body: any): MockResponse {
388412
return {
389413
status: 404,
390414
error: {
391-
statusCode: 409,
415+
statusCode: 404,
392416
error: 'Not Found',
393417
message: `Bucket '${vectorBucketName}' not found`,
394418
},
@@ -400,7 +424,7 @@ function handleGetIndex(body: any): MockResponse {
400424
return {
401425
status: 404,
402426
error: {
403-
statusCode: 409,
427+
statusCode: 404,
404428
error: 'Not Found',
405429
message: `Index '${indexName}' not found`,
406430
},
@@ -420,7 +444,7 @@ function handleListIndexes(body: any): MockResponse {
420444
return {
421445
status: 404,
422446
error: {
423-
statusCode: 409,
447+
statusCode: 404,
424448
error: 'Not Found',
425449
message: `Bucket '${vectorBucketName}' not found`,
426450
},
@@ -445,7 +469,7 @@ function handleDeleteIndex(body: any): MockResponse {
445469
return {
446470
status: 404,
447471
error: {
448-
statusCode: 409,
472+
statusCode: 404,
449473
error: 'Not Found',
450474
message: `Bucket '${vectorBucketName}' not found`,
451475
},
@@ -456,7 +480,7 @@ function handleDeleteIndex(body: any): MockResponse {
456480
return {
457481
status: 404,
458482
error: {
459-
statusCode: 409,
483+
statusCode: 404,
460484
error: 'Not Found',
461485
message: `Index '${indexName}' not found`,
462486
},
@@ -608,7 +632,7 @@ function handleQueryVectors(body: any): MockResponse {
608632
return {
609633
status: 404,
610634
error: {
611-
statusCode: 409,
635+
statusCode: 404,
612636
error: 'Not Found',
613637
message: `Bucket '${vectorBucketName}' not found`,
614638
},

0 commit comments

Comments
 (0)