@superblocksteam/sabs-client 0.419.0 → 0.422.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/sabs.test.ts DELETED
@@ -1,1324 +0,0 @@
1
- import { ApplicationMetadata, BuildStatus, CreateLiveEditResponse, LiveEditStatus } from '@superblocksteam/sabs-types';
2
- import axios, { AxiosError, AxiosHeaders } from 'axios';
3
-
4
- import { SabsClient } from './sabs';
5
-
6
- describe('sabs service', () => {
7
- let anyBuildKey: string;
8
- let anyAccessToken: string;
9
-
10
- beforeEach(() => {
11
- anyBuildKey = 'any-secret-build-key';
12
- anyAccessToken = 'any-access-token';
13
- });
14
-
15
- afterEach(() => {
16
- jest.restoreAllMocks();
17
- });
18
-
19
- describe('build', () => {
20
- test.each([{ accessToken: 'anyScopedJwt', expectedHeaders: { Authorization: 'Bearer anyScopedJwt' } }])(
21
- 'returns expected response with accessToken=$accessToken',
22
- async ({ accessToken, expectedHeaders }) => {
23
- const expectedBuildId = 'expectedBuildId';
24
- const expectedCreated = new Date();
25
- const expectedUpdated = new Date();
26
-
27
- const mockAxios = jest.spyOn(axios, 'request');
28
- mockAxios.mockResolvedValue({
29
- data: {
30
- buildId: expectedBuildId,
31
- created: expectedCreated,
32
- updated: expectedUpdated
33
- }
34
- });
35
-
36
- const anyDirectoryHash = 'anyDirectoryHash';
37
- const anyApplicationMetadata = new ApplicationMetadata({
38
- id: 'anyApplicationId',
39
- organizationId: 'anyOrganizationId'
40
- });
41
-
42
- const sabs = new SabsClient('http://localhost:3000');
43
- const result = await sabs.build({
44
- directoryHash: anyDirectoryHash,
45
- meta: anyApplicationMetadata,
46
- buildKey: anyBuildKey,
47
- accessToken
48
- });
49
-
50
- expect(result).toEqual({
51
- buildId: expectedBuildId,
52
- created: expectedCreated,
53
- updated: expectedUpdated
54
- });
55
- expect(mockAxios).toHaveBeenCalledWith({
56
- method: 'POST',
57
- url: 'http://localhost:3000/v1/builds',
58
- headers: expectedHeaders,
59
- data: {
60
- directoryHash: anyDirectoryHash,
61
- applicationMetadata: anyApplicationMetadata,
62
- buildKey: anyBuildKey
63
- }
64
- });
65
- }
66
- );
67
-
68
- test('raises error when request fails', async () => {
69
- const mockAxios = jest.spyOn(axios, 'request');
70
- mockAxios.mockRejectedValue(new Error('any error'));
71
-
72
- const anyDirectoryHash = 'anyDirectoryHash';
73
- const anyApplicationMetadata = new ApplicationMetadata({
74
- id: 'anyApplicationId',
75
- organizationId: 'anyOrganizationId'
76
- });
77
-
78
- const sabs = new SabsClient('http://localhost:3000');
79
- await expect(
80
- sabs.build({ directoryHash: anyDirectoryHash, meta: anyApplicationMetadata, buildKey: anyBuildKey, accessToken: anyAccessToken })
81
- ).rejects.toThrow();
82
-
83
- expect(mockAxios).toHaveBeenCalledWith({
84
- method: 'POST',
85
- url: 'http://localhost:3000/v1/builds',
86
- headers: { Authorization: `Bearer ${anyAccessToken}` },
87
- data: {
88
- directoryHash: anyDirectoryHash,
89
- applicationMetadata: anyApplicationMetadata,
90
- buildKey: anyBuildKey
91
- }
92
- });
93
- });
94
-
95
- test('raises error when directory hash is empty', async () => {
96
- const sabs = new SabsClient('http://localhost:3000');
97
- const anyApplicationMetadata = new ApplicationMetadata({
98
- id: 'anyApplicationId',
99
- organizationId: 'anyOrganizationId'
100
- });
101
- await expect(sabs.build({ directoryHash: '', meta: anyApplicationMetadata, buildKey: anyBuildKey })).rejects.toThrow();
102
- });
103
-
104
- test('raises error when application metadata is empty', async () => {
105
- const sabs = new SabsClient('http://localhost:3000');
106
- await expect(
107
- sabs.build({
108
- directoryHash: '',
109
- meta: undefined as unknown as ApplicationMetadata,
110
- buildKey: anyBuildKey,
111
- accessToken: anyAccessToken
112
- })
113
- ).rejects.toThrow();
114
- });
115
-
116
- test('raises error when build key is empty', async () => {
117
- const sabs = new SabsClient('http://localhost:3000');
118
- const anyApplicationMetadata = new ApplicationMetadata({
119
- id: 'anyApplicationId',
120
- organizationId: 'anyOrganizationId'
121
- });
122
- await expect(
123
- sabs.build({ directoryHash: 'anyDirectoryHash', meta: anyApplicationMetadata, buildKey: '', accessToken: anyAccessToken })
124
- ).rejects.toThrow();
125
- });
126
-
127
- test('raises error when application id is empty', async () => {
128
- const sabs = new SabsClient('http://localhost:3000');
129
- const anyApplicationMetadata = new ApplicationMetadata({
130
- id: '',
131
- organizationId: 'anyOrganizationId'
132
- });
133
- await expect(
134
- sabs.build({ directoryHash: 'anyDirectoryHash', meta: anyApplicationMetadata, buildKey: anyBuildKey, accessToken: anyAccessToken })
135
- ).rejects.toThrow();
136
- });
137
-
138
- test('raises error when organization id is empty', async () => {
139
- const sabs = new SabsClient('http://localhost:3000');
140
- const anyApplicationMetadata = new ApplicationMetadata({
141
- id: 'anyApplicationId',
142
- organizationId: ''
143
- });
144
- await expect(
145
- sabs.build({ directoryHash: 'anyDirectoryHash', meta: anyApplicationMetadata, buildKey: anyBuildKey, accessToken: anyAccessToken })
146
- ).rejects.toThrow();
147
- });
148
-
149
- test('raises error when access token is empty', async () => {
150
- const sabs = new SabsClient('http://localhost:3000');
151
- const anyApplicationMetadata = new ApplicationMetadata({
152
- id: 'anyApplicationId',
153
- organizationId: 'anyOrganizationId'
154
- });
155
- await expect(
156
- sabs.build({ directoryHash: 'anyDirectoryHash', meta: anyApplicationMetadata, buildKey: anyBuildKey, accessToken: '' })
157
- ).rejects.toThrow();
158
- });
159
- });
160
-
161
- describe('status', () => {
162
- test.each([{ accessToken: 'anyScopedJwt', expectedHeaders: { Authorization: 'Bearer anyScopedJwt' } }])(
163
- 'returns expected response with accessToken=$accessToken',
164
- async ({ accessToken, expectedHeaders }) => {
165
- const expectedBuildId = 'expectedBuildId';
166
- const expectedStatus = BuildStatus.SUCCESS;
167
- const expectedCreated = new Date();
168
- const expectedUpdated = new Date();
169
-
170
- const mockAxios = jest.spyOn(axios, 'request');
171
- mockAxios.mockResolvedValue({
172
- data: {
173
- buildId: expectedBuildId,
174
- status: expectedStatus,
175
- created: expectedCreated,
176
- updated: expectedUpdated
177
- }
178
- });
179
-
180
- const anyBuildId = 'anyBuildId';
181
-
182
- const sabs = new SabsClient('http://localhost:3000');
183
- const result = await sabs.status({ buildId: anyBuildId, accessToken });
184
-
185
- expect(result).toEqual({
186
- buildId: expectedBuildId,
187
- status: expectedStatus,
188
- created: expectedCreated,
189
- updated: expectedUpdated
190
- });
191
- expect(mockAxios).toHaveBeenCalledWith({
192
- method: 'GET',
193
- url: `http://localhost:3000/v1/builds/${anyBuildId}`,
194
- headers: expectedHeaders
195
- });
196
- }
197
- );
198
-
199
- test('raises error when request fails', async () => {
200
- const mockAxios = jest.spyOn(axios, 'request');
201
- mockAxios.mockRejectedValue(new Error('any error'));
202
-
203
- const anyBuildId = 'anyBuildId';
204
-
205
- const sabs = new SabsClient('http://localhost:3000');
206
- await expect(sabs.status({ buildId: anyBuildId, accessToken: anyAccessToken })).rejects.toThrow();
207
-
208
- expect(mockAxios).toHaveBeenCalledWith({
209
- headers: { Authorization: `Bearer ${anyAccessToken}` },
210
- method: 'GET',
211
- url: `http://localhost:3000/v1/builds/${anyBuildId}`
212
- });
213
- });
214
-
215
- test('raises error when build id is empty', async () => {
216
- const sabs = new SabsClient('http://localhost:3000');
217
- await expect(sabs.status({ buildId: '', accessToken: anyAccessToken })).rejects.toThrow();
218
- });
219
-
220
- test('raises error when access token is empty', async () => {
221
- const sabs = new SabsClient('http://localhost:3000');
222
- await expect(sabs.status({ buildId: 'anyBuildId', accessToken: '' })).rejects.toThrow();
223
- });
224
- });
225
-
226
- describe('list', () => {
227
- test.each([{ accessToken: 'anyScopedJwt', expectedHeaders: { Authorization: 'Bearer anyScopedJwt' } }])(
228
- 'returns expected response with accessToken=$accessToken',
229
- async ({ accessToken, expectedHeaders }) => {
230
- const mockAxios = jest.spyOn(axios, 'request');
231
- mockAxios.mockResolvedValue({
232
- data: {
233
- builds: [
234
- {
235
- buildId: 'id1',
236
- status: BuildStatus.SUCCESS,
237
- created: new Date('2023-01-01'),
238
- updated: new Date('2023-01-02')
239
- },
240
- {
241
- buildId: 'id2',
242
- status: BuildStatus.RUNNING,
243
- created: new Date('2023-01-03'),
244
- updated: new Date('2023-01-04')
245
- },
246
- {
247
- buildId: 'id3',
248
- status: BuildStatus.FAILED,
249
- error: 'Build failed',
250
- created: new Date('2023-01-05'),
251
- updated: new Date('2023-01-06')
252
- }
253
- ]
254
- }
255
- });
256
-
257
- const anyOrganizationId = 'anyOrganizationId';
258
- const anyApplicationId = 'anyApplicationId';
259
- const anyDirectoryHash = 'anyDirectoryHash';
260
-
261
- const sabs = new SabsClient('http://localhost:3000');
262
- const result = await sabs.list({
263
- organizationId: anyOrganizationId,
264
- applicationId: anyApplicationId,
265
- directoryHash: anyDirectoryHash,
266
- accessToken
267
- });
268
-
269
- expect(result).toEqual({
270
- builds: [
271
- {
272
- buildId: 'id1',
273
- status: BuildStatus.SUCCESS,
274
- created: new Date('2023-01-01'),
275
- updated: new Date('2023-01-02')
276
- },
277
- {
278
- buildId: 'id2',
279
- status: BuildStatus.RUNNING,
280
- created: new Date('2023-01-03'),
281
- updated: new Date('2023-01-04')
282
- },
283
- {
284
- buildId: 'id3',
285
- status: BuildStatus.FAILED,
286
- error: 'Build failed',
287
- created: new Date('2023-01-05'),
288
- updated: new Date('2023-01-06')
289
- }
290
- ]
291
- });
292
- expect(mockAxios).toHaveBeenCalledWith({
293
- method: 'GET',
294
- url: `http://localhost:3000/v1/build`,
295
- headers: expectedHeaders,
296
- params: {
297
- organizationId: anyOrganizationId,
298
- applicationId: anyApplicationId,
299
- directoryHash: anyDirectoryHash
300
- }
301
- });
302
- }
303
- );
304
-
305
- test('raises error when request fails', async () => {
306
- const mockAxios = jest.spyOn(axios, 'request');
307
- mockAxios.mockRejectedValue(new Error('any error'));
308
-
309
- const anyOrganizationId = 'anyOrganizationId';
310
- const anyApplicationId = 'anyApplicationId';
311
- const anyDirectoryHash = 'anyDirectoryHash';
312
-
313
- const sabs = new SabsClient('http://localhost:3000');
314
- await expect(
315
- sabs.list({
316
- organizationId: anyOrganizationId,
317
- applicationId: anyApplicationId,
318
- directoryHash: anyDirectoryHash,
319
- accessToken: anyAccessToken
320
- })
321
- ).rejects.toThrow();
322
-
323
- expect(mockAxios).toHaveBeenCalledWith({
324
- method: 'GET',
325
- url: `http://localhost:3000/v1/build`,
326
- headers: { Authorization: `Bearer ${anyAccessToken}` },
327
- params: {
328
- organizationId: anyOrganizationId,
329
- applicationId: anyApplicationId,
330
- directoryHash: anyDirectoryHash
331
- }
332
- });
333
- });
334
-
335
- test('raises error when organization id is empty', async () => {
336
- const sabs = new SabsClient('http://localhost:3000');
337
- await expect(
338
- sabs.list({ organizationId: '', applicationId: 'anyApplicationId', directoryHash: 'anyDirectoryHash', accessToken: anyAccessToken })
339
- ).rejects.toThrow();
340
- });
341
-
342
- test('raises error when application id is empty', async () => {
343
- const sabs = new SabsClient('http://localhost:3000');
344
- await expect(
345
- sabs.list({
346
- organizationId: 'anyOrganizationId',
347
- applicationId: '',
348
- directoryHash: 'anyDirectoryHash',
349
- accessToken: anyAccessToken
350
- })
351
- ).rejects.toThrow();
352
- });
353
-
354
- test('raises error when directory hash is empty', async () => {
355
- const sabs = new SabsClient('http://localhost:3000');
356
- await expect(
357
- sabs.list({
358
- organizationId: 'anyOrganizationId',
359
- applicationId: 'anyApplicationId',
360
- directoryHash: '',
361
- accessToken: anyAccessToken
362
- })
363
- ).rejects.toThrow();
364
- });
365
-
366
- test('raises error when access token is empty', async () => {
367
- const sabs = new SabsClient('http://localhost:3000');
368
- await expect(
369
- sabs.list({
370
- organizationId: 'anyOrganizationId',
371
- applicationId: 'anyApplicationId',
372
- directoryHash: 'anyDirectoryHash',
373
- accessToken: ''
374
- })
375
- ).rejects.toThrow();
376
- });
377
- });
378
-
379
- describe('terminate', () => {
380
- test.each([{ accessToken: 'anyScopedJwt', expectedHeaders: { Authorization: 'Bearer anyScopedJwt' } }])(
381
- 'returns expected response with accessToken=$accessToken',
382
- async ({ accessToken, expectedHeaders }) => {
383
- const expectedBuildId = 'expectedBuildId';
384
- const expectedStatus = BuildStatus.TIMED_OUT;
385
- const expectedError = 'build timed out';
386
- const expectedCreated = new Date();
387
- const expectedUpdated = new Date();
388
-
389
- const mockAxios = jest.spyOn(axios, 'request');
390
- mockAxios.mockResolvedValue({
391
- data: {
392
- buildId: expectedBuildId,
393
- status: expectedStatus,
394
- error: expectedError,
395
- created: expectedCreated,
396
- updated: expectedUpdated
397
- }
398
- });
399
-
400
- const anyBuildId = 'anyBuildId';
401
- const anyStatus = BuildStatus.TIMED_OUT;
402
- const anyError = 'build timed out';
403
-
404
- const sabs = new SabsClient('http://localhost:3000');
405
- const result = await sabs.terminate({
406
- buildId: anyBuildId,
407
- status: anyStatus,
408
- buildKey: anyBuildKey,
409
- error: anyError,
410
- accessToken
411
- });
412
-
413
- expect(result).toEqual({
414
- buildId: expectedBuildId,
415
- status: expectedStatus,
416
- error: expectedError,
417
- created: expectedCreated,
418
- updated: expectedUpdated
419
- });
420
- expect(mockAxios).toHaveBeenCalledWith({
421
- method: 'POST',
422
- url: `http://localhost:3000/v1/builds/${anyBuildId}/terminate`,
423
- headers: expectedHeaders,
424
- data: {
425
- buildId: anyBuildId,
426
- status: anyStatus,
427
- error: anyError,
428
- buildKey: anyBuildKey
429
- }
430
- });
431
- }
432
- );
433
-
434
- test('raises error when request fails', async () => {
435
- const mockAxios = jest.spyOn(axios, 'request');
436
- mockAxios.mockRejectedValue(new Error('any error'));
437
-
438
- const anyBuildId = 'anyBuildId';
439
- const anyStatus = BuildStatus.TIMED_OUT;
440
- const anyError = 'build timed out';
441
-
442
- const sabs = new SabsClient('http://localhost:3000');
443
- await expect(
444
- sabs.terminate({ buildId: anyBuildId, status: anyStatus, buildKey: anyBuildKey, error: anyError, accessToken: anyAccessToken })
445
- ).rejects.toThrow();
446
-
447
- expect(mockAxios).toHaveBeenCalledWith({
448
- method: 'POST',
449
- headers: { Authorization: `Bearer ${anyAccessToken}` },
450
- url: `http://localhost:3000/v1/builds/${anyBuildId}/terminate`,
451
- data: {
452
- buildId: anyBuildId,
453
- status: anyStatus,
454
- error: anyError,
455
- buildKey: anyBuildKey
456
- }
457
- });
458
- });
459
-
460
- test('raises error when build id is empty', async () => {
461
- const sabs = new SabsClient('http://localhost:3000');
462
- await expect(
463
- sabs.terminate({
464
- buildId: '',
465
- status: BuildStatus.TIMED_OUT,
466
- buildKey: anyBuildKey,
467
- error: 'build timed out',
468
- accessToken: anyAccessToken
469
- })
470
- ).rejects.toThrow();
471
- });
472
-
473
- test('raises error when status is empty', async () => {
474
- const sabs = new SabsClient('http://localhost:3000');
475
- await expect(
476
- sabs.terminate({
477
- buildId: 'anyBuildId',
478
- status: undefined as unknown as BuildStatus,
479
- buildKey: anyBuildKey,
480
- error: 'build timed out',
481
- accessToken: anyAccessToken
482
- })
483
- ).rejects.toThrow();
484
- });
485
-
486
- test('raises error when build key is empty', async () => {
487
- const sabs = new SabsClient('http://localhost:3000');
488
- await expect(
489
- sabs.terminate({
490
- buildId: 'anyBuildId',
491
- status: BuildStatus.TIMED_OUT,
492
- buildKey: '',
493
- error: 'build timed out',
494
- accessToken: anyAccessToken
495
- })
496
- ).rejects.toThrow();
497
- });
498
-
499
- test('raises error when access token is empty', async () => {
500
- const sabs = new SabsClient('http://localhost:3000');
501
- await expect(
502
- sabs.terminate({
503
- buildId: 'anyBuildId',
504
- status: BuildStatus.TIMED_OUT,
505
- buildKey: anyBuildKey,
506
- error: 'build timed out',
507
- accessToken: ''
508
- })
509
- ).rejects.toThrow();
510
- });
511
-
512
- test('includes stepTimings when provided', async () => {
513
- const mockAxios = jest.spyOn(axios, 'request');
514
- mockAxios.mockResolvedValue({
515
- data: {
516
- buildId: 'expectedBuildId',
517
- status: BuildStatus.SUCCESS,
518
- created: new Date(),
519
- updated: new Date()
520
- }
521
- });
522
-
523
- const stepTimings = {
524
- createTempDirMs: 100,
525
- fetchAppBundleMs: 200,
526
- npmInstallMs: 300,
527
- buildMs: 400,
528
- uploadMs: 500
529
- };
530
-
531
- const sabs = new SabsClient('http://localhost:3000');
532
- await sabs.terminate({
533
- buildId: 'anyBuildId',
534
- status: BuildStatus.SUCCESS,
535
- buildKey: anyBuildKey,
536
- stepTimings,
537
- accessToken: anyAccessToken
538
- });
539
-
540
- expect(mockAxios).toHaveBeenCalledWith(
541
- expect.objectContaining({
542
- data: expect.objectContaining({
543
- stepTimings: expect.objectContaining({
544
- createTempDirMs: BigInt(100),
545
- fetchAppBundleMs: BigInt(200),
546
- npmInstallMs: BigInt(300),
547
- buildMs: BigInt(400),
548
- uploadMs: BigInt(500)
549
- })
550
- })
551
- })
552
- );
553
- });
554
-
555
- test('includes sizeMetrics when provided', async () => {
556
- const mockAxios = jest.spyOn(axios, 'request');
557
- mockAxios.mockResolvedValue({
558
- data: {
559
- buildId: 'expectedBuildId',
560
- status: BuildStatus.SUCCESS,
561
- created: new Date(),
562
- updated: new Date()
563
- }
564
- });
565
-
566
- const sizeMetrics = {
567
- sourceBundleSizeBytes: 1000000,
568
- nodeModulesSizeBytes: 50000000,
569
- buildOutputSizeBytes: 2000000,
570
- dependencyCount: 150
571
- };
572
-
573
- const sabs = new SabsClient('http://localhost:3000');
574
- await sabs.terminate({
575
- buildId: 'anyBuildId',
576
- status: BuildStatus.SUCCESS,
577
- buildKey: anyBuildKey,
578
- sizeMetrics,
579
- accessToken: anyAccessToken
580
- });
581
-
582
- expect(mockAxios).toHaveBeenCalledWith(
583
- expect.objectContaining({
584
- data: expect.objectContaining({
585
- sizeMetrics: expect.objectContaining({
586
- sourceBundleSizeBytes: BigInt(1000000),
587
- nodeModulesSizeBytes: BigInt(50000000),
588
- buildOutputSizeBytes: BigInt(2000000),
589
- dependencyCount: BigInt(150)
590
- })
591
- })
592
- })
593
- );
594
- });
595
-
596
- test('includes both stepTimings and sizeMetrics when provided', async () => {
597
- const mockAxios = jest.spyOn(axios, 'request');
598
- mockAxios.mockResolvedValue({
599
- data: {
600
- buildId: 'expectedBuildId',
601
- status: BuildStatus.SUCCESS,
602
- created: new Date(),
603
- updated: new Date()
604
- }
605
- });
606
-
607
- const stepTimings = {
608
- createTempDirMs: 100,
609
- npmInstallMs: 300
610
- };
611
-
612
- const sizeMetrics = {
613
- sourceBundleSizeBytes: 1000000,
614
- dependencyCount: 150
615
- };
616
-
617
- const sabs = new SabsClient('http://localhost:3000');
618
- await sabs.terminate({
619
- buildId: 'anyBuildId',
620
- status: BuildStatus.SUCCESS,
621
- buildKey: anyBuildKey,
622
- stepTimings,
623
- sizeMetrics,
624
- accessToken: anyAccessToken
625
- });
626
-
627
- expect(mockAxios).toHaveBeenCalledWith(
628
- expect.objectContaining({
629
- data: expect.objectContaining({
630
- stepTimings: expect.objectContaining({
631
- createTempDirMs: BigInt(100),
632
- npmInstallMs: BigInt(300)
633
- }),
634
- sizeMetrics: expect.objectContaining({
635
- sourceBundleSizeBytes: BigInt(1000000),
636
- dependencyCount: BigInt(150)
637
- })
638
- })
639
- })
640
- );
641
- });
642
-
643
- test('handles partial sizeMetrics with undefined fields', async () => {
644
- const mockAxios = jest.spyOn(axios, 'request');
645
- mockAxios.mockResolvedValue({
646
- data: {
647
- buildId: 'expectedBuildId',
648
- status: BuildStatus.SUCCESS,
649
- created: new Date(),
650
- updated: new Date()
651
- }
652
- });
653
-
654
- const sizeMetrics = {
655
- sourceBundleSizeBytes: 1000000
656
- // Other fields intentionally undefined
657
- };
658
-
659
- const sabs = new SabsClient('http://localhost:3000');
660
- await sabs.terminate({
661
- buildId: 'anyBuildId',
662
- status: BuildStatus.SUCCESS,
663
- buildKey: anyBuildKey,
664
- sizeMetrics,
665
- accessToken: anyAccessToken
666
- });
667
-
668
- expect(mockAxios).toHaveBeenCalledWith(
669
- expect.objectContaining({
670
- data: expect.objectContaining({
671
- sizeMetrics: expect.objectContaining({
672
- sourceBundleSizeBytes: BigInt(1000000)
673
- })
674
- })
675
- })
676
- );
677
- });
678
- });
679
-
680
- describe('bulkStatus', () => {
681
- test('returns expected response', async () => {
682
- const mockAxios = jest.spyOn(axios, 'request');
683
- mockAxios.mockResolvedValue({
684
- data: {
685
- builds: [
686
- {
687
- buildId: 'build1',
688
- status: BuildStatus.SUCCESS,
689
- created: new Date('2023-01-01'),
690
- updated: new Date('2023-01-02')
691
- },
692
- {
693
- buildId: 'build2',
694
- status: BuildStatus.RUNNING,
695
- created: new Date('2023-01-03'),
696
- updated: new Date('2023-01-04')
697
- }
698
- ]
699
- }
700
- });
701
-
702
- const anyOrganizationId = 'anyOrganizationId';
703
- const anyApplicationId = 'anyApplicationId';
704
- const anyDirectoryHashes = ['hash1', 'hash2'];
705
-
706
- const sabs = new SabsClient('http://localhost:3000');
707
- const result = await sabs.bulkStatus({
708
- organizationId: anyOrganizationId,
709
- applicationId: anyApplicationId,
710
- directoryHashes: anyDirectoryHashes,
711
- accessToken: anyAccessToken
712
- });
713
-
714
- expect(result).toEqual({
715
- builds: [
716
- {
717
- buildId: 'build1',
718
- status: BuildStatus.SUCCESS,
719
- created: new Date('2023-01-01'),
720
- updated: new Date('2023-01-02')
721
- },
722
- {
723
- buildId: 'build2',
724
- status: BuildStatus.RUNNING,
725
- created: new Date('2023-01-03'),
726
- updated: new Date('2023-01-04')
727
- }
728
- ]
729
- });
730
- expect(mockAxios).toHaveBeenCalledWith({
731
- method: 'POST',
732
- headers: { Authorization: `Bearer ${anyAccessToken}` },
733
- url: `http://localhost:3000/v1/builds/${anyOrganizationId}/${anyApplicationId}/bulk-status`,
734
- data: {
735
- organizationId: anyOrganizationId,
736
- applicationId: anyApplicationId,
737
- directoryHashes: anyDirectoryHashes
738
- }
739
- });
740
- });
741
-
742
- test('raises error when access token is empty', async () => {
743
- const sabs = new SabsClient('http://localhost:3000');
744
- await expect(
745
- sabs.bulkStatus({
746
- organizationId: 'anyOrganizationId',
747
- applicationId: 'anyApplicationId',
748
- directoryHashes: ['anyDirectoryHash'],
749
- accessToken: ''
750
- })
751
- ).rejects.toThrow();
752
- });
753
-
754
- test('raises error when organization id is empty', async () => {
755
- const sabs = new SabsClient('http://localhost:3000');
756
- await expect(
757
- sabs.bulkStatus({
758
- organizationId: '',
759
- applicationId: 'anyApplicationId',
760
- directoryHashes: ['anyDirectoryHash'],
761
- accessToken: anyAccessToken
762
- })
763
- ).rejects.toThrow();
764
- });
765
-
766
- test('raises error when application id is empty', async () => {
767
- const sabs = new SabsClient('http://localhost:3000');
768
- await expect(
769
- sabs.bulkStatus({
770
- organizationId: 'anyOrganizationId',
771
- applicationId: '',
772
- directoryHashes: ['anyDirectoryHash'],
773
- accessToken: anyAccessToken
774
- })
775
- ).rejects.toThrow();
776
- });
777
-
778
- test('raises error when directory hashes is empty', async () => {
779
- const sabs = new SabsClient('http://localhost:3000');
780
- await expect(
781
- sabs.bulkStatus({
782
- organizationId: 'anyOrganizationId',
783
- applicationId: 'anyApplicationId',
784
- directoryHashes: [],
785
- accessToken: anyAccessToken
786
- })
787
- ).rejects.toThrow();
788
- });
789
- });
790
-
791
- describe('liveEdit', () => {
792
- const applicationId = 'anyApplicationId';
793
- const organizationId = 'anyOrganizationId';
794
- const branch = 'anyBranch';
795
-
796
- test('raises error when application id is empty', async () => {
797
- const sabs = new SabsClient('http://localhost:3000');
798
- await expect(
799
- sabs.createLiveEdit({
800
- applicationId: '',
801
- organizationId: 'anyOrganizationId',
802
- branch: 'anyBranch',
803
- expiresIn: 1000,
804
- accessToken: anyAccessToken
805
- })
806
- ).rejects.toThrow();
807
- });
808
-
809
- test('raises error when organization id is empty', async () => {
810
- const sabs = new SabsClient('http://localhost:3000');
811
- await expect(
812
- sabs.createLiveEdit({
813
- applicationId: 'anyApplicationId',
814
- organizationId: '',
815
- branch: 'anyBranch',
816
- expiresIn: 1000,
817
- accessToken: anyAccessToken
818
- })
819
- ).rejects.toThrow();
820
- });
821
-
822
- test('raises error when branch is empty', async () => {
823
- const sabs = new SabsClient('http://localhost:3000');
824
- await expect(
825
- sabs.createLiveEdit({
826
- applicationId: 'anyApplicationId',
827
- organizationId: 'anyOrganizationId',
828
- branch: '',
829
- expiresIn: 1000,
830
- accessToken: anyAccessToken
831
- })
832
- ).rejects.toThrow();
833
- });
834
-
835
- test('raises error when access token is empty', async () => {
836
- const sabs = new SabsClient('http://localhost:3000');
837
- await expect(
838
- sabs.createLiveEdit({
839
- applicationId: 'anyApplicationId',
840
- organizationId: 'anyOrganizationId',
841
- branch: 'anyBranch',
842
- expiresIn: 1000,
843
- accessToken: ''
844
- })
845
- ).rejects.toThrow();
846
- });
847
-
848
- test('createLiveEdit', async () => {
849
- const mockAxios = jest.spyOn(axios, 'request');
850
- const expiresInSeconds = 1000;
851
- const now = Date.now();
852
- const expectedRequest = {
853
- application: {
854
- applicationId,
855
- organizationId,
856
- branch
857
- },
858
- expiresIn: BigInt(expiresInSeconds),
859
- sessionJwt: anyAccessToken,
860
- clientKey: ''
861
- };
862
- const newLiveEditResponse = new CreateLiveEditResponse({
863
- liveEditId: 'liveEditId',
864
- liveEditUrl: 'http://localhost:3000/live-edit/liveEditId',
865
- application: {
866
- applicationId,
867
- organizationId,
868
- branch
869
- },
870
- expiresAt: BigInt(now + expiresInSeconds * 1000)
871
- });
872
-
873
- mockAxios.mockResolvedValue({ data: newLiveEditResponse });
874
-
875
- const sabs = new SabsClient('http://localhost:3000');
876
- const result = await sabs.createLiveEdit({
877
- applicationId,
878
- organizationId,
879
- branch,
880
- expiresIn: expiresInSeconds,
881
- accessToken: anyAccessToken
882
- });
883
-
884
- expect(result).toEqual(newLiveEditResponse);
885
- expect(mockAxios).toHaveBeenCalledWith({
886
- method: 'POST',
887
- url: 'http://localhost:3000/v1/live-edit',
888
- data: expectedRequest,
889
- headers: {
890
- Authorization: `Bearer ${anyAccessToken}`
891
- }
892
- });
893
- });
894
-
895
- test('createLiveEdit with clientKey', async () => {
896
- const mockAxios = jest.spyOn(axios, 'request');
897
- const expiresInSeconds = 1000;
898
- const now = Date.now();
899
- const clientKey = 'user-123';
900
- const expectedRequest = {
901
- application: {
902
- applicationId,
903
- organizationId,
904
- branch
905
- },
906
- expiresIn: BigInt(expiresInSeconds),
907
- sessionJwt: anyAccessToken,
908
- clientKey: clientKey
909
- };
910
- const newLiveEditResponse = new CreateLiveEditResponse({
911
- liveEditId: 'liveEditId',
912
- liveEditUrl: 'http://localhost:3000/live-edit/liveEditId',
913
- application: {
914
- applicationId,
915
- organizationId,
916
- branch
917
- },
918
- expiresAt: BigInt(now + expiresInSeconds * 1000)
919
- });
920
-
921
- mockAxios.mockResolvedValue({ data: newLiveEditResponse });
922
-
923
- const sabs = new SabsClient('http://localhost:3000');
924
- const result = await sabs.createLiveEdit({
925
- applicationId,
926
- organizationId,
927
- branch,
928
- expiresIn: expiresInSeconds,
929
- accessToken: anyAccessToken,
930
- clientKey: clientKey
931
- });
932
-
933
- expect(result).toEqual(newLiveEditResponse);
934
- expect(mockAxios).toHaveBeenCalledWith({
935
- method: 'POST',
936
- url: 'http://localhost:3000/v1/live-edit',
937
- data: expectedRequest,
938
- headers: {
939
- Authorization: `Bearer ${anyAccessToken}`
940
- }
941
- });
942
- });
943
-
944
- test('terminateLiveEdit', async () => {
945
- const mockAxios = jest.spyOn(axios, 'request');
946
- const liveEditId = 'liveEditId';
947
- const expectedRequest = { liveEditId };
948
- const expectedCreated = new Date();
949
- const expectedUpdated = new Date();
950
- const expectedResponse = {
951
- liveEditId,
952
- status: LiveEditStatus.TERMINATED,
953
- application: {
954
- applicationId,
955
- organizationId,
956
- branch
957
- },
958
- created: expectedCreated,
959
- updated: expectedUpdated
960
- };
961
-
962
- mockAxios.mockResolvedValue({ data: expectedResponse });
963
-
964
- const sabs = new SabsClient('http://localhost:3000');
965
- const result = await sabs.terminateLiveEdit({ liveEditId, accessToken: anyAccessToken });
966
-
967
- expect(result).toEqual(expectedResponse);
968
- expect(mockAxios).toHaveBeenCalledWith({
969
- method: 'POST',
970
- url: `http://localhost:3000/v1/live-edit/${liveEditId}/terminate`,
971
- data: expectedRequest,
972
- headers: {
973
- Authorization: `Bearer ${anyAccessToken}`
974
- }
975
- });
976
- });
977
-
978
- test('raises error when live edit id is empty', async () => {
979
- const sabs = new SabsClient('http://localhost:3000');
980
- await expect(sabs.terminateLiveEdit({ liveEditId: '', accessToken: anyAccessToken })).rejects.toThrow();
981
- });
982
-
983
- test('raises error when access token is empty', async () => {
984
- const sabs = new SabsClient('http://localhost:3000');
985
- await expect(sabs.terminateLiveEdit({ liveEditId: 'liveEditId', accessToken: '' })).rejects.toThrow();
986
- });
987
-
988
- test('terminateLiveEdit raises error when request fails', async () => {
989
- const mockAxios = jest.spyOn(axios, 'request');
990
- mockAxios.mockRejectedValue(new Error('any error'));
991
-
992
- const liveEditId = 'liveEditId';
993
-
994
- const sabs = new SabsClient('http://localhost:3000');
995
- await expect(sabs.terminateLiveEdit({ liveEditId, accessToken: anyAccessToken })).rejects.toThrow();
996
-
997
- expect(mockAxios).toHaveBeenCalledWith({
998
- method: 'POST',
999
- url: `http://localhost:3000/v1/live-edit/${liveEditId}/terminate`,
1000
- data: {
1001
- liveEditId
1002
- },
1003
- headers: {
1004
- Authorization: `Bearer ${anyAccessToken}`
1005
- }
1006
- });
1007
- });
1008
- });
1009
-
1010
- describe('executeRequest', () => {
1011
- test('raies expected error when error is axios error', async () => {
1012
- const mockAxios = jest.spyOn(axios, 'request');
1013
- mockAxios.mockRejectedValue(
1014
- new AxiosError('request failed', 'internal', undefined, undefined, {
1015
- headers: {},
1016
- config: {
1017
- headers: new AxiosHeaders()
1018
- },
1019
- status: 500,
1020
- statusText: 'internal server error',
1021
- data: 'failed to process request'
1022
- })
1023
- );
1024
-
1025
- const anyBuildId = 'anyBuildId';
1026
-
1027
- const sabs = new SabsClient('http://localhost:3000');
1028
- await expect(sabs.status({ buildId: anyBuildId, accessToken: anyAccessToken })).rejects.toThrow();
1029
- });
1030
-
1031
- test('re-raises error when error is not axios error', async () => {
1032
- const mockAxios = jest.spyOn(axios, 'request');
1033
- mockAxios.mockRejectedValue(new Error('unexpected error'));
1034
-
1035
- const anyBuildId = 'anyBuildId';
1036
-
1037
- const sabs = new SabsClient('http://localhost:3000');
1038
- await expect(sabs.status({ buildId: anyBuildId, accessToken: anyAccessToken })).rejects.toThrow();
1039
- });
1040
- });
1041
-
1042
- describe('backward compatibility', () => {
1043
- test('errors can be caught as generic Error instances (backward compatibility)', async () => {
1044
- const mockAxios = jest.spyOn(axios, 'request');
1045
- mockAxios.mockRejectedValue(
1046
- new AxiosError('Service unavailable', 'ECONNREFUSED', undefined, undefined, {
1047
- headers: {},
1048
- config: {
1049
- headers: new AxiosHeaders()
1050
- },
1051
- status: 503,
1052
- statusText: 'Service Unavailable',
1053
- data: { message: 'Server temporarily unavailable' }
1054
- })
1055
- );
1056
-
1057
- const sabs = new SabsClient('http://localhost:3000');
1058
- const applicationId = 'test-app';
1059
- const organizationId = 'test-org';
1060
- const directoryHash = 'abc123';
1061
-
1062
- // This simulates how existing applications might handle errors
1063
- try {
1064
- const result = await sabs.build({
1065
- directoryHash,
1066
- meta: new ApplicationMetadata({
1067
- id: applicationId,
1068
- organizationId
1069
- }),
1070
- buildKey: anyBuildKey,
1071
- accessToken: anyAccessToken
1072
- });
1073
-
1074
- // Should not reach here
1075
- expect(result).toBeUndefined();
1076
- } catch (error) {
1077
- // Legacy error handling - should still work with new error types
1078
- expect(error).toBeInstanceOf(Error);
1079
- expect(error.message).toContain('SABS API Error (503)');
1080
-
1081
- // Verify we can still access basic Error properties
1082
- expect(typeof error.message).toBe('string');
1083
- expect(error.name).toBeDefined();
1084
-
1085
- // This is how existing code might log and re-throw
1086
- const loggedError = {
1087
- error,
1088
- applicationId,
1089
- organizationId,
1090
- directoryHash,
1091
- message: error.message
1092
- };
1093
-
1094
- expect(loggedError.error).toBe(error);
1095
- expect(loggedError.message).toBe(error.message);
1096
-
1097
- // Re-throwing as generic Error should work
1098
- expect(() => {
1099
- throw new Error('Unable to launch build');
1100
- }).toThrow('Unable to launch build');
1101
- }
1102
- });
1103
-
1104
- test('new error types provide additional functionality when accessed', async () => {
1105
- const mockAxios = jest.spyOn(axios, 'request');
1106
- mockAxios.mockRejectedValue(
1107
- new AxiosError('Unauthorized', 'UNAUTHORIZED', undefined, undefined, {
1108
- headers: {},
1109
- config: {
1110
- headers: new AxiosHeaders()
1111
- },
1112
- status: 401,
1113
- statusText: 'Unauthorized',
1114
- data: { message: 'Invalid token' }
1115
- })
1116
- );
1117
-
1118
- const sabs = new SabsClient('http://localhost:3000');
1119
-
1120
- try {
1121
- await sabs.status({ buildId: 'test-build', accessToken: 'invalid-token' });
1122
- } catch (error) {
1123
- // Backward compatible - can still catch as Error
1124
- expect(error).toBeInstanceOf(Error);
1125
-
1126
- // But applications can now also check for specific error types
1127
- const { UnauthorizedError } = await import('./errors');
1128
- expect(error).toBeInstanceOf(UnauthorizedError);
1129
-
1130
- // And access the status code if needed
1131
- if ('status' in error) {
1132
- expect(error.status).toBe(401);
1133
- }
1134
- }
1135
- });
1136
-
1137
- describe('response data handling', () => {
1138
- test('handles responseData with message property', async () => {
1139
- const mockAxios = jest.spyOn(axios, 'request');
1140
- mockAxios.mockRejectedValue(
1141
- new AxiosError('Bad Request', 'BAD_REQUEST', undefined, undefined, {
1142
- headers: {},
1143
- config: {
1144
- headers: new AxiosHeaders()
1145
- },
1146
- status: 400,
1147
- statusText: 'Bad Request',
1148
- data: { message: 'Directory hash is required' }
1149
- })
1150
- );
1151
-
1152
- const sabs = new SabsClient('http://localhost:3000');
1153
-
1154
- try {
1155
- await sabs.build({
1156
- directoryHash: 'test', // Use non-empty value to avoid client-side validation
1157
- meta: new ApplicationMetadata({ id: 'test', organizationId: 'test' }),
1158
- buildKey: 'test',
1159
- accessToken: 'test'
1160
- });
1161
- } catch (error) {
1162
- expect(error).toBeInstanceOf(Error);
1163
- expect(error.message).toBe('SABS API Error (400): Directory hash is required');
1164
- }
1165
- });
1166
-
1167
- test('handles responseData as string', async () => {
1168
- const mockAxios = jest.spyOn(axios, 'request');
1169
- mockAxios.mockRejectedValue(
1170
- new AxiosError('Internal Server Error', 'INTERNAL_SERVER_ERROR', undefined, undefined, {
1171
- headers: {},
1172
- config: {
1173
- headers: new AxiosHeaders()
1174
- },
1175
- status: 500,
1176
- statusText: 'Internal Server Error',
1177
- data: 'Database connection failed'
1178
- })
1179
- );
1180
-
1181
- const sabs = new SabsClient('http://localhost:3000');
1182
-
1183
- try {
1184
- await sabs.build({
1185
- directoryHash: 'test',
1186
- meta: new ApplicationMetadata({ id: 'test', organizationId: 'test' }),
1187
- buildKey: 'test',
1188
- accessToken: 'test'
1189
- });
1190
- } catch (error) {
1191
- expect(error).toBeInstanceOf(Error);
1192
- expect(error.message).toBe('SABS API Error (500): Database connection failed');
1193
- }
1194
- });
1195
-
1196
- test('handles null responseData', async () => {
1197
- const mockAxios = jest.spyOn(axios, 'request');
1198
- mockAxios.mockRejectedValue(
1199
- new AxiosError('Service Unavailable', 'SERVICE_UNAVAILABLE', undefined, undefined, {
1200
- headers: {},
1201
- config: {
1202
- headers: new AxiosHeaders()
1203
- },
1204
- status: 503,
1205
- statusText: 'Service Unavailable',
1206
- data: null
1207
- })
1208
- );
1209
-
1210
- const sabs = new SabsClient('http://localhost:3000');
1211
-
1212
- try {
1213
- await sabs.build({
1214
- directoryHash: 'test',
1215
- meta: new ApplicationMetadata({ id: 'test', organizationId: 'test' }),
1216
- buildKey: 'test',
1217
- accessToken: 'test'
1218
- });
1219
- } catch (error) {
1220
- expect(error).toBeInstanceOf(Error);
1221
- expect(error.message).toBe('SABS API Error (503): Service Unavailable (503)');
1222
- }
1223
- });
1224
-
1225
- test('handles undefined responseData', async () => {
1226
- const mockAxios = jest.spyOn(axios, 'request');
1227
- mockAxios.mockRejectedValue(
1228
- new AxiosError('Gateway Timeout', 'GATEWAY_TIMEOUT', undefined, undefined, {
1229
- headers: {},
1230
- config: {
1231
- headers: new AxiosHeaders()
1232
- },
1233
- status: 504,
1234
- statusText: 'Gateway Timeout',
1235
- data: undefined
1236
- })
1237
- );
1238
-
1239
- const sabs = new SabsClient('http://localhost:3000');
1240
-
1241
- try {
1242
- await sabs.build({
1243
- directoryHash: 'test',
1244
- meta: new ApplicationMetadata({ id: 'test', organizationId: 'test' }),
1245
- buildKey: 'test',
1246
- accessToken: 'test'
1247
- });
1248
- } catch (error) {
1249
- expect(error).toBeInstanceOf(Error);
1250
- expect(error.message).toBe('SABS API Error (504): Gateway Timeout (504)');
1251
- }
1252
- });
1253
-
1254
- test('handles responseData object without message property', async () => {
1255
- const mockAxios = jest.spyOn(axios, 'request');
1256
- mockAxios.mockRejectedValue(
1257
- new AxiosError('Bad Request', 'BAD_REQUEST', undefined, undefined, {
1258
- headers: {},
1259
- config: {
1260
- headers: new AxiosHeaders()
1261
- },
1262
- status: 400,
1263
- statusText: 'Bad Request',
1264
- data: { error: 'validation_failed', details: ['field is required'] }
1265
- })
1266
- );
1267
-
1268
- const sabs = new SabsClient('http://localhost:3000');
1269
-
1270
- try {
1271
- await sabs.build({
1272
- directoryHash: 'test',
1273
- meta: new ApplicationMetadata({ id: 'test', organizationId: 'test' }),
1274
- buildKey: 'test',
1275
- accessToken: 'test'
1276
- });
1277
- } catch (error) {
1278
- expect(error).toBeInstanceOf(Error);
1279
- expect(error.message).toBe('SABS API Error (400): Bad Request (400)');
1280
- }
1281
- });
1282
-
1283
- test('all errors have consistent HttpError interface', async () => {
1284
- const responseData = { code: 'TIMEOUT', retryAfter: 30 };
1285
- const mockAxios = jest.spyOn(axios, 'request');
1286
- mockAxios.mockRejectedValue(
1287
- new AxiosError('Request timeout', 'TIMEOUT', undefined, undefined, {
1288
- headers: {},
1289
- config: {
1290
- headers: new AxiosHeaders()
1291
- },
1292
- status: 500,
1293
- statusText: 'Internal Server Error',
1294
- data: responseData
1295
- })
1296
- );
1297
-
1298
- const sabs = new SabsClient('http://localhost:3000');
1299
-
1300
- try {
1301
- await sabs.build({
1302
- directoryHash: 'test',
1303
- meta: new ApplicationMetadata({ id: 'test', organizationId: 'test' }),
1304
- buildKey: 'test',
1305
- accessToken: 'test'
1306
- });
1307
- } catch (error) {
1308
- expect(error).toBeInstanceOf(Error);
1309
- expect(error.message).toBe('SABS API Error (500): Internal Server Error (500)');
1310
-
1311
- // All error types now have consistent HttpError interface
1312
- expect('status' in error).toBe(true);
1313
- expect('title' in error).toBe(true);
1314
- if ('status' in error) {
1315
- expect(error.status).toBe(500);
1316
- }
1317
- if ('title' in error) {
1318
- expect(error.title).toBe('Internal server error');
1319
- }
1320
- }
1321
- });
1322
- });
1323
- });
1324
- });