diff --git a/app/lib/logger.test.ts b/app/lib/logger.test.ts index a81b75bd..eff6d608 100755 --- a/app/lib/logger.test.ts +++ b/app/lib/logger.test.ts @@ -1,41 +1,58 @@ import { createLogger, log } from "./logger" import { Writable } from "stream" import * as winston from "winston" +import * as Transport from "winston-transport" -describe("Log format", () => { - let output = "" - let logger: winston.Logger - beforeEach(() => { - log.silent = true +export class TestLogger { + output = "" + transports: Transport[] + constructor () { const stream = new Writable() stream._write = (chunk, encoding, next) => { - output = output += chunk.toString() + this.output = this.output += chunk.toString() next() } - logger = createLogger(new winston.transports.Stream({ stream })) + this.transports = [...log.transports] + log.clear() + log.add(new winston.transports.Stream({ stream })) + } + + close = () => { + log.clear() + for (let transport of this.transports) { + log.add(transport) + } + } +} + +describe("Log format", () => { + let logger: TestLogger + + beforeAll(() => { + logger = new TestLogger() }) afterEach(() => { - output = "" + logger.output = "" }) test("info log", () => { - logger.info("some info") + log.info("some info") - expect(output).toMatch(/\d+\d+\d+\d+-\d+\d+-\d+\d+T.* INFO some info.*/) + expect(logger.output).toMatch(/\d+\d+\d+\d+-\d+\d+-\d+\d+T.* INFO some info.*/) }) test("warn log", () => { - logger.warn("some warning") + log.warn("some warning") - expect(output).toMatch(/\d+\d+\d+\d+-\d+\d+-\d+\d+T.* WARN some warning.*/) + expect(logger.output).toMatch(/\d+\d+\d+\d+-\d+\d+-\d+\d+T.* WARN some warning.*/) }) test("error log", () => { - logger.error("some error") + log.error("some error") - expect(output).toMatch(/\d+\d+\d+\d+-\d+\d+-\d+\d+T.* ERROR some error.*/) + expect(logger.output).toMatch(/\d+\d+\d+\d+-\d+\d+-\d+\d+T.* ERROR some error.*/) }) }) diff --git a/app/lib/logger.ts b/app/lib/logger.ts index 6ae05b98..4ff0c50e 100755 --- a/app/lib/logger.ts +++ b/app/lib/logger.ts @@ -12,3 +12,4 @@ export const createLogger = (transport: Transport = new winston.transports.Conso } export const log = createLogger() + diff --git a/app/lib/put/put-handler.test.ts b/app/lib/put/put-handler.test.ts index 4a2dfeb5..6cb644cc 100644 --- a/app/lib/put/put-handler.test.ts +++ b/app/lib/put/put-handler.test.ts @@ -3,6 +3,7 @@ import { deviceStubs } from "../api/v2/device-stubs" import { applyDefaults, setTestConfig } from "../config/config" import { LightEffectMessage, LightMessage } from "../messages/light-message" import { putMessage } from "./put-handler" +import { TestLogger } from "../logger.test" let messages: any[] @@ -13,6 +14,16 @@ jest.spyOn(api, "putLight").mockImplementation((x, message) => { }) describe("PUT handler", () => { + let logger: TestLogger + + beforeAll(() => { + logger = new TestLogger() + }) + + afterEach(() => { + logger.output = "" + }) + beforeEach(() => { messages = [] @@ -61,6 +72,16 @@ describe("PUT handler", () => { ]) }) + test("PUT invalid message", async () => { + const message = "invalid-message" + + await putMessage(deviceStubs.lightWithColor, Buffer.from(message)) + + expect(messages.length).toBe(0) + expect(logger.output).toMatch(/\d+\d+\d+\d+-\d+\d+-\d+\d+T.* ERROR invalid message Unexpected token i in JSON at position 0.*/) + + }) + test("Turn on", async () => { const msg: LightMessage = { state: "ON", diff --git a/app/lib/put/put-handler.ts b/app/lib/put/put-handler.ts index 5c150c4d..982d324d 100644 --- a/app/lib/put/put-handler.ts +++ b/app/lib/put/put-handler.ts @@ -8,6 +8,7 @@ import { applyEffect } from "./effects/light-effect-handler" export const putMessage = async (resource: HueIdentifiable, message: Buffer) => { if (isLight(resource)) { // only supported for light messages at the moment + try { const lightMsg = JSON.parse(message.toString()) as LightMessage | LightEffectMessage if (isEffectMessage(lightMsg)) { await applyEffect(resource, lightMsg) @@ -23,5 +24,8 @@ export const putMessage = async (resource: HueIdentifiable, message: Buffer) => log.error(e) } } + } catch (e) { + log.error("invalid message", e) + } } }