|
1 | 1 | import * as fs from 'node:fs';
|
2 | 2 | import * as os from 'node:os';
|
3 | 3 | import * as path from 'node:path';
|
4 |
| -import { describe, expect, it, vi } from 'vitest'; |
| 4 | +import { setTimeout as sleep } from 'node:timers/promises'; |
| 5 | +import { describe, expect, it, MockedFunction, vi } from 'vitest'; |
5 | 6 |
|
6 |
| -import { Action, checkBulkUpload, defaults, Reason } from '@immich/sdk'; |
| 7 | +import { Action, checkBulkUpload, defaults, getSupportedMediaTypes, Reason } from '@immich/sdk'; |
7 | 8 | import createFetchMock from 'vitest-fetch-mock';
|
8 | 9 |
|
9 |
| -import { checkForDuplicates, getAlbumName, uploadFiles, UploadOptionsDto } from './asset'; |
| 10 | +import { checkForDuplicates, getAlbumName, startWatch, uploadFiles, UploadOptionsDto } from 'src/commands/asset'; |
10 | 11 |
|
11 | 12 | vi.mock('@immich/sdk');
|
12 | 13 |
|
@@ -199,3 +200,112 @@ describe('checkForDuplicates', () => {
|
199 | 200 | });
|
200 | 201 | });
|
201 | 202 | });
|
| 203 | + |
| 204 | +describe('startWatch', () => { |
| 205 | + let testFolder: string; |
| 206 | + let checkBulkUploadMocked: MockedFunction<typeof checkBulkUpload>; |
| 207 | + |
| 208 | + beforeEach(async () => { |
| 209 | + vi.restoreAllMocks(); |
| 210 | + |
| 211 | + vi.mocked(getSupportedMediaTypes).mockResolvedValue({ |
| 212 | + image: ['.jpg'], |
| 213 | + sidecar: ['.xmp'], |
| 214 | + video: ['.mp4'], |
| 215 | + }); |
| 216 | + |
| 217 | + testFolder = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'test-startWatch-')); |
| 218 | + checkBulkUploadMocked = vi.mocked(checkBulkUpload); |
| 219 | + checkBulkUploadMocked.mockResolvedValue({ |
| 220 | + results: [], |
| 221 | + }); |
| 222 | + }); |
| 223 | + |
| 224 | + it('should start watching a directory and upload new files', async () => { |
| 225 | + const testFilePath = path.join(testFolder, 'test.jpg'); |
| 226 | + |
| 227 | + await startWatch([testFolder], { concurrency: 1 }, { batchSize: 1, debounceTimeMs: 10 }); |
| 228 | + await sleep(100); // to debounce the watcher from considering the test file as a existing file |
| 229 | + await fs.promises.writeFile(testFilePath, 'testjpg'); |
| 230 | + |
| 231 | + await vi.waitUntil(() => checkBulkUploadMocked.mock.calls.length > 0, 3000); |
| 232 | + expect(checkBulkUpload).toHaveBeenCalledWith({ |
| 233 | + assetBulkUploadCheckDto: { |
| 234 | + assets: [ |
| 235 | + expect.objectContaining({ |
| 236 | + id: testFilePath, |
| 237 | + }), |
| 238 | + ], |
| 239 | + }, |
| 240 | + }); |
| 241 | + }); |
| 242 | + |
| 243 | + it('should filter out unsupported files', async () => { |
| 244 | + const testFilePath = path.join(testFolder, 'test.jpg'); |
| 245 | + const unsupportedFilePath = path.join(testFolder, 'test.txt'); |
| 246 | + |
| 247 | + await startWatch([testFolder], { concurrency: 1 }, { batchSize: 1, debounceTimeMs: 10 }); |
| 248 | + await sleep(100); // to debounce the watcher from considering the test file as a existing file |
| 249 | + await fs.promises.writeFile(testFilePath, 'testjpg'); |
| 250 | + await fs.promises.writeFile(unsupportedFilePath, 'testtxt'); |
| 251 | + |
| 252 | + await vi.waitUntil(() => checkBulkUploadMocked.mock.calls.length > 0, 3000); |
| 253 | + expect(checkBulkUpload).toHaveBeenCalledWith({ |
| 254 | + assetBulkUploadCheckDto: { |
| 255 | + assets: expect.arrayContaining([ |
| 256 | + expect.objectContaining({ |
| 257 | + id: testFilePath, |
| 258 | + }), |
| 259 | + ]), |
| 260 | + }, |
| 261 | + }); |
| 262 | + |
| 263 | + expect(checkBulkUpload).not.toHaveBeenCalledWith({ |
| 264 | + assetBulkUploadCheckDto: { |
| 265 | + assets: expect.arrayContaining([ |
| 266 | + expect.objectContaining({ |
| 267 | + id: unsupportedFilePath, |
| 268 | + }), |
| 269 | + ]), |
| 270 | + }, |
| 271 | + }); |
| 272 | + }); |
| 273 | + |
| 274 | + it('should filger out ignored patterns', async () => { |
| 275 | + const testFilePath = path.join(testFolder, 'test.jpg'); |
| 276 | + const ignoredPattern = 'ignored'; |
| 277 | + const ignoredFolder = path.join(testFolder, ignoredPattern); |
| 278 | + await fs.promises.mkdir(ignoredFolder, { recursive: true }); |
| 279 | + const ignoredFilePath = path.join(ignoredFolder, 'ignored.jpg'); |
| 280 | + |
| 281 | + await startWatch([testFolder], { concurrency: 1, ignore: ignoredPattern }, { batchSize: 1, debounceTimeMs: 10 }); |
| 282 | + await sleep(100); // to debounce the watcher from considering the test file as a existing file |
| 283 | + await fs.promises.writeFile(testFilePath, 'testjpg'); |
| 284 | + await fs.promises.writeFile(ignoredFilePath, 'ignoredjpg'); |
| 285 | + |
| 286 | + await vi.waitUntil(() => checkBulkUploadMocked.mock.calls.length > 0, 3000); |
| 287 | + expect(checkBulkUpload).toHaveBeenCalledWith({ |
| 288 | + assetBulkUploadCheckDto: { |
| 289 | + assets: expect.arrayContaining([ |
| 290 | + expect.objectContaining({ |
| 291 | + id: testFilePath, |
| 292 | + }), |
| 293 | + ]), |
| 294 | + }, |
| 295 | + }); |
| 296 | + |
| 297 | + expect(checkBulkUpload).not.toHaveBeenCalledWith({ |
| 298 | + assetBulkUploadCheckDto: { |
| 299 | + assets: expect.arrayContaining([ |
| 300 | + expect.objectContaining({ |
| 301 | + id: ignoredFilePath, |
| 302 | + }), |
| 303 | + ]), |
| 304 | + }, |
| 305 | + }); |
| 306 | + }); |
| 307 | + |
| 308 | + afterEach(async () => { |
| 309 | + await fs.promises.rm(testFolder, { recursive: true, force: true }); |
| 310 | + }); |
| 311 | +}); |
0 commit comments