v0.2.11-pre.010
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
// file: crates/ksp-offchain-transport-lib/unit_tests/market_price_adapter.rs
|
||||
// version: 1
|
||||
|
||||
#[test]
|
||||
fn timestamp_helpers_preserve_valid_values_and_reject_malformed_pre_epoch_and_overflow() {
|
||||
let epoch_fraction = crate::market_price_timestamp_from_rfc3339("1970-01-01T00:00:00.123Z");
|
||||
assert_eq!(epoch_fraction.map(|value| return value.unix_millis()), std::option::Option::Some(123));
|
||||
assert!(crate::market_price_timestamp_from_rfc3339("1969-12-31T23:59:59Z").is_none());
|
||||
assert!(crate::market_price_timestamp_from_rfc3339("not-a-timestamp").is_none());
|
||||
assert_eq!(crate::market_price_timestamp_from_unix_seconds(1).map(|value| return value.unix_millis()), std::option::Option::Some(1_000));
|
||||
assert!(crate::market_price_timestamp_from_unix_seconds(u64::MAX).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_provider_response_error_contains_only_safe_provider_and_field_context() {
|
||||
let error = crate::invalid_provider_response("coingecko", "price");
|
||||
assert_eq!(error.code(), crate::ERROR_CODE_MARKET_PRICE_PROVIDER_RESPONSE_INVALID);
|
||||
let context = error.context();
|
||||
assert_eq!(context.len(), 2);
|
||||
assert_eq!(context[0].key(), "provider");
|
||||
assert_eq!(context[0].value(), "coingecko");
|
||||
assert_eq!(context[1].key(), "field");
|
||||
assert_eq!(context[1].value(), "price");
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
// file: crates/ksp-offchain-transport-lib/unit_tests/market_price_decimal.rs
|
||||
// version: 4
|
||||
// version: 5
|
||||
|
||||
#[test]
|
||||
fn decimal_normalizes_fractional_and_scientific_forms_without_f64() -> ksp_core_lib::Result<()> {
|
||||
@@ -84,3 +84,19 @@ fn decimal_parses_raw_json_number_and_string_without_f64_round_trip() -> ksp_cor
|
||||
assert_eq!(parsed.to_canonical_string(), "151.23");
|
||||
return std::result::Result::Ok(());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decimal_raw_json_rejects_non_numeric_and_pathological_values() {
|
||||
let invalid = ["null", "true", "false", "{}", "[]", r#""0""#, r#""-1""#, r#""1e-19""#, r#""1e129""#];
|
||||
for source in invalid {
|
||||
let raw = serde_json::from_str::<std::boxed::Box<serde_json::value::RawValue>>(source);
|
||||
assert!(raw.is_ok(), "adversarial raw JSON fixture must itself be syntactically valid: {source}");
|
||||
if let std::result::Result::Ok(raw) = raw {
|
||||
let parsed = crate::MarketPriceDecimal::parse_json_raw(raw.as_ref());
|
||||
assert!(parsed.is_err(), "non-price raw JSON must not become a successful decimal: {source}");
|
||||
if let std::result::Result::Err(error) = parsed {
|
||||
assert_eq!(error.code(), crate::ERROR_CODE_MARKET_PRICE_DECIMAL_INVALID);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// file: crates/ksp-offchain-transport-lib/unit_tests/market_price_observation.rs
|
||||
// version: 3
|
||||
// version: 4
|
||||
|
||||
#[test]
|
||||
fn observation_preserves_pair_exact_price_semantics_timestamps_and_safe_provenance() -> ksp_core_lib::Result<()> {
|
||||
@@ -67,3 +67,18 @@ fn observation_rejects_reversed_ksp_timestamps_and_unsafe_provenance() -> ksp_co
|
||||
assert!(result.is_err());
|
||||
return std::result::Result::Ok(());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provenance_accepts_exact_boundary_and_rejects_trim_control_and_oversize() -> ksp_core_lib::Result<()> {
|
||||
let exact = "a".repeat(crate::MARKET_PRICE_PROVENANCE_MAX_BYTES);
|
||||
let provenance = crate::MarketPriceProvenance::new(exact.clone());
|
||||
assert!(provenance.is_ok());
|
||||
if let std::result::Result::Ok(provenance) = provenance {
|
||||
assert_eq!(provenance.as_str(), exact);
|
||||
}
|
||||
assert!(crate::MarketPriceProvenance::new(format!("{exact}a")).is_err());
|
||||
assert!(crate::MarketPriceProvenance::new(" leading").is_err());
|
||||
assert!(crate::MarketPriceProvenance::new("trailing ").is_err());
|
||||
assert!(crate::MarketPriceProvenance::new("tab\tvalue").is_err());
|
||||
return std::result::Result::Ok(());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user