1+ import { describe , it } from 'node:test' ;
2+ import * as anchor from '@coral-xyz/anchor' ;
3+ import { Keypair , PublicKey } from '@solana/web3.js' ;
4+ import { BankrunProvider } from 'anchor-bankrun' ;
5+ import { assert } from 'chai' ;
6+ import { startAnchor } from 'solana-bankrun' ;
7+ import type { ReallocProgram } from '../target/types/realloc_program' ;
8+
9+ const IDL = require ( '../target/idl/realloc_program.json' ) ;
10+ const PROGRAM_ID = new PublicKey ( IDL . address ) ;
11+
12+ describe ( 'realloc_program' , async ( ) => {
13+ const context = await startAnchor ( '' , [ { name : 'realloc_program' , programId : PROGRAM_ID } ] , [ ] ) ;
14+ const provider = new BankrunProvider ( context ) ;
15+ const connection = provider . connection ;
16+ const payer = provider . wallet as anchor . Wallet ;
17+ const program = new anchor . Program < ReallocProgram > ( IDL , provider ) ;
18+
19+ // Define the message account
20+ const messageAccount = Keypair . generate ( ) ;
21+ let messagePDA : PublicKey ;
22+ let bump : number ;
23+
24+ // Helper function to check account data and message
25+ async function checkAccount ( publicKey : PublicKey , expectedMessage : string ) {
26+ const accountData = await program . account . messageAccountState . fetch ( publicKey ) ;
27+
28+ // Verify the message and bump
29+ assert . equal ( accountData . message , expectedMessage , 'Message should match expected value' ) ;
30+ assert . equal ( accountData . bump , bump , 'Bump should match expected value' ) ;
31+ }
32+
33+ it ( 'initialize the message account' , async ( ) => {
34+ const initialMessage = 'Hello, Solana!' ;
35+
36+ // Call the initialize instruction
37+ await program . methods
38+ . initialize ( initialMessage )
39+ . accounts ( {
40+ payer : payer . publicKey ,
41+ } )
42+ . signers ( [ ] )
43+ . rpc ( ) ;
44+
45+ [ messagePDA , bump ] = await PublicKey . findProgramAddress ( [ Buffer . from ( 'message' ) ] , program . programId ) ;
46+
47+ // Verify the account data
48+ await checkAccount ( messagePDA , initialMessage ) ;
49+ } ) ;
50+
51+ it ( 'update the message account' , async ( ) => {
52+ const updatedMessage = 'Updated Message' ;
53+
54+ // Call the update instruction
55+ await program . methods
56+ . update ( updatedMessage )
57+ . accounts ( {
58+ payer : payer . publicKey ,
59+ } )
60+ . rpc ( ) ;
61+
62+ // Verify the account data
63+ await checkAccount ( messagePDA , updatedMessage ) ;
64+ } ) ;
65+ } ) ;
0 commit comments