2023-01-18 15:47:29 +01:00
|
|
|
import { TransactionStatus } from '../../../shared/constants/transaction';
|
2021-03-16 22:00:08 +01:00
|
|
|
import migration23 from './023';
|
2020-01-09 04:34:58 +01:00
|
|
|
|
2018-03-27 03:09:28 +02:00
|
|
|
const storage = {
|
2020-11-03 00:41:28 +01:00
|
|
|
meta: {},
|
|
|
|
data: {
|
|
|
|
TransactionController: {
|
|
|
|
transactions: [],
|
2018-03-27 03:09:28 +02:00
|
|
|
},
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-03-27 03:09:28 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const transactions = [];
|
|
|
|
const transactions40 = [];
|
|
|
|
const transactions20 = [];
|
2018-03-27 03:09:28 +02:00
|
|
|
|
2023-01-18 15:47:29 +01:00
|
|
|
const txStates = Object.values(TransactionStatus);
|
2018-03-27 03:09:28 +02:00
|
|
|
|
2020-11-07 08:38:12 +01:00
|
|
|
const deletableTxStates = [
|
2023-01-18 15:47:29 +01:00
|
|
|
TransactionStatus.confirmed,
|
|
|
|
TransactionStatus.rejected,
|
|
|
|
TransactionStatus.failed,
|
|
|
|
TransactionStatus.dropped,
|
2021-02-04 19:15:23 +01:00
|
|
|
];
|
2018-03-27 03:09:28 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let nonDeletableCount = 0;
|
2018-03-27 03:09:28 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let status;
|
2018-03-27 03:09:28 +02:00
|
|
|
while (transactions.length <= 100) {
|
2021-02-04 19:15:23 +01:00
|
|
|
status =
|
|
|
|
txStates[Math.floor(Math.random() * Math.floor(txStates.length - 1))];
|
2020-08-12 21:59:14 +02:00
|
|
|
// This is an old migration, let's allow it
|
|
|
|
// eslint-disable-next-line no-loop-func
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!deletableTxStates.find((s) => s === status)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
nonDeletableCount += 1;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
transactions.push({ status });
|
2018-03-27 03:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (transactions40.length < 40) {
|
2021-02-04 19:15:23 +01:00
|
|
|
status =
|
|
|
|
txStates[Math.floor(Math.random() * Math.floor(txStates.length - 1))];
|
|
|
|
transactions40.push({ status });
|
2018-03-27 03:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (transactions20.length < 20) {
|
2021-02-04 19:15:23 +01:00
|
|
|
status =
|
|
|
|
txStates[Math.floor(Math.random() * Math.floor(txStates.length - 1))];
|
|
|
|
transactions20.push({ status });
|
2018-03-27 03:09:28 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
storage.data.TransactionController.transactions = transactions;
|
2018-03-27 03:09:28 +02:00
|
|
|
|
2021-09-21 18:28:13 +02:00
|
|
|
describe('storage is migrated successfully and the proper transactions are remove from state', () => {
|
|
|
|
it('should remove transactions that are unneeded', async () => {
|
|
|
|
const migratedData = await migration23.migrate(storage);
|
|
|
|
|
|
|
|
let leftoverNonDeletableTxCount = 0;
|
|
|
|
const migratedTransactions =
|
|
|
|
migratedData.data.TransactionController.transactions;
|
|
|
|
migratedTransactions.forEach((tx) => {
|
|
|
|
if (!deletableTxStates.find((s) => s === tx.status)) {
|
|
|
|
leftoverNonDeletableTxCount += 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(leftoverNonDeletableTxCount).toStrictEqual(nonDeletableCount);
|
|
|
|
|
|
|
|
expect(migratedTransactions.length >= 40).toStrictEqual(true);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-03-27 03:09:28 +02:00
|
|
|
|
2021-09-21 18:28:13 +02:00
|
|
|
it('should not remove any transactions because 40 is the expected limit', async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
storage.meta.version = 22;
|
|
|
|
storage.data.TransactionController.transactions = transactions40;
|
2021-09-21 18:28:13 +02:00
|
|
|
|
|
|
|
const migratedData = await migration23.migrate(storage);
|
|
|
|
const migratedTransactions =
|
|
|
|
migratedData.data.TransactionController.transactions;
|
|
|
|
|
|
|
|
expect(migratedTransactions).toHaveLength(40);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-03-27 03:09:28 +02:00
|
|
|
|
2021-09-21 18:28:13 +02:00
|
|
|
it('should not remove any transactions because 20 txs is under the expected limit', async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
storage.meta.version = 22;
|
|
|
|
storage.data.TransactionController.transactions = transactions20;
|
2021-09-21 18:28:13 +02:00
|
|
|
|
|
|
|
const migratedData = await migration23.migrate(storage);
|
|
|
|
const migratedTransactions =
|
|
|
|
migratedData.data.TransactionController.transactions;
|
|
|
|
|
|
|
|
expect(migratedTransactions).toHaveLength(20);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|