import { jest } from '@jest/globals' import { APIGatewayProxyEvent, Context } from 'aws-lambda' import { expect as c_expect } from 'chai' import { Request, Response } from 'express' import Globals from '../../../../src/Globals.js' import GenericHandler from '../../../../src/Server/lib/container/GenericHandler.js' function observableResponse(): Response { const resp = ({ send: jest.fn(() => resp), json: jest.fn(() => resp), status: jest.fn(() => resp), header: jest.fn(() => resp), }) return resp } describe('GenericHandler success invocation path', () => { test('Simple success', async () => { const v = { statusCode: 200, body: JSON.stringify({ id: 123 }), } const handler = GenericHandler(async (event: APIGatewayProxyEvent, context: Context) => { context.succeed(v) }) const resp = observableResponse() const handlerResp = await handler(({}), resp) // c_expect(handlerResp).to.be.undefined expect(resp.send).not.toBeCalled() expect(resp.json).toBeCalledWith(JSON.parse(v.body)) expect(resp.status).toBeCalledWith(v.statusCode) expect(resp.header).not.toBeCalled() }) test('Simple success w/ headers', async () => { const v = { statusCode: 200, body: JSON.stringify({ id: 123 }), headers: { Authorization: 123 }, } const handler = GenericHandler(async (event: APIGatewayProxyEvent, context: Context) => { context.succeed(v) }) const resp = observableResponse() const handlerResp = await handler(({}), resp) // c_expect(handlerResp).to.be.undefined expect(resp.send).not.toBeCalled() expect(resp.json).toBeCalledWith(JSON.parse(v.body)) expect(resp.status).toBeCalledWith(v.statusCode) expect(resp.header).toBeCalledTimes(Object.keys(v.headers).length) }) test('Simple success w/ null body', async () => { const v = { statusCode: 200, headers: { Authorization: 123 }, } const handler = GenericHandler(async (event: APIGatewayProxyEvent, context: Context) => { context.succeed(v) }) const resp = observableResponse() const handlerResp = await handler(({}), resp) // c_expect(handlerResp).to.be.undefined expect(resp.send).not.toBeCalled() expect(resp.json).toBeCalledWith({}) expect(resp.status).toBeCalledWith(v.statusCode) expect(resp.header).toBeCalledTimes(Object.keys(v.headers).length) }) }) describe('GenericHandler failure invocation path', () => { test('Invalid response', async () => { const handler = GenericHandler(async (event: APIGatewayProxyEvent, context: Context) => { context.succeed(null) }) const resp = observableResponse() const handlerResp = await handler(({}), resp) // c_expect(handlerResp).to.be.undefined expect(resp.send).not.toBeCalled() expect(resp.header).not.toBeCalled() expect(resp.json).toBeCalledWith({ err: Globals.Resp_MSG_INVALIDRESP, errCode: Globals.Resp_CODE_INVALIDRESP, }) expect(resp.status).toBeCalledWith(Globals.Resp_STATUSCODE_INVALIDRESP) }) test('Error response', async () => { const err = new Error('Failed!') const handler = GenericHandler(async (event: APIGatewayProxyEvent, context: Context) => { context.fail(err) }) const resp = observableResponse() const handlerResp = await handler(({}), resp) // c_expect(handlerResp).to.be.undefined expect(resp.send).not.toBeCalled() expect(resp.header).not.toBeCalled() expect(resp.json).toBeCalledWith({ err, }) expect(resp.status).toBeCalledWith(400) }) test('Exception response', async () => { const err = new Error('Failed!') const handler = GenericHandler(async () => { throw err }) const resp = observableResponse() const handlerResp = await handler(({}), resp) // c_expect(handlerResp).to.be.undefined expect(resp.send).not.toBeCalled() expect(resp.header).not.toBeCalled() expect(resp.json).toBeCalledWith({ message: err.message, stack: err.stack, err: Globals.Resp_MSG_EXCEPTION, errCode: Globals.Resp_CODE_EXCEPTION, }) expect(resp.status).toBeCalledWith(Globals.Resp_STATUSCODE_EXCEPTION) }) }) export {}