Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

3.9 KB
Raw Blame
"use strict";

const assert = require("node:assert/strict");

function make_zblueslip() {
    const lib = {};

    const opts = {
        // Silently swallow all debug, log and info calls.
        debug: false,
        log: false,
        info: false,
        // Check against expected error values for the following.
        warn: true,
        error: true,
    };
    const names = Object.keys(opts);

    // For fatal messages, we should use assert.throws
    /* istanbul ignore next */
    lib.fatal = (msg) => {
        throw new Error(msg);
    };

    // Store valid test data for options.
    lib.test_data = {};
    lib.test_logs = {};

    for (const name of names) {
        lib.test_data[name] = [];
        lib.test_logs[name] = [];
    }

    lib.expect = (name, message, expected_count = 1) => {
        assert.notEqual(opts[name], undefined, `unexpected arg for expect: ${name}`);
        assert.ok(
            expected_count > 0 && Number.isSafeInteger(expected_count),
            "expected count should be a positive integer",
        );
        const obj = {message, count: 0, expected_count};
        lib.test_data[name].push(obj);
    };

    const check_seen_messages = () => {
        for (const name of names) {
            const logs = lib.test_logs[name];
            const data = lib.test_data[name];
            for (const obj of logs) {
                const message = obj.message;
                const i = data.findIndex((x) => x.message === message);
                if (i === -1) {
                    // Only throw this for message types we want to explicitly track.
                    // For example, we do not want to throw here for debug messages.
                    if (opts[name] ?? false) {
                        throw new Error(`Unexpected '${name}' message: ${message}`);
                    }
                    continue;
                }
                data[i].count += 1;
            }

            for (const obj of data) {
                const message = obj.message;
                assert.equal(
                    obj.count,
                    obj.expected_count,
                    `Expected ${obj.expected_count} of '${name}': ${message}`,
                );
            }
        }
    };

    lib.reset = (skip_checks = false) => {
        if (!skip_checks) {
            check_seen_messages();
        }

        for (const name of names) {
            lib.test_data[name] = [];
            lib.test_logs[name] = [];
        }
    };

    lib.get_test_logs = (name) => lib.test_logs[name];

    // Create logging functions
    for (const name of names) {
        if (!(opts[name] ?? false)) {
            // should just log the message.
            lib[name] = function (message, more_info, cause) {
                lib.test_logs[name].push({message, more_info, cause});
            };
            continue;
        }
        lib[name] = function (message, more_info, cause) {
            /* istanbul ignore if */
            if (typeof message !== "string") {
                // We may catch exceptions in blueslip, and if
                // so our stub should include that.
                if (message.toString().includes("exception")) {
                    message = message.toString();
                } else {
                    throw new Error("message should be string: " + message);
                }
            }
            lib.test_logs[name].push({message, more_info, cause});
            const matched_error_message = lib.test_data[name].some((x) => x.message === message);
            const exact_match_fail = !matched_error_message;
            if (exact_match_fail) {
                const error = new Error(
                    `Invalid ${name} message: "${message}".`,
                    cause === undefined ? {} : {cause},
                );
                error.blueslip = true;
                throw error;
            }
        };
    }

    return lib;
}

module.exports = make_zblueslip();