From ec67ec65f8dfd70fb7e73f3f23852d3895fb02a0 Mon Sep 17 00:00:00 2001 From: Adam Gall Date: Fri, 15 Nov 2024 13:19:45 -0500 Subject: [PATCH] Add tests --- .../DecentAutonomousAdminV1.sol | 5 +- .../DecentAutonomousAdminV1.test.ts | 100 ++++++++++++------ 2 files changed, 69 insertions(+), 36 deletions(-) diff --git a/contracts/autonomous-admin/DecentAutonomousAdminV1.sol b/contracts/autonomous-admin/DecentAutonomousAdminV1.sol index 1915b79..25543b7 100644 --- a/contracts/autonomous-admin/DecentAutonomousAdminV1.sol +++ b/contracts/autonomous-admin/DecentAutonomousAdminV1.sol @@ -27,9 +27,10 @@ contract DecentAutonomousAdminV1 is hatsElectionModule.startNextTerm(); - // This will burn the hat since wearer is no longer eligible + // This will burn the hat if wearer is no longer eligible args.hatsProtocol.checkHatWearerStatus(args.hatId, args.currentWearer); - // This will mint the hat to the nominated wearer + + // This will mint the hat to the nominated wearer, if necessary if (args.nominatedWearer != args.currentWearer) { args.hatsProtocol.mintHat(args.hatId, args.nominatedWearer); } diff --git a/test/autonomous-admin/DecentAutonomousAdminV1.test.ts b/test/autonomous-admin/DecentAutonomousAdminV1.test.ts index 7f67ec4..b629a10 100644 --- a/test/autonomous-admin/DecentAutonomousAdminV1.test.ts +++ b/test/autonomous-admin/DecentAutonomousAdminV1.test.ts @@ -48,7 +48,7 @@ describe('DecentAutonomousAdminHatV1', function () { await hatsProtocol.createHat( topHatId, // top hat id 'Details', // Hat details - 100, // Max supply + 1, // Max supply '0x0000000000000000000000000000000000004a75', // Eligibility module (none) '0x0000000000000000000000000000000000004a75', // Toggle module (none) true, // Is mutable @@ -86,55 +86,87 @@ describe('DecentAutonomousAdminHatV1', function () { // Mint the role hat to currentWearer await hatsProtocol.mintHat(roleHatId, await firstWearer.getAddress()); - - // set up the next election - nextTermEnd = firstTermEnd + 100; - await hatsElectionModule.setNextTerm(nextTermEnd); - await hatsElectionModule.elect(nextTermEnd, [await secondWearer.getAddress()]); }); describe('triggerStartNextTerm', function () { - describe('before the first term is over', function () { - it('should have correct wearers', async () => { - expect(await hatsProtocol.isWearerOfHat(firstWearer.address, roleHatId)).to.equal(true); - expect(await hatsProtocol.isWearerOfHat(secondWearer.address, roleHatId)).to.equal(false); + describe('when the new wearer is different from the old wearer', function () { + beforeEach(async () => { + // set up the next election + nextTermEnd = firstTermEnd + 100; + await hatsElectionModule.setNextTerm(nextTermEnd); + await hatsElectionModule.elect(nextTermEnd, [await secondWearer.getAddress()]); }); - }); - describe('after the first term is over', function () { - beforeEach(async () => { - // Wait until the first term is over - await setTime(firstTermEnd + 1); + describe('before the first term is over', function () { + it('should have correct wearers', async () => { + expect(await hatsProtocol.isWearerOfHat(firstWearer.address, roleHatId)).to.equal(true); + expect(await hatsProtocol.isWearerOfHat(secondWearer.address, roleHatId)).to.equal(false); + }); }); - describe('with a valid current wearer', function () { + describe('after the first term is over', function () { beforeEach(async () => { - await decentAutonomousAdminInstance.triggerStartNextTerm({ - currentWearer: await firstWearer.getAddress(), - nominatedWearer: secondWearer.address, - hatsProtocol: await hatsProtocol.getAddress(), - hatId: roleHatId, + // Wait until the first term is over + await setTime(firstTermEnd + 1); + }); + + describe('with a valid current wearer', function () { + beforeEach(async () => { + await decentAutonomousAdminInstance.triggerStartNextTerm({ + currentWearer: await firstWearer.getAddress(), + nominatedWearer: secondWearer.address, + hatsProtocol: await hatsProtocol.getAddress(), + hatId: roleHatId, + }); + }); + + it('should have correct wearers after triggering next term', async () => { + expect(await hatsProtocol.isWearerOfHat(firstWearer.address, roleHatId)).to.equal( + false, + ); + expect(await hatsProtocol.isWearerOfHat(secondWearer.address, roleHatId)).to.equal( + true, + ); }); }); - it('should have correct wearers after triggering next term', async () => { - expect(await hatsProtocol.isWearerOfHat(firstWearer.address, roleHatId)).to.equal(false); - expect(await hatsProtocol.isWearerOfHat(secondWearer.address, roleHatId)).to.equal(true); + describe('with invalid current wearer', function () { + it('should revert if the current wearer is not the wearer of the hat', async () => { + await expect( + decentAutonomousAdminInstance.triggerStartNextTerm({ + currentWearer: await randomUser.getAddress(), + nominatedWearer: secondWearer.address, + hatsProtocol: await hatsProtocol.getAddress(), + hatId: roleHatId, + }), + ).to.be.revertedWithCustomError(hatsProtocol, 'AllHatsWorn'); + }); }); }); + }); - describe('with invalid current wearer', function () { - it('should revert if the current wearer is not the wearer of the hat', async () => { - await expect( - decentAutonomousAdminInstance.triggerStartNextTerm({ - currentWearer: await randomUser.getAddress(), - nominatedWearer: secondWearer.address, - hatsProtocol: await hatsProtocol.getAddress(), - hatId: roleHatId, - }), - ).to.be.revertedWithCustomError(hatsProtocol, 'AllHatsWorn'); + describe('when the new wearer is the same as the old wearer', function () { + beforeEach(async () => { + // set up the next election + nextTermEnd = firstTermEnd + 100; + await hatsElectionModule.setNextTerm(nextTermEnd); + await hatsElectionModule.elect(nextTermEnd, [await firstWearer.getAddress()]); + + // Wait until the first term is over + await setTime(firstTermEnd + 1); + + // trigger the next term + await decentAutonomousAdminInstance.triggerStartNextTerm({ + currentWearer: await firstWearer.getAddress(), + nominatedWearer: firstWearer.address, + hatsProtocol: await hatsProtocol.getAddress(), + hatId: roleHatId, }); }); + + it('should result in original wearer still wearing hat', async () => { + expect(await hatsProtocol.isWearerOfHat(firstWearer.address, roleHatId)).to.equal(true); + }); }); }); });