"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 lmify_1 = __importDefault(require("lmify"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const promises_1 = require("fs/promises");
const logLevel = "TRACE";
(0, tape_1.default)("can instantiate plugins at runtime without install them", async (t) => {
    (0, tape_1.default)("if plugin is not already installed and we set action to instantiate, server starting will fail", async (t2) => {
        const pluginsPath = path_1.default.join(__dirname, "../../../../../../", // walk back up to the project root
        ".tmp/test/cmd-api-server/plugin-import-without-install/", // 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.pluginManagerOptionsJson = pluginManagerOptionsJson;
        apiServerOptions.authorizationProtocol = public_api_1.AuthorizationProtocol.NONE;
        apiServerOptions.configFile = "";
        apiServerOptions.apiCorsDomainCsv = "*";
        apiServerOptions.apiPort = 0;
        apiServerOptions.cockpitPort = 0;
        apiServerOptions.grpcPort = 0;
        apiServerOptions.crpcPort = 0;
        apiServerOptions.apiTlsEnabled = false;
        const instanceId = (0, uuid_1.v4)();
        const keychainId = (0, uuid_1.v4)();
        const plugin = {
            packageName: "@hyperledger/cactus-plugin-keychain-memory",
            type: cactus_core_api_1.PluginImportType.Local,
            action: cactus_core_api_1.PluginImportAction.Instantiate,
            options: {
                instanceId,
                keychainId,
                logLevel,
                version: "0.9.0",
            },
        };
        apiServerOptions.plugins = [plugin];
        const config = await configService.newExampleConfigConvict(apiServerOptions);
        const apiServer = new public_api_1.ApiServer({
            config: config.getProperties(),
        });
        await t2.rejects(apiServer.start());
        t2.end();
    });
    (0, tape_1.default)("if plugin is already installed and we send a different version, it keeps using plugin installed before call apiServer", async (t2) => {
        const pluginsPath = path_1.default.join(__dirname, "../../../../../../", // walk back up to the project root
        ".tmp/test/cmd-api-server/plugin-import-without-install/", // 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.pluginManagerOptionsJson = pluginManagerOptionsJson;
        apiServerOptions.authorizationProtocol = public_api_1.AuthorizationProtocol.NONE;
        apiServerOptions.configFile = "";
        apiServerOptions.apiCorsDomainCsv = "*";
        apiServerOptions.apiPort = 0;
        apiServerOptions.cockpitPort = 0;
        apiServerOptions.grpcPort = 0;
        apiServerOptions.crpcPort = 0;
        apiServerOptions.apiTlsEnabled = false;
        const versionToSendServer = "0.7.0";
        const instanceId = (0, uuid_1.v4)();
        const keychainId = (0, uuid_1.v4)();
        const plugin = {
            packageName: "@hyperledger/cactus-plugin-keychain-memory",
            type: cactus_core_api_1.PluginImportType.Local,
            action: cactus_core_api_1.PluginImportAction.Instantiate,
            options: {
                instanceId,
                keychainId,
                logLevel,
                version: versionToSendServer,
            },
        };
        apiServerOptions.plugins = [plugin];
        const pluginPackageDir = path_1.default.join(pluginsPath, instanceId);
        const versionToInstall = "0.10.0";
        await fs_extra_1.default.mkdirp(pluginPackageDir);
        lmify_1.default.setPackageManager("npm");
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-expect-error
        lmify_1.default.setRootDir(pluginPackageDir);
        const out = await lmify_1.default.install([
            `${plugin.packageName}@${versionToInstall}`,
            "--production",
            "--audit=false",
            "--progress=false",
            "--fund=false",
            `--prefix=${pluginPackageDir}`,
            // "--ignore-workspace-root-check",
        ]);
        t2.equal(out.exitCode, 0, "Plugin installed correctly");
        const config = await configService.newExampleConfigConvict(apiServerOptions, true);
        const apiServer = new public_api_1.ApiServer({
            config: config.getProperties(),
        });
        await t2.doesNotReject(apiServer.start(), "apiServer was starting instantiating a plugin previously installed");
        const packageFilePath = path_1.default.join(pluginsPath, plugin.options.instanceId, "node_modules", `${plugin.packageName}`, "package.json");
        const pkgJsonStr = await (0, promises_1.readFile)(packageFilePath, "utf-8");
        const { version } = JSON.parse(pkgJsonStr);
        t2.strictEquals(version, versionToInstall, "apiServer did not overwrite package");
        const pluginsCount = apiServer.getPluginImportsCount();
        t2.equal(pluginsCount, 1, "apiServer instantiated 1 plugin");
        await t2.doesNotReject(apiServer.shutdown(), "apiServer was stopped instantiating a plugin previously installed");
        t2.end();
    });
    (0, tape_1.default)("if we send action as Installation, apiServer will install the plugin", async (t2) => {
        const pluginsPath = path_1.default.join(__dirname, "../../../../../../", // walk back up to the project root
        ".tmp/test/cmd-api-server/plugin-import-without-install/", // 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.pluginManagerOptionsJson = pluginManagerOptionsJson;
        apiServerOptions.authorizationProtocol = public_api_1.AuthorizationProtocol.NONE;
        apiServerOptions.configFile = "";
        apiServerOptions.apiCorsDomainCsv = "*";
        apiServerOptions.apiPort = 0;
        apiServerOptions.cockpitPort = 0;
        apiServerOptions.grpcPort = 0;
        apiServerOptions.crpcPort = 0;
        apiServerOptions.apiTlsEnabled = false;
        const versionToInstall = "0.8.0";
        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,
                    version: versionToInstall,
                },
            },
        ];
        const config = await configService.newExampleConfigConvict(apiServerOptions, true);
        const apiServer = new public_api_1.ApiServer({
            config: config.getProperties(),
        });
        await t2.doesNotReject(apiServer.start(), "apiServer was starting instantiating a plugin previously installed");
        const packageFilePath = path_1.default.join(pluginsPath, apiServerOptions.plugins[0].options.instanceId, "node_modules", `${apiServerOptions.plugins[0].packageName}`, "package.json");
        const pkgJsonStr = await (0, promises_1.readFile)(packageFilePath, "utf-8");
        const { version } = JSON.parse(pkgJsonStr);
        t2.strictEquals(version, versionToInstall, "apiServer installed the package");
        const pluginsCount = apiServer.getPluginImportsCount();
        t2.equal(pluginsCount, 1, "apiServer instantiated 1 plugin");
        await t2.doesNotReject(apiServer.shutdown(), "apiServer was stopped");
        t2.end();
    });
    t.end();
});
//# sourceMappingURL=data:application/json;base64, 