UNPKG

log-vault

Version:

A generator of Winston logger instance with pre-defined configurable transports and formats and extra functionality.

237 lines (236 loc) 8.24 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const body_parser_1 = __importDefault(require("body-parser")); const express_1 = __importDefault(require("express")); const _1 = require("."); const waitForProcess_1 = require("./test-files/util/waitForProcess"); const wait_1 = require("./test-files/util/wait"); const redisCleanup_1 = require("./test-files/util/redisCleanup"); const testToken = "testToken"; const testChatId = 1; describe("e2e tests: LogVault with Notificator", () => { let tgRequestBody; let mockServer; const mockPort = 7625; let notificator; let logger; let logVault; let timestamp; beforeAll(async () => { await startMockServer(); }); afterAll(async () => { await mockServer.close(); }); beforeEach(async () => { await (0, redisCleanup_1.redisCleanup)(`${testToken}.${testChatId}`); }); afterEach(async () => { tgRequestBody = undefined; notificator.stop(); jest.clearAllMocks(); await (0, redisCleanup_1.redisCleanup)(`${testToken}.${testChatId}`); }); it("e2e:send notification to Telegram, matched by level", async () => { initTest({ matchPatterns: [{ level: "http" }] }); logger.http("something"); await waitForProcessTest(); expect(tgRequestBody).toEqual({ chat_id: 1, text: `🔵 *http log message* ⏱ _${timestamp}_\n` + "\n" + "`[project]: log\\-vault`\n" + "`[process]: log\\-vault`\n" + "`[environment]: test`\n" + "\n" + "```json\n" + '"something"\n' + "```", parse_mode: "MarkdownV2" }); }); it("e2e:send notification to Telegram, mismatch by level", async () => { initTest({ matchPatterns: [{ level: "http" }] }); logger.info("should not be notified"); await (0, wait_1.wait)(350); expect(tgRequestBody).toBe(undefined); }); it("e2e:send notification to Telegram, matched by message, nested", async () => { initTest({ matchPatterns: [{ match: { message: /error/gi } }] }); logger.http({ message: "Request failed", details: { request: { headers: { header1: "header data" }, body: { some: "data" } }, response: { responseData: "Error! Wrong request" } } }); await waitForProcessTest(); expect(tgRequestBody).toEqual({ chat_id: 1, text: `🔵 *http log message* ⏱ _${timestamp}_\n` + "\n" + "`[project]: log\\-vault`\n" + "`[process]: log\\-vault`\n" + "`[environment]: test`\n" + "\n" + "```json\n" + "\\[\n" + ' "Request failed",\n' + " \\{\n" + ' "details": \\{\n' + ' "request": \\{\n' + ' "headers": \\{\n' + ' "header1": "header data"\n' + " \\},\n" + ' "body": \\{\n' + ' "some": "data"\n' + " \\}\n" + " \\},\n" + ' "response": \\{\n' + ' "responseData": "Error\\! Wrong request"\n' + " \\}\n" + " \\}\n" + " \\}\n" + "\\]\n" + "```", parse_mode: "MarkdownV2" }); }); it("e2e:send notification to Telegram, matched by message, nested, with exclusion pattern", async () => { initTest({ matchPatterns: [ { level: "http", match: { message: /error/gi }, exclude: { message: /bot\sping/gi } } ] }); logger.http({ message: "Request failed", details: { request: { headers: { header1: "header data" }, body: { some: "data" } }, response: { responseData: "Error! Wrong request" } } }); await waitForProcessTest(); expect(tgRequestBody).toEqual({ chat_id: 1, text: `🔵 *http log message* ⏱ _${timestamp}_\n` + "\n" + "`[project]: log\\-vault`\n" + "`[process]: log\\-vault`\n" + "`[environment]: test`\n" + "\n" + "```json\n" + "\\[\n" + ' "Request failed",\n' + " \\{\n" + ' "details": \\{\n' + ' "request": \\{\n' + ' "headers": \\{\n' + ' "header1": "header data"\n' + " \\},\n" + ' "body": \\{\n' + ' "some": "data"\n' + " \\}\n" + " \\},\n" + ' "response": \\{\n' + ' "responseData": "Error\\! Wrong request"\n' + " \\}\n" + " \\}\n" + " \\}\n" + "\\]\n" + "```", parse_mode: "MarkdownV2" }); }); it("e2e:send notification to Telegram, matched by message, nested, with exclusion pattern, exclusion matched", async () => { initTest({ matchPatterns: [ { match: { message: /error/gi }, exclude: { message: /bot\sping/gi } } ] }); logger.http({ message: "Request failed", details: { request: { headers: { header1: "header data", identification: "Bot ping" }, body: { some: "data" } }, response: { responseData: "Error! Wrong request" } } }); await (0, wait_1.wait)(350); expect(tgRequestBody).toBe(undefined); }); function initTest(opts) { const { matchPatterns = [] } = opts; notificator = new _1.Notificator({ workerOpts: { limiter: { max: 1, duration: 30 } } }); notificator.add(new _1.TelegramNotificationChannel({ host: `http://localhost:${mockPort}`, token: testToken, chatId: testChatId, matchPatterns, workerOptions: { limiter: { max: 1, duration: 10 } } })); logVault = new _1.LogVault().withNotifications(); logger = logVault.logger; } function startMockServer() { return new Promise((resolve) => { const app = (0, express_1.default)(); app.use(body_parser_1.default.json()); app.post("/*/sendMessage", (req, res) => { tgRequestBody = req.body; return res.status(200).end(); }); mockServer = app.listen(mockPort, () => resolve(true)); }); } async function waitForProcessTest() { const processed = await (0, waitForProcess_1.waitForProcess)(`${testToken}.${testChatId}`); timestamp = processed.timestamp.replace(/([|{[\]*_~}+)(#>!=\-.])/gm, "\\$1"); return processed; } });