import { FeatureEnvironmentKey, IFeatureEnvironmentStore, } from '../../lib/types/stores/feature-environment-store'; import { IFeatureEnvironment, IVariant } from '../../lib/types/model'; import NotFoundError from '../../lib/error/notfound-error'; export default class FakeFeatureEnvironmentStore implements IFeatureEnvironmentStore { featureEnvironments: IFeatureEnvironment[] = []; async addEnvironmentToFeature( featureName: string, environment: string, enabled: boolean, ): Promise { this.featureEnvironments.push({ environment, enabled, featureName }); } async addVariantsToFeatureEnvironment( featureName: string, environment: string, variants: IVariant[], ): Promise { this.featureEnvironments .filter( (fe) => fe.featureName === featureName && fe.environment === environment, ) .map((fe) => (fe.variants = variants)); } async delete(key: FeatureEnvironmentKey): Promise { this.featureEnvironments.splice( this.featureEnvironments.findIndex( (fE) => fE.environment === key.environment && fE.featureName === key.featureName, ), 1, ); } async deleteAll(): Promise { this.featureEnvironments = []; } destroy(): void {} async disconnectFeatures( // eslint-disable-next-line @typescript-eslint/no-unused-vars environment: string, // eslint-disable-next-line @typescript-eslint/no-unused-vars project: string, ): Promise { return Promise.resolve(undefined); } async exists(key: FeatureEnvironmentKey): Promise { return this.featureEnvironments.some( (fE) => fE.featureName === key.featureName && fE.environment === key.environment, ); } async featureHasEnvironment( environment: string, featureName: string, ): Promise { return this.exists({ environment, featureName }); } async get(key: FeatureEnvironmentKey): Promise { const featureEnvironment = this.featureEnvironments.find( (fE) => fE.environment === key.environment && fE.featureName === key.featureName, ); if (featureEnvironment) { return featureEnvironment; } throw new NotFoundError( `Could not find environment ${key.environment} for feature: ${key.featureName}`, ); } async getAll(): Promise { return this.featureEnvironments; } getEnvironmentMetaData( environment: string, featureName: string, ): Promise { return this.get({ environment, featureName }); } async isEnvironmentEnabled( featureName: string, environment: string, ): Promise { try { const fE = await this.get({ featureName, environment }); return fE.enabled; } catch (e) { return false; } } async removeEnvironmentForFeature( featureName: string, environment: string, ): Promise { return this.delete({ featureName, environment }); } async setEnvironmentEnabledStatus( environment: string, featureName: string, enabled: boolean, ): Promise { const fE = await this.get({ environment, featureName }); if (fE.enabled !== enabled) { fE.enabled = enabled; return 1; } else { return 0; } } async connectProject( // eslint-disable-next-line @typescript-eslint/no-unused-vars environment: string, // eslint-disable-next-line @typescript-eslint/no-unused-vars projectId: string, ): Promise { return Promise.resolve(undefined); } async connectFeatures( // eslint-disable-next-line @typescript-eslint/no-unused-vars environment: string, // eslint-disable-next-line @typescript-eslint/no-unused-vars projectId: string, ): Promise { return Promise.reject(new Error('Not implemented')); } async disconnectProject( // eslint-disable-next-line @typescript-eslint/no-unused-vars environment: string, // eslint-disable-next-line @typescript-eslint/no-unused-vars projectId: string, ): Promise { return Promise.reject(new Error('Not implemented')); } async connectFeatureToEnvironmentsForProject( // eslint-disable-next-line @typescript-eslint/no-unused-vars featureName: string, // eslint-disable-next-line @typescript-eslint/no-unused-vars projectId: string, ): Promise { return Promise.resolve(); } disableEnvironmentIfNoStrategies( // eslint-disable-next-line @typescript-eslint/no-unused-vars featureName: string, // eslint-disable-next-line @typescript-eslint/no-unused-vars environment: string, ): Promise { return Promise.reject(new Error('Not implemented')); } copyEnvironmentFeaturesByProjects( // eslint-disable-next-line @typescript-eslint/no-unused-vars sourceEnvironment: string, // eslint-disable-next-line @typescript-eslint/no-unused-vars destinationEnvironment: string, // eslint-disable-next-line @typescript-eslint/no-unused-vars projects: string[], ): Promise { throw new Error('Method not implemented.'); } cloneStrategies( // eslint-disable-next-line @typescript-eslint/no-unused-vars sourceEnvironment: string, // eslint-disable-next-line @typescript-eslint/no-unused-vars destinationEnvironment: string, ): Promise { throw new Error('Method not implemented.'); } async addFeatureEnvironment( featureEnvironment: IFeatureEnvironment, ): Promise { this.featureEnvironments.push(featureEnvironment); return Promise.resolve(); } getEnvironmentsForFeature( // eslint-disable-next-line @typescript-eslint/no-unused-vars featureName: string, ): Promise { throw new Error('Method not implemented.'); } clonePreviousVariants( // eslint-disable-next-line @typescript-eslint/no-unused-vars environment: string, // eslint-disable-next-line @typescript-eslint/no-unused-vars project: string, ): Promise { throw new Error('Method not implemented.'); } }