"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const test_helper_1 = require("../../helpers/test-helper");
const database_init_1 = __importDefault(require("../../helpers/database-init"));
const no_logger_1 = __importDefault(require("../../../fixtures/no-logger"));
const events_1 = require("../../../../lib/types/events");
const random_id_1 = require("../../../../lib/util/random-id");
const services_1 = require("../../../../lib/services");
const events_2 = __importDefault(require("events"));
let app;
let db;
let eventService;
const TEST_USER_ID = -9999;
beforeAll(async () => {
    db = await (0, database_init_1.default)('event_api_serial', no_logger_1.default);
    app = await (0, test_helper_1.setupAppWithCustomConfig)(db.stores, {
        experimental: {
            flags: {
                strictSchemaValidation: true,
            },
        },
    });
    eventService = new services_1.EventService(db.stores, {
        getLogger: no_logger_1.default,
        eventBus: new events_2.default(),
    });
});
beforeEach(async () => {
    await db.stores.eventStore.deleteAll();
});
afterAll(async () => {
    await app.destroy();
    await db.destroy();
});
test('returns events', async () => {
    expect.assertions(0);
    return app.request
        .get('/api/admin/events')
        .expect('Content-Type', /json/)
        .expect(200);
});
test('returns events given a name', async () => {
    expect.assertions(0);
    return app.request
        .get('/api/admin/events/myname')
        .expect('Content-Type', /json/)
        .expect(200);
});
test('Can filter by project', async () => {
    await eventService.storeEvent({
        type: events_1.FEATURE_CREATED,
        project: 'something-else',
        data: { id: 'some-other-feature' },
        tags: [],
        createdBy: 'test-user',
        createdByUserId: TEST_USER_ID,
    });
    await eventService.storeEvent({
        type: events_1.FEATURE_CREATED,
        project: 'default',
        data: { id: 'feature' },
        tags: [],
        createdBy: 'test-user',
        environment: 'test',
        createdByUserId: TEST_USER_ID,
    });
    await app.request
        .get('/api/admin/events?project=default')
        .expect(200)
        .expect((res) => {
        expect(res.body.events).toHaveLength(1);
        expect(res.body.events[0].data.id).toEqual('feature');
    });
});
test('can search for events', async () => {
    const events = [
        {
            type: events_1.FEATURE_CREATED,
            project: (0, random_id_1.randomId)(),
            data: { id: (0, random_id_1.randomId)() },
            tags: [],
            createdBy: (0, random_id_1.randomId)(),
            createdByUserId: TEST_USER_ID,
        },
        {
            type: events_1.FEATURE_CREATED,
            project: (0, random_id_1.randomId)(),
            data: { id: (0, random_id_1.randomId)() },
            preData: { id: (0, random_id_1.randomId)() },
            tags: [{ type: 'simple', value: (0, random_id_1.randomId)() }],
            createdBy: (0, random_id_1.randomId)(),
            createdByUserId: TEST_USER_ID,
        },
    ];
    await Promise.all(events.map((event) => {
        return eventService.storeEvent(event);
    }));
    await app.request
        .post('/api/admin/events/search')
        .send({})
        .expect(200)
        .expect((res) => {
        expect(res.body.events).toHaveLength(2);
    });
    await app.request
        .post('/api/admin/events/search')
        .send({ limit: 1, offset: 1 })
        .expect(200)
        .expect((res) => {
        expect(res.body.events).toHaveLength(1);
    });
    await app.request
        .post('/api/admin/events/search')
        .send({ query: events[1].data.id })
        .expect(200)
        .expect((res) => {
        expect(res.body.events).toHaveLength(1);
        expect(res.body.events[0].data.id).toEqual(events[1].data.id);
    });
    await app.request
        .post('/api/admin/events/search')
        .send({ query: events[1].preData.id })
        .expect(200)
        .expect((res) => {
        expect(res.body.events).toHaveLength(1);
        expect(res.body.events[0].preData.id).toEqual(events[1].preData.id);
    });
    await app.request
        .post('/api/admin/events/search')
        .send({ query: events[1].tags[0].value })
        .expect(200)
        .expect((res) => {
        expect(res.body.events).toHaveLength(1);
        expect(res.body.events[0].data.id).toEqual(events[1].data.id);
    });
});
//# sourceMappingURL=event.e2e.test.js.map