165 lines
6.2 KiB
TypeScript
165 lines
6.2 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
import { MonitorRepository, SEED_MONITOR_RECORDS } from "../src/repositories/monitor.repository.js";
|
|
import { MonitorService } from "../src/services/monitor.service.js";
|
|
import { ProbeResult } from "../src/types/monitor.js";
|
|
import { parseMonitorTarget } from "../src/utils/probe.js";
|
|
|
|
describe("Target Parser & Uptime Monitor Service", () => {
|
|
describe("parseMonitorTarget", () => {
|
|
it("should parse valid HTTP and HTTPS URLs", () => {
|
|
const http = parseMonitorTarget("http://localhost:8080/health");
|
|
expect(http).toEqual({
|
|
valid: true,
|
|
type: "HTTP",
|
|
normalizedTarget: "http://localhost:8080/health",
|
|
});
|
|
|
|
const https = parseMonitorTarget("https://shop.example.com/");
|
|
expect(https).toEqual({
|
|
valid: true,
|
|
type: "HTTP",
|
|
normalizedTarget: "https://shop.example.com/",
|
|
});
|
|
});
|
|
|
|
it("should parse valid TCP host:port targets", () => {
|
|
const tcp = parseMonitorTarget("203.0.113.11:5432");
|
|
expect(tcp).toEqual({
|
|
valid: true,
|
|
type: "TCP",
|
|
normalizedTarget: "203.0.113.11:5432",
|
|
});
|
|
});
|
|
|
|
it("should reject invalid targets and out-of-range ports", () => {
|
|
expect(parseMonitorTarget("").valid).toBe(false);
|
|
expect(parseMonitorTarget("not-a-target").valid).toBe(false);
|
|
expect(parseMonitorTarget("203.0.113.11:70000").valid).toBe(false);
|
|
expect(parseMonitorTarget("256.300.1.1:80").valid).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("MonitorRepository & MonitorService", () => {
|
|
let repository: MonitorRepository;
|
|
let mockProbeResult: ProbeResult;
|
|
let service: MonitorService;
|
|
|
|
beforeEach(() => {
|
|
repository = new MonitorRepository(":memory:");
|
|
repository.init();
|
|
mockProbeResult = { ok: true, latencyMs: 15 };
|
|
service = new MonitorService(repository, async () => mockProbeResult, 2);
|
|
});
|
|
|
|
afterEach(() => {
|
|
service.stopScheduler();
|
|
repository.close();
|
|
});
|
|
|
|
it("should seed all 5 workshop services", () => {
|
|
const inserted = repository.seedIfEmpty();
|
|
expect(inserted).toBe(5);
|
|
expect(SEED_MONITOR_RECORDS).toHaveLength(5);
|
|
|
|
const all = service.listServices();
|
|
expect(all.map((m) => m.name)).toEqual([
|
|
"shop-web",
|
|
"shop-api",
|
|
"staging-web",
|
|
"db-prod",
|
|
"demo-api",
|
|
]);
|
|
expect(service.getServiceStatus("demo-api")?.intervalSeconds).toBe(10);
|
|
expect(service.getServiceStatus("db-prod")?.type).toBe("TCP");
|
|
});
|
|
|
|
it("should add and remove monitored services", () => {
|
|
const added = service.addService({
|
|
name: "custom-api",
|
|
target: "http://localhost:3000/health",
|
|
intervalSeconds: 15,
|
|
});
|
|
|
|
expect(added.name).toBe("custom-api");
|
|
expect(added.type).toBe("HTTP");
|
|
expect(added.status).toBe("PENDING");
|
|
expect(service.listServices()).toHaveLength(1);
|
|
|
|
expect(() =>
|
|
service.addService({ name: "custom-api", target: "http://localhost:3000/health" })
|
|
).toThrow("already exists");
|
|
|
|
expect(service.removeService("custom-api")).toBe(true);
|
|
expect(service.removeService("custom-api")).toBe(false);
|
|
expect(service.listServices()).toHaveLength(0);
|
|
});
|
|
|
|
it("should NOT alert on a single transient failure (anti-flapping) and reset on recovery", async () => {
|
|
const mon = service.addService({
|
|
name: "demo-api",
|
|
target: "http://localhost:8080/health",
|
|
intervalSeconds: 10,
|
|
});
|
|
|
|
// 1. Initial UP check
|
|
const upCheck = await service.checkService(mon);
|
|
expect(upCheck.monitor.status).toBe("UP");
|
|
expect(upCheck.transition).toBeNull();
|
|
|
|
// 2. Single transient failure -> below threshold (2), should NOT alert DOWN
|
|
mockProbeResult = { ok: false, latencyMs: 5000, error: "ECONNREFUSED" };
|
|
const blipCheck = await service.checkService(upCheck.monitor);
|
|
expect(blipCheck.monitor.status).toBe("UP");
|
|
expect(blipCheck.monitor.consecutiveFailures).toBe(1);
|
|
expect(blipCheck.transition).toBeNull();
|
|
|
|
// 3. Recovers on next check -> resets consecutiveFailures without alert
|
|
mockProbeResult = { ok: true, latencyMs: 12 };
|
|
const recoverFromBlip = await service.checkService(blipCheck.monitor);
|
|
expect(recoverFromBlip.monitor.status).toBe("UP");
|
|
expect(recoverFromBlip.monitor.consecutiveFailures).toBe(0);
|
|
expect(recoverFromBlip.transition).toBeNull();
|
|
});
|
|
|
|
it("should alert DOWN once at threshold, avoid duplicate DOWN alerts, and alert RECOVERED once", async () => {
|
|
const mon = service.addService({
|
|
name: "demo-api",
|
|
target: "http://localhost:8080/health",
|
|
intervalSeconds: 10,
|
|
});
|
|
|
|
// 1. Failure #1 (below threshold)
|
|
mockProbeResult = { ok: false, latencyMs: 50, error: "Connection refused" };
|
|
const fail1 = await service.checkService(mon);
|
|
expect(fail1.transition).toBeNull();
|
|
expect(fail1.monitor.status).toBe("PENDING");
|
|
|
|
// 2. Failure #2 (reaches threshold = 2) -> emits DOWN alert once
|
|
const fail2 = await service.checkService(fail1.monitor);
|
|
expect(fail2.transition).toBe("DOWN");
|
|
expect(fail2.monitor.status).toBe("DOWN");
|
|
expect(fail2.monitor.lastDownAt).toBeDefined();
|
|
expect(fail2.monitor.lastError).toBe("Connection refused");
|
|
|
|
// 3. Failure #3 while already DOWN -> NO duplicate alert
|
|
const fail3 = await service.checkService(fail2.monitor);
|
|
expect(fail3.transition).toBeNull();
|
|
expect(fail3.monitor.status).toBe("DOWN");
|
|
expect(fail3.monitor.consecutiveFailures).toBe(3);
|
|
|
|
// 4. Service recovers -> emits RECOVERED alert once
|
|
mockProbeResult = { ok: true, latencyMs: 8 };
|
|
const recovered = await service.checkService(fail3.monitor);
|
|
expect(recovered.transition).toBe("RECOVERED");
|
|
expect(recovered.monitor.status).toBe("UP");
|
|
expect(recovered.monitor.consecutiveFailures).toBe(0);
|
|
expect(recovered.monitor.lastError).toBeUndefined();
|
|
|
|
// 5. Subsequent UP check -> no duplicate recovery alert
|
|
const steady = await service.checkService(recovered.monitor);
|
|
expect(steady.transition).toBeNull();
|
|
expect(steady.monitor.status).toBe("UP");
|
|
});
|
|
});
|
|
});
|