Skip to content

Commit

Permalink
feat(jstzd): implement jstzd cli
Browse files Browse the repository at this point in the history
  • Loading branch information
huancheng-trili committed Nov 25, 2024
1 parent b79b8e9 commit d054009
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 25 deletions.
93 changes: 93 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ description = "JavaScript server runtime for Tezos Smart Rollups"
[workspace.dependencies]
ansi_term = "0.12.1"
anyhow = "1.0.82"
assert_cmd = "2.0.14"
async-dropper-simple = { version = "0.2.6", features = ["tokio"] }
async-trait = "0.1.82"
axum = "0.7.7"
Expand Down Expand Up @@ -74,6 +75,7 @@ nix = { version = "^0.27.1", features = ["process", "signal"] }
nom = "7.1.3"
num-traits = "0.2.16"
parking_lot = "0.12.1"
predicates = "3.1.0"
prettytable = "0.10.0"
pretty_assertions = "1.4.1"
proptest = "1.1"
Expand Down
3 changes: 3 additions & 0 deletions crates/jstzd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ async-dropper-simple.workspace = true
async-trait.workspace = true
axum.workspace = true
bollard.workspace = true
clap.workspace = true
futures.workspace = true
futures-util.workspace = true
http.workspace = true
Expand All @@ -25,6 +26,8 @@ tempfile.workspace = true
tokio.workspace = true

[dev-dependencies]
assert_cmd.workspace = true
predicates.workspace = true
rand.workspace = true
tezos_crypto_rs.workspace = true

Expand Down
3 changes: 1 addition & 2 deletions crates/jstzd/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(dead_code)]
use std::path::{Path, PathBuf};

use crate::task::jstzd::JstzdConfig;
Expand Down Expand Up @@ -47,7 +46,7 @@ async fn parse_config(path: &str) -> anyhow::Result<Config> {
Ok(serde_json::from_str::<Config>(&s)?)
}

async fn build_config(
pub(crate) async fn build_config(
config_path: &Option<String>,
) -> anyhow::Result<(u16, JstzdConfig)> {
let mut config = match config_path {
Expand Down
20 changes: 17 additions & 3 deletions crates/jstzd/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
mod config;
pub mod docker;
pub mod task;

pub use config::BOOTSTRAP_CONTRACT_NAMES;
use std::process::exit;

pub const EXCHANGER_ADDRESS: &str = "KT1F3MuqvT9Yz57TgCS3EkDcKNZe9HpiavUJ";
pub const JSTZ_ROLLUP_ADDRESS: &str = "sr1PuFMgaRUN12rKQ3J2ae5psNtwCxPNmGNK";
pub const JSTZ_NATIVE_BRIDGE_ADDRESS: &str = "KT1GFiPkkTjd14oHe6MrBPiRh5djzRkVWcni";

/// The `main` function for running jstzd
pub async fn main() -> anyhow::Result<()> {
println!("Hello, world!");
Ok(())
pub async fn main(config_path: &Option<String>) {
match config::build_config(config_path).await {
Ok((_port, _config)) => {
// TODO: run JstzdServer here
println!("ready");
}
Err(e) => {
match config_path {
Some(p) => eprintln!("failed to build config from {}: {:?}", p, e),
None => eprintln!("failed to build default config: {:?}", e),
};
exit(1);
}
}
}
20 changes: 17 additions & 3 deletions crates/jstzd/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
use clap::{Parser, Subcommand};

#[derive(Parser, Debug)]
struct Cli {
#[command(subcommand)]
command: Commands,
}

#[derive(Subcommand, Debug)]
enum Commands {
/// Run the sandbox
Run { config_path: Option<String> },
}

#[tokio::main]
async fn main() {
if let Err(e) = jstzd::main().await {
eprintln!("Error: {:?}", e);
std::process::exit(1);
let cli = Cli::parse();
match &cli.command {
Commands::Run { config_path } => jstzd::main(config_path).await,
}
}
17 changes: 0 additions & 17 deletions crates/jstzd/tests/dummy.rs

This file was deleted.

58 changes: 58 additions & 0 deletions crates/jstzd/tests/main_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use assert_cmd::prelude::*; // Add methods on commands
use predicates::prelude::*;
use std::{io::Write, process::Command};
use tempfile::NamedTempFile; // Used for writing assertions // Run programs

#[test]
fn unknown_command() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("jstzd")?;

cmd.arg("test")
.assert()
.failure()
.stderr(predicate::str::contains("unrecognized subcommand \'test\'"));

Ok(())
}

#[test]
fn default_config() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("jstzd")?;

cmd.arg("run")
.assert()
.success()
.stdout(predicate::str::contains("ready"));

Ok(())
}

#[test]
fn valid_config_file() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("jstzd")?;
let mut tmp_file = NamedTempFile::new().unwrap();
tmp_file.write_all(r#"{"protocol":{"bootstrap_accounts":[["edpkuSLWfVU1Vq7Jg9FucPyKmma6otcMHac9zG4oU1KMHSTBpJuGQ2","6000000000"]]}}"#.as_bytes()).unwrap();

cmd.args(["run", &tmp_file.path().to_string_lossy()])
.assert()
.success()
.stdout(predicate::str::contains("ready"));

Ok(())
}

#[test]
fn bad_config_file() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("jstzd")?;
let mut tmp_file = NamedTempFile::new().unwrap();
tmp_file.write_all("{}".as_bytes()).unwrap();

cmd.args(["run", &tmp_file.path().to_string_lossy()])
.assert()
.failure()
.stderr(predicate::str::contains(
"should have at least one bootstrap account with at least 6000 tez",
));

Ok(())
}

0 comments on commit d054009

Please sign in to comment.