#![recursion_limit = "256"]
use serde_json::{Map, Value};
use test_log::test;
use unc_token::UncToken;
use std::fs::File;
use std::path::Path;
#[test(tokio::test)]
async fn test_subaccount_creation() -> anyhow::Result<()> {
let worker = utility_workspaces::sandbox().await?;
let account = worker.dev_create_account().await?;
let sub = account
.create_subaccount("subaccount")
.transact()
.await?
.into_result()?;
let expect_id = format!("subaccount.{}", account.id());
let actual_id = sub.id().to_string();
assert_eq!(actual_id, expect_id);
let savedir = Path::new("../target/credentials");
sub.store_credentials(savedir).await?;
let creds = File::open(savedir.join(format!("{}.json", sub.id())))?;
let contents: Map<String, Value> = serde_json::from_reader(creds)?;
assert_eq!(
contents.get("account_id"),
Some(&Value::String(sub.id().to_string()))
);
Ok(())
}
#[test(tokio::test)]
async fn test_transfer_unc() -> anyhow::Result<()> {
const INITIAL_BALANCE: UncToken = UncToken::from_unc(100);
let worker = utility_workspaces::sandbox().await?;
let (alice, bob) = (
worker.dev_create_account().await?,
worker.dev_create_account().await?,
);
assert_eq!(alice.view_account().await?.balance, INITIAL_BALANCE);
assert_eq!(bob.view_account().await?.balance, INITIAL_BALANCE);
const SENT_AMOUNT: UncToken = UncToken::from_attounc(500_000_000);
let _ = alice.transfer_unc(bob.id(), SENT_AMOUNT).await?;
assert_eq!(
bob.view_account().await?.balance,
INITIAL_BALANCE.saturating_add(SENT_AMOUNT),
);
assert!(alice.view_account().await?.balance <= INITIAL_BALANCE.saturating_sub(SENT_AMOUNT));
Ok(())
}
#[test(tokio::test)]
async fn test_delete_account() -> anyhow::Result<()> {
let worker = utility_workspaces::sandbox().await?;
let (alice, bob) = (
worker.dev_create_account().await?,
worker.dev_create_account().await?,
);
_ = alice.clone().delete_account(bob.id()).await?;
assert!(bob.view_account().await?.balance > UncToken::from_unc(100));
let res = alice.view_account().await;
assert!(res.is_err());
assert!(res
.unwrap_err()
.into_inner()
.unwrap()
.to_string()
.contains(&format!("{} does not exist while viewing", alice.id())),);
Ok(())
}