test: add foundry tests for RepositoryInterface.sol

This commit is contained in:
source_weaver_3 2026-04-19 09:06:26 +00:00
parent 183d56dd8f
commit 52f134cf0f

View File

@ -0,0 +1,153 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "forge-std/Test.sol";
import "../contracts/interfaces/RepositoryInterface.sol";
contract RepositoryInterfaceTest is Test {
RepositoryInterface repositoryContract;
address testOwner;
address testContributor;
function setUp() public {
testOwner = makeAddr("testOwner");
testContributor = makeAddr("testContributor");
vm.prank(testOwner);
repositoryContract = new RepositoryInterface();
}
function test_CreateRepository() public {
vm.prank(testOwner);
bytes32 repositoryId = repositoryContract.createRepository(
"Test Repo",
"A test repository",
RepositoryInterface.AccessLevel.PUBLIC
);
assertNotEq(repositoryId, bytes32(0), "Repository creation should return a valid ID");
}
function testRevert_CreateRepository_EmptyName() public {
vm.expectRevert("Repository name cannot be empty");
vm.prank(testOwner);
repositoryContract.createRepository(
"",
"A test repository",
RepositoryInterface.AccessLevel.PUBLIC
);
}
function test_AddRepositoryEntry() public {
vm.prank(testOwner);
bytes32 repositoryId = repositoryContract.createRepository(
"Test Repo",
"A test repository",
RepositoryInterface.AccessLevel.PUBLIC
);
bytes32 contentHash = keccak256(abi.encodePacked("test content"));
vm.prank(testContributor);
bytes32 entryId = repositoryContract.addRepositoryEntry(
repositoryId,
contentHash,
"Test entry metadata"
);
assertNotEq(entryId, bytes32(0), "Entry addition should return a valid ID");
}
function testRevert_AddRepositoryEntry_InvalidRepository() public {
bytes32 invalidRepositoryId = keccak256(abi.encodePacked("invalid"));
bytes32 contentHash = keccak256(abi.encodePacked("test content"));
vm.expectRevert("Invalid repository");
vm.prank(testContributor);
repositoryContract.addRepositoryEntry(
invalidRepositoryId,
contentHash,
"Test entry metadata"
);
}
function test_UpdateRepositoryEntry() public {
vm.prank(testOwner);
bytes32 repositoryId = repositoryContract.createRepository(
"Test Repo",
"A test repository",
RepositoryInterface.AccessLevel.PUBLIC
);
bytes32 contentHash = keccak256(abi.encodePacked("test content"));
vm.prank(testContributor);
bytes32 entryId = repositoryContract.addRepositoryEntry(
repositoryId,
contentHash,
"Test entry metadata"
);
bytes32 newContentHash = keccak256(abi.encodePacked("updated content"));
vm.prank(testContributor);
repositoryContract.updateRepositoryEntry(
repositoryId,
entryId,
newContentHash,
"Updated metadata"
);
RepositoryInterface.RepositoryEntry memory updatedEntry = repositoryContract.getRepositoryEntry(
repositoryId,
entryId
);
assertEq(updatedEntry.contentHash, newContentHash, "Content hash should be updated");
}
function testRevert_UpdateRepositoryEntry_UnauthorizedUser() public {
vm.prank(testOwner);
bytes32 repositoryId = repositoryContract.createRepository(
"Test Repo",
"A test repository",
RepositoryInterface.AccessLevel.PUBLIC
);
bytes32 contentHash = keccak256(abi.encodePacked("test content"));
vm.prank(testContributor);
bytes32 entryId = repositoryContract.addRepositoryEntry(
repositoryId,
contentHash,
"Test entry metadata"
);
address unauthorized = makeAddr("unauthorized");
bytes32 newContentHash = keccak256(abi.encodePacked("updated content"));
vm.expectRevert("Unauthorized to update entry");
vm.prank(unauthorized);
repositoryContract.updateRepositoryEntry(
repositoryId,
entryId,
newContentHash,
"Updated metadata"
);
}
function testFuzz_CreateRepositoryWithVariedNames(string memory name) public {
vm.assume(bytes(name).length > 0 && bytes(name).length <= 50);
vm.prank(testOwner);
bytes32 repositoryId = repositoryContract.createRepository(
name,
"Fuzzy test repository",
RepositoryInterface.AccessLevel.PUBLIC
);
assertNotEq(repositoryId, bytes32(0), "Repository creation should succeed with valid name");
}
function test_GetRepositoryEntryCount() public {
vm.prank(testOwner);
bytes32 repositor