freenet-core
freenet-core copied to clipboard
Improve contract loading in ping app tests
Problem
The freenet-ping app tests currently have a fragile mechanism for loading compiled WASM contracts. The tests fail with "Failed to read contract code" unless:
- The CARGO_TARGET_DIR environment variable is set to the project's target directory
- The ping contract has been manually built with
cargo build --target wasm32-unknown-unknown - The compiled WASM exists at exactly
$CARGO_TARGET_DIR/wasm32-unknown-unknown/debug/freenet_ping_contract.wasm
Current Implementation
The tests use a hardcoded path in app/tests/common/mod.rs:
pub const PATH_TO_CONTRACT: &str = "../target/wasm32-unknown-unknown/debug/freenet_ping_contract.wasm";
This assumes:
- The target directory is at a specific relative location
- The contract has been pre-built
- The debug build is being used
Proposed Solution
-
Automatic compilation: The test should compile the contract if it doesn't exist, similar to how the
compile_contractfunction works in the common module. -
Better path resolution: Use the existing
compile_contractfunction or similar approach that:- Respects CARGO_TARGET_DIR if set
- Falls back to reasonable defaults
- Compiles the contract on-demand
-
Clear error messages: If the contract can't be found or compiled, provide a helpful error message explaining what's needed.
Example Fix
The deploy_contract function in common/mod.rs could be updated to use the compile_contract function instead of directly reading from PATH_TO_CONTRACT:
pub async fn deploy_contract(
client: &mut WebApi,
initial_ping_state: Ping,
options: &PingContractOptions,
subscribe: bool,
) -> Result<ContractKey> {
let contract_path = PathBuf::from(PACKAGE_DIR).join("../contracts/ping");
let code = compile_contract(&contract_path)?; // Use compile_contract instead
// ... rest of function
}
This would make the tests more robust and easier to run without manual setup.
Related to
https://github.com/freenet/freenet-core/issues/1565 https://github.com/freenet/freenet-core/issues/1566
This should be imrpoved with https://github.com/freenet/freenet-core/pull/1716