From f2c15da81d8979aef2bd92392e6f0ffac235f9ed Mon Sep 17 00:00:00 2001 From: fraxken Date: Sat, 13 Apr 2024 12:55:47 +0200 Subject: [PATCH] chore: add more UT for src/notifiers --- src/notifiers/src/webhook.ts | 5 ++- src/notifiers/test/webhook.spec.ts | 72 +++++++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 8 deletions(-) diff --git a/src/notifiers/src/webhook.ts b/src/notifiers/src/webhook.ts index a2e729c..cf4006a 100644 --- a/src/notifiers/src/webhook.ts +++ b/src/notifiers/src/webhook.ts @@ -50,7 +50,7 @@ export class WebhookNotifier { }; constructor(options: WebhookNotifierOptions) { - this.webhookUrl = JSON.parse(JSON.stringify((options.webhookUrl))); + this.webhookUrl = options.webhookUrl; this.data = JSON.parse(JSON.stringify((options.data))); this.template = JSON.parse(JSON.stringify((options.template))); @@ -76,7 +76,8 @@ export class WebhookNotifier { } if (this.showSeverityEmoji && this.template.title) { - this.template.title = `${kSeverityEmoji[this.data.severity]} ${this.template.title}`; + const emoji = kSeverityEmoji[this.data.severity]; + this.template.title = `${emoji} ${this.template.title}`; } return morphix(this.template.title, this.data, this.titleTemplateOptions()); diff --git a/src/notifiers/test/webhook.spec.ts b/src/notifiers/test/webhook.spec.ts index d9f075f..198d6ab 100644 --- a/src/notifiers/test/webhook.spec.ts +++ b/src/notifiers/test/webhook.spec.ts @@ -1,6 +1,6 @@ // Import Node.js Dependencies import assert from "node:assert"; -import { describe, it, after, before } from "node:test"; +import { describe, it, after, before, test } from "node:test"; // Import Third-party Dependencies import { MockAgent, MockPool, getGlobalDispatcher, setGlobalDispatcher } from "@myunisoft/httpie"; @@ -8,15 +8,19 @@ import { MockAgent, MockPool, getGlobalDispatcher, setGlobalDispatcher } from "@ // Import Internal Dependencies import { WebhookNotifier } from "../src/webhook"; +// CONSTANTS const kMockAgent = new MockAgent(); const kDispatcher = getGlobalDispatcher(); const kDummyWebhoobURL = "https://webhook.test"; -class DummyWebhookNotifier extends WebhookNotifier<{ foo: string }> { - async formatWebhookBody() { - return { foo: "bar" }; +const kDummyNotifierOptions = { + webhookUrl: "https://webhook.test/dummy", + data: { + severity: "critical", + labelCount: 0, + labelMatchCount: 1 } -} +} as const; describe("Webhook", () => { let pool: MockPool; @@ -32,7 +36,57 @@ describe("Webhook", () => { setGlobalDispatcher(kDispatcher); }); - it("should send a webhook", async() => { + describe("formatTitle", () => { + it("should format title using severity variable that we capitalize using morphix", async() => { + const notifier = new DummyWebhookNotifier({ + ...kDummyNotifierOptions, + template: { + title: "foobar {severity | capitalize}", + content: [] + } + }); + + const title = await notifier.formatTitle(); + assert.strictEqual(title, "💥 foobar Critical"); + }); + + it("should remove Emoji from title when showSeverityEmoji is disabled", async() => { + const notifier = new DummyWebhookNotifier({ + ...kDummyNotifierOptions, + template: { + title: "foobar", + content: [] + } + }); + notifier.showSeverityEmoji = false; + + const title = await notifier.formatTitle(); + assert.strictEqual(title, "foobar"); + }); + }); + + describe("formatContent", () => { + it("should format content injecting variables in it and formating with morphix when required", async() => { + const notifier = new DummyWebhookNotifier({ + ...kDummyNotifierOptions, + template: { + title: "", + content: [ + "labelCount: {labelCount}", + "severity: {severity | capitalize}" + ] + } + }); + + const content = await notifier.formatContent(); + assert.deepEqual( + content, + ["labelCount: 0", "severity: Critical"] + ); + }); + }); + + test("execute should trigger an HTTP request", async() => { pool.intercept({ method: "POST", path: "/dummy" @@ -59,3 +113,9 @@ describe("Webhook", () => { assert.strictEqual(response.statusCode, 200); }); }); + +class DummyWebhookNotifier extends WebhookNotifier<{ foo: string }> { + async formatWebhookBody() { + return { foo: "bar" }; + } +}