import { IClientInstance, IClientInstanceStore, INewClientInstance, } from '../../lib/types/stores/client-instance-store'; import NotFoundError from '../../lib/error/notfound-error'; export default class FakeClientInstanceStore implements IClientInstanceStore { instances: IClientInstance[] = []; async bulkUpsert(instances: INewClientInstance[]): Promise { instances.forEach((i) => { this.instances.push({ createdAt: new Date(), ...i }); }); } async delete( key: Pick, ): Promise { this.instances.splice( this.instances.findIndex( (i) => i.instanceId === key.instanceId && i.appName === key.appName, ), 1, ); } async deleteAll(): Promise { this.instances = []; } async deleteForApplication(appName: string): Promise { this.instances = this.instances.filter((i) => i.appName !== appName); } destroy(): void {} async exists( key: Pick, ): Promise { return this.instances.some( (i) => i.appName === key.appName && i.instanceId === key.instanceId, ); } async get( key: Pick, ): Promise { const instance = this.instances.find( (i) => i.appName === key.appName && i.instanceId === key.instanceId, ); if (instance) { return instance; } throw new NotFoundError(`Could not find instance with key: ${key}`); } async getAll(): Promise { return this.instances; } async getByAppName(appName: string): Promise { return this.instances.filter((i) => i.appName === appName); } async getDistinctApplications(): Promise { const apps = new Set(); this.instances.forEach((i) => apps.add(i.appName)); return Array.from(apps.values()); } async insert(details: INewClientInstance): Promise { this.instances.push({ createdAt: new Date(), ...details }); } }