use std::sync::{LazyLock, Mutex};
use color_eyre::{Report, Result, eyre::eyre};
use longtrader_contract::ext::decimal_to_common;
use longtrader_proto::{
client::TerminalClient,
proto::longtrader::{common::v1 as common, trading::v1 as utrading},
};
use rust_decimal::Decimal;
use crate::output::Renderer;
const GRAMMAR: &str = "[-]?digits[.digits]";
fn is_digits(text: &str) -> bool {
!text.is_empty() && text.bytes().all(|byte| byte.is_ascii_digit())
}
fn grammar_violation(s: &str) -> Option<&'static str> {
if s.contains('_') {
return Some("digit separators are not accepted");
}
if s.contains(['e', 'E']) {
return Some("exponents are not accepted");
}
if s.starts_with('+') {
return Some("a leading `+` is not accepted");
}
let body = s.strip_prefix('-').unwrap_or(s);
let (whole, fraction) = body.split_once('.').unwrap_or((body, ""));
if !is_digits(whole) {
return Some("expected one or more digits before the decimal point");
}
if body.contains('.') && !is_digits(fraction) {
return Some("expected one or more digits after the decimal point");
}
if fraction.len() > MAX_FRACTION_DIGITS {
return Some("too many fractional digits");
}
None
}
const MAX_FRACTION_DIGITS: usize = 28;
fn parse_dec(s: &str, field: &'static str) -> Result<Decimal> {
if let Some(reason) = grammar_violation(s) {
return Err(eyre!("bad decimal for {field}: {s:?} (want {GRAMMAR}: {reason})"));
}
let message = format!("bad decimal for {field}: {s:?}");
s.parse::<Decimal>().map_err(|source| Report::new(source).wrap_err(message))
}
fn parse_positive_dec(s: &str, field: &'static str) -> Result<Decimal> {
let value = parse_dec(s, field)?;
if value <= Decimal::ZERO {
return Err(eyre!("{field} must be greater than zero, got {s:?}"));
}
Ok(value)
}
static ORDER_IDS: LazyLock<Mutex<ulid::Generator>> =
LazyLock::new(|| Mutex::new(ulid::Generator::new()));
fn client_order_id() -> String {
let mut generator = ORDER_IDS.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
match generator.generate() {
Ok(id) => format!("cli-{id}"),
Err(overflow) => format!("cli-{}", overflow.commit_overflow_increment()),
}
}
#[expect(clippy::too_many_arguments)]
pub(crate) async fn place_order(
client: &TerminalClient,
exchange: &common::ExchangeId,
symbol: &str,
side: utrading::OrderSide,
quantity: &str,
price: Option<&str>,
take_profit: Option<&str>,
stop_loss: Option<&str>,
output: &Renderer,
) -> Result<()> {
let order_type =
if price.is_some() { utrading::OrderType::Limit } else { utrading::OrderType::Market };
let amount = parse_positive_dec(quantity, "quantity")?;
let limit_price = price
.map(|p| parse_positive_dec(p, "price"))
.transpose()?
.map_or_default(|d| decimal_to_common(d).into());
let bracket = |v: Option<&str>, field: &'static str| -> Result<_> {
Ok(v.map(|p| parse_dec(p, field).map(|d| decimal_to_common(d).into()))
.transpose()?
.unwrap_or_default())
};
let order = utrading::OrderRequest {
client_order_id: client_order_id(),
symbol: symbol.to_string(),
r#type: buffa::EnumValue::Known(order_type),
side: buffa::EnumValue::Known(side),
amount: decimal_to_common(amount).into(),
price: limit_price,
take_profit: bracket(take_profit, "take_profit")?,
stop_loss: bracket(stop_loss, "stop_loss")?,
..Default::default()
};
let req = utrading::CreateOrderRequest {
exchange_id: exchange.clone().into(),
order: order.into(),
..Default::default()
};
match client.create_order(req).await {
Ok(order) => {
output.render_orders(&[order])?;
Ok(())
}
Err(e) => Err(e.into()),
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use buffa::Message;
use longtrader_contract::ext::common_to_decimal;
use proptest::prelude::*;
use tokio::{
io::AsyncReadExt,
net::{TcpListener, TcpStream},
};
use super::*;
use crate::output::OutputFormat;
const DEAD_ENDPOINT: &str = "http://127.0.0.1:1";
const TRANSPORT_DEADLINE: Duration = Duration::from_millis(250);
const LOOPBACK: &str = "127.0.0.1:0";
const LISTEN_DEADLINE: Duration = Duration::from_secs(2);
const READ_DEADLINE: Duration = Duration::from_secs(2);
const RUN_DEADLINE: Duration = Duration::from_secs(5);
const FIELDS: [&str; 4] = ["quantity", "price", "take_profit", "stop_loss"];
const OVERFLOWED: &str = "1234567890123456789012345678901";
fn shape_error(field: &str, input: &str, reason: &str) -> String {
format!("bad decimal for {field}: {input:?} (want {GRAMMAR}: {reason})")
}
fn error_prefix(field: &str, input: &str) -> String {
format!("bad decimal for {field}: {input:?}")
}
async fn read_one_body(socket: &mut TcpStream) -> Vec<u8> {
let mut raw = Vec::new();
let mut buf = [0u8; 4096];
let header_end = loop {
if let Some(at) = raw.windows(4).position(|window| window == b"\r\n\r\n") {
break at + 4;
}
if !read_more(socket, &mut raw, &mut buf).await {
return Vec::new();
}
};
let headers = String::from_utf8_lossy(&raw[..header_end]).to_lowercase();
let declared: usize = headers
.lines()
.find_map(|line| line.strip_prefix("content-length:"))
.and_then(|value| value.trim().parse::<usize>().ok())
.unwrap_or(0);
while raw.len() < header_end + declared {
if !read_more(socket, &mut raw, &mut buf).await {
break;
}
}
raw[header_end..].to_vec()
}
async fn read_more(socket: &mut TcpStream, raw: &mut Vec<u8>, buf: &mut [u8]) -> bool {
let outcome = tokio::time::timeout(READ_DEADLINE, socket.read(buf)).await;
match outcome {
Ok(Ok(read)) => {
raw.extend_from_slice(&buf[..read]);
true
}
_ => false,
}
}
async fn capture_one_body(listener: TcpListener) -> Option<Vec<u8>> {
match tokio::time::timeout(LISTEN_DEADLINE, listener.accept()).await {
Ok(Ok((mut socket, _))) => Some(read_one_body(&mut socket).await),
_ => None,
}
}
async fn captured_order(
quantity: &str,
price: Option<&str>,
take_profit: Option<&str>,
stop_loss: Option<&str>,
) -> utrading::OrderRequest {
let listener =
TcpListener::bind(LOOPBACK).await.expect("a loopback port is always bindable");
let addr = listener.local_addr().expect("a bound listener always has an address");
let capture = tokio::spawn(capture_one_body(listener));
let client = TerminalClient::new(&format!("http://{addr}"));
let output = Renderer::new(OutputFormat::Table);
let exchange = common::ExchangeId::default();
let call = super::place_order(
&client,
&exchange,
"BTCUSDT",
utrading::OrderSide::Buy,
quantity,
price,
take_profit,
stop_loss,
&output,
);
let _ = tokio::time::timeout(RUN_DEADLINE, call).await;
let body = capture
.await
.expect("the capture task never panics")
.expect("a parseable order always sends its request");
let request = utrading::CreateOrderRequest::decode_from_slice(&body)
.expect("the CLI encodes a decodable CreateOrderRequest");
request.order.into_option().expect("place_order always populates the order")
}
async fn preflight(
quantity: &str,
price: Option<&str>,
take_profit: Option<&str>,
stop_loss: Option<&str>,
) -> core::result::Result<(), String> {
let client = TerminalClient::new(DEAD_ENDPOINT);
let output = Renderer::new(OutputFormat::Table);
let exchange = common::ExchangeId::default();
let call = super::place_order(
&client,
&exchange,
"BTCUSDT",
utrading::OrderSide::Buy,
quantity,
price,
take_profit,
stop_loss,
&output,
);
match tokio::time::timeout(TRANSPORT_DEADLINE, call).await {
Err(_elapsed) => Err("transport still pending".to_string()),
Ok(Ok(())) => Ok(()),
Ok(Err(report)) => Err(report.to_string()),
}
}
#[test]
fn a_bare_integer_is_the_whole_number_it_spells() {
assert_eq!(super::parse_dec("1", "quantity").expect("valid"), Decimal::from(1));
}
#[test]
fn a_decimal_point_is_honoured_as_a_scale_not_a_thousands_separator() {
assert_eq!(super::parse_dec("1.5", "quantity").expect("valid"), Decimal::new(15, 1));
}
#[test]
fn a_leading_minus_is_accepted_and_keeps_the_sign() {
assert_eq!(super::parse_dec("-2", "quantity").expect("valid"), Decimal::from(-2));
}
#[test]
fn a_small_fraction_survives_at_full_scale() {
assert_eq!(super::parse_dec("0.001", "quantity").expect("valid"), Decimal::new(1, 3));
}
#[test]
fn a_leading_plus_is_rejected_like_the_contract() {
let message = super::parse_dec("+1", "quantity")
.expect_err("a leading `+` is not a money literal")
.to_string();
assert_eq!(message, shape_error("quantity", "+1", "a leading `+` is not accepted"));
}
#[test]
fn a_sign_is_only_accepted_as_the_very_first_character() {
assert!(super::parse_dec("+-1", "quantity").is_err());
assert!(super::parse_dec("1-", "quantity").is_err());
assert!(super::parse_dec("--1", "quantity").is_err());
assert!(super::parse_dec("+1", "quantity").is_err());
}
#[test]
fn a_trailing_decimal_point_is_rejected_because_the_fraction_is_empty() {
let message = super::parse_dec("1.", "quantity")
.expect_err("`1.` is not a money literal")
.to_string();
assert_eq!(
message,
shape_error("quantity", "1.", "expected one or more digits after the decimal point")
);
}
#[test]
fn a_leading_decimal_point_is_rejected_because_the_whole_part_is_empty() {
let message = super::parse_dec(".5", "quantity")
.expect_err("`.5` is not a money literal")
.to_string();
assert_eq!(
message,
shape_error("quantity", ".5", "expected one or more digits before the decimal point")
);
}
#[test]
fn scientific_notation_is_rejected_rather_than_reaching_the_lossy_fallback() {
for input in ["1e3", "1E3", "1.5e3", "-1e3"] {
let rejected = super::parse_dec(input, "quantity");
let message = rejected.expect_err("an exponent is not accepted").to_string();
assert_eq!(message, shape_error("quantity", input, "exponents are not accepted"));
}
}
#[test]
fn an_explicitly_positive_exponent_is_rejected_so_a_typed_plus_cannot_scale_an_order() {
let message = super::parse_dec("1e+3", "quantity")
.expect_err("an exponent is not accepted")
.to_string();
assert_eq!(message, shape_error("quantity", "1e+3", "exponents are not accepted"));
}
#[test]
fn a_negative_exponent_is_rejected_too_so_no_scale_is_ever_implicit() {
let message = super::parse_dec("1e-3", "quantity")
.expect_err("an exponent is not accepted")
.to_string();
assert_eq!(message, shape_error("quantity", "1e-3", "exponents are not accepted"));
}
#[test]
fn a_thousands_separated_integer_is_rejected_before_it_can_send_1000() {
for input in ["1_000", "1_000.5", "1_0_0_0"] {
let message = super::parse_dec(input, "quantity")
.expect_err("a digit separator is not accepted")
.to_string();
assert_eq!(
message,
shape_error("quantity", input, "digit separators are not accepted")
);
}
}
#[test]
fn an_empty_run_of_separators_is_rejected_too() {
for input in ["1_", "1._5", "_1"] {
let message = super::parse_dec(input, "quantity")
.expect_err("a digit separator is not accepted")
.to_string();
assert_eq!(
message,
shape_error("quantity", input, "digit separators are not accepted")
);
}
}
#[test]
fn an_empty_string_is_rejected() {
assert!(super::parse_dec("", "quantity").is_err());
}
#[test]
fn surrounding_whitespace_is_rejected_because_the_input_is_never_trimmed() {
assert!(super::parse_dec(" 1 ", "quantity").is_err());
assert!(super::parse_dec("\t1", "quantity").is_err());
assert!(super::parse_dec("1\n", "quantity").is_err());
}
#[test]
fn nan_is_rejected() {
assert!(super::parse_dec("NaN", "quantity").is_err());
assert!(super::parse_dec("nan", "quantity").is_err());
}
#[test]
fn infinity_is_rejected() {
assert!(super::parse_dec("inf", "quantity").is_err());
assert!(super::parse_dec("Infinity", "quantity").is_err());
assert!(super::parse_dec("-inf", "quantity").is_err());
}
#[test]
fn a_bare_sign_with_no_digits_is_rejected() {
assert!(super::parse_dec("-", "quantity").is_err());
assert!(super::parse_dec("+", "quantity").is_err());
assert!(super::parse_dec(".", "quantity").is_err());
}
#[test]
fn two_decimal_points_are_rejected() {
for input in ["1.2.3", "0.1."] {
let message = super::parse_dec(input, "quantity")
.expect_err("a second decimal point is not accepted")
.to_string();
assert_eq!(
message,
shape_error(
"quantity",
input,
"expected one or more digits after the decimal point"
)
);
}
}
#[test]
fn a_comma_decimal_separator_is_rejected() {
assert!(super::parse_dec("1,5", "quantity").is_err());
}
#[test]
fn a_currency_prefix_or_suffix_is_rejected() {
for input in ["$1", "1$", "BTC", "1btc", "0x10"] {
assert!(super::parse_dec(input, "quantity").is_err(), "accepted {input:?}");
}
}
#[test]
fn an_exponent_marker_anywhere_is_rejected() {
for input in ["1e", "e3", "1e3e3", "1E", "E3"] {
assert!(super::parse_dec(input, "quantity").is_err(), "accepted {input:?}");
}
}
#[test]
fn twenty_eight_fractional_digits_is_the_largest_exactly_representable_input() {
let smallest = "0.0000000000000000000000000001";
assert_eq!(
super::parse_dec(smallest, "quantity").expect("the maximum scale is representable"),
Decimal::new(1, 28)
);
}
#[test]
fn a_rejection_names_the_field_it_came_from() {
let message = super::parse_dec(OVERFLOWED, "quantity")
.expect_err("31 digits overflow a 96-bit mantissa")
.to_string();
assert_eq!(message, error_prefix("quantity", OVERFLOWED));
}
#[test]
fn a_rejection_echoes_the_input_with_debug_quoting() {
let message =
super::parse_dec(" 1 ", "price").expect_err("a space is not a decimal").to_string();
assert_eq!(
message,
shape_error("price", " 1 ", "expected one or more digits before the decimal point")
);
}
#[test]
fn every_field_name_survives_into_the_message_verbatim() {
for field in FIELDS {
let report =
super::parse_dec(OVERFLOWED, field).expect_err("31 digits overflow the mantissa");
assert_eq!(report.to_string(), error_prefix(field, OVERFLOWED), "{field}");
}
}
#[test]
fn a_parser_rejection_keeps_the_rust_decimal_reason_as_the_reports_cause() {
let report =
super::parse_dec(OVERFLOWED, "quantity").expect_err("31 digits overflow the mantissa");
assert_eq!(report.to_string(), error_prefix("quantity", OVERFLOWED));
let mut causes = String::new();
for cause in report.chain().skip(1) {
causes.push_str(&cause.to_string());
}
assert_eq!(causes, "Invalid decimal: overflow from too many digits");
}
#[test]
fn the_error_message_never_names_the_parser_crate() {
for input in ["abc", OVERFLOWED, "1_000", "1e3", " 1 "] {
let message = super::parse_dec(input, "quantity")
.expect_err("none of these is an accepted money literal")
.to_string();
assert!(!message.contains("rust_decimal"), "{input:?}: {message}");
}
}
#[tokio::test]
async fn a_malformed_quantity_is_reported_before_any_bracket_is_parsed() {
let Err(message) = preflight("abc", Some("xyz"), Some("!"), Some("?")).await else {
unreachable!("`abc` is never a valid quantity");
};
assert_eq!(
message,
shape_error("quantity", "abc", "expected one or more digits before the decimal point")
);
}
#[tokio::test]
async fn the_bracket_fields_are_parsed_in_declaration_order() {
let Err(price) = preflight("1", Some("x"), Some("y"), Some("z")).await else {
unreachable!("`x` is never a valid price");
};
assert_eq!(
price,
shape_error("price", "x", "expected one or more digits before the decimal point")
);
let Err(take_profit) = preflight("1", Some("1"), Some("y"), Some("z")).await else {
unreachable!("`y` is never a valid take_profit");
};
assert_eq!(
take_profit,
shape_error("take_profit", "y", "expected one or more digits before the decimal point")
);
let Err(stop_loss) = preflight("1", Some("1"), Some("1"), Some("z")).await else {
unreachable!("`z` is never a valid stop_loss");
};
assert_eq!(
stop_loss,
shape_error("stop_loss", "z", "expected one or more digits before the decimal point")
);
}
#[tokio::test]
async fn a_parseable_order_reaches_the_transport_so_the_parse_is_not_the_gate() {
assert!(preflight("1", Some("1"), Some("2"), Some("3")).await.is_err());
}
#[tokio::test]
async fn an_absent_price_stays_absent_rather_than_encoding_a_zero() {
let order = captured_order("1", None, None, None).await;
assert!(order.price.is_unset(), "an absent price must stay absent");
assert!(order.price.as_option().is_none());
}
#[tokio::test]
async fn absent_brackets_stay_absent_rather_than_encoding_zeros() {
let order = captured_order("1", None, None, None).await;
assert!(order.take_profit.is_unset());
assert!(order.stop_loss.is_unset());
}
#[tokio::test]
async fn a_supplied_price_is_present_and_encodes_the_typed_decimal() {
let order = captured_order("1", Some("1500.25"), None, None).await;
let price = order.price.as_option().expect("a supplied price must be present");
assert_eq!(price.value, "1500.25");
}
#[tokio::test]
async fn both_brackets_can_be_armed_independently() {
let only_take_profit = captured_order("1", None, Some("1600"), None).await;
assert!(only_take_profit.take_profit.is_set());
assert!(only_take_profit.stop_loss.is_unset());
let only_stop_loss = captured_order("1", None, None, Some("1400")).await;
assert!(only_stop_loss.take_profit.is_unset());
assert!(only_stop_loss.stop_loss.is_set());
}
#[tokio::test]
async fn a_negative_bracket_price_keeps_its_sign() {
let order = captured_order("1", None, Some("-2"), None).await;
let take_profit = order.take_profit.as_option().expect("a supplied bracket is present");
assert_eq!(take_profit.value, "-2");
}
#[tokio::test]
async fn an_arm_bracket_at_the_widest_representable_value_survives_unchanged() {
let order = captured_order("1", None, Some("79228162514264337593543950335"), None).await;
let take_profit = order.take_profit.as_option().expect("a supplied bracket is present");
assert_eq!(take_profit.value, "79228162514264337593543950335");
assert_eq!(
common_to_decimal(take_profit).expect("the CLI writes only decodable payloads"),
Decimal::MAX
);
}
#[tokio::test]
async fn an_order_without_a_price_is_a_market_order() {
let order = captured_order("1", None, None, None).await;
assert_eq!(order.r#type, buffa::EnumValue::Known(utrading::OrderType::Market));
}
#[tokio::test]
async fn an_order_with_a_price_is_a_limit_order() {
let order = captured_order("1", Some("1500.25"), None, None).await;
assert_eq!(order.r#type, buffa::EnumValue::Known(utrading::OrderType::Limit));
}
#[tokio::test]
async fn a_bracket_alone_never_promotes_a_market_order_to_a_limit() {
let order = captured_order("1", None, Some("1600"), Some("1400")).await;
assert_eq!(order.r#type, buffa::EnumValue::Known(utrading::OrderType::Market));
assert!(order.take_profit.is_set());
assert!(order.stop_loss.is_set());
}
#[tokio::test]
async fn a_zero_price_is_refused_before_a_limit_order_can_reach_a_venue() {
for price in ["0", "0.0", "0.000"] {
let Err(message) = preflight("1", Some(price), None, None).await else {
unreachable!("a zero price must not be sent as a limit order");
};
assert_eq!(message, format!("price must be greater than zero, got {price:?}"));
}
}
#[tokio::test]
async fn a_negative_price_is_refused_for_the_same_reason() {
for price in ["-1", "-0.5", "-1500"] {
let Err(message) = preflight("1", Some(price), None, None).await else {
unreachable!("a negative limit price must not be sent");
};
assert_eq!(message, format!("price must be greater than zero, got {price:?}"));
}
}
#[tokio::test]
async fn a_non_positive_quantity_is_refused_before_the_request_is_built() {
for quantity in ["0", "0.000", "-1", "-0.5"] {
let Err(message) = preflight(quantity, Some("1500"), None, None).await else {
unreachable!("a zero or negative quantity is not an order");
};
assert_eq!(message, format!("quantity must be greater than zero, got {quantity:?}"));
}
}
#[tokio::test]
async fn the_quantity_risk_check_wins_over_a_later_bad_price() {
let Err(message) = preflight("0", Some("abc"), None, None).await else {
unreachable!("a zero quantity must not be sent");
};
assert_eq!(message, "quantity must be greater than zero, got \"0\"");
}
#[tokio::test]
async fn a_non_positive_exit_bracket_is_still_accepted_because_brackets_keep_their_sign() {
let Err(message) = preflight("1", None, Some("-2"), Some("-1")).await else {
unreachable!("the request must reach the dead transport");
};
assert!(!message.contains("must be greater than zero"), "{message}");
}
#[tokio::test]
async fn the_smallest_accepted_price_still_survives_the_risk_check() {
let typed = "0.0000000000000000000000000001";
let order = captured_order("1", Some(typed), None, None).await;
assert_eq!(order.r#type, buffa::EnumValue::Known(utrading::OrderType::Limit));
let price = order.price.as_option().expect("an accepted price is present");
assert_eq!(price.value, typed);
}
#[tokio::test]
async fn the_side_and_symbol_are_copied_onto_the_wire_unchanged() {
let order = captured_order("2.5", Some("1500"), None, None).await;
let amount = order.amount.as_option().expect("the amount is always set");
assert_eq!(order.symbol, "BTCUSDT");
assert_eq!(order.side, buffa::EnumValue::Known(utrading::OrderSide::Buy));
assert_eq!(amount.value, "2.5");
}
#[tokio::test]
async fn the_client_order_id_carries_the_cli_namespace_prefix() {
let order = captured_order("1", None, None, None).await;
assert!(
order.client_order_id.starts_with("cli-"),
"the venue scopes ids by prefix, got {:?}",
order.client_order_id
);
assert_eq!(order.client_order_id.len(), "cli-".len() + 26, "a ULID is 26 base32 chars");
}
#[tokio::test]
async fn two_orders_get_two_distinct_client_order_ids() {
let first = captured_order("1", None, None, None).await;
let second = captured_order("1", None, None, None).await;
assert_ne!(first.client_order_id, second.client_order_id);
}
#[test]
fn successive_client_order_ids_are_strictly_increasing() {
let ids: Vec<String> = (0..16).map(|_| super::client_order_id()).collect();
for pair in ids.windows(2) {
assert!(pair[0] < pair[1], "{} is not below {}", pair[0], pair[1]);
}
}
#[test]
fn every_client_order_id_is_prefixed_and_therefore_uniform_in_length() {
let mut seen = std::collections::BTreeSet::new();
for _ in 0..64 {
let id = super::client_order_id();
assert!(id.starts_with("cli-"), "{id}");
assert_eq!(id.len(), "cli-".len() + 26, "{id}");
assert!(seen.insert(id.clone()), "{id} was minted twice");
}
}
#[tokio::test]
async fn ids_minted_from_concurrent_tasks_are_distinct() {
let mut handles = Vec::new();
for _ in 0..8 {
handles.push(tokio::task::spawn_blocking(super::client_order_id));
}
let mut ids = Vec::with_capacity(handles.len());
for handle in handles {
ids.push(handle.await.expect("a blocking task never panics"));
}
let mut unique = ids.clone();
unique.sort_unstable();
unique.dedup();
assert_eq!(unique.len(), ids.len(), "two tasks minted the same id: {ids:?}");
}
#[tokio::test]
async fn the_exchange_selector_is_carried_onto_the_wire() {
let listener =
TcpListener::bind(LOOPBACK).await.expect("a loopback port is always bindable");
let addr = listener.local_addr().expect("a bound listener always has an address");
let capture = tokio::spawn(capture_one_body(listener));
let client = TerminalClient::new(&format!("http://{addr}"));
let output = Renderer::new(OutputFormat::Table);
let exchange = crate::commands::exchange_id("binance");
let call = super::place_order(
&client,
&exchange,
"ETHUSDT",
utrading::OrderSide::Sell,
"1",
None,
None,
None,
&output,
);
let _ = tokio::time::timeout(RUN_DEADLINE, call).await;
let body = capture
.await
.expect("the capture task never panics")
.expect("a parseable order always sends its request");
let request = utrading::CreateOrderRequest::decode_from_slice(&body)
.expect("the CLI encodes a decodable CreateOrderRequest");
assert_eq!(
request.exchange_id.as_option().expect("the selector is always set").id,
"binance"
);
let order = request.order.into_option().expect("the order is always set");
assert_eq!(order.symbol, "ETHUSDT");
assert_eq!(order.side, buffa::EnumValue::Known(utrading::OrderSide::Sell));
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(256))]
#[test]
fn a_rejection_always_names_the_field_and_echoes_the_input(
input in ".{0,10}",
field in proptest::sample::select(FIELDS.to_vec()),
) {
if let Err(report) = super::parse_dec(&input, field) {
prop_assert!(
report.to_string().starts_with(&error_prefix(field, &input)),
"{}",
report
);
}
}
#[test]
fn the_accepted_grammar_does_not_depend_on_the_field(
input in ".{0,10}",
) {
let outcomes: Vec<bool> =
FIELDS.iter().map(|field| super::parse_dec(&input, field).is_ok()).collect();
prop_assert!(
outcomes.windows(2).all(|pair| pair[0] == pair[1]),
"input={:?} outcomes={:?}",
input,
outcomes
);
}
#[test]
fn the_grammar_is_exactly_the_documented_shape(
sign in proptest::sample::select(&["", "-"][..]),
whole in "[0-9]{1,9}",
dot in proptest::sample::select(&["", "."][..]),
frac in "[0-9]{1,9}",
) {
let literal = format!("{sign}{whole}{dot}{frac}");
prop_assert!(
super::parse_dec(&literal, "quantity").is_ok(),
"literal={:?} was refused",
literal
);
}
#[test]
fn an_accepted_decimal_survives_the_contract_round_trip(
whole in "[0-9]{1,9}",
frac in "[0-9]{1,9}",
) {
let input = format!("{whole}.{frac}");
let parsed = super::parse_dec(&input, "quantity")
.expect("a plain decimal literal always parses");
let wire = decimal_to_common(parsed);
prop_assert_eq!(
common_to_decimal(&wire).expect("the encoder always emits a decodable decimal"),
parsed,
"input={:?}",
input
);
}
#[test]
fn no_digit_separator_or_exponent_ever_reaches_the_parser(
noise in "[-_eE+]{1,4}",
) {
let literal = format!("1{noise}000");
let refused = super::parse_dec(&literal, "quantity");
prop_assert!(
refused.is_err(),
"literal={:?} was accepted as {:?}",
literal,
refused.map(|value| value.to_string())
);
}
#[test]
fn an_encoded_decimal_carries_its_own_value(
mantissa in proptest::num::i64::ANY,
scale in 0u32..=28,
) {
let value = Decimal::new(mantissa, scale);
let wire = decimal_to_common(value);
prop_assert_eq!(&wire.value, &value.to_string());
prop_assert_eq!(
common_to_decimal(&wire).expect("the encoder writes only decodable payloads"),
value,
"mantissa={} scale={}",
mantissa,
scale
);
}
}
}