Skip to content

Commit e654f7c

Browse files
committed
feat: add LiteSVM tests for page visit tracking in Anchor program
* Implement tests for creating and incrementing page visit counts using LiteSVM. * Update existing test structure to include assertions for page visit tracking. * Refactor test descriptions for clarity.
1 parent 020ebe7 commit e654f7c

File tree

2 files changed

+130
-6
lines changed

2 files changed

+130
-6
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import anchor from "@coral-xyz/anchor";
2+
import {
3+
Keypair,
4+
PublicKey,
5+
SystemProgram,
6+
Transaction,
7+
TransactionInstruction,
8+
} from "@solana/web3.js";
9+
import { assert } from "chai";
10+
import { LiteSVM } from "litesvm";
11+
import IDL from "../target/idl/program_derived_addresses_program.json" with {
12+
type: "json",
13+
};
14+
15+
describe("LiteSVM: PDA", () => {
16+
const svm = new LiteSVM();
17+
const programId = new PublicKey(IDL.address);
18+
const coder = new anchor.BorshCoder(IDL as anchor.Idl);
19+
20+
const payer = Keypair.generate();
21+
svm.airdrop(payer.publicKey, BigInt(1000000000));
22+
23+
const programPath = new URL(
24+
"../target/deploy/program_derived_addresses_program.so",
25+
import.meta.url,
26+
).pathname;
27+
svm.addProgramFromFile(programId, programPath);
28+
29+
// PDA for the page visits account
30+
const [pageVisitPDA] = PublicKey.findProgramAddressSync(
31+
[Buffer.from("page_visits"), payer.publicKey.toBuffer()],
32+
programId,
33+
);
34+
35+
it("Create the page visits tracking PDA", () => {
36+
const data = coder.instruction.encode("create_page_visits", {});
37+
const ix = new TransactionInstruction({
38+
keys: [
39+
{ pubkey: payer.publicKey, isSigner: true, isWritable: true },
40+
{ pubkey: pageVisitPDA, isSigner: false, isWritable: true },
41+
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
42+
],
43+
programId,
44+
data,
45+
});
46+
47+
const tx = new Transaction().add(ix);
48+
tx.feePayer = payer.publicKey;
49+
tx.recentBlockhash = svm.latestBlockhash();
50+
tx.sign(payer);
51+
svm.sendTransaction(tx);
52+
53+
//Fetch the pageVisitPDA account and check it page visit count
54+
const pageVisitPDAAccInfo = svm.getAccount(pageVisitPDA);
55+
const pageVisitAccount = coder.accounts.decode(
56+
"PageVisits",
57+
Buffer.from(pageVisitPDAAccInfo.data),
58+
);
59+
60+
assert.equal(pageVisitAccount.page_visits, 0);
61+
});
62+
63+
it("Visit the page!", () => {
64+
const data = coder.instruction.encode("increment_page_visits", {});
65+
const ix = new TransactionInstruction({
66+
keys: [
67+
{ pubkey: payer.publicKey, isSigner: true, isWritable: true },
68+
{ pubkey: pageVisitPDA, isSigner: false, isWritable: true },
69+
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
70+
],
71+
programId,
72+
data,
73+
});
74+
75+
const tx = new Transaction().add(ix);
76+
tx.feePayer = payer.publicKey;
77+
tx.recentBlockhash = svm.latestBlockhash();
78+
tx.sign(payer);
79+
svm.sendTransaction(tx);
80+
svm.expireBlockhash();
81+
82+
//Fetch the pageVisitPDA account and check it page visit count
83+
const pageVisitPDAAccInfo = svm.getAccount(pageVisitPDA);
84+
const pageVisitAccount = coder.accounts.decode(
85+
"PageVisits",
86+
Buffer.from(pageVisitPDAAccInfo.data),
87+
);
88+
89+
assert.equal(pageVisitAccount.page_visits, 1);
90+
});
91+
92+
it("Again visit the page!", () => {
93+
const data = coder.instruction.encode("increment_page_visits", {});
94+
const ix = new TransactionInstruction({
95+
keys: [
96+
{ pubkey: payer.publicKey, isSigner: true, isWritable: true },
97+
{ pubkey: pageVisitPDA, isSigner: false, isWritable: true },
98+
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
99+
],
100+
programId,
101+
data,
102+
});
103+
104+
const tx = new Transaction().add(ix);
105+
tx.feePayer = payer.publicKey;
106+
tx.recentBlockhash = svm.latestBlockhash();
107+
tx.sign(payer);
108+
svm.sendTransaction(tx);
109+
110+
//Fetch the pageVisitPDA account and check it page visit count
111+
const pageVisitPDAAccInfo = svm.getAccount(pageVisitPDA);
112+
const pageVisitAccount = coder.accounts.decode(
113+
"PageVisits",
114+
Buffer.from(pageVisitPDAAccInfo.data),
115+
);
116+
117+
assert.equal(pageVisitAccount.page_visits, 2);
118+
});
119+
});

basics/program-derived-addresses/anchor/tests/test.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as anchor from "@coral-xyz/anchor";
22
import { PublicKey } from "@solana/web3.js";
3-
import type { ProgramDerivedAddressesProgram } from "../target/types/program_derived_addresses_program";
3+
import { assert } from "chai";
4+
import type { ProgramDerivedAddressesProgram } from "../target/types/program_derived_addresses_program.ts";
45

5-
describe("PDAs", () => {
6+
describe("Anchor: PDAs", () => {
67
const provider = anchor.AnchorProvider.env();
78
anchor.setProvider(provider);
89
const payer = provider.wallet as anchor.Wallet;
@@ -22,6 +23,9 @@ describe("PDAs", () => {
2223
payer: payer.publicKey,
2324
})
2425
.rpc();
26+
27+
const pageVisits = await program.account.pageVisits.fetch(pageVisitPDA);
28+
assert.equal(pageVisits.pageVisits, 0);
2529
});
2630

2731
it("Visit the page!", async () => {
@@ -31,19 +35,20 @@ describe("PDAs", () => {
3135
user: payer.publicKey,
3236
})
3337
.rpc();
38+
39+
const pageVisits = await program.account.pageVisits.fetch(pageVisitPDA);
40+
assert.equal(pageVisits.pageVisits, 1);
3441
});
3542

36-
it("Visit the page!", async () => {
43+
it("Again visit the page!", async () => {
3744
await program.methods
3845
.incrementPageVisits()
3946
.accounts({
4047
user: payer.publicKey,
4148
})
4249
.rpc();
43-
});
4450

45-
it("View page visits", async () => {
4651
const pageVisits = await program.account.pageVisits.fetch(pageVisitPDA);
47-
console.log(`Number of page visits: ${pageVisits.pageVisits}`);
52+
assert.equal(pageVisits.pageVisits, 2);
4853
});
4954
});

0 commit comments

Comments
 (0)