2024-02-13 08:48:47 -05:00
|
|
|
import { WatchOptions } from 'chokidar';
|
2024-03-20 15:20:38 -05:00
|
|
|
import { StorageCore } from 'src/cores/storage.core';
|
2024-04-15 22:05:08 -05:00
|
|
|
import { IStorageRepository, WatchEvents } from 'src/interfaces/storage.interface';
|
2024-04-16 09:44:45 -05:00
|
|
|
import { Mocked, vitest } from 'vitest';
|
2024-02-13 08:48:47 -05:00
|
|
|
|
|
|
|
interface MockWatcherOptions {
|
|
|
|
items?: Array<{ event: 'change' | 'add' | 'unlink' | 'error'; value: string }>;
|
2024-03-05 17:23:06 -05: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-04-15 22:05:08 -05:00
|
|
|
case 'add': {
|
2024-02-13 08:48:47 -05:00
|
|
|
events.onAdd?.(item.value);
|
|
|
|
break;
|
|
|
|
}
|
2024-04-15 22:05:08 -05:00
|
|
|
case 'change': {
|
2024-02-13 08:48:47 -05:00
|
|
|
events.onChange?.(item.value);
|
|
|
|
break;
|
|
|
|
}
|
2024-04-15 22:05:08 -05:00
|
|
|
case 'unlink': {
|
2024-02-13 08:48:47 -05:00
|
|
|
events.onUnlink?.(item.value);
|
|
|
|
break;
|
|
|
|
}
|
2024-04-15 22:05:08 -05:00
|
|
|
case 'error': {
|
2024-02-13 08:48:47 -05:00
|
|
|
events.onError?.(new Error(item.value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-05 17:23:06 -05:00
|
|
|
|
|
|
|
if (close) {
|
|
|
|
return () => close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => Promise.resolve();
|
2024-02-13 08:48:47 -05:00
|
|
|
};
|
2023-10-23 10:52:21 -05:00
|
|
|
|
2024-04-16 09:44:45 -05:00
|
|
|
export const newStorageRepositoryMock = (reset = true): Mocked<IStorageRepository> => {
|
2023-10-23 10:52:21 -05:00
|
|
|
if (reset) {
|
|
|
|
StorageCore.reset();
|
|
|
|
}
|
2023-02-03 10:16:25 -05:00
|
|
|
|
|
|
|
return {
|
2024-04-16 09:44:45 -05:00
|
|
|
createZipStream: vitest.fn(),
|
|
|
|
createReadStream: vitest.fn(),
|
|
|
|
readFile: vitest.fn(),
|
2024-09-20 18:16:53 -05:00
|
|
|
createFile: vitest.fn(),
|
2024-10-31 06:29:42 -05:00
|
|
|
createWriteStream: vitest.fn(),
|
2024-09-20 18:16:53 -05:00
|
|
|
createOrOverwriteFile: vitest.fn(),
|
|
|
|
overwriteFile: vitest.fn(),
|
2024-04-16 09:44:45 -05: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 10:39:24 -05:00
|
|
|
realpath: vitest.fn().mockImplementation((filepath: string) => Promise.resolve(filepath)),
|
2024-04-16 09:44:45 -05: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 10:16:25 -05:00
|
|
|
};
|
|
|
|
};
|