realloc-program.ts 2.0 KB

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