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