Repository: louislam/uptime-kuma
File: test/backend-test/monitors/test-rabbitmq.js (JavaScript)

Code before:

describe("RabbitMQ Multi-Node (Mocked)", () => {
    test("check() succeeds when first node is healthy", async () => {
        const rabbitMQMonitor = new RabbitMqMonitorType();
        const monitor = {
            rabbitmqNodes: JSON.stringify([ "http://node1:15672", "http://node2:15672" ]),
            rabbitmqUsername: "guest",
            rabbitmqPassword: "guest",
            timeout: 10,
        };

        const heartbeat = {
            msg: "",
            status: PENDING,
        };

        // Mock checkSingleNode to succeed on first call (just don't throw)
        let callCount = 0;
        rabbitMQMonitor.checkSingleNode = async (mon, url, nodeInfo) => {
            callCount++;
            // Success - don't throw
        };

        await rabbitMQMonitor.check(monitor, heartbeat, {});
        assert.strictEqual(heartbeat.status, UP);
        assert.strictEqual(heartbeat.msg, "OK");
        assert.strictEqual(callCount, 1, "Should only check first node");
    });

    test("check() succeeds when second node is healthy after first fails", async () => {
        const rabbitMQMonitor = new RabbitMqMonitorType();
        const monitor = {
            rabbitmqNodes: JSON.stringify([ "http://node1:15672", "http://node2:15672" ]),
            rabbitmqUsername: "guest",
            rabbitmqPassword: "guest",
            timeout: 10,
        };

        const heartbeat = {
            msg: "",
            status: PENDING,
        };

        // Mock checkSingleNode to fail first, succeed second
        let callCount = 0;
        rabbitMQMonitor.checkSingleNode = async (mon, url, nodeInfo) => {
            callCount++;
            if (callCount === 1) {
                throw new Error("Node 1 connection failed");
            }
            // Second call succeeds - don't throw

Review comment (suggestion):
```suggestion
        assert.strictEqual(heartbeat.msg, "One of the 2 nodes is reachable and there are no alerts in the cluster");
```

Code after:

describe("RabbitMQ Multi-Node (Mocked)", () => {
    test("check() succeeds when first node is healthy", async () => {
        const rabbitMQMonitor = new RabbitMqMonitorType();
        const monitor = {
            rabbitmqNodes: JSON.stringify([ "http://node1:15672", "http://node2:15672" ]),
            rabbitmqUsername: "guest",
            rabbitmqPassword: "guest",
            timeout: 10,
        };

        const heartbeat = {
            msg: "",
            status: PENDING,
        };

        // Mock checkSingleNode to succeed on first call (just don't throw)
        let callCount = 0;
        rabbitMQMonitor.checkSingleNode = async (mon, url, nodeInfo) => {
            callCount++;
            // Success - don't throw
        };

        await rabbitMQMonitor.check(monitor, heartbeat, {});
        assert.strictEqual(heartbeat.status, UP);
        assert.strictEqual(heartbeat.msg, "One of the 2 nodes is reachable and there are no alerts in the cluster");
        assert.strictEqual(callCount, 1, "Should only check first node");
    });

    test("check() succeeds when second node is healthy after first fails", async () => {
        const rabbitMQMonitor = new RabbitMqMonitorType();
        const monitor = {
            rabbitmqNodes: JSON.stringify([ "http://node1:15672", "http://node2:15672" ]),
            rabbitmqUsername: "guest",
            rabbitmqPassword: "guest",
            timeout: 10,
        };

        const heartbeat = {
            msg: "",
            status: PENDING,
        };

        // Mock checkSingleNode to fail first, succeed second
        let callCount = 0;
        rabbitMQMonitor.checkSingleNode = async (mon, url, nodeInfo) => {
            callCount++;
            if (callCount === 1) {
                throw new Error("Node 1 connection failed");
            }
            // Second call succeeds - don't throw