amp-rust 0.0.9

A Rust client for the Blockstream AMP API, providing interfaces for asset management, user operations, and token handling on the Liquid Network.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
use amp_rs::client::{RetryClient, RetryConfig, TokenError};
use httpmock::prelude::*;
use serial_test::serial;
use std::env;
use std::time::Duration as StdDuration;

#[test]
fn test_retry_config_default() {
    let config = RetryConfig::default();
    assert_eq!(config.max_attempts, 3);
    assert_eq!(config.base_delay_ms, 1000);
    assert_eq!(config.max_delay_ms, 30000);
    assert_eq!(config.timeout_seconds, 10);
}

#[test]
fn test_retry_config_for_tests() {
    let config = RetryConfig::for_tests();
    assert_eq!(config.max_attempts, 2);
    assert_eq!(config.base_delay_ms, 500);
    assert_eq!(config.max_delay_ms, 5000);
    assert_eq!(config.timeout_seconds, 5);
}

#[test]
#[serial]
fn test_retry_config_from_env_defaults() {
    // Don't load .env for this test since we want to test defaults
    // Clear any existing environment variables
    env::remove_var("API_RETRY_MAX_ATTEMPTS");
    env::remove_var("API_RETRY_BASE_DELAY_MS");
    env::remove_var("API_RETRY_MAX_DELAY_MS");
    env::remove_var("API_REQUEST_TIMEOUT_SECONDS");

    let config = RetryConfig::from_env().unwrap();
    assert_eq!(config.max_attempts, 3);
    assert_eq!(config.base_delay_ms, 1000);
    assert_eq!(config.max_delay_ms, 30000);
    assert_eq!(config.timeout_seconds, 10);
}

#[test]
#[serial]
fn test_retry_config_from_env_custom_values() {
    dotenvy::dotenv().ok();
    env::set_var("API_RETRY_MAX_ATTEMPTS", "5");
    env::set_var("API_RETRY_BASE_DELAY_MS", "2000");
    env::set_var("API_RETRY_MAX_DELAY_MS", "60000");
    env::set_var("API_REQUEST_TIMEOUT_SECONDS", "30");

    let config = RetryConfig::from_env().unwrap();
    assert_eq!(config.max_attempts, 5);
    assert_eq!(config.base_delay_ms, 2000);
    assert_eq!(config.max_delay_ms, 60000);
    assert_eq!(config.timeout_seconds, 30);

    // Clean up
    env::remove_var("API_RETRY_MAX_ATTEMPTS");
    env::remove_var("API_RETRY_BASE_DELAY_MS");
    env::remove_var("API_RETRY_MAX_DELAY_MS");
    env::remove_var("API_REQUEST_TIMEOUT_SECONDS");
}
#[test]
#[serial]
fn test_retry_config_from_env_invalid_values() {
    dotenvy::dotenv().ok();
    env::set_var("API_RETRY_MAX_ATTEMPTS", "invalid");
    let result = RetryConfig::from_env();
    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("Invalid API_RETRY_MAX_ATTEMPTS"));
    env::remove_var("API_RETRY_MAX_ATTEMPTS");

    env::set_var("API_RETRY_BASE_DELAY_MS", "not_a_number");
    let result = RetryConfig::from_env();
    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("Invalid API_RETRY_BASE_DELAY_MS"));
    env::remove_var("API_RETRY_BASE_DELAY_MS");

    env::set_var("API_RETRY_MAX_DELAY_MS", "-1");
    let result = RetryConfig::from_env();
    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("Invalid API_RETRY_MAX_DELAY_MS"));
    env::remove_var("API_RETRY_MAX_DELAY_MS");

    env::set_var("API_REQUEST_TIMEOUT_SECONDS", "abc");
    let result = RetryConfig::from_env();
    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("Invalid API_REQUEST_TIMEOUT_SECONDS"));
    env::remove_var("API_REQUEST_TIMEOUT_SECONDS");
}

#[test]
#[serial]
fn test_retry_config_validation() {
    dotenvy::dotenv().ok();
    // Test zero max_attempts
    env::set_var("API_RETRY_MAX_ATTEMPTS", "0");
    let result = RetryConfig::from_env();
    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("max_attempts must be greater than 0"));
    env::remove_var("API_RETRY_MAX_ATTEMPTS");

    // Test zero base_delay_ms
    env::set_var("API_RETRY_BASE_DELAY_MS", "0");
    let result = RetryConfig::from_env();
    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("base_delay_ms must be greater than 0"));
    env::remove_var("API_RETRY_BASE_DELAY_MS");

    // Test max_delay_ms < base_delay_ms
    env::set_var("API_RETRY_BASE_DELAY_MS", "2000");
    env::set_var("API_RETRY_MAX_DELAY_MS", "1000");
    let result = RetryConfig::from_env();
    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("max_delay_ms must be greater than or equal to base_delay_ms"));
    env::remove_var("API_RETRY_BASE_DELAY_MS");
    env::remove_var("API_RETRY_MAX_DELAY_MS");

    // Test zero timeout_seconds
    env::set_var("API_REQUEST_TIMEOUT_SECONDS", "0");
    let result = RetryConfig::from_env();
    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("timeout_seconds must be greater than 0"));
    env::remove_var("API_REQUEST_TIMEOUT_SECONDS");
}

#[test]
fn test_retry_config_builder_methods() {
    let config = RetryConfig::default()
        .with_timeout(20)
        .with_max_attempts(5)
        .with_base_delay_ms(2000)
        .with_max_delay_ms(60000);

    assert_eq!(config.timeout_seconds, 20);
    assert_eq!(config.max_attempts, 5);
    assert_eq!(config.base_delay_ms, 2000);
    assert_eq!(config.max_delay_ms, 60000);
}

#[test]
#[serial]
fn test_retry_config_partial_env_vars() {
    dotenvy::dotenv().ok();
    // Set only some environment variables, others should use defaults
    env::set_var("API_RETRY_MAX_ATTEMPTS", "7");
    env::set_var("API_RETRY_BASE_DELAY_MS", "1500");
    // Don't set API_RETRY_MAX_DELAY_MS and API_REQUEST_TIMEOUT_SECONDS

    let config = RetryConfig::from_env().unwrap();
    assert_eq!(config.max_attempts, 7);
    assert_eq!(config.base_delay_ms, 1500);
    assert_eq!(config.max_delay_ms, 30000); // default
    assert_eq!(config.timeout_seconds, 10); // default

    // Clean up
    env::remove_var("API_RETRY_MAX_ATTEMPTS");
    env::remove_var("API_RETRY_BASE_DELAY_MS");
}

// RetryClient Tests
#[test]
fn test_retry_client_creation() {
    let config = RetryConfig::for_tests();
    let retry_client = RetryClient::new(config.clone());

    assert_eq!(retry_client.config().max_attempts, config.max_attempts);
    assert_eq!(retry_client.config().base_delay_ms, config.base_delay_ms);
    assert_eq!(retry_client.config().max_delay_ms, config.max_delay_ms);
    assert_eq!(
        retry_client.config().timeout_seconds,
        config.timeout_seconds
    );
}

#[test]
fn test_retry_client_with_default_config() {
    let retry_client = RetryClient::with_default_config();
    let default_config = RetryConfig::default();

    assert_eq!(
        retry_client.config().max_attempts,
        default_config.max_attempts
    );
    assert_eq!(
        retry_client.config().base_delay_ms,
        default_config.base_delay_ms
    );
    assert_eq!(
        retry_client.config().max_delay_ms,
        default_config.max_delay_ms
    );
    assert_eq!(
        retry_client.config().timeout_seconds,
        default_config.timeout_seconds
    );
}

#[test]
fn test_retry_client_for_tests() {
    let retry_client = RetryClient::for_tests();
    let test_config = RetryConfig::for_tests();

    assert_eq!(retry_client.config().max_attempts, test_config.max_attempts);
    assert_eq!(
        retry_client.config().base_delay_ms,
        test_config.base_delay_ms
    );
    assert_eq!(retry_client.config().max_delay_ms, test_config.max_delay_ms);
    assert_eq!(
        retry_client.config().timeout_seconds,
        test_config.timeout_seconds
    );
}

#[test]
fn test_retry_client_calculate_backoff_delay() {
    let config = RetryConfig {
        max_attempts: 3,
        base_delay_ms: 1000,
        max_delay_ms: 10000,
        timeout_seconds: 10,
    };
    let retry_client = RetryClient::new(config);

    // Test first attempt (should be base_delay + jitter)
    let delay1 = retry_client.calculate_backoff_delay(1);
    assert!(delay1.as_millis() >= 1000); // At least base delay
    assert!(delay1.as_millis() <= 1500); // Base delay + max jitter (base/2)

    // Test second attempt (should be 2 * base_delay + jitter)
    let delay2 = retry_client.calculate_backoff_delay(2);
    assert!(delay2.as_millis() >= 2000); // At least 2 * base delay
    assert!(delay2.as_millis() <= 2500); // 2 * base delay + max jitter

    // Test third attempt (should be 4 * base_delay + jitter)
    let delay3 = retry_client.calculate_backoff_delay(3);
    assert!(delay3.as_millis() >= 4000); // At least 4 * base delay
    assert!(delay3.as_millis() <= 4500); // 4 * base delay + max jitter

    // Test that delay is capped at max_delay_ms
    let delay_large = retry_client.calculate_backoff_delay(10);
    assert_eq!(delay_large.as_millis(), 10000); // Should be capped at max_delay_ms
}

#[test]
fn test_retry_client_calculate_backoff_delay_with_small_max() {
    let config = RetryConfig {
        max_attempts: 5,
        base_delay_ms: 1000,
        max_delay_ms: 2000, // Small max delay to test capping
        timeout_seconds: 10,
    };
    let retry_client = RetryClient::new(config);

    // Test that exponential backoff is capped properly
    let delay3 = retry_client.calculate_backoff_delay(3);
    assert_eq!(delay3.as_millis(), 2000); // Should be capped at max_delay_ms

    let delay4 = retry_client.calculate_backoff_delay(4);
    assert_eq!(delay4.as_millis(), 2000); // Should still be capped
}

#[test]
fn test_retry_client_extract_retry_after() {
    use reqwest::header::{HeaderMap, HeaderValue};

    let config = RetryConfig::for_tests();
    let _retry_client = RetryClient::new(config);

    // Create a mock response with Retry-After header
    let mut headers = HeaderMap::new();
    headers.insert("retry-after", HeaderValue::from_static("120"));

    // We can't easily create a reqwest::Response for testing, so we'll test the logic
    // by creating a simple test that verifies the header parsing logic would work
    let retry_after_value = headers
        .get("retry-after")
        .and_then(|value| value.to_str().ok())
        .and_then(|s| s.parse::<u64>().ok());

    assert_eq!(retry_after_value, Some(120));

    // Test invalid header value
    let mut headers_invalid = HeaderMap::new();
    headers_invalid.insert("retry-after", HeaderValue::from_static("invalid"));

    let retry_after_invalid = headers_invalid
        .get("retry-after")
        .and_then(|value| value.to_str().ok())
        .and_then(|s| s.parse::<u64>().ok());

    assert_eq!(retry_after_invalid, None);

    // Test missing header
    let headers_empty = HeaderMap::new();
    let retry_after_missing = headers_empty
        .get("retry-after")
        .and_then(|value| value.to_str().ok())
        .and_then(|s| s.parse::<u64>().ok());

    assert_eq!(retry_after_missing, None);
}

#[test]
fn test_retry_client_client_access() {
    let retry_client = RetryClient::for_tests();
    let client = retry_client.client();

    // Verify we can access the underlying client
    assert!(client.get("https://example.com").build().is_ok());
}

#[test]
fn test_retry_client_config_access() {
    let config = RetryConfig {
        max_attempts: 5,
        base_delay_ms: 2000,
        max_delay_ms: 20000,
        timeout_seconds: 15,
    };
    let retry_client = RetryClient::new(config.clone());

    let retrieved_config = retry_client.config();
    assert_eq!(retrieved_config.max_attempts, config.max_attempts);
    assert_eq!(retrieved_config.base_delay_ms, config.base_delay_ms);
    assert_eq!(retrieved_config.max_delay_ms, config.max_delay_ms);
    assert_eq!(retrieved_config.timeout_seconds, config.timeout_seconds);
}

#[test]
fn test_retry_client_clone() {
    let config = RetryConfig::for_tests();
    let retry_client = RetryClient::new(config);
    let cloned_client = retry_client.clone();

    // Verify the clone has the same configuration
    assert_eq!(
        retry_client.config().max_attempts,
        cloned_client.config().max_attempts
    );
    assert_eq!(
        retry_client.config().base_delay_ms,
        cloned_client.config().base_delay_ms
    );
    assert_eq!(
        retry_client.config().max_delay_ms,
        cloned_client.config().max_delay_ms
    );
    assert_eq!(
        retry_client.config().timeout_seconds,
        cloned_client.config().timeout_seconds
    );
}

#[tokio::test]
async fn test_retry_client_with_mock_server() {
    let server = MockServer::start();

    // Test successful request (no retries needed)
    let success_mock = server.mock(|when, then| {
        when.method(GET).path("/success");
        then.status(200).body("success");
    });

    let retry_client = RetryClient::for_tests();
    let url = format!("{}/success", server.base_url());

    let result = retry_client
        .execute_with_retry(|| retry_client.client().get(&url))
        .await;

    assert!(result.is_ok());
    let response = result.unwrap();
    assert_eq!(response.status(), 200);
    success_mock.assert_hits(1); // Should only be called once
}

#[tokio::test]
async fn test_retry_client_server_error_retry() {
    let server = MockServer::start();

    // Test server error - this will always return 500
    // We'll verify that it retries by checking the hit count
    let _server_error_mock = server.mock(|when, then| {
        when.method(GET).path("/server-error");
        then.status(500).body("server error");
    });

    let retry_client = RetryClient::for_tests();
    let url = format!("{}/server-error", server.base_url());

    let result = retry_client
        .execute_with_retry(|| retry_client.client().get(&url))
        .await;

    // Should fail after retries are exhausted
    assert!(result.is_err());
    match result.unwrap_err() {
        TokenError::ObtainFailed {
            attempts,
            last_error,
        } => {
            assert_eq!(attempts, 2); // Should have tried max_attempts times
            assert!(last_error.contains("Server error: 500"));
        }
        _ => panic!("Expected ObtainFailed error"),
    }

    // Verify it was called the expected number of times
    _server_error_mock.assert_hits(2); // Should be called max_attempts times
}

#[tokio::test]
async fn test_retry_client_rate_limit_handling() {
    let server = MockServer::start();

    // Test rate limiting (429) response
    let rate_limit_mock = server.mock(|when, then| {
        when.method(GET).path("/rate-limited");
        then.status(429)
            .header("retry-after", "1") // 1 second retry
            .body("rate limited");
    });

    let retry_client = RetryClient::for_tests();
    let url = format!("{}/rate-limited", server.base_url());

    let start_time = std::time::Instant::now();
    let result = retry_client
        .execute_with_retry(|| retry_client.client().get(&url))
        .await;

    let elapsed = start_time.elapsed();

    // Should fail with rate limit error after all retries
    assert!(result.is_err());
    match result.unwrap_err() {
        TokenError::RateLimited {
            retry_after_seconds,
        } => {
            assert_eq!(retry_after_seconds, 1);
        }
        _ => panic!("Expected RateLimited error"),
    }

    // Should have waited for rate limit delays
    assert!(elapsed.as_secs() >= 1); // At least 1 second for the rate limit delay

    // Should have been called max_attempts times
    rate_limit_mock.assert_hits(2); // for_tests() config has max_attempts = 2
}

#[tokio::test]
async fn test_retry_client_non_retryable_client_error() {
    let server = MockServer::start();

    // Test non-retryable client error (404)
    let not_found_mock = server.mock(|when, then| {
        when.method(GET).path("/not-found");
        then.status(404).body("not found");
    });

    let retry_client = RetryClient::for_tests();
    let url = format!("{}/not-found", server.base_url());

    let result = retry_client
        .execute_with_retry(|| retry_client.client().get(&url))
        .await;

    // Should fail immediately without retries for 404
    assert!(result.is_err());
    match result.unwrap_err() {
        TokenError::ObtainFailed {
            attempts,
            last_error,
        } => {
            assert_eq!(attempts, 1); // Should not retry
            assert!(last_error.contains("Client error: 404"));
        }
        _ => panic!("Expected ObtainFailed error"),
    }

    not_found_mock.assert_hits(1); // Should only be called once
}

#[tokio::test]
async fn test_retry_client_timeout_handling() {
    let server = MockServer::start();

    // Test timeout scenario
    let _timeout_mock = server.mock(|when, then| {
        when.method(GET).path("/timeout");
        then.status(200)
            .delay(StdDuration::from_secs(10)) // Delay longer than timeout
            .body("delayed response");
    });

    let config = RetryConfig {
        max_attempts: 2,
        base_delay_ms: 100,
        max_delay_ms: 1000,
        timeout_seconds: 1, // Very short timeout
    };
    let retry_client = RetryClient::new(config);
    let url = format!("{}/timeout", server.base_url());

    let start_time = std::time::Instant::now();
    let result = retry_client
        .execute_with_retry(|| retry_client.client().get(&url))
        .await;

    let elapsed = start_time.elapsed();

    // Should fail with timeout error
    assert!(result.is_err());
    let error = result.unwrap_err();
    match error {
        TokenError::Timeout { timeout_seconds } => {
            assert_eq!(timeout_seconds, 1);
        }
        _ => panic!("Expected Timeout error, got: {:?}", error),
    }

    // Should have timed out relatively quickly (within a few seconds)
    assert!(elapsed.as_secs() < 5);
}

#[tokio::test]
async fn test_retry_client_exhausted_retries() {
    let server = MockServer::start();

    // Test that all retries are exhausted
    let always_fail_mock = server.mock(|when, then| {
        when.method(GET).path("/always-fail");
        then.status(500).body("server error");
    });

    let retry_client = RetryClient::for_tests(); // max_attempts = 2
    let url = format!("{}/always-fail", server.base_url());

    let result = retry_client
        .execute_with_retry(|| retry_client.client().get(&url))
        .await;

    // Should fail after all retries are exhausted
    assert!(result.is_err());
    match result.unwrap_err() {
        TokenError::ObtainFailed {
            attempts,
            last_error,
        } => {
            assert_eq!(attempts, 2); // Should have tried max_attempts times
            assert!(last_error.contains("Server error: 500"));
        }
        _ => panic!("Expected ObtainFailed error"),
    }

    always_fail_mock.assert_hits(2); // Should be called max_attempts times
}