Add tests for new controller and DB methods, add missing statusService test
This commit is contained in:
1 parent
22506efaef
commit
792de1105f
3 files changed
+158
-1
No files matched your search
@@ -1,5 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
getAllMonitors,
|
getAllMonitors,
|
||||||
|
getAllMonitorsWithUptimeStats,
|
||||||
getMonitorStatsById,
|
getMonitorStatsById,
|
||||||
getMonitorCertificate,
|
getMonitorCertificate,
|
||||||
getMonitorById,
|
getMonitorById,
|
||||||
@@ -61,6 +62,47 @@ describe("Monitor Controller - getAllMonitors", () => {
|
|||||||
).to.be.true;
|
).to.be.true;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
describe("Monitor Controller - getAllMonitorsWithUptimeStats", () => {
|
||||||
|
let req, res, next;
|
||||||
|
beforeEach(() => {
|
||||||
|
req = {
|
||||||
|
params: {},
|
||||||
|
query: {},
|
||||||
|
body: {},
|
||||||
|
db: {
|
||||||
|
getAllMonitorsWithUptimeStats: sinon.stub(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
res = {
|
||||||
|
status: sinon.stub().returnsThis(),
|
||||||
|
json: sinon.stub(),
|
||||||
|
};
|
||||||
|
next = sinon.stub();
|
||||||
|
});
|
||||||
|
afterEach(() => {
|
||||||
|
sinon.restore();
|
||||||
|
});
|
||||||
|
it("should reject with an error if DB operations fail", async () => {
|
||||||
|
req.db.getAllMonitorsWithUptimeStats.throws(new Error("DB error"));
|
||||||
|
await getAllMonitorsWithUptimeStats(req, res, next);
|
||||||
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
|
expect(next.firstCall.args[0].message).to.equal("DB error");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return success message and data if all operations succeed", async () => {
|
||||||
|
const data = [{ monitor: "data" }];
|
||||||
|
req.db.getAllMonitorsWithUptimeStats.returns(data);
|
||||||
|
await getAllMonitorsWithUptimeStats(req, res, next);
|
||||||
|
expect(res.status.firstCall.args[0]).to.equal(200);
|
||||||
|
expect(
|
||||||
|
res.json.calledOnceWith({
|
||||||
|
success: true,
|
||||||
|
msg: successMessages.MONITOR_GET_ALL,
|
||||||
|
data: data,
|
||||||
|
})
|
||||||
|
).to.be.true;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("Monitor Controller - getMonitorStatsById", () => {
|
describe("Monitor Controller - getMonitorStatsById", () => {
|
||||||
let req, res, next;
|
let req, res, next;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import Notification from "../../db/models/Notification.js";
|
|||||||
import { errorMessages } from "../../utils/messages.js";
|
import { errorMessages } from "../../utils/messages.js";
|
||||||
import {
|
import {
|
||||||
getAllMonitors,
|
getAllMonitors,
|
||||||
|
getAllMonitorsWithUptimeStats,
|
||||||
getMonitorStatsById,
|
getMonitorStatsById,
|
||||||
getMonitorById,
|
getMonitorById,
|
||||||
getMonitorsAndSummaryByTeamId,
|
getMonitorsAndSummaryByTeamId,
|
||||||
@@ -22,7 +23,6 @@ import {
|
|||||||
getAverageResponseTime,
|
getAverageResponseTime,
|
||||||
getUptimePercentage,
|
getUptimePercentage,
|
||||||
getIncidents,
|
getIncidents,
|
||||||
getDateRange,
|
|
||||||
getMonitorChecks,
|
getMonitorChecks,
|
||||||
processChecksForDisplay,
|
processChecksForDisplay,
|
||||||
groupChecksByTime,
|
groupChecksByTime,
|
||||||
@@ -91,6 +91,100 @@ describe("monitorModule", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("getAllMonitorsWithUptimeStats", () => {
|
||||||
|
it("should return monitors with uptime stats for different time periods", async () => {
|
||||||
|
// Mock data
|
||||||
|
const mockMonitors = [
|
||||||
|
{
|
||||||
|
_id: "monitor1",
|
||||||
|
type: "http",
|
||||||
|
toObject: () => ({
|
||||||
|
_id: "monitor1",
|
||||||
|
type: "http",
|
||||||
|
name: "Test Monitor",
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const mockChecks = [
|
||||||
|
{ status: true },
|
||||||
|
{ status: true },
|
||||||
|
{ status: false },
|
||||||
|
{ status: true },
|
||||||
|
];
|
||||||
|
|
||||||
|
monitorFindStub.resolves(mockMonitors);
|
||||||
|
checkFindStub.resolves(mockChecks);
|
||||||
|
|
||||||
|
const result = await getAllMonitorsWithUptimeStats();
|
||||||
|
|
||||||
|
expect(result).to.be.an("array");
|
||||||
|
expect(result).to.have.lengthOf(1);
|
||||||
|
|
||||||
|
const monitor = result[0];
|
||||||
|
expect(monitor).to.have.property("_id", "monitor1");
|
||||||
|
expect(monitor).to.have.property("name", "Test Monitor");
|
||||||
|
|
||||||
|
// Check uptime percentages exist for all time periods
|
||||||
|
expect(monitor).to.have.property("1");
|
||||||
|
expect(monitor).to.have.property("7");
|
||||||
|
expect(monitor).to.have.property("30");
|
||||||
|
expect(monitor).to.have.property("90");
|
||||||
|
|
||||||
|
// Verify uptime percentage calculation (3 successful out of 4 = 75%)
|
||||||
|
expect(monitor["1"]).to.equal(75);
|
||||||
|
expect(monitor["7"]).to.equal(75);
|
||||||
|
expect(monitor["30"]).to.equal(75);
|
||||||
|
expect(monitor["90"]).to.equal(75);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle errors appropriately", async () => {
|
||||||
|
// Setup stub to throw error
|
||||||
|
monitorFindStub.rejects(new Error("Database error"));
|
||||||
|
|
||||||
|
try {
|
||||||
|
await getAllMonitorsWithUptimeStats();
|
||||||
|
} catch (error) {
|
||||||
|
expect(error).to.be.an("error");
|
||||||
|
expect(error.message).to.equal("Database error");
|
||||||
|
expect(error.service).to.equal("monitorModule");
|
||||||
|
expect(error.method).to.equal("getAllMonitorsWithUptimeStats");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
it("should handle empty monitor list", async () => {
|
||||||
|
monitorFindStub.resolves([]);
|
||||||
|
|
||||||
|
const result = await getAllMonitorsWithUptimeStats();
|
||||||
|
|
||||||
|
expect(result).to.be.an("array");
|
||||||
|
expect(result).to.have.lengthOf(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle monitor with no checks", async () => {
|
||||||
|
const mockMonitors = [
|
||||||
|
{
|
||||||
|
_id: "monitor1",
|
||||||
|
type: "http",
|
||||||
|
toObject: () => ({
|
||||||
|
_id: "monitor1",
|
||||||
|
type: "http",
|
||||||
|
name: "Test Monitor",
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
monitorFindStub.resolves(mockMonitors);
|
||||||
|
checkFindStub.resolves([]);
|
||||||
|
|
||||||
|
const result = await getAllMonitorsWithUptimeStats();
|
||||||
|
|
||||||
|
expect(result[0]).to.have.property("1", 0);
|
||||||
|
expect(result[0]).to.have.property("7", 0);
|
||||||
|
expect(result[0]).to.have.property("30", 0);
|
||||||
|
expect(result[0]).to.have.property("90", 0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("calculateUptimeDuration", () => {
|
describe("calculateUptimeDuration", () => {
|
||||||
let clock;
|
let clock;
|
||||||
const NOW = new Date("2024-01-01T12:00:00Z").getTime();
|
const NOW = new Date("2024-01-01T12:00:00Z").getTime();
|
||||||
|
|||||||
@@ -98,6 +98,7 @@ describe("StatusService", () => {
|
|||||||
expect(check.responseTime).to.equal(100);
|
expect(check.responseTime).to.equal(100);
|
||||||
expect(check.message).to.equal("Test message");
|
expect(check.message).to.equal("Test message");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should build a check object for pagespeed type", () => {
|
it("should build a check object for pagespeed type", () => {
|
||||||
const check = statusService.buildCheck({
|
const check = statusService.buildCheck({
|
||||||
monitorId: "test",
|
monitorId: "test",
|
||||||
@@ -193,6 +194,26 @@ describe("StatusService", () => {
|
|||||||
expect(check.disk).to.equal("disk");
|
expect(check.disk).to.equal("disk");
|
||||||
expect(check.host).to.equal("host");
|
expect(check.host).to.equal("host");
|
||||||
});
|
});
|
||||||
|
it("should build a check for hardware type with missing data", () => {
|
||||||
|
const check = statusService.buildCheck({
|
||||||
|
monitorId: "test",
|
||||||
|
type: "hardware",
|
||||||
|
status: true,
|
||||||
|
responseTime: 100,
|
||||||
|
code: 200,
|
||||||
|
message: "Test message",
|
||||||
|
payload: {},
|
||||||
|
});
|
||||||
|
expect(check.monitorId).to.equal("test");
|
||||||
|
expect(check.status).to.be.true;
|
||||||
|
expect(check.statusCode).to.equal(200);
|
||||||
|
expect(check.responseTime).to.equal(100);
|
||||||
|
expect(check.message).to.equal("Test message");
|
||||||
|
expect(check.cpu).to.deep.equal({});
|
||||||
|
expect(check.memory).to.deep.equal({});
|
||||||
|
expect(check.disk).to.deep.equal({});
|
||||||
|
expect(check.host).to.deep.equal({});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
describe("insertCheck", () => {
|
describe("insertCheck", () => {
|
||||||
it("should log an error if one is thrown", async () => {
|
it("should log an error if one is thrown", async () => {
|
||||||
|
|||||||
Reference in new issue
Block a user