test files

This commit is contained in:
warlock 2025-04-09 21:22:31 +05:30
parent 773da40650
commit 0746f63bb9
3 changed files with 206 additions and 14 deletions

View File

@ -1,5 +1,5 @@
{ {
"license": "ISC", "license": "ISC",
"scripts": { "scripts": {
"lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w", "lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check" "lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
@ -8,13 +8,14 @@
"@coral-xyz/anchor": "^0.30.1" "@coral-xyz/anchor": "^0.30.1"
}, },
"devDependencies": { "devDependencies": {
"@types/bn.js": "^5.1.0",
"@types/chai": "^5.2.1",
"@types/mocha": "^10.0.10",
"@types/node": "^22.14.0",
"chai": "^4.3.4", "chai": "^4.3.4",
"mocha": "^9.0.3", "mocha": "^9.0.3",
"prettier": "^2.6.2",
"ts-mocha": "^10.0.0", "ts-mocha": "^10.0.0",
"@types/bn.js": "^5.1.0", "typescript": "^4.3.5"
"@types/chai": "^4.3.0",
"@types/mocha": "^9.0.0",
"typescript": "^4.3.5",
"prettier": "^2.6.2"
} }
} }

172
tests/bets-comprehensive.ts Normal file
View File

@ -0,0 +1,172 @@
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { Bets } from "../target/types/bets";
import { PublicKey, Keypair, LAMPORTS_PER_SOL, SystemProgram } from "@solana/web3.js";
import { expect } from "chai";
import { BN } from "bn.js";
describe("bets", () => {
// Configure the client to use the local cluster
const provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);
const program = anchor.workspace.Bets as Program<Bets>;
const payer = Keypair.generate();
const joiner = Keypair.generate();
let betsListPda: PublicKey;
let betVaultPda: PublicKey;
const gameId = "GAME123";
const userId = "USER123";
const joinerId = "JOINER123";
const wager = new BN(1 * LAMPORTS_PER_SOL);
const nonce = new BN(1);
const FEE_COLLECTOR = new PublicKey("cocD4r4yNpHxPq7CzUebxEMyLki3X4d2Y3HcTX5ptUc");
before(async () => {
// Airdrop SOL to payer and joiner for testing
const signature1 = await provider.connection.requestAirdrop(
payer.publicKey,
2 * LAMPORTS_PER_SOL
);
await provider.connection.confirmTransaction(signature1);
const signature2 = await provider.connection.requestAirdrop(
joiner.publicKey,
2 * LAMPORTS_PER_SOL
);
await provider.connection.confirmTransaction(signature2);
// Find PDA for bets list
[betsListPda] = PublicKey.findProgramAddressSync(
[Buffer.from("bets_list")],
program.programId
);
// Find PDA for bet vault
[betVaultPda] = PublicKey.findProgramAddressSync(
[
Buffer.from("bet_vault"),
payer.publicKey.toBuffer(),
Buffer.from(gameId),
nonce.toArrayLike(Buffer, "le", 8)
],
program.programId
);
});
it("Can initialize bets list", async () => {
try {
await program.methods
.initialize()
.accounts({
payer: provider.wallet.publicKey
})
.rpc();
} catch (error) {
// Ignore the error if account already exists
if (!error.toString().includes("already in use")) {
throw error;
}
}
// Verify the bets list exists
const betsListAccount = await program.account.betsList.fetch(betsListPda);
expect(betsListAccount.bets).to.be.an('array');
});
it("Can create a new bet", async () => {
await program.methods
.createBet(wager, userId, gameId, nonce)
.accounts({
betsList: betsListPda,
payer: payer.publicKey
})
.signers([payer])
.rpc();
// Verify the bet was created correctly
const betVaultAccount = await program.account.betVault.fetch(betVaultPda);
expect(betVaultAccount.gameId).to.equal(gameId);
expect(betVaultAccount.owner.toString()).to.equal(payer.publicKey.toString());
expect(betVaultAccount.wager.toString()).to.equal(wager.toString());
});
it("Can join a bet", async () => {
await program.methods
.joinBet(joinerId, gameId)
.accounts({
betVault: betVaultPda,
payer: joiner.publicKey
})
.signers([joiner])
.rpc();
// Verify the joiner was added to the bet
const betVaultAccount = await program.account.betVault.fetch(betVaultPda);
expect(betVaultAccount.joiner.toString()).to.equal(joiner.publicKey.toString());
});
it("Can close a bet", async () => {
await program.methods
.closeBet(payer.publicKey)
.accounts({
betsList: betsListPda,
betVault: betVaultPda,
winner: payer.publicKey,
feeWallet: FEE_COLLECTOR,
payer: payer.publicKey
})
.signers([payer])
.rpc();
// Verify the bet was closed by checking if it still exists
try {
await program.account.betVault.fetch(betVaultPda);
expect.fail("Bet should have been closed");
} catch (error) {
expect(error).to.be.an('Error');
}
});
// it("Can refund a bet", async () => {
// // First create a new bet for testing refund
// const newNonce = new BN(2);
// const [newBetVaultPda] = PublicKey.findProgramAddressSync(
// [
// Buffer.from("bet_vault"),
// payer.publicKey.toBuffer(),
// Buffer.from(gameId),
// newNonce.toArrayLike(Buffer, "le", 8)
// ],
// program.programId
// );
// await program.methods
// .createBet(wager, userId, gameId, newNonce)
// .accounts({
// payer: payer.publicKey,
// betsList: betsListPda
// })
// .signers([payer])
// .rpc();
// // Now refund the bet
// const tx = await program.methods
// .refundBet(payer.publicKey)
// .accounts({
// betVault: newBetVaultPda,
// betsList: betsListPda,
// payer: payer.publicKey
// })
// .signers([payer])
// .rpc();
// // Verify the bet was refunded by checking if it still exists
// try {
// await program.account.betVault.fetch(newBetVaultPda);
// expect.fail("Bet should have been refunded and closed");
// } catch (error) {
// expect(error).to.be.an('Error');
// }
// });
});

View File

@ -97,10 +97,12 @@
dependencies: dependencies:
"@types/node" "*" "@types/node" "*"
"@types/chai@^4.3.0": "@types/chai@^5.2.1":
version "4.3.20" version "5.2.1"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.3.20.tgz#cb291577ed342ca92600430841a00329ba05cecc" resolved "https://registry.yarnpkg.com/@types/chai/-/chai-5.2.1.tgz#85687a58b27eac736ec0e87e5cb98f21e57a0bb1"
integrity sha512-/pC9HAB5I/xMlc5FP77qjCnI16ChlJfW0tGa0IUcFn38VJrTV6DeZ60NU5KZBtaOZqjdpwTWohz5HU1RrhiYxQ== integrity sha512-iu1JLYmGmITRzUgNiLMZD3WCoFzpYtueuyAgHTXqgwSRAMIlFTnZqG6/xenkpUGRJEzSfklUTI4GNSzks/dc0w==
dependencies:
"@types/deep-eql" "*"
"@types/connect@^3.4.33": "@types/connect@^3.4.33":
version "3.4.38" version "3.4.38"
@ -109,15 +111,20 @@
dependencies: dependencies:
"@types/node" "*" "@types/node" "*"
"@types/deep-eql@*":
version "4.0.2"
resolved "https://registry.yarnpkg.com/@types/deep-eql/-/deep-eql-4.0.2.tgz#334311971d3a07121e7eb91b684a605e7eea9cbd"
integrity sha512-c9h9dVVMigMPc4bwTvC5dxqtqJZwQPePsWjPlpSOnojbor6pGqdk541lfA7AqFQr5pB1BRdq0juY9db81BwyFw==
"@types/json5@^0.0.29": "@types/json5@^0.0.29":
version "0.0.29" version "0.0.29"
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ== integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==
"@types/mocha@^9.0.0": "@types/mocha@^10.0.10":
version "9.1.1" version "10.0.10"
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-9.1.1.tgz#e7c4f1001eefa4b8afbd1eee27a237fee3bf29c4" resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-10.0.10.tgz#91f62905e8d23cbd66225312f239454a23bebfa0"
integrity sha512-Z61JK7DKDtdKTWwLeElSEBcWGRLY8g95ic5FoQqI9CMx0ns/Ghep3B4DfcEimiKMvtamNVULVNKEsiwV3aQmXw== integrity sha512-xPyYSz1cMPnJQhl0CLMH68j3gprKZaTjG3s5Vi+fDgx+uhG9NOXwbVt52eFS8ECyXhyKcjDLCBEqBExKuiZb7Q==
"@types/node@*": "@types/node@*":
version "22.13.14" version "22.13.14"
@ -131,6 +138,13 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.20.55.tgz#c329cbd434c42164f846b909bd6f85b5537f6240" resolved "https://registry.yarnpkg.com/@types/node/-/node-12.20.55.tgz#c329cbd434c42164f846b909bd6f85b5537f6240"
integrity sha512-J8xLz7q2OFulZ2cyGTLE1TbbZcjpno7FaN6zdJNrgAdrJ+DZzh/uFR6YrTb4C+nXakvud8Q4+rbhoIWlYQbUFQ== integrity sha512-J8xLz7q2OFulZ2cyGTLE1TbbZcjpno7FaN6zdJNrgAdrJ+DZzh/uFR6YrTb4C+nXakvud8Q4+rbhoIWlYQbUFQ==
"@types/node@^22.14.0":
version "22.14.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-22.14.0.tgz#d3bfa3936fef0dbacd79ea3eb17d521c628bb47e"
integrity sha512-Kmpl+z84ILoG+3T/zQFyAJsU6EPTmOCj8/2+83fSN6djd6I4o7uOuGIH6vq3PrjY5BGitSbFuMN18j3iknubbA==
dependencies:
undici-types "~6.21.0"
"@types/uuid@^8.3.4": "@types/uuid@^8.3.4":
version "8.3.4" version "8.3.4"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.4.tgz#bd86a43617df0594787d38b735f55c805becf1bc" resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.4.tgz#bd86a43617df0594787d38b735f55c805becf1bc"
@ -1071,6 +1085,11 @@ undici-types@~6.20.0:
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.20.0.tgz#8171bf22c1f588d1554d55bf204bc624af388433" resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.20.0.tgz#8171bf22c1f588d1554d55bf204bc624af388433"
integrity sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg== integrity sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg==
undici-types@~6.21.0:
version "6.21.0"
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.21.0.tgz#691d00af3909be93a7faa13be61b3a5b50ef12cb"
integrity sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==
utf-8-validate@^5.0.2: utf-8-validate@^5.0.2:
version "5.0.10" version "5.0.10"
resolved "https://registry.yarnpkg.com/utf-8-validate/-/utf-8-validate-5.0.10.tgz#d7d10ea39318171ca982718b6b96a8d2442571a2" resolved "https://registry.yarnpkg.com/utf-8-validate/-/utf-8-validate-5.0.10.tgz#d7d10ea39318171ca982718b6b96a8d2442571a2"