@@ -4,6 +4,9 @@ import { PublicKey } from '@solana/web3.js';
4
4
import * as anchor from '@coral-xyz/anchor' ;
5
5
import { BN , Program } from "@coral-xyz/anchor" ;
6
6
7
+ // This is a "Bankrun" way of writing cases. Bankrun helps manage all the setup stuff
8
+ // needed for case to run successfully. It doesn't depend on solana-test-validator, so
9
+ // it's more convenient way to do testing.
7
10
8
11
const IDL = require ( "../target/idl/voting.json" ) ;
9
12
import { Voting } from '../target/types/voting' ;
@@ -12,31 +15,81 @@ const PUPPET_PROGRAM_ID = new PublicKey("5s3PtT8kLYCv1WEp6dSh3T7EuF35Z6jSu5Cvx4h
12
15
13
16
describe ( 'Create a system account' , ( ) => {
14
17
15
- test ( "bankrun" , async ( ) => {
16
- const context = await startAnchor ( "" , [ { name : "voting" , programId : PUPPET_PROGRAM_ID } ] , [ ] ) ;
17
- const provider = new BankrunProvider ( context ) ;
18
+ let context ;
19
+ let provider ;
20
+ let votingProg ;
21
+ let pollAddress ;
18
22
19
- const puppetProgram = new Program < Voting > (
20
- IDL ,
21
- provider ,
22
- ) ;
23
-
24
- const [ pollAddress ] = PublicKey . findProgramAddressSync (
23
+ beforeAll ( async ( ) => {
24
+ console . log ( "init context,provider,voting program instance ..." ) ;
25
+ context = await startAnchor ( "" , [ { name : "voting" , programId : PUPPET_PROGRAM_ID } ] , [ ] ) ;
26
+ provider = new BankrunProvider ( context ) ;
27
+ votingProg = new Program < Voting > ( IDL , provider ) ;
28
+ [ pollAddress ] = PublicKey . findProgramAddressSync (
25
29
[ Buffer . from ( "poll" ) , new anchor . BN ( 1 ) . toArrayLike ( Buffer , "le" , 8 ) ] ,
26
- puppetProgram . programId
30
+ votingProg . programId
27
31
) ;
32
+ console . log ( "program id: " , votingProg . programId ) ;
33
+ } )
28
34
29
- await puppetProgram . methods . initializePoll (
35
+ test ( "initialize poll" , async ( ) => {
36
+ await votingProg . methods . initializePoll (
30
37
new anchor . BN ( 1 ) ,
31
38
new anchor . BN ( 0 ) ,
32
39
new anchor . BN ( 1759508293 ) ,
33
40
"test-poll" ,
34
41
"description" ,
35
42
) . rpc ( ) ;
36
43
37
- const pollAccount = await puppetProgram . account . pollAccount . fetch ( pollAddress ) ;
44
+ const pollAccount = await votingProg . account . pollAccount . fetch ( pollAddress ) ;
38
45
console . log ( pollAccount ) ;
46
+ expect ( pollAccount . pollOptionIndex . toNumber ( ) ) . toEqual ( 0 ) ;
47
+ expect ( pollAccount . pollDescription ) . toEqual ( "description" ) ;
48
+ expect ( pollAccount . pollVotingStart . toNumber ( ) )
49
+ . toBeLessThan ( pollAccount . pollVotingEnd . toNumber ( ) ) ;
50
+ } ) ;
51
+
52
+ it ( "initialize candidate" , async ( ) => {
53
+ await votingProg . methods . initializeCandidate (
54
+ new anchor . BN ( 1 ) ,
55
+ "Smooth"
56
+ ) . accounts ( { pollAccount : pollAddress } )
57
+ . rpc ( ) ;
58
+
59
+ await votingProg . methods . initializeCandidate (
60
+ new anchor . BN ( 1 ) ,
61
+ "Crunchy"
62
+ ) . accounts ( { pollAccount : pollAddress } )
63
+ . rpc ( ) ;
64
+
65
+ const [ crunchyAddr ] = PublicKey . findProgramAddressSync (
66
+ [ new anchor . BN ( 1 ) . toArrayLike ( Buffer , 'le' , 8 ) , Buffer . from ( "Crunchy" ) ] ,
67
+ votingProg . programId ,
68
+ ) ;
69
+ const crunchyData = await votingProg . account . candidateAccount . fetch ( crunchyAddr ) ;
70
+ console . log ( crunchyData ) ;
71
+ expect ( crunchyData . candidateVotes . toNumber ( ) ) . toEqual ( 0 ) ;
39
72
73
+ const pollData = await votingProg . account . pollAccount . fetch ( pollAddress ) ;
74
+ expect ( pollData . pollOptionIndex . toNumber ( ) ) . toEqual ( 2 ) ;
75
+ } ) ;
76
+
77
+ it ( "vote" , async ( ) => {
78
+ await votingProg . methods . vote (
79
+ new anchor . BN ( 1 ) ,
80
+ "Crunchy"
81
+ ) . rpc ( ) ;
82
+
83
+ await votingProg . methods . vote (
84
+ new anchor . BN ( 1 ) ,
85
+ "Crunchy"
86
+ ) . rpc ( ) ;
87
+ const [ crunchyAddr ] = PublicKey . findProgramAddressSync (
88
+ [ new anchor . BN ( 1 ) . toArrayLike ( Buffer , 'le' , 8 ) , Buffer . from ( "Crunchy" ) ] ,
89
+ votingProg . programId ,
90
+ ) ;
91
+ const crunchyData = await votingProg . account . candidateAccount . fetch ( crunchyAddr ) ;
92
+ expect ( crunchyData . candidateVotes . toNumber ( ) ) . toEqual ( 2 ) ;
40
93
} ) ;
41
94
42
95
} ) ;
0 commit comments