Repository: louislam/uptime-kuma
File: test/backend-test/test-util-server.js (JavaScript)

Code before:
const { describe, test } = require("node:test");
const assert = require("node:assert");
const { pingAsync } = require("../../server/util-server");

describe("Server Utilities: pingAsync", () => {
    test("should convert IDN domains to Punycode before pinging", async () => {
        try {
            // timeout 1s
            await pingAsync("münchen.de", false, 1, "", true, 56, 1, 1);
        } catch (e) {
            if (e.message.includes("Parameter string not correctly encoded")) {
                assert.fail("Ping failed with encoding error: IDN was not converted");
            }
            assert.ok(
                e.message.includes("xn--mnchen-3ya.de"),
                `Error message should contain "xn--mnchen-3ya.de". Got: ${e.message}`
            );
        }
    });

    test("should strip brackets from IPv6 addresses before pinging", async () => {
        try {
            // Cloudflare DNS
            await pingAsync("[2606:4700:4700::1111]", true, 1, "", true, 56, 1, 1);
        } catch (e) {
            if (e.message.includes("[2606:4700:4700::1111]")) {
                assert.fail(`Error message contained brackets, implying they were not stripped. Got: ${e.message}`);
            }
            // The error message should contain the raw IP without brackets
            assert.ok(
                e.message.includes("2606:4700:4700::1111"),
                `Error message should contain the raw IP "2606:4700:4700::1111". Got: ${e.message}`
            );
        }
    });

    test("should handle standard ASCII domains correctly", async () => {
        try {
            await pingAsync("google.com", false, 1, "", true, 56, 1, 1);
        } catch (e) {
            if (e.message.includes("Parameter string not correctly encoded")) {
                assert.fail("Ping failed with encoding error for ASCII domain");
            }

Review comment (bug):
Please assert using assert.throws if you expect an error.
This way, we actually know that it happened.

Currently, this code might fail or succeed.
This is what I meant by the first part of my previous feedback, could have been clearer.

Code after:
const { describe, test } = require("node:test");
const assert = require("node:assert");
const { pingAsync } = require("../../server/util-server");

describe("Server Utilities: pingAsync", () => {
    test("should convert IDN domains to Punycode before pinging", async () => {
        const idnDomain = "münchen.de";
        const punycodeDomain = "xn--mnchen-3ya.de";

        await assert.rejects(pingAsync(idnDomain, false, 1, "", true, 56, 1, 1), (err) => {
            if (err.message.includes("Parameter string not correctly encoded")) {
                assert.fail("Ping failed with encoding error: IDN was not converted");
            }
            assert.ok(
                err.message.includes(punycodeDomain),
                `Error message should contain the Punycode domain "${punycodeDomain}". Got: ${err.message}`
            );
            return true;
        });
    });

    test("should strip brackets from IPv6 addresses before pinging", async () => {
        const ipv6WithBrackets = "[2606:4700:4700::1111]";
        const ipv6Raw = "2606:4700:4700::1111";

        await assert.rejects(pingAsync(ipv6WithBrackets, true, 1, "", true, 56, 1, 1), (err) => {
            assert.strictEqual(
                err.message.includes(ipv6WithBrackets),
                false,
                "Error message should not contain brackets"
            );
            // Allow either the IP in the message (local) OR "Network is unreachable"
            const containsIP = err.message.includes(ipv6Raw);
            const isUnreachable =
                err.message.includes("Network is unreachable") || err.message.includes("Network unreachable");
            // macOS error when IPv6 stack is missing
            const isMacOSError = err.message.includes("nodename nor servname provided");
            assert.ok(
                containsIP || isUnreachable || isMacOSError,
                `Ping failed correctly, but error message format was unexpected.\nGot: "${err.message}"\nExpected to contain IP "${ipv6Raw}" OR be a standard network error.`
            );
            return true;
        });