import { ForgeInvocationError, ForgeInvocationErrorOptions, InvocationStatusCode, } from '@atlassian/forge-graphql-types'; import { ForgeInvocationErrorResponse, ForgeInvocationErrorResponseBuilderError, } from '../../builders/forge-invocation-error-response'; const ERROR_MESSAGE = 'error message'; describe('ForgeInvocationErrorResponse class tests', () => { it('returns valid ForgeInvocationError', () => { const forgeInvocationErrorResponse = new ForgeInvocationErrorResponse( ERROR_MESSAGE, InvocationStatusCode.BAD_REQUEST, { backoffTimeInSeconds: 0 }, ).build(); const expectedResult: ForgeInvocationError = { error: ERROR_MESSAGE, statusCode: InvocationStatusCode.BAD_REQUEST, options: { backoffTimeInSeconds: 0, }, }; expect(forgeInvocationErrorResponse).toEqual(expectedResult); }); it('throws error if provided invalid error', () => { const errorOfInvalidType = 12345; const errorResponseBuilder = new ForgeInvocationErrorResponse( errorOfInvalidType as unknown as string, InvocationStatusCode.BAD_REQUEST, { backoffTimeInSeconds: 0 }, ); const expectedError = new ForgeInvocationErrorResponseBuilderError( 'Error must be a string.', ); expect(() => { errorResponseBuilder.build(); }).toThrow(expectedError); }); it('throws error if provided empty error', () => { const emptyError = ''; const errorResponseBuilder = new ForgeInvocationErrorResponse( emptyError, InvocationStatusCode.BAD_REQUEST, { backoffTimeInSeconds: 0 }, ); const expectedError = new ForgeInvocationErrorResponseBuilderError( 'Error must be a string.', ); expect(() => { errorResponseBuilder.build(); }).toThrow(expectedError); }); it('returns 500 status if provided unsupported status code', () => { const unsupportedStatusCode = 405; const forgeInvocationErrorResponse = new ForgeInvocationErrorResponse( ERROR_MESSAGE, unsupportedStatusCode as InvocationStatusCode, { backoffTimeInSeconds: 0 }, ).build(); const expectedResult: ForgeInvocationError = { error: ERROR_MESSAGE, statusCode: InvocationStatusCode.INTERNAL_SERVER_ERROR, options: { backoffTimeInSeconds: 0, }, }; expect(forgeInvocationErrorResponse).toEqual(expectedResult); }); it('throws error if options are not provided', () => { const params = [ ERROR_MESSAGE, InvocationStatusCode.BAD_REQUEST, ] as unknown as [string, InvocationStatusCode, ForgeInvocationErrorOptions]; const errorResponseBuilder = new ForgeInvocationErrorResponse(...params); const expectedError = new ForgeInvocationErrorResponseBuilderError( 'Options object is not provided.', ); expect(() => { errorResponseBuilder.build(); }).toThrow(expectedError); }); it('throws error if provided unsupported option', () => { const unsupportedOption = 'unsupportedOption'; const options = { backoffTimeInSeconds: 0, [unsupportedOption]: true }; const errorResponseBuilder = new ForgeInvocationErrorResponse( ERROR_MESSAGE, InvocationStatusCode.BAD_REQUEST, options, ); const expectedError = new ForgeInvocationErrorResponseBuilderError( `${unsupportedOption} is not a valid option.`, ); expect(() => { errorResponseBuilder.build(); }).toThrow(expectedError); }); it('throws error if provided option has invalid value type', () => { const backoffTimeInSeconds = 'backoffTimeInSeconds'; const errorResponseBuilder = new ForgeInvocationErrorResponse( ERROR_MESSAGE, InvocationStatusCode.BAD_REQUEST, { [backoffTimeInSeconds]: 'invalid type' as unknown as number }, ); const expectedError = new ForgeInvocationErrorResponseBuilderError( `${backoffTimeInSeconds} has invalid value.`, ); expect(() => { errorResponseBuilder.build(); }).toThrow(expectedError); }); it('throws error if provided option has invalid value', () => { const backoffTimeInSeconds = 'backoffTimeInSeconds'; const errorResponseBuilder = new ForgeInvocationErrorResponse( ERROR_MESSAGE, InvocationStatusCode.BAD_REQUEST, { [backoffTimeInSeconds]: -1 }, ); const expectedError = new ForgeInvocationErrorResponseBuilderError( `${backoffTimeInSeconds} has invalid value.`, ); expect(() => { errorResponseBuilder.build(); }).toThrow(expectedError); }); });