How to use agent-rs and replica in end2end test?

How to use agent-rs and replica in end2end test?

I’m not sure what problem you’re having, but you can see an example of e2e tests with agent-rs and replica (via DFX) here: https://github.com/dfinity/response-verification/blob/main/packages/ic-response-verification-tests/src/main.rs

You can get the address of the replica in a shell script like this:

DFX_REPLICA_PORT=$(dfx info replica-port)
DFX_REPLICA_ADDRESS="http://localhost:$DFX_REPLICA_PORT"

Then this DFX_REPLICA_ADDRESS variable is passed into the e2e test binary and it’s given to the agent:

let transport = ReqwestHttpReplicaV2Transport::create(env::var("DFX_REPLICA_ADDRESS").unwrap())?;

let agent = Agent::builder().with_transport(transport).build()?;
agent.fetch_root_key().await?;

I have a multi-canister project that uses a lot of inter-canister calls. I want test those calls in the project level with product paths (using agent-rs to test). So i have to run a mainnet like environment in almost every tests, and then install the canister, call the method with arguments and maybe cycles, test the outputs. In this situation I can also test my project communicating with ledgers, cmc-canister…

So actually i need/want to write a test tool to start a totally fresh replica in every tests starting.

dfx do similar works to start a replica. But it has some problems such as slow starting time, per project per replica state, communicating it through only command line…thus no suitable in my situation.

And ic-test-state-machine lacks a lot of features of ic replica.

Similar product in ethereal network: Hardhat Network | Ethereum development environment for professionals by Nomic Foundation

Hardhat comes built-in with Hardhat Network, a local Ethereum network node designed for development. It allows you to deploy your contracts, run your tests and debug your code, all within the confines of your local machine.

Here is the introduction of harhad’s test suite:

# How does it work?

It runs as either an in-process or stand-alone daemon, servicing JSON-RPC and WebSocket requests.
By default, it mines a block with each transaction that it receives, in order and with no delay.
It’s backed by the @ethereumjs/vm EVM implementation, the same one used by ganache, Remix and Ethereum Studio.

Hi @famouscat8

you might want to have a look at the LightIC project GitHub - icopen/lightic by @stopak.

Thx for reply, I already noticed that project. But it still not suitable for me as

  1. I want to test in rust as my project writing in rust mostly.
  2. It lacks a lot of features of ic replica. i checked lightic’s code , found it just another ic-test-state-machine but in typescript.
1 Like

communicating it through only command line

You can run shell commands through Rust too, you may need to make some convenience methods in your code to wrap these commands but that’s trivial to implement.

slow starting time

That’s true, but is it really a blocker to use DFX? The e2e tests will take a long time to run, but they will work.

per project per replica state

Why do you need to run your project on multiple replicas? Inter-canister query calls are run on the same replica on mainnet anyway.

For inter-canister update calls, your code should not change if you are making calls to canisters on the same subnet or another subnet. So if you want to test with multiple subnets locally then you are not testing your own code anymore, you are then testing the code of the network. You only need to test that your code works with inter-canister call on the same replica, everything else is the responsibility of the network.

I can also test my project communicating with ledgers, cmc-canister

The dfx nns extension can install all of those canisters for you.

a totally fresh replica in every tests starting

Do you mean for every test in your test suite that you will start a new replica? I think this is not necessary. If you need fresh canister state then you can just reinstall your own canister. This will be much faster that starting a fresh replica for every test.

Thanks for checking out the project. What features do you miss? You can make inter-canister calls and deploy multiple canisters. The hardhat was actually an inspiration for lightic