// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import { Test } from "forge-std/Test.sol";

/// @notice Optional, read-only fork availability checks for every EVM chain
/// configured in foundry.toml. V3 pilot deployment is intentionally Base-only.
/// @dev An absent RPC URL is a logged skip, not a local-test failure.
contract SupportedChainForkAvailabilityV3Test is Test {
    function testFork_BaseEndpointIsBaseWhenConfigured() external {
        _selectAndAssertOrSkip("BASE_RPC_URL", 8453, "Base");
    }

    function testFork_AvalancheEndpointIsAvalancheWhenConfigured() external {
        _selectAndAssertOrSkip("AVAX_RPC_URL", 43114, "Avalanche");
    }

    function testFork_ArbitrumEndpointIsArbitrumWhenConfigured() external {
        _selectAndAssertOrSkip("ARB_RPC_URL", 42161, "Arbitrum");
    }

    function testFork_PolygonEndpointIsPolygonWhenConfigured() external {
        _selectAndAssertOrSkip("POLYGON_RPC_URL", 137, "Polygon");
    }

    function _selectAndAssertOrSkip(string memory environmentVariable, uint256 expectedChainId, string memory label)
        internal
    {
        string memory rpcUrl = vm.envOr(environmentVariable, string(""));
        if (bytes(rpcUrl).length == 0) {
            emit log(string.concat("SKIP ", label, " fork availability: ", environmentVariable, " is not configured"));
            return;
        }

        vm.createSelectFork(rpcUrl);
        assertEq(block.chainid, expectedChainId, string.concat(label, " RPC returned an unexpected chain ID"));
    }
}
