"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const __1 = require("..");
const secure_webhooks_1 = require("secure-webhooks");
const runQuirrel_1 = require("../../api/test/runQuirrel");
const util_1 = require("./util");
test("migration", async () => {
    process.env.NODE_ENV = "production";
    const newToken = "4ws8syoOgeQX6WFvXuUneGNwy7QvLxpk";
    const oldToken = "asdasidjkl3n12kln12kl23jklasmdkl";
    const client = new __1.QuirrelClient({
        handler: async () => { },
        route: "",
        config: {
            token: newToken,
            oldToken,
            quirrelBaseUrl: "unused",
            applicationBaseUrl: "unused",
        },
    });
    async function requestWith(token) {
        const body = JSON.stringify("hello world");
        const signature = secure_webhooks_1.symmetric.sign(body, token);
        const { status } = await client.respondTo(body, {
            "x-quirrel-signature": signature,
        });
        return status;
    }
    expect(await requestWith(newToken)).toBe(200);
    expect(await requestWith(oldToken)).toBe(200);
    expect(await requestWith("wrong token")).toBe(401);
});
test("migration read + write", async () => {
    var _a, _b;
    const passphrase = "password";
    const appId = "migration-test";
    const oldServer = await (0, runQuirrel_1.run)("Mock", {
        passphrases: [passphrase],
    });
    const newServer = await (0, runQuirrel_1.run)("Mock", {
        passphrases: [passphrase],
    });
    const oldToken = await ((_a = oldServer.app.app.tokens) === null || _a === void 0 ? void 0 : _a.create(appId));
    const newToken = await ((_b = newServer.app.app.tokens) === null || _b === void 0 ? void 0 : _b.create(appId));
    expect(oldToken).toBeDefined();
    expect(newToken).toBeDefined();
    const oldClient = new __1.QuirrelClient({
        handler: async () => { },
        route: "",
        config: {
            token: oldToken,
            quirrelBaseUrl: (0, util_1.getAddress)(oldServer.server),
            applicationBaseUrl: "unused",
        },
    });
    const newClient = new __1.QuirrelClient({
        handler: async () => { },
        route: "",
        config: {
            token: newToken,
            oldToken,
            quirrelBaseUrl: (0, util_1.getAddress)(newServer.server),
            quirrelOldBaseUrl: (0, util_1.getAddress)(oldServer.server),
            applicationBaseUrl: "unused",
        },
    });
    // get: should iterate through both
    const jobAOnOld = await oldClient.enqueue("a", {
        delay: "1h",
    });
    await newClient.enqueue("b", {
        delay: "1h",
    });
    const allJobs = [];
    for await (const jobs of newClient.get()) {
        allJobs.push(...jobs);
    }
    expect(allJobs).toHaveLength(2);
    // .getById should look at old
    expect(await newClient.getById(jobAOnOld.id)).toBeTruthy();
    // override: false. if job already exists on old instance, let it stay there
    await oldClient.enqueue("old payload", {
        id: "foo",
        delay: "1h",
    });
    const jobThatAlreadyExists = await newClient.enqueue("new payload", {
        id: "foo",
        delay: "1h",
    });
    expect(jobThatAlreadyExists.body).toEqual("old payload");
    const jobThatAlreadyExistsOnNew = await newClient.fetchAgainstCurrent("/foo");
    expect(jobThatAlreadyExistsOnNew.status).toBe(404);
    // override: false. if job doesn't exist on old instance, it should be on new
    await newClient.enqueue("new payload", {
        id: "override-false-on-new",
        delay: "1h",
    });
    const jobOnNew = await newClient.fetchAgainstCurrent("/override-false-on-new");
    expect(jobOnNew.status).toBe(200);
    // override: true. if job already exists on old instance, it should be deleted
    await oldClient.enqueue("old payload", {
        id: "override-true",
        delay: "1h",
    });
    await newClient.enqueue("new payload", {
        id: "override-true",
        delay: "1h",
        override: true,
    });
    expect((await oldClient.fetchAgainstCurrent("/override-true")).status).toBe(404);
    expect((await newClient.fetchAgainstCurrent("/override-true")).status).toBe(200);
    // delete: should run against both instances
    await oldClient.enqueue("old payload", {
        id: "delete",
        delay: "1h",
    });
    expect(await newClient.delete("delete")).toBe(true);
    expect(await oldClient.getById("delete")).toBeNull();
    await newClient.enqueue("new payload", {
        id: "delete",
        delay: "1h",
    });
    expect(await newClient.delete("delete")).toBe(true);
    expect(await newClient.getById("delete")).toBeNull();
    // invoke: should look at both instances
    await oldClient.enqueue("", {
        id: "invoke",
        delay: "1h",
    });
    expect(await newClient.invoke("invoke")).toBe(true);
    await newClient.enqueue("", {
        id: "invoke-2",
        delay: "1h",
    });
    expect(await newClient.invoke("invoke-2")).toBe(true);
    await oldServer.teardown();
    await newServer.teardown();
});
//# sourceMappingURL=migration.test.js.map