litesvm.test.ts 2.2 KB

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