fix(test): Use 'npm run lint-fix' to fix 'mocha/no-mocha-arrows' like lint warnings
- ref: https://mochajs.org/#arrow-functions
This commit is contained in:
1 parent
319e6d6017
commit
fcda60fe5b
31 files changed
+1268
-907
No files matched your search
@@ -31,7 +31,7 @@ import {
|
||||
calculateGroupStats,
|
||||
} from "../../db/mongo/modules/monitorModule.js";
|
||||
|
||||
describe("monitorModule", () => {
|
||||
describe("monitorModule", function() {
|
||||
let monitorFindStub,
|
||||
monitorFindByIdStub,
|
||||
monitorFindByIdAndUpdateStub,
|
||||
@@ -42,7 +42,8 @@ describe("monitorModule", () => {
|
||||
checkFindStub,
|
||||
pageSpeedCheckFindStub,
|
||||
hardwareCheckFindStub;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
monitorFindStub = sinon.stub(Monitor, "find");
|
||||
monitorFindByIdStub = sinon.stub(Monitor, "findById");
|
||||
monitorFindByIdAndUpdateStub = sinon.stub(Monitor, "findByIdAndUpdate");
|
||||
@@ -61,12 +62,13 @@ describe("monitorModule", () => {
|
||||
sort: sinon.stub(),
|
||||
});
|
||||
});
|
||||
afterEach(() => {
|
||||
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe("getAllMonitors", () => {
|
||||
it("should return all monitors", async () => {
|
||||
describe("getAllMonitors", function() {
|
||||
it("should return all monitors", async function() {
|
||||
const mockMonitors = [
|
||||
{ _id: "1", name: "Monitor 1", url: "test1.com" },
|
||||
{ _id: "2", name: "Monitor 2", url: "test2.com" },
|
||||
@@ -78,12 +80,14 @@ describe("monitorModule", () => {
|
||||
expect(monitorFindStub.calledOnce).to.be.true;
|
||||
expect(monitorFindStub.firstCall.args).to.deep.equal([]);
|
||||
});
|
||||
it("should handle empty results", async () => {
|
||||
|
||||
it("should handle empty results", async function() {
|
||||
monitorFindStub.returns([]);
|
||||
const result = await getAllMonitors();
|
||||
expect(result).to.be.an("array").that.is.empty;
|
||||
});
|
||||
it("should throw error when database fails", async () => {
|
||||
|
||||
it("should throw error when database fails", async function() {
|
||||
// Arrange
|
||||
const error = new Error("Database error");
|
||||
error.service = "MonitorModule";
|
||||
@@ -101,8 +105,8 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAllMonitorsWithUptimeStats", () => {
|
||||
it("should return monitors with uptime stats for different time periods", async () => {
|
||||
describe("getAllMonitorsWithUptimeStats", function() {
|
||||
it("should return monitors with uptime stats for different time periods", async function() {
|
||||
// Mock data
|
||||
const mockMonitors = [
|
||||
{
|
||||
@@ -147,7 +151,8 @@ describe("monitorModule", () => {
|
||||
expect(monitor["30"]).to.equal(75);
|
||||
expect(monitor["90"]).to.equal(75);
|
||||
});
|
||||
it("should return monitors with stats for pagespeed type", async () => {
|
||||
|
||||
it("should return monitors with stats for pagespeed type", async function() {
|
||||
// Mock data
|
||||
const mockMonitors = [
|
||||
{
|
||||
@@ -192,7 +197,8 @@ describe("monitorModule", () => {
|
||||
expect(monitor["30"]).to.equal(75);
|
||||
expect(monitor["90"]).to.equal(75);
|
||||
});
|
||||
it("should return monitors with stats for hardware type", async () => {
|
||||
|
||||
it("should return monitors with stats for hardware type", async function() {
|
||||
// Mock data
|
||||
const mockMonitors = [
|
||||
{
|
||||
@@ -238,7 +244,7 @@ describe("monitorModule", () => {
|
||||
expect(monitor["90"]).to.equal(75);
|
||||
});
|
||||
|
||||
it("should handle errors appropriately", async () => {
|
||||
it("should handle errors appropriately", async function() {
|
||||
// Setup stub to throw error
|
||||
monitorFindStub.rejects(new Error("Database error"));
|
||||
|
||||
@@ -251,7 +257,8 @@ describe("monitorModule", () => {
|
||||
expect(error.method).to.equal("getAllMonitorsWithUptimeStats");
|
||||
}
|
||||
});
|
||||
it("should handle empty monitor list", async () => {
|
||||
|
||||
it("should handle empty monitor list", async function() {
|
||||
monitorFindStub.resolves([]);
|
||||
|
||||
const result = await getAllMonitorsWithUptimeStats();
|
||||
@@ -260,7 +267,7 @@ describe("monitorModule", () => {
|
||||
expect(result).to.have.lengthOf(0);
|
||||
});
|
||||
|
||||
it("should handle monitor with no checks", async () => {
|
||||
it("should handle monitor with no checks", async function() {
|
||||
const mockMonitors = [
|
||||
{
|
||||
_id: "monitor1",
|
||||
@@ -285,28 +292,28 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("calculateUptimeDuration", () => {
|
||||
describe("calculateUptimeDuration", function() {
|
||||
let clock;
|
||||
const NOW = new Date("2024-01-01T12:00:00Z").getTime();
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
// Fix the current time
|
||||
clock = sinon.useFakeTimers(NOW);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
clock.restore();
|
||||
});
|
||||
|
||||
it("should return 0 when checks array is empty", () => {
|
||||
it("should return 0 when checks array is empty", function() {
|
||||
expect(calculateUptimeDuration([])).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return 0 when checks array is null", () => {
|
||||
it("should return 0 when checks array is null", function() {
|
||||
expect(calculateUptimeDuration(null)).to.equal(0);
|
||||
});
|
||||
|
||||
it("should calculate uptime from last down check to most recent check", () => {
|
||||
it("should calculate uptime from last down check to most recent check", function() {
|
||||
const checks = [
|
||||
{ status: true, createdAt: "2024-01-01T11:00:00Z" }, // Most recent
|
||||
{ status: true, createdAt: "2024-01-01T10:00:00Z" },
|
||||
@@ -318,7 +325,7 @@ describe("monitorModule", () => {
|
||||
expect(calculateUptimeDuration(checks)).to.equal(7200000);
|
||||
});
|
||||
|
||||
it("should calculate uptime from first check when no down checks exist", () => {
|
||||
it("should calculate uptime from first check when no down checks exist", function() {
|
||||
const checks = [
|
||||
{ status: true, createdAt: "2024-01-01T11:00:00Z" },
|
||||
{ status: true, createdAt: "2024-01-01T10:00:00Z" },
|
||||
@@ -330,28 +337,28 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getLastChecked", () => {
|
||||
describe("getLastChecked", function() {
|
||||
let clock;
|
||||
const NOW = new Date("2024-01-01T12:00:00Z").getTime();
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
// Fix the current time
|
||||
clock = sinon.useFakeTimers(NOW);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
clock.restore();
|
||||
});
|
||||
|
||||
it("should return 0 when checks array is empty", () => {
|
||||
it("should return 0 when checks array is empty", function() {
|
||||
expect(getLastChecked([])).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return 0 when checks array is null", () => {
|
||||
it("should return 0 when checks array is null", function() {
|
||||
expect(getLastChecked(null)).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return time difference between now and most recent check", () => {
|
||||
it("should return time difference between now and most recent check", function() {
|
||||
const checks = [
|
||||
{ createdAt: "2024-01-01T11:30:00Z" }, // 30 minutes ago
|
||||
{ createdAt: "2024-01-01T11:00:00Z" },
|
||||
@@ -362,7 +369,7 @@ describe("monitorModule", () => {
|
||||
expect(getLastChecked(checks)).to.equal(1800000);
|
||||
});
|
||||
|
||||
it("should handle checks from different days", () => {
|
||||
it("should handle checks from different days", function() {
|
||||
const checks = [
|
||||
{ createdAt: "2023-12-31T12:00:00Z" }, // 24 hours ago
|
||||
{ createdAt: "2023-12-30T12:00:00Z" },
|
||||
@@ -372,16 +379,17 @@ describe("monitorModule", () => {
|
||||
expect(getLastChecked(checks)).to.equal(86400000);
|
||||
});
|
||||
});
|
||||
describe("getLatestResponseTime", () => {
|
||||
it("should return 0 when checks array is empty", () => {
|
||||
|
||||
describe("getLatestResponseTime", function() {
|
||||
it("should return 0 when checks array is empty", function() {
|
||||
expect(getLatestResponseTime([])).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return 0 when checks array is null", () => {
|
||||
it("should return 0 when checks array is null", function() {
|
||||
expect(getLatestResponseTime(null)).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return response time from most recent check", () => {
|
||||
it("should return response time from most recent check", function() {
|
||||
const checks = [
|
||||
{ responseTime: 150, createdAt: "2024-01-01T11:30:00Z" }, // Most recent
|
||||
{ responseTime: 200, createdAt: "2024-01-01T11:00:00Z" },
|
||||
@@ -391,7 +399,7 @@ describe("monitorModule", () => {
|
||||
expect(getLatestResponseTime(checks)).to.equal(150);
|
||||
});
|
||||
|
||||
it("should handle missing responseTime in checks", () => {
|
||||
it("should handle missing responseTime in checks", function() {
|
||||
const checks = [
|
||||
{ createdAt: "2024-01-01T11:30:00Z" },
|
||||
{ responseTime: 200, createdAt: "2024-01-01T11:00:00Z" },
|
||||
@@ -400,16 +408,17 @@ describe("monitorModule", () => {
|
||||
expect(getLatestResponseTime(checks)).to.equal(0);
|
||||
});
|
||||
});
|
||||
describe("getAverageResponseTime", () => {
|
||||
it("should return 0 when checks array is empty", () => {
|
||||
|
||||
describe("getAverageResponseTime", function() {
|
||||
it("should return 0 when checks array is empty", function() {
|
||||
expect(getAverageResponseTime([])).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return 0 when checks array is null", () => {
|
||||
it("should return 0 when checks array is null", function() {
|
||||
expect(getAverageResponseTime(null)).to.equal(0);
|
||||
});
|
||||
|
||||
it("should calculate average response time from all checks", () => {
|
||||
it("should calculate average response time from all checks", function() {
|
||||
const checks = [
|
||||
{ responseTime: 100, createdAt: "2024-01-01T11:30:00Z" },
|
||||
{ responseTime: 200, createdAt: "2024-01-01T11:00:00Z" },
|
||||
@@ -420,7 +429,7 @@ describe("monitorModule", () => {
|
||||
expect(getAverageResponseTime(checks)).to.equal(200);
|
||||
});
|
||||
|
||||
it("should handle missing responseTime in some checks", () => {
|
||||
it("should handle missing responseTime in some checks", function() {
|
||||
const checks = [
|
||||
{ responseTime: 100, createdAt: "2024-01-01T11:30:00Z" },
|
||||
{ createdAt: "2024-01-01T11:00:00Z" },
|
||||
@@ -431,7 +440,7 @@ describe("monitorModule", () => {
|
||||
expect(getAverageResponseTime(checks)).to.equal(200);
|
||||
});
|
||||
|
||||
it("should return 0 when no checks have responseTime", () => {
|
||||
it("should return 0 when no checks have responseTime", function() {
|
||||
const checks = [
|
||||
{ createdAt: "2024-01-01T11:30:00Z" },
|
||||
{ createdAt: "2024-01-01T11:00:00Z" },
|
||||
@@ -440,26 +449,27 @@ describe("monitorModule", () => {
|
||||
expect(getAverageResponseTime(checks)).to.equal(0);
|
||||
});
|
||||
});
|
||||
describe("getUptimePercentage", () => {
|
||||
it("should return 0 when checks array is empty", () => {
|
||||
|
||||
describe("getUptimePercentage", function() {
|
||||
it("should return 0 when checks array is empty", function() {
|
||||
expect(getUptimePercentage([])).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return 0 when checks array is null", () => {
|
||||
it("should return 0 when checks array is null", function() {
|
||||
expect(getUptimePercentage(null)).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return 100 when all checks are up", () => {
|
||||
it("should return 100 when all checks are up", function() {
|
||||
const checks = [{ status: true }, { status: true }, { status: true }];
|
||||
expect(getUptimePercentage(checks)).to.equal(100);
|
||||
});
|
||||
|
||||
it("should return 0 when all checks are down", () => {
|
||||
it("should return 0 when all checks are down", function() {
|
||||
const checks = [{ status: false }, { status: false }, { status: false }];
|
||||
expect(getUptimePercentage(checks)).to.equal(0);
|
||||
});
|
||||
|
||||
it("should calculate correct percentage for mixed status checks", () => {
|
||||
it("should calculate correct percentage for mixed status checks", function() {
|
||||
const checks = [
|
||||
{ status: true },
|
||||
{ status: false },
|
||||
@@ -470,32 +480,33 @@ describe("monitorModule", () => {
|
||||
expect(getUptimePercentage(checks)).to.equal(75);
|
||||
});
|
||||
|
||||
it("should handle undefined status values", () => {
|
||||
it("should handle undefined status values", function() {
|
||||
const checks = [{ status: true }, { status: undefined }, { status: true }];
|
||||
// 2 up out of 3 total ≈ 66.67%
|
||||
expect(getUptimePercentage(checks)).to.equal((2 / 3) * 100);
|
||||
});
|
||||
});
|
||||
describe("getIncidents", () => {
|
||||
it("should return 0 when checks array is empty", () => {
|
||||
|
||||
describe("getIncidents", function() {
|
||||
it("should return 0 when checks array is empty", function() {
|
||||
expect(getIncidents([])).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return 0 when checks array is null", () => {
|
||||
it("should return 0 when checks array is null", function() {
|
||||
expect(getIncidents(null)).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return 0 when all checks are up", () => {
|
||||
it("should return 0 when all checks are up", function() {
|
||||
const checks = [{ status: true }, { status: true }, { status: true }];
|
||||
expect(getIncidents(checks)).to.equal(0);
|
||||
});
|
||||
|
||||
it("should count all incidents when all checks are down", () => {
|
||||
it("should count all incidents when all checks are down", function() {
|
||||
const checks = [{ status: false }, { status: false }, { status: false }];
|
||||
expect(getIncidents(checks)).to.equal(3);
|
||||
});
|
||||
|
||||
it("should count correct number of incidents for mixed status checks", () => {
|
||||
it("should count correct number of incidents for mixed status checks", function() {
|
||||
const checks = [
|
||||
{ status: true },
|
||||
{ status: false },
|
||||
@@ -506,7 +517,7 @@ describe("monitorModule", () => {
|
||||
expect(getIncidents(checks)).to.equal(2);
|
||||
});
|
||||
|
||||
it("should handle undefined status values", () => {
|
||||
it("should handle undefined status values", function() {
|
||||
const checks = [
|
||||
{ status: true },
|
||||
{ status: undefined },
|
||||
@@ -517,10 +528,11 @@ describe("monitorModule", () => {
|
||||
expect(getIncidents(checks)).to.equal(2);
|
||||
});
|
||||
});
|
||||
describe("getMonitorChecks", () => {
|
||||
|
||||
describe("getMonitorChecks", function() {
|
||||
let mockModel;
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
// Create a mock model with chainable methods
|
||||
const mockChecks = [
|
||||
{ monitorId: "123", createdAt: new Date("2024-01-01") },
|
||||
@@ -534,11 +546,11 @@ describe("monitorModule", () => {
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should return all checks and date-ranged checks", async () => {
|
||||
it("should return all checks and date-ranged checks", async function() {
|
||||
// Arrange
|
||||
const monitorId = "123";
|
||||
const dateRange = {
|
||||
@@ -567,7 +579,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle empty results", async () => {
|
||||
it("should handle empty results", async function() {
|
||||
// Arrange
|
||||
const emptyModel = {
|
||||
find: sinon.stub().returns({
|
||||
@@ -591,7 +603,7 @@ describe("monitorModule", () => {
|
||||
expect(result.checksForDateRange).to.be.an("array").that.is.empty;
|
||||
});
|
||||
|
||||
it("should maintain sort order", async () => {
|
||||
it("should maintain sort order", async function() {
|
||||
// Arrange
|
||||
const sortedChecks = [
|
||||
{ monitorId: "123", createdAt: new Date("2024-01-02") },
|
||||
@@ -625,39 +637,39 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("processChecksForDisplay", () => {
|
||||
describe("processChecksForDisplay", function() {
|
||||
let normalizeStub;
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
normalizeStub = sinon.stub();
|
||||
});
|
||||
|
||||
it("should return original checks when numToDisplay is not provided", () => {
|
||||
it("should return original checks when numToDisplay is not provided", function() {
|
||||
const checks = [1, 2, 3, 4, 5];
|
||||
const result = processChecksForDisplay(normalizeStub, checks);
|
||||
expect(result).to.deep.equal(checks);
|
||||
});
|
||||
|
||||
it("should return original checks when numToDisplay is greater than checks length", () => {
|
||||
it("should return original checks when numToDisplay is greater than checks length", function() {
|
||||
const checks = [1, 2, 3];
|
||||
const result = processChecksForDisplay(normalizeStub, checks, 5);
|
||||
expect(result).to.deep.equal(checks);
|
||||
});
|
||||
|
||||
it("should filter checks based on numToDisplay", () => {
|
||||
it("should filter checks based on numToDisplay", function() {
|
||||
const checks = [1, 2, 3, 4, 5, 6];
|
||||
const result = processChecksForDisplay(normalizeStub, checks, 3);
|
||||
// Should return [1, 3, 5] as n = ceil(6/3) = 2
|
||||
expect(result).to.deep.equal([1, 3, 5]);
|
||||
});
|
||||
|
||||
it("should handle empty checks array", () => {
|
||||
it("should handle empty checks array", function() {
|
||||
const checks = [];
|
||||
const result = processChecksForDisplay(normalizeStub, checks, 3);
|
||||
expect(result).to.be.an("array").that.is.empty;
|
||||
});
|
||||
|
||||
it("should call normalizeData when normalize is true", () => {
|
||||
it("should call normalizeData when normalize is true", function() {
|
||||
const checks = [1, 2, 3];
|
||||
normalizeStub.returns([10, 20, 30]);
|
||||
|
||||
@@ -667,7 +679,7 @@ describe("monitorModule", () => {
|
||||
expect(result).to.deep.equal([10, 20, 30]);
|
||||
});
|
||||
|
||||
it("should handle both filtering and normalization", () => {
|
||||
it("should handle both filtering and normalization", function() {
|
||||
const checks = [1, 2, 3, 4, 5, 6];
|
||||
normalizeStub.returns([10, 30, 50]);
|
||||
|
||||
@@ -677,7 +689,8 @@ describe("monitorModule", () => {
|
||||
expect(result).to.deep.equal([10, 30, 50]);
|
||||
});
|
||||
});
|
||||
describe("groupChecksByTime", () => {
|
||||
|
||||
describe("groupChecksByTime", function() {
|
||||
const mockChecks = [
|
||||
{ createdAt: "2024-01-15T10:30:45Z" },
|
||||
{ createdAt: "2024-01-15T10:45:15Z" },
|
||||
@@ -685,7 +698,7 @@ describe("monitorModule", () => {
|
||||
{ createdAt: "2024-01-16T10:30:00Z" },
|
||||
];
|
||||
|
||||
it("should group checks by hour when dateRange is 'day'", () => {
|
||||
it("should group checks by hour when dateRange is 'day'", function() {
|
||||
const result = groupChecksByTime(mockChecks, "day");
|
||||
|
||||
// Get timestamps for 10:00 and 11:00 on Jan 15
|
||||
@@ -700,7 +713,7 @@ describe("monitorModule", () => {
|
||||
expect(result[time3].checks).to.have.lengthOf(1);
|
||||
});
|
||||
|
||||
it("should group checks by day when dateRange is not 'day'", () => {
|
||||
it("should group checks by day when dateRange is not 'day'", function() {
|
||||
const result = groupChecksByTime(mockChecks, "week");
|
||||
|
||||
expect(Object.keys(result)).to.have.lengthOf(2);
|
||||
@@ -708,12 +721,12 @@ describe("monitorModule", () => {
|
||||
expect(result["2024-01-16"].checks).to.have.lengthOf(1);
|
||||
});
|
||||
|
||||
it("should handle empty checks array", () => {
|
||||
it("should handle empty checks array", function() {
|
||||
const result = groupChecksByTime([], "day");
|
||||
expect(result).to.deep.equal({});
|
||||
});
|
||||
|
||||
it("should handle single check", () => {
|
||||
it("should handle single check", function() {
|
||||
const singleCheck = [{ createdAt: "2024-01-15T10:30:45Z" }];
|
||||
const result = groupChecksByTime(singleCheck, "day");
|
||||
|
||||
@@ -721,7 +734,8 @@ describe("monitorModule", () => {
|
||||
expect(Object.keys(result)).to.have.lengthOf(1);
|
||||
expect(result[expectedTime].checks).to.have.lengthOf(1);
|
||||
});
|
||||
it("should skip invalid dates and process valid ones", () => {
|
||||
|
||||
it("should skip invalid dates and process valid ones", function() {
|
||||
const checksWithInvalidDate = [
|
||||
{ createdAt: "invalid-date" },
|
||||
{ createdAt: "2024-01-15T10:30:45Z" },
|
||||
@@ -737,7 +751,8 @@ describe("monitorModule", () => {
|
||||
expect(result[expectedTime].checks).to.have.lengthOf(1);
|
||||
expect(result[expectedTime].checks[0].createdAt).to.equal("2024-01-15T10:30:45Z");
|
||||
});
|
||||
it("should handle checks in same time group", () => {
|
||||
|
||||
it("should handle checks in same time group", function() {
|
||||
const checksInSameHour = [
|
||||
{ createdAt: "2024-01-15T10:15:00Z" },
|
||||
{ createdAt: "2024-01-15T10:45:00Z" },
|
||||
@@ -750,16 +765,17 @@ describe("monitorModule", () => {
|
||||
expect(result[expectedTime].checks).to.have.lengthOf(2);
|
||||
});
|
||||
});
|
||||
describe("calculateGroupStats", () => {
|
||||
|
||||
describe("calculateGroupStats", function() {
|
||||
// Mock getUptimePercentage function
|
||||
let uptimePercentageStub;
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
uptimePercentageStub = sinon.stub();
|
||||
uptimePercentageStub.returns(95); // Default return value
|
||||
});
|
||||
|
||||
it("should calculate stats correctly for a group of checks", () => {
|
||||
it("should calculate stats correctly for a group of checks", function() {
|
||||
const mockGroup = {
|
||||
time: "2024-01-15",
|
||||
checks: [
|
||||
@@ -780,7 +796,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle empty checks array", () => {
|
||||
it("should handle empty checks array", function() {
|
||||
const mockGroup = {
|
||||
time: "2024-01-15",
|
||||
checks: [],
|
||||
@@ -797,7 +813,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle missing responseTime values", () => {
|
||||
it("should handle missing responseTime values", function() {
|
||||
const mockGroup = {
|
||||
time: "2024-01-15",
|
||||
checks: [
|
||||
@@ -818,7 +834,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle all checks with status false", () => {
|
||||
it("should handle all checks with status false", function() {
|
||||
const mockGroup = {
|
||||
time: "2024-01-15",
|
||||
checks: [
|
||||
@@ -839,7 +855,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle all checks with status true", () => {
|
||||
it("should handle all checks with status true", function() {
|
||||
const mockGroup = {
|
||||
time: "2024-01-15",
|
||||
checks: [
|
||||
@@ -861,7 +877,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getMonitorStatsById", () => {
|
||||
describe("getMonitorStatsById", function() {
|
||||
const now = new Date();
|
||||
const oneHourAgo = new Date(now - 3600000);
|
||||
const twoHoursAgo = new Date(now - 7200000);
|
||||
@@ -958,18 +974,18 @@ describe("monitorModule", () => {
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
checkFindStub.returns({
|
||||
sort: () => checkDocs,
|
||||
});
|
||||
monitorFindByIdStub.returns(mockMonitor);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should return monitor stats with calculated values, sort order desc", async () => {
|
||||
it("should return monitor stats with calculated values, sort order desc", async function() {
|
||||
req.query.sortOrder = "desc";
|
||||
const result = await getMonitorStatsById(req);
|
||||
expect(result).to.include.keys([
|
||||
@@ -992,7 +1008,8 @@ describe("monitorModule", () => {
|
||||
expect(result.periodUptime).to.be.a("number");
|
||||
expect(result.aggregateData).to.be.an("array");
|
||||
});
|
||||
it("should return monitor stats with calculated values, ping type", async () => {
|
||||
|
||||
it("should return monitor stats with calculated values, ping type", async function() {
|
||||
monitorFindByIdStub.returns(mockMonitorPing);
|
||||
req.query.sortOrder = "desc";
|
||||
const result = await getMonitorStatsById(req);
|
||||
@@ -1016,7 +1033,8 @@ describe("monitorModule", () => {
|
||||
expect(result.periodUptime).to.be.a("number");
|
||||
expect(result.aggregateData).to.be.an("array");
|
||||
});
|
||||
it("should return monitor stats with calculated values, docker type", async () => {
|
||||
|
||||
it("should return monitor stats with calculated values, docker type", async function() {
|
||||
monitorFindByIdStub.returns(mockMonitorDocker);
|
||||
req.query.sortOrder = "desc";
|
||||
const result = await getMonitorStatsById(req);
|
||||
@@ -1040,7 +1058,8 @@ describe("monitorModule", () => {
|
||||
expect(result.periodUptime).to.be.a("number");
|
||||
expect(result.aggregateData).to.be.an("array");
|
||||
});
|
||||
it("should return monitor stats with calculated values", async () => {
|
||||
|
||||
it("should return monitor stats with calculated values", async function() {
|
||||
req.query.sortOrder = "asc";
|
||||
const result = await getMonitorStatsById(req);
|
||||
expect(result).to.include.keys([
|
||||
@@ -1063,7 +1082,8 @@ describe("monitorModule", () => {
|
||||
expect(result.periodUptime).to.be.a("number");
|
||||
expect(result.aggregateData).to.be.an("array");
|
||||
});
|
||||
it("should throw error when monitor is not found", async () => {
|
||||
|
||||
it("should throw error when monitor is not found", async function() {
|
||||
monitorFindByIdStub.returns(Promise.resolve(null));
|
||||
|
||||
const req = {
|
||||
@@ -1081,21 +1101,21 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getMonitorById", () => {
|
||||
describe("getMonitorById", function() {
|
||||
let notificationFindStub;
|
||||
let monitorSaveStub;
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
// Create stubs
|
||||
notificationFindStub = sinon.stub(Notification, "find");
|
||||
monitorSaveStub = sinon.stub().resolves();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should return monitor with notifications when found", async () => {
|
||||
it("should return monitor with notifications when found", async function() {
|
||||
// Arrange
|
||||
const monitorId = "123";
|
||||
const mockMonitor = {
|
||||
@@ -1119,7 +1139,7 @@ describe("monitorModule", () => {
|
||||
expect(monitorSaveStub.calledOnce).to.be.true;
|
||||
});
|
||||
|
||||
it("should throw 404 error when monitor not found", async () => {
|
||||
it("should throw 404 error when monitor not found", async function() {
|
||||
// Arrange
|
||||
const monitorId = "nonexistent";
|
||||
monitorFindByIdStub.resolves(null);
|
||||
@@ -1136,7 +1156,7 @@ describe("monitorModule", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle database errors properly", async () => {
|
||||
it("should handle database errors properly", async function() {
|
||||
// Arrange
|
||||
const monitorId = "123";
|
||||
const dbError = new Error("Database connection failed");
|
||||
@@ -1153,7 +1173,7 @@ describe("monitorModule", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle notification fetch errors", async () => {
|
||||
it("should handle notification fetch errors", async function() {
|
||||
// Arrange
|
||||
const monitorId = "123";
|
||||
const mockMonitor = {
|
||||
@@ -1177,7 +1197,7 @@ describe("monitorModule", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle monitor save errors", async () => {
|
||||
it("should handle monitor save errors", async function() {
|
||||
// Arrange
|
||||
const monitorId = "123";
|
||||
const mockMonitor = {
|
||||
@@ -1201,8 +1221,9 @@ describe("monitorModule", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("getMonitorsAndSummaryByTeamId", () => {
|
||||
it("should return monitors and correct summary counts", async () => {
|
||||
|
||||
describe("getMonitorsAndSummaryByTeamId", function() {
|
||||
it("should return monitors and correct summary counts", async function() {
|
||||
// Arrange
|
||||
const teamId = "team123";
|
||||
const type = "http";
|
||||
@@ -1228,7 +1249,7 @@ describe("monitorModule", () => {
|
||||
expect(monitorFindStub.calledOnceWith({ teamId, type })).to.be.true;
|
||||
});
|
||||
|
||||
it("should return empty results for non-existent team", async () => {
|
||||
it("should return empty results for non-existent team", async function() {
|
||||
// Arrange
|
||||
monitorFindStub.resolves([]);
|
||||
|
||||
@@ -1245,7 +1266,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle database errors", async () => {
|
||||
it("should handle database errors", async function() {
|
||||
// Arrange
|
||||
const error = new Error("Database error");
|
||||
error.service = "MonitorModule";
|
||||
@@ -1263,8 +1284,9 @@ describe("monitorModule", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("getMonitorsByTeamId", () => {
|
||||
beforeEach(() => {
|
||||
|
||||
describe("getMonitorsByTeamId", function() {
|
||||
beforeEach(function() {
|
||||
// Chain stubs for Monitor.find().skip().limit().sort()
|
||||
|
||||
// Stub for CHECK_MODEL_LOOKUP model find
|
||||
@@ -1275,11 +1297,11 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should return monitors with basic query parameters", async () => {
|
||||
it("should return monitors with basic query parameters", async function() {
|
||||
const mockMonitors = [
|
||||
{ _id: "1", type: "http", toObject: () => ({ _id: "1", type: "http" }) },
|
||||
{ _id: "2", type: "ping", toObject: () => ({ _id: "2", type: "ping" }) },
|
||||
@@ -1312,7 +1334,7 @@ describe("monitorModule", () => {
|
||||
expect(result).to.have.property("monitorCount", 2);
|
||||
});
|
||||
|
||||
it("should return monitors with basic query parameters", async () => {
|
||||
it("should return monitors with basic query parameters", async function() {
|
||||
const mockMonitors = [
|
||||
{ _id: "1", type: "http", toObject: () => ({ _id: "1", type: "http" }) },
|
||||
{ _id: "2", type: "ping", toObject: () => ({ _id: "2", type: "ping" }) },
|
||||
@@ -1345,7 +1367,7 @@ describe("monitorModule", () => {
|
||||
expect(result).to.have.property("monitorCount", 2);
|
||||
});
|
||||
|
||||
it("should handle type filter with array input", async () => {
|
||||
it("should handle type filter with array input", async function() {
|
||||
const req = {
|
||||
params: { teamId: "team123" },
|
||||
query: {
|
||||
@@ -1370,7 +1392,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle text search filter", async () => {
|
||||
it("should handle text search filter", async function() {
|
||||
const req = {
|
||||
params: { teamId: "team123" },
|
||||
query: {
|
||||
@@ -1398,7 +1420,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle pagination parameters", async () => {
|
||||
it("should handle pagination parameters", async function() {
|
||||
const req = {
|
||||
params: { teamId: "team123" },
|
||||
query: {
|
||||
@@ -1423,7 +1445,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle sorting parameters", async () => {
|
||||
it("should handle sorting parameters", async function() {
|
||||
const req = {
|
||||
params: { teamId: "team123" },
|
||||
query: {
|
||||
@@ -1450,7 +1472,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should return early when limit is -1", async () => {
|
||||
it("should return early when limit is -1", async function() {
|
||||
// Arrange
|
||||
const req = {
|
||||
params: { teamId: "team123" },
|
||||
@@ -1484,7 +1506,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should normalize checks when normalize parameter is provided", async () => {
|
||||
it("should normalize checks when normalize parameter is provided", async function() {
|
||||
const req = {
|
||||
params: { teamId: "team123" },
|
||||
query: { normalize: "true" },
|
||||
@@ -1508,7 +1530,8 @@ describe("monitorModule", () => {
|
||||
expect(result.monitorCount).to.equal(2);
|
||||
expect(result.monitors).to.have.lengthOf(2);
|
||||
});
|
||||
it("should handle database errors", async () => {
|
||||
|
||||
it("should handle database errors", async function() {
|
||||
const req = {
|
||||
params: { teamId: "team123" },
|
||||
query: {},
|
||||
@@ -1533,8 +1556,9 @@ describe("monitorModule", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("createMonitor", () => {
|
||||
it("should create a monitor without notifications", async () => {
|
||||
|
||||
describe("createMonitor", function() {
|
||||
it("should create a monitor without notifications", async function() {
|
||||
let monitorSaveStub = sinon.stub(Monitor.prototype, "save").resolves();
|
||||
|
||||
const req = {
|
||||
@@ -1558,7 +1582,8 @@ describe("monitorModule", () => {
|
||||
expect(result.name).to.equal(expectedMonitor.name);
|
||||
expect(result.url).to.equal(expectedMonitor.url);
|
||||
});
|
||||
it("should handle database errors", async () => {
|
||||
|
||||
it("should handle database errors", async function() {
|
||||
const req = {
|
||||
body: {
|
||||
name: "Test Monitor",
|
||||
@@ -1575,8 +1600,8 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteMonitor", () => {
|
||||
it("should delete a monitor successfully", async () => {
|
||||
describe("deleteMonitor", function() {
|
||||
it("should delete a monitor successfully", async function() {
|
||||
const monitorId = "123456789";
|
||||
const mockMonitor = {
|
||||
_id: monitorId,
|
||||
@@ -1596,7 +1621,7 @@ describe("monitorModule", () => {
|
||||
sinon.assert.calledWith(monitorFindByIdAndDeleteStub, monitorId);
|
||||
});
|
||||
|
||||
it("should throw error when monitor not found", async () => {
|
||||
it("should throw error when monitor not found", async function() {
|
||||
const monitorId = "nonexistent123";
|
||||
const req = {
|
||||
params: { monitorId },
|
||||
@@ -1614,7 +1639,7 @@ describe("monitorModule", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle database errors", async () => {
|
||||
it("should handle database errors", async function() {
|
||||
const monitorId = "123456789";
|
||||
const req = {
|
||||
params: { monitorId },
|
||||
@@ -1634,8 +1659,8 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteAllMonitors", () => {
|
||||
it("should delete all monitors for a team successfully", async () => {
|
||||
describe("deleteAllMonitors", function() {
|
||||
it("should delete all monitors for a team successfully", async function() {
|
||||
const teamId = "team123";
|
||||
const mockMonitors = [
|
||||
{ _id: "1", name: "Monitor 1", teamId },
|
||||
@@ -1655,7 +1680,7 @@ describe("monitorModule", () => {
|
||||
sinon.assert.calledWith(monitorDeleteManyStub, { teamId });
|
||||
});
|
||||
|
||||
it("should return empty array when no monitors found", async () => {
|
||||
it("should return empty array when no monitors found", async function() {
|
||||
const teamId = "emptyTeam";
|
||||
|
||||
monitorFindStub.resolves([]);
|
||||
@@ -1671,7 +1696,7 @@ describe("monitorModule", () => {
|
||||
sinon.assert.calledWith(monitorDeleteManyStub, { teamId });
|
||||
});
|
||||
|
||||
it("should handle database errors", async () => {
|
||||
it("should handle database errors", async function() {
|
||||
const teamId = "team123";
|
||||
const dbError = new Error("Database connection error");
|
||||
monitorFindStub.rejects(dbError);
|
||||
@@ -1685,7 +1710,7 @@ describe("monitorModule", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle deleteMany errors", async () => {
|
||||
it("should handle deleteMany errors", async function() {
|
||||
const teamId = "team123";
|
||||
monitorFindStub.resolves([{ _id: "1", name: "Monitor 1" }]);
|
||||
monitorDeleteManyStub.rejects(new Error("Delete operation failed"));
|
||||
@@ -1700,14 +1725,14 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteMonitorsByUserId", () => {
|
||||
beforeEach(() => {});
|
||||
describe("deleteMonitorsByUserId", function() {
|
||||
beforeEach(function() {});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should delete all monitors for a user successfully", async () => {
|
||||
it("should delete all monitors for a user successfully", async function() {
|
||||
// Arrange
|
||||
const userId = "user123";
|
||||
const mockResult = {
|
||||
@@ -1725,7 +1750,7 @@ describe("monitorModule", () => {
|
||||
sinon.assert.calledWith(monitorDeleteManyStub, { userId: userId });
|
||||
});
|
||||
|
||||
it("should return zero deletedCount when no monitors found", async () => {
|
||||
it("should return zero deletedCount when no monitors found", async function() {
|
||||
// Arrange
|
||||
const userId = "nonexistentUser";
|
||||
const mockResult = {
|
||||
@@ -1743,7 +1768,7 @@ describe("monitorModule", () => {
|
||||
sinon.assert.calledWith(monitorDeleteManyStub, { userId: userId });
|
||||
});
|
||||
|
||||
it("should handle database errors", async () => {
|
||||
it("should handle database errors", async function() {
|
||||
// Arrange
|
||||
const userId = "user123";
|
||||
const dbError = new Error("Database connection error");
|
||||
@@ -1761,8 +1786,8 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("editMonitor", () => {
|
||||
it("should edit a monitor successfully", async () => {
|
||||
describe("editMonitor", function() {
|
||||
it("should edit a monitor successfully", async function() {
|
||||
// Arrange
|
||||
const candidateId = "monitor123";
|
||||
const candidateMonitor = {
|
||||
@@ -1801,7 +1826,7 @@ describe("monitorModule", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("should return null when monitor not found", async () => {
|
||||
it("should return null when monitor not found", async function() {
|
||||
// Arrange
|
||||
const candidateId = "nonexistent123";
|
||||
const candidateMonitor = {
|
||||
@@ -1823,7 +1848,7 @@ describe("monitorModule", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("should remove notifications from update data", async () => {
|
||||
it("should remove notifications from update data", async function() {
|
||||
// Arrange
|
||||
const candidateId = "monitor123";
|
||||
const candidateMonitor = {
|
||||
@@ -1855,7 +1880,7 @@ describe("monitorModule", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("should handle database errors", async () => {
|
||||
it("should handle database errors", async function() {
|
||||
// Arrange
|
||||
const candidateId = "monitor123";
|
||||
const candidateMonitor = {
|
||||
@@ -1877,8 +1902,8 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("addDemoMonitors", () => {
|
||||
it("should add demo monitors successfully", async () => {
|
||||
describe("addDemoMonitors", function() {
|
||||
it("should add demo monitors successfully", async function() {
|
||||
// Arrange
|
||||
const userId = "user123";
|
||||
const teamId = "team123";
|
||||
@@ -1887,7 +1912,7 @@ describe("monitorModule", () => {
|
||||
expect(result).to.deep.equal([{ _id: "123" }]);
|
||||
});
|
||||
|
||||
it("should handle database errors", async () => {
|
||||
it("should handle database errors", async function() {
|
||||
const userId = "user123";
|
||||
const teamId = "team123";
|
||||
|
||||
|
||||
Reference in new issue
Block a user