2021-12-06 17:40:39 +01:00
|
|
|
/* eslint-disable jest/no-conditional-expect */
|
2021-02-04 19:15:23 +01:00
|
|
|
import fs from 'fs';
|
|
|
|
import { cloneDeep } from 'lodash';
|
2021-03-16 22:00:08 +01:00
|
|
|
import liveMigrations from '../../migrations';
|
|
|
|
import data from '../../first-time-state';
|
|
|
|
import Migrator from '.';
|
2020-01-09 04:34:58 +01:00
|
|
|
|
2018-04-06 02:49:50 +02:00
|
|
|
const stubMigrations = [
|
2017-05-11 19:47:58 +02:00
|
|
|
{
|
|
|
|
version: 1,
|
2020-08-18 21:24:47 +02:00
|
|
|
migrate: (state) => {
|
2017-05-11 19:47:58 +02:00
|
|
|
// clone the data just like we do in migrations
|
2021-02-04 19:15:23 +01:00
|
|
|
const clonedData = cloneDeep(state);
|
|
|
|
clonedData.meta.version = 1;
|
|
|
|
return Promise.resolve(clonedData);
|
2017-05-11 19:47:58 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
version: 2,
|
2020-08-18 21:24:47 +02:00
|
|
|
migrate: (state) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const clonedData = cloneDeep(state);
|
|
|
|
clonedData.meta.version = 2;
|
|
|
|
return Promise.resolve(clonedData);
|
2017-05-11 19:47:58 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
version: 3,
|
2020-08-18 21:24:47 +02:00
|
|
|
migrate: (state) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const clonedData = cloneDeep(state);
|
|
|
|
clonedData.meta.version = 3;
|
|
|
|
return Promise.resolve(clonedData);
|
2017-05-11 19:47:58 +02:00
|
|
|
},
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
];
|
|
|
|
const versionedData = { meta: { version: 0 }, data: { hello: 'world' } };
|
2018-04-06 02:49:50 +02:00
|
|
|
|
|
|
|
const firstTimeState = {
|
|
|
|
meta: { version: 0 },
|
2020-01-09 04:34:58 +01:00
|
|
|
data,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-12-13 01:14:29 +01:00
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
describe('migrations', () => {
|
|
|
|
describe('liveMigrations require list', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
let migrationNumbers;
|
2020-02-27 05:49:59 +01:00
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
beforeAll(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const fileNames = fs.readdirSync('./app/scripts/migrations/');
|
2020-02-27 05:49:59 +01:00
|
|
|
migrationNumbers = fileNames
|
|
|
|
.reduce((acc, filename) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const name = filename.split('.')[0];
|
2020-11-03 00:41:28 +01:00
|
|
|
if (/^\d+$/u.test(name)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
acc.push(name);
|
2020-02-27 05:49:59 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return acc;
|
2020-02-27 05:49:59 +01:00
|
|
|
}, [])
|
2021-02-04 19:15:23 +01:00
|
|
|
.map((num) => parseInt(num, 10));
|
|
|
|
});
|
2020-02-27 05:49:59 +01:00
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
it('should include all migrations', () => {
|
2020-02-11 17:51:13 +01:00
|
|
|
migrationNumbers.forEach((num) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const migration = liveMigrations.find((m) => m.version === num);
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(migration.version).toStrictEqual(num);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2020-02-27 05:49:59 +01:00
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
it('should have tests for all migrations', () => {
|
2021-03-16 22:00:08 +01:00
|
|
|
const fileNames = fs.readdirSync('./app/scripts/migrations/');
|
2020-02-27 05:49:59 +01:00
|
|
|
const testNumbers = fileNames
|
|
|
|
.reduce((acc, filename) => {
|
2021-03-09 20:08:06 +01:00
|
|
|
const name = filename.split('.test.')[0];
|
2021-12-06 17:40:39 +01:00
|
|
|
// eslint-disable-next-line jest/no-if
|
2020-11-03 00:41:28 +01:00
|
|
|
if (/^\d+$/u.test(name)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
acc.push(name);
|
2020-02-27 05:49:59 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return acc;
|
2020-02-27 05:49:59 +01:00
|
|
|
}, [])
|
2021-02-04 19:15:23 +01:00
|
|
|
.map((num) => parseInt(num, 10));
|
2020-02-27 05:49:59 +01:00
|
|
|
|
|
|
|
migrationNumbers.forEach((num) => {
|
|
|
|
if (num >= 33) {
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(testNumbers).toContain(num);
|
2020-02-27 05:49:59 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-04-06 02:49:50 +02:00
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
describe('Migrator', () => {
|
|
|
|
it('migratedData version should be version 3', async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const migrator = new Migrator({ migrations: stubMigrations });
|
|
|
|
const migratedData = await migrator.migrateData(versionedData);
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(migratedData.meta.version).toStrictEqual(
|
|
|
|
stubMigrations[2].version,
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-04-06 02:49:50 +02:00
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
it('should match the last version in live migrations', async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const migrator = new Migrator({ migrations: liveMigrations });
|
|
|
|
const migratedData = await migrator.migrateData(firstTimeState);
|
|
|
|
const last = liveMigrations.length - 1;
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(migratedData.meta.version).toStrictEqual(
|
|
|
|
liveMigrations[last].version,
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-04-06 02:49:50 +02:00
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
it('should emit an error', async () => {
|
2020-02-11 17:51:13 +01:00
|
|
|
const migrator = new Migrator({
|
2020-11-03 00:41:28 +01:00
|
|
|
migrations: [
|
|
|
|
{
|
|
|
|
version: 1,
|
|
|
|
async migrate() {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error('test');
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
2020-02-11 17:51:13 +01:00
|
|
|
},
|
2020-11-03 00:41:28 +01:00
|
|
|
],
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-12-06 17:40:39 +01:00
|
|
|
await expect(async () => {
|
|
|
|
await migrator.migrateData({ meta: { version: 0 } });
|
|
|
|
}).rejects.toThrow('Error: MetaMask Migration Error #1: test');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|