"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.crpcPort = 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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5zdGFsbC1iYXNpYy1wbHVnaW4ta2V5Y2hhaW4tbWVtb3J5LnRlc3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi9zcmMvdGVzdC90eXBlc2NyaXB0L3VuaXQvcGx1Z2lucy9pbnN0YWxsLWJhc2ljLXBsdWdpbi1rZXljaGFpbi1tZW1vcnkudGVzdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLDZEQUErQztBQUMvQywrQkFBb0M7QUFJcEMsa0VBSXNDO0FBRXRDLHVFQUln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