"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const tape_1 = __importDefault(require("tape-promise/tape"));
const uuid_1 = require("uuid");
const cactus_core_api_1 = require("@hyperledger/cactus-core-api");
const public_api_1 = require("../../../../main/typescript/public-api");
const metrics_1 = require("../../../../main/typescript/prometheus-exporter/metrics");
const public_api_2 = require("../../../../main/typescript/public-api");
const logLevel = "TRACE";
tape_1.default("can import plugins at runtime (CLI)", async (t) => {
    // const pluginsPath = path.join(
    //   "/tmp/org/hyperledger/cactus/cmd-api-server/runtime-plugin-imports_test", // the dir path from the root
    //   uuidv4(), // then a random directory to ensure proper isolation
    // );
    const pluginsPath = path_1.default.join(__dirname, // start at the current file's path
    "../../../../../../../", // walk back up to the project root
    ".tmp/test/cmd-api-server/install-basic-plugin-ledger-connector-quorum-0-7-0_test", // the dir path from the root
    uuid_1.v4());
    const pluginManagerOptionsJson = JSON.stringify({
        pluginsPath,
        npmInstallMode: "noCache",
    });
    const configService = new public_api_1.ConfigService();
    const apiServerOptions = configService.newExampleConfig();
    apiServerOptions.authorizationProtocol = public_api_1.AuthorizationProtocol.NONE;
    apiServerOptions.pluginManagerOptionsJson = pluginManagerOptionsJson;
    apiServerOptions.configFile = "";
    apiServerOptions.apiCorsDomainCsv = "*";
    apiServerOptions.apiPort = 0;
    apiServerOptions.cockpitPort = 0;
    apiServerOptions.apiTlsEnabled = false;
    apiServerOptions.plugins = [
        {
            packageName: "@hyperledger/cactus-plugin-ledger-connector-quorum",
            type: cactus_core_api_1.PluginImportType.Local,
            options: {
                instanceId: uuid_1.v4(),
                logLevel,
                rpcApiHttpHost: "localhost:8545",
            },
        },
    ];
    const config = configService.newExampleConfigConvict(apiServerOptions);
    const apiServer = new public_api_1.ApiServer({
        config: config.getProperties(),
    });
    const startResponse = apiServer.start();
    await t.doesNotReject(startResponse, "started API server dynamic imports OK");
    t.ok(startResponse, "startResponse truthy OK");
    const addressInfoApi = (await startResponse).addressInfoApi;
    const protocol = apiServerOptions.apiTlsEnabled ? "https" : "http";
    const { address, port } = addressInfoApi;
    const apiHost = `${protocol}://${address}:${port}`;
    t.comment(`Metrics URL: ${apiHost}/api/v1/api-server/get-prometheus-exporter-metrics`);
    const apiConfig = new cactus_core_api_1.Configuration({ basePath: apiHost });
    const apiClient = new public_api_2.DefaultApi(apiConfig);
    {
        const res = await apiClient.getPrometheusMetricsV1();
        const promMetricsOutput = "# HELP " +
            metrics_1.K_CACTUS_API_SERVER_TOTAL_PLUGIN_IMPORTS +
            " Total number of plugins imported\n" +
            "# TYPE " +
            metrics_1.K_CACTUS_API_SERVER_TOTAL_PLUGIN_IMPORTS +
            " gauge\n" +
            metrics_1.K_CACTUS_API_SERVER_TOTAL_PLUGIN_IMPORTS +
            '{type="' +
            metrics_1.K_CACTUS_API_SERVER_TOTAL_PLUGIN_IMPORTS +
            '"} 1';
        t.ok(res);
        t.ok(res.data);
        t.equal(res.status, 200);
        t.true(res.data.includes(promMetricsOutput), "Total 1 plugins imported as expected. RESULT OK");
    }
    tape_1.default.onFinish(() => apiServer.shutdown());
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5zdGFsbC1iYXNpYy1wbHVnaW4tbGVkZ2VyLWNvbm5lY3Rvci1xdW9ydW0tMC03LTAudGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uL3NyYy90ZXN0L3R5cGVzY3JpcHQvdW5pdC9wbHVnaW5zL2luc3RhbGwtYmFzaWMtcGx1Z2luLWxlZGdlci1jb25uZWN0b3ItcXVvcnVtLTAtNy0wLnRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7QUFBQSxnREFBd0I7QUFDeEIsNkRBQStDO0FBQy9DLCtCQUFvQztBQUlwQyxrRUFBK0U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