proofchain/packages/contracts/test/compliance/PhytosanitaryCertificate.t.sol
caelum0x fb76a963d9 feat: Wave A — +56 contracts across 8 real-world domains (120 total, 1468 tests)
- tradefinance: LoC, bill of exchange, factoring, PO financing, dynamic discounting,
  securitization + tranche tokens, credit lines, guarantees
- compliance: sanctions/AML, certificate of origin, phytosanitary, halal, recalls,
  export licenses, duty/tariff, customs declaration
- dpp: EU Digital Product Passport (ERC721) + lifecycle/materials/repairability/recycling
- logistics: freight, cold-chain, bonded warehouse, fleet, containers, proof-of-delivery
- commodities: tokens, harvest/grading registries, storage receipts, price oracle, vault
- energy: renewable energy certs (ERC1155), emissions trading, water/biodiversity, green bonds
- workforce: soulbound worker credentials, safety training, milestone payroll, skills
- data: IoT sensors, quality inspection, lab tests, oracle aggregator, data marketplace
- 1468 forge tests green; 117 ABIs exported; DeployPlatform extended

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-31 21:12:08 +03:00

120 lines
4.3 KiB
Solidity

// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
import { Test } from "forge-std/Test.sol";
import { IAccessControl } from "@openzeppelin/contracts/access/IAccessControl.sol";
import { AddressBook } from "../../src/core/AddressBook.sol";
import { Roles } from "../../src/core/Roles.sol";
import { PhytosanitaryCertificate } from "../../src/compliance/PhytosanitaryCertificate.sol";
import { IPhytosanitaryCertificate } from "../../src/interfaces/IPhytosanitaryCertificate.sol";
contract PhytosanitaryCertificateTest is Test {
AddressBook internal book;
PhytosanitaryCertificate internal phyto;
address internal admin = address(0xA11CE);
address internal certifier = address(0xCE47);
address internal stranger = address(0xDEAD);
bytes32 internal constant CERT = keccak256("cert-1");
bytes32 internal constant BATCH = keccak256("batch-1");
bytes32 internal constant ORIGIN = bytes32("KE");
bytes32 internal constant DEST = bytes32("NL");
bytes32 internal constant PLANT = bytes32("Rosa");
bytes32 internal constant DOC = keccak256("doc");
uint64 internal expiry;
event Issued(
bytes32 indexed certId,
bytes32 indexed batchId,
bytes32 originCountry,
bytes32 destinationCountry,
IPhytosanitaryCertificate.TreatmentType treatment,
uint64 expiry
);
function setUp() public {
expiry = uint64(block.timestamp + 90 days);
vm.startPrank(admin);
book = new AddressBook(admin);
phyto = new PhytosanitaryCertificate(address(book), admin);
phyto.grantRole(Roles.CERTIFIER_ROLE, certifier);
vm.stopPrank();
}
function _issue() internal {
vm.prank(certifier);
phyto.issue(
CERT, BATCH, ORIGIN, DEST, PLANT, IPhytosanitaryCertificate.TreatmentType.Fumigation, DOC, expiry
);
}
function test_Issue_Happy() public {
vm.expectEmit(true, true, false, true);
emit Issued(CERT, BATCH, ORIGIN, DEST, IPhytosanitaryCertificate.TreatmentType.Fumigation, expiry);
_issue();
assertTrue(phyto.isValid(CERT));
IPhytosanitaryCertificate.Certificate memory c = phyto.certificateOf(CERT);
assertEq(uint8(c.treatment), uint8(IPhytosanitaryCertificate.TreatmentType.Fumigation));
assertEq(c.botanicalName, PLANT);
assertEq(c.issuer, certifier);
}
function test_Revoke_Happy() public {
_issue();
vm.prank(certifier);
phyto.revoke(CERT, "pest-interception");
assertFalse(phyto.isValid(CERT));
}
function test_IsValid_FalseWhenExpired() public {
_issue();
vm.warp(expiry + 1);
assertFalse(phyto.isValid(CERT));
}
function test_Revert_Issue_CertExists() public {
_issue();
vm.prank(certifier);
vm.expectRevert(abi.encodeWithSelector(IPhytosanitaryCertificate.CertExists.selector, CERT));
phyto.issue(CERT, BATCH, ORIGIN, DEST, PLANT, IPhytosanitaryCertificate.TreatmentType.None, DOC, expiry);
}
function test_Revert_Issue_PastExpiry() public {
vm.prank(certifier);
vm.expectRevert(
abi.encodeWithSelector(IPhytosanitaryCertificate.PastExpiry.selector, uint64(block.timestamp))
);
phyto.issue(
CERT, BATCH, ORIGIN, DEST, PLANT, IPhytosanitaryCertificate.TreatmentType.None, DOC, uint64(block.timestamp)
);
}
function test_Revert_Revoke_Unknown() public {
vm.prank(certifier);
vm.expectRevert(abi.encodeWithSelector(IPhytosanitaryCertificate.UnknownCert.selector, CERT));
phyto.revoke(CERT, "x");
}
function test_Revert_Revoke_AlreadyRevoked() public {
_issue();
vm.startPrank(certifier);
phyto.revoke(CERT, "x");
vm.expectRevert(abi.encodeWithSelector(IPhytosanitaryCertificate.AlreadyRevoked.selector, CERT));
phyto.revoke(CERT, "x");
vm.stopPrank();
}
function test_Revert_Issue_AccessControl() public {
vm.prank(stranger);
vm.expectRevert(
abi.encodeWithSelector(
IAccessControl.AccessControlUnauthorizedAccount.selector, stranger, Roles.CERTIFIER_ROLE
)
);
phyto.issue(CERT, BATCH, ORIGIN, DEST, PLANT, IPhytosanitaryCertificate.TreatmentType.None, DOC, expiry);
}
}