Skip to content

Commit

Permalink
Add support for pure integration tests (#458)
Browse files Browse the repository at this point in the history
* Embed mint_url as a field of HttpClient

* Create pure integration tests

* DirectMintConnection: convert between String and Uuid
  • Loading branch information
ok300 authored Dec 8, 2024
1 parent 9cb684e commit 86c4c2d
Show file tree
Hide file tree
Showing 18 changed files with 426 additions and 170 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions crates/cdk-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,17 @@ async fn main() -> Result<()> {

let mints = localstore.get_mints().await?;

for (mint, _) in mints {
for (mint_url, _) in mints {
let mut wallet = Wallet::new(
&mint.to_string(),
&mint_url.to_string(),
cdk::nuts::CurrencyUnit::Sat,
localstore.clone(),
&mnemonic.to_seed_normalized(""),
None,
)?;
if let Some(proxy_url) = args.proxy.as_ref() {
wallet.set_client(HttpClient::with_proxy(proxy_url.clone(), None, true)?);
let http_client = HttpClient::with_proxy(mint_url, proxy_url.clone(), None, true)?;
wallet.set_client(Arc::from(http_client));
}

wallets.push(wallet);
Expand Down
11 changes: 5 additions & 6 deletions crates/cdk-cli/src/sub_commands/mint_info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use cdk::mint_url::MintUrl;
use cdk::wallet::client::HttpClientMethods;
use cdk::wallet::client::MintConnector;
use cdk::HttpClient;
use clap::Args;
use url::Url;
Expand All @@ -11,14 +11,13 @@ pub struct MintInfoSubcommand {
}

pub async fn mint_info(proxy: Option<Url>, sub_command_args: &MintInfoSubcommand) -> Result<()> {
let mint_url = sub_command_args.mint_url.clone();
let client = match proxy {
Some(proxy) => HttpClient::with_proxy(proxy, None, true)?,
None => HttpClient::new(),
Some(proxy) => HttpClient::with_proxy(mint_url, proxy, None, true)?,
None => HttpClient::new(mint_url),
};

let info = client
.get_mint_info(sub_command_args.mint_url.clone())
.await?;
let info = client.get_mint_info().await?;

println!("{:#?}", info);

Expand Down
1 change: 1 addition & 0 deletions crates/cdk-integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ getrandom = { version = "0.2", features = ["js"] }
instant = { version = "0.1", features = ["wasm-bindgen", "inaccurate"] }

[dev-dependencies]
async-trait = "0.1"
rand = "0.8.5"
bip39 = { version = "2.0", features = ["rand"] }
anyhow = "1"
Expand Down
14 changes: 7 additions & 7 deletions crates/cdk-integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::{HashMap, HashSet};
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;

Expand All @@ -10,12 +11,13 @@ use cdk::cdk_database::mint_memory::MintMemoryDatabase;
use cdk::cdk_lightning::MintLightning;
use cdk::dhke::construct_proofs;
use cdk::mint::FeeReserve;
use cdk::mint_url::MintUrl;
use cdk::nuts::{
CurrencyUnit, Id, KeySet, MintBolt11Request, MintInfo, MintQuoteBolt11Request, MintQuoteState,
Nuts, PaymentMethod, PreMintSecrets, Proofs, State,
};
use cdk::types::{LnKey, QuoteTTL};
use cdk::wallet::client::{HttpClient, HttpClientMethods};
use cdk::wallet::client::{HttpClient, MintConnector};
use cdk::{Mint, Wallet};
use cdk_fake_wallet::FakeWallet;
use init_regtest::{get_mint_addr, get_mint_port, get_mint_url};
Expand Down Expand Up @@ -155,23 +157,21 @@ pub async fn mint_proofs(
println!("Minting for ecash");
println!();

let wallet_client = HttpClient::new();
let wallet_client = HttpClient::new(MintUrl::from_str(mint_url)?);

let request = MintQuoteBolt11Request {
amount,
unit: CurrencyUnit::Sat,
description,
};

let mint_quote = wallet_client
.post_mint_quote(mint_url.parse()?, request)
.await?;
let mint_quote = wallet_client.post_mint_quote(request).await?;

println!("Please pay: {}", mint_quote.request);

loop {
let status = wallet_client
.get_mint_quote_status(mint_url.parse()?, &mint_quote.quote)
.get_mint_quote_status(&mint_quote.quote)
.await?;

if status.state == MintQuoteState::Paid {
Expand All @@ -189,7 +189,7 @@ pub async fn mint_proofs(
outputs: premint_secrets.blinded_messages(),
};

let mint_response = wallet_client.post_mint(mint_url.parse()?, request).await?;
let mint_response = wallet_client.post_mint(request).await?;

let pre_swap_proofs = construct_proofs(
mint_response.signatures,
Expand Down
6 changes: 3 additions & 3 deletions crates/cdk-integration-tests/tests/fake_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cdk::cdk_database::WalletMemoryDatabase;
use cdk::nuts::{
CurrencyUnit, MeltBolt11Request, MeltQuoteState, MintQuoteState, PreMintSecrets, State,
};
use cdk::wallet::client::{HttpClient, HttpClientMethods};
use cdk::wallet::client::{HttpClient, MintConnector};
use cdk::wallet::Wallet;
use cdk_fake_wallet::{create_fake_invoice, FakeInvoiceDescription};
use cdk_integration_tests::attempt_to_swap_pending;
Expand Down Expand Up @@ -354,15 +354,15 @@ async fn test_fake_melt_change_in_quote() -> Result<()> {

let premint_secrets = PreMintSecrets::random(keyset.id, 100.into(), &SplitTarget::default())?;

let client = HttpClient::new();
let client = HttpClient::new(MINT_URL.parse()?);

let melt_request = MeltBolt11Request {
quote: melt_quote.id.clone(),
inputs: proofs.clone(),
outputs: Some(premint_secrets.blinded_messages()),
};

let melt_response = client.post_melt(MINT_URL.parse()?, melt_request).await?;
let melt_response = client.post_melt(melt_request).await?;

assert!(melt_response.change.is_some());

Expand Down
Loading

0 comments on commit 86c4c2d

Please sign in to comment.