"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const event_store_1 = __importDefault(require("./event-store"));
const no_logger_1 = __importDefault(require("../../../test/fixtures/no-logger"));
const database_init_1 = __importDefault(require("../../../test/e2e/helpers/database-init"));
const stream_1 = require("stream");
const event_service_1 = __importDefault(require("./event-service"));
const metric_events_1 = require("../../metric-events");
let db;
beforeAll(async () => {
    db = await (0, database_init_1.default)('events_test', no_logger_1.default);
});
afterAll(async () => {
    await db.rawDatabase('events').del();
    await db.rawDatabase('users').del();
    await db.destroy();
});
test('sets created_by_user_id on events with user username/email set as created_by', async () => {
    const store = new event_store_1.default(db.rawDatabase, no_logger_1.default);
    await db.rawDatabase('users').insert({ username: 'test1' });
    await db.rawDatabase('events').insert({
        type: 'feature-created',
        created_by: 'test1',
        feature_name: 'feature1',
        data: `{"test": "data-migrate"}`,
    });
    await store.setCreatedByUserId(200);
    const user = await db
        .rawDatabase('users')
        .where({ username: 'test1' })
        .first('id');
    const events = await db.rawDatabase('events').select('*');
    const notSet = events.filter((e) => !e.created_by_user_id && e.data.test === 'data-migrate');
    const test1 = events.filter((e) => e.created_by_user_id === user.id && e.data.test === 'data-migrate');
    expect(notSet).toHaveLength(0);
    expect(test1).toHaveLength(1);
});
test('sets created_by_user_id on a mix of events and created_bys', async () => {
    const store = new event_store_1.default(db.rawDatabase, no_logger_1.default);
    await db.rawDatabase('users').insert({ username: 'test2' });
    await db.rawDatabase('api_tokens').insert({
        secret: 'token1',
        username: 'adm-token',
        type: 'admin',
        environment: 'default',
        token_name: 'admin-token',
    });
    await db.rawDatabase('events').insert({
        type: 'feature-created',
        created_by: 'test2',
        feature_name: 'feature1',
        data: `{"test": "data-migrate"}`,
    });
    await db.rawDatabase('events').insert({
        type: 'strategy-created',
        created_by: 'migration',
        data: `{"test": "data-migrate"}`,
    });
    await db.rawDatabase('events').insert({
        type: 'api-token-created',
        created_by: 'init-api-tokens',
        data: `{"test": "data-migrate"}`,
    });
    await db.rawDatabase('events').insert({
        type: 'application-created',
        created_by: '::1',
        data: `{"test": "data-migrate"}`,
    });
    await db.rawDatabase('events').insert({
        type: 'feature-created',
        created_by: 'unknown',
        feature_name: 'feature2',
        data: `{"test": "data-migrate"}`,
    });
    await db.rawDatabase('events').insert({
        type: 'feature-created',
        created_by: 'adm-token',
        feature_name: 'feature3',
        data: `{"test": "data-migrate"}`,
    });
    await store.setCreatedByUserId(200);
    const user = await db
        .rawDatabase('users')
        .where({ username: 'test2' })
        .first('id');
    const events = await db.rawDatabase('events').select('*');
    const notSet = events.filter((e) => !e.created_by_user_id && e.data.test === 'data-migrate');
    const test = events.filter((e) => e.created_by_user_id === user.id && e.data.test === 'data-migrate');
    expect(notSet).toHaveLength(1);
    expect(test).toHaveLength(1);
});
test('emits events with details on amount of updated rows', async () => {
    const store = new event_store_1.default(db.rawDatabase, no_logger_1.default);
    const eventBus = new stream_1.EventEmitter();
    const service = new event_service_1.default({ eventStore: store, featureTagStore: db.stores.featureTagStore }, { getLogger: no_logger_1.default, eventBus });
    let triggered = false;
    eventBus.on(metric_events_1.EVENTS_CREATED_BY_PROCESSED, ({ updated }) => {
        expect(updated).toBe(2);
        triggered = true;
    });
    await db.rawDatabase('users').insert({ username: 'events-test-1' });
    await db.rawDatabase('events').insert({
        type: 'feature-created',
        created_by: 'events-test-1',
        feature_name: 'feature1',
    });
    await db.rawDatabase('events').insert({
        type: 'feature-created',
        created_by: 'events-test-1',
        feature_name: 'feature2',
    });
    await db.rawDatabase('events').insert({
        type: 'feature-created',
        created_by: 'doesnt-exist',
        feature_name: 'not-counted',
    });
    await service.setEventCreatedByUserId();
    expect(triggered).toBeTruthy();
});
//# sourceMappingURL=event-created-by-migration.test.js.map