Skip to content

Commit

Permalink
test: unit tests in pop-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexD10S committed Dec 12, 2024
1 parent c9b2396 commit d2d7c1a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
51 changes: 29 additions & 22 deletions crates/pop-cli/src/commands/call/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ impl CallChainCommand {
}
}

if !prompt_to_repeat_call ||
!cli.confirm("Do you want to perform another call?")
if !prompt_to_repeat_call
|| !cli
.confirm("Do you want to perform another call?")
.initial_value(false)
.interact()?
{
Expand Down Expand Up @@ -211,7 +212,7 @@ impl CallChainCommand {
// skip the prompt.
let suri = match self.suri.as_ref() {
Some(suri) => suri.clone(),
None =>
None => {
if !self.use_wallet {
if cli.confirm("Do you want to use your browser wallet to sign the transaction? (Selecting 'No' will prompt you to manually enter the secret key URI for signing, e.g., '//Alice')")
.initial_value(true)
Expand All @@ -224,7 +225,8 @@ impl CallChainCommand {
}
} else {
DEFAULT_URI.to_string()
},
}
},
};

return Ok(Call {
Expand Down Expand Up @@ -252,8 +254,9 @@ impl CallChainCommand {
None => &cli.input("Signer of the extrinsic:").default_input(DEFAULT_URI).interact()?,
};
cli.info(format!("Encoded call data: {}", call_data))?;
if !self.skip_confirm &&
!cli.confirm("Do you want to submit the extrinsic?")
if !self.skip_confirm
&& !cli
.confirm("Do you want to submit the extrinsic?")
.initial_value(true)
.interact()?
{
Expand Down Expand Up @@ -281,22 +284,24 @@ impl CallChainCommand {
// execute the call via `sudo`.
fn configure_sudo(&mut self, chain: &Chain, cli: &mut impl Cli) -> Result<()> {
match find_dispatchable_by_name(&chain.pallets, "Sudo", "sudo") {
Ok(_) =>
Ok(_) => {
if !self.sudo {
self.sudo = cli
.confirm(
"Would you like to dispatch this function call with `Root` origin?",
)
.initial_value(false)
.interact()?;
},
Err(_) =>
}
},
Err(_) => {
if self.sudo {
cli.warning(
"NOTE: sudo is not supported by the chain. Ignoring `--sudo` flag.",
)?;
self.sudo = false;
},
}
},
}
Ok(())
}
Expand All @@ -312,11 +317,11 @@ impl CallChainCommand {

// Function to check if all required fields are specified.
fn requires_user_input(&self) -> bool {
self.pallet.is_none() ||
self.function.is_none() ||
self.args.is_empty() ||
self.url.is_none() ||
self.suri.is_none()
self.pallet.is_none()
|| self.function.is_none()
|| self.args.is_empty()
|| self.url.is_none()
|| self.suri.is_none()
}

/// Replaces file arguments with their contents, leaving other arguments unchanged.
Expand Down Expand Up @@ -397,8 +402,9 @@ impl Call {
tx: DynamicPayload,
cli: &mut impl Cli,
) -> Result<()> {
if !self.skip_confirm &&
!cli.confirm("Do you want to submit the extrinsic?")
if !self.skip_confirm
&& !cli
.confirm("Do you want to submit the extrinsic?")
.initial_value(true)
.interact()?
{
Expand Down Expand Up @@ -465,7 +471,7 @@ impl Call {
}
full_message.push_str(&format!(" --url {}", chain.url));
if self.use_wallet {
full_message.push_str("--use-wallet");
full_message.push_str(" --use-wallet");
} else {
full_message.push_str(&format!(" --suri {}", self.suri));
}
Expand Down Expand Up @@ -699,7 +705,7 @@ mod tests {
)
.expect_input("The value for `remark` might be too large to enter. You may enter the path to a file instead.", "0x11".into())
.expect_confirm("Would you like to dispatch this function call with `Root` origin?", true)
.expect_input("Signer of the extrinsic:", "//Bob".into());
.expect_confirm("Do you want to use your browser wallet to sign the transaction? (Selecting 'No' will prompt you to manually enter the secret key URI for signing, e.g., '//Alice')", true);

let chain = call_config.configure_chain(&mut cli).await?;
assert_eq!(chain.url, Url::parse(POP_NETWORK_TESTNET_URL)?);
Expand All @@ -708,9 +714,10 @@ mod tests {
assert_eq!(call_chain.function.pallet, "System");
assert_eq!(call_chain.function.name, "remark");
assert_eq!(call_chain.args, ["0x11".to_string()].to_vec());
assert_eq!(call_chain.suri, "//Bob");
assert_eq!(call_chain.suri, "//Alice"); // Default value
assert!(call_chain.use_wallet);
assert!(call_chain.sudo);
assert_eq!(call_chain.display(&chain), "pop call chain --pallet System --function remark --args \"0x11\" --url wss://rpc1.paseo.popnetwork.xyz/ --suri //Bob --sudo");
assert_eq!(call_chain.display(&chain), "pop call chain --pallet System --function remark --args \"0x11\" --url wss://rpc1.paseo.popnetwork.xyz/ --use-wallet --sudo");
cli.verify()
}

Expand Down Expand Up @@ -901,7 +908,7 @@ mod tests {
assert_eq!(call_config.function, None);
assert_eq!(call_config.args.len(), 0);
assert!(!call_config.sudo);
assert!(call_config.use_wallet);
assert!(!call_config.use_wallet);
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions crates/pop-cli/src/common/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub async fn wait_for_signature(call_data: Vec<u8>, url: String) -> anyhow::Resu
Ok(signed_payload)
}

#[ignore]
#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit d2d7c1a

Please sign in to comment.