|
| 1 | +import type { TransactionControllerState } from '@metamask/transaction-controller'; |
| 2 | + |
| 3 | +import { ShieldController } from './ShieldController'; |
| 4 | +import { createMockBackend } from '../tests/mocks/backend'; |
| 5 | +import { createMockMessenger } from '../tests/mocks/messenger'; |
| 6 | +import { generateMockTxMeta } from '../tests/utils'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Sets up a ShieldController for testing. |
| 10 | + * |
| 11 | + * @param options - The options for setup. |
| 12 | + * @param options.coverageHistoryLimit - The coverage history limit. |
| 13 | + * @param options.transactionHistoryLimit - The transaction history limit. |
| 14 | + * @returns Objects that have been created for testing. |
| 15 | + */ |
| 16 | +function setup({ |
| 17 | + coverageHistoryLimit, |
| 18 | + transactionHistoryLimit, |
| 19 | +}: { |
| 20 | + coverageHistoryLimit?: number; |
| 21 | + transactionHistoryLimit?: number; |
| 22 | +} = {}) { |
| 23 | + const backend = createMockBackend(); |
| 24 | + const { messenger, baseMessenger } = createMockMessenger(); |
| 25 | + |
| 26 | + const controller = new ShieldController({ |
| 27 | + backend, |
| 28 | + coverageHistoryLimit, |
| 29 | + transactionHistoryLimit, |
| 30 | + messenger, |
| 31 | + }); |
| 32 | + controller.start(); |
| 33 | + return { |
| 34 | + controller, |
| 35 | + messenger, |
| 36 | + baseMessenger, |
| 37 | + backend, |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +describe('ShieldController', () => { |
| 42 | + describe('checkCoverage', () => { |
| 43 | + it('should trigger checkCoverage when a new transaction is added', async () => { |
| 44 | + const { baseMessenger, backend } = setup(); |
| 45 | + const txMeta = generateMockTxMeta(); |
| 46 | + const coverageResultReceived = new Promise<void>((resolve) => { |
| 47 | + baseMessenger.subscribe( |
| 48 | + 'ShieldController:coverageResultReceived', |
| 49 | + (_coverageResult) => resolve(), |
| 50 | + ); |
| 51 | + }); |
| 52 | + baseMessenger.publish( |
| 53 | + 'TransactionController:stateChange', |
| 54 | + { transactions: [txMeta] } as TransactionControllerState, |
| 55 | + undefined as never, |
| 56 | + ); |
| 57 | + expect(await coverageResultReceived).toBeUndefined(); |
| 58 | + expect(backend.checkCoverage).toHaveBeenCalledWith(txMeta); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should no longer trigger checkCoverage when controller is stopped', async () => { |
| 62 | + const { controller, baseMessenger, backend } = setup(); |
| 63 | + controller.stop(); |
| 64 | + const txMeta = generateMockTxMeta(); |
| 65 | + const coverageResultReceived = new Promise<void>((resolve, reject) => { |
| 66 | + baseMessenger.subscribe( |
| 67 | + 'ShieldController:coverageResultReceived', |
| 68 | + (_coverageResult) => resolve(), |
| 69 | + ); |
| 70 | + setTimeout( |
| 71 | + () => reject(new Error('Coverage result not received')), |
| 72 | + 100, |
| 73 | + ); |
| 74 | + }); |
| 75 | + baseMessenger.publish( |
| 76 | + 'TransactionController:stateChange', |
| 77 | + { transactions: [txMeta] } as TransactionControllerState, |
| 78 | + undefined as never, |
| 79 | + ); |
| 80 | + await expect(coverageResultReceived).rejects.toThrow( |
| 81 | + 'Coverage result not received', |
| 82 | + ); |
| 83 | + expect(backend.checkCoverage).not.toHaveBeenCalled(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should purge coverage history when the limit is exceeded', async () => { |
| 87 | + const { controller } = setup({ |
| 88 | + coverageHistoryLimit: 1, |
| 89 | + }); |
| 90 | + const txMeta = generateMockTxMeta(); |
| 91 | + await controller.checkCoverage(txMeta); |
| 92 | + await controller.checkCoverage(txMeta); |
| 93 | + expect(controller.state.coverageResults).toHaveProperty(txMeta.id); |
| 94 | + expect(controller.state.coverageResults[txMeta.id].results).toHaveLength( |
| 95 | + 1, |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should purge transaction history when the limit is exceeded', async () => { |
| 100 | + const { controller } = setup({ |
| 101 | + transactionHistoryLimit: 1, |
| 102 | + }); |
| 103 | + const txMeta1 = generateMockTxMeta(); |
| 104 | + const txMeta2 = generateMockTxMeta(); |
| 105 | + await controller.checkCoverage(txMeta1); |
| 106 | + await controller.checkCoverage(txMeta2); |
| 107 | + expect(controller.state.coverageResults).toHaveProperty(txMeta2.id); |
| 108 | + expect(controller.state.coverageResults[txMeta2.id].results).toHaveLength( |
| 109 | + 1, |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should check coverage when a transaction is simulated', async () => { |
| 114 | + const { baseMessenger, backend } = setup(); |
| 115 | + const txMeta = generateMockTxMeta(); |
| 116 | + const coverageResultReceived = new Promise<void>((resolve) => { |
| 117 | + baseMessenger.subscribe( |
| 118 | + 'ShieldController:coverageResultReceived', |
| 119 | + (_coverageResult) => resolve(), |
| 120 | + ); |
| 121 | + }); |
| 122 | + |
| 123 | + // Add transaction. |
| 124 | + baseMessenger.publish( |
| 125 | + 'TransactionController:stateChange', |
| 126 | + { transactions: [txMeta] } as TransactionControllerState, |
| 127 | + undefined as never, |
| 128 | + ); |
| 129 | + expect(await coverageResultReceived).toBeUndefined(); |
| 130 | + expect(backend.checkCoverage).toHaveBeenCalledWith(txMeta); |
| 131 | + |
| 132 | + // Simulate transaction. |
| 133 | + txMeta.simulationData = { |
| 134 | + tokenBalanceChanges: [], |
| 135 | + }; |
| 136 | + baseMessenger.publish( |
| 137 | + 'TransactionController:stateChange', |
| 138 | + { transactions: [txMeta] } as TransactionControllerState, |
| 139 | + undefined as never, |
| 140 | + ); |
| 141 | + expect(await coverageResultReceived).toBeUndefined(); |
| 142 | + expect(backend.checkCoverage).toHaveBeenCalledWith(txMeta); |
| 143 | + }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments