Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgall committed Nov 15, 2024
1 parent 428d175 commit ec67ec6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 36 deletions.
5 changes: 3 additions & 2 deletions contracts/autonomous-admin/DecentAutonomousAdminV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
100 changes: 66 additions & 34 deletions test/autonomous-admin/DecentAutonomousAdminV1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
});
});
});
});

0 comments on commit ec67ec6

Please sign in to comment.