Skip to content

Commit e8fea97

Browse files
committed
tests: check that ttl is used
1 parent 1122fd1 commit e8fea97

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

__tests__/storage/storage.test.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
import * as cryptoUtils from '../../src/utils/crypto';
2626
import { InvalidPasswdError } from '../../src/errors';
2727
import Network from '../../src/models/network';
28-
import { ILockedUtxo, IUtxo, OutputValueType, WALLET_FLAGS } from '../../src/types';
28+
import { IHistoryTx, ILockedUtxo, IUtxo, IUtxoId, OutputValueType, WALLET_FLAGS } from '../../src/types';
2929

3030
const DATA_DIR = './testdata.leveldb';
3131

@@ -362,7 +362,7 @@ test('selecting utxos', async () => {
362362
const options = {
363363
filter_method: utxo => utxo.txId === wantedTx,
364364
};
365-
const buf = [];
365+
const buf: IUtxo[] = [];
366366
for await (const utxo of storage.selectUtxos(options)) {
367367
buf.push(utxo);
368368
}
@@ -379,14 +379,14 @@ test('utxos selected as inputs', async () => {
379379
storage.utxosSelectedAsInput.set('a-tx-id2:0', true);
380380

381381
// Should check if the utxo is selected as input
382-
await expect(storage.isUtxoSelectedAsInput({ txId: 'a-tx-id1', index: '0' })).resolves.toBe(true);
383-
await expect(storage.isUtxoSelectedAsInput({ txId: 'a-tx-id2', index: '0' })).resolves.toBe(true);
384-
await expect(storage.isUtxoSelectedAsInput({ txId: 'a-tx-id3', index: '0' })).resolves.toBe(
382+
await expect(storage.isUtxoSelectedAsInput({ txId: 'a-tx-id1', index: 0 })).resolves.toBe(true);
383+
await expect(storage.isUtxoSelectedAsInput({ txId: 'a-tx-id2', index: 0 })).resolves.toBe(true);
384+
await expect(storage.isUtxoSelectedAsInput({ txId: 'a-tx-id3', index: 0 })).resolves.toBe(
385385
false
386386
);
387387

388388
// Iterate on all utxos selected as input
389-
const buf = [];
389+
const buf: IUtxoId[] = [];
390390
for await (const u of storage.utxoSelectedAsInputIter()) {
391391
buf.push(u);
392392
}
@@ -395,7 +395,10 @@ test('utxos selected as inputs', async () => {
395395
expect(buf).toContainEqual({ txId: 'a-tx-id2', index: 0 });
396396

397397
const tx = { txId: 'a-tx-id3', outputs: [{ value: 10, token: '00', spent_by: null }] };
398-
const getTxSpy = jest.spyOn(storage, 'getTx').mockImplementation(async () => tx);
398+
const getTxSpy = jest.spyOn(storage, 'getTx').mockImplementation(
399+
async _txId => {
400+
return tx as unknown as IHistoryTx;
401+
});
399402
// no timeout, mark as selected: true
400403
await storage.utxoSelectAsInput({ txId: 'a-tx-id3', index: 0 }, true);
401404
expect(storage.utxosSelectedAsInput.get('a-tx-id3:0')).toBe(true);
@@ -411,12 +414,27 @@ test('utxos selected as inputs', async () => {
411414
await storage.utxoSelectAsInput({ txId: 'a-tx-id4', index: 0 }, true);
412415
expect(storage.utxosSelectedAsInput.get('a-tx-id4:0')).toBeUndefined();
413416
// Or with a spent output
414-
getTxSpy.mockImplementation(async () => ({
415-
txId: 'a-tx-id3',
416-
outputs: [{ value: 10, token: '00', spent_by: 'a-tx-id5' }],
417-
}));
417+
getTxSpy.mockImplementation(async _txId => {
418+
return {
419+
txId: 'a-tx-id3',
420+
outputs: [{ value: 10, token: '00', spent_by: 'a-tx-id5' }],
421+
} as unknown as IHistoryTx;
422+
});
418423
await storage.utxoSelectAsInput({ txId: 'a-tx-id3', index: 0 }, true);
419424
expect(storage.utxosSelectedAsInput.get('a-tx-id3:0')).toBeUndefined();
425+
426+
getTxSpy.mockImplementation(async _txId => {
427+
return {
428+
txId: 'a-tx-id4',
429+
outputs: [{ value: 10, token: '00', spent_by: null }],
430+
} as unknown as IHistoryTx;
431+
});
432+
await storage.utxoSelectAsInput({ txId: 'a-tx-id4', index: 0 }, true, 500);
433+
expect(storage.utxosSelectedAsInput.get('a-tx-id4:0')).toBe(true);
434+
await new Promise<void>(resolve => {
435+
setTimeout(resolve, 1000);
436+
});
437+
expect(storage.utxosSelectedAsInput.get('a-tx-id4:0')).not.toBe(true);
420438
});
421439

422440
describe('process locked utxos', () => {

0 commit comments

Comments
 (0)