// SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.23; import {Test, console} from "forge-std/Test.sol"; import {DCO2s} from "../src/DCO2s.sol"; contract DCO2sTest is Test { DCO2s public dco2sc; string public name; string public symbol; string public baseURI; string[] public nfts; function setUp() public { baseURI = vm.envString("BASE_URI"); name = vm.envString("NAME"); symbol = vm.envString("SYMBOL"); dco2sc = new DCO2s(address(this), address(this), baseURI, name, symbol); // Import nfts array from .env and split by ' ' nfts = vm.envString("nfts", " "); } // @notice this code safe mints an NFT to address 0x1 function testMint() public { address to = address(0x1); dco2sc.safeMint(to, nfts[0]); assertEq(dco2sc.ownerOf(0), to, "Token should be minted to specified address."); } // @notice: here we test getting the Base and full URIS function testGetURI15() public { dco2sc.safeMint(address(0x1), nfts[15]); string memory tokenURI = dco2sc.tokenURI(0); //console.log(tokenURI); assertEq(tokenURI, string.concat(baseURI, nfts[15])); } // @notice: checkFailed token Index function testFailTokenIndex() public view { dco2sc.tokenURI(0); } // Todo How to verify bytes message error // @notice: checkFailed token Index //function testTokenIndex() public { // vm.expectRevert(bytes("ERC721NonexistentToken(0)")); // dco2sc.tokenURI(0); //} // @notice use basic Enumerable properties totalSupply // Find a more efficient way mintedNFTs = nfts[:5];? function testEnumerableTotalSupply() public { string[5] memory mintedNFTs; for (uint256 i = 0; i < 5; i++) { mintedNFTs[i] = nfts[i]; } // mint 5 nfts for (uint256 i = 0; i < mintedNFTs.length; i++) { dco2sc.safeMint(address(1), mintedNFTs[i]); } assertEq(mintedNFTs.length, dco2sc.totalSupply()); } // @notice: use the basic Enumerable property tokenByIndex // @notice: All nfts in the .env function testEnumerableTokenByIndex() public { for (uint256 i; i < nfts.length; i++) { dco2sc.safeMint(address(1), nfts[i]); } assertEq(nfts.length, dco2sc.totalSupply()); // Test for token By Index 4 uint256 tokenID = dco2sc.tokenByIndex(4); assertEq(nfts[4], nfts[tokenID]); // Test for token By Index 3 tokenID = dco2sc.tokenByIndex(3); assertEq(nfts[3], nfts[tokenID]); } // @notice use the basic Enumerable property TokenOfOwnerByIndex // @notice: All nfts in the .env function testEnumerableTokenOfOwnerByIndex() public { // mints pair to address 1 for (uint256 i; i < nfts.length; i++) { if (i % 2 == 0) { dco2sc.safeMint(address(1), nfts[i]); } else { dco2sc.safeMint(address(2), nfts[i]); } } // Get the balance of NFTs of the account 1 pairs are 8 uint256 account1_balance = dco2sc.balanceOf(address(1)); assertEq(account1_balance, 8); for (uint256 i; i < account1_balance; i++) { // get the TokenID from account NFTs Index uint256 token_id = dco2sc.tokenOfOwnerByIndex(address(1), i); assertEq(dco2sc.tokenURI(token_id), string.concat(baseURI, nfts[token_id])); } } // @notice: testFailMintToZeroAddress test that cannot safe mint to address 0 function testFailMintToZeroAddress() public { dco2sc.safeMint(address(0), nfts[0]); } // @notice testMintAll mints all the NFTs URIS and distribute to wallets // then it assertEq that it is properly minted to the wallet and the correct URI function testMintAll() public { //assign to nfts.lenght addresses each one 1 NFT for (uint256 i; i < nfts.length; i++) { dco2sc.safeMint(address(uint160(i + 1)), nfts[i]); } // assert that each NFT gets minted with the proper URI // and address for (uint256 i; i < nfts.length; i++) { uint256 token_id = dco2sc.tokenOfOwnerByIndex(address(uint160(i + 1)), 0); assertEq(string.concat(baseURI, nfts[i]), dco2sc.tokenURI(token_id)); } } }