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-04-16 05:05:08 +02:00
|
|
|
import { IStorageRepository, WatchEvents } from 'src/interfaces/storage.interface';
|
2024-04-16 16:44:45 +02:00
|
|
|
import { Mocked, vitest } from 'vitest';
|
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-04-16 05:05:08 +02:00
|
|
|
case 'add': {
|
2024-02-13 15:48:47 +02:00
|
|
|
events.onAdd?.(item.value);
|
|
|
|
break;
|
|
|
|
}
|
2024-04-16 05:05:08 +02:00
|
|
|
case 'change': {
|
2024-02-13 15:48:47 +02:00
|
|
|
events.onChange?.(item.value);
|
|
|
|
break;
|
|
|
|
}
|
2024-04-16 05:05:08 +02:00
|
|
|
case 'unlink': {
|
2024-02-13 15:48:47 +02:00
|
|
|
events.onUnlink?.(item.value);
|
|
|
|
break;
|
|
|
|
}
|
2024-04-16 05:05:08 +02:00
|
|
|
case '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
|
|
|
|
2024-04-16 16:44:45 +02:00
|
|
|
export const newStorageRepositoryMock = (reset = true): Mocked<IStorageRepository> => {
|
2023-10-23 17:52:21 +02:00
|
|
|
if (reset) {
|
|
|
|
StorageCore.reset();
|
|
|
|
}
|
2023-02-03 17:16:25 +02:00
|
|
|
|
|
|
|
return {
|
2024-04-16 16:44:45 +02:00
|
|
|
createZipStream: vitest.fn(),
|
|
|
|
createReadStream: vitest.fn(),
|
|
|
|
readFile: vitest.fn(),
|
2024-09-21 01:16:53 +02:00
|
|
|
createFile: vitest.fn(),
|
|
|
|
createOrOverwriteFile: vitest.fn(),
|
|
|
|
overwriteFile: vitest.fn(),
|
2024-04-16 16:44:45 +02:00
|
|
|
unlink: vitest.fn(),
|
|
|
|
unlinkDir: vitest.fn().mockResolvedValue(true),
|
|
|
|
removeEmptyDirs: vitest.fn(),
|
|
|
|
checkFileExists: vitest.fn(),
|
|
|
|
mkdirSync: vitest.fn(),
|
|
|
|
checkDiskUsage: vitest.fn(),
|
|
|
|
readdir: vitest.fn(),
|
2024-08-13 17:39:24 +02:00
|
|
|
realpath: vitest.fn().mockImplementation((filepath: string) => Promise.resolve(filepath)),
|
2024-04-16 16:44:45 +02:00
|
|
|
stat: vitest.fn(),
|
|
|
|
crawl: vitest.fn(),
|
|
|
|
walk: vitest.fn().mockImplementation(async function* () {}),
|
|
|
|
rename: vitest.fn(),
|
|
|
|
copyFile: vitest.fn(),
|
|
|
|
utimes: vitest.fn(),
|
|
|
|
watch: vitest.fn().mockImplementation(makeMockWatcher({})),
|
2023-02-03 17:16:25 +02:00
|
|
|
};
|
|
|
|
};
|