|
| 1 | +import { cloneDeep } from 'lodash'; |
| 2 | +import { migrate, version } from './120.3'; |
| 3 | + |
| 4 | +const sentryCaptureExceptionMock = jest.fn(); |
| 5 | + |
| 6 | +global.sentry = { |
| 7 | + captureException: sentryCaptureExceptionMock, |
| 8 | +}; |
| 9 | + |
| 10 | +const oldVersion = 120.2; |
| 11 | + |
| 12 | +describe('migration #120.3', () => { |
| 13 | + afterEach(() => { |
| 14 | + jest.resetAllMocks(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('updates the version metadata', async () => { |
| 18 | + const oldStorage = { |
| 19 | + meta: { version: oldVersion }, |
| 20 | + data: {}, |
| 21 | + }; |
| 22 | + |
| 23 | + const newStorage = await migrate(oldStorage); |
| 24 | + |
| 25 | + expect(newStorage.meta).toStrictEqual({ version }); |
| 26 | + }); |
| 27 | + |
| 28 | + it('returns state unchanged if TransactionController state is missing', async () => { |
| 29 | + const oldStorage = { |
| 30 | + meta: { version: oldVersion }, |
| 31 | + data: { |
| 32 | + PreferencesController: {}, |
| 33 | + }, |
| 34 | + }; |
| 35 | + const oldStorageDataClone = cloneDeep(oldStorage.data); |
| 36 | + |
| 37 | + const newStorage = await migrate(oldStorage); |
| 38 | + |
| 39 | + expect(newStorage.data).toStrictEqual(oldStorageDataClone); |
| 40 | + }); |
| 41 | + |
| 42 | + it('reports error and returns state unchanged if TransactionController state is invalid', async () => { |
| 43 | + const oldStorage = { |
| 44 | + meta: { version: oldVersion }, |
| 45 | + data: { |
| 46 | + PreferencesController: {}, |
| 47 | + TransactionController: 'invalid', |
| 48 | + }, |
| 49 | + }; |
| 50 | + const oldStorageDataClone = cloneDeep(oldStorage.data); |
| 51 | + |
| 52 | + const newStorage = await migrate(oldStorage); |
| 53 | + |
| 54 | + expect(sentryCaptureExceptionMock).toHaveBeenCalledWith( |
| 55 | + `Migration ${version}: Invalid TransactionController state of type 'string'`, |
| 56 | + ); |
| 57 | + expect(newStorage.data).toStrictEqual(oldStorageDataClone); |
| 58 | + }); |
| 59 | + |
| 60 | + it('returns state unchanged if transactions are missing', async () => { |
| 61 | + const oldStorage = { |
| 62 | + meta: { version: oldVersion }, |
| 63 | + data: { |
| 64 | + PreferencesController: {}, |
| 65 | + TransactionController: {}, |
| 66 | + }, |
| 67 | + }; |
| 68 | + const oldStorageDataClone = cloneDeep(oldStorage.data); |
| 69 | + |
| 70 | + const newStorage = await migrate(oldStorage); |
| 71 | + |
| 72 | + expect(newStorage.data).toStrictEqual(oldStorageDataClone); |
| 73 | + }); |
| 74 | + |
| 75 | + it('reports error and returns state unchanged if transactions property is invalid', async () => { |
| 76 | + const oldStorage = { |
| 77 | + meta: { version: oldVersion }, |
| 78 | + data: { |
| 79 | + PreferencesController: {}, |
| 80 | + TransactionController: { |
| 81 | + transactions: 'invalid', |
| 82 | + }, |
| 83 | + }, |
| 84 | + }; |
| 85 | + const oldStorageDataClone = cloneDeep(oldStorage.data); |
| 86 | + |
| 87 | + const newStorage = await migrate(oldStorage); |
| 88 | + |
| 89 | + expect(sentryCaptureExceptionMock).toHaveBeenCalledWith( |
| 90 | + `Migration ${version}: Invalid TransactionController transactions state of type 'string'`, |
| 91 | + ); |
| 92 | + expect(newStorage.data).toStrictEqual(oldStorageDataClone); |
| 93 | + }); |
| 94 | + |
| 95 | + it('reports error and returns state unchanged if there is an invalid transaction', async () => { |
| 96 | + const oldStorage = { |
| 97 | + meta: { version: oldVersion }, |
| 98 | + data: { |
| 99 | + PreferencesController: {}, |
| 100 | + TransactionController: { |
| 101 | + transactions: [ |
| 102 | + {}, // empty object is valid for the purposes of this migration |
| 103 | + 'invalid', |
| 104 | + {}, |
| 105 | + ], |
| 106 | + }, |
| 107 | + }, |
| 108 | + }; |
| 109 | + const oldStorageDataClone = cloneDeep(oldStorage.data); |
| 110 | + |
| 111 | + const newStorage = await migrate(oldStorage); |
| 112 | + |
| 113 | + expect(sentryCaptureExceptionMock).toHaveBeenCalledWith( |
| 114 | + `Migration ${version}: Invalid transaction of type 'string'`, |
| 115 | + ); |
| 116 | + expect(newStorage.data).toStrictEqual(oldStorageDataClone); |
| 117 | + }); |
| 118 | + |
| 119 | + it('reports error and returns state unchanged if there is a transaction with an invalid history property', async () => { |
| 120 | + const oldStorage = { |
| 121 | + meta: { version: oldVersion }, |
| 122 | + data: { |
| 123 | + PreferencesController: {}, |
| 124 | + TransactionController: { |
| 125 | + transactions: [ |
| 126 | + {}, // empty object is valid for the purposes of this migration |
| 127 | + { |
| 128 | + history: 'invalid', |
| 129 | + }, |
| 130 | + {}, |
| 131 | + ], |
| 132 | + }, |
| 133 | + }, |
| 134 | + }; |
| 135 | + const oldStorageDataClone = cloneDeep(oldStorage.data); |
| 136 | + |
| 137 | + const newStorage = await migrate(oldStorage); |
| 138 | + |
| 139 | + expect(sentryCaptureExceptionMock).toHaveBeenCalledWith( |
| 140 | + `Migration ${version}: Invalid transaction history of type 'string'`, |
| 141 | + ); |
| 142 | + expect(newStorage.data).toStrictEqual(oldStorageDataClone); |
| 143 | + }); |
| 144 | + |
| 145 | + it('returns state unchanged if there are no transactions', async () => { |
| 146 | + const oldStorage = { |
| 147 | + meta: { version: oldVersion }, |
| 148 | + data: { |
| 149 | + PreferencesController: {}, |
| 150 | + TransactionController: { |
| 151 | + transactions: [], |
| 152 | + }, |
| 153 | + }, |
| 154 | + }; |
| 155 | + const oldStorageDataClone = cloneDeep(oldStorage.data); |
| 156 | + |
| 157 | + const newStorage = await migrate(oldStorage); |
| 158 | + |
| 159 | + expect(newStorage.data).toStrictEqual(oldStorageDataClone); |
| 160 | + }); |
| 161 | + |
| 162 | + it('returns state unchanged if there are no transactions with history', async () => { |
| 163 | + const oldStorage = { |
| 164 | + meta: { version: oldVersion }, |
| 165 | + data: { |
| 166 | + PreferencesController: {}, |
| 167 | + TransactionController: { |
| 168 | + transactions: [{}, {}, {}], |
| 169 | + }, |
| 170 | + }, |
| 171 | + }; |
| 172 | + const oldStorageDataClone = cloneDeep(oldStorage.data); |
| 173 | + |
| 174 | + const newStorage = await migrate(oldStorage); |
| 175 | + |
| 176 | + expect(newStorage.data).toStrictEqual(oldStorageDataClone); |
| 177 | + }); |
| 178 | + |
| 179 | + it('returns state unchanged if there are no transactions with history exceeding max size', async () => { |
| 180 | + const oldStorage = { |
| 181 | + meta: { version: oldVersion }, |
| 182 | + data: { |
| 183 | + PreferencesController: {}, |
| 184 | + TransactionController: { |
| 185 | + transactions: [ |
| 186 | + { |
| 187 | + history: [...Array(99).keys()], |
| 188 | + }, |
| 189 | + { |
| 190 | + history: [...Array(100).keys()], |
| 191 | + }, |
| 192 | + { |
| 193 | + history: [], |
| 194 | + }, |
| 195 | + ], |
| 196 | + }, |
| 197 | + }, |
| 198 | + }; |
| 199 | + const oldStorageDataClone = cloneDeep(oldStorage.data); |
| 200 | + |
| 201 | + const newStorage = await migrate(oldStorage); |
| 202 | + |
| 203 | + expect(newStorage.data).toStrictEqual(oldStorageDataClone); |
| 204 | + }); |
| 205 | + |
| 206 | + it('trims histories exceeding max size', async () => { |
| 207 | + const oldStorage = { |
| 208 | + meta: { version: oldVersion }, |
| 209 | + data: { |
| 210 | + PreferencesController: {}, |
| 211 | + TransactionController: { |
| 212 | + transactions: [ |
| 213 | + { |
| 214 | + history: [...Array(99).keys()], |
| 215 | + }, |
| 216 | + { |
| 217 | + history: [...Array(100).keys()], |
| 218 | + }, |
| 219 | + { |
| 220 | + history: [...Array(101).keys()], |
| 221 | + }, |
| 222 | + { |
| 223 | + history: [...Array(1000).keys()], |
| 224 | + }, |
| 225 | + ], |
| 226 | + }, |
| 227 | + }, |
| 228 | + }; |
| 229 | + const oldStorageDataClone = cloneDeep(oldStorage.data); |
| 230 | + |
| 231 | + const newStorage = await migrate(oldStorage); |
| 232 | + |
| 233 | + expect(newStorage.data).toStrictEqual({ |
| 234 | + ...oldStorageDataClone, |
| 235 | + TransactionController: { |
| 236 | + transactions: [ |
| 237 | + { |
| 238 | + history: [...Array(99).keys()], |
| 239 | + }, |
| 240 | + { |
| 241 | + history: [...Array(100).keys()], |
| 242 | + }, |
| 243 | + { |
| 244 | + history: [...Array(100).keys()], |
| 245 | + }, |
| 246 | + { |
| 247 | + history: [...Array(100).keys()], |
| 248 | + }, |
| 249 | + ], |
| 250 | + }, |
| 251 | + }); |
| 252 | + }); |
| 253 | +}); |
0 commit comments