source_weaver_3-contract-lab/test/RepositoryExplorerTest.t.sol
source_weaver_3 3a7024e52e
Some checks are pending
CI / build-and-test (push) Waiting to run
CI / slither (push) Waiting to run
test: add foundry tests for RepositoryExplorerTest.sol
2026-04-19 09:11:45 +00:00

122 lines
4.9 KiB
Solidity

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {Test, console} from "forge-std/Test.sol";
import {RepositoryExplorer} from "../contracts/RepositoryExplorer.sol";
import {Repository} from "../contracts/Repository.sol";
contract RepositoryExplorerTest is Test {
RepositoryExplorer public repositoryExplorer;
Repository public repository;
address public owner;
address public testUser;
function setUp() public {
owner = address(this);
testUser = vm.addr(1);
repository = new Repository(owner);
repositoryExplorer = new RepositoryExplorer(address(repository));
vm.startPrank(testUser);
}
function test_CreateRepository() public {
string memory repoName = "TestRepo";
string memory repoDescription = "A test repository for exploration";
uint256 repositoryId = repositoryExplorer.createRepository(repoName, repoDescription);
assertEq(repositoryId, 1, "First repository should have ID 1");
(string memory name, string memory description, address creator) = repository.getRepositoryDetails(repositoryId);
assertEq(name, repoName, "Repository name should match");
assertEq(description, repoDescription, "Repository description should match");
assertEq(creator, testUser, "Repository creator should be test user");
}
function test_AddRepositoryContent() public {
string memory repoName = "ContentRepo";
string memory repoDescription = "Repository for content testing";
uint256 repositoryId = repositoryExplorer.createRepository(repoName, repoDescription);
string memory contentHash = "QmTestHash123";
string memory contentType = "text/markdown";
repositoryExplorer.addRepositoryContent(repositoryId, contentHash, contentType);
(string memory retrievedHash, string memory retrievedType) = repository.getRepositoryContent(repositoryId, 0);
assertEq(retrievedHash, contentHash, "Content hash should match");
assertEq(retrievedType, contentType, "Content type should match");
}
function test_TransferRepositoryOwnership() public {
address newOwner = vm.addr(2);
string memory repoName = "OwnershipRepo";
string memory repoDescription = "Repository for ownership transfer";
uint256 repositoryId = repositoryExplorer.createRepository(repoName, repoDescription);
vm.startPrank(testUser);
repositoryExplorer.transferRepositoryOwnership(repositoryId, newOwner);
address currentOwner = repository.getRepositoryOwner(repositoryId);
assertEq(currentOwner, newOwner, "Repository ownership should be transferred");
}
function testRevert_CreateRepositoryWithEmptyName() public {
vm.expectRevert("Repository name cannot be empty");
repositoryExplorer.createRepository("", "Empty name test");
}
function test_MultipleRepositoryCreation() public {
uint256 initialRepositoryCount = repositoryExplorer.getTotalRepositories();
repositoryExplorer.createRepository("Repo1", "First test repository");
repositoryExplorer.createRepository("Repo2", "Second test repository");
uint256 finalRepositoryCount = repositoryExplorer.getTotalRepositories();
assertEq(finalRepositoryCount, initialRepositoryCount + 2, "Two repositories should be created");
}
function testFuzz_CreateRepositoryWithVariedNames(string memory repoName) public {
vm.assume(bytes(repoName).length > 0);
uint256 repositoryId = repositoryExplorer.createRepository(repoName, "Fuzzy repository");
(string memory name,, ) = repository.getRepositoryDetails(repositoryId);
assertEq(name, repoName, "Fuzzy repository name should match input");
}
function testGas_CreateRepository() public {
string memory repoName = "GasTestRepo";
string memory repoDescription = "Repository for gas measurement";
uint256 gasStart = gasleft();
repositoryExplorer.createRepository(repoName, repoDescription);
uint256 gasSpent = gasStart - gasleft();
console.log("Gas spent on repository creation:", gasSpent);
}
function test_RepositoryContentLimits() public {
string memory repoName = "LimitTestRepo";
uint256 repositoryId = repositoryExplorer.createRepository(repoName, "Limit testing");
for (uint256 i = 0; i < 10; i++) {
repositoryExplorer.addRepositoryContent(
repositoryId,
string(abi.encodePacked("QmTestHash", vm.toString(i))),
"text/plain"
);
}
uint256 contentCount = repository.getRepositoryContentCount(repositoryId);
assertEq(contentCount, 10, "R