Skip to content

Commit acf5c1d

Browse files
committed
feat: add LiteSVM tests for repository-layout anchor program
* Implement tests for various interactions at the carnival, including going on rides, playing games, and eating food. * Introduce a utility function to create and send transactions for the defined interactions. * Utilize LiteSVM and Anchor for testing the carnival program's functionality.
1 parent 020ebe7 commit acf5c1d

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import anchor from "@coral-xyz/anchor";
2+
import {
3+
Keypair,
4+
LAMPORTS_PER_SOL,
5+
PublicKey,
6+
Transaction,
7+
TransactionInstruction,
8+
} from "@solana/web3.js";
9+
import { LiteSVM } from "litesvm";
10+
import Idl from "../target/idl/carnival.json" with { type: "json" };
11+
12+
describe("LiteSVM: Carnival", () => {
13+
const svm = new LiteSVM();
14+
const programId = new PublicKey(Idl.address);
15+
const coder = new anchor.BorshCoder(Idl as anchor.Idl);
16+
const payer = Keypair.generate();
17+
svm.airdrop(payer.publicKey, BigInt(5 * LAMPORTS_PER_SOL));
18+
19+
const programPath = new URL("../target/deploy/carnival.so", import.meta.url)
20+
.pathname;
21+
svm.addProgramFromFile(programId, programPath);
22+
23+
it("Go on some rides!", () => {
24+
const jimmyIxArgs = {
25+
name: "Jimmy",
26+
height: 36,
27+
ticket_count: 15,
28+
ride_name: "Scrambler",
29+
};
30+
const maryIxArgs = {
31+
name: "Mary",
32+
height: 52,
33+
ticket_count: 1,
34+
ride_name: "Ferris Wheel",
35+
};
36+
const bobIxArgs = {
37+
name: "Alice",
38+
height: 56,
39+
ticket_count: 15,
40+
ride_name: "Scrambler",
41+
};
42+
const aliceIxArgs = {
43+
name: "Bob",
44+
height: 49,
45+
ticket_count: 6,
46+
ride_name: "Tilt-a-Whirl",
47+
};
48+
49+
createAndSendTx(jimmyIxArgs, "go_on_ride");
50+
createAndSendTx(maryIxArgs, "go_on_ride");
51+
createAndSendTx(bobIxArgs, "go_on_ride");
52+
createAndSendTx(aliceIxArgs, "go_on_ride");
53+
});
54+
55+
it("Play some games!", () => {
56+
const jimmyIxArgs = {
57+
name: "Jimmy",
58+
ticket_count: 15,
59+
game_name: "I Got It!",
60+
};
61+
const maryIxArgs = {
62+
name: "Mary",
63+
ticket_count: 1,
64+
game_name: "Ring Toss",
65+
};
66+
const aliceIxArgs = {
67+
name: "Alice",
68+
ticket_count: 15,
69+
game_name: "Ladder Climb",
70+
};
71+
const bobIxArgs = {
72+
name: "Bob",
73+
ticket_count: 6,
74+
game_name: "Ring Toss",
75+
};
76+
77+
createAndSendTx(jimmyIxArgs, "play_game");
78+
createAndSendTx(maryIxArgs, "play_game");
79+
createAndSendTx(aliceIxArgs, "play_game");
80+
createAndSendTx(bobIxArgs, "play_game");
81+
});
82+
83+
it("Eat some food!", () => {
84+
const jimmyIxArgs = {
85+
name: "Jimmy",
86+
ticket_count: 15,
87+
food_stand_name: "Taco Shack",
88+
};
89+
const maryIxArgs = {
90+
name: "Mary",
91+
ticket_count: 1,
92+
food_stand_name: "Larry's Pizza",
93+
};
94+
const aliceIxArgs = {
95+
name: "Alice",
96+
ticket_count: 15,
97+
food_stand_name: "Dough Boy's",
98+
};
99+
const bobIxArgs = {
100+
name: "Bob",
101+
ticket_count: 6,
102+
food_stand_name: "Dough Boy's",
103+
};
104+
105+
createAndSendTx(jimmyIxArgs, "eat_food");
106+
createAndSendTx(maryIxArgs, "eat_food");
107+
createAndSendTx(aliceIxArgs, "eat_food");
108+
createAndSendTx(bobIxArgs, "eat_food");
109+
});
110+
111+
function createAndSendTx(
112+
ixArgs: Record<string, string | number>,
113+
ixName: string,
114+
) {
115+
const data = coder.instruction.encode(ixName, ixArgs);
116+
const ix = new TransactionInstruction({
117+
keys: [{ pubkey: payer.publicKey, isSigner: true, isWritable: true }],
118+
programId,
119+
data,
120+
});
121+
122+
const tx = new Transaction().add(ix);
123+
tx.feePayer = payer.publicKey;
124+
tx.recentBlockhash = svm.latestBlockhash();
125+
tx.sign(payer);
126+
const re = svm.sendTransaction(tx);
127+
// console.log(re.toString());
128+
}
129+
});

0 commit comments

Comments
 (0)