Skip to content

Commit ae37cec

Browse files
committed
Add tests
1 parent f32d831 commit ae37cec

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import * as anchor from '@coral-xyz/anchor';
2+
import { Program } from '@coral-xyz/anchor';
3+
import { assert } from 'chai';
4+
import { ReallocProgram } from '../target/types/realloc_program';
5+
6+
describe('realloc_program', () => {
7+
// Configure the client to use the local cluster.
8+
const provider = anchor.AnchorProvider.env();
9+
anchor.setProvider(provider);
10+
const program = anchor.workspace.ReallocProgram as Program<ReallocProgram>;
11+
12+
// Define account keypair and PDA
13+
const messageAccount = anchor.web3.Keypair.generate();
14+
let messagePDA: anchor.web3.PublicKey;
15+
let bump: number;
16+
17+
before(async () => {
18+
// Derive the PDA using the seed [b"message"]
19+
[messagePDA, bump] = await anchor.web3.PublicKey.findProgramAddress([Buffer.from('message')], program.programId);
20+
});
21+
22+
it('initialize the message account', async () => {
23+
// Define a message to store
24+
const initialMessage = 'Hello, Solana!';
25+
26+
// Call the initialize instruction
27+
await program.methods
28+
.initialize(initialMessage)
29+
.accounts({
30+
payer: provider.wallet.publicKey,
31+
})
32+
.signers([])
33+
.rpc();
34+
35+
// Fetch the account to confirm the data
36+
const account = await program.account.messageAccountState.fetch(messagePDA);
37+
assert.equal(account.message, initialMessage, 'Message should be initialized correctly');
38+
assert.equal(account.bump, bump, 'Bump value should match');
39+
});
40+
41+
it('update the message account', async () => {
42+
// Define a new message to update
43+
const updatedMessage = 'changed';
44+
45+
// Call the update instruction
46+
await program.methods
47+
.update(updatedMessage)
48+
.accounts({
49+
payer: provider.wallet.publicKey,
50+
})
51+
.signers([])
52+
.rpc();
53+
54+
// Fetch the account to confirm the updated data
55+
const account = await program.account.messageAccountState.fetch(messagePDA);
56+
assert.equal(account.message, updatedMessage, 'Message should be updated correctly');
57+
});
58+
});

0 commit comments

Comments
 (0)