From cf616d6211b8df813aa799d47edbaae7991ec33c Mon Sep 17 00:00:00 2001 From: tani404 Date: Wed, 18 Mar 2026 15:29:52 +0530 Subject: [PATCH 1/4] test: add failure-case coverage for Chainvoice invoices --- contracts/test/Chainvoice.t.sol | 81 ++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 7 deletions(-) diff --git a/contracts/test/Chainvoice.t.sol b/contracts/test/Chainvoice.t.sol index ecabfd5b..e5822027 100644 --- a/contracts/test/Chainvoice.t.sol +++ b/contracts/test/Chainvoice.t.sol @@ -5,6 +5,7 @@ import {Test} from "forge-std/Test.sol"; import {console} from "forge-std/console.sol"; import "../src/Chainvoice.sol"; + contract ChainvoiceTest is Test { Chainvoice chainvoice; @@ -51,6 +52,7 @@ contract ChainvoiceTest is Test { assertFalse(inv.isCancelled); } + /* ------------------------------------------------------------ */ /* PAY INVOICE */ /* ------------------------------------------------------------ */ @@ -93,24 +95,89 @@ contract ChainvoiceTest is Test { } /* ------------------------------------------------------------ */ - /* FAILURE CASES */ + /* FAILURE CASES - CREATE INVOICE */ /* ------------------------------------------------------------ */ - function testPayInvoice_RevertIfWrongPayer() public { + function testCreateInvoiceRevertIfInvalidAddress() public{ + vm.prank(alice); + vm.expectRevert("Recipient address is zero"); + chainvoice.createInvoice(address(0), 1 ether, address(0), "data", "hash"); + } + + function testCreateInvoiceRevertIfSelfInvoicing() public{ + vm.prank(alice); + vm.expectRevert("Self-invoicing not allowed"); + chainvoice.createInvoice(alice, 1 ether, address(0), "data", "hash"); + } + + /* ------------------------------------------------------------ */ + /* FAILURE CASES - PAY INVOICE */ + /* ------------------------------------------------------------ */ + + modifier createInvoice(){ vm.prank(alice); chainvoice.createInvoice(bob, 1 ether, address(0), "data", "hash"); + _; + } + + function testPayInvoice_RevertIfWrongPayer() public createInvoice { uint256 fee = chainvoice.fee(); vm.expectRevert("Not authorized"); vm.prank(alice); chainvoice.payInvoice{value: 1 ether + fee}(0); } - function testPayInvoice_RevertIfIncorrectValue() public { - vm.prank(alice); - chainvoice.createInvoice(bob, 1 ether, address(0), "data", "hash"); - + function testPayInvoice_RevertIfIncorrectValue() public createInvoice { vm.expectRevert("Incorrect payment amount"); vm.prank(bob); - chainvoice.payInvoice{value: 1 ether}(0); + chainvoice.payInvoice{value: 2 ether}(0); + } + + function testPayInvoice_RevertIfInvalidInvoiceId() public createInvoice{ + uint256 fee = chainvoice.fee(); + + vm.expectRevert("Invalid invoice ID"); + vm.prank(bob); + chainvoice.payInvoice{value: 1 ether + fee}(1); + } + + function testPayInvoice_RevertIfInvoiceAlreadyPaid() public createInvoice{ + uint256 fee = chainvoice.fee(); + + vm.prank(bob); + chainvoice.payInvoice{value: 1 ether + fee}(0); + + vm.expectRevert("Already paid"); + vm.prank(bob); + chainvoice.payInvoice{value: 1 ether + fee}(0); + } + + /* ------------------------------------------------------------ */ + /* FAILURE CASES - CANCEL INVOICE */ + /* ------------------------------------------------------------ */ + + function testCancelInvoiceRevertIfNotTheOwner() public createInvoice{ + vm.expectRevert("Only invoice creator can cancel"); + vm.prank(bob); + chainvoice.cancelInvoice(0); + } + + function testCancelInvoiceRevertIfItIsAlreadyPaid() public createInvoice{ + uint256 fee = chainvoice.fee(); + vm.prank(bob); + chainvoice.payInvoice{value: 1 ether + fee}(0); + + vm.expectRevert("Invoice not cancellable"); + vm.prank(alice); + chainvoice.cancelInvoice(0); + } + + function testCancelInvoiceRevertIfItIsAlreadyCancelled() public createInvoice{ + vm.prank(alice); + chainvoice.cancelInvoice(0); + + vm.expectRevert("Invoice not cancellable"); + vm.prank(alice); + chainvoice.cancelInvoice(0); } } \ No newline at end of file From fcd723fe7d07637f19027606e155cb993b09a89c Mon Sep 17 00:00:00 2001 From: tanisha404 Date: Thu, 19 Mar 2026 12:17:18 +0530 Subject: [PATCH 2/4] Update contracts/test/Chainvoice.t.sol Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- contracts/test/Chainvoice.t.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/test/Chainvoice.t.sol b/contracts/test/Chainvoice.t.sol index e5822027..595edf6e 100644 --- a/contracts/test/Chainvoice.t.sol +++ b/contracts/test/Chainvoice.t.sol @@ -138,7 +138,7 @@ contract ChainvoiceTest is Test { vm.expectRevert("Invalid invoice ID"); vm.prank(bob); - chainvoice.payInvoice{value: 1 ether + fee}(1); + chainvoice.payInvoice{value: 1 ether + fee}(type(uint256).max); } function testPayInvoice_RevertIfInvoiceAlreadyPaid() public createInvoice{ From 60512fff6af6f5513763fbccd21a70311caa2e71 Mon Sep 17 00:00:00 2001 From: tanisha404 Date: Thu, 19 Mar 2026 12:20:34 +0530 Subject: [PATCH 3/4] Fix test to expect revert on incorrect payment amount --- contracts/test/Chainvoice.t.sol | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/contracts/test/Chainvoice.t.sol b/contracts/test/Chainvoice.t.sol index 595edf6e..254da028 100644 --- a/contracts/test/Chainvoice.t.sol +++ b/contracts/test/Chainvoice.t.sol @@ -128,9 +128,10 @@ contract ChainvoiceTest is Test { } function testPayInvoice_RevertIfIncorrectValue() public createInvoice { + uint256 fee = chanvoice.fee(); vm.expectRevert("Incorrect payment amount"); vm.prank(bob); - chainvoice.payInvoice{value: 2 ether}(0); + chainvoice.payInvoice{value: 1 ether + fee + 1}(0); } function testPayInvoice_RevertIfInvalidInvoiceId() public createInvoice{ @@ -180,4 +181,4 @@ contract ChainvoiceTest is Test { vm.prank(alice); chainvoice.cancelInvoice(0); } -} \ No newline at end of file +} From 10b959e09b2a49db81a9808e6cfa38ab28d3bf75 Mon Sep 17 00:00:00 2001 From: tanisha404 Date: Thu, 19 Mar 2026 14:20:25 +0530 Subject: [PATCH 4/4] Fix typo in variable name for fee retrieval --- contracts/test/Chainvoice.t.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/test/Chainvoice.t.sol b/contracts/test/Chainvoice.t.sol index 254da028..d74261f8 100644 --- a/contracts/test/Chainvoice.t.sol +++ b/contracts/test/Chainvoice.t.sol @@ -128,7 +128,7 @@ contract ChainvoiceTest is Test { } function testPayInvoice_RevertIfIncorrectValue() public createInvoice { - uint256 fee = chanvoice.fee(); + uint256 fee = chainvoice.fee(); vm.expectRevert("Incorrect payment amount"); vm.prank(bob); chainvoice.payInvoice{value: 1 ether + fee + 1}(0);