freenet-core icon indicating copy to clipboard operation
freenet-core copied to clipboard

Improve contract loading in ping app tests

Open sanity opened this issue 6 months ago • 1 comments

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:

  1. The CARGO_TARGET_DIR environment variable is set to the project's target directory
  2. The ping contract has been manually built with cargo build --target wasm32-unknown-unknown
  3. 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

  1. Automatic compilation: The test should compile the contract if it doesn't exist, similar to how the compile_contract function works in the common module.

  2. Better path resolution: Use the existing compile_contract function or similar approach that:

    • Respects CARGO_TARGET_DIR if set
    • Falls back to reasonable defaults
    • Compiles the contract on-demand
  3. 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.

sanity avatar Jun 06 '25 00:06 sanity

Related to

https://github.com/freenet/freenet-core/issues/1565 https://github.com/freenet/freenet-core/issues/1566

iduartgomez avatar Jun 14 '25 18:06 iduartgomez

This should be imrpoved with https://github.com/freenet/freenet-core/pull/1716

iduartgomez avatar Jul 22 '25 12:07 iduartgomez