"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const supertest_1 = __importDefault(require("supertest"));
const test_config_1 = require("../../../test/config/test-config");
const store_1 = __importDefault(require("../../../test/fixtures/store"));
const permissions_1 = __importDefault(require("../../../test/fixtures/permissions"));
const app_1 = __importDefault(require("../../app"));
const services_1 = require("../../services");
async function getSetup() {
    const randomBase = `/random${Math.round(Math.random() * 1000)}`;
    const perms = (0, permissions_1.default)();
    const stores = (0, store_1.default)();
    const config = (0, test_config_1.createTestConfig)({
        server: { baseUriPath: randomBase },
        preRouterHook: perms.hook,
    });
    const services = (0, services_1.createServices)(stores, config);
    const app = await (0, app_1.default)(config, stores, services);
    return {
        base: randomBase,
        strategyStore: stores.strategyStore,
        request: (0, supertest_1.default)(app),
        perms,
    };
}
test('add version numbers for /strategies', async () => {
    const { request, base } = await getSetup();
    return request
        .get(`${base}/api/admin/strategies`)
        .expect('Content-Type', /json/)
        .expect(200)
        .expect((res) => {
        expect(res.body.version).toBe(1);
    });
});
test('require a name when creating a new strategy', async () => {
    const { request, base } = await getSetup();
    return request
        .post(`${base}/api/admin/strategies`)
        .send({})
        .expect(400)
        .expect((res) => {
        expect(['name', 'property', 'required'].every((word) => res.body.details[0].description.includes(word))).toBeTruthy();
    });
});
test('require parameters array when creating a new strategy', async () => {
    const { request, base } = await getSetup();
    const { body } = await request
        .post(`${base}/api/admin/strategies`)
        .send({ name: 'TestStrat' })
        .expect(400);
    const detailsDescription = body.details[0].description;
    expect(detailsDescription).toEqual(expect.stringMatching('parameters'));
    expect(detailsDescription).toEqual(expect.stringMatching('required'));
});
test('create a new strategy with empty parameters', async () => {
    const { request, base } = await getSetup();
    return request
        .post(`${base}/api/admin/strategies`)
        .send({ name: 'TestStrat', parameters: [] })
        .expect(201);
});
test('not be possible to override name', async () => {
    const { request, base, strategyStore } = await getSetup();
    strategyStore.createStrategy({ name: 'Testing', parameters: [] });
    return request
        .post(`${base}/api/admin/strategies`)
        .send({ name: 'Testing', parameters: [] })
        .expect(409);
});
test('update strategy', async () => {
    const { request, base, strategyStore } = await getSetup();
    const name = 'AnotherStrat';
    strategyStore.createStrategy({ name, parameters: [] });
    return request
        .put(`${base}/api/admin/strategies/${name}`)
        .send({ name, parameters: [], description: 'added' })
        .expect(200);
});
test('not update unknown strategy', async () => {
    const { request, base } = await getSetup();
    const name = 'UnknownStrat';
    return request
        .put(`${base}/api/admin/strategies/${name}`)
        .send({ name, parameters: [], description: 'added' })
        .expect(404);
});
test('validate format when updating strategy', async () => {
    const { request, base, strategyStore } = await getSetup();
    const name = 'AnotherStrat';
    strategyStore.createStrategy({ name, parameters: [] });
    return request
        .put(`${base}/api/admin/strategies/${name}`)
        .send({})
        .expect(400);
});
test('editable=false will stop delete request', async () => {
    jest.spyOn(global.console, 'error').mockImplementation(() => jest.fn());
    const { request, base } = await getSetup();
    const name = 'default';
    return request.delete(`${base}/api/admin/strategies/${name}`).expect(500);
});
test('editable=false will stop edit request', async () => {
    jest.spyOn(global.console, 'error').mockImplementation(() => jest.fn());
    const { request, base } = await getSetup();
    const name = 'default';
    return request
        .put(`${base}/api/admin/strategies/${name}`)
        .send({ name, parameters: [] })
        .expect(500);
});
test('editable=true will allow delete request', async () => {
    const { request, base, strategyStore } = await getSetup();
    const name = 'deleteStrat';
    strategyStore.createStrategy({ name, parameters: [] });
    return request
        .delete(`${base}/api/admin/strategies/${name}`)
        .send({})
        .expect(200);
});
test('editable=true will allow edit request', async () => {
    const { request, base, strategyStore } = await getSetup();
    const name = 'editStrat';
    strategyStore.createStrategy({ name, parameters: [] });
    return request
        .put(`${base}/api/admin/strategies/${name}`)
        .send({ name, parameters: [] })
        .expect(200);
});
test('deprecating a strategy works', async () => {
    const { request, base, strategyStore } = await getSetup();
    const name = 'editStrat';
    strategyStore.createStrategy({ name, parameters: [] });
    await request
        .post(`${base}/api/admin/strategies/${name}/deprecate`)
        .set('Content-Type', 'application/json')
        .send()
        .expect(200);
    return request
        .get(`${base}/api/admin/strategies/${name}`)
        .expect(200)
        .expect((res) => expect(res.body.deprecated).toBe(true));
});
test('deprecating a non-existent strategy yields 404', async () => {
    const { request, base } = await getSetup();
    return request
        .post(`${base}/api/admin/strategies/non-existent-strategy/deprecate`)
        .set('Content-Type', 'application/json')
        .expect(404);
});
test('reactivating a strategy works', async () => {
    const { request, base, strategyStore } = await getSetup();
    const name = 'editStrat';
    strategyStore.createStrategy({ name, parameters: [] });
    await request
        .post(`${base}/api/admin/strategies/${name}/reactivate`)
        .set('Content-Type', 'application/json')
        .send()
        .expect(200);
    return request
        .get(`${base}/api/admin/strategies/${name}`)
        .expect(200)
        .expect((res) => expect(res.body.deprecated).toBe(false));
});
test('reactivating a non-existent strategy yields 404', async () => {
    const { request, base } = await getSetup();
    return request
        .post(`${base}/api/admin/strategies/non-existent-strategy/reactivate`)
        .set('Content-Type', 'application/json')
        .expect(404);
});
//# sourceMappingURL=strategy.test.js.map