feat: integrate observability
All checks were successful
Monorepo Pipeline / 🧪 Quality Assurance (push) Successful in 3m50s
Monorepo Pipeline / 🚀 Release (push) Successful in 3m38s
Monorepo Pipeline / 🐳 Build & Push Images (push) Successful in 7m6s

This commit is contained in:
2026-02-07 10:23:51 +01:00
parent 6501eac38a
commit 61e78ea672
34 changed files with 1632 additions and 14 deletions

View File

@@ -0,0 +1,82 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { GotifyNotificationService } from "./gotify";
describe("GotifyNotificationService", () => {
const mockConfig = {
url: "https://gotify.test",
token: "test-token",
enabled: true,
};
const mockLogger = {
error: vi.fn(),
};
beforeEach(() => {
vi.clearAllMocks();
global.fetch = vi.fn();
});
it("should not notify if disabled", async () => {
const service = new GotifyNotificationService({
...mockConfig,
enabled: false,
});
await service.notify({ title: "test", message: "test" });
expect(global.fetch).not.toHaveBeenCalled();
});
it("should send correct payload to Gotify", async () => {
const service = new GotifyNotificationService(mockConfig, mockLogger);
(global.fetch as any).mockResolvedValue({ ok: true });
await service.notify({
title: "Alert",
message: "Critical issue",
priority: 8,
});
expect(global.fetch).toHaveBeenCalledWith(
"https://gotify.test/message?token=test-token",
expect.objectContaining({
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
title: "Alert",
message: "Critical issue",
priority: 8,
}),
}),
);
});
it("should handle missing trailing slash in URL", async () => {
const service = new GotifyNotificationService({
...mockConfig,
url: "https://gotify.test",
});
(global.fetch as any).mockResolvedValue({ ok: true });
await service.notify({ title: "test", message: "test" });
expect((global.fetch as any).mock.calls[0][0]).toBe(
"https://gotify.test/message?token=test-token",
);
});
it("should log error if notify fails", async () => {
const service = new GotifyNotificationService(mockConfig, mockLogger);
(global.fetch as any).mockResolvedValue({
ok: false,
status: 401,
text: () => Promise.resolve("Unauthorized"),
});
await service.notify({ title: "test", message: "test" });
expect(mockLogger.error).toHaveBeenCalledWith(
"Gotify notification failed",
expect.objectContaining({ status: 401 }),
);
});
});

View File

@@ -0,0 +1,56 @@
import { NotificationOptions, NotificationService } from "./service";
export interface GotifyConfig {
url: string;
token: string;
enabled: boolean;
}
/**
* Gotify Notification Service implementation.
*/
export class GotifyNotificationService implements NotificationService {
constructor(
private config: GotifyConfig,
private logger?: { error(msg: string, data?: any): void },
) {}
async notify(options: NotificationOptions): Promise<void> {
if (!this.config.enabled) return;
try {
const { title, message, priority = 4 } = options;
// Ensure we have a trailing slash for base URL, then append 'message'
const baseUrl = this.config.url.endsWith("/")
? this.config.url
: `${this.config.url}/`;
const url = new URL("message", baseUrl);
url.searchParams.set("token", this.config.token);
const response = await fetch(url.toString(), {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
title,
message,
priority,
}),
});
if (!response.ok) {
const errorText = await response.text();
this.logger?.error("Gotify notification failed", {
status: response.status,
error: errorText.slice(0, 100),
});
}
} catch (error) {
this.logger?.error("Gotify notification error", {
error: (error as Error).message,
});
}
}
}

View File

@@ -0,0 +1,11 @@
import { describe, it, expect } from "vitest";
import { NoopNotificationService } from "./noop";
describe("NoopNotificationService", () => {
it("should not throw on notify", async () => {
const service = new NoopNotificationService();
await expect(
service.notify({ title: "test", message: "test" }),
).resolves.not.toThrow();
});
});

View File

@@ -0,0 +1,10 @@
import { NotificationService } from "./service";
/**
* No-operation notification service.
*/
export class NoopNotificationService implements NotificationService {
async notify(): Promise<void> {
// Do nothing
}
}

View File

@@ -0,0 +1,16 @@
export interface NotificationOptions {
title: string;
message: string;
priority?: number;
}
/**
* Interface for notification service implementations.
* Allows for different implementations (Gotify, Slack, Email, etc.)
*/
export interface NotificationService {
/**
* Send a notification.
*/
notify(options: NotificationOptions): Promise<void>;
}