add tests

This commit is contained in:
Alex Holliday committed 2025-11-14 19:10:34 +00:00
1 parent b4da045770
commit c9d33c4292
3 files changed
+215 -14

No files matched your search

+3 -1
View File
@@ -1,3 +1,5 @@
import got from "got";
import {
CheckService,
InviteService,
@@ -38,7 +40,7 @@ export const initServices = async () => {
const emailService = new EmailService(userService);
const slackService = new SlackService();
const webhookService = new WebhookService();
const networkService = new NetworkService();
const networkService = new NetworkService(got);
const statusService = new StatusService();
const notificationService = new NotificationService(userService);
const incidentService = new IncidentService();
@@ -1,5 +1,4 @@
import { Got, HTTPError } from "got";
import got from "got";
import ping from "ping";
import { IMonitor } from "@/db/models/index.js";
import { GotTimings } from "@/db/models/checks/Check.js";
@@ -48,15 +47,15 @@ export interface StatusResponse<TPayload = unknown> {
class NetworkService implements INetworkService {
public SERVICE_NAME = SERVICE_NAME;
private got: Got;
private client: Got;
private NETWORK_ERROR: number;
constructor() {
constructor(got: Got) {
this.SERVICE_NAME = SERVICE_NAME;
this.NETWORK_ERROR = 5000;
const cacheable = new CacheableLookup();
this.got = got.extend({
this.client = got.extend({
dnsCache: cacheable,
timeout: {
request: 30000,
@@ -112,7 +111,7 @@ class NetworkService implements INetworkService {
}
try {
const response: Response = await this.got(url);
const response: Response = await this.client(url);
return this.buildStatusResponse(monitor, response, null);
} catch (error) {
return this.buildStatusResponse(monitor, null, error);
@@ -134,10 +133,13 @@ class NetworkService implements INetworkService {
let statusResponse: StatusResponse<ICapturePayload>;
try {
const response: Response<ICapturePayload> | null = await this.got(url, {
headers: { Authorization: `Bearer ${secret}` },
responseType: "json",
});
const response: Response<ICapturePayload> | null = await this.client(
url,
{
headers: { Authorization: `Bearer ${secret}` },
responseType: "json",
}
);
statusResponse = this.buildStatusResponse(monitor, response, null);
if (!response?.body) {
@@ -167,7 +169,7 @@ class NetworkService implements INetworkService {
let statusResponse: StatusResponse<ILighthousePayload>;
try {
const response: Response = await this.got(url);
const response: Response = await this.client(url);
statusResponse = this.buildStatusResponse(
monitor,
response,
@@ -178,9 +180,12 @@ class NetworkService implements INetworkService {
}
const pagespeedUrl = `https://pagespeedonline.googleapis.com/pagespeedonline/v5/runPagespeed?url=${url}&category=seo&category=accessibility&category=best-practices&category=performance&key=${apiKey}`;
const pagespeedResponse = await this.got<ILighthousePayload>(pagespeedUrl, {
responseType: "json",
});
const pagespeedResponse = await this.client<ILighthousePayload>(
pagespeedUrl,
{
responseType: "json",
}
);
const payload = pagespeedResponse.body;
if (payload) {
statusResponse.payload = payload;