feat: integrate observability
This commit is contained in:
82
packages/observability/src/notifications/gotify.test.ts
Normal file
82
packages/observability/src/notifications/gotify.test.ts
Normal 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 }),
|
||||
);
|
||||
});
|
||||
});
|
||||
56
packages/observability/src/notifications/gotify.ts
Normal file
56
packages/observability/src/notifications/gotify.ts
Normal 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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
11
packages/observability/src/notifications/noop.test.ts
Normal file
11
packages/observability/src/notifications/noop.test.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
10
packages/observability/src/notifications/noop.ts
Normal file
10
packages/observability/src/notifications/noop.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { NotificationService } from "./service";
|
||||
|
||||
/**
|
||||
* No-operation notification service.
|
||||
*/
|
||||
export class NoopNotificationService implements NotificationService {
|
||||
async notify(): Promise<void> {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
16
packages/observability/src/notifications/service.ts
Normal file
16
packages/observability/src/notifications/service.ts
Normal 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>;
|
||||
}
|
||||
Reference in New Issue
Block a user