|
1 | 1 | import { describe, it, expect, beforeEach, vi } from 'vitest'; |
2 | 2 | import { http, HttpResponse } from 'msw'; |
3 | 3 | import { PlatformSDKHttp } from './platform-sdk.js'; |
4 | | -import { AuthenticationError } from './errors.js'; |
| 4 | +import { APIError, AuthenticationError, UnexpectedError } from './errors.js'; |
5 | 5 | import { setMockScenario, server } from './test-utils/http-mocks.js'; |
6 | 6 | import { ItemState, ItemTerminationReason } from './generated/api.js'; |
| 7 | +import { PublicApi } from './generated/index.js'; |
7 | 8 |
|
8 | 9 | describe('PlatformSDK', () => { |
9 | 10 | let sdk: PlatformSDKHttp; |
@@ -601,4 +602,50 @@ describe('PlatformSDK', () => { |
601 | 602 | ); |
602 | 603 | expect(callCount).toBe(1); |
603 | 604 | }); |
| 605 | + |
| 606 | + it('should throw APIError with 422 status when validation error occurs', async () => { |
| 607 | + mockTokenProvider.mockResolvedValue('mocked-token'); |
| 608 | + setMockScenario('validationError'); |
| 609 | + |
| 610 | + await expect(sdk.listApplications()).rejects.toThrow(APIError); |
| 611 | + await expect(sdk.listApplications()).rejects.toThrow('Validation error:'); |
| 612 | + }); |
| 613 | + |
| 614 | + it('should throw APIError with 403 status when access is forbidden', async () => { |
| 615 | + mockTokenProvider.mockResolvedValue('mocked-token'); |
| 616 | + |
| 617 | + server.use( |
| 618 | + http.get('*/v1/applications', () => { |
| 619 | + return HttpResponse.json({ detail: 'Forbidden' }, { status: 403 }); |
| 620 | + }) |
| 621 | + ); |
| 622 | + |
| 623 | + await expect(sdk.listApplications()).rejects.toThrow(APIError); |
| 624 | + await expect(sdk.listApplications()).rejects.toThrow('Access forbidden:'); |
| 625 | + }); |
| 626 | + |
| 627 | + it('should throw APIError with 410 status when resource is gone', async () => { |
| 628 | + mockTokenProvider.mockResolvedValue('mocked-token'); |
| 629 | + |
| 630 | + server.use( |
| 631 | + http.get('*/v1/runs/:runId', () => { |
| 632 | + return HttpResponse.json({ detail: 'Gone' }, { status: 410 }); |
| 633 | + }) |
| 634 | + ); |
| 635 | + |
| 636 | + await expect(sdk.getRun('test-run-id')).rejects.toThrow(APIError); |
| 637 | + await expect(sdk.getRun('test-run-id')).rejects.toThrow('Resource gone:'); |
| 638 | + }); |
| 639 | + |
| 640 | + it('should throw UnexpectedError for non-axios errors', async () => { |
| 641 | + mockTokenProvider.mockResolvedValue('mocked-token'); |
| 642 | + |
| 643 | + vi.spyOn(PublicApi.prototype, 'listApplicationsV1ApplicationsGet').mockRejectedValueOnce( |
| 644 | + new RangeError('unexpected internal failure') |
| 645 | + ); |
| 646 | + |
| 647 | + const error = await sdk.listApplications().catch((e) => e); |
| 648 | + expect(error).toBeInstanceOf(UnexpectedError); |
| 649 | + expect(error.message).toContain('Unexpected error:'); |
| 650 | + }); |
604 | 651 | }); |
0 commit comments