"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
const __1 = require("..");
const runQuirrel_1 = require("../../api/test/runQuirrel");
const http = __importStar(require("http"));
const util_1 = require("./util");
const crypto_1 = require("crypto");
function getKeyPair() {
    return (0, crypto_1.generateKeyPairSync)("rsa", {
        modulusLength: 4096,
        publicKeyEncoding: {
            type: "spki",
            format: "pem",
        },
        privateKeyEncoding: {
            type: "pkcs8",
            format: "pem",
        },
    });
}
test("asymmetric signature", async () => {
    const oldNodeEnv = process.env.NODE_ENV;
    process.env.NODE_ENV = "production";
    const { publicKey, privateKey } = getKeyPair();
    const server = await (0, runQuirrel_1.run)("Mock", {
        webhookSigningPrivateKey: privateKey,
    });
    let quirrel;
    const responses = [];
    const endpoint = http
        .createServer((req, res) => {
        let body = "";
        req.on("data", (data) => {
            body += data;
        });
        req.on("end", async () => {
            const result = await quirrel.respondTo(body, req.headers);
            responses.push(result);
        });
    })
        .listen(0);
    quirrel = new __1.QuirrelClient({
        async handler() { },
        route: "/lol",
        config: {
            applicationBaseUrl: (0, util_1.getAddress)(endpoint),
            quirrelBaseUrl: (0, util_1.getAddress)(server.server),
            encryptionSecret: "4ws8syoOgeQX6WFvXuUneGNwy7QvLxpk",
            signaturePublicKey: publicKey,
        },
    });
    await quirrel.enqueue("hello world");
    await (0, util_1.waitUntil)(() => responses.length === 1, 100);
    expect(responses[0]).toEqual({
        body: "OK",
        headers: {},
        status: 200,
    });
    quirrel.signaturePublicKey = getKeyPair().publicKey;
    await quirrel.enqueue("hello world");
    await (0, util_1.waitUntil)(() => responses.length === 2, 100);
    expect(responses[1].body).toEqual("Signature invalid");
    server.teardown();
    endpoint.close();
    process.env.NODE_ENV = oldNodeEnv;
});
//# sourceMappingURL=asymmetric-signature.test.js.map