1 import { createTestItem } from '@proton/pass/lib/items/item.test.utils';
2 import type { EncryptedPassCache } from '@proton/pass/types/worker/cache';
3 import { uniqueId } from '@proton/pass/utils/string/unique-id';
5 import { cacheGuard, migrate } from './migrate';
6 import { INITIAL_HIGHSECURITY_SETTINGS, rootReducer } from './reducers';
7 import { INITIAL_ORGANIZATION_SETTINGS } from './reducers/organization';
9 describe('`migrate`', () => {
10 test('should sanitize invalid items', () => {
11 const state = rootReducer(undefined, { type: '__TEST__' });
12 const shareId = uniqueId();
14 const login = createTestItem('login');
15 const note = createTestItem('note', { shareId });
17 state.items.byShareId[login.shareId] = { [login.itemId]: login };
19 state.items.byShareId[shareId] = {
22 [uniqueId()]: undefined,
27 const migration = migrate(state, { from: '1.0.0', to: '2.0.0 ' });
29 expect(migration.items.byShareId[shareId]).toStrictEqual({ [note.itemId]: note });
30 expect(migration.items.byShareId[login.shareId]).toStrictEqual({ [login.itemId]: login });
33 test('should move organization data out of user state', () => {
34 const state = rootReducer(undefined, { type: '__TEST__' });
35 const organization = { ID: uniqueId(), Name: 'ProtonB2B' };
36 state.user = { organization } as any;
37 const migration = migrate(state, { from: '1.0.0', to: '2.0.0 ' });
39 expect('organization' in migration.user).toBe(false);
41 expect(migration.organization).toStrictEqual({
44 settings: INITIAL_ORGANIZATION_SETTINGS,
48 test('should hydrate initial `HighSecurity` settings if empty', () => {
49 const state = rootReducer(undefined, { type: '__TEST__' });
50 state.user.userSettings = {} as any;
51 const migration = migrate(state, { from: '1.0.0', to: '2.0.0 ' });
53 expect(migration.user.userSettings!.HighSecurity).toStrictEqual(INITIAL_HIGHSECURITY_SETTINGS);
57 describe('`cacheGuard`', () => {
58 const createMockCache = (version: string): EncryptedPassCache => ({
60 encryptedCacheKey: 'cache-key',
61 snapshot: 'encrypted-snapshot',
62 state: 'encrypted-state',
63 salt: 'encryption-salt',
66 test.each(['1.0.0', '0.0.0.9', '1.0.0-rc0'])(
67 'should return original cache when `%s` is older than app version',
69 const cache = createMockCache(version);
71 expect(cacheGuard(cache, '2.0.0')).toEqual(cache);
72 expect(cacheGuard(cache, '1.0.1')).toEqual(cache);
73 expect(cacheGuard(cache, '1.2.0-rc1')).toEqual(cache);
77 test('should return original cache when versions are equal', () => {
78 const cache = createMockCache('2.0.0');
79 expect(cacheGuard(cache, '2.0.0')).toEqual(cache);
82 test.each(['2.0.2', '2.1.0.1', '2.2.0-rc1'])(
83 'should return empty object when `%s` is newer than app version',
85 const cache = createMockCache(version);
87 expect(cacheGuard(cache, '1.9.0')).toEqual({});
88 expect(cacheGuard(cache, '2.0.0')).toEqual({});
89 expect(cacheGuard(cache, '2.0.0-rc2')).toEqual({});
93 test('should handle missing version in cache', () => {
94 const cache = createMockCache('');
97 expect(cacheGuard(cache, '1.0.0')).toEqual({});
98 expect(cacheGuard(createMockCache(''), '2.0.0')).toEqual({});