Skip to content

Commit

Permalink
fix for invalid messages
Browse files Browse the repository at this point in the history
  • Loading branch information
philipparndt committed Jan 19, 2022
1 parent 4cdc2eb commit b54f0bf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 14 deletions.
45 changes: 31 additions & 14 deletions app/lib/logger.test.ts
Original file line number Diff line number Diff line change
@@ -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.*/)
})
})
1 change: 1 addition & 0 deletions app/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export const createLogger = (transport: Transport = new winston.transports.Conso
}

export const log = createLogger()

21 changes: 21 additions & 0 deletions app/lib/put/put-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]

Expand All @@ -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 = []

Expand Down Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions app/lib/put/put-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -23,5 +24,8 @@ export const putMessage = async (resource: HueIdentifiable, message: Buffer) =>
log.error(e)
}
}
} catch (e) {
log.error("invalid message", e)
}
}
}

0 comments on commit b54f0bf

Please sign in to comment.