projectx-client 6.0.0

An async, provider-native Rust client for the ProjectX Gateway 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
// SPDX-FileCopyrightText: 2026 Kevin Monaghan
// SPDX-License-Identifier: MIT-0

//! Public-API contract tests using synthetic local fixtures.

use std::time::Duration;

use httpmock::prelude::*;
use projectx_client::{
    AccountId, ApplicationCredentials, BarUnit, Bracket, CancelOrder, Client, ContractId,
    Credentials, Decimal, Endpoints, Error, HistoryRequest, ModifyOrder, OrderId, OrderStatus,
    OrderType, PartialCloseContract, PlaceOrder, RequestValidationError, Side, Timestamp,
    TradeLogType,
};
use serde_json::json;

fn fixture_client(server: &MockServer) -> Client {
    let credentials = Credentials::new("synthetic-user", "synthetic-key")
        .unwrap_or_else(|error| panic!("synthetic credentials must be valid: {error}"));
    let endpoints = Endpoints::custom(&server.base_url(), &server.base_url())
        .unwrap_or_else(|error| panic!("fixture URL must be valid: {error}"));
    Client::builder(credentials)
        .endpoints(endpoints)
        .build()
        .unwrap_or_else(|error| panic!("fixture client must build: {error}"))
}

#[tokio::test]
async fn authenticates_then_searches_active_accounts() {
    let server = MockServer::start_async().await;
    let login = server
        .mock_async(|when, then| {
            when.method(POST)
                .path("/api/Auth/loginKey")
                .header("accept", "text/plain")
                .json_body(json!({
                    "userName": "synthetic-user",
                    "apiKey": "synthetic-key"
                }));
            then.status(200).json_body(json!({
                "success": true,
                "errorCode": 0,
                "token": "synthetic-token"
            }));
        })
        .await;
    let accounts = server
        .mock_async(|when, then| {
            when.method(POST)
                .path("/api/Account/search")
                .header("authorization", "Bearer synthetic-token")
                .json_body(json!({"onlyActiveAccounts": true}));
            then.status(200).json_body(json!({
                "accounts": [{
                    "id": 42,
                    "name": "SYNTHETIC",
                    "balance": 1234.50,
                    "canTrade": true,
                    "isVisible": true,
                    "simulated": true
                }],
                "success": true,
                "errorCode": 0,
                "errorMessage": null
            }));
        })
        .await;
    let all_accounts = server
        .mock_async(|when, then| {
            when.method(POST)
                .path("/api/Account/search")
                .header("authorization", "Bearer synthetic-token")
                .json_body(json!({"onlyActiveAccounts": false}));
            then.status(200).json_body(json!({
                "accounts": [],
                "success": true,
                "errorCode": 0
            }));
        })
        .await;

    let client = fixture_client(&server);
    client
        .authenticate()
        .await
        .unwrap_or_else(|error| panic!("fixture login must succeed: {error}"));
    let result = client
        .search_active_accounts()
        .await
        .unwrap_or_else(|error| panic!("fixture account search must succeed: {error}"));
    let all = client
        .search_accounts(false)
        .await
        .unwrap_or_else(|error| panic!("fixture all-account search must succeed: {error}"));

    login.assert_async().await;
    accounts.assert_async().await;
    all_accounts.assert_async().await;
    let rows = result
        .into_listed()
        .unwrap_or_else(|| panic!("fixture account search must list explicitly"));
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0].id.get(), 42);
    assert_eq!(rows[0].balance, Some(Decimal::new(123_450, 2)));
    assert!(all.is_explicitly_empty());
}

#[tokio::test]
async fn application_authentication_ping_and_logout_match_provider_contracts() {
    let server = MockServer::start_async().await;
    let ping = server
        .mock_async(|when, then| {
            when.method(GET).path("/api/Status/ping");
            then.status(200).body("pong");
        })
        .await;
    let login = server
        .mock_async(|when, then| {
            when.method(POST)
                .path("/api/Auth/loginApp")
                .json_body(json!({
                    "userName": "synthetic-app-user",
                    "password": "synthetic-password",
                    "deviceId": "synthetic-device",
                    "appId": "synthetic-app",
                    "verifyKey": "synthetic-verify-key"
                }));
            then.status(200).json_body(json!({
                "success": true,
                "errorCode": 0,
                "token": "synthetic-app-token"
            }));
        })
        .await;
    let logout = server
        .mock_async(|when, then| {
            when.method(POST)
                .path("/api/Auth/logout")
                .header("authorization", "Bearer synthetic-app-token")
                .body("");
            then.status(200)
                .json_body(json!({"success": true, "errorCode": 0}));
        })
        .await;
    let credentials = ApplicationCredentials::builder("synthetic-app-user", "synthetic-password")
        .device_id("synthetic-device")
        .app_id("synthetic-app")
        .verify_key("synthetic-verify-key")
        .build()
        .unwrap_or_else(|error| panic!("fixture application credentials must be valid: {error}"));
    let endpoints = Endpoints::custom(&server.base_url(), &server.base_url())
        .unwrap_or_else(|error| panic!("fixture URL must be valid: {error}"));
    let client = Client::application_builder(credentials)
        .endpoints(endpoints)
        .build()
        .unwrap_or_else(|error| panic!("fixture application client must build: {error}"));

    client
        .ping()
        .await
        .unwrap_or_else(|error| panic!("fixture ping must succeed: {error}"));
    client
        .authenticate()
        .await
        .unwrap_or_else(|error| panic!("fixture application login must succeed: {error}"));
    client
        .logout()
        .await
        .unwrap_or_else(|error| panic!("fixture logout must succeed: {error}"));

    ping.assert_async().await;
    login.assert_async().await;
    logout.assert_async().await;
    assert!(matches!(
        client.search_active_accounts().await,
        Err(Error::NotAuthenticated)
    ));
}

#[tokio::test]
async fn ping_rejects_a_non_exact_status_body() {
    let server = MockServer::start_async().await;
    let ping = server
        .mock_async(|when, then| {
            when.method(GET).path("/api/Status/ping");
            then.status(200).body("pong\n");
        })
        .await;
    let client = fixture_client(&server);

    assert!(matches!(
        client.ping().await,
        Err(Error::UnexpectedStatusResponse)
    ));
    ping.assert_async().await;
}

#[tokio::test]
async fn cancelled_logout_invalidates_only_its_admitted_session() {
    let server = MockServer::start_async().await;
    let _login = server
        .mock_async(|when, then| {
            when.method(POST).path("/api/Auth/loginKey");
            then.status(200)
                .json_body(json!({"success": true, "errorCode": 0, "token": "logout-token"}));
        })
        .await;
    let _logout = server
        .mock_async(|when, then| {
            when.method(POST).path("/api/Auth/logout");
            then.status(200)
                .delay(Duration::from_millis(200))
                .json_body(json!({"success": true, "errorCode": 0}));
        })
        .await;
    let client = fixture_client(&server);
    client
        .authenticate()
        .await
        .unwrap_or_else(|error| panic!("fixture login must succeed: {error}"));

    assert!(
        tokio::time::timeout(Duration::from_millis(20), client.logout())
            .await
            .is_err()
    );
    assert!(matches!(
        client.search_active_accounts().await,
        Err(Error::NotAuthenticated)
    ));
}

#[tokio::test]
async fn provider_rate_limited_logout_retains_the_session() {
    let server = MockServer::start_async().await;
    let _login = server
        .mock_async(|when, then| {
            when.method(POST).path("/api/Auth/loginKey");
            then.status(200)
                .json_body(json!({"success": true, "errorCode": 0, "token": "logout-token"}));
        })
        .await;
    let logout = server
        .mock_async(|when, then| {
            when.method(POST).path("/api/Auth/logout");
            // Zero keeps the shared cooldown clear so the account search proceeds immediately.
            then.status(429).header("retry-after", "0");
        })
        .await;
    let accounts = server
        .mock_async(|when, then| {
            when.method(POST)
                .path("/api/Account/search")
                .header("authorization", "Bearer logout-token");
            then.status(200).json_body(json!({
                "accounts": [],
                "success": true,
                "errorCode": 0
            }));
        })
        .await;
    let client = fixture_client(&server);
    client
        .authenticate()
        .await
        .unwrap_or_else(|error| panic!("fixture login must succeed: {error}"));

    assert!(matches!(
        client.logout().await,
        Err(Error::ProviderRateLimited { .. })
    ));
    client
        .search_active_accounts()
        .await
        .unwrap_or_else(|error| panic!("rate-limited logout must retain the session: {error}"));

    logout.assert_async().await;
    accounts.assert_async().await;
}

#[test]
fn credentials_are_redacted_and_identifiers_validate() {
    let credentials = Credentials::new("private-user", "private-key")
        .unwrap_or_else(|error| panic!("fixture credentials must be valid: {error}"));
    let debug = format!("{credentials:?}");
    assert!(!debug.contains("private-user"));
    assert!(!debug.contains("private-key"));
    assert!(debug.contains("[REDACTED]"));

    let builder = ApplicationCredentials::builder("private-app-user", "private-password")
        .device_id("private-device")
        .app_id("private-app")
        .verify_key("private-verify-key");
    let secrets = [
        "private-app-user",
        "private-password",
        "private-device",
        "private-app",
        "private-verify-key",
    ];
    let builder_debug = format!("{builder:?}");
    for secret in secrets {
        assert!(!builder_debug.contains(secret));
    }
    assert!(builder_debug.contains("[REDACTED]"));
    let application = builder
        .build()
        .unwrap_or_else(|error| panic!("fixture application credentials must be valid: {error}"));
    let application_debug = format!("{application:?}");
    for secret in secrets {
        assert!(!application_debug.contains(secret));
    }
    assert!(application_debug.contains("[REDACTED]"));
    assert!(matches!(
        ApplicationCredentials::builder("private-app-user", "private-password").build(),
        Err(Error::Configuration(_))
    ));
    assert!(matches!(
        ApplicationCredentials::builder(" padded-user ", "private-password")
            .device_id("private-device")
            .app_id("private-app")
            .verify_key("private-verify-key")
            .build(),
        Err(Error::Configuration(_))
    ));

    assert!(matches!(
        AccountId::new(0),
        Err(Error::InvalidIdentifier { .. })
    ));
    assert!(matches!(
        ContractId::new(" padded "),
        Err(Error::InvalidIdentifier { .. })
    ));
}

#[test]
fn unknown_provider_enum_codes_remain_observable() {
    let side: Side = serde_json::from_str("99")
        .unwrap_or_else(|error| panic!("unknown wire code must remain decodable: {error}"));
    assert_eq!(side, Side::Unknown(99));
    assert_eq!(side.code(), 99);
}

#[test]
fn provider_enums_preserve_known_and_future_wire_codes() {
    let stop_limit: OrderType = serde_json::from_str("3")
        .unwrap_or_else(|error| panic!("stop-limit code must decode: {error}"));
    let future_status: OrderStatus = serde_json::from_str("99")
        .unwrap_or_else(|error| panic!("future order status must decode: {error}"));
    let buy: TradeLogType = serde_json::from_str("0")
        .unwrap_or_else(|error| panic!("buy trade-log code must decode: {error}"));
    let sell: TradeLogType = serde_json::from_str("1")
        .unwrap_or_else(|error| panic!("sell trade-log code must decode: {error}"));
    let future_trade: TradeLogType = serde_json::from_str("42")
        .unwrap_or_else(|error| panic!("future trade-log code must decode: {error}"));

    assert_eq!(stop_limit, OrderType::StopLimit);
    for (code, expected) in [
        (0, OrderStatus::None),
        (1, OrderStatus::Open),
        (2, OrderStatus::Filled),
        (3, OrderStatus::Cancelled),
        (4, OrderStatus::Expired),
        (5, OrderStatus::Rejected),
        (6, OrderStatus::Pending),
        (7, OrderStatus::PendingCancellation),
        (8, OrderStatus::Suspended),
    ] {
        let status: OrderStatus = serde_json::from_value(json!(code))
            .unwrap_or_else(|error| panic!("known order-status code must decode: {error}"));
        assert_eq!(status, expected);
        assert_eq!(status.code(), code);
    }
    assert_eq!(future_status, OrderStatus::Unknown(99));
    assert_eq!(buy, TradeLogType::Buy);
    assert_eq!(sell, TradeLogType::Sell);
    assert_eq!(future_trade, TradeLogType::Unknown(42));
    let tick_code = serde_json::to_value(BarUnit::Tick)
        .unwrap_or_else(|error| panic!("tick unit must encode: {error}"));
    assert_eq!(tick_code, json!(7));
    assert!(serde_json::from_str::<BarUnit>("0").is_err());
}

#[test]
fn request_builders_reject_invalid_states() {
    let account_id =
        AccountId::new(42).unwrap_or_else(|error| panic!("fixture account must be valid: {error}"));
    let contract_id = ContractId::new("CON.F.US.MNQ.M26")
        .unwrap_or_else(|error| panic!("fixture contract must be valid: {error}"));
    let order_id =
        OrderId::new(84).unwrap_or_else(|error| panic!("fixture order must be valid: {error}"));

    assert_eq!(
        PlaceOrder::builder(account_id, contract_id, OrderType::Market, Side::Bid, 0,).build(),
        Err(RequestValidationError::NonPositiveOrderSize)
    );
    assert_eq!(
        ModifyOrder::builder(account_id, order_id).build(),
        Err(RequestValidationError::EmptyModification)
    );
    assert_eq!(
        ModifyOrder::builder(account_id, order_id).size(-1).build(),
        Err(RequestValidationError::NonPositiveReplacementSize)
    );
    assert_eq!(
        Bracket::new(0, OrderType::Stop),
        Err(RequestValidationError::NonPositiveBracketTicks)
    );
    assert_eq!(
        Bracket::new(4, OrderType::Unknown(99)),
        Err(RequestValidationError::UnsupportedOrderType { code: 99 })
    );
    assert_eq!(
        Bracket::new(4, OrderType::StopLimit),
        Err(RequestValidationError::UnsupportedOrderType { code: 3 })
    );
    let stop_limit_contract = ContractId::new("CON.F.US.MNQ.M26")
        .unwrap_or_else(|error| panic!("fixture contract must be valid: {error}"));
    assert_eq!(
        PlaceOrder::builder(
            account_id,
            stop_limit_contract,
            OrderType::StopLimit,
            Side::Bid,
            1,
        )
        .build(),
        Err(RequestValidationError::UnsupportedOrderType { code: 3 })
    );
    let unknown_contract = ContractId::new("CON.F.US.MNQ.M26")
        .unwrap_or_else(|error| panic!("fixture contract must be valid: {error}"));
    assert_eq!(
        PlaceOrder::builder(
            account_id,
            unknown_contract,
            OrderType::Market,
            Side::Unknown(7),
            1,
        )
        .build(),
        Err(RequestValidationError::UnsupportedOrderSide { code: 7 })
    );

    let history_contract = ContractId::new("CON.F.US.MNQ.M26")
        .unwrap_or_else(|error| panic!("fixture contract must be valid: {error}"));
    let history = || {
        HistoryRequest::builder(
            history_contract.clone(),
            false,
            Timestamp::new("2026-01-01T00:00:00Z")
                .unwrap_or_else(|error| panic!("fixture timestamp must be valid: {error}")),
            Timestamp::new("2026-01-02T00:00:00Z")
                .unwrap_or_else(|error| panic!("fixture timestamp must be valid: {error}")),
            BarUnit::Minute,
        )
    };
    assert_eq!(
        history().unit_number(0).build(),
        Err(RequestValidationError::NonPositiveHistoryUnitNumber)
    );
    assert_eq!(
        history().limit(20_001).build(),
        Err(RequestValidationError::HistoryLimitOutOfRange)
    );
    assert_eq!(
        PartialCloseContract::new(account_id, history_contract.clone(), 0),
        Err(RequestValidationError::NonPositivePartialCloseSize)
    );
}

#[test]
fn only_trailing_stop_placements_require_a_trail_price() {
    let account_id =
        AccountId::new(42).unwrap_or_else(|error| panic!("fixture account must be valid: {error}"));
    let contract_id = ContractId::new("CON.F.US.MNQ.M26")
        .unwrap_or_else(|error| panic!("fixture contract must be valid: {error}"));
    for order_type in [
        OrderType::Limit,
        OrderType::Market,
        OrderType::Stop,
        OrderType::TrailingStop,
        OrderType::JoinBid,
        OrderType::JoinAsk,
    ] {
        let builder =
            PlaceOrder::builder(account_id, contract_id.clone(), order_type, Side::Ask, 1);
        if order_type == OrderType::TrailingStop {
            assert_eq!(
                builder.build(),
                Err(RequestValidationError::MissingTrailPrice)
            );
        } else {
            assert!(builder.build().is_ok());
        }
    }

    // Absolute price levels have no locally imposed positivity or distance limit.
    for price in [
        Decimal::NEGATIVE_ONE,
        Decimal::ZERO,
        Decimal::new(10_425, 2),
    ] {
        let request = PlaceOrder::builder(
            account_id,
            contract_id.clone(),
            OrderType::TrailingStop,
            Side::Ask,
            1,
        )
        .trail_price(price)
        .build()
        .unwrap_or_else(|error| panic!("an explicit trail price must build: {error}"));
        assert_eq!(request.trail_price(), Some(price));
    }
}

#[tokio::test]
async fn bodyless_provider_rejection_is_a_typed_provider_error() {
    let server = MockServer::start_async().await;
    let _login = server
        .mock_async(|when, then| {
            when.method(POST).path("/api/Auth/loginKey");
            then.status(200)
                .json_body(json!({"success": true, "errorCode": 0, "token": "synthetic-token"}));
        })
        .await;
    let accounts = server
        .mock_async(|when, then| {
            when.method(POST).path("/api/Account/search");
            then.status(200).json_body(json!({
                "success": false,
                "errorCode": 17,
                "errorMessage": "synthetic rejection"
            }));
        })
        .await;

    let client = fixture_client(&server);
    client
        .authenticate()
        .await
        .unwrap_or_else(|error| panic!("fixture login must succeed: {error}"));
    let result = client.search_active_accounts().await;

    accounts.assert_async().await;
    assert!(matches!(
        result,
        Err(Error::Provider(provider_error))
            if provider_error.code == 17 && provider_error.name.is_none()
    ));
}

#[tokio::test]
async fn rejected_credentials_report_the_published_login_code_text() {
    let server = MockServer::start_async().await;
    let _login = server
        .mock_async(|when, then| {
            when.method(POST).path("/api/Auth/loginKey");
            then.status(200)
                .json_body(json!({"success": false, "errorCode": 1, "errorMessage": null}));
        })
        .await;

    let client = fixture_client(&server);
    let Err(rejection) = client.authenticate().await else {
        panic!("a rejected login must fail");
    };

    assert!(matches!(
        rejection,
        Error::CredentialsRejected {
            code: 1,
            name: Some("UserNotFound"),
        }
    ));
    assert_eq!(
        rejection.to_string(),
        "provider rejected the credentials (code: 1 UserNotFound)"
    );
}

#[tokio::test]
async fn provider_rejection_text_is_the_endpoints_published_code_name() {
    let server = MockServer::start_async().await;
    let _login = server
        .mock_async(|when, then| {
            when.method(POST).path("/api/Auth/loginKey");
            then.status(200)
                .json_body(json!({"success": true, "errorCode": 0, "token": "synthetic-token"}));
        })
        .await;
    let placement = server
        .mock_async(|when, then| {
            when.method(POST).path("/api/Order/place");
            then.status(200).json_body(json!({
                "orderId": 84,
                "success": false,
                "errorCode": 2,
                "errorMessage": "Brackets cannot be used with Position Brackets."
            }));
        })
        .await;
    let cancellation = server
        .mock_async(|when, then| {
            when.method(POST).path("/api/Order/cancel");
            then.status(200).json_body(json!({
                "success": false,
                "errorCode": 2,
                "errorMessage": "Follower accounts cannot cancel orders"
            }));
        })
        .await;
    let modification = server
        .mock_async(|when, then| {
            when.method(POST).path("/api/Order/modify");
            then.status(200)
                .json_body(json!({"success": false, "errorCode": 4, "errorMessage": null}));
        })
        .await;

    let client = fixture_client(&server);
    client
        .authenticate()
        .await
        .unwrap_or_else(|error| panic!("fixture login must succeed: {error}"));
    let account_id =
        AccountId::new(42).unwrap_or_else(|error| panic!("fixture account must be valid: {error}"));
    let contract_id = ContractId::new("CON.F.US.MNQ.M26")
        .unwrap_or_else(|error| panic!("fixture contract must be valid: {error}"));
    let order_id =
        OrderId::new(84).unwrap_or_else(|error| panic!("fixture order must be valid: {error}"));
    let place = PlaceOrder::builder(account_id, contract_id, OrderType::Market, Side::Bid, 1)
        .build()
        .unwrap_or_else(|error| panic!("fixture placement must be valid: {error}"));
    let modify = ModifyOrder::builder(account_id, order_id)
        .size(1)
        .build()
        .unwrap_or_else(|error| panic!("fixture modification must be valid: {error}"));

    // Code 2 is published as a different name by each endpoint.
    let Err(rejection) = client.place_order(&place).await else {
        panic!("a definitive placement rejection must fail");
    };
    assert!(matches!(
        rejection,
        Error::Provider(ref provider_error)
            if provider_error.name == Some("OrderRejected")
    ));
    assert_eq!(
        rejection.to_string(),
        "ProjectX rejected the operation (code: 2 OrderRejected)"
    );

    let Err(rejection) = client
        .cancel_order(&CancelOrder {
            account_id,
            order_id,
        })
        .await
    else {
        panic!("a definitive cancellation rejection must fail");
    };
    assert!(matches!(
        rejection,
        Error::Provider(ref provider_error)
            if provider_error.name == Some("OrderNotFound")
    ));

    // A documented pending outcome stays ambiguous, and still reports the code.
    let Err(rejection) = client.modify_order(&modify).await else {
        panic!("a pending modification outcome must stay ambiguous");
    };
    assert!(matches!(
        rejection,
        Error::AmbiguousMutation {
            operation: "order modification",
            code: Some(4),
            name: Some("Pending"),
            origin: None,
        }
    ));
    assert_eq!(
        rejection.to_string(),
        "order modification outcome is ambiguous (code: 4 Pending); reconcile provider state before retrying"
    );

    placement.assert_async().await;
    cancellation.assert_async().await;
    modification.assert_async().await;
}

#[tokio::test]
async fn authenticated_calls_fail_closed_before_login() {
    let server = MockServer::start_async().await;
    let client = fixture_client(&server);
    assert!(matches!(
        client.search_active_accounts().await,
        Err(Error::NotAuthenticated)
    ));
}

#[tokio::test]
async fn response_limit_is_enforced_while_streaming() {
    let server = MockServer::start_async().await;
    let _login = server
        .mock_async(|when, then| {
            when.method(POST).path("/api/Auth/loginKey");
            then.status(200)
                .json_body(json!({"success": true, "errorCode": 0, "token": "token"}));
        })
        .await;
    let _accounts = server
        .mock_async(|when, then| {
            when.method(POST).path("/api/Account/search");
            then.status(200).body("{\"accounts\":[],\"success\":true}");
        })
        .await;

    let credentials = Credentials::new("user", "key")
        .unwrap_or_else(|error| panic!("fixture credentials must be valid: {error}"));
    let endpoints = Endpoints::custom(&server.base_url(), &server.base_url())
        .unwrap_or_else(|error| panic!("fixture URL must be valid: {error}"));
    let client = Client::builder(credentials)
        .endpoints(endpoints)
        .response_limit(8)
        .build()
        .unwrap_or_else(|error| panic!("fixture client must build: {error}"));

    assert!(matches!(
        client.authenticate().await,
        Err(Error::ResponseTooLarge { limit_bytes: 8 })
    ));
}