"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
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 path_1 = __importDefault(require("path"));
const logLevel = "TRACE";
(0, tape_1.default)("can import plugins at runtime (CLI)", async (t) => {
    // const pluginsPath = path.join(
    //   "/tmp/org/hyperledger/cactus/cmd-api-server/install-basic-plugin-keychain-memory_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/runtime-plugin-imports_test", // the dir path from the root
    (0, uuid_1.v4)());
    const pluginManagerOptionsJson = JSON.stringify({ pluginsPath });
    const configService = new public_api_1.ConfigService();
    const apiServerOptions = await configService.newExampleConfig();
    apiServerOptions.authorizationProtocol = public_api_1.AuthorizationProtocol.NONE;
    apiServerOptions.pluginManagerOptionsJson = pluginManagerOptionsJson;
    apiServerOptions.configFile = "";
    apiServerOptions.apiCorsDomainCsv = "*";
    apiServerOptions.apiPort = 0;
    apiServerOptions.cockpitPort = 0;
    apiServerOptions.grpcPort = 0;
    apiServerOptions.apiTlsEnabled = false;
    apiServerOptions.plugins = [
        {
            packageName: "@hyperledger/cactus-plugin-keychain-memory",
            type: cactus_core_api_1.PluginImportType.Local,
            action: cactus_core_api_1.PluginImportAction.Install,
            options: {
                instanceId: (0, uuid_1.v4)(),
                keychainId: (0, uuid_1.v4)(),
                logLevel,
            },
        },
    ];
    const config = await 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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5zdGFsbC1iYXNpYy1wbHVnaW4ta2V5Y2hhaW4tbWVtb3J5LnRlc3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi9zcmMvdGVzdC90eXBlc2NyaXB0L3VuaXQvcGx1Z2lucy9pbnN0YWxsLWJhc2ljLXBsdWdpbi1rZXljaGFpbi1tZW1vcnkudGVzdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLDZEQUErQztBQUMvQywrQkFBb0M7QUFJcEMsa0VBSXNDO0FBRXRDLHVFQUlnRDtBQUVoRCxxRkFBbUg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