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

Code before:
const { execSync } = require("node:child_process");

let isSystemd = false;
let isWindows = process.platform === "win32";

if (process.platform === "linux") {
    try {
        // Check if PID 1 is systemd (or init which maps to systemd)
        const pid1Comm = execSync("ps -p 1 -o comm=", { encoding: "utf-8" }).trim();
        if (pid1Comm === "systemd" || pid1Comm === "init") {
            isSystemd = true;
        }
    } catch (e) {
        // Command failed, likely not systemd
    }
}

const shouldRun = isWindows || isSystemd;

// With Linux and no Systemd (ARM64), the test is skipped.
if (process.platform === "linux" && !isSystemd) {
    console.log("::warning title=Systemd Missing::Linux environment detected without systemd (PID 1).");
    console.log("Skipping System Service test for ARM64 runner or containers.");
}

describe("SystemServiceMonitorType", { skip: !shouldRun }, () => {
    let monitorType;
    let heartbeat;
    let originalPlatform;

    beforeEach(() => {
        monitorType = new SystemServiceMonitorType();
        heartbeat = {
            status: DOWN,
            msg: "",
        };
        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 = {

Review comment (refactor):
lets simplify how this is to read

```suggestion
function shouldSkip() {
    if (process.platform === "win32") return false;
    if (process.platform !== "linux") return true;

    // We currently only support systemd as an init system on linux
    // -> Check if PID 1 is systemd (or init which maps to systemd)
    try {
        const pid1Comm = execSync("ps -p 1 -o comm=", { encoding: "utf-8" }).trim();
        return !["systemd", "init"].includes(pid1);
    } catch (e) {
        return true;
    }
}

describe("SystemServiceMonitorType", { skip: shouldSkip() }, () => {
```

Code after:
const { execSync } = require("node:child_process");

/**
 * Check if the test should be skipped.
 * @returns {boolean} True if the test should be skipped
 */
function shouldSkip() {
    if (process.platform === "win32") {
        return false;
    }
    if (process.platform !== "linux") {
        return true;
    }

    // We currently only support systemd as an init system on linux
    // -> Check if PID 1 is systemd (or init which maps to systemd)
    try {
        const pid1Comm = execSync("ps -p 1 -o comm=", { encoding: "utf-8" }).trim();
        return ![ "systemd", "init" ].includes(pid1Comm);
    } catch (e) {
        return true;
    }
}

describe("SystemServiceMonitorType", { skip: shouldSkip() }, () => {
    let monitorType;
    let heartbeat;
    let originalPlatform;

    beforeEach(() => {
        monitorType = new SystemServiceMonitorType();
        heartbeat = {
            status: DOWN,
            msg: "",
        };
        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,