1- import * as anchor from " @coral-xyz/anchor" ;
2- import { Program } from " @coral-xyz/anchor" ;
3- import { Keypair } from " @solana/web3.js" ;
4- import { assert } from " chai" ;
5- import { CounterProgram } from " ../target/types/counter_program" ;
1+ import * as anchor from ' @coral-xyz/anchor' ;
2+ import { Program } from ' @coral-xyz/anchor' ;
3+ import { Keypair } from ' @solana/web3.js' ;
4+ import { assert } from ' chai' ;
5+ import { CounterProgram } from ' ../target/types/counter_program' ;
66
7- describe ( " counter_program" , ( ) => {
7+ describe ( ' counter_program' , ( ) => {
88 // Configure the client to use the local cluster.
99
1010 const provider = anchor . AnchorProvider . env ( ) ;
@@ -14,119 +14,65 @@ describe("counter_program", () => {
1414
1515 const program = anchor . workspace . CounterProgram as Program < CounterProgram > ;
1616
17- const [ counterState , _ ] = anchor . web3 . PublicKey . findProgramAddressSync (
18- [ anchor . utils . bytes . utf8 . encode ( "count" ) ] ,
19- program . programId
20- ) ;
17+ const [ counterState , _ ] = anchor . web3 . PublicKey . findProgramAddressSync ( [ anchor . utils . bytes . utf8 . encode ( 'count' ) ] , program . programId ) ;
2118
22- it ( " Initialize Counter" , async ( ) => {
19+ it ( ' Initialize Counter' , async ( ) => {
2320 await program . methods
2421 . initializeCounter ( )
2522 . accounts ( {
2623 payer : payer . publicKey ,
2724 } )
2825 . rpc ( ) ;
2926
30- const currentCount = await program . account . counter . fetch (
31- counterState
32- ) ;
27+ const currentCount = await program . account . counter . fetch ( counterState ) ;
3328
34- assert (
35- currentCount . count . toNumber ( ) === 0 ,
36- "Expected initialized count to be 0"
37- ) ;
29+ assert ( currentCount . count . toNumber ( ) === 0 , 'Expected initialized count to be 0' ) ;
3830 } ) ;
3931
32+ it ( 'Increment Counter' , async ( ) => {
33+ await program . methods . increment ( ) . accounts ( { } ) . rpc ( ) ;
4034
41- it ( "Increment Counter" , async ( ) => {
42- await program . methods
43- . increment ( )
44- . accounts ( { } )
45- . rpc ( ) ;
46-
47- const currentCount = await program . account . counter . fetch (
48- counterState
49- ) ;
35+ const currentCount = await program . account . counter . fetch ( counterState ) ;
5036
51- assert ( currentCount . count . toNumber ( ) === 1 , " Expected count to be 1" ) ;
37+ assert ( currentCount . count . toNumber ( ) === 1 , ' Expected count to be 1' ) ;
5238 } ) ;
5339
54- it ( "Increment Counter Again" , async ( ) => {
55- await program . methods
56- . increment ( )
57- . accounts ( { counter : counterState } )
58- . rpc ( ) ;
40+ it ( 'Increment Counter Again' , async ( ) => {
41+ await program . methods . increment ( ) . accounts ( { counter : counterState } ) . rpc ( ) ;
5942
60- const currentCount = await program . account . counter . fetch (
61- counterState
62- ) ;
43+ const currentCount = await program . account . counter . fetch ( counterState ) ;
44+
45+ assert ( currentCount . count . toNumber ( ) === 2 , 'Expected count to be 2' ) ;
46+ } ) ;
47+ it ( 'Decrement counter' , async ( ) => {
48+ await program . methods . decrement ( ) . accounts ( { } ) . rpc ( ) ;
49+
50+ const currentCount = await program . account . counter . fetch ( counterState ) ;
51+ assert ( currentCount . count . toNumber ( ) === 1 , 'Expected count to be 1' ) ;
52+ } ) ;
53+ it ( 'Increment and decrement multiple times' , async ( ) => {
54+ // Increment the counter 5 times
55+ for ( let i = 0 ; i < 5 ; i ++ ) {
56+ await program . methods . increment ( ) . accounts ( { } ) . rpc ( ) ;
57+ }
58+
59+ let currentCount = await program . account . counter . fetch ( counterState ) ;
60+ assert . strictEqual ( currentCount . count . toNumber ( ) , 6 , 'Expected count to be 6 after 5 increments' ) ;
61+
62+ // Decrement the counter 4 times
63+ for ( let i = 0 ; i < 4 ; i ++ ) {
64+ await program . methods . decrement ( ) . accounts ( { } ) . rpc ( ) ;
65+ }
66+
67+ currentCount = await program . account . counter . fetch ( counterState ) ;
68+ assert . strictEqual ( currentCount . count . toNumber ( ) , 2 , 'Expected count to be 2 after 4 decrements' ) ;
69+ } ) ;
6370
64- assert ( currentCount . count . toNumber ( ) === 2 , "Expected count to be 2" ) ;
71+ it ( 'Cannot decrement below 0' , async ( ) => {
72+ // Decrement the counter to 0
73+ await program . methods . decrement ( ) . accounts ( { } ) . rpc ( ) ;
74+ await program . methods . decrement ( ) . accounts ( { } ) . rpc ( ) ;
75+ const currentCount = await program . account . counter . fetch ( counterState ) ;
76+ assert . strictEqual ( currentCount . count . toNumber ( ) , 0 , 'Expected count to be 0 after multiple decrements' ) ;
6577 } ) ;
66- it ( "Decrement counter" , async ( ) => {
67- await program . methods
68- . decrement ( )
69- . accounts ( { } )
70- . rpc ( ) ;
71-
72- const currentCount = await program . account . counter . fetch (
73- counterState
74- ) ;
75- assert ( currentCount . count . toNumber ( ) === 1 , "Expected count to be 1" ) ;
76- } ) ;
77- it ( "Increment and decrement multiple times" , async ( ) => {
78- // Increment the counter 5 times
79- for ( let i = 0 ; i < 5 ; i ++ ) {
80- await program . methods
81- . increment ( )
82- . accounts ( { } )
83- . rpc ( ) ;
84- }
85-
86- let currentCount = await program . account . counter . fetch (
87- counterState
88- ) ;
89- assert . strictEqual (
90- currentCount . count . toNumber ( ) ,
91- 6 ,
92- "Expected count to be 6 after 5 increments"
93- ) ;
94-
95- // Decrement the counter 4 times
96- for ( let i = 0 ; i < 4 ; i ++ ) {
97- await program . methods
98- . decrement ( )
99- . accounts ( { } )
100- . rpc ( ) ;
101- }
102-
103- currentCount = await program . account . counter . fetch (
104- counterState
105- ) ;
106- assert . strictEqual (
107- currentCount . count . toNumber ( ) ,
108- 2 ,
109- "Expected count to be 2 after 4 decrements"
110- ) ;
111- } ) ;
112-
113- it ( "Cannot decrement below 0" , async ( ) => {
114- // Decrement the counter to 0
115- await program . methods
116- . decrement ( )
117- . accounts ( { } )
118- . rpc ( ) ;
119- await program . methods
120- . decrement ( )
121- . accounts ( { } )
122- . rpc ( ) ;
123- const currentCount = await program . account . counter . fetch (
124- counterState
125- ) ;
126- assert . strictEqual (
127- currentCount . count . toNumber ( ) ,
128- 0 ,
129- "Expected count to be 0 after multiple decrements"
130- ) ;
131- } ) ;
13278} ) ;
0 commit comments