|
1 | 1 | import BaseClass from './base-class'; |
2 | 2 | import { cliux as ux, ContentstackClient } from '@contentstack/cli-utilities'; |
3 | 3 | import config from '../config'; |
| 4 | +import { FILE_UPLOAD_SIZE_LIMIT_USER_MESSAGE } from '../util/deployment-errors'; |
4 | 5 |
|
5 | 6 | jest.mock('@contentstack/cli-utilities', () => ({ |
6 | 7 | cliux: { |
@@ -546,4 +547,146 @@ describe('BaseClass', () => { |
546 | 547 | ]); |
547 | 548 | }); |
548 | 549 | }); |
| 550 | + |
| 551 | + describe('createNewDeployment', () => { |
| 552 | + let mutateMock: jest.Mock; |
| 553 | + |
| 554 | + beforeEach(() => { |
| 555 | + mutateMock = jest.fn(); |
| 556 | + baseClass = new BaseClass({ |
| 557 | + log: logMock, |
| 558 | + exit: exitMock, |
| 559 | + apolloClient: { mutate: mutateMock } as any, |
| 560 | + config: { |
| 561 | + currentConfig: { deployments: [] }, |
| 562 | + }, |
| 563 | + } as any); |
| 564 | + }); |
| 565 | + |
| 566 | + it('should log success and append deployment when mutate succeeds', async () => { |
| 567 | + const deployment = { uid: 'dep-1', status: 'PENDING' }; |
| 568 | + mutateMock.mockResolvedValueOnce({ data: { deployment } }); |
| 569 | + |
| 570 | + await baseClass.createNewDeployment(false, 'env-uid-1'); |
| 571 | + |
| 572 | + expect(mutateMock).toHaveBeenCalled(); |
| 573 | + expect(logMock).toHaveBeenCalledWith('Deployment process started.!', 'info'); |
| 574 | + expect(baseClass.config.currentConfig.deployments).toEqual([deployment]); |
| 575 | + expect(exitMock).not.toHaveBeenCalled(); |
| 576 | + }); |
| 577 | + |
| 578 | + it('should log file size limit message and exit when mutate fails with deployment file size error', async () => { |
| 579 | + const apolloError = { |
| 580 | + graphQLErrors: [ |
| 581 | + { |
| 582 | + extensions: { |
| 583 | + exception: { |
| 584 | + messages: ['launch.DEPLOYMENT.INVALID_FILE_SIZE'], |
| 585 | + }, |
| 586 | + }, |
| 587 | + }, |
| 588 | + ], |
| 589 | + }; |
| 590 | + mutateMock.mockRejectedValueOnce(apolloError); |
| 591 | + |
| 592 | + await baseClass.createNewDeployment(true, 'env-uid-2', 'upload-uid-1'); |
| 593 | + |
| 594 | + expect(logMock).toHaveBeenCalledWith('Deployment process failed.!', 'error'); |
| 595 | + expect(logMock).toHaveBeenCalledWith(apolloError, 'debug'); |
| 596 | + expect(logMock).toHaveBeenCalledWith(FILE_UPLOAD_SIZE_LIMIT_USER_MESSAGE, 'error'); |
| 597 | + expect(exitMock).toHaveBeenCalledWith(1); |
| 598 | + expect(logMock).not.toHaveBeenCalledWith(apolloError, 'error'); |
| 599 | + }); |
| 600 | + |
| 601 | + it('should log raw error and exit when mutate fails with a non file size error', async () => { |
| 602 | + const otherError = new Error('GraphQL failure'); |
| 603 | + mutateMock.mockRejectedValueOnce(otherError); |
| 604 | + |
| 605 | + await baseClass.createNewDeployment(false, 'env-uid-3'); |
| 606 | + |
| 607 | + expect(logMock).toHaveBeenCalledWith('Deployment process failed.!', 'error'); |
| 608 | + expect(logMock).toHaveBeenCalledWith(otherError, 'error'); |
| 609 | + expect(logMock).not.toHaveBeenCalledWith(FILE_UPLOAD_SIZE_LIMIT_USER_MESSAGE, 'error'); |
| 610 | + expect(exitMock).toHaveBeenCalledWith(1); |
| 611 | + }); |
| 612 | + }); |
| 613 | + |
| 614 | + describe('handleNewProjectCreationError', () => { |
| 615 | + beforeEach(() => { |
| 616 | + baseClass = new BaseClass({ |
| 617 | + log: logMock, |
| 618 | + exit: exitMock, |
| 619 | + config: { |
| 620 | + projectCreationRetryMaxCount: 3, |
| 621 | + }, |
| 622 | + } as any); |
| 623 | + }); |
| 624 | + |
| 625 | + it('should log file size limit message and exit when error is launch.DEPLOYMENT.INVALID_FILE_SIZE', async () => { |
| 626 | + const apolloError = { |
| 627 | + graphQLErrors: [ |
| 628 | + { |
| 629 | + extensions: { |
| 630 | + exception: { |
| 631 | + messages: ['launch.DEPLOYMENT.INVALID_FILE_SIZE'], |
| 632 | + }, |
| 633 | + }, |
| 634 | + }, |
| 635 | + ], |
| 636 | + }; |
| 637 | + |
| 638 | + await baseClass.handleNewProjectCreationError(apolloError); |
| 639 | + |
| 640 | + expect(logMock).toHaveBeenCalledWith('New project creation failed!', 'error'); |
| 641 | + expect(logMock).toHaveBeenCalledWith(apolloError, 'debug'); |
| 642 | + expect(logMock).toHaveBeenCalledWith(FILE_UPLOAD_SIZE_LIMIT_USER_MESSAGE, 'error'); |
| 643 | + expect(exitMock).toHaveBeenCalledWith(1); |
| 644 | + expect(logMock).not.toHaveBeenCalledWith(apolloError, 'error'); |
| 645 | + }); |
| 646 | + |
| 647 | + it('should log file size limit message and exit when error is launch.DEPLOYMENT.FILE_UPLOAD_FAILED in errorObject', async () => { |
| 648 | + const apolloError = { |
| 649 | + graphQLErrors: [ |
| 650 | + { |
| 651 | + extensions: { |
| 652 | + exception: { |
| 653 | + errorObject: { |
| 654 | + uploadUid: [{ code: 'launch.DEPLOYMENT.FILE_UPLOAD_FAILED' }], |
| 655 | + }, |
| 656 | + }, |
| 657 | + }, |
| 658 | + }, |
| 659 | + ], |
| 660 | + }; |
| 661 | + |
| 662 | + await baseClass.handleNewProjectCreationError(apolloError); |
| 663 | + |
| 664 | + expect(logMock).toHaveBeenCalledWith('New project creation failed!', 'error'); |
| 665 | + expect(logMock).toHaveBeenCalledWith(apolloError, 'debug'); |
| 666 | + expect(logMock).toHaveBeenCalledWith(FILE_UPLOAD_SIZE_LIMIT_USER_MESSAGE, 'error'); |
| 667 | + expect(exitMock).toHaveBeenCalledWith(1); |
| 668 | + expect(logMock).not.toHaveBeenCalledWith(apolloError, 'error'); |
| 669 | + }); |
| 670 | + |
| 671 | + it('should log raw error and exit when error is not a known handled case', async () => { |
| 672 | + const apolloError = { |
| 673 | + graphQLErrors: [ |
| 674 | + { |
| 675 | + extensions: { |
| 676 | + exception: { |
| 677 | + messages: ['launch.PROJECTS.UPLOADED_FILE_NOT_FOUND_ERROR'], |
| 678 | + }, |
| 679 | + }, |
| 680 | + }, |
| 681 | + ], |
| 682 | + }; |
| 683 | + |
| 684 | + await baseClass.handleNewProjectCreationError(apolloError); |
| 685 | + |
| 686 | + expect(logMock).toHaveBeenCalledWith('New project creation failed!', 'error'); |
| 687 | + expect(logMock).toHaveBeenCalledWith(apolloError, 'error'); |
| 688 | + expect(logMock).not.toHaveBeenCalledWith(FILE_UPLOAD_SIZE_LIMIT_USER_MESSAGE, 'error'); |
| 689 | + expect(exitMock).toHaveBeenCalledWith(1); |
| 690 | + }); |
| 691 | + }); |
549 | 692 | }); |
0 commit comments