Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

test(backend:files): strengthen coverage across files services

johaven0d842f1129cd
diff --git a/backend/src/applications/files/services/files-content-indexer.service.spec.ts b/backend/src/applications/files/services/files-content-indexer.service.spec.ts
new file mode 100644
index 00000000..f9adbdae
--- /dev/null
+++ b/backend/src/applications/files/services/files-content-indexer.service.spec.ts
@@ -0,0 +1,302 @@
+import { Test, TestingModule } from '@nestjs/testing'
+import fs from 'fs/promises'
+import path from 'node:path'
+import { Cache } from '../../../infrastructure/cache/services/cache.service'
+import { CACHE_INDEXING_UPDATE_PREFIX } from '../constants/indexing'
+import { FILE_REPOSITORY } from '../constants/operations'
+import { FileParseContext } from '../interfaces/file-parse-index'
+import { FilesContentStore } from '../models/files-content-store'
+import * as docTextifyModule from '../utils/doc-textify/doc-textify'
+import { PdfOCRWorkerManager } from '../utils/doc-textify/utils/pdf-ocr'
+import { FilesParser } from './files-parser.service'
+import { FilesContentIndexer } from './files-content-indexer.service'
+
+describe(FilesContentIndexer.name, () => {
+ let service: FilesContentIndexer
+ let cache: {
+ keys: jest.Mock
+ del: jest.Mock
+ }
+ let filesIndexer: {
+ getIndexName: jest.Mock
+ createIndex: jest.Mock
+ getRecordStats: jest.Mock
+ insertRecord: jest.Mock
+ deleteRecords: jest.Mock
+ dropIndex: jest.Mock
+ cleanIndexes: jest.Mock
+ }
+ let filesParser: {
+ allPaths: jest.Mock
+ }
+
+ const asyncGen = <T>(items: T[]) =>
+ (async function* () {
+ for (const item of items) {
+ yield item
+ }
+ })()
+
+ beforeEach(async () => {
+ cache = {
+ keys: jest.fn().mockResolvedValue([]),
+ del: jest.fn().mockResolvedValue(true)
+ }
+ filesIndexer = {
+ getIndexName: jest.fn((suffix: string) => `files_content_${suffix}`),
+ createIndex: jest.fn().mockResolvedValue(true),
+ getRecordStats: jest.fn().mockResolvedValue(new Map()),
+ insertRecord: jest.fn().mockResolvedValue(undefined),
+ deleteRecords: jest.fn().mockResolvedValue(undefined),
+ dropIndex: jest.fn().mockResolvedValue(true),
+ cleanIndexes: jest.fn().mockResolvedValue(undefined)
+ }
+ filesParser = {
+ allPaths: jest.fn().mockReturnValue(asyncGen([]))
+ }
+ const module: TestingModule = await Test.createTestingModule({
+ providers: [
+ FilesContentIndexer,
+ { provide: Cache, useValue: cache },
+ { provide: FilesContentStore, useValue: filesIndexer },
+ { provide: FilesParser, useValue: filesParser }
+ ]
+ }).compile()
+
+ module.useLogger(['fatal'])
+ service = module.get<FilesContentIndexer>(FilesContentIndexer)
+ })
+
+ afterEach(() => {
+ jest.restoreAllMocks()
+ jest.clearAllMocks()
+ })
+
+ it('should be defined', () => {
+ expect(service).toBeDefined()
+ })
+
+ it('should aggregate indexing event keys, parse matching entries and clean cache keys', async () => {
+ cache.keys.mockResolvedValueOnce([
+ `${CACHE_INDEXING_UPDATE_PREFIX}-user-1`,
+ `${CACHE_INDEXING_UPDATE_PREFIX}-space-2`,
+ `${CACHE_INDEXING_UPDATE_PREFIX}-share-3`,
+ `${CACHE_INDEXING_UPDATE_PREFIX}-unknown-9`
+ ])
+ const parseSpy = jest.spyOn(service, 'parseAndIndexAllFiles').mockResolvedValueOnce(undefined)
+
+ await service.updateIndexEntries()
+
+ expect(parseSpy).toHaveBeenCalledWith([1], [2], [3])
+ expect(cache.del).toHaveBeenCalledTimes(4)
+ expect(cache.del).toHaveBeenCalledWith(`${CACHE_INDEXING_UPDATE_PREFIX}-user-1`)
+ expect(cache.del).toHaveBeenCalledWith(`${CACHE_INDEXING_UPDATE_PREFIX}-space-2`)
+ expect(cache.del).toHaveBeenCalledWith(`${CACHE_INDEXING_UPDATE_PREFIX}-share-3`)
+ expect(cache.del).toHaveBeenCalledWith(`${CACHE_INDEXING_UPDATE_PREFIX}-unknown-9`)
+ })
+
+ it('should only clean keys when no valid indexing repository type is found', async () => {
+ cache.keys.mockResolvedValueOnce([`${CACHE_INDEXING_UPDATE_PREFIX}-unknown-10`])
+ const parseSpy = jest.spyOn(service, 'parseAndIndexAllFiles').mockResolvedValueOnce(undefined)
+
+ await service.updateIndexEntries()
+
+ expect(parseSpy).not.toHaveBeenCalled()
+ expect(cache.del).toHaveBeenCalledWith(`${CACHE_INDEXING_UPDATE_PREFIX}-unknown-10`)
+ })
+
+ it('should start and stop OCR manager, index all parser paths and clean stale indexes on full reindex', async () => {
+ const ocrManager = {
+ worker: null,
+ start: jest.fn().mockResolvedValue(null),
+ stop: jest.fn().mockResolvedValue(undefined)
+ }
+ jest.spyOn(PdfOCRWorkerManager, 'getInstance').mockReturnValue(ocrManager as any)
+ filesParser.allPaths.mockReturnValue(
+ asyncGen([
+ [1, FILE_REPOSITORY.USER, [{ realPath: '/u/john', pathPrefix: 'files/personal', isDir: true }]],
+ [5, FILE_REPOSITORY.SPACE, [{ realPath: '/s/project', pathPrefix: 'files/project', isDir: true }]]
+ ] as [number, FILE_REPOSITORY, FileParseContext[]][])
+ )
+ const indexSpy = jest.spyOn(service as any, 'indexFiles').mockResolvedValue(undefined)
+
+ await service.parseAndIndexAllFiles()
+
+ expect(ocrManager.start).toHaveBeenCalledTimes(1)
+ expect(indexSpy).toHaveBeenNthCalledWith(1, 'user_1', [{ realPath: '/u/john', pathPrefix: 'files/personal', isDir: true }])
+ expect(indexSpy).toHaveBeenNthCalledWith(2, 'space_5', [{ realPath: '/s/project', pathPrefix: 'files/project', isDir: true }])
+ expect(filesIndexer.cleanIndexes).toHaveBeenCalledWith(['user_1', 'space_5'])
+ expect(ocrManager.stop).toHaveBeenCalledTimes(1)
+ })
+
+ it('should continue parseAndIndexAllFiles when OCR startup fails and skip cleanIndexes for incremental runs', async () => {
+ const ocrManager = {
+ worker: null,
+ start: jest.fn().mockRejectedValue(new Error('ocr init failed')),
+ stop: jest.fn().mockResolvedValue(undefined)
+ }
+ jest.spyOn(PdfOCRWorkerManager, 'getInstance').mockReturnValue(ocrManager as any)
+ filesParser.allPaths.mockReturnValue(
+ asyncGen([[9, FILE_REPOSITORY.USER, [{ realPath: '/u/jane', pathPrefix: 'files/personal', isDir: true }]]] as any)
+ )
+ jest.spyOn(service as any, 'indexFiles').mockResolvedValue(undefined)
+
+ await service.parseAndIndexAllFiles([9], [], [])
+
+ expect(filesIndexer.cleanIndexes).not.toHaveBeenCalled()
+ expect(ocrManager.stop).toHaveBeenCalledTimes(1)
+ })
+
+ it('should skip indexFiles when index creation fails', async () => {
+ filesIndexer.createIndex.mockResolvedValueOnce(false)
+
+ await (service as any).indexFiles('user_7', [{ realPath: '/u/john', pathPrefix: 'files/personal', isDir: true }])
+
+ expect(filesIndexer.getRecordStats).not.toHaveBeenCalled()
+ expect(filesIndexer.insertRecord).not.toHaveBeenCalled()
+ expect(filesIndexer.deleteRecords).not.toHaveBeenCalled()
+ expect(filesIndexer.dropIndex).not.toHaveBeenCalled()
+ })
+
+ it('should drop empty index when there is no db data and no indexed records', async () => {
+ filesIndexer.getRecordStats.mockResolvedValueOnce(new Map())
+ jest.spyOn(service as any, 'parseFiles').mockReturnValue(asyncGen([]))
+
+ await (service as any).indexFiles('user_8', [{ realPath: '/empty', pathPrefix: 'files/personal', isDir: true }])
+
+ expect(filesIndexer.dropIndex).toHaveBeenCalledWith('files_content_user_8')
+ })
+
+ it('should insert parsed records and delete stale entries in indexFiles', async () => {
+ filesIndexer.getRecordStats.mockResolvedValueOnce(
+ new Map([
+ [1, { path: 'files/personal', name: 'old.txt', size: 10 }],
+ [2, { path: 'files/personal', name: 'keep.txt', size: 20 }]
+ ])
+ )
+ jest.spyOn(service as any, 'parseFiles').mockImplementation(async function* (_dir: string, context: any) {
+ context.fs.add(2)
+ context.fs.add(3)
+ yield {
+ id: 3,
+ path: 'files/personal',
+ name: 'new.txt',
+ mime: 'text-plain',
+ size: 30,
+ mtime: 1700000000000,
+ content: 'indexed'
+ }
+ })
+
+ await (service as any).indexFiles('user_9', [{ realPath: '/root', pathPrefix: 'files/personal', isDir: true }])
+
+ expect(filesIndexer.insertRecord).toHaveBeenCalledWith(
+ 'files_content_user_9',
+ expect.objectContaining({ id: 3, name: 'new.txt', content: 'indexed' })
+ )
+ expect(filesIndexer.deleteRecords).toHaveBeenCalledWith('files_content_user_9', [1])
+ })
+
+ it('should recursively parse directories and yield file contents only', async () => {
+ const readdirSpy = jest.spyOn(fs, 'readdir')
+ readdirSpy.mockResolvedValueOnce([
+ { parentPath: '/root', name: 'sub', isDirectory: () => true },
+ { parentPath: '/root', name: 'a.txt', isDirectory: () => false }
+ ] as any)
+ readdirSpy.mockResolvedValueOnce([{ parentPath: '/root/sub', name: 'b.txt', isDirectory: () => false }] as any)
+ jest.spyOn(service as any, 'analyzeFile').mockImplementation(async (realPath: string) => ({
+ id: realPath.length,
+ path: 'files/personal',
+ name: path.basename(realPath),
+ mime: 'text-plain',
+ size: 1,
+ mtime: 1,
+ content: 'ok'
+ }))
+ const yielded: any[] = []
+
+ for await (const fileContent of (service as any).parseFiles('/root', {
+ indexSuffix: 'user_1',
+ pathPrefix: 'files/personal',
+ regexBasePath: /^\/?root\/?/,
+ db: new Map(),
+ fs: new Set()
+ })) {
+ yielded.push(fileContent)
+ }
+
+ expect(yielded.map((f) => f.name)).toEqual(['b.txt', 'a.txt'])
+ })
+
+ it('should skip non-indexable files and unchanged files in analyzeFile', async () => {
+ const statSpy = jest.spyOn(fs, 'stat')
+ const context = {
+ indexSuffix: 'user_1',
+ pathPrefix: 'files/personal',
+ regexBasePath: /^\/?data\/?/,
+ db: new Map<number, { name: string; path: string; size: number }>([[123, { path: 'files/personal/sub', name: 'doc.txt', size: 12 }]]),
+ fs: new Set<number>()
+ }
+
+ const unsupported = await (service as any).analyzeFile('/data/image.png', context, false)
+ expect(unsupported).toBeNull()
+ expect(statSpy).not.toHaveBeenCalled()
+
+ statSpy.mockResolvedValueOnce({
+ ino: 123,
+ size: 12,
+ mtime: new Date('2024-01-01T00:00:00.000Z')
+ } as any)
+ const unchanged = await (service as any).analyzeFile('/data/sub/doc.txt', context, false)
+ expect(unchanged).toBeNull()
+ expect(context.fs.has(123)).toBe(true)
+ })
+
+ it('should build indexed file content in analyzeFile for changed files', async () => {
+ const context = {
+ indexSuffix: 'user_1',
+ pathPrefix: 'files/personal',
+ regexBasePath: /^\/?data\/base\/?/,
+ db: new Map<number, { name: string; path: string; size: number }>(),
+ fs: new Set<number>()
+ }
+ jest.spyOn(fs, 'stat').mockResolvedValueOnce({
+ ino: 200,
+ size: 42,
+ mtime: new Date('2024-01-02T03:04:05.000Z')
+ } as any)
+ jest.spyOn(service as any, 'parseContent').mockResolvedValueOnce('indexed content')
+
+ const fileContent = await (service as any).analyzeFile('/data/base/sub/doc.txt', context, false)
+
+ expect(fileContent).toEqual({
+ id: 200,
+ path: 'files/personal/sub',
+ name: 'doc.txt',
+ mime: 'text-plain',
+ size: 42,
+ mtime: new Date('2024-01-02T03:04:05.000Z').getTime(),
+ content: 'indexed content'
+ })
+ expect(context.fs.has(200)).toBe(true)
+ })
+
+ it('should parse content and return null when parser returns empty or throws', async () => {
+ ;(service as any).pdfOcrWorkerManager = { worker: { id: 'worker-1' } }
+ const docTextifySpy = jest.spyOn(docTextifyModule, 'docTextify')
+ docTextifySpy.mockResolvedValueOnce('')
+ docTextifySpy.mockRejectedValueOnce(new Error('parse failed'))
+
+ const empty = await (service as any).parseContent('/tmp/a.txt', 'txt')
+ const failed = await (service as any).parseContent('/tmp/a.txt', 'txt')
+
+ expect(empty).toBeNull()
+ expect(failed).toBeNull()
+ expect(docTextifySpy).toHaveBeenNthCalledWith(
+ 1,
+ '/tmp/a.txt',
+ expect.objectContaining({ newlineDelimiter: ' ', minCharsToExtract: 10, ocrWorker: { id: 'worker-1' } }),
+ { extension: 'txt', verified: true }
+ )
+ })
+})
diff --git a/backend/src/applications/files/services/files-event-manager.service.spec.ts b/backend/src/applications/files/services/files-event-manager.service.spec.ts
index 4c1214f5..bf58a4d3 100644
--- a/backend/src/applications/files/services/files-event-manager.service.spec.ts
+++ b/backend/src/applications/files/services/files-event-manager.service.spec.ts
@@ -1,25 +1,126 @@
import { Test, TestingModule } from '@nestjs/testing'
import { FilesEventManager } from './files-event-manager.service'
import { Cache } from '../../../infrastructure/cache/services/cache.service'
+import { ACTION } from '../../../common/constants'
+import { FileEvent } from '../events/file-events'
+import type { FileEventType } from '../interfaces/file-event.interface'
+import { CACHE_QUOTA_TTL } from '../constants/cache'
+import { CACHE_INDEXING_TTL } from '../constants/indexing'
+import { quotaCacheKeyFromSpace } from '../utils/quota'
+import { indexingUpdateCacheKeysFromSpace } from '../utils/indexing'
+import { configuration } from '../../../configuration/config.environment'
describe(FilesEventManager.name, () => {
let service: FilesEventManager
+ let cacheSetMock: jest.Mock<Promise<boolean>, [string, unknown, number?]>
+ let contentIndexingEnabled: boolean
- beforeAll(async () => {
+ const buildEvent = (props?: Partial<FileEventType>): FileEventType =>
+ ({
+ user: { id: 7, login: 'john' } as any,
+ space: { id: 13, alias: 'project', inPersonalSpace: false, inSharesRepository: false, root: {} } as any,
+ action: ACTION.ADD,
+ rPath: '/files/document.bin',
+ ...props
+ }) as FileEventType
+
+ beforeEach(async () => {
+ cacheSetMock = jest.fn().mockResolvedValue(true)
const module: TestingModule = await Test.createTestingModule({
providers: [
FilesEventManager,
{
provide: Cache,
- useValue: {}
+ useValue: { set: cacheSetMock }
}
]
}).compile()
+ module.useLogger(['fatal'])
service = module.get<FilesEventManager>(FilesEventManager)
+ contentIndexingEnabled = configuration.applications.files.contentIndexing.enabled
+ })
+
+ afterEach(async () => {
+ configuration.applications.files.contentIndexing.enabled = contentIndexingEnabled
+ jest.useRealTimers()
+ jest.clearAllMocks()
+ await service.onModuleDestroy()
})
it('should be defined', () => {
expect(service).toBeDefined()
})
+
+ it('should cache a quota key once for duplicate non-delete events', async () => {
+ const event = buildEvent({ rPath: '/files/document.tmp' })
+
+ await (service as any).processEvents([event, { ...event, action: ACTION.UPDATE }])
+
+ expect(cacheSetMock).toHaveBeenCalledTimes(1)
+ expect(cacheSetMock).toHaveBeenCalledWith(quotaCacheKeyFromSpace(event.user.id, event.space, true), true, CACHE_QUOTA_TTL)
+ })
+
+ it('should ignore quota cache update for delete events', async () => {
+ await (service as any).processEvents([buildEvent({ action: ACTION.DELETE, rPath: '/files/deleted.tmp' })])
+
+ expect(cacheSetMock).not.toHaveBeenCalled()
+ })
+
+ it('should cache quota and indexing keys for indexable files', async () => {
+ configuration.applications.files.contentIndexing.enabled = true
+ const event = buildEvent({
+ rPath: '/shares/spec.pdf',
+ space: { id: 42, alias: 'shared-space', inPersonalSpace: false, inSharesRepository: true, root: {} } as any
+ })
+ const quotaKey = quotaCacheKeyFromSpace(event.user.id, event.space, true)
+ const indexingKeys = indexingUpdateCacheKeysFromSpace(event.user.id, event.space)
+
+ await (service as any).processEvents([event])
+
+ expect(cacheSetMock).toHaveBeenCalledTimes(1 + indexingKeys.length)
+ expect(cacheSetMock).toHaveBeenCalledWith(quotaKey, true, CACHE_QUOTA_TTL)
+ for (const key of indexingKeys) {
+ expect(cacheSetMock).toHaveBeenCalledWith(key, true, CACHE_INDEXING_TTL)
+ }
+ })
+
+ it('should retry failed cache keys on the next flush', async () => {
+ const event = buildEvent()
+ const quotaKey = quotaCacheKeyFromSpace(event.user.id, event.space, true)
+ cacheSetMock.mockResolvedValueOnce(false).mockResolvedValueOnce(true)
+
+ await (service as any).processEvents([event])
+ await (service as any).processEvents([])
+
+ expect(cacheSetMock).toHaveBeenNthCalledWith(1, quotaKey, true, CACHE_QUOTA_TTL)
+ expect(cacheSetMock).toHaveBeenNthCalledWith(2, quotaKey, true, CACHE_QUOTA_TTL)
+ })
+
+ it('should flush buffered events and unsubscribe on module destroy', async () => {
+ const event = buildEvent()
+ const listenersBeforeDestroy = FileEvent.listenerCount('event')
+ FileEvent.emit('event', event)
+
+ await service.onModuleDestroy()
+
+ expect(cacheSetMock).toHaveBeenCalledWith(quotaCacheKeyFromSpace(event.user.id, event.space, true), true, CACHE_QUOTA_TTL)
+ expect(FileEvent.listenerCount('event')).toBe(listenersBeforeDestroy - 1)
+ cacheSetMock.mockClear()
+
+ FileEvent.emit('event', event)
+ await (service as any).flushEvents()
+ expect(cacheSetMock).not.toHaveBeenCalled()
+ })
+
+ it('should flush immediately when max buffer size is reached', async () => {
+ ;(service as any).MAX_BUFFER_SIZE = 2
+ const event = buildEvent()
+
+ ;(service as any).onFileEvent(event)
+ ;(service as any).onFileEvent(event)
+ await new Promise((resolve) => setImmediate(resolve))
+
+ expect(cacheSetMock).toHaveBeenCalledWith(quotaCacheKeyFromSpace(event.user.id, event.space, true), true, CACHE_QUOTA_TTL)
+ })
})
diff --git a/backend/src/applications/files/services/files-lock-manager.service.spec.ts b/backend/src/applications/files/services/files-lock-manager.service.spec.ts
index b4570e59..08401403 100644
--- a/backend/src/applications/files/services/files-lock-manager.service.spec.ts
+++ b/backend/src/applications/files/services/files-lock-manager.service.spec.ts
@@ -1,25 +1,346 @@
import { Test, TestingModule } from '@nestjs/testing'
+import crypto from 'node:crypto'
+import { currentTimeStamp } from '../../../common/shared'
import { Cache } from '../../../infrastructure/cache/services/cache.service'
-import { DB_TOKEN_PROVIDER } from '../../../infrastructure/database/constants'
+import { CACHE_LOCK_PREFIX } from '../constants/cache'
+import { LockConflict } from '../models/file-lock-error'
+import { DEPTH, LOCK_PREFIX, LOCK_SCOPE, WEBDAV_APP_LOCK } from '../../webdav/constants/webdav'
import { FilesLockManager } from './files-lock-manager.service'
describe(FilesLockManager.name, () => {
- let module: TestingModule
let filesLockManager: FilesLockManager
+ let cacheStore: Map<string, any>
+ let cache: {
+ set: jest.Mock
+ get: jest.Mock
+ mget: jest.Mock
+ del: jest.Mock
+ keys: jest.Mock
+ }
- beforeAll(async () => {
- module = await Test.createTestingModule({
- providers: [FilesLockManager, { provide: DB_TOKEN_PROVIDER, useValue: {} }, { provide: Cache, useValue: {} }]
+ const createPatternRegex = (pattern: string): RegExp => {
+ const escapedPattern = pattern.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*')
+ return new RegExp(`^${escapedPattern}$`)
+ }
+
+ const generatorFrom = <T>(items: T[]) =>
+ (async function* () {
+ for (const item of items) {
+ yield item
+ }
+ })()
+
+ beforeEach(async () => {
+ cacheStore = new Map<string, any>()
+ cache = {
+ set: jest.fn(async (key: string, value: unknown) => {
+ cacheStore.set(key, value)
+ return true
+ }),
+ get: jest.fn(async (key: string) => cacheStore.get(key)),
+ mget: jest.fn(async (keys: string[]) => keys.map((k) => cacheStore.get(k))),
+ del: jest.fn(async (key: string) => cacheStore.delete(key)),
+ keys: jest.fn(async (pattern: string) => [...cacheStore.keys()].filter((k) => createPatternRegex(pattern).test(k)))
+ }
+ const module: TestingModule = await Test.createTestingModule({
+ providers: [FilesLockManager, { provide: Cache, useValue: cache }]
}).compile()
+ module.useLogger(['fatal'])
filesLockManager = module.get<FilesLockManager>(FilesLockManager)
})
- afterAll(async () => {
- await module.close()
+ afterEach(() => {
+ jest.restoreAllMocks()
+ jest.clearAllMocks()
})
it('should be defined', () => {
expect(filesLockManager).toBeDefined()
})
+
+ it('should remove lock by key using cache', async () => {
+ cacheStore.set('flock|key-1', { key: 'flock|key-1' })
+
+ const removed = await filesLockManager.removeLock('flock|key-1')
+
+ expect(removed).toBe(true)
+ expect(cache.del).toHaveBeenCalledWith('flock|key-1')
+ })
+
+ it('should create and cache a lock when there is no conflict', async () => {
+ const user = {
+ id: 10,
+ asOwner: () => ({ id: 10, login: 'john', email: '[email protected]', fullName: 'John' })
+ } as any
+ const dbFile = { path: 'docs/file.txt', ownerId: 10, inTrash: false } as any
+ const conflictSpy = jest.spyOn(filesLockManager, 'checkConflicts').mockResolvedValueOnce(undefined)
+
+ const [created, lock] = await filesLockManager.create(
+ user,
+ dbFile,
+ WEBDAV_APP_LOCK,
+ DEPTH.RESOURCE,
+ { lockRoot: '/webdav/docs/file.txt', lockToken: 'opaquelocktoken:t1', lockScope: LOCK_SCOPE.EXCLUSIVE },
+ 100
+ )
+
+ expect(conflictSpy).toHaveBeenCalledWith(dbFile, DEPTH.RESOURCE, { app: WEBDAV_APP_LOCK, lockScope: LOCK_SCOPE.EXCLUSIVE })
+ expect(created).toBe(true)
+ expect(lock.key).toContain(`${CACHE_LOCK_PREFIX}|token:opaquelocktoken:t1|depth:0|path:docs/file.txt|ownerId:10`)
+ expect(cache.set).toHaveBeenCalledWith(lock.key, lock, 100)
+ expect(lock.expiration).toBeGreaterThan(currentTimeStamp())
+ })
+
+ it('should return existing conflict lock on create', async () => {
+ const conflictLock = {
+ owner: { id: 99, login: 'other', email: '[email protected]', fullName: 'Other' },
+ dbFilePath: 'docs/file.txt',
+ key: 'flock|x'
+ } as any
+ jest.spyOn(filesLockManager, 'checkConflicts').mockRejectedValueOnce(new LockConflict(conflictLock, 'conflict'))
+
+ const [created, lock] = await filesLockManager.create(
+ { id: 10, asOwner: () => ({ id: 10, login: 'john', email: '', fullName: '' }) } as any,
+ { path: 'docs/file.txt', ownerId: 10, inTrash: false } as any,
+ WEBDAV_APP_LOCK,
+ DEPTH.INFINITY
+ )
+
+ expect(created).toBe(false)
+ expect(lock).toBe(conflictLock)
+ expect(cache.set).not.toHaveBeenCalled()
+ })
+
+ it('should refresh owned lock when createOrRefresh finds a stale lock', async () => {
+ const lock = {
+ key: 'flock|k1',
+ owner: { id: 10, login: 'john' },
+ expiration: currentTimeStamp() + 100
+ } as any
+ jest.spyOn(filesLockManager, 'getLocksByPath').mockResolvedValueOnce([lock])
+ const refreshSpy = jest.spyOn(filesLockManager, 'refreshLockTimeout').mockResolvedValueOnce(undefined)
+
+ const [created, result] = await filesLockManager.createOrRefresh(
+ { id: 10 } as any,
+ { path: 'docs/file.txt', ownerId: 10, inTrash: false } as any,
+ WEBDAV_APP_LOCK,
+ DEPTH.RESOURCE,
+ 300
+ )
+
+ expect(created).toBe(false)
+ expect(result).toBe(lock)
+ expect(refreshSpy).toHaveBeenCalledWith(lock, 300)
+ })
+
+ it('should throw conflict in createOrRefresh when lock is owned by another user', async () => {
+ const lock = { key: 'flock|k2', owner: { id: 42, login: 'alice' } } as any
+ jest.spyOn(filesLockManager, 'getLocksByPath').mockResolvedValueOnce([lock])
+
+ await expect(
+ filesLockManager.createOrRefresh(
+ { id: 10 } as any,
+ { path: 'docs/file.txt', ownerId: 10, inTrash: false } as any,
+ WEBDAV_APP_LOCK,
+ DEPTH.RESOURCE
+ )
+ ).rejects.toEqual(new LockConflict(lock, 'Conflicting lock'))
+ })
+
+ it('should create a new lock in createOrRefresh when no lock exists', async () => {
+ const lock = { key: 'flock|k3', owner: { id: 10 } } as any
+ jest.spyOn(filesLockManager, 'getLocksByPath').mockResolvedValueOnce([])
+ const createSpy = jest.spyOn(filesLockManager, 'create').mockResolvedValueOnce([true, lock])
+
+ const [created, result] = await filesLockManager.createOrRefresh(
+ { id: 10 } as any,
+ { path: 'docs/file.txt', ownerId: 10, inTrash: false } as any,
+ WEBDAV_APP_LOCK,
+ DEPTH.RESOURCE,
+ 55
+ )
+
+ expect(createSpy).toHaveBeenCalledWith(
+ { id: 10 },
+ { path: 'docs/file.txt', ownerId: 10, inTrash: false },
+ WEBDAV_APP_LOCK,
+ DEPTH.RESOURCE,
+ null,
+ 55
+ )
+ expect(created).toBe(true)
+ expect(result).toBe(lock)
+ })
+
+ it('should return locks by file path', async () => {
+ const key = `${CACHE_LOCK_PREFIX}|depth:0|path:docs/file.txt|ownerId:10`
+ cacheStore.set(key, { key, dbFilePath: 'docs/file.txt', owner: { login: 'john' } })
+
+ const locks = await filesLockManager.getLocksByPath({ path: 'docs/file.txt', ownerId: 10, inTrash: false } as any)
+
+ expect(locks).toHaveLength(1)
+ expect(locks[0].dbFilePath).toBe('docs/file.txt')
+ })
+
+ it('should browse locks and return keyed object by file name', async () => {
+ jest.spyOn(filesLockManager, 'getLocksByPath').mockResolvedValueOnce([
+ { dbFilePath: 'docs/a.txt', owner: { login: 'john' } },
+ { dbFilePath: 'docs/b.txt', owner: { login: 'alice' } }
+ ] as any)
+
+ const locks = await filesLockManager.browseLocks({ path: 'docs', ownerId: 10, inTrash: false } as any)
+
+ expect(locks).toEqual({
+ 'a.txt': expect.objectContaining({ dbFilePath: 'docs/a.txt' }),
+ 'b.txt': expect.objectContaining({ dbFilePath: 'docs/b.txt' })
+ })
+ })
+
+ it('should return false for isPathLocked when path is not root path', async () => {
+ const spy = jest.spyOn(filesLockManager as any, 'searchKeysByPath')
+
+ const isLocked = await filesLockManager.isPathLocked({ path: 'docs/file.txt', ownerId: 1, inTrash: false } as any)
+
+ expect(isLocked).toBe(false)
+ expect(spy).not.toHaveBeenCalled()
+ })
+
+ it('should return true for isPathLocked when root path has lock keys', async () => {
+ const spy = jest.spyOn(filesLockManager as any, 'searchKeysByPath').mockResolvedValueOnce(['flock|root-key'])
+
+ const isLocked = await filesLockManager.isPathLocked({ path: '.', ownerId: 1, inTrash: false } as any)
+
+ expect(isLocked).toBe(true)
+ expect(spy).toHaveBeenCalled()
+ })
+
+ it('should resolve lock by token and match path ancestry', async () => {
+ const key = `${CACHE_LOCK_PREFIX}|token:opaquelocktoken:t1|depth:infinity|path:docs|ownerId:1`
+ const lock = { key, dbFilePath: 'docs', owner: { id: 1, login: 'john' } }
+ cacheStore.set(key, lock)
+
+ await expect(filesLockManager.getLockByToken('opaquelocktoken:t1')).resolves.toBe(lock)
+ await expect(filesLockManager.isLockedWithToken('opaquelocktoken:t1', 'docs/sub/file.txt')).resolves.toBe(lock)
+ await expect(filesLockManager.isLockedWithToken('opaquelocktoken:t1', 'other/path')).resolves.toBeNull()
+ })
+
+ it('should browse parent and child locks and keep first shared duplicate name', async () => {
+ jest.spyOn(filesLockManager, 'browseLocks').mockResolvedValueOnce({
+ root: { dbFilePath: 'docs/root' } as any
+ })
+ jest
+ .spyOn(filesLockManager as any, 'searchChildLocks')
+ .mockReturnValue(
+ generatorFrom([
+ { dbFilePath: 'docs/a.txt', owner: { login: 'a' } } as any,
+ { dbFilePath: 'docs/a.txt', owner: { login: 'b' } } as any,
+ { dbFilePath: 'docs/sub/b.txt', owner: { login: 'c' } } as any
+ ])
+ )
+
+ const locks = await filesLockManager.browseParentChildLocks({ path: 'docs', ownerId: 1, inTrash: false } as any)
+
+ expect(locks.root).toBeDefined()
+ expect(locks['a.txt']).toEqual(expect.objectContaining({ dbFilePath: 'docs/a.txt', owner: { login: 'a' } }))
+ expect(locks['b.txt']).toBeUndefined()
+ })
+
+ it('should remove owned child locks and throw on conflicting owner lock', async () => {
+ const removeSpy = jest.spyOn(filesLockManager, 'removeLock').mockResolvedValue(true)
+
+ jest
+ .spyOn(filesLockManager as any, 'searchChildLocks')
+ .mockReturnValueOnce(
+ generatorFrom([
+ { key: 'k1', owner: { id: 7, login: 'john' }, dbFilePath: 'docs/a.txt' } as any,
+ { key: 'k2', owner: { id: 7, login: 'john' }, dbFilePath: 'docs/b.txt' } as any
+ ])
+ )
+ await filesLockManager.removeChildLocks({ id: 7, login: 'john' } as any, { path: 'docs', ownerId: 7, inTrash: false } as any)
+ expect(removeSpy).toHaveBeenCalledWith('k1')
+ expect(removeSpy).toHaveBeenCalledWith('k2')
+
+ jest
+ .spyOn(filesLockManager as any, 'searchChildLocks')
+ .mockReturnValueOnce(generatorFrom([{ key: 'k3', owner: { id: 9, login: 'alice' }, dbFilePath: 'docs/c.txt' } as any]))
+ await expect(
+ filesLockManager.removeChildLocks({ id: 7, login: 'john' } as any, { path: 'docs', ownerId: 7, inTrash: false } as any)
+ ).rejects.toBeInstanceOf(LockConflict)
+ })
+
+ it('should accept shared same-app conflicts and owner token matches in checkConflicts', async () => {
+ jest.spyOn(filesLockManager as any, 'searchParentLocks').mockReturnValue(
+ generatorFrom([
+ {
+ owner: { id: 2, login: 'alice' },
+ app: WEBDAV_APP_LOCK,
+ options: { lockScope: LOCK_SCOPE.SHARED }
+ } as any
+ ])
+ )
+ jest.spyOn(filesLockManager as any, 'searchChildLocks').mockReturnValue(
+ generatorFrom([
+ {
+ owner: { id: 7, login: 'john' },
+ app: WEBDAV_APP_LOCK,
+ options: { lockToken: 'opaquelocktoken:t1' }
+ } as any
+ ])
+ )
+
+ await expect(
+ filesLockManager.checkConflicts({ path: 'docs/file.txt', ownerId: 7, inTrash: false } as any, DEPTH.INFINITY, {
+ userId: 7,
+ app: WEBDAV_APP_LOCK,
+ lockScope: LOCK_SCOPE.SHARED,
+ lockTokens: ['opaquelocktoken:t1']
+ })
+ ).resolves.toBeUndefined()
+ })
+
+ it('should throw on parent conflict in checkConflicts', async () => {
+ const parentLock = {
+ owner: { id: 99, login: 'other' },
+ dbFilePath: 'docs',
+ app: WEBDAV_APP_LOCK,
+ options: { lockScope: LOCK_SCOPE.EXCLUSIVE }
+ } as any
+ jest.spyOn(filesLockManager as any, 'searchParentLocks').mockReturnValue(generatorFrom([parentLock]))
+ jest.spyOn(filesLockManager as any, 'searchChildLocks').mockReturnValue(generatorFrom([]))
+
+ await expect(
+ filesLockManager.checkConflicts({ path: 'docs/file.txt', ownerId: 7, inTrash: false } as any, DEPTH.RESOURCE, {
+ userId: 7,
+ app: WEBDAV_APP_LOCK
+ })
+ ).rejects.toEqual(new LockConflict(parentLock, 'conflicting parent lock : docs/file.txt -> docs (other)'))
+ })
+
+ it('should convert lock to file lock props and generate a DAV token', () => {
+ jest.spyOn(crypto, 'randomUUID').mockReturnValueOnce('33333333-3333-4333-8333-333333333333')
+ const lock = {
+ owner: { id: 2, login: 'alice' },
+ app: WEBDAV_APP_LOCK,
+ options: { lockInfo: 'office', lockScope: LOCK_SCOPE.SHARED }
+ } as any
+
+ const props = filesLockManager.convertLockToFileLockProps(lock)
+ const token = filesLockManager.genDAVToken()
+
+ expect(props).toEqual({ owner: { id: 2, login: 'alice' }, app: WEBDAV_APP_LOCK, info: 'office', isExclusive: false })
+ expect(token).toBe(`${LOCK_PREFIX}33333333-3333-4333-8333-333333333333`)
+ })
+
+ it('should refresh lock timeout and persist updated expiration', async () => {
+ const lock = {
+ key: 'flock|refresh',
+ expiration: currentTimeStamp() + 1
+ } as any
+
+ await filesLockManager.refreshLockTimeout(lock, 120)
+
+ expect(lock.expiration).toBeGreaterThan(currentTimeStamp())
+ expect(cache.set).toHaveBeenCalledWith('flock|refresh', lock, 120)
+ })
})
diff --git a/backend/src/applications/files/services/files-manager.service.spec.ts b/backend/src/applications/files/services/files-manager.service.spec.ts
index eef29ebe..65cd420c 100644
--- a/backend/src/applications/files/services/files-manager.service.spec.ts
+++ b/backend/src/applications/files/services/files-manager.service.spec.ts
@@ -1,37 +1,506 @@
import { HttpService } from '@nestjs/axios'
+import { HttpStatus } from '@nestjs/common'
import { Test, TestingModule } from '@nestjs/testing'
+import archiver from 'archiver'
+import fs from 'node:fs'
+import path from 'node:path'
+import { Readable } from 'node:stream'
+import * as tar from 'tar'
+import * as imageUtils from '../../../common/image'
import { ContextManager } from '../../../infrastructure/context/services/context-manager.service'
-import { DB_TOKEN_PROVIDER } from '../../../infrastructure/database/constants'
import { NotificationsManager } from '../../notifications/services/notifications-manager.service'
import { SpacesManager } from '../../spaces/services/spaces-manager.service'
+import * as spacesPathUtils from '../../spaces/utils/paths'
+import * as spacesPermsUtils from '../../spaces/utils/permissions'
+import { DEPTH } from '../../webdav/constants/webdav'
+import { ACTION } from '../../../common/constants'
+import { FILE_OPERATION } from '../constants/operations'
+import { FileEvent, FileTaskEvent } from '../events/file-events'
+import { FileError } from '../models/file-error'
+import { LockConflict } from '../models/file-lock-error'
+import { SendFile } from '../utils/send-file'
+import * as unzipUtils from '../utils/unzip-file'
+import * as filesUtils from '../utils/files'
import { FilesLockManager } from './files-lock-manager.service'
import { FilesManager } from './files-manager.service'
import { FilesQueries } from './files-queries.service'
+jest.mock('archiver', () => ({
+ __esModule: true,
+ default: jest.fn()
+}))
+
+jest.mock('tar', () => ({
+ __esModule: true,
+ extract: jest.fn()
+}))
+
describe(FilesManager.name, () => {
let service: FilesManager
+ let http: { axiosRef: jest.Mock }
+ let filesQueries: { moveFiles: jest.Mock; deleteFiles: jest.Mock }
+ let spacesManager: { spaceEnv: jest.Mock }
+ let contextManager: { headerOriginUrl: jest.Mock }
+ let notificationsManager: { create: jest.Mock }
+ let filesLockManager: {
+ create: jest.Mock
+ checkConflicts: jest.Mock
+ removeLock: jest.Mock
+ createOrRefresh: jest.Mock
+ getLocksByPath: jest.Mock
+ convertLockToFileLockProps: jest.Mock
+ removeChildLocks: jest.Mock
+ }
+
+ const user = { id: 7, login: 'john', tasksPath: '/data/users/john/tmp/tasks' } as any
+
+ const makeSpace = (overrides: Record<string, any> = {}) =>
+ ({
+ id: 1,
+ alias: 'personal',
+ repository: 'files',
+ url: 'files/personal/file.txt',
+ realPath: '/data/users/john/files/file.txt',
+ realBasePath: '/data/users/john/files',
+ dbFile: { ownerId: 7, path: 'file.txt', inTrash: false },
+ inTrashRepository: false,
+ quotaIsExceeded: false,
+ storageQuota: null,
+ willExceedQuota: jest.fn().mockReturnValue(false),
+ task: { cacheKey: '', props: {} },
+ ...overrides
+ }) as any
+
+ const setPathExists = (values: Record<string, boolean>, fallback = false) => {
+ ;(filesUtils.isPathExists as jest.Mock).mockImplementation(async (p: string) => (p in values ? values[p] : fallback))
+ }
+
+ beforeEach(async () => {
+ http = { axiosRef: jest.fn() }
+ filesQueries = {
+ moveFiles: jest.fn().mockResolvedValue(undefined),
+ deleteFiles: jest.fn().mockResolvedValue(undefined)
+ }
+ spacesManager = {
+ spaceEnv: jest.fn().mockResolvedValue(makeSpace())
+ }
+ contextManager = {
+ headerOriginUrl: jest.fn().mockReturnValue('https://sync-in.example')
+ }
+ notificationsManager = {
+ create: jest.fn().mockResolvedValue(undefined)
+ }
+ filesLockManager = {
+ create: jest.fn().mockResolvedValue([true, { key: 'lock-1' }]),
+ checkConflicts: jest.fn().mockResolvedValue(undefined),
+ removeLock: jest.fn().mockResolvedValue(true),
+ createOrRefresh: jest.fn().mockResolvedValue([false, { key: 'lock-2' }]),
+ getLocksByPath: jest.fn().mockResolvedValue([]),
+ convertLockToFileLockProps: jest.fn().mockReturnValue({ owner: { id: 7, login: 'john' }, app: 'Sync-in', isExclusive: true }),
+ removeChildLocks: jest.fn().mockResolvedValue(undefined)
+ }
- beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
- { provide: DB_TOKEN_PROVIDER, useValue: {} },
- { provide: FilesLockManager, useValue: {} },
- { provide: SpacesManager, useValue: {} },
- { provide: ContextManager, useValue: {} },
- { provide: NotificationsManager, useValue: {} },
- {
- provide: HttpService,
- useValue: {}
- },
- FilesManager,
- FilesQueries
+ { provide: FilesQueries, useValue: filesQueries },
+ { provide: SpacesManager, useValue: spacesManager },
+ { provide: ContextManager, useValue: contextManager },
+ { provide: NotificationsManager, useValue: notificationsManager },
+ { provide: HttpService, useValue: http },
+ { provide: FilesLockManager, useValue: filesLockManager },
+ FilesManager
]
}).compile()
+ module.useLogger(['fatal'])
service = module.get<FilesManager>(FilesManager)
+
+ jest.spyOn(filesUtils, 'isPathExists').mockResolvedValue(true)
+ jest.spyOn(filesUtils, 'isPathIsDir').mockResolvedValue(false)
+ jest.spyOn(filesUtils, 'makeDir').mockResolvedValue('/tmp' as any)
+ jest.spyOn(filesUtils, 'writeFromStream').mockResolvedValue(undefined)
+ jest.spyOn(filesUtils, 'writeFromStreamAndChecksum').mockResolvedValue('sha256-abc')
+ jest.spyOn(filesUtils, 'moveFiles').mockResolvedValue(undefined)
+ jest.spyOn(filesUtils, 'copyFiles').mockResolvedValue(undefined)
+ jest.spyOn(filesUtils, 'removeFiles').mockResolvedValue(undefined)
+ jest.spyOn(filesUtils, 'touchFile').mockResolvedValue(undefined)
+ jest.spyOn(filesUtils, 'createEmptyFile').mockResolvedValue(undefined)
+ jest.spyOn(filesUtils, 'copyFileContent').mockResolvedValue(undefined)
+ jest.spyOn(filesUtils, 'fileSize').mockResolvedValue(100)
+ jest.spyOn(filesUtils, 'dirSize').mockResolvedValue([123, {}] as any)
+ jest.spyOn(filesUtils, 'uniqueFilePathFromDir').mockResolvedValue('/tmp/unique-path.txt')
+ jest.spyOn(filesUtils, 'uniqueDatedFilePath').mockResolvedValue({ isDir: false, path: '/trash/file-2026.txt' })
+ jest.spyOn(filesUtils, 'getMimeType').mockReturnValue('image-png')
+ jest.spyOn(spacesPermsUtils, 'canAccessToSpace').mockReturnValue(true)
+ jest.spyOn(spacesPermsUtils, 'haveSpaceEnvPermissions').mockReturnValue(true)
+ jest.spyOn(spacesPathUtils, 'realTrashPathFromSpace').mockReturnValue('/data/users/john/trash')
+ })
+
+ afterEach(() => {
+ jest.restoreAllMocks()
+ jest.clearAllMocks()
})
it('should be defined', () => {
expect(service).toBeDefined()
})
+
+ it('sendFileFromSpace should return a SendFile instance', () => {
+ const space = makeSpace()
+ const sendFile = service.sendFileFromSpace(space, 'download.txt')
+ expect(sendFile).toBeInstanceOf(SendFile)
+ })
+
+ it('saveStream should reject POST when resource already exists', async () => {
+ const space = makeSpace()
+ setPathExists({ [space.realPath]: true }, true)
+
+ await expect(service.saveStream(user, space, { method: 'POST', headers: {}, raw: Readable.from(['x']) } as any)).rejects.toEqual(
+ new FileError(HttpStatus.METHOD_NOT_ALLOWED, 'Resource already exists')
+ )
+ })
+
+ it('saveStream should write stream, emit event and release lock', async () => {
+ const space = makeSpace()
+ setPathExists({ [space.realPath]: false, [path.dirname(space.realPath)]: true }, false)
+ const emitSpy = jest.spyOn(FileEvent, 'emit')
+
+ const result = await service.saveStream(user, space, { method: 'PUT', headers: {}, raw: Readable.from(['hello']) } as any)
+
+ expect(result).toBe(false)
+ expect(filesLockManager.create).toHaveBeenCalledWith(user, space.dbFile, 'Sync-in', DEPTH.RESOURCE)
+ expect(filesUtils.writeFromStream).toHaveBeenCalledWith(space.realPath, expect.anything(), 0)
+ expect(filesLockManager.removeLock).toHaveBeenCalledWith('lock-1')
+ expect(emitSpy).toHaveBeenCalledWith('event', { user, space, action: ACTION.ADD, rPath: space.realPath })
+ })
+
+ it('saveStream should use DAV conflict checks and checksum mode when requested', async () => {
+ const space = makeSpace()
+ setPathExists({ [space.realPath]: true, [path.dirname(space.realPath)]: true }, true)
+
+ const checksum = await service.saveStream(
+ user,
+ space,
+ { method: 'PUT', headers: { 'content-range': 'bytes 100-199/200' }, raw: Readable.from(['chunk']) } as any,
+ { dav: { depth: DEPTH.RESOURCE, lockTokens: ['token'] }, checksumAlg: 'sha256' }
+ )
+
+ expect(checksum).toBe('sha256-abc')
+ expect(filesLockManager.checkConflicts).toHaveBeenCalledWith(space.dbFile, DEPTH.RESOURCE, { userId: 7, lockTokens: ['token'] })
+ expect(filesLockManager.create).not.toHaveBeenCalled()
+ expect(filesUtils.writeFromStreamAndChecksum).toHaveBeenCalled()
+ })
+
+ it('saveMultipart should write one PATCH part and emit update event', async () => {
+ const space = makeSpace({
+ url: 'files/personal/report.txt',
+ realPath: '/data/users/john/files/report.txt',
+ dbFile: { ownerId: 7, path: 'report.txt' }
+ })
+ setPathExists({ [path.dirname(space.realPath)]: true, [space.realPath]: true }, false)
+ ;(filesUtils.isPathIsDir as jest.Mock).mockResolvedValue(true)
+ const emitSpy = jest.spyOn(FileEvent, 'emit')
+
+ const req = {
+ method: 'PATCH',
+ files: async function* () {
+ yield { filename: 'ignored-on-patch.txt', file: Readable.from(['content']) }
+ }
+ }
+
+ await service.saveMultipart(user, space, req as any)
+
+ expect(filesLockManager.createOrRefresh).toHaveBeenCalled()
+ expect(filesUtils.writeFromStream).toHaveBeenCalledWith('/data/users/john/files/report.txt', expect.anything())
+ expect(emitSpy).toHaveBeenCalledWith('event', expect.objectContaining({ action: ACTION.UPDATE, rPath: '/data/users/john/files/report.txt' }))
+ })
+
+ it('touch should fail when location does not exist', async () => {
+ const space = makeSpace()
+ ;(filesUtils.isPathExists as jest.Mock).mockResolvedValueOnce(false)
+
+ await expect(service.touch(user, space, 123456)).rejects.toEqual(new FileError(HttpStatus.NOT_FOUND, 'Location not found'))
+ })
+
+ it('touch should check locks and update mtime', async () => {
+ const space = makeSpace()
+ ;(filesUtils.isPathExists as jest.Mock).mockResolvedValueOnce(true)
+
+ await service.touch(user, space, 111)
+
+ expect(filesLockManager.checkConflicts).toHaveBeenCalledWith(space.dbFile, DEPTH.RESOURCE, { userId: 7 })
+ expect(filesUtils.touchFile).toHaveBeenCalledWith(space.realPath, 111)
+ })
+
+ it('mkFile should use sample document when requested', async () => {
+ const space = makeSpace({ realPath: '/data/users/john/files/doc.docx' })
+ ;(filesUtils.isPathExists as jest.Mock).mockResolvedValueOnce(false)
+ const emitSpy = jest.spyOn(FileEvent, 'emit')
+
+ await service.mkFile(user, space, false, true, true)
+
+ expect(filesUtils.copyFileContent).toHaveBeenCalledWith(expect.stringContaining('assets/samples/sample.docx'), '/data/users/john/files/doc.docx')
+ expect(emitSpy).toHaveBeenCalledWith('event', { user, space, action: ACTION.ADD, rPath: space.realPath })
+ })
+
+ it('mkDir should check conflicts and create directory', async () => {
+ const space = makeSpace({ realPath: '/data/users/john/files/folder' })
+ ;(filesUtils.isPathExists as jest.Mock).mockResolvedValueOnce(false).mockResolvedValueOnce(true)
+
+ await service.mkDir(user, space, false, { depth: DEPTH.INFINITY, lockTokens: ['lt1'] })
+
+ expect(filesLockManager.checkConflicts).toHaveBeenCalledWith(space.dbFile, DEPTH.INFINITY, { userId: 7, lockTokens: ['lt1'] })
+ expect(filesUtils.makeDir).toHaveBeenCalledWith('/data/users/john/files/folder', false)
+ })
+
+ it('copyMove should copy file and emit add event', async () => {
+ const src = makeSpace({
+ id: 10,
+ url: 'files/personal/src.txt',
+ realPath: '/data/users/john/files/src.txt',
+ realBasePath: '/data/users/john/files',
+ dbFile: { ownerId: 7, path: 'src.txt', inTrash: false }
+ })
+ const dst = makeSpace({
+ id: 11,
+ url: 'files/personal/dst.txt',
+ realPath: '/data/users/john/files/dst.txt',
+ realBasePath: '/data/users/john/files',
+ dbFile: { ownerId: 7, path: 'dst.txt', inTrash: false },
+ storageQuota: null
+ })
+ setPathExists(
+ {
+ [src.realPath]: true,
+ [path.dirname(dst.realPath)]: true,
+ [dst.realPath]: false
+ },
+ false
+ )
+ ;(filesUtils.isPathIsDir as jest.Mock).mockResolvedValueOnce(false)
+ const emitSpy = jest.spyOn(FileEvent, 'emit')
+
+ await service.copyMove(user, src, dst, false)
+
+ expect(filesLockManager.checkConflicts).toHaveBeenCalledWith(dst.dbFile, DEPTH.RESOURCE, { userId: 7, lockTokens: undefined })
+ expect(filesUtils.copyFiles).toHaveBeenCalledWith(src.realPath, dst.realPath, false, false)
+ expect(emitSpy).toHaveBeenCalledWith('event', { user, space: dst, action: ACTION.ADD, rPath: dst.realPath })
+ })
+
+ it('copyMove should move across spaces and update db', async () => {
+ const src = makeSpace({
+ id: 21,
+ url: 'files/personal/src.txt',
+ realPath: '/src-base/src.txt',
+ realBasePath: '/src-base',
+ dbFile: { ownerId: 7, path: 'src.txt', inTrash: false }
+ })
+ const dst = makeSpace({
+ id: 22,
+ url: 'files/project/dst.txt',
+ realPath: '/dst-base/dst.txt',
+ realBasePath: '/dst-base',
+ dbFile: { ownerId: null, spaceId: 22, path: 'dst.txt', inTrash: false },
+ storageQuota: null
+ })
+ setPathExists(
+ {
+ [src.realPath]: true,
+ [path.dirname(dst.realPath)]: true,
+ [dst.realPath]: false
+ },
+ false
+ )
+ ;(filesUtils.isPathIsDir as jest.Mock).mockResolvedValueOnce(false)
+ const emitSpy = jest.spyOn(FileEvent, 'emit')
+
+ await service.copyMove(user, src, dst, true)
+
+ expect(filesUtils.moveFiles).toHaveBeenCalledWith('/src-base/src.txt', '/dst-base/dst.txt', false)
+ expect(filesQueries.moveFiles).toHaveBeenCalledWith(src.dbFile, dst.dbFile, false)
+ expect(emitSpy).toHaveBeenCalledWith('event', { user, space: src, action: ACTION.DELETE_PERMANENTLY, rPath: '/src-base/src.txt' })
+ expect(emitSpy).toHaveBeenCalledWith('event', { user, space: dst, action: ACTION.ADD, rPath: '/dst-base/dst.txt' })
+ })
+
+ it('delete should remove trash file, locks and db entries', async () => {
+ const space = makeSpace({ inTrashRepository: true, realPath: '/data/users/john/trash/old.txt' })
+ ;(filesUtils.isPathExists as jest.Mock).mockResolvedValueOnce(true)
+ ;(filesUtils.isPathIsDir as jest.Mock).mockResolvedValueOnce(true)
+ filesLockManager.getLocksByPath.mockResolvedValueOnce([{ key: 'lk-1' }])
+ const emitSpy = jest.spyOn(FileEvent, 'emit')
+
+ await service.delete(user, space)
+
+ expect(filesUtils.removeFiles).toHaveBeenCalledWith('/data/users/john/trash/old.txt')
+ expect(filesLockManager.removeChildLocks).toHaveBeenCalledWith(user, space.dbFile)
+ expect(filesLockManager.removeLock).toHaveBeenCalledWith('lk-1')
+ expect(filesQueries.deleteFiles).toHaveBeenCalledWith(space.dbFile, true, false)
+ expect(emitSpy).toHaveBeenCalledWith('event', { user, space, action: ACTION.DELETE_PERMANENTLY, rPath: '/data/users/john/trash/old.txt' })
+ })
+
+ it('delete should force delete when trash path is not available', async () => {
+ const space = makeSpace({ realPath: '/data/users/john/files/no-trash.txt', inTrashRepository: false })
+ ;(filesUtils.isPathExists as jest.Mock).mockResolvedValueOnce(true)
+ ;(filesUtils.isPathIsDir as jest.Mock).mockResolvedValueOnce(false)
+ ;(spacesPathUtils.realTrashPathFromSpace as jest.Mock).mockReturnValueOnce(null)
+
+ await service.delete(user, space)
+
+ expect(filesUtils.removeFiles).toHaveBeenCalledWith('/data/users/john/files/no-trash.txt')
+ expect(filesQueries.deleteFiles).toHaveBeenCalledWith(space.dbFile, false, true)
+ })
+
+ it('downloadFromUrl should throw conflict when lock cannot be created', async () => {
+ const space = makeSpace()
+ filesLockManager.create.mockResolvedValueOnce([false, { key: 'other', owner: { id: 99 } }])
+
+ await expect(service.downloadFromUrl(user, space, 'https://example.org/file.txt')).rejects.toBeInstanceOf(LockConflict)
+ })
+
+ it('downloadFromUrl should handle HEAD+GET and emit task watch/event', async () => {
+ const space = makeSpace({ task: { cacheKey: 'task-1', props: {} } })
+ ;(filesUtils.uniqueFilePathFromDir as jest.Mock).mockResolvedValueOnce('/tmp/download.txt')
+ http.axiosRef
+ .mockResolvedValueOnce({
+ headers: { 'content-length': '55' },
+ request: { socket: { remoteAddress: '8.8.8.8' } }
+ })
+ .mockResolvedValueOnce({
+ data: Readable.from(['abc']),
+ request: { socket: { remoteAddress: '8.8.8.8' } }
+ })
+ const taskEmitSpy = jest.spyOn(FileTaskEvent, 'emit')
+ const fileEmitSpy = jest.spyOn(FileEvent, 'emit')
+
+ await service.downloadFromUrl(user, space, 'https://example.org/file.txt')
+
+ expect(space.task.props.totalSize).toBe(55)
+ expect(taskEmitSpy).toHaveBeenCalledWith('startWatch', space, FILE_OPERATION.DOWNLOAD, '/tmp/download.txt')
+ expect(filesUtils.writeFromStream).toHaveBeenCalledWith('/tmp/download.txt', expect.anything())
+ expect(filesLockManager.removeLock).toHaveBeenCalledWith('lock-1')
+ expect(fileEmitSpy).toHaveBeenCalledWith('event', { user, space, action: ACTION.ADD, rPath: '/tmp/download.txt' })
+ })
+
+ it('compress should archive files and emit events', async () => {
+ const archive = {
+ on: jest.fn().mockReturnThis(),
+ pipe: jest.fn().mockReturnThis(),
+ directory: jest.fn().mockReturnThis(),
+ file: jest.fn().mockReturnThis(),
+ finalize: jest.fn().mockResolvedValue(undefined)
+ }
+ ;(archiver as unknown as jest.Mock).mockReturnValueOnce(archive)
+ jest.spyOn(fs, 'createWriteStream').mockReturnValue({} as any)
+ ;(filesUtils.uniqueFilePathFromDir as jest.Mock).mockResolvedValueOnce('/tmp/archive.tar.gz')
+ ;(filesUtils.isPathIsDir as jest.Mock).mockImplementation(async (p: string) => p.endsWith('/dir'))
+ const space = makeSpace({ realPath: '/data/users/john/files/source.txt', task: { cacheKey: 'task-c', props: {} } })
+ const dto = {
+ name: 'archive',
+ extension: 'tar.gz',
+ compressInDirectory: false,
+ files: [
+ { path: '/data/users/john/files/dir', name: 'dir', rootAlias: null },
+ { path: '/data/users/john/files/file.txt', name: 'file.txt', rootAlias: null }
+ ]
+ } as any
+ const taskEmitSpy = jest.spyOn(FileTaskEvent, 'emit')
+
+ await service.compress(user, space, dto)
+
+ expect(archiver as unknown as jest.Mock).toHaveBeenCalled()
+ expect(archive.directory).toHaveBeenCalled()
+ expect(archive.file).toHaveBeenCalled()
+ expect(archive.finalize).toHaveBeenCalled()
+ expect(taskEmitSpy).toHaveBeenCalledWith('startWatch', space, FILE_OPERATION.COMPRESS, '/tmp/archive.tar.gz')
+ })
+
+ it('decompress should extract zip and release lock', async () => {
+ const space = makeSpace({ realPath: '/data/users/john/files/archive.zip', task: { cacheKey: 'task-d', props: {} } })
+ ;(filesUtils.isPathExists as jest.Mock).mockResolvedValueOnce(true)
+ ;(filesUtils.uniqueFilePathFromDir as jest.Mock).mockResolvedValueOnce('/data/users/john/files/archive')
+ const unzipSpy = jest.spyOn(unzipUtils, 'extractZip').mockResolvedValueOnce(undefined)
+ const taskEmitSpy = jest.spyOn(FileTaskEvent, 'emit')
+
+ await service.decompress(user, space)
+
+ expect(filesUtils.makeDir).toHaveBeenCalledWith('/data/users/john/files/archive')
+ expect(unzipSpy).toHaveBeenCalledWith('/data/users/john/files/archive.zip', '/data/users/john/files/archive')
+ expect(taskEmitSpy).toHaveBeenCalledWith('startWatch', space, FILE_OPERATION.DECOMPRESS, '/data/users/john/files/archive')
+ expect(filesLockManager.removeLock).toHaveBeenCalledWith('lock-1')
+ })
+
+ it('decompress should extract tar formats via tar.extract', async () => {
+ const space = makeSpace({ realPath: '/data/users/john/files/archive.tar.gz' })
+ ;(filesUtils.isPathExists as jest.Mock).mockResolvedValueOnce(true)
+ ;(filesUtils.uniqueFilePathFromDir as jest.Mock).mockResolvedValueOnce('/data/users/john/files/archive')
+ ;(tar.extract as unknown as jest.Mock).mockResolvedValueOnce(undefined)
+
+ await service.decompress(user, space)
+
+ expect(tar.extract).toHaveBeenCalledWith(
+ expect.objectContaining({
+ file: '/data/users/john/files/archive.tar.gz',
+ cwd: '/data/users/john/files/archive',
+ gzip: true
+ })
+ )
+ })
+
+ it('generateThumbnail should validate image and return generated stream', async () => {
+ const space = makeSpace({ realPath: '/data/users/john/files/image.png' })
+ ;(filesUtils.isPathExists as jest.Mock).mockResolvedValueOnce(true)
+ ;(filesUtils.getMimeType as jest.Mock).mockReturnValueOnce('image-png')
+ const stream = Readable.from(['img'])
+ jest.spyOn(imageUtils, 'generateThumbnail').mockReturnValueOnce(stream as any)
+
+ const result = await service.generateThumbnail(space, 256)
+
+ expect(result).toBe(stream)
+ })
+
+ it('lock should fail if resource does not exist', async () => {
+ const space = makeSpace()
+ ;(filesUtils.isPathExists as jest.Mock).mockResolvedValueOnce(false)
+
+ await expect(service.lock(user, space)).rejects.toEqual(new FileError(HttpStatus.BAD_REQUEST, 'Lock refresh must specify an existing resource'))
+ })
+
+ it('unlock should remove owned lock and reject foreign lock', async () => {
+ const space = makeSpace()
+ ;(filesUtils.isPathExists as jest.Mock).mockResolvedValue(true)
+ filesLockManager.getLocksByPath.mockResolvedValueOnce([{ key: 'l1', owner: { id: 7 } }])
+
+ await service.unlock(user, space)
+ expect(filesLockManager.removeLock).toHaveBeenCalledWith('l1')
+
+ filesLockManager.getLocksByPath.mockResolvedValueOnce([{ key: 'l2', owner: { id: 99, login: 'alice' } }])
+ await expect(service.unlock(user, space)).rejects.toEqual(
+ new LockConflict({ key: 'l2', owner: { id: 99, login: 'alice' } } as any, 'Conflicting lock')
+ )
+ })
+
+ it('unlockRequest should throw when lock list is empty and notify foreign owner otherwise', async () => {
+ const space = makeSpace()
+ filesLockManager.getLocksByPath.mockResolvedValueOnce([])
+ await expect(service.unlockRequest(user, space)).rejects.toEqual(new FileError(HttpStatus.NOT_FOUND, 'Lock not found'))
+
+ filesLockManager.getLocksByPath.mockResolvedValueOnce([{ key: 'l3', owner: { id: 42 } }])
+ await service.unlockRequest(user, space)
+ expect(notificationsManager.create).toHaveBeenCalledWith(
+ [42],
+ expect.objectContaining({ element: 'file.txt', url: 'files/personal' }),
+ expect.objectContaining({ author: user, currentUrl: 'https://sync-in.example' })
+ )
+ })
+
+ it('getSize should return directory size or file size depending on target type', async () => {
+ const space = makeSpace()
+ ;(filesUtils.isPathExists as jest.Mock).mockResolvedValue(true)
+ ;(filesUtils.isPathIsDir as jest.Mock).mockResolvedValueOnce(true).mockResolvedValueOnce(false)
+ ;(filesUtils.dirSize as jest.Mock).mockResolvedValueOnce([500, {}])
+ ;(filesUtils.fileSize as jest.Mock).mockResolvedValueOnce(20)
+
+ await expect(service.getSize(space)).resolves.toBe(500)
+ await expect(service.getSize(space)).resolves.toBe(20)
+ })
})
diff --git a/backend/src/applications/files/services/files-quota-manager.service.spec.ts b/backend/src/applications/files/services/files-quota-manager.service.spec.ts
index 9850130f..f0e37789 100644
--- a/backend/src/applications/files/services/files-quota-manager.service.spec.ts
+++ b/backend/src/applications/files/services/files-quota-manager.service.spec.ts
@@ -3,24 +3,201 @@ import { FilesQuotaManager } from './files-quota-manager.service'
import { SpacesQueries } from '../../spaces/services/spaces-queries.service'
import { UsersQueries } from '../../users/services/users-queries.service'
import { SharesQueries } from '../../shares/services/shares-queries.service'
+import { CACHE_QUOTA_TTL } from '../constants/cache'
+import { FILE_REPOSITORY } from '../constants/operations'
+import { genQuotaCacheKey } from '../utils/quota'
+import { SpaceModel } from '../../spaces/models/space.model'
+import * as filesUtils from '../utils/files'
describe(FilesQuotaManager.name, () => {
let service: FilesQuotaManager
+ let spacesQueries: {
+ cache: {
+ get: jest.Mock
+ set: jest.Mock
+ del: jest.Mock
+ keys: jest.Mock
+ }
+ spacesQuotaPaths: jest.Mock
+ updateSpace: jest.Mock
+ }
+ let usersQueries: {
+ selectUsers: jest.Mock
+ updateUserOrGuest: jest.Mock
+ }
+ let sharesQueries: {
+ cache: {
+ set: jest.Mock
+ }
+ sharesQuotaExternalPaths: jest.Mock
+ updateShare: jest.Mock
+ }
+ let isPathExistsSpy: jest.SpiedFunction<typeof filesUtils.isPathExists>
+ let dirSizeSpy: jest.SpiedFunction<typeof filesUtils.dirSize>
- beforeAll(async () => {
+ beforeEach(async () => {
+ spacesQueries = {
+ cache: {
+ get: jest.fn().mockResolvedValue(null),
+ set: jest.fn().mockResolvedValue(true),
+ del: jest.fn().mockResolvedValue(true),
+ keys: jest.fn().mockResolvedValue([])
+ },
+ spacesQuotaPaths: jest.fn().mockResolvedValue([]),
+ updateSpace: jest.fn().mockResolvedValue(true)
+ }
+ usersQueries = {
+ selectUsers: jest.fn().mockResolvedValue([]),
+ updateUserOrGuest: jest.fn().mockResolvedValue(true)
+ }
+ sharesQueries = {
+ cache: {
+ set: jest.fn().mockResolvedValue(true)
+ },
+ sharesQuotaExternalPaths: jest.fn().mockResolvedValue([]),
+ updateShare: jest.fn().mockResolvedValue(true)
+ }
const module: TestingModule = await Test.createTestingModule({
providers: [
FilesQuotaManager,
- { provide: SpacesQueries, useValue: {} },
- { provide: UsersQueries, useValue: {} },
- { provide: SharesQueries, useValue: {} }
+ { provide: SpacesQueries, useValue: spacesQueries },
+ { provide: UsersQueries, useValue: usersQueries },
+ { provide: SharesQueries, useValue: sharesQueries }
]
}).compile()
+ module.useLogger(['fatal'])
service = module.get<FilesQuotaManager>(FilesQuotaManager)
+ isPathExistsSpy = jest.spyOn(filesUtils, 'isPathExists').mockResolvedValue(true)
+ dirSizeSpy = jest.spyOn(filesUtils, 'dirSize').mockResolvedValue([0, {}])
+ })
+
+ afterEach(() => {
+ jest.restoreAllMocks()
+ jest.clearAllMocks()
})
it('should be defined', () => {
expect(service).toBeDefined()
})
+
+ it('should skip quota update when the space is a shares list', async () => {
+ const user = { id: 3 } as any
+ const space = { id: 10, alias: 'shares', inSharesList: true } as any
+
+ await service.setQuotaExceeded(user, space)
+
+ expect(spacesQueries.cache.get).not.toHaveBeenCalled()
+ })
+
+ it('should set quota from cache when available', async () => {
+ const user = { id: 3 } as any
+ const space = { id: 10, alias: 'project', inSharesList: false, inPersonalSpace: false, inSharesRepository: false } as any
+ const cachedQuota = { storageUsage: 12, storageQuota: 10 }
+ spacesQueries.cache.get.mockResolvedValueOnce(cachedQuota)
+
+ await service.setQuotaExceeded(user, space)
+
+ expect(spacesQueries.cache.get).toHaveBeenCalledWith(genQuotaCacheKey(10, FILE_REPOSITORY.SPACE))
+ expect(space.storageUsage).toBe(12)
+ expect(space.storageQuota).toBe(10)
+ expect(space.quotaIsExceeded).toBe(true)
+ })
+
+ it('should compute personal quota when cache is missing', async () => {
+ const user = { id: 7 } as any
+ const space = { id: 0, alias: 'personal', inSharesList: false, inPersonalSpace: true, inSharesRepository: false } as any
+ const personalQuota = { storageUsage: 20, storageQuota: 100 }
+ const spy = jest.spyOn(service, 'updatePersonalSpacesQuota').mockResolvedValueOnce(personalQuota)
+
+ await service.setQuotaExceeded(user, space)
+
+ expect(spy).toHaveBeenCalledWith(7)
+ expect(space.storageUsage).toBe(20)
+ expect(space.storageQuota).toBe(100)
+ expect(space.quotaIsExceeded).toBe(false)
+ })
+
+ it('should dispatch storage usage updates by repository type and clean cache keys', async () => {
+ spacesQueries.cache.keys.mockResolvedValueOnce([
+ 'event-update-quota-user-1',
+ 'event-update-quota-space-2',
+ 'event-update-quota-share-3',
+ 'event-update-quota-unknown-4'
+ ])
+ const userSpy = jest.spyOn(service, 'updatePersonalSpacesQuota').mockResolvedValueOnce(undefined)
+ const spaceSpy = jest.spyOn(service, 'updateSpacesQuota').mockResolvedValueOnce(undefined)
+ const shareSpy = jest.spyOn(service, 'updateSharesExternalPathQuota').mockResolvedValueOnce(undefined)
+
+ await service.updateStorageUsageEntries()
+
+ expect(userSpy).toHaveBeenCalledWith(1)
+ expect(spaceSpy).toHaveBeenCalledWith(2)
+ expect(shareSpy).toHaveBeenCalledWith(3)
+ expect(spacesQueries.cache.del).toHaveBeenCalledTimes(4)
+ expect(spacesQueries.cache.del).toHaveBeenCalledWith('event-update-quota-user-1')
+ expect(spacesQueries.cache.del).toHaveBeenCalledWith('event-update-quota-space-2')
+ expect(spacesQueries.cache.del).toHaveBeenCalledWith('event-update-quota-share-3')
+ expect(spacesQueries.cache.del).toHaveBeenCalledWith('event-update-quota-unknown-4')
+ })
+
+ it('should update cached storage quota when current usage is already cached', async () => {
+ spacesQueries.cache.get.mockResolvedValueOnce({ storageUsage: 55, storageQuota: 80 })
+
+ await service.updateStorageQuota(9, FILE_REPOSITORY.SPACE, 100)
+
+ expect(spacesQueries.cache.set).toHaveBeenCalledWith(
+ genQuotaCacheKey(9, FILE_REPOSITORY.SPACE),
+ { storageUsage: 55, storageQuota: 100 },
+ CACHE_QUOTA_TTL
+ )
+ })
+
+ it('should enqueue a quota update event when cached usage is missing', async () => {
+ spacesQueries.cache.get.mockResolvedValueOnce(null)
+
+ await service.updateStorageQuota(9, FILE_REPOSITORY.SPACE, 100)
+
+ expect(spacesQueries.cache.set).toHaveBeenCalledWith(genQuotaCacheKey(9, FILE_REPOSITORY.SPACE, true), true, CACHE_QUOTA_TTL)
+ })
+
+ it('should compute personal quota from filesystem and return it for a specific user', async () => {
+ usersQueries.selectUsers.mockResolvedValueOnce([{ id: 5, login: 'john', storageUsage: 2, storageQuota: 50 }])
+ dirSizeSpy.mockResolvedValueOnce([42, {}])
+
+ const quota = await service.updatePersonalSpacesQuota(5)
+
+ expect(isPathExistsSpy).toHaveBeenCalled()
+ expect(quota).toEqual({ storageUsage: 42, storageQuota: 50 })
+ expect(spacesQueries.cache.set).toHaveBeenCalledWith(genQuotaCacheKey(5, FILE_REPOSITORY.USER), quota, CACHE_QUOTA_TTL)
+ expect(usersQueries.updateUserOrGuest).toHaveBeenCalledWith(5, { storageUsage: 42 })
+ })
+
+ it('should compute space quota from home path and external paths', async () => {
+ spacesQueries.spacesQuotaPaths.mockResolvedValueOnce([
+ { id: 8, alias: 'engineering', storageUsage: 1, storageQuota: 500, externalPaths: ['/mnt/ext-space', null] }
+ ])
+ dirSizeSpy.mockResolvedValueOnce([30, {}]).mockResolvedValueOnce([5, {}])
+
+ const quota = await service.updateSpacesQuota(8)
+
+ expect(isPathExistsSpy).toHaveBeenCalledWith(SpaceModel.getHomePath('engineering'))
+ expect(quota).toEqual({ storageUsage: 35, storageQuota: 500 })
+ expect(spacesQueries.cache.set).toHaveBeenCalledWith(genQuotaCacheKey(8, FILE_REPOSITORY.SPACE), quota, CACHE_QUOTA_TTL)
+ expect(spacesQueries.updateSpace).toHaveBeenCalledWith(8, { storageUsage: 35 })
+ })
+
+ it('should compute share external path quota and persist usage change', async () => {
+ sharesQueries.sharesQuotaExternalPaths.mockResolvedValueOnce([
+ { id: 12, alias: 'share-1', externalPath: '/mnt/share', storageUsage: 2, storageQuota: 10 }
+ ])
+ dirSizeSpy.mockResolvedValueOnce([7, {}])
+
+ const quota = await service.updateSharesExternalPathQuota(12)
+
+ expect(isPathExistsSpy).toHaveBeenCalledWith('/mnt/share')
+ expect(quota).toEqual({ storageUsage: 7, storageQuota: 10 })
+ expect(sharesQueries.cache.set).toHaveBeenCalledWith(genQuotaCacheKey(12, FILE_REPOSITORY.SHARE), quota, CACHE_QUOTA_TTL)
+ expect(sharesQueries.updateShare).toHaveBeenCalledWith(12, { storageUsage: 7 })
+ })
})
diff --git a/backend/src/applications/files/services/files-recents.service.spec.ts b/backend/src/applications/files/services/files-recents.service.spec.ts
index 0174d559..81abbe30 100644
--- a/backend/src/applications/files/services/files-recents.service.spec.ts
+++ b/backend/src/applications/files/services/files-recents.service.spec.ts
@@ -1,4 +1,6 @@
import { Test, TestingModule } from '@nestjs/testing'
+import { convertHumanTimeToMs } from '../../../common/functions'
+import { currentTimeStamp } from '../../../common/shared'
import { SharesQueries } from '../../shares/services/shares-queries.service'
import { SpacesQueries } from '../../spaces/services/spaces-queries.service'
import { FilesQueries } from './files-queries.service'
@@ -6,24 +8,158 @@ import { FilesRecents } from './files-recents.service'
describe(FilesRecents.name, () => {
let service: FilesRecents
+ let filesQueries: {
+ getRecentsFromUser: jest.Mock
+ getRecentsFromLocation: jest.Mock
+ updateRecents: jest.Mock
+ }
+ let spacesQueries: {
+ spaceIds: jest.Mock
+ }
+ let sharesQueries: {
+ shareIds: jest.Mock
+ }
- beforeAll(async () => {
+ beforeEach(async () => {
+ filesQueries = {
+ getRecentsFromUser: jest.fn().mockResolvedValue([]),
+ getRecentsFromLocation: jest.fn().mockResolvedValue([]),
+ updateRecents: jest.fn().mockResolvedValue(undefined)
+ }
+ spacesQueries = {
+ spaceIds: jest.fn().mockResolvedValue([])
+ }
+ sharesQueries = {
+ shareIds: jest.fn().mockResolvedValue([])
+ }
const module: TestingModule = await Test.createTestingModule({
providers: [
FilesRecents,
- { provide: FilesQueries, useValue: {} },
- { provide: SpacesQueries, useValue: {} },
+ { provide: FilesQueries, useValue: filesQueries },
+ { provide: SpacesQueries, useValue: spacesQueries },
{
provide: SharesQueries,
- useValue: {}
+ useValue: sharesQueries
}
]
}).compile()
+ module.useLogger(['fatal'])
service = module.get<FilesRecents>(FilesRecents)
})
+ afterEach(() => {
+ jest.clearAllMocks()
+ })
+
it('should be defined', () => {
expect(service).toBeDefined()
})
+
+ it('should load recents from user accessible spaces and shares', async () => {
+ const recents = [{ id: 1, name: 'a.txt' }]
+ spacesQueries.spaceIds.mockResolvedValueOnce([10, 11])
+ sharesQueries.shareIds.mockResolvedValueOnce([20])
+ filesQueries.getRecentsFromUser.mockResolvedValueOnce(recents)
+
+ const result = await service.getRecents({ id: 7, isAdmin: true } as any, 25)
+
+ expect(spacesQueries.spaceIds).toHaveBeenCalledWith(7)
+ expect(sharesQueries.shareIds).toHaveBeenCalledWith(7, 1)
+ expect(filesQueries.getRecentsFromUser).toHaveBeenCalledWith(7, [10, 11], [20], 25)
+ expect(result).toBe(recents)
+ })
+
+ it('should ignore updateRecents when browsing trash repository', async () => {
+ await service.updateRecents({ id: 5 } as any, { inTrashRepository: true, url: 'trash/personal' } as any, [])
+
+ expect(filesQueries.getRecentsFromLocation).not.toHaveBeenCalled()
+ expect(filesQueries.updateRecents).not.toHaveBeenCalled()
+ })
+
+ it('should skip persistence when filtered fs recents and db recents are both empty', async () => {
+ const oldMtime = currentTimeStamp(null, true) - convertHumanTimeToMs('30d')
+ const files = [
+ { id: 1, name: 'dir', isDir: true, size: 1, mtime: currentTimeStamp(null, true), mime: 'directory' },
+ { id: 2, name: 'zero.txt', isDir: false, size: 0, mtime: currentTimeStamp(null, true), mime: 'text-plain' },
+ { id: 3, name: 'old.txt', isDir: false, size: 10, mtime: oldMtime, mime: 'text-plain' }
+ ] as any
+
+ await service.updateRecents({ id: 5 } as any, { inTrashRepository: false, inPersonalSpace: true, url: 'files/personal/docs' } as any, files)
+
+ expect(filesQueries.getRecentsFromLocation).toHaveBeenCalledWith({ ownerId: 5, path: 'files/personal/docs' })
+ expect(filesQueries.updateRecents).not.toHaveBeenCalled()
+ })
+
+ it('should compute add update remove diff and persist recents for personal space', async () => {
+ const now = currentTimeStamp(null, true)
+ const location = { ownerId: 5, path: 'files/personal/docs' }
+ const dbRecents = [
+ { id: 1, name: 'old-name.txt', mtime: now - 1000, mime: 'text-plain' },
+ { id: 2, name: 'remove-me.txt', mtime: now - 1000, mime: 'text-plain' }
+ ]
+ const fsFiles = [
+ { id: 1, name: 'new-name.txt', isDir: false, size: 100, mtime: now, mime: 'text-plain' },
+ { id: 3, name: 'add-me.txt', isDir: false, size: 200, mtime: now, mime: 'text-plain' }
+ ]
+ filesQueries.getRecentsFromLocation.mockResolvedValueOnce(dbRecents)
+
+ await service.updateRecents({ id: 5 } as any, { inTrashRepository: false, inPersonalSpace: true, url: location.path } as any, fsFiles as any)
+
+ expect(filesQueries.getRecentsFromLocation).toHaveBeenCalledWith(location)
+ expect(filesQueries.updateRecents).toHaveBeenCalledTimes(1)
+ const [loc, toAdd, toUpdate, toRemove] = filesQueries.updateRecents.mock.calls[0]
+ expect(loc).toEqual(location)
+ expect(toAdd).toEqual([{ id: 3, name: 'add-me.txt', mtime: now, mime: 'text-plain', ...location }])
+ expect(toUpdate).toEqual([{ name: 'new-name.txt', mtime: now, object: fsFiles[0] }])
+ expect(toRemove).toEqual([2])
+ })
+
+ it('should use share location list and per-file share id when browsing shares list', async () => {
+ const now = currentTimeStamp(null, true)
+ const files = [
+ { id: 10, name: 'a.txt', isDir: false, size: 5, mtime: now, mime: 'text-plain', root: { id: 101 } },
+ { id: 11, name: 'b.txt', isDir: false, size: 5, mtime: now, mime: 'text-plain', root: { id: 102 } }
+ ]
+ filesQueries.getRecentsFromLocation.mockResolvedValueOnce([])
+
+ await service.updateRecents(
+ { id: 5 } as any,
+ { inTrashRepository: false, inSharesList: true, inSharesRepository: false, inPersonalSpace: false, id: 0, url: 'shares' } as any,
+ files as any
+ )
+
+ const [location, toAdd] = filesQueries.updateRecents.mock.calls[0]
+ expect(location).toEqual({ shareId: [101, 102], path: 'shares' })
+ expect(toAdd).toEqual([
+ { id: 10, name: 'a.txt', mtime: now, mime: 'text-plain', path: 'shares', shareId: 101 },
+ { id: 11, name: 'b.txt', mtime: now, mime: 'text-plain', path: 'shares', shareId: 102 }
+ ])
+ })
+
+ it('should use repository share id for a single share location', async () => {
+ const now = currentTimeStamp(null, true)
+ filesQueries.getRecentsFromLocation.mockResolvedValueOnce([])
+
+ await service.updateRecents(
+ { id: 5 } as any,
+ { inTrashRepository: false, inSharesList: false, inSharesRepository: true, inPersonalSpace: false, id: 77, url: 'shares/project' } as any,
+ [{ id: 1, name: 'doc.txt', isDir: false, size: 1, mtime: now, mime: 'text-plain' }] as any
+ )
+
+ expect(filesQueries.getRecentsFromLocation).toHaveBeenCalledWith({ shareId: 77, path: 'shares/project' })
+ })
+
+ it('should use space id location for regular space repository', async () => {
+ const now = currentTimeStamp(null, true)
+ filesQueries.getRecentsFromLocation.mockResolvedValueOnce([])
+
+ await service.updateRecents(
+ { id: 5 } as any,
+ { inTrashRepository: false, inSharesList: false, inSharesRepository: false, inPersonalSpace: false, id: 44, url: 'files/engineering' } as any,
+ [{ id: 1, name: 'doc.txt', isDir: false, size: 1, mtime: now, mime: 'text-plain' }] as any
+ )
+
+ expect(filesQueries.getRecentsFromLocation).toHaveBeenCalledWith({ spaceId: 44, path: 'files/engineering' })
+ })
})
diff --git a/backend/src/applications/files/services/files-search-manager.service.spec.ts b/backend/src/applications/files/services/files-search-manager.service.spec.ts
index cf9f416a..9b023b3a 100644
--- a/backend/src/applications/files/services/files-search-manager.service.spec.ts
+++ b/backend/src/applications/files/services/files-search-manager.service.spec.ts
@@ -1,31 +1,204 @@
import { Test, TestingModule } from '@nestjs/testing'
+import { HttpException, HttpStatus } from '@nestjs/common'
+import fs from 'fs/promises'
import { SharesQueries } from '../../shares/services/shares-queries.service'
import { SpacesQueries } from '../../spaces/services/spaces-queries.service'
import { FilesContentStore } from '../models/files-content-store'
+import { FILE_REPOSITORY } from '../constants/operations'
+import { configuration } from '../../../configuration/config.environment'
import { FilesParser } from './files-parser.service'
import { FilesSearchManager } from './files-search-manager.service'
describe(FilesSearchManager.name, () => {
let service: FilesSearchManager
+ let filesIndexer: {
+ existingIndexes: jest.Mock
+ searchRecords: jest.Mock
+ }
+ let filesParser: {
+ allPaths: jest.Mock
+ }
+ let spacesQueries: {
+ spaceIds: jest.Mock
+ }
+ let sharesQueries: {
+ shareIds: jest.Mock
+ }
+ let contentIndexingEnabled: boolean
- beforeAll(async () => {
+ const fileContent = (name: string) => ({
+ id: name.length,
+ path: `files/personal/${name}`,
+ name,
+ mime: 'text-plain',
+ size: 1,
+ mtime: 1
+ })
+
+ beforeEach(async () => {
+ filesIndexer = {
+ existingIndexes: jest.fn().mockResolvedValue([]),
+ searchRecords: jest.fn().mockResolvedValue([])
+ }
+ filesParser = {
+ allPaths: jest.fn()
+ }
+ spacesQueries = {
+ spaceIds: jest.fn().mockResolvedValue([])
+ }
+ sharesQueries = {
+ shareIds: jest.fn().mockResolvedValue([])
+ }
const module: TestingModule = await Test.createTestingModule({
providers: [
FilesSearchManager,
- { provide: FilesContentStore, useValue: {} },
- { provide: FilesParser, useValue: {} },
+ { provide: FilesContentStore, useValue: filesIndexer },
+ { provide: FilesParser, useValue: filesParser },
{
provide: SpacesQueries,
- useValue: {}
+ useValue: spacesQueries
},
- { provide: SharesQueries, useValue: {} }
+ { provide: SharesQueries, useValue: sharesQueries }
]
}).compile()
+ module.useLogger(['fatal'])
service = module.get<FilesSearchManager>(FilesSearchManager)
+ contentIndexingEnabled = configuration.applications.files.contentIndexing.enabled
+ })
+
+ afterEach(() => {
+ configuration.applications.files.contentIndexing.enabled = contentIndexingEnabled
+ jest.restoreAllMocks()
+ jest.clearAllMocks()
})
it('should be defined', () => {
expect(service).toBeDefined()
})
+
+ it('should reject full-text search when indexing is disabled', async () => {
+ configuration.applications.files.contentIndexing.enabled = false
+
+ await expect(service.search({ id: 9, isAdmin: false } as any, { content: 'report', fullText: true, limit: 10 } as any)).rejects.toEqual(
+ new HttpException('Full-text search is disabled', HttpStatus.BAD_REQUEST)
+ )
+ })
+
+ it('should route to full-text search with space and share ids', async () => {
+ configuration.applications.files.contentIndexing.enabled = true
+ spacesQueries.spaceIds.mockResolvedValueOnce([1, 2])
+ sharesQueries.shareIds.mockResolvedValueOnce([4])
+ const fullTextSpy = jest.spyOn(service as any, 'searchFullText').mockResolvedValueOnce([fileContent('match.md')])
+
+ const result = await service.search({ id: 10, isAdmin: true } as any, { content: 'match', fullText: true, limit: 5 } as any)
+
+ expect(spacesQueries.spaceIds).toHaveBeenCalledWith(10)
+ expect(sharesQueries.shareIds).toHaveBeenCalledWith(10, 1)
+ expect(fullTextSpy).toHaveBeenCalledWith(10, [1, 2], [4], 'match', 5)
+ expect(result).toEqual([fileContent('match.md')])
+ })
+
+ it('should route to filename search when fullText is false', async () => {
+ spacesQueries.spaceIds.mockResolvedValueOnce([6])
+ sharesQueries.shareIds.mockResolvedValueOnce([8, 9])
+ const nameSearchSpy = jest.spyOn(service as any, 'searchFileNames').mockResolvedValueOnce([fileContent('report.pdf')])
+
+ const result = await service.search({ id: 3, isAdmin: false } as any, { content: 'report', fullText: false, limit: 2 } as any)
+
+ expect(nameSearchSpy).toHaveBeenCalledWith(3, [6], [8, 9], 'report', 2)
+ expect(result).toEqual([fileContent('report.pdf')])
+ })
+
+ it('should return empty results when no full-text index exists', async () => {
+ filesIndexer.existingIndexes.mockResolvedValueOnce([])
+
+ const result = await (service as any).searchFullText(5, [1], [7], 'invoice', 10)
+
+ expect(filesIndexer.existingIndexes).toHaveBeenCalledWith([`user_${5}`, `${FILE_REPOSITORY.SPACE}_1`, `${FILE_REPOSITORY.SHARE}_7`])
+ expect(filesIndexer.searchRecords).not.toHaveBeenCalled()
+ expect(result).toEqual([])
+ })
+
+ it('should map invalid regex full-text errors to a bad request syntax error', async () => {
+ filesIndexer.existingIndexes.mockResolvedValueOnce(['user_5'])
+ filesIndexer.searchRecords.mockRejectedValueOnce(new Error('Invalid regular expression: /[/'))
+
+ try {
+ await (service as any).searchFullText(5, [], [], '[', 5)
+ fail('searchFullText should throw')
+ } catch (e) {
+ expect(e).toBeInstanceOf(HttpException)
+ expect((e as HttpException).getStatus()).toBe(HttpStatus.BAD_REQUEST)
+ expect((e as HttpException).message).toBe('SyntaxError (check special characters)')
+ }
+ })
+
+ it('should stop filename search when the limit is reached', async () => {
+ filesParser.allPaths.mockReturnValue(
+ (async function* () {
+ yield [
+ 5,
+ FILE_REPOSITORY.USER,
+ [
+ { realPath: '/root/file-a.txt', pathPrefix: 'files/personal', isDir: false },
+ { realPath: '/root/dir', pathPrefix: 'files/personal', isDir: true }
+ ]
+ ]
+ })()
+ )
+ jest.spyOn(service as any, 'analyzeFile').mockResolvedValue(fileContent('file-a.txt'))
+ jest.spyOn(service as any, 'parseFileNames').mockReturnValue(
+ (async function* () {
+ yield fileContent('from-dir-1.txt')
+ yield fileContent('from-dir-2.txt')
+ })()
+ )
+
+ const result = await (service as any).searchFileNames(5, [], [], 'file', 2)
+
+ expect(result).toHaveLength(2)
+ expect(result[0].name).toBe('file-a.txt')
+ expect(result[1].name).toBe('from-dir-1.txt')
+ })
+
+ it('should ignore parse errors in parseFileNames', async () => {
+ jest.spyOn(fs, 'readdir').mockRejectedValueOnce(new Error('EACCES'))
+ const result: any[] = []
+
+ for await (const item of (service as any).parseFileNames('/forbidden', 'files/personal', /^\/?forbidden\/?/, /a/i)) {
+ result.push(item)
+ }
+
+ expect(result).toEqual([])
+ })
+
+ it('should analyze a matching file and return its metadata', async () => {
+ jest.spyOn(fs, 'stat').mockResolvedValueOnce({
+ ino: 42,
+ size: 512,
+ mtime: new Date('2024-01-02T03:04:05.000Z'),
+ isDirectory: () => false
+ } as any)
+
+ const result = await (service as any).analyzeFile('/base/docs/readme.txt', 'files/personal', /^\/?base\/?/, /readme/i)
+
+ expect(result).toEqual({
+ id: 42,
+ path: 'files/personal/docs',
+ name: 'readme.txt',
+ mime: 'text-plain',
+ size: 512,
+ mtime: new Date('2024-01-02T03:04:05.000Z').getTime()
+ })
+ })
+
+ it('should return null for analyzeFile when terms do not match', async () => {
+ const statSpy = jest.spyOn(fs, 'stat')
+
+ const result = await (service as any).analyzeFile('/base/docs/readme.txt', 'files/personal', /^\/?base\/?/, /invoice/i)
+
+ expect(result).toBeNull()
+ expect(statSpy).not.toHaveBeenCalled()
+ })
})
diff --git a/backend/src/applications/files/services/files-tasks-manager.service.spec.ts b/backend/src/applications/files/services/files-tasks-manager.service.spec.ts
index 947a957c..b30974e9 100644
--- a/backend/src/applications/files/services/files-tasks-manager.service.spec.ts
+++ b/backend/src/applications/files/services/files-tasks-manager.service.spec.ts
@@ -1,31 +1,228 @@
import { Test, TestingModule } from '@nestjs/testing'
+import { HttpException, HttpStatus } from '@nestjs/common'
+import crypto from 'node:crypto'
import { Cache } from '../../../infrastructure/cache/services/cache.service'
-import { SpacesManager } from '../../spaces/services/spaces-manager.service'
-import { FilesManager } from './files-manager.service'
+import * as filesUtils from '../utils/files'
+import { FileTaskEvent } from '../events/file-events'
+import { SendFile } from '../utils/send-file'
+import { FILE_OPERATION } from '../constants/operations'
+import { CACHE_TASK_PREFIX, CACHE_TASK_TTL } from '../constants/cache'
+import { FileTaskStatus } from '../models/file-task'
import { FilesMethods } from './files-methods.service'
import { FilesTasksManager } from './files-tasks-manager.service'
describe(FilesTasksManager.name, () => {
let filesTasksManager: FilesTasksManager
+ let filesMethods: {
+ doWork: jest.Mock
+ }
+ let cacheStore: Map<string, any>
+ let cache: {
+ set: jest.Mock
+ get: jest.Mock
+ keys: jest.Mock
+ mget: jest.Mock
+ del: jest.Mock
+ }
- beforeAll(async () => {
+ const flushPromises = async () => {
+ await new Promise((resolve) => setImmediate(resolve))
+ await new Promise((resolve) => setImmediate(resolve))
+ }
+
+ const createPatternRegex = (pattern: string): RegExp => {
+ const escapedPattern = pattern.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*')
+ return new RegExp(`^${escapedPattern}$`)
+ }
+
+ beforeEach(async () => {
+ cacheStore = new Map<string, any>()
+ cache = {
+ set: jest.fn(async (key: string, value: unknown) => {
+ cacheStore.set(key, value)
+ return true
+ }),
+ get: jest.fn(async (key: string) => cacheStore.get(key)),
+ keys: jest.fn(async (pattern: string) => [...cacheStore.keys()].filter((k) => createPatternRegex(pattern).test(k))),
+ mget: jest.fn(async (keys: string[]) => keys.map((k) => cacheStore.get(k)).filter((v) => v !== undefined)),
+ del: jest.fn(async (key: string) => cacheStore.delete(key))
+ }
+ filesMethods = {
+ doWork: jest.fn()
+ }
const module: TestingModule = await Test.createTestingModule({
providers: [
FilesTasksManager,
- { provide: FilesMethods, useValue: {} },
- { provide: FilesManager, useValue: {} },
- { provide: SpacesManager, useValue: {} },
{
provide: Cache,
- useValue: {}
- }
+ useValue: cache
+ },
+ { provide: FilesMethods, useValue: filesMethods }
]
}).compile()
+ module.useLogger(['fatal'])
filesTasksManager = module.get<FilesTasksManager>(FilesTasksManager)
})
+ afterEach(() => {
+ FileTaskEvent.removeAllListeners('startWatch')
+ jest.restoreAllMocks()
+ jest.clearAllMocks()
+ })
+
it('should be defined', () => {
expect(filesTasksManager).toBeDefined()
})
+
+ it('should build task cache key with and without task id', () => {
+ expect(FilesTasksManager.getCacheKey(10, 'task-1')).toBe(`${CACHE_TASK_PREFIX}-10-task-1`)
+ expect(FilesTasksManager.getCacheKey(10)).toBe(`${CACHE_TASK_PREFIX}-10-*`)
+ })
+
+ it('should create a task and mark it as success when the async method resolves', async () => {
+ jest.spyOn(crypto, 'randomUUID').mockReturnValueOnce('11111111-1111-4111-8111-111111111111')
+ filesMethods.doWork.mockResolvedValueOnce({ props: { totalSize: 99 }, result: 'done' })
+ const user = { id: 7 } as any
+ const space = { url: 'files/personal/document.txt' } as any
+
+ const task = await filesTasksManager.createTask(FILE_OPERATION.COPY, user, space, { foo: 'bar' }, 'doWork')
+ await flushPromises()
+
+ expect(task.id).toBe('11111111-1111-4111-8111-111111111111')
+ expect(task.status).toBe(FileTaskStatus.SUCCESS)
+ expect(space.task.cacheKey).toBe(`${CACHE_TASK_PREFIX}-7-11111111-1111-4111-8111-111111111111`)
+ expect(filesMethods.doWork).toHaveBeenCalledWith(user, space, { foo: 'bar' })
+ expect(cache.set).toHaveBeenCalledWith(
+ `${CACHE_TASK_PREFIX}-7-11111111-1111-4111-8111-111111111111`,
+ expect.objectContaining({ id: '11111111-1111-4111-8111-111111111111', status: FileTaskStatus.SUCCESS, result: 'done' }),
+ CACHE_TASK_TTL
+ )
+ const storedTask = cacheStore.get(`${CACHE_TASK_PREFIX}-7-11111111-1111-4111-8111-111111111111`)
+ expect(storedTask.status).toBe(FileTaskStatus.SUCCESS)
+ expect(storedTask.endedAt).toBeDefined()
+ expect(storedTask.props.totalSize).toBe(99)
+ })
+
+ it('should create a task and mark it as error when the async method fails', async () => {
+ jest.spyOn(crypto, 'randomUUID').mockReturnValueOnce('22222222-2222-4222-8222-222222222222')
+ filesMethods.doWork.mockRejectedValueOnce(new Error('operation failed'))
+ const user = { id: 7 } as any
+ const space = { url: 'files/personal/document.txt' } as any
+
+ await filesTasksManager.createTask(FILE_OPERATION.MOVE, user, space, null, 'doWork')
+ await flushPromises()
+
+ const storedTask = cacheStore.get(`${CACHE_TASK_PREFIX}-7-22222222-2222-4222-8222-222222222222`)
+ expect(storedTask.status).toBe(FileTaskStatus.ERROR)
+ expect(storedTask.result).toBe('operation failed')
+ })
+
+ it('should return one task by id and throw when it does not exist', async () => {
+ cacheStore.set(`${CACHE_TASK_PREFIX}-11-task-a`, { id: 'task-a' })
+
+ await expect(filesTasksManager.getTasks(11, 'task-a')).resolves.toEqual({ id: 'task-a' })
+ await expect(filesTasksManager.getTasks(11, 'missing')).rejects.toEqual(new HttpException('Task not found', HttpStatus.NOT_FOUND))
+ })
+
+ it('should return all tasks for a user when no task id is provided', async () => {
+ cacheStore.set(`${CACHE_TASK_PREFIX}-15-task-1`, { id: 'task-1' })
+ cacheStore.set(`${CACHE_TASK_PREFIX}-15-task-2`, { id: 'task-2' })
+ cacheStore.set(`${CACHE_TASK_PREFIX}-99-task-9`, { id: 'task-9' })
+
+ const tasks = await filesTasksManager.getTasks(15)
+
+ expect(cache.keys).toHaveBeenCalledWith(`${CACHE_TASK_PREFIX}-15-*`)
+ expect(tasks).toEqual([{ id: 'task-1' }, { id: 'task-2' }])
+ })
+
+ it('should delete completed task, remove archive file and delete cache entry', async () => {
+ const removeFilesSpy = jest.spyOn(filesUtils, 'removeFiles').mockResolvedValueOnce(undefined)
+ const stopWatchSpy = jest.spyOn(filesTasksManager as any, 'stopWatch').mockResolvedValueOnce(undefined)
+ const user = { id: 22, tasksPath: '/tmp/tasks' } as any
+ cacheStore.set(`${CACHE_TASK_PREFIX}-22-task-ok`, {
+ id: 'task-ok',
+ name: 'archive.tar',
+ status: FileTaskStatus.SUCCESS,
+ props: { compressInDirectory: false }
+ })
+
+ await filesTasksManager.deleteTasks(user, 'task-ok')
+
+ expect(removeFilesSpy).toHaveBeenCalledWith('/tmp/tasks/archive.tar')
+ expect(stopWatchSpy).toHaveBeenCalledWith(`${CACHE_TASK_PREFIX}-22-task-ok`)
+ expect(cache.del).toHaveBeenCalledWith(`${CACHE_TASK_PREFIX}-22-task-ok`)
+ })
+
+ it('should ignore pending tasks when deleting all tasks', async () => {
+ const removeFilesSpy = jest.spyOn(filesUtils, 'removeFiles').mockResolvedValue(undefined)
+ const stopWatchSpy = jest.spyOn(filesTasksManager as any, 'stopWatch').mockResolvedValue(undefined)
+ const user = { id: 31, tasksPath: '/tmp/tasks' } as any
+ cacheStore.set(`${CACHE_TASK_PREFIX}-31-task-pending`, {
+ id: 'task-pending',
+ status: FileTaskStatus.PENDING,
+ props: { compressInDirectory: false }
+ })
+ cacheStore.set(`${CACHE_TASK_PREFIX}-31-task-done`, {
+ id: 'task-done',
+ name: 'done.tar',
+ status: FileTaskStatus.SUCCESS,
+ props: { compressInDirectory: true }
+ })
+
+ await filesTasksManager.deleteTasks(user)
+
+ expect(cache.keys).toHaveBeenCalledWith(`${CACHE_TASK_PREFIX}-31-*`)
+ expect(removeFilesSpy).not.toHaveBeenCalled()
+ expect(stopWatchSpy).toHaveBeenCalledTimes(1)
+ expect(cache.del).toHaveBeenCalledTimes(1)
+ expect(cache.del).toHaveBeenCalledWith(`${CACHE_TASK_PREFIX}-31-task-done`)
+ })
+
+ it('should reject archive download when task is not applicable', async () => {
+ cacheStore.set(`${CACHE_TASK_PREFIX}-40-task-a`, {
+ id: 'task-a',
+ status: FileTaskStatus.PENDING,
+ props: { compressInDirectory: false }
+ })
+
+ await expect(filesTasksManager.downloadArchive({ id: 40, tasksPath: '/tmp/tasks' } as any, 'task-a', {} as any, {} as any)).rejects.toEqual(
+ new HttpException('Not applicable', HttpStatus.BAD_REQUEST)
+ )
+ })
+
+ it('should map send-file checks error to HttpException during archive download', async () => {
+ cacheStore.set(`${CACHE_TASK_PREFIX}-50-task-ok`, {
+ id: 'task-ok',
+ name: 'archive.tar',
+ status: FileTaskStatus.SUCCESS,
+ props: { compressInDirectory: false }
+ })
+ jest.spyOn(SendFile.prototype, 'checks').mockRejectedValueOnce({ message: 'Location not found', httpCode: 404 } as any)
+
+ try {
+ await filesTasksManager.downloadArchive({ id: 50, tasksPath: '/tmp/tasks' } as any, 'task-ok', {} as any, {} as any)
+ fail('downloadArchive should throw')
+ } catch (e) {
+ expect(e).toBeInstanceOf(HttpException)
+ expect((e as HttpException).getStatus()).toBe(404)
+ expect((e as HttpException).message).toBe('Location not found')
+ }
+ })
+
+ it('should stream archive when task is valid', async () => {
+ const streamable = { ok: true } as any
+ cacheStore.set(`${CACHE_TASK_PREFIX}-60-task-ok`, {
+ id: 'task-ok',
+ name: 'archive.tar',
+ status: FileTaskStatus.SUCCESS,
+ props: { compressInDirectory: false }
+ })
+ jest.spyOn(SendFile.prototype, 'checks').mockResolvedValueOnce(undefined)
+ jest.spyOn(SendFile.prototype, 'stream').mockResolvedValueOnce(streamable)
+
+ const result = await filesTasksManager.downloadArchive({ id: 60, tasksPath: '/tmp/tasks' } as any, 'task-ok', { raw: {} } as any, {} as any)
+
+ expect(result).toBe(streamable)
+ })
})