"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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 grpc = __importStar(require("@grpc/grpc-js"));
const public_api_1 = require("../../../main/typescript/public-api");
const public_api_2 = require("../../../main/typescript/public-api");
const public_api_3 = require("../../../main/typescript/public-api");
const public_api_4 = require("../../../main/typescript/public-api");
const public_api_5 = require("../../../main/typescript/public-api");
const run_time_error_cjs_1 = require("run-time-error-cjs");
const testCase = "API server: runs gRPC web services - mTLS";
const logLevel = "TRACE";
(0, tape_1.default)(testCase, async (t) => {
    var _a;
    const generator = new public_api_2.SelfSignedPkiGenerator();
    t.ok(generator, "Instantiated SelfSignedCertificateGenerator OK.");
    const serverCert = generator.create("127.0.0.1");
    const clientCert = generator.create("client.127.0.0.1", serverCert);
    const serverRootCertPemBuf = Buffer.from(serverCert.certificatePem);
    const configService = new public_api_1.ConfigService();
    const apiSrvOpts = await configService.newExampleConfig();
    apiSrvOpts.authorizationProtocol = public_api_3.AuthorizationProtocol.NONE;
    apiSrvOpts.configFile = "";
    apiSrvOpts.logLevel = logLevel;
    apiSrvOpts.apiCorsDomainCsv = "*";
    apiSrvOpts.apiPort = 0;
    apiSrvOpts.apiTlsCertPem = serverCert.certificatePem;
    apiSrvOpts.apiTlsKeyPem = serverCert.privateKeyPem;
    apiSrvOpts.apiTlsClientCaPem = clientCert.certificatePem;
    apiSrvOpts.grpcPort = 0;
    apiSrvOpts.crpcPort = 0;
    apiSrvOpts.cockpitPort = 0;
    apiSrvOpts.grpcMtlsEnabled = true;
    apiSrvOpts.apiTlsEnabled = false;
    apiSrvOpts.plugins = [];
    const config = await configService.newExampleConfigConvict(apiSrvOpts);
    const apiServer = new public_api_1.ApiServer({
        config: config.getProperties(),
    });
    tape_1.default.onFinish(async () => await apiServer.shutdown());
    const startResponse = apiServer.start();
    await t.doesNotReject(startResponse, "API server started OK");
    t.ok(startResponse, "startResponse truthy OK");
    const addressInfoGrpc = (await startResponse).addressInfoGrpc;
    const { address, port } = addressInfoGrpc;
    const grpcHostAndPort = `${address}:${port}`;
    const tlsCredentials = grpc.credentials.createSsl(serverRootCertPemBuf, Buffer.from(clientCert.privateKeyPem), Buffer.from(clientCert.certificatePem));
    const apiClient = new public_api_4.default_service.org.hyperledger.cactus.cmd_api_server.DefaultServiceClient(grpcHostAndPort, tlsCredentials);
    t.ok(apiClient, "apiClient truthy OK");
    const responsePromise = new Promise((resolve, reject) => {
        apiClient.GetHealthCheckV1(new public_api_5.empty.google.protobuf.Empty(), (error, response) => {
            if (error) {
                reject(error);
            }
            else if (response) {
                resolve(response);
            }
            else {
                throw new run_time_error_cjs_1.RuntimeError("No error, nor response received.");
            }
        });
    });
    await t.doesNotReject(responsePromise, "No error in healthcheck OK");
    const res = await responsePromise;
    const resHc = res.toObject();
    t.ok(resHc, `healthcheck response truthy OK`);
    t.ok(resHc.createdAt, `resHc.createdAt truthy OK`);
    t.ok(resHc.memoryUsage, `resHc.memoryUsage truthy OK`);
    t.ok((_a = resHc.memoryUsage) === null || _a === void 0 ? void 0 : _a.rss, `resHc.memoryUsage.rss truthy OK`);
    t.ok(resHc.success, `resHc.success truthy OK`);
    t.end();
});
//# sourceMappingURL=data:application/json;base64, 