ig-client 0.16.1

This crate provides a client for the IG Markets API
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
//! Acceptance tests for the API key pool.
//!
//! The pool exists because IG meters its non-trading allowance per API key. The
//! properties asserted here are the ones that make that worth having: work is
//! spread *before* a key is rejected, one sent request costs exactly one token,
//! and the errors that do not belong to a key never burn another one.
//!
//! Driven against a `wiremock::MockServer`, which records the `X-IG-API-KEY` of
//! every request and so shows which key served what.

use ig_client::application::config::{
    Config, Credentials, DatabaseConfig, RateLimiterConfig, RestApiConfig, WebSocketConfig,
};
use ig_client::application::http::HttpClient;
use ig_client::application::rate_limiter::{RateLimitClass, RateLimiter};
use ig_client::error::AppError;
use ig_client::model::retry::RetryConfig;
use serde::Deserialize;
use std::time::{Duration, Instant};
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

/// Minimal DTO for the mock endpoint; the tests care about the traffic, not the
/// payload.
#[derive(Debug, Deserialize)]
struct Dummy {
    #[allow(dead_code)]
    ok: bool,
}

/// Builds a config whose `api_key` is the comma-separated pool under test.
///
/// Budgets here count the login too: a key's session and its data requests
/// share one limiter, because IG meters both against the same per-key
/// allowance. So "one data request" on a fresh key costs two tokens — one for
/// `/session`, one for the data — and the tests size `max_requests`
/// accordingly.
fn pool_config(base_url: &str, keys: &str, max_requests: u32, period_seconds: u64) -> Config {
    pool_config_burst(base_url, keys, max_requests, period_seconds, max_requests)
}

/// Same as [`pool_config`] with an explicit burst size.
///
/// The burst is the bucket's capacity, so it decides how many tokens can be
/// held at once: with a burst of 1 a key can never have a login and a data
/// request ready together, no matter how large the per-period budget is.
fn pool_config_burst(
    base_url: &str,
    keys: &str,
    max_requests: u32,
    period_seconds: u64,
    burst_size: u32,
) -> Config {
    Config {
        credentials: Credentials {
            username: "fake-user".to_string(),
            password: "fake-pass".to_string(),
            account_id: "ABC12".to_string(),
            api_key: keys.to_string(),
            client_token: None,
            account_token: None,
        },
        rest_api: RestApiConfig {
            base_url: base_url.to_string(),
            timeout: 30,
        },
        websocket: WebSocketConfig {
            url: "wss://example.invalid".to_string(),
            reconnect_interval: 5,
        },
        database: DatabaseConfig {
            url: "postgres://localhost/none".to_string(),
            max_connections: 1,
        },
        rate_limiter: RateLimiterConfig {
            max_requests,
            period_seconds,
            burst_size,
        },
        sleep_hours: 1,
        page_size: 20,
        days_to_look_back: 7,
        api_version: Some(3),
    }
}

/// Mounts a `/session` endpoint that always authenticates.
async fn mount_login_ok(server: &MockServer) {
    Mock::given(method("POST"))
        .and(path("/session"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "clientId": "FAKE-CLIENT-1",
            "accountId": "ABC12",
            "timezoneOffset": 1,
            "lightstreamerEndpoint": "https://example.invalid",
            "oauthToken": {
                "access_token": "FAKE-ACCESS",
                "refresh_token": "FAKE-REFRESH",
                "scope": "profile",
                "token_type": "Bearer",
                "expires_in": "600"
            }
        })))
        .mount(server)
        .await;
}

/// Mounts the data endpoint used by every test, always succeeding.
async fn mount_data_ok(server: &MockServer) {
    Mock::given(method("GET"))
        .and(path("/data"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"ok": true})))
        .mount(server)
        .await;
}

/// The `X-IG-API-KEY` of every `/data` request the server received, in order.
async fn keys_used(server: &MockServer) -> Vec<String> {
    server
        .received_requests()
        .await
        .unwrap_or_default()
        .iter()
        .filter(|r| r.url.path() == "/data")
        .map(|r| {
            r.headers
                .get("X-IG-API-KEY")
                .and_then(|v| v.to_str().ok())
                .unwrap_or_default()
                .to_string()
        })
        .collect()
}

/// Two keys with one token each: both requests go out at once, on different
/// keys. A pool that waited for the first key instead of moving to the second
/// would serialise them.
#[tokio::test]
async fn test_pool_two_keys_first_two_requests_use_distinct_keys_immediately() {
    let server = MockServer::start().await;
    mount_login_ok(&server).await;
    mount_data_ok(&server).await;

    // Capacity for exactly a login plus one data request per key, so a second
    // data request on the same key would have to wait for a refill.
    let client = HttpClient::new_lazy(pool_config_burst(&server.uri(), "key-a,key-b", 2, 60, 2))
        .expect("client builds");

    let started = Instant::now();
    client.get::<Dummy>("/data", Some(1)).await.expect("first");
    client.get::<Dummy>("/data", Some(1)).await.expect("second");
    let elapsed = started.elapsed();

    let used = keys_used(&server).await;
    assert_eq!(used.len(), 2, "both requests were sent");
    assert_ne!(used[0], used[1], "the two requests used different keys");
    assert!(
        elapsed < Duration::from_secs(5),
        "neither request waited on a refill, took {elapsed:?}"
    );
}

/// The third request has no token anywhere, so it must wait for a refill. With
/// a 60 s period and one token per key, it cannot complete quickly.
#[tokio::test]
async fn test_pool_third_request_waits_for_the_first_refill() {
    let server = MockServer::start().await;
    mount_login_ok(&server).await;
    mount_data_ok(&server).await;

    // Login plus exactly one data request per key; nothing left for a third.
    let client = HttpClient::new_lazy(pool_config_burst(&server.uri(), "key-a,key-b", 2, 60, 2))
        .expect("client builds");

    client.get::<Dummy>("/data", Some(1)).await.expect("first");
    client.get::<Dummy>("/data", Some(1)).await.expect("second");

    // Both buckets are empty now; the third must block rather than be sent.
    let third = tokio::time::timeout(
        Duration::from_millis(300),
        client.get::<Dummy>("/data", Some(1)),
    )
    .await;

    assert!(third.is_err(), "the third request waited for a refill");
    assert_eq!(
        keys_used(&server).await.len(),
        2,
        "no third request reached the server while waiting"
    );
}

/// One sent request costs exactly one token.
///
/// This is the reserve-then-wait bug made visible: selection took a cell with
/// `check()` and the send took another with `until_ready()`, so every request
/// cost two and the effective rate was half the configured one. Counted
/// directly on the bucket rather than by timing, so the assertion is exact.
#[tokio::test]
async fn test_send_with_reservation_consumes_exactly_one_token() {
    let server = MockServer::start().await;
    mount_data_ok(&server).await;

    // Three tokens available at once, none refilling during the test.
    let limiter = RateLimiter::new(&RateLimiterConfig {
        max_requests: 3,
        period_seconds: 600,
        burst_size: 3,
    });

    // Reserve as the pool does, then send saying the token is already held.
    assert!(limiter.try_reserve(RateLimitClass::NonTrading), "token 1");
    let response = ig_client::application::http::make_http_request_reserved(
        &reqwest::Client::new(),
        &limiter,
        reqwest::Method::GET,
        &format!("{}/data", server.uri()),
        vec![],
        &None::<()>,
        RetryConfig {
            max_retry_count: Some(0),
            retry_delay_secs: Some(0),
        },
        true,
    )
    .await;
    assert!(response.is_ok(), "the request was sent");

    // Two of the three tokens must remain: the send spent none of its own.
    assert!(
        limiter.try_reserve(RateLimitClass::NonTrading),
        "second token still available"
    );
    assert!(
        limiter.try_reserve(RateLimitClass::NonTrading),
        "third token still available - the send did not take a second one"
    );
    assert!(
        !limiter.try_reserve(RateLimitClass::NonTrading),
        "and the budget is now genuinely spent"
    );
}

/// A failed login must cost only the login request. If the data token were
/// reserved first, it would be spent on a request that is never sent.
#[tokio::test]
async fn test_pool_failed_login_does_not_consume_a_data_token() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/session"))
        .respond_with(ResponseTemplate::new(401).set_body_json(serde_json::json!({
            "errorCode": "error.security.invalid-details"
        })))
        .mount(&server)
        .await;
    mount_data_ok(&server).await;

    // Capacity for the failed login, the retried login and one data request. If
    // the failed attempt also took a data token, the budget would not cover the
    // request that follows it.
    let client = HttpClient::new_lazy(pool_config_burst(&server.uri(), "key-a", 3, 60, 3))
        .expect("client builds");

    let failed = client.get::<Dummy>("/data", Some(1)).await;
    assert!(failed.is_err(), "the request fails because login fails");
    assert!(
        keys_used(&server).await.is_empty(),
        "no data request was sent"
    );

    // The token survived the failed login, so a working login can spend it now
    // without waiting for a refill.
    server.reset().await;
    mount_login_ok(&server).await;
    mount_data_ok(&server).await;

    let started = Instant::now();
    let second = tokio::time::timeout(
        Duration::from_secs(5),
        client.get::<Dummy>("/data", Some(1)),
    )
    .await;
    assert!(
        second.is_ok(),
        "the data token was not spent by the failed login, waited {:?}",
        started.elapsed()
    );
}

/// Concurrent requests spread over the pool instead of stacking on the first
/// key. Without the round-robin cursor every caller starts its scan at index 0.
#[tokio::test]
async fn test_pool_concurrent_requests_spread_across_keys() {
    let server = MockServer::start().await;
    mount_login_ok(&server).await;
    mount_data_ok(&server).await;

    let client = std::sync::Arc::new(
        HttpClient::new_lazy(pool_config_burst(
            &server.uri(),
            "key-a,key-b,key-c,key-d",
            2,
            60,
            2,
        ))
        .expect("client builds"),
    );

    let mut tasks = Vec::new();
    for _ in 0..4 {
        let client = client.clone();
        tasks.push(tokio::spawn(async move {
            client
                .get::<Dummy>("/data", Some(1))
                .await
                .map(|_: Dummy| ())
        }));
    }
    for task in tasks {
        let _ = task.await;
    }

    let mut used = keys_used(&server).await;
    used.sort();
    used.dedup();
    assert_eq!(
        used.len(),
        4,
        "four concurrent requests used four distinct keys, got {used:?}"
    );
}

/// When every key is empty the pool waits on all of them and takes whichever
/// refills first, rather than parking on an arbitrary one. Key B refills in 1 s
/// while key A would need 60 s, so the wait must be the short one.
#[tokio::test]
async fn test_rate_limiter_waits_for_the_key_that_refills_first() {
    let slow = RateLimiter::new(&RateLimiterConfig {
        max_requests: 1,
        period_seconds: 60,
        burst_size: 1,
    });
    let fast = RateLimiter::new(&RateLimiterConfig {
        max_requests: 1,
        period_seconds: 1,
        burst_size: 1,
    });

    // Drain both buckets.
    assert!(slow.try_reserve(RateLimitClass::NonTrading));
    assert!(fast.try_reserve(RateLimitClass::NonTrading));

    let started = Instant::now();
    let waits: Vec<std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>>> = vec![
        Box::pin(async move { slow.reserve(RateLimitClass::NonTrading).await }),
        Box::pin(async move { fast.reserve(RateLimitClass::NonTrading).await }),
    ];
    let (_, _, _) = futures::future::select_all(waits).await;

    assert!(
        started.elapsed() < Duration::from_secs(10),
        "the wait ended on the fast bucket, took {:?}",
        started.elapsed()
    );
}

/// A per-key allowance rejection moves to another key straight away, and the
/// request succeeds there.
#[tokio::test]
async fn test_pool_api_key_allowance_rotates_immediately() {
    let server = MockServer::start().await;
    mount_login_ok(&server).await;

    // First data call is rejected for the key's allowance; the retry on the
    // other key succeeds.
    Mock::given(method("GET"))
        .and(path("/data"))
        .respond_with(ResponseTemplate::new(403).set_body_json(serde_json::json!({
            "errorCode": "error.public-api.exceeded-api-key-allowance"
        })))
        .up_to_n_times(1)
        .mount(&server)
        .await;
    mount_data_ok(&server).await;

    let client = HttpClient::new_lazy(pool_config(&server.uri(), "key-a,key-b", 5, 1))
        .expect("client builds");

    let result = client.get::<Dummy>("/data", Some(1)).await;
    assert!(result.is_ok(), "the request succeeded on another key");

    let used = keys_used(&server).await;
    assert_eq!(used.len(), 2, "one rejection plus one success");
    assert_ne!(used[0], used[1], "the retry went to a different key");
}

/// The account-wide allowance belongs to the account every key authenticates,
/// so it must surface as its own error without spending a second key.
#[tokio::test]
async fn test_pool_account_allowance_does_not_rotate() {
    let server = MockServer::start().await;
    mount_login_ok(&server).await;
    Mock::given(method("GET"))
        .and(path("/data"))
        .respond_with(ResponseTemplate::new(403).set_body_json(serde_json::json!({
            "errorCode": "error.public-api.exceeded-account-allowance"
        })))
        .mount(&server)
        .await;

    let client = HttpClient::new_lazy(pool_config(&server.uri(), "key-a,key-b", 5, 1))
        .expect("client builds");

    let err = client
        .get::<Dummy>("/data", Some(1))
        .await
        .expect_err("account allowance is an error");
    assert!(
        matches!(err, AppError::AccountAllowanceExceeded),
        "got {err:?}"
    );

    let used = keys_used(&server).await;
    assert!(
        used.iter().collect::<std::collections::HashSet<_>>().len() <= 1,
        "the account allowance did not burn a second key, used {used:?}"
    );
}

/// Trading traffic stays pinned to one key: the trading allowance is metered
/// against the account, so rotating buys nothing and would scatter order
/// traffic over sessions.
#[tokio::test]
async fn test_pool_trading_allowance_does_not_rotate() {
    let server = MockServer::start().await;
    mount_login_ok(&server).await;
    Mock::given(method("POST"))
        .and(path("/positions/otc"))
        .respond_with(ResponseTemplate::new(403).set_body_json(serde_json::json!({
            "errorCode": "error.public-api.exceeded-account-trading-allowance"
        })))
        .mount(&server)
        .await;

    let client = HttpClient::new_lazy(pool_config(&server.uri(), "key-a,key-b", 5, 1))
        .expect("client builds");

    let err = client
        .post::<_, Dummy>("/positions/otc", serde_json::json!({}), Some(2))
        .await
        .expect_err("trading allowance is an error");
    assert!(
        matches!(err, AppError::TradingAllowanceExceeded),
        "got {err:?}"
    );

    let orders: Vec<String> = server
        .received_requests()
        .await
        .unwrap_or_default()
        .iter()
        .filter(|r| r.url.path() == "/positions/otc")
        .map(|r| {
            r.headers
                .get("X-IG-API-KEY")
                .and_then(|v| v.to_str().ok())
                .unwrap_or_default()
                .to_string()
        })
        .collect();
    assert_eq!(orders.len(), 1, "the order was sent once, got {orders:?}");
}

/// A single key behaves exactly as before the pool existed: requests go out on
/// that key, paced by its budget.
#[tokio::test]
async fn test_pool_single_key_keeps_previous_behaviour() {
    let server = MockServer::start().await;
    mount_login_ok(&server).await;
    mount_data_ok(&server).await;

    let client =
        HttpClient::new_lazy(pool_config(&server.uri(), "only-key", 5, 1)).expect("client builds");

    client.get::<Dummy>("/data", Some(1)).await.expect("first");
    client.get::<Dummy>("/data", Some(1)).await.expect("second");

    let used = keys_used(&server).await;
    assert_eq!(used, vec!["only-key".to_string(), "only-key".to_string()]);
}

/// A per-key allowance rejection *during login* rotates at once, instead of
/// spending three backoffs on a key that has already said it is empty.
#[tokio::test]
async fn test_pool_key_allowance_during_login_rotates_without_backoff() {
    let server = MockServer::start().await;

    // The first login attempt is refused for the key's allowance; the next one
    // succeeds. If the client retried the same key, this test would take the
    // backoff (tens of seconds) instead of finishing immediately.
    Mock::given(method("POST"))
        .and(path("/session"))
        .respond_with(ResponseTemplate::new(403).set_body_json(serde_json::json!({
            "errorCode": "error.public-api.exceeded-api-key-allowance"
        })))
        .up_to_n_times(1)
        .mount(&server)
        .await;
    mount_login_ok(&server).await;
    mount_data_ok(&server).await;

    let client = HttpClient::new_lazy(pool_config_burst(&server.uri(), "key-a,key-b", 4, 60, 4))
        .expect("client builds");

    let started = Instant::now();
    let result = tokio::time::timeout(
        Duration::from_secs(8),
        client.get::<Dummy>("/data", Some(1)),
    )
    .await;

    assert!(
        matches!(result, Ok(Ok(_))),
        "the request succeeded on another key, got {result:?}"
    );
    assert!(
        started.elapsed() < Duration::from_secs(8),
        "no backoff was paid on the refused key, took {:?}",
        started.elapsed()
    );

    let used = keys_used(&server).await;
    assert_eq!(used.len(), 1, "exactly one data request was sent");
}

/// Two consecutive trading calls use the same slot. Order traffic is metered
/// against the account, so spreading it buys nothing and would scatter a
/// position's requests over sessions a reconciliation then has to match up.
#[tokio::test]
async fn test_pool_consecutive_trading_requests_use_the_same_slot() {
    let server = MockServer::start().await;
    mount_login_ok(&server).await;
    Mock::given(method("POST"))
        .and(path("/positions/otc"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"ok": true})))
        .mount(&server)
        .await;

    let client = HttpClient::new_lazy(pool_config_burst(
        &server.uri(),
        "key-a,key-b,key-c",
        20,
        1,
        20,
    ))
    .expect("client builds");

    for _ in 0..2 {
        client
            .post::<_, Dummy>("/positions/otc", serde_json::json!({}), Some(2))
            .await
            .expect("order accepted");
    }

    let orders: Vec<String> = server
        .received_requests()
        .await
        .unwrap_or_default()
        .iter()
        .filter(|r| r.url.path() == "/positions/otc")
        .map(|r| {
            r.headers
                .get("X-IG-API-KEY")
                .and_then(|v| v.to_str().ok())
                .unwrap_or_default()
                .to_string()
        })
        .collect();

    assert_eq!(orders.len(), 2, "both orders were sent");
    assert_eq!(
        orders[0], orders[1],
        "both used the same key, got {orders:?}"
    );
}

/// Serving one request must not authenticate the whole pool. With a burst of 1
/// a key holds at most one token, so a selection that probes every key with
/// `get_session` would log every one of them in.
#[tokio::test]
async fn test_pool_first_request_does_not_authenticate_every_key() {
    let server = MockServer::start().await;
    mount_login_ok(&server).await;
    mount_data_ok(&server).await;

    let client = HttpClient::new_lazy(pool_config_burst(
        &server.uri(),
        "key-a,key-b,key-c,key-d,key-e",
        10,
        1,
        1,
    ))
    .expect("client builds");

    client.get::<Dummy>("/data", Some(1)).await.expect("first");

    let logins = server
        .received_requests()
        .await
        .unwrap_or_default()
        .iter()
        .filter(|r| r.url.path() == "/session")
        .count();

    // One login for the key that serves it. A second is possible when that key
    // turns out to have no token and the wait is won by another key, but the
    // pool must never authenticate all five to serve one request.
    assert!(
        logins <= 2,
        "one request did not authenticate the pool, got {logins} logins"
    );
}

/// When IG refuses the last key too, that key is marked as well: the pool must
/// not hand the next request straight back to a key that has just said it is
/// empty.
#[tokio::test]
async fn test_pool_last_key_is_also_marked_on_allowance() {
    let server = MockServer::start().await;
    mount_login_ok(&server).await;
    Mock::given(method("GET"))
        .and(path("/data"))
        .respond_with(ResponseTemplate::new(403).set_body_json(serde_json::json!({
            "errorCode": "error.public-api.exceeded-api-key-allowance"
        })))
        .mount(&server)
        .await;

    let client = HttpClient::new_lazy(pool_config_burst(&server.uri(), "key-a,key-b", 20, 1, 20))
        .expect("client builds");

    let err = client
        .get::<Dummy>("/data", Some(1))
        .await
        .expect_err("every key refused");
    assert!(
        matches!(err, AppError::ApiKeyAllowanceExceeded),
        "got {err:?}"
    );

    let after_first = keys_used(&server).await.len();
    assert_eq!(after_first, 2, "both keys were tried once");

    // Both are now in cooldown, so a second call must not send anything more
    // while it waits for one of them to come back.
    let second = tokio::time::timeout(
        Duration::from_millis(300),
        client.get::<Dummy>("/data", Some(1)),
    )
    .await;
    assert!(second.is_err(), "the second call waited on the cooldown");
    assert_eq!(
        keys_used(&server).await.len(),
        after_first,
        "no request was sent to a key still in cooldown"
    );
}

/// The account allowance is spent for every key at once, so retrying it only
/// burns more of an allowance that is already gone: exactly one request.
#[tokio::test]
async fn test_account_allowance_sends_exactly_one_request() {
    let server = MockServer::start().await;
    mount_login_ok(&server).await;
    Mock::given(method("GET"))
        .and(path("/data"))
        .respond_with(ResponseTemplate::new(403).set_body_json(serde_json::json!({
            "errorCode": "error.public-api.exceeded-account-allowance"
        })))
        .mount(&server)
        .await;

    let client = HttpClient::new_lazy(pool_config_burst(&server.uri(), "key-a", 20, 1, 20))
        .expect("client builds");

    let err = client
        .get::<Dummy>("/data", Some(1))
        .await
        .expect_err("account allowance is an error");
    assert!(
        matches!(err, AppError::AccountAllowanceExceeded),
        "got {err:?}"
    );
    assert_eq!(
        keys_used(&server).await.len(),
        1,
        "one request, no retries against an exhausted account budget"
    );
}

/// An expired OAuth token on a key other than the first refreshes *that* key's
/// session. Refreshing slot 0 instead would replay the same rejected token.
#[tokio::test]
async fn test_pool_oauth_refresh_targets_the_failing_slot() {
    let server = MockServer::start().await;
    mount_login_ok(&server).await;

    // First data call is answered with an invalidated OAuth token, the next one
    // succeeds. Two keys with one data token each force the first call onto one
    // key and make the replay observable.
    Mock::given(method("GET"))
        .and(path("/data"))
        .respond_with(ResponseTemplate::new(401).set_body_json(serde_json::json!({
            "errorCode": "error.security.oauth-token-invalid"
        })))
        .up_to_n_times(1)
        .mount(&server)
        .await;
    mount_data_ok(&server).await;

    let client = HttpClient::new_lazy(pool_config_burst(&server.uri(), "key-a,key-b", 20, 1, 20))
        .expect("client builds");

    let result = client.get::<Dummy>("/data", Some(1)).await;
    assert!(result.is_ok(), "the replay succeeded, got {result:?}");

    let used = keys_used(&server).await;
    assert_eq!(used.len(), 2, "the rejected call plus its replay");
    assert_eq!(
        used[0], used[1],
        "the replay stayed on the key whose token was refreshed, got {used:?}"
    );
}

/// The pool paces against an account-wide budget on top of the per-key ones.
/// Without it, N keys would pace N times what the account allows.
#[tokio::test]
async fn test_pool_respects_an_account_wide_budget() {
    let server = MockServer::start().await;
    mount_login_ok(&server).await;
    mount_data_ok(&server).await;

    // Ten keys with a generous per-key budget: the per-key limiters would let
    // far more through than the account ceiling of 30/minute allows.
    let keys: Vec<String> = (0..10).map(|i| format!("key-{i}")).collect();
    let client = HttpClient::new_lazy(pool_config_burst(
        &server.uri(),
        &keys.join(","),
        100,
        60,
        100,
    ))
    .expect("client builds");

    let mut sent = 0;
    for _ in 0..40 {
        match tokio::time::timeout(
            Duration::from_millis(50),
            client.get::<Dummy>("/data", Some(1)),
        )
        .await
        {
            Ok(Ok(_)) => sent += 1,
            _ => break,
        }
    }

    assert!(
        sent <= 30,
        "the account ceiling capped the burst at 30/minute, sent {sent}"
    );
    assert!(sent > 0, "the pool still served requests");
}