Skip to content

Commit

Permalink
Merge pull request #1124 from bluewave-labs/feat/be/monitor-module-mi…
Browse files Browse the repository at this point in the history
…ssing-tests

Add missing test cases for pagespeed and hardware monitor types
  • Loading branch information
ajhollid authored Nov 11, 2024
2 parents 617aba0 + b5d9345 commit b554074
Showing 1 changed file with 101 additions and 1 deletion.
102 changes: 101 additions & 1 deletion Server/tests/db/monitorModule.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import sinon from "sinon";
import Monitor from "../../db/models/Monitor.js";
import Check from "../../db/models/Check.js";
import PageSpeedCheck from "../../db/models/PageSpeedCheck.js";
import HardwareCheck from "../../db/models/HardwareCheck.js";
import Notification from "../../db/models/Notification.js";

import { errorMessages } from "../../utils/messages.js";
Expand Down Expand Up @@ -37,7 +39,9 @@ describe("monitorModule", () => {
monitorDeleteManyStub,
monitorCountStub,
monitorInsertManyStub,
checkFindStub;
checkFindStub,
pageSpeedCheckFindStub,
hardwareCheckFindStub;
beforeEach(() => {
monitorFindStub = sinon.stub(Monitor, "find");
monitorFindByIdStub = sinon.stub(Monitor, "findById");
Expand All @@ -50,6 +54,12 @@ describe("monitorModule", () => {
checkFindStub = sinon.stub(Check, "find").returns({
sort: sinon.stub(),
});
pageSpeedCheckFindStub = sinon.stub(PageSpeedCheck, "find").returns({
sort: sinon.stub(),
});
hardwareCheckFindStub = sinon.stub(HardwareCheck, "find").returns({
sort: sinon.stub(),
});
});
afterEach(() => {
sinon.restore();
Expand Down Expand Up @@ -137,6 +147,96 @@ describe("monitorModule", () => {
expect(monitor["30"]).to.equal(75);
expect(monitor["90"]).to.equal(75);
});
it("should return monitors with stats for pagespeed type", async () => {
// Mock data
const mockMonitors = [
{
_id: "monitor1",
type: "pagespeed",
toObject: () => ({
_id: "monitor1",
type: "pagespeed",
name: "Test Monitor",
}),
},
];

const mockChecks = [
{ status: true },
{ status: true },
{ status: false },
{ status: true },
];

monitorFindStub.resolves(mockMonitors);
pageSpeedCheckFindStub.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 return monitors with stats for hardware type", async () => {
// Mock data
const mockMonitors = [
{
_id: "monitor1",
type: "hardware",
toObject: () => ({
_id: "monitor1",
type: "hardware",
name: "Test Monitor",
}),
},
];

const mockChecks = [
{ status: true },
{ status: true },
{ status: false },
{ status: true },
];

monitorFindStub.resolves(mockMonitors);
hardwareCheckFindStub.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
Expand Down

0 comments on commit b554074

Please sign in to comment.