"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const database_init_1 = __importDefault(require("../../../test/e2e/helpers/database-init"));
const test_helper_1 = require("../../../test/e2e/helpers/test-helper");
const no_logger_1 = __importDefault(require("../../../test/fixtures/no-logger"));
const types_1 = require("../../types");
const feature_lifecycle_service_1 = require("./feature-lifecycle-service");
let app;
let db;
let featureLifecycleService;
let eventStore;
let eventBus;
beforeAll(async () => {
    db = await (0, database_init_1.default)('feature_lifecycle', no_logger_1.default);
    app = await (0, test_helper_1.setupAppWithAuth)(db.stores, {
        experimental: {
            flags: {
                featureLifecycle: true,
            },
        },
    }, db.rawDatabase);
    eventStore = db.stores.eventStore;
    eventBus = app.config.eventBus;
    featureLifecycleService = app.services.featureLifecycleService;
    await app.request
        .post(`/auth/demo/login`)
        .send({
        email: 'user@getunleash.io',
    })
        .expect(200);
});
afterAll(async () => {
    await app.destroy();
    await db.destroy();
});
beforeEach(async () => { });
const getFeatureLifecycle = async (featureName, expectedCode = 200) => {
    return app.request
        .get(`/api/admin/projects/default/features/${featureName}/lifecycle`)
        .expect(expectedCode);
};
const completeFeature = async (featureName, expectedCode = 200) => {
    return app.request
        .post(`/api/admin/projects/default/features/${featureName}/lifecycle/complete`)
        .expect(expectedCode);
};
const uncompleteFeature = async (featureName, expectedCode = 200) => {
    return app.request
        .post(`/api/admin/projects/default/features/${featureName}/lifecycle/uncomplete`)
        .expect(expectedCode);
};
function reachedStage(name) {
    return new Promise((resolve) => featureLifecycleService.on(feature_lifecycle_service_1.STAGE_ENTERED, (event) => {
        if (event.stage === name)
            resolve(name);
    }));
}
const expectFeatureStage = async (featureName, stage) => {
    const { body: feature } = await app.getProjectFeatures('default', featureName);
    expect(feature.lifecycle).toMatchObject({
        stage,
        enteredStageAt: expect.any(String),
    });
};
test('should return lifecycle stages', async () => {
    await app.createFeature('my_feature_a');
    await app.enableFeature('my_feature_a', 'default');
    eventStore.emit(types_1.FEATURE_CREATED, { featureName: 'my_feature_a' });
    await reachedStage('initial');
    await expectFeatureStage('my_feature_a', 'initial');
    eventBus.emit(types_1.CLIENT_METRICS, {
        bucket: {
            toggles: {
                my_feature_a: 'irrelevant',
                non_existent_feature: 'irrelevent',
            },
        },
        environment: 'default',
    });
    // missing feature
    eventBus.emit(types_1.CLIENT_METRICS, {
        environment: 'default',
        bucket: { toggles: {} },
    });
    // non existent env
    eventBus.emit(types_1.CLIENT_METRICS, {
        bucket: {
            toggles: {
                my_feature_a: 'irrelevant',
            },
        },
        environment: 'non-existent',
    });
    await reachedStage('live');
    await expectFeatureStage('my_feature_a', 'live');
    eventStore.emit(types_1.FEATURE_ARCHIVED, { featureName: 'my_feature_a' });
    await reachedStage('archived');
    const { body } = await getFeatureLifecycle('my_feature_a');
    expect(body).toEqual([
        {
            stage: 'initial',
            enteredStageAt: expect.any(String),
        },
        { stage: 'pre-live', enteredStageAt: expect.any(String) },
        {
            stage: 'live',
            enteredStageAt: expect.any(String),
        },
        {
            stage: 'archived',
            enteredStageAt: expect.any(String),
        },
    ]);
    await expectFeatureStage('my_feature_a', 'archived');
    eventStore.emit(types_1.FEATURE_REVIVED, { featureName: 'my_feature_a' });
    await reachedStage('initial');
});
test('should be able to toggle between completed/uncompleted', async () => {
    await app.createFeature('my_feature_b');
    await completeFeature('my_feature_b');
    await expectFeatureStage('my_feature_b', 'completed');
    await uncompleteFeature('my_feature_b');
    const { body } = await getFeatureLifecycle('my_feature_b');
    expect(body).toEqual([]);
});
//# sourceMappingURL=feature-lifecycle.e2e.test.js.map