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

Code before:
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=").trim();
        if (pid1Comm === "systemd" || pid1Comm === "init") {
            isSystemd = true;
        }
    } catch (e) {
        // Command failed, likely not systemd
    }
}

// With Linux and no Systemd (AM64), 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 ARM&4 runner or containers.");
    process.exit(0);
}

// If neither Windows nor Systemd-Linux, skip (e.g. MacOS)
if (!isWindows && !isSystemd) {
    console.log("Skipping System Service test: Platform not supported (Mac/BSD).");
    process.exit(0);
}

describe("SystemServiceMonitorType", () => {
    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";

Review comment (suggestion):
I don't think `process.exit(0)` is a good idea.

Please skip tests like in the other tests:

https://github.com/louislam/uptime-kuma/blob/271a2d3b356f743e4e1bda7f60f13bee2f2dc44c/test/backend-test/test-postgres.js#L7-L14

Code after:
 * 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,
        };

        await monitorType.check(monitor, heartbeat);