"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 path_1 = __importDefault(require("path"));
const public_api_1 = require("../../../main/typescript/public-api");
const public_api_2 = require("../../../main/typescript/public-api");
const grpc = __importStar(require("@grpc/grpc-js"));
const protoLoader = __importStar(require("@grpc/proto-loader"));
const empty_pb_1 = require("google-protobuf/google/protobuf/empty_pb");
const testCase = "API server: runs gRPC web services - proto loader";
const logLevel = "TRACE";
(0, tape_1.default)(testCase, async (t) => {
    const configService = new public_api_1.ConfigService();
    const apiSrvOpts = await configService.newExampleConfig();
    apiSrvOpts.authorizationProtocol = public_api_2.AuthorizationProtocol.NONE;
    apiSrvOpts.configFile = "";
    apiSrvOpts.logLevel = logLevel;
    apiSrvOpts.apiCorsDomainCsv = "*";
    apiSrvOpts.apiPort = 0;
    apiSrvOpts.grpcPort = 0;
    apiSrvOpts.crpcPort = 0;
    apiSrvOpts.cockpitPort = 0;
    apiSrvOpts.grpcMtlsEnabled = false;
    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, "start API server OK");
    t.ok(startResponse, "startResponse truthy OK");
    const addressInfoApi = (await startResponse).addressInfoGrpc;
    const { address, port } = addressInfoApi;
    const grpcHostAndPort = `${address}:${port}`;
    t.ok(grpcHostAndPort, "grpcHostAndPort truthy OK");
    const PROTO_PATH = path_1.default.join(__dirname, "../../../main/proto/generated/openapi/services/default_service.proto");
    const PROTO_INCLUDE_DIR = path_1.default.join(__dirname, "../../../main/proto/generated/openapi/");
    const packageDefinition = await protoLoader.load(PROTO_PATH, {
        includeDirs: [PROTO_INCLUDE_DIR],
        keepCase: true,
        longs: String,
        enums: String,
        defaults: true,
        oneofs: true,
    });
    const grpcPkg = grpc.loadPackageDefinition(packageDefinition);
    t.ok(grpcPkg, "grpcPkg truthy OK");
    const DefaultService = grpcPkg.org
        .hyperledger.cactus.cmd_api_server.DefaultService;
    t.ok(DefaultService, "DefaultService truthy OK");
    const client = new DefaultService(grpcHostAndPort, grpc.credentials.createInsecure());
    t.ok(client, "proto loaded client truthy OK");
    const request = new empty_pb_1.Empty();
    const res1 = await new Promise((resolve, reject) => {
        client.getHealthCheckV1(request, (err, value) => {
            if (err) {
                reject(err);
            }
            else {
                resolve(value);
            }
        });
    });
    t.ok(res1, "res1 truthy OK");
    t.ok(res1.createdAt, "res1.createdAt truthy OK");
    t.ok(res1.memoryUsage, "res1.memoryUsage truthy OK");
    t.ok(res1.memoryUsage.heapTotal, "res1.memoryUsage.heapTotal truthy OK");
    t.ok(res1.memoryUsage.heapUsed, "res1.memoryUsage.heapUsed truthy OK");
    t.ok(res1.memoryUsage.rss, "res1.memoryUsage.rss truthy OK");
    t.true(res1.success, "res1.success true OK");
    t.end();
});
//# sourceMappingURL=data:application/json;base64, 