Repository: louislam/uptime-kuma
File: test/backend-test/test-system-service.js (JavaScript)

Code before:
        };
        originalPlatform = Object.getOwnPropertyDescriptor(process, "platform");
    });

    afterEach(() => {
        if (originalPlatform) {
            Object.defineProperty(process, "platform", originalPlatform);
        }
    });

    it("should detect a running service", async () => {
        // Windows: 'Dnscache' is always running.
        // Linux: 'dbus' or 'cron' are standard services.
        const serviceName = isWindows ? "Dnscache" : "dbus";

        const monitor = {
            system_service_name: serviceName,
        };

        await monitorType.check(monitor, heartbeat);

        assert.strictEqual(heartbeat.status, UP);
        assert.ok(heartbeat.msg.includes("is running"));
    });

    it("should detect a stopped service", async () => {
        const monitor = {
            system_service_name: "non-existent-service-12345",
        };

        // Query a non-existent service to force an error/down state.
        // We pass the promise directly to assert.rejects, avoiding unnecessary async wrappers.
        await assert.rejects(monitorType.check(monitor, heartbeat));

        assert.strictEqual(heartbeat.status, DOWN);
    });

    it("should fail gracefully with invalid characters", async () => {
        // Mock platform for validation logic test
        Object.defineProperty(process, "platform", {
            value: "linux",
            configurable: true,
        });

        const monitor = {
            system_service_name: "invalid&service;name",
        };

        // Expected validation error
        await assert.rejects(monitorType.check(monitor, heartbeat));


Review comment (suggestion):
```suggestion
    test("check() returns DOWN for a stopped service", async () => {
```

Code after:
        originalPlatform = Object.getOwnPropertyDescriptor(process, "platform");
    });

    afterEach(() => {
        if (originalPlatform) {
            Object.defineProperty(process, "platform", originalPlatform);
        }
    });

    test("check() returns UP for a running service", async () => {
        // Windows: 'Dnscache' is always running.
        // Linux: 'dbus' or 'cron' are standard services.
        const serviceName = process.platform === "win32" ? "Dnscache" : "dbus";

        const monitor = {
            system_service_name: serviceName,
        };

        await monitorType.check(monitor, heartbeat);

        assert.strictEqual(heartbeat.status, UP);
        assert.ok(heartbeat.msg.includes("is running"));
    });

    test("check() returns DOWN for a stopped service", async () => {
        const monitor = {
            system_service_name: "non-existent-service-12345",
        };

        // Query a non-existent service to force an error/down state.
        // We pass the promise directly to assert.rejects, avoiding unnecessary async wrappers.
        await assert.rejects(monitorType.check(monitor, heartbeat));

        assert.strictEqual(heartbeat.status, DOWN);
    });

    test("check() fails gracefully with invalid characters", async () => {
        // Mock platform for validation logic test
        Object.defineProperty(process, "platform", {
            value: "linux",
            configurable: true,
        });

        const monitor = {
            system_service_name: "invalid&service;name",
        };

        // Expected validation error
        await assert.rejects(monitorType.check(monitor, heartbeat));

        assert.strictEqual(heartbeat.status, DOWN);