Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gui unit test transactions panel #1029

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions gui/src/app/state/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,15 +350,16 @@ mod tests {
client::{Lianad, Request},
model::*,
},
utils::{mock::Daemon, sandbox::Sandbox},
utils::{
mock::{mock_wallet, Daemon},
sandbox::Sandbox,
},
};

use liana::{descriptors::LianaDescriptor, miniscript::bitcoin::Address};
use liana::miniscript::bitcoin::Address;
use serde_json::json;
use std::str::FromStr;

const DESC: &str = "wsh(or_d(multi(2,[ffd63c8d/48'/1'/0'/2']tpubDExA3EC3iAsPxPhFn4j6gMiVup6V2eH3qKyk69RcTc9TTNRfFYVPad8bJD5FCHVQxyBT4izKsvr7Btd2R4xmQ1hZkvsqGBaeE82J71uTK4N/<0;1>/*,[de6eb005/48'/1'/0'/2']tpubDFGuYfS2JwiUSEXiQuNGdT3R7WTDhbaE6jbUhgYSSdhmfQcSx7ZntMPPv7nrkvAqjpj3jX9wbhSGMeKVao4qAzhbNyBi7iQmv5xxQk6H6jz/<0;1>/*),and_v(v:pkh([ffd63c8d/48'/1'/0'/2']tpubDExA3EC3iAsPxPhFn4j6gMiVup6V2eH3qKyk69RcTc9TTNRfFYVPad8bJD5FCHVQxyBT4izKsvr7Btd2R4xmQ1hZkvsqGBaeE82J71uTK4N/<2;3>/*),older(3))))#p9ax3xxp";

#[tokio::test]
async fn test_receive_panel() {
let addr =
Expand All @@ -372,7 +373,7 @@ mod tests {
ChildNumber::from_normal_idx(0).unwrap()
))),
)]);
let wallet = Arc::new(Wallet::new(LianaDescriptor::from_str(DESC).unwrap()));
let wallet = Arc::new(mock_wallet());
let sandbox: Sandbox<ReceivePanel> =
Sandbox::new(ReceivePanel::new(PathBuf::new(), wallet.clone()));
let client = Arc::new(Lianad::new(daemon.run()));
Expand Down
75 changes: 67 additions & 8 deletions gui/src/app/state/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::{
collections::{HashMap, HashSet},
convert::TryInto,
sync::Arc,
time::{SystemTime, UNIX_EPOCH},
};

use iced::Command;
Expand All @@ -25,6 +24,7 @@ use crate::{
wallet::Wallet,
},
daemon::model,
time,
};

use crate::daemon::{
Expand Down Expand Up @@ -179,7 +179,12 @@ impl State for TransactionsPanel {
self.pending_txs
.iter_mut()
.map(|tx| tx as &mut dyn Labelled)
.chain(self.txs.iter_mut().map(|tx| tx as &mut dyn Labelled)),
.chain(self.txs.iter_mut().map(|tx| tx as &mut dyn Labelled))
.chain(
self.selected_tx
.iter_mut()
.map(|tx| tx as &mut dyn Labelled),
),
) {
Ok(cmd) => {
return cmd;
Expand Down Expand Up @@ -244,19 +249,18 @@ impl State for TransactionsPanel {
let daemon1 = daemon.clone();
let daemon2 = daemon.clone();
let daemon3 = daemon.clone();
let now: u32 = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
.try_into()
.unwrap();

Command::batch(vec![
Command::perform(
async move { daemon3.list_pending_txs().map_err(|e| e.into()) },
Message::PendingTransactions,
),
Command::perform(
async move {
let now: u32 = time::now()
.timestamp()
.try_into()
.expect("i64 conversion to u32 for timestamp");
daemon1
.list_history_txs(0, now, view::home::HISTORY_EVENT_PAGE_SIZE)
.map_err(|e| e.into())
Expand Down Expand Up @@ -455,3 +459,58 @@ async fn rbf(
daemon.update_spend_tx(&psbt)?;
Ok(psbt.unsigned_tx.txid())
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{
app::cache::Cache,
daemon::{
client::{Lianad, Request},
model::*,
},
utils::{
mock::{mock_descriptor, mock_wallet, Daemon},
sandbox::Sandbox,
},
};

use liana::miniscript::bitcoin::Network;

use serde_json::json;

#[tokio::test]
async fn test_transactions_panel() {
let daemon = Daemon::new(vec![
(
Some(json!({"method": "getinfo", "params": Option::<Request>::None})),
Ok(json!(GetInfoResult {
version: "".to_string(),
network: Network::Testnet,
block_height: 1,
sync: 1.0,
rescan_progress: None,
descriptors: GetInfoDescriptors {
main: mock_descriptor()
},
timestamp: 1,
})),
),
(
Some(json!({"method": "listcoins", "params": Option::<Request>::None})),
Ok(json!(ListCoinsResult { coins: Vec::new() })),
),
(
Some(json!({"method": "listcoins", "params": Option::<Request>::None})),
Ok(json!(ListCoinsResult { coins: Vec::new() })),
),
]);
let wallet = Arc::new(mock_wallet());
let sandbox: Sandbox<TransactionsPanel> =
Sandbox::new(TransactionsPanel::new(wallet.clone()));
let client = Arc::new(Lianad::new(daemon.run()));
let sandbox = sandbox.load(client, &Cache::default(), wallet).await;

let _panel = sandbox.state();
}
}
4 changes: 4 additions & 0 deletions gui/src/daemon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ pub trait Daemon: Debug {
}
}

if txids.is_empty() {
return Ok(Vec::new());
}

let txs = self.list_txs(&txids)?.transactions;
let mut txs = txs
.into_iter()
Expand Down
6 changes: 3 additions & 3 deletions gui/src/daemon/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::collections::{HashMap, HashSet};
use liana::descriptors::LianaDescriptor;
pub use liana::{
commands::{
CreateSpendResult, GetAddressResult, GetInfoResult, GetLabelsResult, LabelItem,
ListCoinsEntry, ListCoinsResult, ListSpendEntry, ListSpendResult, ListTransactionsResult,
TransactionInfo,
CreateSpendResult, GetAddressResult, GetInfoDescriptors, GetInfoResult, GetLabelsResult,
LabelItem, ListCoinsEntry, ListCoinsResult, ListSpendEntry, ListSpendResult,
ListTransactionsResult, TransactionInfo,
},
descriptors::{LianaPolicy, PartialSpendInfo, PathSpendInfo},
miniscript::bitcoin::{
Expand Down
1 change: 1 addition & 0 deletions gui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod launcher;
pub mod loader;
pub mod logger;
pub mod signer;
pub mod time;
pub mod utils;

use liana::Version;
Expand Down
31 changes: 31 additions & 0 deletions gui/src/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use chrono::prelude::*;

#[cfg(test)]
pub mod mock_time {
use super::*;
use std::cell::RefCell;

thread_local! {
static MOCK_TIME: RefCell<Option<DateTime<Utc>>> = RefCell::new(None);
}

pub fn now() -> DateTime<Utc> {
MOCK_TIME.with(|cell| cell.borrow().as_ref().cloned().unwrap_or_else(Utc::now))
}

pub fn set_mock_time(time: DateTime<Utc>) {
MOCK_TIME.with(|cell| *cell.borrow_mut() = Some(time));
}

pub fn clear_mock_time() {
MOCK_TIME.with(|cell| *cell.borrow_mut() = None);
}
}

#[cfg(test)]
pub use mock_time::now;

#[cfg(not(test))]
pub fn now() -> DateTime<Utc> {
Utc::now()
}
21 changes: 18 additions & 3 deletions gui/src/utils/mock.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use crate::daemon::{client::Client, DaemonError};
use serde::{de::DeserializeOwned, Serialize};
use serde_json::{json, Value};
use std::fmt::Debug;
use std::str::FromStr;
use std::sync::{
mpsc::{channel, Receiver, Sender},
Mutex,
};
use std::thread;

use liana::descriptors::LianaDescriptor;

use crate::app::wallet::Wallet;
use crate::daemon::{client::Client, DaemonError};
use serde::{de::DeserializeOwned, Serialize};
use serde_json::{json, Value};

type TransportReceiver = Receiver<Result<Value, DaemonError>>;

#[derive(Debug)]
Expand Down Expand Up @@ -75,3 +80,13 @@ impl Daemon {
}
}
}

const DESC: &str = "wsh(or_d(multi(2,[ffd63c8d/48'/1'/0'/2']tpubDExA3EC3iAsPxPhFn4j6gMiVup6V2eH3qKyk69RcTc9TTNRfFYVPad8bJD5FCHVQxyBT4izKsvr7Btd2R4xmQ1hZkvsqGBaeE82J71uTK4N/<0;1>/*,[de6eb005/48'/1'/0'/2']tpubDFGuYfS2JwiUSEXiQuNGdT3R7WTDhbaE6jbUhgYSSdhmfQcSx7ZntMPPv7nrkvAqjpj3jX9wbhSGMeKVao4qAzhbNyBi7iQmv5xxQk6H6jz/<0;1>/*),and_v(v:pkh([ffd63c8d/48'/1'/0'/2']tpubDExA3EC3iAsPxPhFn4j6gMiVup6V2eH3qKyk69RcTc9TTNRfFYVPad8bJD5FCHVQxyBT4izKsvr7Btd2R4xmQ1hZkvsqGBaeE82J71uTK4N/<2;3>/*),older(3))))#p9ax3xxp";

pub fn mock_wallet() -> Wallet {
Wallet::new(mock_descriptor())
}

pub fn mock_descriptor() -> LianaDescriptor {
LianaDescriptor::from_str(DESC).unwrap()
}
Loading