2020-01-09 04:34:58 +01:00
import assert from 'assert'
import migration23 from '../../../app/scripts/migrations/023'
2018-03-27 03:09:28 +02:00
const storage = {
2018-07-03 00:49:33 +02:00
'meta' : { } ,
'data' : {
'TransactionController' : {
'transactions' : [
] ,
2018-03-27 03:09:28 +02:00
} ,
} ,
}
const transactions = [ ]
const transactions40 = [ ]
const transactions20 = [ ]
const txStates = [
'unapproved' ,
'approved' ,
'signed' ,
'submitted' ,
'confirmed' ,
'rejected' ,
'failed' ,
'dropped' ,
]
const deletableTxStates = [
'confirmed' ,
'rejected' ,
'failed' ,
'dropped' ,
]
let nonDeletableCount = 0
let status
while ( transactions . length <= 100 ) {
status = txStates [ Math . floor ( Math . random ( ) * Math . floor ( txStates . length - 1 ) ) ]
2019-11-20 01:03:20 +01:00
if ( ! deletableTxStates . find ( ( s ) => s === status ) ) {
nonDeletableCount ++
}
2019-12-03 21:50:55 +01:00
transactions . push ( { status } )
2018-03-27 03:09:28 +02:00
}
while ( transactions40 . length < 40 ) {
status = txStates [ Math . floor ( Math . random ( ) * Math . floor ( txStates . length - 1 ) ) ]
2019-12-03 21:50:55 +01:00
transactions40 . push ( { status } )
2018-03-27 03:09:28 +02:00
}
while ( transactions20 . length < 20 ) {
status = txStates [ Math . floor ( Math . random ( ) * Math . floor ( txStates . length - 1 ) ) ]
2019-12-03 21:50:55 +01:00
transactions20 . push ( { status } )
2018-03-27 03:09:28 +02:00
}
storage . data . TransactionController . transactions = transactions
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 ) {
2018-03-27 03:09:28 +02:00
migration23 . migrate ( storage )
2019-07-31 22:17:11 +02:00
. then ( ( migratedData ) => {
let leftoverNonDeletableTxCount = 0
const migratedTransactions = migratedData . data . TransactionController . transactions
migratedTransactions . forEach ( ( tx ) => {
if ( ! deletableTxStates . find ( ( s ) => s === tx . status ) ) {
leftoverNonDeletableTxCount ++
}
} )
assert . equal ( leftoverNonDeletableTxCount , nonDeletableCount , 'migration shouldnt delete transactions we want to keep' )
assert ( ( migratedTransactions . length >= 40 ) , ` should be equal or greater to 40 if they are non deletable states got ${ migratedTransactions . length } transactions ` )
done ( )
} ) . catch ( done )
2018-03-27 03:09:28 +02:00
} )
2020-02-11 17:51:13 +01:00
it ( 'should not remove any transactions because 40 is the expectable limit' , function ( done ) {
2018-03-27 03:09:28 +02:00
storage . meta . version = 22
storage . data . TransactionController . transactions = transactions40
migration23 . migrate ( storage )
2019-07-31 22:17:11 +02:00
. then ( ( migratedData ) => {
const migratedTransactions = migratedData . data . TransactionController . transactions
2018-03-27 03:09:28 +02:00
2019-07-31 22:17:11 +02:00
assert . equal ( migratedTransactions . length , 40 , 'migration shouldnt delete when at limit' )
done ( )
} ) . catch ( done )
2018-03-27 03:09:28 +02:00
} )
2020-02-11 17:51:13 +01:00
it ( 'should not remove any transactions because 20 txs is under the expectable limit' , function ( done ) {
2018-03-27 03:09:28 +02:00
storage . meta . version = 22
storage . data . TransactionController . transactions = transactions20
migration23 . migrate ( storage )
2019-07-31 22:17:11 +02:00
. then ( ( migratedData ) => {
const migratedTransactions = migratedData . data . TransactionController . transactions
assert . equal ( migratedTransactions . length , 20 , 'migration shouldnt delete when under limit' )
done ( )
} ) . catch ( done )
2018-03-27 03:09:28 +02:00
} )
} )