import { APIGatewayProxyEvent, Context } from 'aws-lambda' import { expect } from 'chai' import { Request } from 'express' import { HttpMethod } from '../../../../src/API/Request.js' import GenericHandlerEvent from '../../../../src/Server/lib/container/GenericHandlerEvent.js' describe('GenericHandlerEvent invocation paths', () => { test('Simple success', async () => { const event = new GenericHandlerEvent( ({ socket: { remoteAddress: '127.0.0.1' }, }), async (event: APIGatewayProxyEvent, context: Context) => { expect(context.getRemainingTimeInMillis()).to.not.be.null context.succeed('Success') } ) const resp = await event.invoke() expect(resp).to.not.be.null expect(resp.err).to.be.undefined expect(resp.data).to.be.equals('Success') }) test('Simple failure', async () => { const event = new GenericHandlerEvent( ({}), async (event: APIGatewayProxyEvent, context: Context) => { context.fail('Error!') } ) const resp = await event.invoke() expect(resp).to.not.be.null expect(resp.data).to.be.undefined expect(resp.err).to.be.equals('Error!') }) test('Simple done (errored)', async () => { const event = new GenericHandlerEvent( ({}), async (event: APIGatewayProxyEvent, context: Context) => { context.done(new Error('Error!')) } ) const resp = await event.invoke() expect(resp).to.not.be.null expect(resp.data).to.be.undefined expect(resp.err).to.be.an('Error') if (resp.err instanceof Error) expect(resp.err?.message).to.be.equals('Error!') }) test('Simple done (success)', async () => { const event = new GenericHandlerEvent( ({}), async (event: APIGatewayProxyEvent, context: Context) => { context.done(undefined, 'Success!') } ) const resp = await event.invoke() expect(resp).to.not.be.null expect(resp.err).to.be.undefined expect(resp.data).to.be.equals('Success!') }) test('Exception on execution', async () => { const event = new GenericHandlerEvent(({}), async () => { throw new Error('Failed!') }) let err: any = null, resp: any = null try { resp = await event.invoke() } catch (e) { err = e } expect(resp).to.be.null expect(err).to.not.be.null expect(err).to.be.an('Error') expect(err.message).to.be.equals('Failed!') }) }) describe('GenericHandlerEvent event test', () => { test('Simple event test', async () => { const event = new GenericHandlerEvent( ({ headers: { Authorization: '123', 'x-forwarded-for': '127.0.0.1' }, method: HttpMethod.GET, query: '/status?name=ryan', }), async (event: APIGatewayProxyEvent, context: Context) => { expect(context.getRemainingTimeInMillis()).to.not.be.null context.succeed('Success') } ) const resp = await event.invoke() expect(resp).to.not.be.null expect(resp.err).to.be.undefined expect(resp.data).to.be.equals('Success') }) }) export {}