bankrun.test.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. const IDL = require('../target/idl/realloc_program.json');
  9. const PROGRAM_ID = new PublicKey(IDL.address);
  10. describe('realloc_program', async () => {
  11. const context = await startAnchor('', [{ name: 'realloc_program', programId: PROGRAM_ID }], []);
  12. const provider = new BankrunProvider(context);
  13. const connection = provider.connection;
  14. const payer = provider.wallet as anchor.Wallet;
  15. const program = new anchor.Program<ReallocProgram>(IDL, provider);
  16. // Define the message account
  17. const messageAccount = Keypair.generate();
  18. let messagePDA: PublicKey;
  19. let bump: number;
  20. // Helper function to check account data and message
  21. async function checkAccount(publicKey: PublicKey, expectedMessage: string) {
  22. const accountData = await program.account.messageAccountState.fetch(publicKey);
  23. // Verify the message and bump
  24. assert.equal(accountData.message, expectedMessage, 'Message should match expected value');
  25. assert.equal(accountData.bump, bump, 'Bump should match expected value');
  26. }
  27. it('initialize the message account', async () => {
  28. const initialMessage = 'Hello, Solana!';
  29. // Call the initialize instruction
  30. await program.methods
  31. .initialize(initialMessage)
  32. .accounts({
  33. payer: payer.publicKey,
  34. })
  35. .signers([])
  36. .rpc();
  37. [messagePDA, bump] = await PublicKey.findProgramAddress([Buffer.from('message')], program.programId);
  38. // Verify the account data
  39. await checkAccount(messagePDA, initialMessage);
  40. });
  41. it('update the message account', async () => {
  42. const updatedMessage = 'Updated Message';
  43. // Call the update instruction
  44. await program.methods
  45. .update(updatedMessage)
  46. .accounts({
  47. payer: payer.publicKey,
  48. account: messagePDA,
  49. })
  50. .rpc();
  51. // Verify the account data
  52. await checkAccount(messagePDA, updatedMessage);
  53. });
  54. });