feat: add ntfy notification support
This commit is contained in:
1 parent
4a939ad2c4
commit
8a83bc99e9
17 files changed
+492
-27
No files matched your search
@@ -0,0 +1,112 @@
|
||||
import { describe, expect, it, jest, beforeEach } from "@jest/globals";
|
||||
import { createMockLogger } from "../../../helpers/createMockLogger.ts";
|
||||
import { makeNotification, makeMessage, makeMessageWithThresholds, makeMessageWithIncident } from "../../../helpers/notificationMessage.ts";
|
||||
import { testNotificationProviderContract } from "../../../helpers/notificationProviderContract.ts";
|
||||
|
||||
const mockGotPost = jest.fn().mockResolvedValue({});
|
||||
jest.unstable_mockModule("got", () => ({ default: { post: mockGotPost } }));
|
||||
|
||||
const { NtfyProvider } = await import("../../../../src/service/infrastructure/notificationProviders/ntfy.ts");
|
||||
|
||||
const createProvider = () => {
|
||||
const logger = createMockLogger();
|
||||
return { provider: new NtfyProvider(logger as any), logger };
|
||||
};
|
||||
|
||||
const makeNtfyNotification = (overrides = {}) =>
|
||||
makeNotification({
|
||||
address: "https://ntfy.sh/",
|
||||
topic: "checkmate-alerts",
|
||||
...overrides,
|
||||
});
|
||||
|
||||
testNotificationProviderContract("NtfyProvider", {
|
||||
create: () => {
|
||||
mockGotPost.mockResolvedValue({});
|
||||
return createProvider().provider;
|
||||
},
|
||||
makeNotification: () => makeNtfyNotification(),
|
||||
});
|
||||
|
||||
describe("NtfyProvider", () => {
|
||||
beforeEach(() => mockGotPost.mockReset().mockResolvedValue({}));
|
||||
|
||||
describe("sendTestAlert", () => {
|
||||
it("posts the test message to the configured topic", async () => {
|
||||
expect(await createProvider().provider.sendTestAlert(makeNtfyNotification())).toBe(true);
|
||||
expect(mockGotPost).toHaveBeenCalledWith(
|
||||
"https://ntfy.sh/checkmate-alerts",
|
||||
expect.objectContaining({
|
||||
body: expect.any(String),
|
||||
headers: expect.objectContaining({ Title: "Checkmate test notification" }),
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("returns false when address is missing", async () => {
|
||||
expect(await createProvider().provider.sendTestAlert(makeNtfyNotification({ address: "" }))).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when topic is missing", async () => {
|
||||
expect(await createProvider().provider.sendTestAlert(makeNtfyNotification({ topic: "" }))).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false and logs on error", async () => {
|
||||
mockGotPost.mockRejectedValue(new Error("fail"));
|
||||
const { provider, logger } = createProvider();
|
||||
expect(await provider.sendTestAlert(makeNtfyNotification())).toBe(false);
|
||||
expect(logger.warn).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("sendMessage", () => {
|
||||
it("posts plain text with severity headers to the configured topic", async () => {
|
||||
const { provider } = createProvider();
|
||||
expect(await provider.sendMessage(makeNtfyNotification() as any, makeMessage())).toBe(true);
|
||||
expect(mockGotPost).toHaveBeenCalledWith(
|
||||
"https://ntfy.sh/checkmate-alerts",
|
||||
expect.objectContaining({
|
||||
body: expect.stringContaining("Monitor Details"),
|
||||
headers: expect.objectContaining({
|
||||
Title: "Monitor Down: Test Monitor",
|
||||
Priority: "high",
|
||||
Tags: "rotating_light",
|
||||
}),
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("url-encodes topics", async () => {
|
||||
const { provider } = createProvider();
|
||||
await provider.sendMessage(makeNtfyNotification({ topic: "ops alerts" }) as any, makeMessage());
|
||||
expect(mockGotPost.mock.calls[0][0]).toBe("https://ntfy.sh/ops%20alerts");
|
||||
});
|
||||
|
||||
it("returns false when address is missing", async () => {
|
||||
expect(await createProvider().provider.sendMessage(makeNtfyNotification({ address: "" }) as any, makeMessage())).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when topic is missing", async () => {
|
||||
expect(await createProvider().provider.sendMessage(makeNtfyNotification({ topic: "" }) as any, makeMessage())).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false and logs on error", async () => {
|
||||
mockGotPost.mockRejectedValue(new Error("fail"));
|
||||
const { provider, logger } = createProvider();
|
||||
expect(await provider.sendMessage(makeNtfyNotification() as any, makeMessage())).toBe(false);
|
||||
expect(logger.warn).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("includes threshold breaches in text", async () => {
|
||||
const { provider } = createProvider();
|
||||
await provider.sendMessage(makeNtfyNotification() as any, makeMessageWithThresholds());
|
||||
expect(mockGotPost.mock.calls[0][1].body).toContain("CPU");
|
||||
});
|
||||
|
||||
it("includes incident links in text", async () => {
|
||||
const { provider } = createProvider();
|
||||
await provider.sendMessage(makeNtfyNotification() as any, makeMessageWithIncident());
|
||||
expect(mockGotPost.mock.calls[0][1].body).toContain("/infrastructure/mon-1");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -47,6 +47,7 @@ const createService = (overrides?: Record<string, unknown>) => {
|
||||
const telegramProvider = createProvider();
|
||||
const pushoverProvider = createProvider();
|
||||
const twilioProvider = createProvider();
|
||||
const ntfyProvider = createProvider();
|
||||
const settingsService = createSettingsService();
|
||||
const notificationMessageBuilder = createMessageBuilder();
|
||||
|
||||
@@ -64,6 +65,7 @@ const createService = (overrides?: Record<string, unknown>) => {
|
||||
telegramProvider,
|
||||
pushoverProvider,
|
||||
twilioProvider,
|
||||
ntfyProvider,
|
||||
settingsService,
|
||||
notificationMessageBuilder,
|
||||
...overrides,
|
||||
@@ -82,6 +84,7 @@ const createService = (overrides?: Record<string, unknown>) => {
|
||||
defaults.telegramProvider as any,
|
||||
defaults.pushoverProvider as any,
|
||||
defaults.twilioProvider as any,
|
||||
defaults.ntfyProvider as any,
|
||||
defaults.settingsService as any,
|
||||
defaults.logger as any,
|
||||
defaults.notificationMessageBuilder as any
|
||||
@@ -147,7 +150,7 @@ describe("NotificationsService", () => {
|
||||
});
|
||||
|
||||
it("routes to correct provider for each notification type", async () => {
|
||||
const types = ["webhook", "slack", "matrix", "pager_duty", "discord", "email", "teams", "telegram", "pushover", "twilio"] as const;
|
||||
const types = ["webhook", "slack", "matrix", "pager_duty", "discord", "email", "teams", "telegram", "pushover", "twilio", "ntfy"] as const;
|
||||
for (const type of types) {
|
||||
const deps = createService();
|
||||
(deps.notificationsRepository.findNotificationsByIds as jest.Mock).mockResolvedValue([makeNotification({ type })]);
|
||||
@@ -165,6 +168,7 @@ describe("NotificationsService", () => {
|
||||
telegram: deps.telegramProvider,
|
||||
pushover: deps.pushoverProvider,
|
||||
twilio: deps.twilioProvider,
|
||||
ntfy: deps.ntfyProvider,
|
||||
};
|
||||
expect(providerMap[type].sendMessage).toHaveBeenCalledTimes(1);
|
||||
}
|
||||
@@ -238,30 +242,40 @@ describe("NotificationsService", () => {
|
||||
// ── sendTestNotification ─────────────────────────────────────────────────
|
||||
|
||||
describe("sendTestNotification", () => {
|
||||
it.each([["email"], ["slack"], ["discord"], ["pager_duty"], ["matrix"], ["webhook"], ["teams"], ["telegram"], ["pushover"], ["twilio"]] as const)(
|
||||
"routes %s to the correct provider",
|
||||
async (type) => {
|
||||
const deps = createService();
|
||||
const notification = makeNotification({ type: type as any });
|
||||
it.each([
|
||||
["email"],
|
||||
["slack"],
|
||||
["discord"],
|
||||
["pager_duty"],
|
||||
["matrix"],
|
||||
["webhook"],
|
||||
["teams"],
|
||||
["telegram"],
|
||||
["pushover"],
|
||||
["twilio"],
|
||||
["ntfy"],
|
||||
] as const)("routes %s to the correct provider", async (type) => {
|
||||
const deps = createService();
|
||||
const notification = makeNotification({ type: type as any });
|
||||
|
||||
const result = await deps.service.sendTestNotification(notification);
|
||||
const result = await deps.service.sendTestNotification(notification);
|
||||
|
||||
expect(result).toBe(true);
|
||||
const providerMap: Record<string, ReturnType<typeof createProvider>> = {
|
||||
webhook: deps.webhookProvider,
|
||||
slack: deps.slackProvider,
|
||||
matrix: deps.matrixProvider,
|
||||
pager_duty: deps.pagerDutyProvider,
|
||||
discord: deps.discordProvider,
|
||||
email: deps.emailProvider,
|
||||
teams: deps.teamsProvider,
|
||||
telegram: deps.telegramProvider,
|
||||
pushover: deps.pushoverProvider,
|
||||
twilio: deps.twilioProvider,
|
||||
};
|
||||
expect(providerMap[type].sendTestAlert).toHaveBeenCalledWith(notification);
|
||||
}
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
const providerMap: Record<string, ReturnType<typeof createProvider>> = {
|
||||
webhook: deps.webhookProvider,
|
||||
slack: deps.slackProvider,
|
||||
matrix: deps.matrixProvider,
|
||||
pager_duty: deps.pagerDutyProvider,
|
||||
discord: deps.discordProvider,
|
||||
email: deps.emailProvider,
|
||||
teams: deps.teamsProvider,
|
||||
telegram: deps.telegramProvider,
|
||||
pushover: deps.pushoverProvider,
|
||||
twilio: deps.twilioProvider,
|
||||
ntfy: deps.ntfyProvider,
|
||||
};
|
||||
expect(providerMap[type].sendTestAlert).toHaveBeenCalledWith(notification);
|
||||
});
|
||||
|
||||
it("returns false for unknown notification type", async () => {
|
||||
const { service } = createService();
|
||||
|
||||
Reference in new issue
Block a user