aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit/migrations/023-test.js
blob: 7da94448d829dadc582a9cca5bf47e870dc0cb32 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const assert = require('assert')
const migration23 = require('../../../app/scripts/migrations/023')
const storage = {
  'meta': {},
  'data': {
    'TransactionController': {
      'transactions': [
      ],
    },
  },
}

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))]
  if (!deletableTxStates.find((s) => s === status)) nonDeletableCount++
  transactions.push({status})
}

while (transactions40.length < 40) {
  status = txStates[Math.floor(Math.random() * Math.floor(txStates.length - 1))]
  transactions40.push({status})
}

while (transactions20.length < 20) {
  status = txStates[Math.floor(Math.random() * Math.floor(txStates.length - 1))]
  transactions20.push({status})
}


storage.data.TransactionController.transactions = transactions

describe('storage is migrated successfully and the proper transactions are remove from state', () => {
  it('should remove transactions that are unneeded', (done) => {
    migration23.migrate(storage)
    .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)
  })

  it('should not remove any transactions because 40 is the expectable limit', (done) => {
    storage.meta.version = 22
    storage.data.TransactionController.transactions = transactions40
    migration23.migrate(storage)
    .then((migratedData) => {
      const migratedTransactions = migratedData.data.TransactionController.transactions

      assert.equal(migratedTransactions.length, 40, 'migration shouldnt delete when at limit')
      done()
    }).catch(done)
  })

  it('should not remove any transactions because 20 txs is under the expectable limit', (done) => {
    storage.meta.version = 22
    storage.data.TransactionController.transactions = transactions20
    migration23.migrate(storage)
    .then((migratedData) => {
      const migratedTransactions = migratedData.data.TransactionController.transactions
      assert.equal(migratedTransactions.length, 20, 'migration shouldnt delete when under limit')
      done()
    }).catch(done)
  })

})