anchor-realloc.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import * as anchor from '@coral-xyz/anchor';
  2. import type { Program } from '@coral-xyz/anchor';
  3. import { Keypair } from '@solana/web3.js';
  4. import { assert } from 'chai';
  5. import type { AnchorRealloc } from '../target/types/anchor_realloc';
  6. describe('anchor-realloc', () => {
  7. // Configure the client to use the local cluster.
  8. const provider = anchor.AnchorProvider.env();
  9. anchor.setProvider(provider);
  10. const payer = provider.wallet as anchor.Wallet;
  11. const connection = provider.connection;
  12. const program = anchor.workspace.AnchorRealloc as Program<AnchorRealloc>;
  13. const messageAccount = new Keypair();
  14. // helper function to check the account data and message
  15. async function checkAccount(publicKey, expectedMessage) {
  16. const accountInfo = await connection.getAccountInfo(publicKey);
  17. const accountData = await program.account.message.fetch(publicKey);
  18. // 8 bytes for the discriminator,
  19. // 4 bytes for the length of the message,
  20. // and the length of the message
  21. assert.equal(accountInfo.data.length, 8 + 4 + expectedMessage.length);
  22. assert.equal(accountData.message, expectedMessage);
  23. console.log(`Account Data Length: ${accountInfo.data.length}`);
  24. console.log(`Message: ${accountData.message}`);
  25. }
  26. it('Is initialized!', async () => {
  27. const input = 'hello';
  28. await program.methods
  29. .initialize(input)
  30. .accounts({
  31. payer: payer.publicKey,
  32. messageAccount: messageAccount.publicKey,
  33. })
  34. .signers([messageAccount])
  35. .rpc();
  36. await checkAccount(messageAccount.publicKey, input);
  37. });
  38. it('Update', async () => {
  39. const input = 'hello world';
  40. await program.methods
  41. .update(input)
  42. .accounts({
  43. payer: payer.publicKey,
  44. messageAccount: messageAccount.publicKey,
  45. })
  46. .rpc();
  47. await checkAccount(messageAccount.publicKey, input);
  48. });
  49. it('Update', async () => {
  50. const input = 'hi';
  51. await program.methods
  52. .update(input)
  53. .accounts({
  54. payer: payer.publicKey,
  55. messageAccount: messageAccount.publicKey,
  56. })
  57. .rpc();
  58. await checkAccount(messageAccount.publicKey, input);
  59. });
  60. });