describe("Fund Create Function Tests", () => {
it("1- If GOAL IS NOT SET, WILL NOT REVERT!", async () => {
const invalidGoal = new anchor.BN(0);
try {
[fundPDA] = await PublicKey.findProgramAddress(
[Buffer.from(fundName), creator.publicKey.toBuffer()],
program.programId
);
await program.methods
.fundCreate(fundName, description, invalidGoal)
.accounts({
fund: fundPDA,
creator: creator.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
})
.rpc();
const fund = await program.account.fund.fetch(fundPDA);
console.log("\n✅ Fund Created with Zero Goal:");
console.log(` - Fund Name: ${fund.name}`);
console.log(` - Fund Goal: ${fund.goal}`);
console.log(" - Expected Behavior: Fund creation did not revert with zero goal.");
} catch (err) {
console.error("\n❌ Unexpected Error: Fund creation reverted with zero goal.");
throw err;
}
});
it("2- Empty NAME WILL NOT REVERT", async () => {
const emptyName = "";
try {
[fundPDA] = await PublicKey.findProgramAddress(
[Buffer.from(emptyName), creator.publicKey.toBuffer()],
program.programId
);
await program.methods
.fundCreate(emptyName, description, goal)
.accounts({
fund: fundPDA,
creator: creator.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
})
.rpc();
const fund = await program.account.fund.fetch(fundPDA);
console.log("\n✅ Fund Created with Empty Name:");
console.log(` - Fund Name: '${fund.name}'`);
console.log(" - Expected Behavior: Fund creation did not revert with empty name.");
} catch (err) {
console.error("\n❌ Unexpected Error: Fund creation reverted with empty name.");
throw err;
}
});
it("3- Empty DESCRIPTION WILL NOT REVERT", async () => {
const emptyDescription = "";
try {
[fundPDA] = await PublicKey.findProgramAddress(
[Buffer.from(fundName), creator.publicKey.toBuffer()],
program.programId
);
await program.methods
.fundCreate(fundName, emptyDescription, goal)
.accounts({
fund: fundPDA,
creator: creator.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
})
.rpc();
const fund = await program.account.fund.fetch(fundPDA);
console.log("\n✅ Fund Created with Empty Description:");
console.log(` - Fund Description: '${fund.description}'`);
console.log(" - Expected Behavior: Fund creation did not revert with empty description.");
} catch (err) {
console.error("\n❌ Unexpected Error: Fund creation reverted with empty description.");
throw err;
}
});
it("4- DESCRIPTION > 5000 WILL REVERT BECAUSE OF RANGE ERROR", async () => {
const longDescription = "a".repeat(5001);
try {
[fundPDA] = await PublicKey.findProgramAddress(
[Buffer.from(fundName), creator.publicKey.toBuffer()],
program.programId
);
await program.methods
.fundCreate(fundName, longDescription, goal)
.accounts({
fund: fundPDA,
creator: creator.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
})
.rpc();
console.error("\n❌ Unexpected Behavior: Fund creation succeeded with a description longer than 5000 characters.");
} catch (err) {
console.log("\n✅ Fund Creation Reverted as Expected:");
console.log(" - Reason: Description exceeds maximum allowed length (5000 characters).");
expect(err.toString()).to.include("encoding overruns Buffer");
}
});
it("5- NAME + CREATOR ADDRESS = SEED > 32 BYTES WILL REVERT WITH EXCEED MAX SEED", async () => {
const longName = "a".repeat(33);
try {
[fundPDA] = await PublicKey.findProgramAddress(
[Buffer.from(longName), creator.publicKey.toBuffer()],
program.programId
);
await program.methods
.fundCreate(longName, description, goal)
.accounts({
fund: fundPDA,
creator: creator.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
})
.rpc();
console.error("\n❌ Unexpected Behavior: Fund creation succeeded with a name exceeding 32 bytes.");
} catch (err) {
console.log("\n✅ Fund Creation Reverted as Expected:");
console.log(" - Reason: Name + creator address exceeds maximum seed length (32 bytes).");
expect(err.toString()).to.include("Max seed length exceeded");
}
});
it("6- Duplicate FundCrowd WILL REVERT (Same Name and Creator Address) PDA UNIQUENESS", async () => {
try {
[fundPDA] = await PublicKey.findProgramAddress(
[Buffer.from(fundName), creator.publicKey.toBuffer()],
program.programId
);
await program.methods
.fundCreate(fundName, description, goal)
.accounts({
fund: fundPDA,
creator: creator.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
})
.rpc();
console.log("\n✅ First Fund Created Successfully.");
await program.methods
.fundCreate(fundName, description, goal)
.accounts({
fund: fundPDA,
creator: creator.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
})
.rpc();
console.error("\n❌ Unexpected Behavior: Duplicate fund creation succeeded.");
} catch (err) {
console.log("\n✅ Duplicate Fund Creation Reverted as Expected:");
console.log(" - Reason: Duplicate fund creation is prevented by PDA uniqueness.");
}
});
});
rustfund
Fund Create Function Tests
✅ Fund Created with Zero Goal:
- Fund Name: firstflight FundA
- Fund Goal: 0
- Expected Behavior: Fund creation did not revert with zero goal.
✔ 1- If GOAL IS NOT SET, WILL NOT REVERT! (422ms)
✅ Fund Created with Empty Name:
- Fund Name: ''
- Expected Behavior: Fund creation did not revert with empty name.
✔ 2- Empty NAME WILL NOT REVERT (335ms)
✅ Fund Created with Empty Description:
- Fund Description: ''
- Expected Behavior: Fund creation did not revert with empty description.
✔ 3- Empty DESCRIPTION WILL NOT REVERT (372ms)
✅ Fund Creation Reverted as Expected:
- Reason: Description exceeds maximum allowed length (5000 characters).
✔ 4- DESCRIPTION > 5000 WILL REVERT BECAUSE OF RANGE ERROR
✅ Fund Creation Reverted as Expected:
- Reason: Name + creator address exceeds maximum seed length (32 bytes).
✔ 5- NAME + CREATOR ADDRESS = SEED > 32 BYTES WILL REVERT WITH EXCEED MAX SEED
✅ Duplicate Fund Creation Reverted as Expected:
- Reason: Duplicate fund creation is prevented by PDA uniqueness.
✔ 6- Duplicate FundCrowd WILL REVERT (Same Name and Creator Address) PDA UNIQUENESS