import type { Query } from '@mongodb-js/compass-query-history'; import type { StoredPipeline } from '@mongodb-js/compass-aggregations'; type UpdateAttributes = Record; interface FavoriteQueryStorage { new (): { loadAll(): Promise; updateAttributes(id: string, attributes: UpdateAttributes): Promise; delete(id: string): Promise; }; } interface PipelineStorageClass { new (): { loadAll(): Promise; updateAttributes( id: string, attributes: UpdateAttributes ): Promise; delete(id: string): Promise; }; } export function createCompassAggregationsMock(aggregations: StoredPipeline[]): { PipelineStorage: PipelineStorageClass; } { let data = [...aggregations]; return { PipelineStorage: class PipelineStorageClass { loadAll(): Promise { return Promise.resolve(data); } updateAttributes( id: string, attributes: UpdateAttributes ): Promise { const index = data.findIndex((x) => x.id === id); if (index >= 0) { data[index] = { ...data[index], ...attributes, }; return Promise.resolve(data[index]); } throw new Error('Can not find pipeline'); } delete(id: string): Promise { data = data.filter((x: StoredPipeline) => x.id !== id); return Promise.resolve(); } }, }; } export function createCompassQueryHistoryMock(queries: Query[]): { FavoriteQueryStorage: FavoriteQueryStorage; } { let data = [...queries]; return { FavoriteQueryStorage: class FavoriteQueryStorage { loadAll(): Promise { return Promise.resolve(data); } updateAttributes( id: string, attributes: UpdateAttributes ): Promise { const index = data.findIndex((x) => x._id === id); if (index >= 0) { data[index] = { ...data[index], ...attributes, }; return Promise.resolve(data[index]); } throw new Error('Can not find query'); } delete(id: string): Promise { data = data.filter((x: Query) => x._id !== id); return Promise.resolve(); } }, }; } export function createProxyquireMockForQueriesAndAggregationsPlugins( pipelines: StoredPipeline[], queries: Query[] ): unknown { return { '@mongodb-js/compass-query-history': { ...createCompassQueryHistoryMock(queries), '@global': true, '@noCallThru': true, }, '@mongodb-js/compass-aggregations': { ...createCompassAggregationsMock(pipelines), '@global': true, '@noCallThru': true, }, }; }