103 lines
4.1 KiB
Solidity
103 lines
4.1 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 testCreateRepository() 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 testAddRepositoryContent() 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 testTransferRepositoryOwnership() 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 testCannotCreateRepositoryWithEmptyName() public {
|
|
vm.expectRevert("Repository name cannot be empty");
|
|
repositoryExplorer.createRepository("", "Empty name test");
|
|
}
|
|
|
|
function testMultipleRepositoryCreation() 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 testRepositoryContentLimits() 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, "Should add 10 content items");
|
|
}
|
|
} |