update tests

This commit is contained in:
Alex Holliday committed 2024-10-29 13:51:57 +08:00
1 parent a8060a72ab
commit e940a0d151
2 files changed
+82 -9

No files matched your search

+62 -9
View File
@@ -2,7 +2,6 @@ import sinon from "sinon";
import NetworkService from "../../service/networkService.js";
import { expect } from "chai";
import http from "http";
import exp from "constants";
describe("Network Service", () => {
let axios, ping, logger, networkService;
@@ -74,7 +73,7 @@ describe("Network Service", () => {
});
describe("requestHttp", () => {
it("should return a response object if http successful", async () => {
const job = { data: { url: "http://test.com", _id: "123" } };
const job = { data: { url: "http://test.com", _id: "123", type: "http" } };
const httpResult = await networkService.requestHttp(job);
expect(httpResult.monitorId).to.equal("123");
expect(httpResult.type).to.equal("http");
@@ -87,7 +86,7 @@ describe("Network Service", () => {
networkService.timeRequest = sinon
.stub()
.resolves({ response: null, responseTime: 1, error });
const job = { data: { url: "http://test.com", _id: "123" } };
const job = { data: { url: "http://test.com", _id: "123", type: "http" } };
const httpResult = await networkService.requestHttp(job);
expect(httpResult.monitorId).to.equal("123");
expect(httpResult.type).to.equal("http");
@@ -101,7 +100,7 @@ describe("Network Service", () => {
networkService.timeRequest = sinon
.stub()
.resolves({ response: null, responseTime: 1, error });
const job = { data: { url: "http://test.com", _id: "123" } };
const job = { data: { url: "http://test.com", _id: "123", type: "http" } };
const httpResult = await networkService.requestHttp(job);
expect(httpResult.monitorId).to.equal("123");
expect(httpResult.type).to.equal("http");
@@ -113,7 +112,7 @@ describe("Network Service", () => {
describe("requestPagespeed", () => {
it("should return a response object if pagespeed successful", async () => {
const job = { data: { url: "http://test.com", _id: "123" } };
const job = { data: { url: "http://test.com", _id: "123", type: "pagespeed" } };
const pagespeedResult = await networkService.requestPagespeed(job);
expect(pagespeedResult.monitorId).to.equal("123");
expect(pagespeedResult.type).to.equal("pagespeed");
@@ -126,7 +125,7 @@ describe("Network Service", () => {
networkService.timeRequest = sinon
.stub()
.resolves({ response: null, responseTime: 1, error });
const job = { data: { url: "http://test.com", _id: "123" } };
const job = { data: { url: "http://test.com", _id: "123", type: "pagespeed" } };
const pagespeedResult = await networkService.requestPagespeed(job);
expect(pagespeedResult.monitorId).to.equal("123");
expect(pagespeedResult.type).to.equal("pagespeed");
@@ -140,7 +139,7 @@ describe("Network Service", () => {
networkService.timeRequest = sinon
.stub()
.resolves({ response: null, responseTime: 1, error });
const job = { data: { url: "http://test.com", _id: "123" } };
const job = { data: { url: "http://test.com", _id: "123", type: "pagespeed" } };
const pagespeedResult = await networkService.requestPagespeed(job);
expect(pagespeedResult.monitorId).to.equal("123");
expect(pagespeedResult.type).to.equal("pagespeed");
@@ -150,6 +149,60 @@ describe("Network Service", () => {
});
});
describe("requestHardware", () => {
it("should return a response object if hardware successful", async () => {
const job = { data: { url: "http://test.com", _id: "123", type: "hardware" } };
const httpResult = await networkService.requestHardware(job);
expect(httpResult.monitorId).to.equal("123");
expect(httpResult.type).to.equal("hardware");
expect(httpResult.responseTime).to.be.a("number");
expect(httpResult.status).to.be.true;
});
it("should return a response object if hardware successful and job jas a secret", async () => {
const job = {
data: {
url: "http://test.com",
_id: "123",
type: "hardware",
secret: "my_secret",
},
};
const httpResult = await networkService.requestHardware(job);
expect(httpResult.monitorId).to.equal("123");
expect(httpResult.type).to.equal("hardware");
expect(httpResult.responseTime).to.be.a("number");
expect(httpResult.status).to.be.true;
});
it("should return a response object if hardware unsuccessful", async () => {
const error = new Error("Test error");
error.response = { status: 404 };
networkService.timeRequest = sinon
.stub()
.resolves({ response: null, responseTime: 1, error });
const job = { data: { url: "http://test.com", _id: "123", type: "hardware" } };
const httpResult = await networkService.requestHardware(job);
expect(httpResult.monitorId).to.equal("123");
expect(httpResult.type).to.equal("hardware");
expect(httpResult.responseTime).to.be.a("number");
expect(httpResult.status).to.be.false;
expect(httpResult.code).to.equal(404);
});
it("should return a response object if hardware unsuccessful with unknown code", async () => {
const error = new Error("Test error");
error.response = {};
networkService.timeRequest = sinon
.stub()
.resolves({ response: null, responseTime: 1, error });
const job = { data: { url: "http://test.com", _id: "123", type: "hardware" } };
const httpResult = await networkService.requestHardware(job);
expect(httpResult.monitorId).to.equal("123");
expect(httpResult.type).to.equal("hardware");
expect(httpResult.responseTime).to.be.a("number");
expect(httpResult.status).to.be.false;
expect(httpResult.code).to.equal(networkService.NETWORK_ERROR);
});
});
describe("getStatus", () => {
beforeEach(() => {
networkService.requestPing = sinon.stub();
@@ -179,10 +232,10 @@ describe("Network Service", () => {
expect(networkService.requestHttp.notCalled).to.be.true;
expect(networkService.requestPagespeed.calledOnce).to.be.true;
});
it("should call requestHandleHardware if type is hardware", () => {
it("should call requestHardware if type is hardware", () => {
networkService.getStatus({ data: { type: "hardware" } });
expect(networkService.requestHardware.calledOnce).to.be.true;
expect(networkService.requestPing.notCalled).to.be.true;
expect(networkService.requestHttp.notCalled).to.be.true;
expect(networkService.requestPagespeed.notCalled).to.be.true;
});
it("should log an error if an unknown type is provided", () => {
@@ -173,6 +173,26 @@ describe("StatusService", () => {
tbt: 0,
});
});
it("should build a check for hardware type", () => {
const check = statusService.buildCheck({
monitorId: "test",
type: "hardware",
status: true,
responseTime: 100,
code: 200,
message: "Test message",
payload: { cpu: "cpu", memory: "memory", disk: "disk", host: "host" },
});
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.equal("cpu");
expect(check.memory).to.equal("memory");
expect(check.disk).to.equal("disk");
expect(check.host).to.equal("host");
});
});
describe("insertCheck", () => {
it("should log an error if one is thrown", async () => {