commit e81bc7465fb46fe7fe2263c6da7ff4ca971e06f4
parent e9622d87a8147721ea0b0d978288a37d92895342
Author: triesap <triesap@radroots.dev>
Date: Sat, 3 Jan 2026 17:28:51 +0000
jsonrpc: add unit tests for helpers
- Cover limit_or defaulting and MAX_LIMIT capping behavior
- Verify timeout_or default and explicit override semantics
- Assert apply_time_bounds sets or preserves since/until fields
- Validate parse_pubkeys_opt success path and InvalidParams errors
Diffstat:
1 file changed, 63 insertions(+), 0 deletions(-)
diff --git a/src/api/jsonrpc/params.rs b/src/api/jsonrpc/params.rs
@@ -67,3 +67,66 @@ pub(crate) fn apply_time_bounds(
}
filter
}
+
+#[cfg(test)]
+mod tests {
+ use super::{
+ apply_time_bounds,
+ limit_or,
+ parse_pubkeys_opt,
+ timeout_or,
+ DEFAULT_LIMIT,
+ DEFAULT_TIMEOUT_SECS,
+ MAX_LIMIT,
+ };
+ use crate::api::jsonrpc::RpcError;
+ use radroots_nostr::prelude::RadrootsNostrFilter;
+
+ #[test]
+ fn limit_or_defaults_and_caps() {
+ assert_eq!(limit_or(None), DEFAULT_LIMIT as usize);
+ assert_eq!(limit_or(Some(MAX_LIMIT + 1)), MAX_LIMIT as usize);
+ assert_eq!(limit_or(Some(0)), 0);
+ }
+
+ #[test]
+ fn timeout_or_defaults() {
+ assert_eq!(timeout_or(None), DEFAULT_TIMEOUT_SECS);
+ assert_eq!(timeout_or(Some(3)), 3);
+ }
+
+ #[test]
+ fn apply_time_bounds_sets_since_until() {
+ let filter = RadrootsNostrFilter::new();
+ let filter = apply_time_bounds(filter, Some(10), Some(20));
+ assert_eq!(filter.since.map(|t| t.as_secs()), Some(10));
+ assert_eq!(filter.until.map(|t| t.as_secs()), Some(20));
+ }
+
+ #[test]
+ fn apply_time_bounds_noop_when_empty() {
+ let filter = RadrootsNostrFilter::new();
+ let filter = apply_time_bounds(filter, None, None);
+ assert!(filter.since.is_none());
+ assert!(filter.until.is_none());
+ }
+
+ #[test]
+ fn parse_pubkeys_opt_accepts_valid() {
+ let key = "1bdebe7b23fccb167fc8843280b789839dfa296ae9fd86cc9769b4813d76d8a4";
+ let out = parse_pubkeys_opt("author", Some(vec![key.to_string()])).expect("pubkey");
+ let out = out.expect("some");
+ assert_eq!(out.len(), 1);
+ assert_eq!(out[0].to_string(), key);
+ }
+
+ #[test]
+ fn parse_pubkeys_opt_rejects_invalid() {
+ let err = parse_pubkeys_opt("author", Some(vec!["nope".to_string()]))
+ .expect_err("error");
+ match err {
+ RpcError::InvalidParams(msg) => assert!(msg.contains("invalid author")),
+ _ => panic!("unexpected error"),
+ }
+ }
+}