From a3eaed8678fcd8a4aabc68f5143a6126d0971c3c Mon Sep 17 00:00:00 2001 From: Zaid-maker Date: Wed, 20 Nov 2024 19:12:46 +0500 Subject: [PATCH] Fix linting --- config/vitest.config.js | 16 ++++---- test/backend-test/test-dns-monitor.js | 42 ++++++++++++++++----- test/backend-test/test-notification.js | 52 +++++++++++++++++++------- test/component/MonitorList.spec.js | 4 +- test/component/PingChart.spec.js | 9 +++++ test/component/Status.spec.js | 48 +++++++++++++++++++++--- 6 files changed, 132 insertions(+), 39 deletions(-) diff --git a/config/vitest.config.js b/config/vitest.config.js index dead5a1f6..01124f0b9 100644 --- a/config/vitest.config.js +++ b/config/vitest.config.js @@ -1,21 +1,21 @@ -import { defineConfig } from 'vite'; -import vue from '@vitejs/plugin-vue'; -import { fileURLToPath } from 'url'; -import { dirname, resolve } from 'path'; +import { defineConfig } from "vite"; +import vue from "@vitejs/plugin-vue"; +import { fileURLToPath } from "url"; +import { dirname, resolve } from "path"; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); export default defineConfig({ - plugins: [vue()], + plugins: [ vue() ], test: { globals: true, - environment: 'jsdom', - setupFiles: ['./test/component/setup.js'], + environment: "jsdom", + setupFiles: [ "./test/component/setup.js" ], }, resolve: { alias: { - '@': resolve(__dirname, '../src'), + "@": resolve(__dirname, "../src"), }, }, }); diff --git a/test/backend-test/test-dns-monitor.js b/test/backend-test/test-dns-monitor.js index 7f0307ea7..989bf279d 100644 --- a/test/backend-test/test-dns-monitor.js +++ b/test/backend-test/test-dns-monitor.js @@ -4,6 +4,30 @@ const { DnsMonitorType } = require("../../server/monitor-types/dns"); const { UP, DOWN } = require("../../src/util"); const dayjs = require("dayjs"); +test("DNSMonitor - Basic Creation Test", async (t) => { + const monitor = new DnsMonitorType(); + assert.ok(monitor, "Should create monitor instance"); +}); + +test("DNSMonitor - Status Test", async (t) => { + const monitor = new DnsMonitorType(); + + // Test UP status + monitor.status = UP; + assert.strictEqual(monitor.status, UP, "Should set UP status"); + + // Test DOWN status + monitor.status = DOWN; + assert.strictEqual(monitor.status, DOWN, "Should set DOWN status"); +}); + +test("DNSMonitor - Timestamp Test", async (t) => { + const monitor = new DnsMonitorType(); + const now = dayjs(); + monitor.timestamp = now; + assert.strictEqual(monitor.timestamp.valueOf(), now.valueOf(), "Should set timestamp correctly"); +}); + test("DNS Monitor - Basic A Record Test", async (t) => { const monitor = { hostname: "example.com", @@ -11,14 +35,14 @@ test("DNS Monitor - Basic A Record Test", async (t) => { port: 53, dns_resolve_type: "A" }; - + const heartbeat = { ping: 0 }; const dnsMonitor = new DnsMonitorType(); await dnsMonitor.check(monitor, heartbeat); - + assert.ok(heartbeat.ping > 0, "Ping should be recorded"); assert.ok(Array.isArray(heartbeat.dnsRecords), "DNS records should be an array"); }); @@ -30,7 +54,7 @@ test("DNS Monitor - Invalid Domain Test", async (t) => { port: 53, dns_resolve_type: "A" }; - + const heartbeat = { ping: 0 }; @@ -51,14 +75,14 @@ test("DNS Monitor - Custom DNS Server Test", async (t) => { port: 53, dns_resolve_type: "A" }; - + const heartbeat = { ping: 0 }; const dnsMonitor = new DnsMonitorType(); await dnsMonitor.check(monitor, heartbeat); - + assert.ok(heartbeat.ping > 0, "Ping should be recorded"); }); @@ -69,14 +93,14 @@ test("DNS Monitor - TXT Record Test", async (t) => { port: 53, dns_resolve_type: "TXT" }; - + const heartbeat = { ping: 0 }; const dnsMonitor = new DnsMonitorType(); await dnsMonitor.check(monitor, heartbeat); - + assert.ok(heartbeat.ping > 0, "Ping should be recorded"); assert.ok(Array.isArray(heartbeat.dnsRecords), "DNS records should be an array"); }); @@ -93,13 +117,13 @@ test("DNS Monitor - Condition Evaluation Test", async (t) => { value: "93.184.216.34" // example.com's IP (this might change) }] }; - + const heartbeat = { ping: 0 }; const dnsMonitor = new DnsMonitorType(); await dnsMonitor.check(monitor, heartbeat); - + assert.ok(heartbeat.ping > 0, "Ping should be recorded"); }); diff --git a/test/backend-test/test-notification.js b/test/backend-test/test-notification.js index aa0ca22d5..16f0b75bd 100644 --- a/test/backend-test/test-notification.js +++ b/test/backend-test/test-notification.js @@ -11,12 +11,12 @@ test("Notification - Basic Creation Test", async (t) => { test("Notification - Format Message Test", async (t) => { const notification = new Notification(); - + const monitor = { name: "Test Monitor", hostname: "example.com" }; - + const msg = { type: "down", monitor, @@ -29,28 +29,52 @@ test("Notification - Format Message Test", async (t) => { assert.ok(formatted.includes("Connection failed"), "Should include error message"); }); +test("Notification - Status Test", async (t) => { + const notification = new Notification(); + + // Test UP status + const upMsg = { + type: "up", + monitor: { name: "Test1" }, + msg: "Service is up", + status: UP + }; + const upFormatted = notification.format(upMsg); + assert.ok(upFormatted.includes("up"), "Should indicate UP status"); + + // Test DOWN status + const downMsg = { + type: "down", + monitor: { name: "Test2" }, + msg: "Service is down", + status: DOWN + }; + const downFormatted = notification.format(downMsg); + assert.ok(downFormatted.includes("down"), "Should indicate DOWN status"); +}); + test("Notification - Queue Management Test", async (t) => { const notification = new Notification(); - + // Add items to queue notification.add({ type: "down", monitor: { name: "Test1" }, msg: "Error 1" }); - + notification.add({ type: "up", monitor: { name: "Test2" }, msg: "Recovered" }); - + assert.strictEqual(notification.queue.length, 2, "Queue should have 2 items"); }); test("Notification - Priority Test", async (t) => { const notification = new Notification(); - + // Add items with different priorities notification.add({ type: "down", @@ -58,21 +82,21 @@ test("Notification - Priority Test", async (t) => { msg: "Critical Error", priority: "high" }); - + notification.add({ type: "down", monitor: { name: "Test2" }, msg: "Warning", priority: "low" }); - + const nextItem = notification.queue[0]; assert.strictEqual(nextItem.priority, "high", "High priority item should be first"); }); test("Notification - Retry Logic Test", async (t) => { const notification = new Notification(); - + const testMsg = { type: "down", monitor: { name: "Test1" }, @@ -80,9 +104,9 @@ test("Notification - Retry Logic Test", async (t) => { retries: 0, maxRetries: 3 }; - + notification.add(testMsg); - + // Simulate failed send try { await notification.send(testMsg); @@ -95,7 +119,7 @@ test("Notification - Retry Logic Test", async (t) => { test("Notification - Rate Limiting Test", async (t) => { const notification = new Notification(); const monitor = { name: "Test Monitor" }; - + // Add multiple notifications for same monitor for (let i = 0; i < 5; i++) { notification.add({ @@ -104,11 +128,11 @@ test("Notification - Rate Limiting Test", async (t) => { msg: `Error ${i}` }); } - + // Check if rate limiting is applied const processedCount = notification.queue.filter( item => item.monitor.name === "Test Monitor" ).length; - + assert.ok(processedCount < 5, "Should apply rate limiting"); }); diff --git a/test/component/MonitorList.spec.js b/test/component/MonitorList.spec.js index f80747e47..36d27a24c 100644 --- a/test/component/MonitorList.spec.js +++ b/test/component/MonitorList.spec.js @@ -73,7 +73,7 @@ describe("MonitorList.vue", () => { MonitorListItem: { name: "MonitorListItem", template: "
", - props: ["active"] + props: [ "active" ] }, Confirm: true, MonitorListFilter: true, @@ -93,7 +93,7 @@ describe("MonitorList.vue", () => { const items = wrapper.findAll("[data-testid='monitor-list'] .monitor-list-item"); await items[0].trigger("click"); expect(wrapper.emitted("select-monitor")).toBeTruthy(); - expect(wrapper.emitted("select-monitor")[0]).toEqual([1]); + expect(wrapper.emitted("select-monitor")[0]).toEqual([ 1 ]); }); it("applies active class to selected monitor", async () => { diff --git a/test/component/PingChart.spec.js b/test/component/PingChart.spec.js index 6e3dacb34..e921352ce 100644 --- a/test/component/PingChart.spec.js +++ b/test/component/PingChart.spec.js @@ -1,6 +1,7 @@ import { describe, it, expect, beforeEach, vi } from "vitest"; import { mount } from "@vue/test-utils"; import PingChart from "../../src/components/PingChart.vue"; +import { Line } from "vue-chartjs"; // Mock Chart.js vi.mock("chart.js", () => ({ @@ -8,6 +9,14 @@ vi.mock("chart.js", () => ({ registerables: [] })); +// Mock vue-chartjs +vi.mock("vue-chartjs", () => ({ + Line: { + name: "Line", + template: "" + } +})); + describe("PingChart.vue", () => { let wrapper; const mockMonitorId = 1; diff --git a/test/component/Status.spec.js b/test/component/Status.spec.js index 974a3fad0..b69f89583 100644 --- a/test/component/Status.spec.js +++ b/test/component/Status.spec.js @@ -18,25 +18,25 @@ describe("Status.vue", () => { }; it("renders UP status correctly", () => { - const wrapper = mountStatus(1); // UP status + const wrapper = mountStatus(UP); // UP status expect(wrapper.find(".badge").classes()).toContain("bg-primary"); expect(wrapper.text()).toBe("Up"); }); it("renders DOWN status correctly", () => { - const wrapper = mountStatus(0); // DOWN status + const wrapper = mountStatus(DOWN); // DOWN status expect(wrapper.find(".badge").classes()).toContain("bg-danger"); expect(wrapper.text()).toBe("Down"); }); it("renders PENDING status correctly", () => { - const wrapper = mountStatus(2); // PENDING status + const wrapper = mountStatus(PENDING); // PENDING status expect(wrapper.find(".badge").classes()).toContain("bg-warning"); expect(wrapper.text()).toBe("Pending"); }); it("renders MAINTENANCE status correctly", () => { - const wrapper = mountStatus(3); // MAINTENANCE status + const wrapper = mountStatus(MAINTENANCE); // MAINTENANCE status expect(wrapper.find(".badge").classes()).toContain("bg-maintenance"); expect(wrapper.text()).toBe("statusMaintenance"); }); @@ -48,10 +48,46 @@ describe("Status.vue", () => { }); it("updates when status prop changes", async () => { - const wrapper = mountStatus(1); // UP status + const wrapper = mountStatus(UP); // UP status expect(wrapper.find(".badge").classes()).toContain("bg-primary"); - await wrapper.setProps({ status: 0 }); // Change to DOWN status + await wrapper.setProps({ status: DOWN }); // Change to DOWN status expect(wrapper.find(".badge").classes()).toContain("bg-danger"); }); + + it("displays correct status classes", async () => { + // Test UP status + const wrapper = mountStatus(UP); + expect(wrapper.find(".badge").classes()).toContain("bg-primary"); + + // Test DOWN status + await wrapper.setProps({ status: DOWN }); + expect(wrapper.find(".badge").classes()).toContain("bg-danger"); + + // Test PENDING status + await wrapper.setProps({ status: PENDING }); + expect(wrapper.find(".badge").classes()).toContain("bg-warning"); + + // Test MAINTENANCE status + await wrapper.setProps({ status: MAINTENANCE }); + expect(wrapper.find(".badge").classes()).toContain("bg-maintenance"); + }); + + it("displays correct status text", async () => { + // Test UP status + const wrapper = mountStatus(UP); + expect(wrapper.text()).toBe("Up"); + + // Test DOWN status + await wrapper.setProps({ status: DOWN }); + expect(wrapper.text()).toBe("Down"); + + // Test PENDING status + await wrapper.setProps({ status: PENDING }); + expect(wrapper.text()).toBe("Pending"); + + // Test MAINTENANCE status + await wrapper.setProps({ status: MAINTENANCE }); + expect(wrapper.text()).toBe("statusMaintenance"); + }); });