2024-02-13 08:48:47 -05:00
|
|
|
import { WatchOptions } from 'chokidar';
|
2024-03-20 21:20:38 +01:00
|
|
|
import { StorageCore } from 'src/cores/storage.core';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { IStorageRepository, StorageEventType, WatchEvents } from 'src/interfaces/storage.interface';
|
2024-02-13 08:48:47 -05:00
|
|
|
|
|
|
|
interface MockWatcherOptions {
|
|
|
|
items?: Array<{ event: 'change' | 'add' | 'unlink' | 'error'; value: string }>;
|
2024-03-05 23:23:06 +01:00
|
|
|
close?: () => Promise<void>;
|
2024-02-13 08:48:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export const makeMockWatcher =
|
|
|
|
({ items, close }: MockWatcherOptions) =>
|
|
|
|
(paths: string[], options: WatchOptions, events: Partial<WatchEvents>) => {
|
|
|
|
events.onReady?.();
|
|
|
|
for (const item of items || []) {
|
|
|
|
switch (item.event) {
|
2024-03-07 18:36:53 +01:00
|
|
|
case StorageEventType.ADD: {
|
2024-02-13 08:48:47 -05:00
|
|
|
events.onAdd?.(item.value);
|
|
|
|
break;
|
|
|
|
}
|
2024-03-07 18:36:53 +01:00
|
|
|
case StorageEventType.CHANGE: {
|
2024-02-13 08:48:47 -05:00
|
|
|
events.onChange?.(item.value);
|
|
|
|
break;
|
|
|
|
}
|
2024-03-07 18:36:53 +01:00
|
|
|
case StorageEventType.UNLINK: {
|
2024-02-13 08:48:47 -05:00
|
|
|
events.onUnlink?.(item.value);
|
|
|
|
break;
|
|
|
|
}
|
2024-03-07 18:36:53 +01:00
|
|
|
case StorageEventType.ERROR: {
|
2024-02-13 08:48:47 -05:00
|
|
|
events.onError?.(new Error(item.value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-05 23:23:06 +01:00
|
|
|
|
|
|
|
if (close) {
|
|
|
|
return () => close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => Promise.resolve();
|
2024-02-13 08:48:47 -05:00
|
|
|
};
|
2023-10-23 17:52:21 +02:00
|
|
|
|
|
|
|
export const newStorageRepositoryMock = (reset = true): jest.Mocked<IStorageRepository> => {
|
|
|
|
if (reset) {
|
|
|
|
StorageCore.reset();
|
|
|
|
}
|
2023-02-03 10:16:25 -05:00
|
|
|
|
|
|
|
return {
|
2023-06-30 12:24:28 -04:00
|
|
|
createZipStream: jest.fn(),
|
2023-02-03 10:16:25 -05:00
|
|
|
createReadStream: jest.fn(),
|
2023-09-27 20:44:51 +02:00
|
|
|
readFile: jest.fn(),
|
|
|
|
writeFile: jest.fn(),
|
2023-02-25 09:12:03 -05:00
|
|
|
unlink: jest.fn(),
|
2023-05-26 15:43:24 -04:00
|
|
|
unlinkDir: jest.fn().mockResolvedValue(true),
|
2023-02-25 09:12:03 -05:00
|
|
|
removeEmptyDirs: jest.fn(),
|
|
|
|
checkFileExists: jest.fn(),
|
|
|
|
mkdirSync: jest.fn(),
|
2023-03-21 22:49:19 -04:00
|
|
|
checkDiskUsage: jest.fn(),
|
2023-08-01 21:56:10 -04:00
|
|
|
readdir: jest.fn(),
|
2023-09-20 13:16:33 +02:00
|
|
|
stat: jest.fn(),
|
|
|
|
crawl: jest.fn(),
|
2024-03-14 01:52:30 -04:00
|
|
|
walk: jest.fn().mockImplementation(async function* () {}),
|
2023-12-29 18:41:33 +00:00
|
|
|
rename: jest.fn(),
|
|
|
|
copyFile: jest.fn(),
|
2024-02-11 21:40:34 -07:00
|
|
|
utimes: jest.fn(),
|
2024-02-13 08:48:47 -05:00
|
|
|
watch: jest.fn().mockImplementation(makeMockWatcher({})),
|
2023-02-03 10:16:25 -05:00
|
|
|
};
|
|
|
|
};
|