counter-program.test.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 { startAnchor } from "solana-bankrun";
  6. import type { CounterProgram } from "../target/types/counter_program";
  7. import { expect } from "chai";
  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(
  12. "",
  13. [{ name: "counter_program", programId: PROGRAM_ID }],
  14. []
  15. );
  16. const provider = new BankrunProvider(context);
  17. const payer = provider.wallet as anchor.Wallet;
  18. const program = new anchor.Program<CounterProgram>(IDL, provider);
  19. const [stateAccount, _] = anchor.web3.PublicKey.findProgramAddressSync(
  20. [anchor.utils.bytes.utf8.encode("count")],
  21. program.programId
  22. );
  23. it("Initialize the counter", async () => {
  24. await program.methods
  25. .initialize()
  26. .accounts({
  27. state: stateAccount as PublicKey,
  28. user: payer.publicKey,
  29. })
  30. .signers([
  31. { publicKey: payer.publicKey, secretKey: payer.payer.secretKey },
  32. ])
  33. .rpc();
  34. const account = await program.account.counterState.fetch(stateAccount);
  35. console.log("Counter after initialization:", account.count.toString());
  36. // Expecting the count to be 0
  37. expect(account.count.toString()).to.equal("0");
  38. });
  39. it("Increment the counter", async () => {
  40. await program.methods
  41. .increment()
  42. .accounts({
  43. state: stateAccount,
  44. })
  45. .rpc();
  46. const account = await program.account.counterState.fetch(stateAccount);
  47. console.log("Counter after increment:", account.count.toString());
  48. expect(account.count.toString()).to.equal("1");
  49. });
  50. });