You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
uptime-kuma/test/backend-test/test-notification.js

139 lines
3.8 KiB

2 weeks ago
const test = require("node:test");
const assert = require("node:assert");
const { Notification } = require("../../server/notification");
const { UP, DOWN } = require("../../src/util");
test("Notification - Basic Creation Test", async (t) => {
const notification = new Notification();
assert.ok(notification, "Should create notification instance");
assert.ok(typeof notification.send === "function", "Should have send method");
});
test("Notification - Format Message Test", async (t) => {
const notification = new Notification();
2 weeks ago
2 weeks ago
const monitor = {
name: "Test Monitor",
hostname: "example.com"
};
2 weeks ago
2 weeks ago
const msg = {
type: "down",
monitor,
msg: "Connection failed"
};
const formatted = notification.format(msg);
assert.ok(formatted.includes("Test Monitor"), "Should include monitor name");
assert.ok(formatted.includes("example.com"), "Should include hostname");
assert.ok(formatted.includes("Connection failed"), "Should include error message");
});
2 weeks ago
test("Notification - Status Test", async (t) => {
const notification = new Notification();
// Test UP status
const upMsg = {
type: "up",
monitor: { name: "Test1" },
msg: "Service is up",
status: UP
};
const upFormatted = notification.format(upMsg);
assert.ok(upFormatted.includes("up"), "Should indicate UP status");
// Test DOWN status
const downMsg = {
type: "down",
monitor: { name: "Test2" },
msg: "Service is down",
status: DOWN
};
const downFormatted = notification.format(downMsg);
assert.ok(downFormatted.includes("down"), "Should indicate DOWN status");
});
2 weeks ago
test("Notification - Queue Management Test", async (t) => {
const notification = new Notification();
2 weeks ago
2 weeks ago
// Add items to queue
notification.add({
type: "down",
monitor: { name: "Test1" },
msg: "Error 1"
});
2 weeks ago
2 weeks ago
notification.add({
type: "up",
monitor: { name: "Test2" },
msg: "Recovered"
});
2 weeks ago
2 weeks ago
assert.strictEqual(notification.queue.length, 2, "Queue should have 2 items");
});
test("Notification - Priority Test", async (t) => {
const notification = new Notification();
2 weeks ago
2 weeks ago
// Add items with different priorities
notification.add({
type: "down",
monitor: { name: "Test1" },
msg: "Critical Error",
priority: "high"
});
2 weeks ago
2 weeks ago
notification.add({
type: "down",
monitor: { name: "Test2" },
msg: "Warning",
priority: "low"
});
2 weeks ago
2 weeks ago
const nextItem = notification.queue[0];
assert.strictEqual(nextItem.priority, "high", "High priority item should be first");
});
test("Notification - Retry Logic Test", async (t) => {
const notification = new Notification();
2 weeks ago
2 weeks ago
const testMsg = {
type: "down",
monitor: { name: "Test1" },
msg: "Error",
retries: 0,
maxRetries: 3
};
2 weeks ago
2 weeks ago
notification.add(testMsg);
2 weeks ago
2 weeks ago
// Simulate failed send
try {
await notification.send(testMsg);
} catch (error) {
assert.ok(testMsg.retries === 1, "Should increment retry count");
assert.ok(notification.queue.length === 1, "Should keep in queue for retry");
}
});
test("Notification - Rate Limiting Test", async (t) => {
const notification = new Notification();
const monitor = { name: "Test Monitor" };
2 weeks ago
2 weeks ago
// Add multiple notifications for same monitor
for (let i = 0; i < 5; i++) {
notification.add({
type: "down",
monitor,
msg: `Error ${i}`
});
}
2 weeks ago
2 weeks ago
// Check if rate limiting is applied
const processedCount = notification.queue.filter(
item => item.monitor.name === "Test Monitor"
).length;
2 weeks ago
2 weeks ago
assert.ok(processedCount < 5, "Should apply rate limiting");
});