@@ -13,6 +13,21 @@ const hyper = new Hyper('hyper_1234');
13
13
describe ( 'Hypercode Types API methods' , ( ) => {
14
14
afterEach ( ( ) => fetchMock . resetMocks ( ) ) ;
15
15
16
+ describe ( "makeRequest method's error handling" , ( ) => {
17
+ it ( 'should return error if response is not ok' , async ( ) => {
18
+ fetchMock . mockOnce ( JSON . stringify ( { error : 'Not found' } ) , {
19
+ status : 404 ,
20
+ } ) ;
21
+
22
+ const result = await hyper [ 'types' ] [ 'makeRequest' ] ( {
23
+ endpointType : 'string' ,
24
+ query : 'Who is the CEO of SpaceX?' ,
25
+ } ) ;
26
+
27
+ expect ( result ) . toEqual ( { data : null , error : 'Not found' } ) ;
28
+ } ) ;
29
+ } ) ;
30
+
16
31
describe ( 'string method' , ( ) => {
17
32
it ( 'should return the correct string for an identification query' , async ( ) => {
18
33
fetchMock . mockOnce ( JSON . stringify ( { data : 'Elon Musk' } ) , {
@@ -228,4 +243,264 @@ describe('Hypercode Types API methods', () => {
228
243
) ;
229
244
} ) ;
230
245
} ) ;
246
+
247
+ describe ( 'stringArray method' , ( ) => {
248
+ const expectedResult = [
249
+ 'Human Resources' ,
250
+ 'Finance' ,
251
+ 'Research and Development' ,
252
+ 'Sales' ,
253
+ 'Customer Support' ,
254
+ ] ;
255
+
256
+ it ( 'should return the correct string array for a string array query' , async ( ) => {
257
+ fetchMock . mockOnce ( JSON . stringify ( { data : expectedResult } ) , {
258
+ status : 200 ,
259
+ } ) ;
260
+
261
+ const { data } = await hyper . types . stringArray (
262
+ 'List all department names' ,
263
+ ) ;
264
+
265
+ expect ( typeof data ) . toBe ( 'object' ) ;
266
+ expect ( Array . isArray ( data ) ) . toBe ( true ) ;
267
+ expect ( typeof data ! [ 0 ] ) . toBe ( 'string' ) ;
268
+ expect ( data ) . toEqual ( expectedResult ) ;
269
+ } ) ;
270
+
271
+ it ( 'should return the correct string array for a string array query with contextId' , async ( ) => {
272
+ const contextId = '9a8b7c6d-5e4f-3a2b-1c0d-e9f8a7b6c5d4' ;
273
+
274
+ fetchMock . mockOnce ( JSON . stringify ( { data : expectedResult } ) , {
275
+ status : 200 ,
276
+ } ) ;
277
+
278
+ const { data } = await hyper . types . stringArray (
279
+ 'List all department names' ,
280
+ { contextId } ,
281
+ ) ;
282
+
283
+ expect ( typeof data ) . toBe ( 'object' ) ;
284
+ expect ( Array . isArray ( data ) ) . toBe ( true ) ;
285
+ expect ( typeof data ! [ 0 ] ) . toBe ( 'string' ) ;
286
+ expect ( data ) . toEqual ( expectedResult ) ;
287
+ expect ( fetchMock ) . toHaveBeenLastCalledWith (
288
+ fullEndpoint ( 'string_array' ) , // endpoint
289
+ // body and headers
290
+ expect . objectContaining ( {
291
+ body : expect . stringContaining (
292
+ JSON . stringify ( {
293
+ query : 'List all department names' ,
294
+ context_id : contextId ,
295
+ } ) ,
296
+ ) ,
297
+ } ) ,
298
+ ) ;
299
+ } ) ;
300
+ } ) ;
301
+
302
+ describe ( 'integerArray method' , ( ) => {
303
+ const expectedResult = [ 25 , 40 , 15 , 50 , 30 ] ;
304
+
305
+ it ( 'should return the correct integer array for a integer array query' , async ( ) => {
306
+ fetchMock . mockOnce ( JSON . stringify ( { data : expectedResult } ) , {
307
+ status : 200 ,
308
+ } ) ;
309
+
310
+ const { data } = await hyper . types . integerArray (
311
+ 'What is the headcount for each department?' ,
312
+ ) ;
313
+
314
+ expect ( typeof data ) . toBe ( 'object' ) ;
315
+ expect ( Array . isArray ( data ) ) . toBe ( true ) ;
316
+ expect ( typeof data ! [ 0 ] ) . toBe ( 'number' ) ;
317
+ expect ( data ) . toEqual ( expectedResult ) ;
318
+ } ) ;
319
+
320
+ it ( 'should return the correct integer array for a integer array query with contextId' , async ( ) => {
321
+ const contextId = 'a1b2c3d4-e5f6-4g7h-8i9j-0k1l2m3n4o5p' ;
322
+
323
+ fetchMock . mockOnce ( JSON . stringify ( { data : expectedResult } ) , {
324
+ status : 200 ,
325
+ } ) ;
326
+
327
+ const { data } = await hyper . types . integerArray (
328
+ 'What is the headcount for each department?' ,
329
+ { contextId } ,
330
+ ) ;
331
+
332
+ expect ( typeof data ) . toBe ( 'object' ) ;
333
+ expect ( Array . isArray ( data ) ) . toBe ( true ) ;
334
+ expect ( typeof data ! [ 0 ] ) . toBe ( 'number' ) ;
335
+ expect ( data ) . toEqual ( expectedResult ) ;
336
+ expect ( fetchMock ) . toHaveBeenLastCalledWith (
337
+ fullEndpoint ( 'integer_array' ) , // endpoint
338
+ // body and headers
339
+ expect . objectContaining ( {
340
+ body : expect . stringContaining (
341
+ JSON . stringify ( {
342
+ query : 'What is the headcount for each department?' ,
343
+ context_id : contextId ,
344
+ } ) ,
345
+ ) ,
346
+ } ) ,
347
+ ) ;
348
+ } ) ;
349
+ } ) ;
350
+
351
+ describe ( 'floatArray method' , ( ) => {
352
+ const expectedResult = [ 4.2 , 3.8 , 4.5 , 4.7 , 3.9 ] ;
353
+
354
+ it ( 'should return the correct float array for a float array query' , async ( ) => {
355
+ fetchMock . mockOnce ( JSON . stringify ( { data : expectedResult } ) , {
356
+ status : 200 ,
357
+ } ) ;
358
+
359
+ const { data } = await hyper . types . floatArray (
360
+ 'What were the customer satisfaction ratings from the last survey?' ,
361
+ ) ;
362
+
363
+ expect ( typeof data ) . toBe ( 'object' ) ;
364
+ expect ( Array . isArray ( data ) ) . toBe ( true ) ;
365
+ expect ( typeof data ! [ 0 ] ) . toBe ( 'number' ) ;
366
+ expect ( data ) . toEqual ( expectedResult ) ;
367
+ } ) ;
368
+
369
+ it ( 'should return the correct float array for a float array query with contextId' , async ( ) => {
370
+ const contextId = '0f9e8d7c-6b5a-4c3d-2e1f-0a9b8c7d6e5f' ;
371
+
372
+ fetchMock . mockOnce ( JSON . stringify ( { data : expectedResult } ) , {
373
+ status : 200 ,
374
+ } ) ;
375
+
376
+ const { data } = await hyper . types . floatArray (
377
+ 'What were the customer satisfaction ratings from the last survey?' ,
378
+ { contextId } ,
379
+ ) ;
380
+
381
+ expect ( typeof data ) . toBe ( 'object' ) ;
382
+ expect ( Array . isArray ( data ) ) . toBe ( true ) ;
383
+ expect ( typeof data ! [ 0 ] ) . toBe ( 'number' ) ;
384
+ expect ( data ) . toEqual ( expectedResult ) ;
385
+ expect ( fetchMock ) . toHaveBeenLastCalledWith (
386
+ fullEndpoint ( 'float_array' ) , // endpoint
387
+ // body and headers
388
+ expect . objectContaining ( {
389
+ body : expect . stringContaining (
390
+ JSON . stringify ( {
391
+ query :
392
+ 'What were the customer satisfaction ratings from the last survey?' ,
393
+ context_id : contextId ,
394
+ } ) ,
395
+ ) ,
396
+ } ) ,
397
+ ) ;
398
+ } ) ;
399
+ } ) ;
400
+
401
+ describe ( 'booleanArray method' , ( ) => {
402
+ const expectedResult = [ true , false , true , true , false ] ;
403
+
404
+ it ( 'should return the correct boolean array for a boolean array query' , async ( ) => {
405
+ fetchMock . mockOnce ( JSON . stringify ( { data : expectedResult } ) , {
406
+ status : 200 ,
407
+ } ) ;
408
+
409
+ const { data } = await hyper . types . booleanArray (
410
+ 'Are services meeting performance targets?' ,
411
+ ) ;
412
+
413
+ expect ( typeof data ) . toBe ( 'object' ) ;
414
+ expect ( Array . isArray ( data ) ) . toBe ( true ) ;
415
+ expect ( typeof data ! [ 0 ] ) . toBe ( 'boolean' ) ;
416
+ expect ( data ) . toEqual ( expectedResult ) ;
417
+ } ) ;
418
+
419
+ it ( 'should return the correct boolean array for a boolean array query with contextId' , async ( ) => {
420
+ const contextId = '5f6a7b8c-9d0e-1f2a-3b4c-5d6e7f8g9h0i' ;
421
+
422
+ fetchMock . mockOnce ( JSON . stringify ( { data : expectedResult } ) , {
423
+ status : 200 ,
424
+ } ) ;
425
+
426
+ const { data } = await hyper . types . booleanArray (
427
+ 'Are services meeting performance targets?' ,
428
+ { contextId } ,
429
+ ) ;
430
+
431
+ expect ( typeof data ) . toBe ( 'object' ) ;
432
+ expect ( Array . isArray ( data ) ) . toBe ( true ) ;
433
+ expect ( typeof data ! [ 0 ] ) . toBe ( 'boolean' ) ;
434
+ expect ( data ) . toEqual ( expectedResult ) ;
435
+ expect ( fetchMock ) . toHaveBeenLastCalledWith (
436
+ fullEndpoint ( 'boolean_array' ) , // endpoint
437
+ // body and headers
438
+ expect . objectContaining ( {
439
+ body : expect . stringContaining (
440
+ JSON . stringify ( {
441
+ query : 'Are services meeting performance targets?' ,
442
+ context_id : contextId ,
443
+ } ) ,
444
+ ) ,
445
+ } ) ,
446
+ ) ;
447
+ } ) ;
448
+ } ) ;
449
+
450
+ describe ( 'datetimeArray method' , ( ) => {
451
+ const expectedResult = [
452
+ '2023-11-15T17:00:00Z' ,
453
+ '2023-12-01T17:00:00Z' ,
454
+ '2023-12-20T17:00:00Z' ,
455
+ ] ;
456
+
457
+ it ( 'should return the correct datetime array for a datetime array query' , async ( ) => {
458
+ fetchMock . mockOnce ( JSON . stringify ( { data : expectedResult } ) , {
459
+ status : 200 ,
460
+ } ) ;
461
+
462
+ const { data } = await hyper . types . datetimeArray (
463
+ 'What are the upcoming project deadlines?' ,
464
+ ) ;
465
+
466
+ expect ( typeof data ) . toBe ( 'object' ) ;
467
+ expect ( Array . isArray ( data ) ) . toBe ( true ) ;
468
+ expect ( typeof data ! [ 0 ] ) . toBe ( 'string' ) ;
469
+ expect ( data ) . toEqual ( expectedResult ) ;
470
+ expect ( new Date ( data ! [ 0 ] ) ) . toBeInstanceOf ( Date ) ;
471
+ expect ( new Date ( data ! [ 0 ] ) . getFullYear ( ) ) . toBe ( 2023 ) ;
472
+ } ) ;
473
+
474
+ it ( 'should return the correct datetime array for a datetime array query with contextId' , async ( ) => {
475
+ const contextId = '12345678-90ab-cdef-g123-456789abcdef' ;
476
+
477
+ fetchMock . mockOnce ( JSON . stringify ( { data : expectedResult } ) , {
478
+ status : 200 ,
479
+ } ) ;
480
+
481
+ const { data } = await hyper . types . datetimeArray (
482
+ 'What are the upcoming project deadlines?' ,
483
+ { contextId } ,
484
+ ) ;
485
+
486
+ expect ( typeof data ) . toBe ( 'object' ) ;
487
+ expect ( Array . isArray ( data ) ) . toBe ( true ) ;
488
+ expect ( typeof data ! [ 0 ] ) . toBe ( 'string' ) ;
489
+ expect ( data ) . toEqual ( expectedResult ) ;
490
+ expect ( new Date ( data ! [ 0 ] ) ) . toBeInstanceOf ( Date ) ;
491
+ expect ( new Date ( data ! [ 0 ] ) . getFullYear ( ) ) . toBe ( 2023 ) ;
492
+ expect ( fetchMock ) . toHaveBeenLastCalledWith (
493
+ fullEndpoint ( 'datetime_array' ) , // endpoint
494
+ // body and headers
495
+ expect . objectContaining ( {
496
+ body : expect . stringContaining (
497
+ JSON . stringify ( {
498
+ query : 'What are the upcoming project deadlines?' ,
499
+ context_id : contextId ,
500
+ } ) ,
501
+ ) ,
502
+ } ) ,
503
+ ) ;
504
+ } ) ;
505
+ } ) ;
231
506
} ) ;
0 commit comments