Skip to content

updated bankrun test for voting program #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 89 additions & 25 deletions project-2-voting/anchor/tests/bankrun.spec.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,106 @@
import { startAnchor } from "solana-bankrun";
import { BankrunProvider } from "anchor-bankrun";
import { PublicKey } from '@solana/web3.js';
import * as anchor from '@coral-xyz/anchor';
import { BN, Program } from "@coral-xyz/anchor";

import * as anchor from "@coral-xyz/anchor";
import { Keypair, PublicKey } from "@solana/web3.js";
import { BankrunProvider, startAnchor } from "anchor-bankrun";
import { Voting } from "../target/types/voting";

const IDL = require("../target/idl/voting.json");
import { Voting } from '../target/types/voting';

const PUPPET_PROGRAM_ID = new PublicKey("5s3PtT8kLYCv1WEp6dSh3T7EuF35Z6jSu5Cvx4hWG79H");
const PROGRAM_ID = new PublicKey(IDL.address);

describe('Create a system account', () => {
describe("Voting", () => {
let context;
let provider;
let votingProgram: anchor.Program<Voting>;

test("bankrun", async () => {
const context = await startAnchor("", [{name: "voting", programId: PUPPET_PROGRAM_ID}], []);
const provider = new BankrunProvider(context);

const puppetProgram = new Program<Voting>(
beforeAll(async () => {
context = await startAnchor('', [{ name: "voting", programId: PROGRAM_ID }], []);
provider = new BankrunProvider(context);
votingProgram = new anchor.Program<Voting>(
IDL,
provider,
);
});

it("initializes a poll", async () => {
await votingProgram.methods.initializePoll(
new anchor.BN(1),
"What is your favorite color?",
new anchor.BN(100),
new anchor.BN(1739370789),
).rpc();

const [pollAddress] = PublicKey.findProgramAddressSync(
[Buffer.from("poll"), new anchor.BN(1).toArrayLike(Buffer, "le", 8)],
puppetProgram.programId
[new anchor.BN(1).toArrayLike(Buffer, "le", 8)],
votingProgram.programId,
);

await puppetProgram.methods.initializePoll(
const poll = await votingProgram.account.poll.fetch(pollAddress);

console.log(poll);

expect(poll.pollId.toNumber()).toBe(1);
expect(poll.description).toBe("What is your favorite color?");
expect(poll.pollStart.toNumber()).toBe(100);
});

it("initializes candidates", async () => {
await votingProgram.methods.initializeCandidate(
"Pink",
new anchor.BN(1),
).rpc();
await votingProgram.methods.initializeCandidate(
"Blue",
new anchor.BN(1),
new anchor.BN(0),
new anchor.BN(1759508293),
"test-poll",
"description",
).rpc();

const pollAccount = await puppetProgram.account.pollAccount.fetch(pollAddress);
console.log(pollAccount);
const [pinkAddress] = PublicKey.findProgramAddressSync(
[new anchor.BN(1).toArrayLike(Buffer, "le", 8), Buffer.from("Pink")],
votingProgram.programId,
);
const pinkCandidate = await votingProgram.account.candidate.fetch(pinkAddress);
console.log(pinkCandidate);
expect(pinkCandidate.candidateVotes.toNumber()).toBe(0);
expect(pinkCandidate.candidateName).toBe("Pink");

const [blueAddress] = PublicKey.findProgramAddressSync(
[new anchor.BN(1).toArrayLike(Buffer, "le", 8), Buffer.from("Blue")],
votingProgram.programId,
);
const blueCandidate = await votingProgram.account.candidate.fetch(blueAddress);
console.log(blueCandidate);
expect(blueCandidate.candidateVotes.toNumber()).toBe(0);
expect(blueCandidate.candidateName).toBe("Blue");
});

});
it("vote candidates", async () => {
await votingProgram.methods.vote(
"Pink",
new anchor.BN(1),
).rpc();
await votingProgram.methods.vote(
"Blue",
new anchor.BN(1),
).rpc();
await votingProgram.methods.vote(
"Pink",
new anchor.BN(1),
).rpc();

const [pinkAddress] = PublicKey.findProgramAddressSync(
[new anchor.BN(1).toArrayLike(Buffer, "le", 8), Buffer.from("Pink")],
votingProgram.programId,
);
const pinkCandidate = await votingProgram.account.candidate.fetch(pinkAddress);
console.log(pinkCandidate);
expect(pinkCandidate.candidateVotes.toNumber()).toBe(2);
expect(pinkCandidate.candidateName).toBe("Pink");

const [blueAddress] = PublicKey.findProgramAddressSync(
[new anchor.BN(1).toArrayLike(Buffer, "le", 8), Buffer.from("Blue")],
votingProgram.programId,
);
const blueCandidate = await votingProgram.account.candidate.fetch(blueAddress);
console.log(blueCandidate);
expect(blueCandidate.candidateVotes.toNumber()).toBe(1);
expect(blueCandidate.candidateName).toBe("Blue");
});
});