1
0
mirror of https://github.com/immich-app/immich.git synced 2024-11-30 09:47:31 +02:00
immich/server/test/repositories/storage.repository.mock.ts

72 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-02-13 15:48:47 +02:00
import { WatchOptions } from 'chokidar';
import { StorageCore } from 'src/cores/storage.core';
import { IStorageRepository, WatchEvents } from 'src/interfaces/storage.interface';
import { Mocked, vitest } from 'vitest';
2024-02-13 15:48:47 +02:00
interface MockWatcherOptions {
items?: Array<{ event: 'change' | 'add' | 'unlink' | 'error'; value: string }>;
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) {
case 'add': {
2024-02-13 15:48:47 +02:00
events.onAdd?.(item.value);
break;
}
case 'change': {
2024-02-13 15:48:47 +02:00
events.onChange?.(item.value);
break;
}
case 'unlink': {
2024-02-13 15:48:47 +02:00
events.onUnlink?.(item.value);
break;
}
case 'error': {
2024-02-13 15:48:47 +02:00
events.onError?.(new Error(item.value));
}
}
}
if (close) {
return () => close();
}
return () => Promise.resolve();
2024-02-13 15:48:47 +02:00
};
export const newStorageRepositoryMock = (reset = true): Mocked<IStorageRepository> => {
if (reset) {
StorageCore.reset();
}
return {
createZipStream: vitest.fn(),
createReadStream: vitest.fn(),
readFile: vitest.fn(),
createFile: vitest.fn(),
createWriteStream: vitest.fn(),
createOrOverwriteFile: vitest.fn(),
overwriteFile: vitest.fn(),
unlink: vitest.fn(),
unlinkDir: vitest.fn().mockResolvedValue(true),
removeEmptyDirs: vitest.fn(),
checkFileExists: vitest.fn(),
mkdirSync: vitest.fn(),
checkDiskUsage: vitest.fn(),
readdir: vitest.fn(),
realpath: vitest.fn().mockImplementation((filepath: string) => Promise.resolve(filepath)),
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({})),
};
};