From 3cce1e16c4d148fbdeae3eb7f9dc83ef17f95f29 Mon Sep 17 00:00:00 2001 From: "Jason Schrader (aider)" Date: Tue, 24 Dec 2024 15:32:06 -0700 Subject: [PATCH] test: Add comprehensive tests for conclude-proposal() function --- .../extensions/aibtc-ext001-actions.test.ts | 186 +++++++++++++++++- 1 file changed, 180 insertions(+), 6 deletions(-) diff --git a/tests/dao/extensions/aibtc-ext001-actions.test.ts b/tests/dao/extensions/aibtc-ext001-actions.test.ts index bbbf546..bd9de96 100644 --- a/tests/dao/extensions/aibtc-ext001-actions.test.ts +++ b/tests/dao/extensions/aibtc-ext001-actions.test.ts @@ -502,11 +502,185 @@ describe("aibtc-ext001-actions", () => { // Conclusion Tests describe("conclude-proposal()", () => { - it("fails if contract not initialized"); - it("fails if treasury mismatches"); - it("fails if proposal still active"); - it("fails if proposal already concluded"); - it("succeeds and executes if passed"); - it("succeeds without executing if failed"); + it("fails if contract not initialized", () => { + const receipt = simnet.callPublicFn( + contractAddress, + "conclude-proposal", + [ + Cl.uint(1), + Cl.contractPrincipal(addressDeployer, "test-treasury"), + Cl.contractPrincipal(addressDeployer, "test-token") + ], + address1 + ); + expect(receipt.result).toBeErr(Cl.uint(ErrCode.ERR_NOT_INITIALIZED)); + }); + + it("fails if treasury mismatches", () => { + // First set the treasury and token + simnet.callPublicFn( + contractAddress, + "set-protocol-treasury", + [Cl.contractPrincipal(addressDeployer, "test-treasury")], + addressDeployer + ); + + simnet.callPublicFn( + contractAddress, + "set-voting-token", + [Cl.contractPrincipal(addressDeployer, "test-token")], + addressDeployer + ); + + const receipt = simnet.callPublicFn( + contractAddress, + "conclude-proposal", + [ + Cl.uint(1), + Cl.contractPrincipal(addressDeployer, "wrong-treasury"), + Cl.contractPrincipal(addressDeployer, "test-token") + ], + address1 + ); + expect(receipt.result).toBeErr(Cl.uint(ErrCode.ERR_TREASURY_MISMATCH)); + }); + + it("fails if proposal still active", () => { + // Create a new proposal + simnet.callPublicFn( + contractAddress, + "propose-action", + [ + Cl.stringAscii("send-message"), + Cl.list([Cl.stringUtf8("Hello World")]), + Cl.contractPrincipal(addressDeployer, "test-token") + ], + address1 + ); + + const receipt = simnet.callPublicFn( + contractAddress, + "conclude-proposal", + [ + Cl.uint(3), + Cl.contractPrincipal(addressDeployer, "test-treasury"), + Cl.contractPrincipal(addressDeployer, "test-token") + ], + address1 + ); + expect(receipt.result).toBeErr(Cl.uint(ErrCode.ERR_PROPOSAL_STILL_ACTIVE)); + }); + + it("fails if proposal already concluded", () => { + // Mine blocks to end voting period + simnet.mineEmptyBlocks(144); + + // Conclude the proposal first time + simnet.callPublicFn( + contractAddress, + "conclude-proposal", + [ + Cl.uint(3), + Cl.contractPrincipal(addressDeployer, "test-treasury"), + Cl.contractPrincipal(addressDeployer, "test-token") + ], + address1 + ); + + // Try to conclude again + const receipt = simnet.callPublicFn( + contractAddress, + "conclude-proposal", + [ + Cl.uint(3), + Cl.contractPrincipal(addressDeployer, "test-treasury"), + Cl.contractPrincipal(addressDeployer, "test-token") + ], + address1 + ); + expect(receipt.result).toBeErr(Cl.uint(ErrCode.ERR_PROPOSAL_ALREADY_CONCLUDED)); + }); + + it("succeeds and executes if passed", () => { + // Create a new proposal + simnet.callPublicFn( + contractAddress, + "propose-action", + [ + Cl.stringAscii("send-message"), + Cl.list([Cl.stringUtf8("Hello World")]), + Cl.contractPrincipal(addressDeployer, "test-token") + ], + address1 + ); + + // Vote in favor with enough tokens to pass + simnet.callPublicFn( + contractAddress, + "vote-on-proposal", + [ + Cl.uint(4), + Cl.contractPrincipal(addressDeployer, "test-token"), + Cl.bool(true) + ], + address1 + ); + + // Mine blocks to end voting period + simnet.mineEmptyBlocks(144); + + const receipt = simnet.callPublicFn( + contractAddress, + "conclude-proposal", + [ + Cl.uint(4), + Cl.contractPrincipal(addressDeployer, "test-treasury"), + Cl.contractPrincipal(addressDeployer, "test-token") + ], + address1 + ); + expect(receipt.result).toBeOk(Cl.bool(true)); + }); + + it("succeeds without executing if failed", () => { + // Create a new proposal + simnet.callPublicFn( + contractAddress, + "propose-action", + [ + Cl.stringAscii("send-message"), + Cl.list([Cl.stringUtf8("Hello World")]), + Cl.contractPrincipal(addressDeployer, "test-token") + ], + address1 + ); + + // Vote against with enough tokens to fail + simnet.callPublicFn( + contractAddress, + "vote-on-proposal", + [ + Cl.uint(5), + Cl.contractPrincipal(addressDeployer, "test-token"), + Cl.bool(false) + ], + address1 + ); + + // Mine blocks to end voting period + simnet.mineEmptyBlocks(144); + + const receipt = simnet.callPublicFn( + contractAddress, + "conclude-proposal", + [ + Cl.uint(5), + Cl.contractPrincipal(addressDeployer, "test-treasury"), + Cl.contractPrincipal(addressDeployer, "test-token") + ], + address1 + ); + expect(receipt.result).toBeOk(Cl.bool(false)); + }); }); });