@@ -7,51 +7,35 @@ import { describe, test, expect, beforeAll, afterAll } from "vitest";
7
7
8
8
const TEST_DB_PATH = `${ import . meta. dirname } /test-db` ;
9
9
10
- // Utility function to clean the test database directory
11
- function cleanTestDB ( )
12
- {
13
- if ( fs . existsSync ( TEST_DB_PATH ) )
14
- {
15
- fs . rmSync ( TEST_DB_PATH , { recursive : true } ) ;
16
- }
17
- }
18
-
19
- const minerKeys = Wallet . generateKeyPair ( ) ;
20
- function initializeBlockchain ( )
21
- {
22
- const consensus = new POWConsensus ( ) ;
23
- return new Blockchain ( {
24
- dbPath : TEST_DB_PATH ,
25
- nodes : {
26
- list : [ "http://127.0.0.1:3001" ] ,
27
- hostUrl : "http://127.0.0.1:3000"
28
- } ,
29
- chainName : "test-chain" ,
30
- minerPublicKey : minerKeys . publicKey ,
31
- consensus
32
- } ) ;
33
- }
34
-
35
10
describe ( "Blockchain Test Suite" , ( ) =>
36
11
{
12
+ let blockchain : Blockchain ;
13
+ let senderKeys : KeyPair ;
14
+ let receiverKeys : KeyPair ;
15
+ let minerKeys : KeyPair ;
16
+
37
17
beforeAll ( ( ) =>
38
18
{
39
19
cleanTestDB ( ) ;
40
- } , 1000 ) ;
20
+ minerKeys = Wallet . generateKeyPair ( ) ;
21
+ blockchain = initializeBlockchain ( minerKeys . publicKey ) ; // miner: 100
22
+ senderKeys = Wallet . generateKeyPair ( ) ;
23
+ receiverKeys = Wallet . generateKeyPair ( ) ;
24
+ } ) ;
25
+
41
26
afterAll ( ( ) =>
42
27
{
43
28
cleanTestDB ( ) ;
44
- } , 1000 ) ;
29
+ } ) ;
45
30
46
31
test ( "serial test" , ( ) =>
47
32
{
48
- const blockchain = initializeBlockchain ( ) ; // miner: 100
49
-
50
- const senderKeys = Wallet . generateKeyPair ( ) ;
51
- const receiverKeys = Wallet . generateKeyPair ( ) ;
52
33
const newBlock = blockchain . mineNewBlock ( ) ; // miner: 200
53
34
expect ( newBlock . index ) . toBe ( 1 ) ;
35
+ } ) ;
54
36
37
+ test ( "parallel test" , ( ) =>
38
+ {
55
39
const transaction1 = new Transaction ( {
56
40
from : minerKeys . publicKey ,
57
41
to : senderKeys . publicKey ,
@@ -169,3 +153,26 @@ describe( "Blockchain Test Suite", () =>
169
153
}
170
154
} ) ;
171
155
} ) ;
156
+
157
+ function cleanTestDB ( )
158
+ {
159
+ if ( fs . existsSync ( TEST_DB_PATH ) )
160
+ {
161
+ fs . rmSync ( TEST_DB_PATH , { recursive : true } ) ;
162
+ }
163
+ }
164
+
165
+ function initializeBlockchain ( minerKeysPublicKey : string )
166
+ {
167
+ const consensus = new POWConsensus ( ) ;
168
+ return new Blockchain ( {
169
+ dbPath : TEST_DB_PATH ,
170
+ nodes : {
171
+ list : [ "http://127.0.0.1:3001" ] ,
172
+ hostUrl : "http://127.0.0.1:3000"
173
+ } ,
174
+ chainName : "test-chain" ,
175
+ minerPublicKey : minerKeysPublicKey ,
176
+ consensus
177
+ } ) ;
178
+ }
0 commit comments