build error
This commit is contained in:
1 parent
9255500bfc
commit
8aac3086b8
2 files changed
+76
-57
No files matched your search
@@ -229,6 +229,7 @@ export const initializeServices = async ({
|
|||||||
jwt,
|
jwt,
|
||||||
errorService,
|
errorService,
|
||||||
jobQueue: superSimpleQueue,
|
jobQueue: superSimpleQueue,
|
||||||
|
monitorsRepository,
|
||||||
});
|
});
|
||||||
const checkService = new CheckService({
|
const checkService = new CheckService({
|
||||||
db,
|
db,
|
||||||
|
|||||||
@@ -1,12 +1,19 @@
|
|||||||
import { jest } from "@jest/globals";
|
import { jest } from "@jest/globals";
|
||||||
import { MonitorService } from "../src/service/business/monitorService.ts";
|
import { MonitorService } from "../src/service/business/monitorService.ts";
|
||||||
import type { IMonitorsRepository, IChecksRepository } from "../src/repositories/index.ts";
|
import type {
|
||||||
|
IChecksRepository,
|
||||||
|
IMonitorStatsRepository,
|
||||||
|
IMonitorsRepository,
|
||||||
|
IStatusPagesRepository,
|
||||||
|
} from "../src/repositories/index.ts";
|
||||||
|
|
||||||
const createMonitorsRepositoryMock = () =>
|
const createMonitorsRepositoryMock = () =>
|
||||||
({
|
({
|
||||||
findMonitorCountByTeamIdAndType: jest.fn(),
|
findMonitorCountByTeamIdAndType: jest.fn(),
|
||||||
findByTeamId: jest.fn(),
|
findByTeamId: jest.fn(),
|
||||||
findById: jest.fn(),
|
findById: jest.fn(),
|
||||||
|
findMonitorsSummaryByTeamId: jest.fn(),
|
||||||
|
findGroupsByTeamId: jest.fn(),
|
||||||
create: jest.fn(),
|
create: jest.fn(),
|
||||||
createBulkMonitors: jest.fn(),
|
createBulkMonitors: jest.fn(),
|
||||||
deleteByTeamId: jest.fn(),
|
deleteByTeamId: jest.fn(),
|
||||||
@@ -18,32 +25,34 @@ const createChecksRepositoryMock = () =>
|
|||||||
findDateRangeChecksByMonitor: jest.fn(),
|
findDateRangeChecksByMonitor: jest.fn(),
|
||||||
}) as unknown as IChecksRepository;
|
}) as unknown as IChecksRepository;
|
||||||
|
|
||||||
|
const createMonitorStatsRepositoryMock = () =>
|
||||||
|
({
|
||||||
|
findByMonitorId: jest.fn(),
|
||||||
|
deleteByMonitorId: jest.fn(),
|
||||||
|
}) as unknown as IMonitorStatsRepository;
|
||||||
|
|
||||||
|
const createStatusPagesRepositoryMock = () =>
|
||||||
|
({
|
||||||
|
removeMonitorFromStatusPages: jest.fn(),
|
||||||
|
}) as unknown as IStatusPagesRepository;
|
||||||
|
|
||||||
const createService = ({
|
const createService = ({
|
||||||
monitorsRepository = createMonitorsRepositoryMock(),
|
monitorsRepository = createMonitorsRepositoryMock(),
|
||||||
checksRepository = createChecksRepositoryMock(),
|
checksRepository = createChecksRepositoryMock(),
|
||||||
monitorStatsRepository = { findByMonitorId: jest.fn() },
|
monitorStatsRepository = createMonitorStatsRepositoryMock(),
|
||||||
monitorModuleOverrides = {},
|
statusPagesRepository = createStatusPagesRepositoryMock(),
|
||||||
}: {
|
}: {
|
||||||
monitorsRepository?: IMonitorsRepository;
|
monitorsRepository?: IMonitorsRepository;
|
||||||
checksRepository?: IChecksRepository;
|
checksRepository?: IChecksRepository;
|
||||||
monitorStatsRepository?: { findByMonitorId: jest.Mock };
|
monitorStatsRepository?: IMonitorStatsRepository;
|
||||||
monitorModuleOverrides?: Record<string, unknown>;
|
statusPagesRepository?: IStatusPagesRepository;
|
||||||
} = {}) => {
|
} = {}) => {
|
||||||
const monitorModule = {
|
|
||||||
getMonitorById: jest.fn().mockResolvedValue({ teamId: { equals: () => true } }),
|
|
||||||
getMonitorStatsById: jest.fn().mockResolvedValue({ latest: {} }),
|
|
||||||
getMonitorsByTeamId: jest.fn().mockResolvedValue([]),
|
|
||||||
getMonitorsAndSummaryByTeamId: jest.fn().mockResolvedValue({ monitors: [], summary: {} }),
|
|
||||||
...monitorModuleOverrides,
|
|
||||||
};
|
|
||||||
|
|
||||||
return new MonitorService({
|
return new MonitorService({
|
||||||
db: {
|
db: {
|
||||||
monitorModule,
|
|
||||||
statusPageModule: { deleteStatusPagesByMonitorId: jest.fn() },
|
|
||||||
checkModule: { deleteChecks: jest.fn() },
|
checkModule: { deleteChecks: jest.fn() },
|
||||||
|
statusPageModule: { deleteStatusPagesByMonitorId: jest.fn() },
|
||||||
pageSpeedCheckModule: { deletePageSpeedChecksByMonitorId: jest.fn() },
|
pageSpeedCheckModule: { deletePageSpeedChecksByMonitorId: jest.fn() },
|
||||||
notificationsModule: { deleteNotificationsByMonitorId: jest.fn() },
|
notificationModule: { deleteNotificationsByMonitorId: jest.fn() },
|
||||||
},
|
},
|
||||||
jobQueue: {
|
jobQueue: {
|
||||||
addJob: jest.fn(),
|
addJob: jest.fn(),
|
||||||
@@ -66,6 +75,7 @@ const createService = ({
|
|||||||
monitorsRepository,
|
monitorsRepository,
|
||||||
checksRepository,
|
checksRepository,
|
||||||
monitorStatsRepository,
|
monitorStatsRepository,
|
||||||
|
statusPagesRepository,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -79,17 +89,17 @@ describe("MonitorService", () => {
|
|||||||
{ id: "m2", name: "Monitor 2", interval: 60000 },
|
{ id: "m2", name: "Monitor 2", interval: 60000 },
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const checksRepository = createChecksRepositoryMock();
|
const checksRepository = createChecksRepositoryMock();
|
||||||
(checksRepository.findLatestChecksByMonitorIds as jest.Mock).mockResolvedValue({
|
(checksRepository.findLatestChecksByMonitorIds as jest.Mock).mockResolvedValue({
|
||||||
m1: [
|
m1: [
|
||||||
{ responseTime: 10, status: true, message: "OK" },
|
{ responseTime: 10, status: true, message: "OK" },
|
||||||
{ responseTime: 20, status: true, message: "OK" },
|
{ responseTime: 20, status: true, message: "OK" },
|
||||||
],
|
],
|
||||||
m2: [{ responseTime: 50, status: true, message: "OK" }],
|
m2: [{ responseTime: 50, status: true, message: "OK" }],
|
||||||
});
|
});
|
||||||
|
|
||||||
const service = createService({ monitorsRepository, checksRepository });
|
const service = createService({ monitorsRepository, checksRepository });
|
||||||
const result = await service.getMonitorsWithChecksByTeamId({ teamId: "team" });
|
const result = await service.getMonitorsWithChecksByTeamId({ teamId: "team" });
|
||||||
|
|
||||||
expect(result).toMatchObject({ count: 2 });
|
expect(result).toMatchObject({ count: 2 });
|
||||||
expect(result.monitors).toHaveLength(2);
|
expect(result.monitors).toHaveLength(2);
|
||||||
@@ -106,29 +116,41 @@ describe("MonitorService", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("getMonitorsByTeamId", () => {
|
describe("getMonitorsByTeamId", () => {
|
||||||
it("returns monitors array from db module", async () => {
|
it("returns monitors array from repository", async () => {
|
||||||
const monitorsPayload = [
|
const monitorsRepository = createMonitorsRepositoryMock();
|
||||||
|
(monitorsRepository.findByTeamId as jest.Mock).mockResolvedValue([
|
||||||
{ id: "m1", name: "Monitor 1" },
|
{ id: "m1", name: "Monitor 1" },
|
||||||
{ id: "m2", name: "Monitor 2" },
|
{ id: "m2", name: "Monitor 2" },
|
||||||
];
|
]);
|
||||||
const monitorModuleOverrides = {
|
const service = createService({ monitorsRepository });
|
||||||
getMonitorsByTeamId: jest.fn().mockResolvedValue(monitorsPayload),
|
|
||||||
};
|
|
||||||
const service = createService({ monitorModuleOverrides });
|
|
||||||
const result = await service.getMonitorsByTeamId({ teamId: "team" } as any);
|
const result = await service.getMonitorsByTeamId({ teamId: "team" } as any);
|
||||||
expect(result).toHaveLength(2);
|
expect(result).toHaveLength(2);
|
||||||
expect(result[0]).toHaveProperty("id", "m1");
|
expect(result[0]).toHaveProperty("id", "m1");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getMonitorsAndSummaryByTeamId", () => {
|
describe("getMonitorsWithChecksByTeamId summary", () => {
|
||||||
it("returns monitors with summary block", async () => {
|
it("includes summary and monitors with checks", async () => {
|
||||||
const monitorModuleOverrides = {
|
const monitorsRepository = createMonitorsRepositoryMock();
|
||||||
getMonitorsAndSummaryByTeamId: jest.fn().mockResolvedValue({ monitors: [{ id: "m1" }], summary: { total: 1, uptime: 0.99 } }),
|
(monitorsRepository.findMonitorCountByTeamIdAndType as jest.Mock).mockResolvedValue(1);
|
||||||
};
|
(monitorsRepository.findByTeamId as jest.Mock).mockResolvedValue([{ id: "m1", type: "http" }]);
|
||||||
const service = createService({ monitorModuleOverrides });
|
(monitorsRepository.findMonitorsSummaryByTeamId as jest.Mock).mockResolvedValue({
|
||||||
const result = await service.getMonitorsAndSummaryByTeamId({ teamId: "team" });
|
totalMonitors: 1,
|
||||||
expect(result).toEqual({ monitors: [{ id: "m1" }], summary: { total: 1, uptime: 0.99 } });
|
upMonitors: 1,
|
||||||
|
downMonitors: 0,
|
||||||
|
pausedMonitors: 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
const checksRepository = createChecksRepositoryMock();
|
||||||
|
(checksRepository.findLatestChecksByMonitorIds as jest.Mock).mockResolvedValue({ m1: [] });
|
||||||
|
|
||||||
|
const service = createService({ monitorsRepository, checksRepository });
|
||||||
|
const result = await service.getMonitorsWithChecksByTeamId({ teamId: "team" });
|
||||||
|
expect(result).toEqual({
|
||||||
|
summary: { totalMonitors: 1, upMonitors: 1, downMonitors: 0, pausedMonitors: 0 },
|
||||||
|
count: 1,
|
||||||
|
monitors: [{ id: "m1", type: "http", checks: [] }],
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -163,7 +185,7 @@ describe("MonitorService", () => {
|
|||||||
(monitorsRepository.findById as jest.Mock).mockResolvedValue(monitor);
|
(monitorsRepository.findById as jest.Mock).mockResolvedValue(monitor);
|
||||||
const checksRepository = createChecksRepositoryMock();
|
const checksRepository = createChecksRepositoryMock();
|
||||||
(checksRepository.findDateRangeChecksByMonitor as jest.Mock).mockResolvedValue({
|
(checksRepository.findDateRangeChecksByMonitor as jest.Mock).mockResolvedValue({
|
||||||
monitorType: "uptime",
|
monitorType: "http",
|
||||||
groupedChecks: [{ _id: "2024-01-01", avgResponseTime: 100, totalChecks: 2 }],
|
groupedChecks: [{ _id: "2024-01-01", avgResponseTime: 100, totalChecks: 2 }],
|
||||||
groupedUpChecks: [{ _id: "2024-01-01", totalChecks: 2, avgResponseTime: 90 }],
|
groupedUpChecks: [{ _id: "2024-01-01", totalChecks: 2, avgResponseTime: 90 }],
|
||||||
groupedDownChecks: [{ _id: "2024-01-01", totalChecks: 0, avgResponseTime: 0 }],
|
groupedDownChecks: [{ _id: "2024-01-01", totalChecks: 0, avgResponseTime: 0 }],
|
||||||
@@ -171,27 +193,23 @@ describe("MonitorService", () => {
|
|||||||
avgResponseTime: 95,
|
avgResponseTime: 95,
|
||||||
});
|
});
|
||||||
|
|
||||||
const monitorStatsRepository = {
|
const monitorStatsRepository = createMonitorStatsRepositoryMock();
|
||||||
findByMonitorId: jest.fn().mockResolvedValue({
|
(monitorStatsRepository.findByMonitorId as jest.Mock).mockResolvedValue({
|
||||||
id: "stats-1",
|
id: "stats-1",
|
||||||
monitorId: monitor.id,
|
monitorId: monitor.id,
|
||||||
avgResponseTime: 90,
|
avgResponseTime: 90,
|
||||||
totalChecks: 10,
|
totalChecks: 10,
|
||||||
totalUpChecks: 9,
|
totalUpChecks: 9,
|
||||||
totalDownChecks: 1,
|
totalDownChecks: 1,
|
||||||
uptimePercentage: 0.9,
|
uptimePercentage: 0.9,
|
||||||
lastCheckTimestamp: 123456789,
|
lastCheckTimestamp: 123456789,
|
||||||
lastResponseTime: 80,
|
lastResponseTime: 80,
|
||||||
timeOfLastFailure: 123456700,
|
timeOfLastFailure: 123456700,
|
||||||
createdAt: new Date().toISOString(),
|
createdAt: new Date().toISOString(),
|
||||||
updatedAt: new Date().toISOString(),
|
updatedAt: new Date().toISOString(),
|
||||||
}),
|
});
|
||||||
} as any;
|
|
||||||
const monitorModuleOverrides = {
|
|
||||||
getMonitorById: jest.fn().mockResolvedValue({ teamId: { equals: (value: string) => value === TEAM_ID } }),
|
|
||||||
};
|
|
||||||
|
|
||||||
const service = createService({ monitorsRepository, checksRepository, monitorModuleOverrides, monitorStatsRepository });
|
const service = createService({ monitorsRepository, checksRepository, monitorStatsRepository });
|
||||||
const result = await service.getUptimeDetailsById({ teamId: TEAM_ID, monitorId: "monitor-1", dateRange: "recent" });
|
const result = await service.getUptimeDetailsById({ teamId: TEAM_ID, monitorId: "monitor-1", dateRange: "recent" });
|
||||||
|
|
||||||
expect(result).toHaveProperty("monitorData");
|
expect(result).toHaveProperty("monitorData");
|
||||||
|
|||||||
Reference in new issue
Block a user