bankrun.test.ts 2.4 KB

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