2021-02-04 19:15:23 +01:00
|
|
|
import assert from 'assert';
|
|
|
|
import { TRANSACTION_STATUSES } 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
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const txStates = Object.values(TRANSACTION_STATUSES);
|
2018-03-27 03:09:28 +02:00
|
|
|
|
2020-11-07 08:38:12 +01:00
|
|
|
const deletableTxStates = [
|
|
|
|
TRANSACTION_STATUSES.CONFIRMED,
|
|
|
|
TRANSACTION_STATUSES.REJECTED,
|
|
|
|
TRANSACTION_STATUSES.FAILED,
|
|
|
|
TRANSACTION_STATUSES.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
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('storage is migrated successfully and the proper transactions are remove from state', function () {
|
|
|
|
it('should remove transactions that are unneeded', function (done) {
|
2020-11-03 00:41:28 +01:00
|
|
|
migration23
|
|
|
|
.migrate(storage)
|
2019-07-31 22:17:11 +02:00
|
|
|
.then((migratedData) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
let leftoverNonDeletableTxCount = 0;
|
2020-11-03 00:41:28 +01:00
|
|
|
const migratedTransactions =
|
2021-02-04 19:15:23 +01:00
|
|
|
migratedData.data.TransactionController.transactions;
|
2019-07-31 22:17:11 +02:00
|
|
|
migratedTransactions.forEach((tx) => {
|
|
|
|
if (!deletableTxStates.find((s) => s === tx.status)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
leftoverNonDeletableTxCount += 1;
|
2019-07-31 22:17:11 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
leftoverNonDeletableTxCount,
|
|
|
|
nonDeletableCount,
|
|
|
|
"migration shouldn't delete transactions we want to keep",
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
assert(
|
|
|
|
migratedTransactions.length >= 40,
|
|
|
|
`should be equal or greater to 40 if they are non deletable states got ${migratedTransactions.length} transactions`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
done();
|
2020-11-03 00:41:28 +01:00
|
|
|
})
|
2021-02-04 19:15:23 +01:00
|
|
|
.catch(done);
|
|
|
|
});
|
2018-03-27 03:09:28 +02:00
|
|
|
|
2020-07-20 19:02:49 +02:00
|
|
|
it('should not remove any transactions because 40 is the expected limit', function (done) {
|
2021-02-04 19:15:23 +01:00
|
|
|
storage.meta.version = 22;
|
|
|
|
storage.data.TransactionController.transactions = transactions40;
|
2020-11-03 00:41:28 +01:00
|
|
|
migration23
|
|
|
|
.migrate(storage)
|
2019-07-31 22:17:11 +02:00
|
|
|
.then((migratedData) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const migratedTransactions =
|
2021-02-04 19:15:23 +01:00
|
|
|
migratedData.data.TransactionController.transactions;
|
2018-03-27 03:09:28 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
migratedTransactions.length,
|
|
|
|
40,
|
|
|
|
"migration shouldn't delete when at limit",
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
done();
|
2020-11-03 00:41:28 +01:00
|
|
|
})
|
2021-02-04 19:15:23 +01:00
|
|
|
.catch(done);
|
|
|
|
});
|
2018-03-27 03:09:28 +02:00
|
|
|
|
2020-07-20 19:02:49 +02:00
|
|
|
it('should not remove any transactions because 20 txs is under the expected limit', function (done) {
|
2021-02-04 19:15:23 +01:00
|
|
|
storage.meta.version = 22;
|
|
|
|
storage.data.TransactionController.transactions = transactions20;
|
2020-11-03 00:41:28 +01:00
|
|
|
migration23
|
|
|
|
.migrate(storage)
|
2019-07-31 22:17:11 +02:00
|
|
|
.then((migratedData) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const migratedTransactions =
|
2021-02-04 19:15:23 +01:00
|
|
|
migratedData.data.TransactionController.transactions;
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
migratedTransactions.length,
|
|
|
|
20,
|
|
|
|
"migration shouldn't delete when under limit",
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
done();
|
2020-11-03 00:41:28 +01:00
|
|
|
})
|
2021-02-04 19:15:23 +01:00
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
});
|