litesvm.test.ts 2.4 KB

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