1 import type { CryptoApiInterface, PrivateKeyReference } from '@proton/crypto';
2 import { CryptoProxy } from '@proton/crypto';
3 import { LinkType } from '@proton/shared/lib/interfaces/drive/link';
5 import type { KeyCache } from './useKeysCache';
6 import { createKeysCache } from './useKeysCache';
12 LinkID: 'link-mock-id',
19 Type: LinkType.FOLDER,
22 const DECRYPTED_NAME = 'a smell of petroleum prevails throughout';
23 const PRIVATE_KEY = 'private-key';
25 const mockedCryptoApi = {
26 importPrivateKey: jest.fn().mockImplementation(() => PRIVATE_KEY),
27 } as any as CryptoApiInterface;
29 jest.mock('@proton/shared/lib/keys/driveKeys', () => ({
30 decryptUnsigned: jest.fn().mockImplementation(() => DECRYPTED_NAME),
33 jest.mock('@proton/shared/lib/keys/drivePassphrase', () => ({
34 decryptPassphrase: jest.fn().mockImplementation(() => ({
35 decryptedPassphrase: '',
39 describe('useKeysCache', () => {
40 let keyCache: KeyCache;
43 CryptoProxy.setEndpoint(mockedCryptoApi);
46 afterAll(async () => {
47 await CryptoProxy.releaseEndpoint();
51 keyCache = createKeysCache('key' as unknown as PrivateKeyReference);
54 it('caches decrypted links', async () => {
55 const { name } = await keyCache.decryptAndCacheLink(linkMock, {} as unknown as PrivateKeyReference);
57 expect(name).toEqual(DECRYPTED_NAME);
58 const key = keyCache.getCachedPrivateKey(linkMock.LinkID);
59 expect(key).toEqual(PRIVATE_KEY);
62 it("returns undefined when unknown link's keys are requested", async () => {
63 const result = keyCache.getCachedPrivateKey('new-link-id');
64 expect(result).toBe(undefined);