import { PureCryptoInterface, StreamEncryptor } from '@standardnotes/sncrypto-common' import { FileItem } from '@standardnotes/models' import { EncryptionProvider } from '@standardnotes/encryption' import { ItemManagerInterface } from '../Item/ItemManagerInterface' import { ChallengeServiceInterface } from '../Challenge' import { InternalEventBusInterface } from '..' import { AlertService } from '../Alert/AlertService' import { ApiServiceInterface } from '../Api/ApiServiceInterface' import { SyncServiceInterface } from '../Sync/SyncServiceInterface' import { FileService } from './FileService' describe('fileService', () => { let apiService: ApiServiceInterface let itemManager: ItemManagerInterface let syncService: SyncServiceInterface let alertService: AlertService let crypto: PureCryptoInterface let challengor: ChallengeServiceInterface let fileService: FileService let encryptor: EncryptionProvider let internalEventBus: InternalEventBusInterface beforeEach(() => { apiService = {} as jest.Mocked apiService.addEventObserver = jest.fn() apiService.createFileValetToken = jest.fn() apiService.downloadFile = jest.fn() apiService.deleteFile = jest.fn().mockReturnValue({}) itemManager = {} as jest.Mocked itemManager.createItem = jest.fn() itemManager.createTemplateItem = jest.fn().mockReturnValue({}) itemManager.setItemToBeDeleted = jest.fn() itemManager.addObserver = jest.fn() itemManager.changeItem = jest.fn() challengor = {} as jest.Mocked syncService = {} as jest.Mocked syncService.sync = jest.fn() encryptor = {} as jest.Mocked alertService = {} as jest.Mocked alertService.confirm = jest.fn().mockReturnValue(true) alertService.alert = jest.fn() crypto = {} as jest.Mocked crypto.base64Decode = jest.fn() internalEventBus = {} as jest.Mocked internalEventBus.publish = jest.fn() fileService = new FileService( apiService, itemManager, syncService, encryptor, challengor, alertService, crypto, internalEventBus, ) crypto.xchacha20StreamInitDecryptor = jest.fn().mockReturnValue({ state: {}, } as StreamEncryptor) crypto.xchacha20StreamDecryptorPush = jest.fn().mockReturnValue({ message: new Uint8Array([0xaa]), tag: 0 }) crypto.xchacha20StreamInitEncryptor = jest.fn().mockReturnValue({ header: 'some-header', state: {}, } as StreamEncryptor) crypto.xchacha20StreamEncryptorPush = jest.fn().mockReturnValue(new Uint8Array()) }) it.only('should cache file after download', async () => { const file = { uuid: '1', decryptedSize: 100_000, encryptedSize: 101_000, encryptedChunkSizes: [101_000], } as jest.Mocked let downloadMock = apiService.downloadFile as jest.Mock await fileService.downloadFile(file, async () => { return Promise.resolve() }) expect(downloadMock).toHaveBeenCalledTimes(1) downloadMock = apiService.downloadFile = jest.fn() await fileService.downloadFile(file, async () => { return Promise.resolve() }) expect(downloadMock).toHaveBeenCalledTimes(0) expect(fileService['encryptedCache'].get(file.uuid)).toBeTruthy() }) it('deleting file should remove it from cache', async () => { const file = { uuid: '1', decryptedSize: 100_000, } as jest.Mocked await fileService.downloadFile(file, async () => { return Promise.resolve() }) await fileService.deleteFile(file) expect(fileService['encryptedCache'].get(file.uuid)).toBeFalsy() }) })