counter-program.test.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { describe, it } from 'node:test';
  2. import * as anchor from '@coral-xyz/anchor';
  3. import { Keypair, PublicKey } from '@solana/web3.js';
  4. import { BankrunProvider } from 'anchor-bankrun';
  5. import { expect } from 'chai';
  6. import { startAnchor } from 'solana-bankrun';
  7. import type { CounterProgram } from '../target/types/counter_program';
  8. const IDL = require('../target/idl/counter_program.json');
  9. const PROGRAM_ID = new PublicKey(IDL.address);
  10. describe('counter_program', async () => {
  11. const context = await startAnchor('', [{ name: 'counter_program', programId: PROGRAM_ID }], []);
  12. const provider = new BankrunProvider(context);
  13. const payer = provider.wallet as anchor.Wallet;
  14. const program = new anchor.Program<CounterProgram>(IDL, provider);
  15. const [stateAccount, _] = anchor.web3.PublicKey.findProgramAddressSync([anchor.utils.bytes.utf8.encode('count')], program.programId);
  16. it('Initialize the counter', async () => {
  17. await program.methods
  18. .initialize()
  19. .accounts({
  20. state: stateAccount as PublicKey,
  21. user: payer.publicKey,
  22. })
  23. .signers([{ publicKey: payer.publicKey, secretKey: payer.payer.secretKey }])
  24. .rpc();
  25. const account = await program.account.counterState.fetch(stateAccount);
  26. console.log('Counter after initialization:', account.count.toString());
  27. // Expecting the count to be 0
  28. expect(account.count.toString()).to.equal('0');
  29. });
  30. it('Increment the counter', async () => {
  31. await program.methods
  32. .increment()
  33. .accounts({
  34. state: stateAccount,
  35. })
  36. .rpc();
  37. const account = await program.account.counterState.fetch(stateAccount);
  38. console.log('Counter after increment:', account.count.toString());
  39. expect(account.count.toString()).to.equal('1');
  40. });
  41. });