From 9465e79255540a72d83d736f4bc8a1f8592f5d1c Mon Sep 17 00:00:00 2001 From: sourcekeeper_42 Date: Sun, 19 Apr 2026 09:08:22 +0000 Subject: [PATCH] test: add foundry tests for LabHelper.sol --- test/LabHelper.t.sol | 135 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 test/LabHelper.t.sol diff --git a/test/LabHelper.t.sol b/test/LabHelper.t.sol new file mode 100644 index 000000000..0ee98ec8a --- /dev/null +++ b/test/LabHelper.t.sol @@ -0,0 +1,135 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.19; + +import "forge-std/Test.sol"; +import "../contracts/LabHelper.sol"; +import "./mocks/MockLabRegistry.sol"; + +contract LabHelperTest is Test { + MockLabRegistry internal mockRegistry; + address internal testOwner; + address internal testContributor; + + function setUp() public { + testOwner = makeAddr("labOwner"); + testContributor = makeAddr("contributor"); + mockRegistry = new MockLabRegistry(); + } + + function test_validateLabCreation_Success() public { + uint256 initialFunds = 0.1 ether; + bool result = LabHelper.validateLabCreation(testOwner, initialFunds); + assertTrue(result, "Lab creation should be valid"); + } + + function testRevert_validateLabCreation_ZeroAddress() public { + uint256 initialFunds = 0.1 ether; + vm.expectRevert(abi.encodeWithSignature("InvalidLabConfiguration(string)", "Invalid owner address")); + LabHelper.validateLabCreation(address(0), initialFunds); + } + + function testRevert_validateLabCreation_InsufficientFunds() public { + uint256 initialFunds = 0.001 ether; + vm.expectRevert( + abi.encodeWithSignature( + "InsufficientContribution(uint256,uint256)", + 0.01 ether, + initialFunds + ) + ); + LabHelper.validateLabCreation(testOwner, initialFunds); + } + + function test_calculateContributionScore() public { + LabHelper.ContributionRecord[] memory records = new LabHelper.ContributionRecord[](3); + + records[0] = LabHelper.ContributionRecord({ + contributor: testContributor, + amount: 10, + timestamp: block.timestamp, + contributionType: "code" + }); + + records[1] = LabHelper.ContributionRecord({ + contributor: testContributor, + amount: 20, + timestamp: block.timestamp, + contributionType: "research" + }); + + records[2] = LabHelper.ContributionRecord({ + contributor: testContributor, + amount: 30, + timestamp: block.timestamp, + contributionType: "documentation" + }); + + uint256 score = LabHelper.calculateContributionScore(records); + assertEq(score, 10 * 3 + 20 * 2 + 30 * 1, "Contribution score calculation incorrect"); + } + + function testFuzz_calculateContributionScore( + uint256 codeAmount, + uint256 researchAmount, + uint256 docAmount + ) public { + LabHelper.ContributionRecord[] memory records = new LabHelper.ContributionRecord[](3); + + records[0] = LabHelper.ContributionRecord({ + contributor: testContributor, + amount: codeAmount, + timestamp: block.timestamp, + contributionType: "code" + }); + + records[1] = LabHelper.ContributionRecord({ + contributor: testContributor, + amount: researchAmount, + timestamp: block.timestamp, + contributionType: "research" + }); + + records[2] = LabHelper.ContributionRecord({ + contributor: testContributor, + amount: docAmount, + timestamp: block.timestamp, + contributionType: "documentation" + }); + + uint256 score = LabHelper.calculateContributionScore(records); + assertGe(score, 0, "Contribution score should be non-negative"); + } + + function test_validateContributor_Success() public { + vm.mockCall( + address(mockRegistry), + abi.encodeWithSelector(ILabRegistry.isValidContributor.selector, testContributor), + abi.encode(true) + ); + + bool result = LabHelper.validateContributor(testContributor, mockRegistry); + assertTrue(result, "Valid contributor should return true"); + } + + function test_validateContributor_ZeroAddress() public { + bool result = LabHelper.validateContributor(address(0), mockRegistry); + assertFalse(result, "Zero address should be invalid"); + } + + function test_generateLabMetadata() public { + LabHelper.LabMetadata memory metadata = LabHelper.generateLabMetadata(testOwner); + + assertEq(metadata.owner, testOwner, "Owner address mismatch"); + assertEq(metadata.isActive, true, "Lab should be active by default"); + assertEq(metadata.totalContributions, 0, "Initial contributions should be zero"); + assertGt(metadata.createdAt, 0, "Created timestamp should be set"); + assertEq(metadata.createdAt, metadata.lastUpdated, "Created and updated timestamps should match"); + } + + function test_GasBenchmark_CalculateContributionScore() public { + LabHelper.ContributionRecord[] memory records = new LabHelper.ContributionRecord[](3); + + records[0] = LabHelper.ContributionRecord({ + contributor: testContributor, + amount: 10, + timestamp: \ No newline at end of file