diff --git a/wk-6 Testing/hardhat_test/contracts/Adashe.sol b/wk-6 Testing/hardhat_test/contracts/Adashe.sol index fe83797..33d817a 100644 --- a/wk-6 Testing/hardhat_test/contracts/Adashe.sol +++ b/wk-6 Testing/hardhat_test/contracts/Adashe.sol @@ -62,19 +62,14 @@ contract Adashe { } modifier adasheIsNotFull() { - // if (noOfPeople >= maxPeople) revert AdasheFull(); - require(noOfPeople < maxPeople, "AdasheFull"); + if (noOfPeople >= maxPeople) revert AdasheFull(); _; } /// @notice Register for the savings circle. Turn order follows registration order. function registerForAdashe(string calldata _name) public adasheIsNotFull returns (uint256 turn_) { - // if (isRegistered[msg.sender]) revert AlreadyRegistered(); - // if (bytes(_name).length == 0) revert EmptyName(); - - - require(!isRegistered[msg.sender], "AlreadyRegistered"); - require(bytes(_name).length > 0, "EmptyName"); + if (isRegistered[msg.sender]) revert AlreadyRegistered(); + if (bytes(_name).length == 0) revert EmptyName(); noOfPeople++; uint8 personId = noOfPeople; @@ -101,9 +96,9 @@ contract Adashe { /// @notice Pay your contribution for the current round. function contribute() external payable { - if (status != Packed.PACKED) revert AdasheNotPacked(); + if (status != Packed.PACKED) revert AdasheNotPacked(); // must be PACKED: registering the last member calls _packAdashe(), which starts the round if (!isRegistered[msg.sender]) revert NotRegistered(); - if (block.timestamp > roundStartTime + duration) revert RoundExpired(); + if (block.timestamp > roundStartTime + duration) revert RoundExpired(); // roundStartTime is set at pack time, so the deadline is (packTime + duration) uint8 personId = memberId[msg.sender]; Person storage person = adashePeople[personId]; diff --git a/wk-6 Testing/hardhat_test/test/Adashe.ts b/wk-6 Testing/hardhat_test/test/Adashe.ts index 285e82d..ad3ba07 100644 --- a/wk-6 Testing/hardhat_test/test/Adashe.ts +++ b/wk-6 Testing/hardhat_test/test/Adashe.ts @@ -43,7 +43,7 @@ describe("Deployment Successful", function () { }); }); -describe.only("Registering a new member", function () { +describe("Registering a new member", function () { it("Should register a new member", async function () { await adashe.connect(addr1).registerForAdashe("John Doe"); expect(await adashe.noOfPeople()).to.equal(1); @@ -61,19 +61,98 @@ describe.only("Registering a new member", function () { it("Should revert when the same address registers again", async function () { await adashe.connect(addr1).registerForAdashe("John Doe"); - await expect(adashe.connect(addr1).registerForAdashe("John Doe")).to.be.revertedWith("AlreadyRegistered"); + await expect(adashe.connect(addr1).registerForAdashe("John Doe")).to.be.revertedWithCustomError(adashe, "AlreadyRegistered"); }); it("Should revert when the name is empty", async function () { - await expect(adashe.connect(addr1).registerForAdashe("")).to.be.revertedWith("EmptyName"); + await expect(adashe.connect(addr1).registerForAdashe("")).to.be.revertedWithCustomError(adashe, "EmptyName"); }); it("Should revert when the number of people is full", async function () { await adashe.connect(addr1).registerForAdashe("John Doe"); await adashe.connect(addr2).registerForAdashe("Jane Doe"); await adashe.connect(addr3).registerForAdashe("Jim Doe"); - await expect(adashe.connect(addr4).registerForAdashe("Jack Doe")).to.be.revertedWith("AdasheFull"); + await expect(adashe.connect(addr4).registerForAdashe("Jack Doe")).to.be.revertedWithCustomError(adashe, "AdasheFull"); }); + + }); - + +// Tests for the contribute() function. +// Note: maxPeople = 3, amountPerHead = 100. The circle only becomes PACKED +// (contributions allowed) after the 3rd member registers — that's when +// registerForAdashe() auto-calls _packAdashe(). +describe("A Member Paying Contribution", function () { + + it("Should revert when contributing before the circle is packed", async function () { + await adashe.connect(addr1).registerForAdashe("John Doe"); + await expect( + adashe.connect(addr1).contribute({ value: amountPerHead }) + ).to.be.revertedWithCustomError(adashe, "AdasheNotPacked"); + }); + + it("Should revert when a non-member tries to contribute", async function () { + await adashe.connect(addr1).registerForAdashe("John Doe"); + await adashe.connect(addr2).registerForAdashe("Jane Doe"); + await adashe.connect(addr3).registerForAdashe("Jim Doe"); + await expect( + adashe.connect(addr4).contribute({ value: amountPerHead }) + ).to.be.revertedWithCustomError(adashe, "NotRegistered"); + }); + + it("Should revert when the wrong amount is sent", async function () { + await adashe.connect(addr1).registerForAdashe("John Doe"); + await adashe.connect(addr2).registerForAdashe("Jane Doe"); + await adashe.connect(addr3).registerForAdashe("Jim Doe"); + await expect( + adashe.connect(addr1).contribute({ value: amountPerHead + 1 }) + ).to.be.revertedWithCustomError(adashe, "InvalidAmount"); + }); + + it("Should allow a registered member to contribute and mark them as paid", async function () { + await adashe.connect(addr1).registerForAdashe("John Doe"); + await adashe.connect(addr2).registerForAdashe("Jane Doe"); + await adashe.connect(addr3).registerForAdashe("Jim Doe"); + // Only addr1 pays. If ALL three paid, _distributePayout() resets + // everyone's hasPaid back to false, so we check after a single payment. + await adashe.connect(addr1).contribute({ value: amountPerHead }); + + const member1 = await adashe.getAdasheMember(1); + expect(member1.hasPaid).to.equal(true); + }); + + it("Should revert when a member contributes twice in the same round", async function () { + await adashe.connect(addr1).registerForAdashe("John Doe"); + await adashe.connect(addr2).registerForAdashe("Jane Doe"); + await adashe.connect(addr3).registerForAdashe("Jim Doe"); + await adashe.connect(addr1).contribute({ value: amountPerHead }); + await expect( + adashe.connect(addr1).contribute({ value: amountPerHead }) + ).to.be.revertedWithCustomError(adashe, "AlreadyPaid"); + }); + +}); + +// Tests for getAdasheMember() +describe("Reading a member with getAdasheMember", function () { + it("Should return the stored member data by id", async function () { + await adashe.connect(addr1).registerForAdashe("John Doe"); + + const member1 = await adashe.getAdasheMember(1); + expect(member1.name).to.equal("John Doe"); + expect(member1.addr).to.equal(addr1.address); + expect(member1.turn).to.equal(1); + expect(member1.hasPaid).to.equal(false); + }); + + it("Should revert for an out-of-range id", async function () { + // No one registered yet, so id 1 is out of range (noOfPeople = 0). + await expect(adashe.getAdasheMember(1)).to.be.revertedWithCustomError(adashe, "InvalidPersonId"); + }); + + it("Should revert for id 0", async function () { + await expect(adashe.getAdasheMember(0)).to.be.revertedWithCustomError(adashe, "InvalidPersonId"); + }); +}); + });