diff --git a/src/routes/health/index.ts b/src/routes/health/index.ts new file mode 100644 index 0000000..27ca0bb --- /dev/null +++ b/src/routes/health/index.ts @@ -0,0 +1,23 @@ +import { ResponseObject, ResponseToolkit, ServerRoute } from '@hapi/hapi'; +import { Config } from '../../config'; + +export const routes = (config: Config): ServerRoute[] => { + return [ + { + method: 'GET', + options: { + handler: (_: Request, h: ResponseToolkit): ResponseObject => { + const isTwitchConfigOk = + !!config.TWITCH_BOT_CHANNEL && !!config.TWITCH_BOT_NAME && !!config.TWITCH_BOT_TOKEN; + + const isStreamLabsConfigOk = !!config.STREAMLABS_TOKEN; + + return h + .response({ config: { twitch: isTwitchConfigOk, streamLabs: isStreamLabsConfigOk } }) + .code(200); + }, + }, + path: '/health', + }, + ]; +}; diff --git a/src/routes/index.ts b/src/routes/index.ts index e98fd73..fa3d6f3 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -1,8 +1,9 @@ import { ServerRoute } from '@hapi/hapi'; import { routes as gitlab } from './gitlab'; import { routes as github } from './github'; +import { routes as health } from './health'; import { Config } from '../config'; export const routes = (config: Config): ServerRoute[] => { - return [...github(config), ...gitlab(config)]; + return [...github(config), ...gitlab(config), ...health(config)]; }; diff --git a/test/routes/health/index.spec.ts b/test/routes/health/index.spec.ts new file mode 100644 index 0000000..758d63e --- /dev/null +++ b/test/routes/health/index.spec.ts @@ -0,0 +1,75 @@ +import { ServerInjectOptions } from '@hapi/hapi'; +import { getConfig } from '../../../src/config'; +import { initServer } from '../../../src/server'; + +describe('/health', () => { + describe('GET /health', () => { + it('returns 200 OK if everything is well configured', async () => { + const subject = await initServer(getConfig()); + const request: ServerInjectOptions = { method: 'GET', url: '/health' }; + + const { statusCode } = await subject.inject(request); + + expect(statusCode).toEqual(200); + }); + + describe('Twitch Config', () => { + it('returns that the Twitch Bot is configured', async () => { + const subject = await initServer({ + ...getConfig(), + TWITCH_BOT_CHANNEL: 'streamdevs', + TWITCH_BOT_NAME: 'streamdevs', + TWITCH_BOT_TOKEN: 'token', + }); + const request: ServerInjectOptions = { method: 'GET', url: '/health' }; + + const { result } = await subject.inject(request); + + expect(result).toEqual( + expect.objectContaining({ config: expect.objectContaining({ twitch: true }) }), + ); + }); + + it("returns that the Twitch Bot isn't configured", async () => { + const subject = await initServer({ ...getConfig(), TWITCH_BOT_CHANNEL: undefined }); + const request: ServerInjectOptions = { method: 'GET', url: '/health' }; + + const { result } = await subject.inject(request); + + expect(result).toEqual( + expect.objectContaining({ config: expect.objectContaining({ twitch: false }) }), + ); + }); + }); + + describe('StreamLabs Config', () => { + it('returns that StreamLabs is configured', async () => { + const subject = await initServer({ + ...getConfig(), + STREAMLABS_TOKEN: 'token', + }); + const request: ServerInjectOptions = { method: 'GET', url: '/health' }; + + const { result } = await subject.inject(request); + + expect(result).toEqual( + expect.objectContaining({ config: expect.objectContaining({ streamLabs: true }) }), + ); + }); + + it('returns that StreamLabs is configured', async () => { + const subject = await initServer({ + ...getConfig(), + STREAMLABS_TOKEN: undefined, + }); + const request: ServerInjectOptions = { method: 'GET', url: '/health' }; + + const { result } = await subject.inject(request); + + expect(result).toEqual( + expect.objectContaining({ config: expect.objectContaining({ streamLabs: false }) }), + ); + }); + }); + }); +});