ezcapsolver-rs 0.1.0

Rust SDK for EzCaptchaSolver.
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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
use std::{sync::Arc, time::Duration};

use mockito::{Matcher, Server};
use serde_json::json;

use super::*;
use crate::{
    ReClassificationSolution, RecaptchaSolution, RecaptchaV2ClassificationTask, RecaptchaV2Task,
    Solved, TaskType,
};

#[tokio::test]
async fn solve_posts_the_expected_requests_and_decodes_the_solution()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut server = Server::new_async().await;
    let create_mock = server
        .mock("POST", "/createTask")
        .match_header(
            "content-type",
            Matcher::Regex("application/json.*".to_owned()),
        )
        .match_body(Matcher::Json(json!({
            "clientKey": "client-key",
            "appId": 42,
            "task": {
                "type": "ReCaptchaV2TaskProxyless",
                "websiteURL": "https://example.com",
                "websiteKey": "site-key",
                "isInvisible": false
            }
        })))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"errorId":0,"requestId":"request-1","taskId":"task-123"}"#)
        .create_async()
        .await;
    let result_mock = server
        .mock("POST", "/getTaskResult")
        .match_body(Matcher::Json(json!({
            "clientKey": "client-key",
            "taskId": "task-123"
        })))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"{
                "errorId":0,
                "status":"ready",
                "solution":{
                    "gRecaptchaResponse":"token-value",
                    "sec_ch_ua":"\"Chromium\";v=\"140\"",
                    "user_agent":"Mozilla/5.0"
                }
            }"#,
        )
        .create_async()
        .await;

    let config = ClientConfig::default()
        .with_app_id(42)
        .with_polling(PollingConfig::new(Duration::from_millis(1), 2));
    let client = test_client(config, &server)?;
    let mut task = RecaptchaV2Task {
        website_url: "https://example.com".to_owned(),
        website_key: "site-key".to_owned(),
        ..Default::default()
    };
    task.extra
        .insert("type".to_owned(), json!("IncorrectTaskType"));

    let completed: Solved<RecaptchaSolution> =
        client.solve_recaptcha_v2_task_proxyless(&task).await?;
    assert_eq!(completed.task_id.as_deref(), Some("task-123"));
    assert_eq!(completed.request_id.as_deref(), Some("request-1"));
    assert_eq!(completed.solution.token, "token-value");
    create_mock.assert_async().await;
    result_mock.assert_async().await;
    Ok(())
}

#[tokio::test]
async fn sync_solve_accepts_a_custom_type_without_polling()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut async_server = Server::new_async().await;
    let mut sync_server = Server::new_async().await;
    let async_mock = async_server
        .mock("POST", Matcher::Any)
        .expect(0)
        .create_async()
        .await;
    let solution = json!({"token": "token-value", "futureField": {"items": [1, null, true]}});
    let sync_mock = sync_server
        .mock("POST", "/createSyncTask")
        .match_body(Matcher::Json(json!({
            "clientKey": "client-key",
            "appId": 42,
            "task": {
                "type": "BrandNewSyncTaskType",
                "websiteURL": "https://example.com",
                "futureParam": {"enabled": true}
            }
        })))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "errorId": 0,
                "status": "ready",
                "requestId": "request-sync",
                "solution": solution
            })
            .to_string(),
        )
        .expect(1)
        .create_async()
        .await;

    let client = AsyncEzCapSolverClient::with_config(
        ClientConfig::default()
            .with_client_key("client-key")
            .with_app_id(42)
            .with_base_urls(async_server.url(), sync_server.url()),
    )?;
    let task = json!({
        "type": "IncorrectTaskType",
        "websiteURL": "https://example.com",
        "futureParam": {"enabled": true}
    });
    let solved: Solved<Solution> = client.sync_solve("BrandNewSyncTaskType", &task).await?;

    assert_eq!(solved.task_id, None);
    assert_eq!(solved.request_id.as_deref(), Some("request-sync"));
    assert_eq!(solved.solution.as_value(), Some(&solution));
    assert_eq!(solved.raw, solved.solution);
    sync_mock.assert_async().await;
    async_mock.assert_async().await;
    Ok(())
}

/// The synchronous endpoint assigns and returns a taskId on both the success and
/// the failure path. Dropping it discards the only handle on a billed task.
#[tokio::test]
async fn sync_solve_keeps_the_identifier_the_service_assigned()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut sync_server = Server::new_async().await;
    let sync_mock = sync_server
        .mock("POST", "/createSyncTask")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "errorId": 0,
                "requestId": "request-sync",
                "taskId": "sync-task-1",
                "status": "ready",
                "solution": {"gRecaptchaResponse": "token-value"}
            })
            .to_string(),
        )
        .expect(2)
        .create_async()
        .await;

    let client = AsyncEzCapSolverClient::with_config(
        ClientConfig::default()
            .with_client_key("client-key")
            .with_base_urls(sync_server.url(), sync_server.url()),
    )?;

    // The general entry point.
    let raw: Solved<Solution> = client
        .sync_solve(TaskType::RecaptchaV2TaskProxyless, &json!({}))
        .await?;
    assert_eq!(raw.task_id.as_deref(), Some("sync-task-1"));

    // The typed convenience method goes through decode_sync_result, a second
    // code path.
    let typed = client
        .sync_solve_recaptcha_v2_task_proxyless(&RecaptchaV2Task::default())
        .await?;
    assert_eq!(typed.task_id.as_deref(), Some("sync-task-1"));
    assert_eq!(typed.solution.token, "token-value");

    sync_mock.assert_async().await;
    Ok(())
}

#[tokio::test]
async fn sync_solve_preserves_missing_and_null_solutions()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    for (body, expected) in [
        (r#"{"errorId":0,"status":"ready"}"#, Solution::Missing),
        (
            r#"{"errorId":0,"status":"ready","solution":null}"#,
            Solution::Value(json!(null)),
        ),
    ] {
        let mut server = Server::new_async().await;
        let sync_mock = server
            .mock("POST", "/createSyncTask")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(body)
            .create_async()
            .await;
        let client = test_client(ClientConfig::default(), &server)?;

        let solved = client.sync_solve(TaskType::Hcaptcha, &json!({})).await?;

        assert_eq!(solved.task_id, None);
        assert_eq!(solved.request_id, None);
        assert_eq!(solved.solution, expected);
        assert_eq!(solved.raw, expected);
        sync_mock.assert_async().await;
    }
    Ok(())
}

#[tokio::test]
async fn sync_solve_rejects_non_ready_results()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    for status in ["processing", "error"] {
        let mut server = Server::new_async().await;
        let sync_mock = server
            .mock("POST", "/createSyncTask")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                json!({
                    "errorId": 0,
                    "status": status,
                    "solution": {"token": "not-ready"}
                })
                .to_string(),
            )
            .create_async()
            .await;
        let client = test_client(ClientConfig::default(), &server)?;

        let result = client.sync_solve("BrandNewSyncTaskType", &json!({})).await;

        assert!(
            matches!(result, Err(Error::UnexpectedResponse(message)) if message.contains(status))
        );
        sync_mock.assert_async().await;
    }
    Ok(())
}

#[tokio::test]
async fn sync_solve_preserves_api_error_details()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut server = Server::new_async().await;
    let sync_mock = server
        .mock("POST", "/createSyncTask")
        .with_status(400)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "errorId": 1,
                "errorCode": "ERROR_REQUEST_PARAMETERS",
                "errorDescription": "Invalid parameters",
                "requestId": "request-sync-error",
                "errors": {"task.websiteURL": "must not be blank"}
            })
            .to_string(),
        )
        .create_async()
        .await;
    let client = test_client(ClientConfig::default(), &server)?;

    let result = client.sync_solve("BrandNewSyncTaskType", &json!({})).await;

    let Err(Error::EzError(api_error)) = result else {
        return Err(std::io::Error::other("expected a structured API error").into());
    };
    assert_eq!(
        api_error.error_code.as_deref(),
        Some("ERROR_REQUEST_PARAMETERS")
    );
    assert_eq!(
        api_error.error_description.as_deref(),
        Some("Invalid parameters")
    );
    assert_eq!(api_error.request_id.as_deref(), Some("request-sync-error"));
    assert_eq!(api_error.task_id, None);
    assert_eq!(api_error.http_status, 400);
    assert_eq!(
        api_error.errors.get("task.websiteURL").map(String::as_str),
        Some("must not be blank")
    );
    sync_mock.assert_async().await;
    Ok(())
}

#[tokio::test]
async fn specialized_sync_solver_returns_a_classification_struct()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut server = Server::new_async().await;
    let sync_mock = server
        .mock("POST", "/createSyncTask")
        .match_body(Matcher::Json(json!({
            "clientKey": "client-key",
            "task": {
                "type": "ReCaptchaV2Classification",
                "image": "aW1n",
                "question": "question",
                "size": 1
            }
        })))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "errorId": 0,
                "status": "ready",
                "solution": {"type": "single", "hasObject": false, "confidence": 0.9}
            })
            .to_string(),
        )
        .create_async()
        .await;

    let client = test_client(ClientConfig::default(), &server)?;
    let task = RecaptchaV2ClassificationTask {
        image: "aW1n".to_owned(),
        question: "question".to_owned(),
        size: 1,
        ..Default::default()
    };
    let solution: ReClassificationSolution = client
        .sync_solve_recaptcha_v2_classification(&task)
        .await?
        .solution;

    assert_eq!(solution.r#type, "single");
    assert!(solution.is_single());
    assert!(!solution.is_multi());
    assert!(!solution.has_object);
    assert!(solution.objects.is_empty());
    assert_eq!(solution.extra["confidence"], json!(0.9));
    sync_mock.assert_async().await;
    Ok(())
}

/// The mode a type documents does not restrict which methods exist: the
/// `sync_`-prefixed one sends a polling type to `/createSyncTask`, and it is the
/// endpoint that changes, not the parameters.
#[tokio::test]
async fn sync_prefixed_method_sends_a_polling_type_to_the_synchronous_endpoint()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut server = Server::new_async().await;
    let sync_mock = server
        .mock("POST", "/createSyncTask")
        .match_body(Matcher::Json(json!({
            "clientKey": "client-key",
            "task": {
                "type": "ReCaptchaV2TaskProxyless",
                "websiteURL": "https://example.com",
                "websiteKey": "site-key",
                "isInvisible": false
            }
        })))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "errorId": 0,
                "status": "ready",
                "solution": {
                    "gRecaptchaResponse": "token-value",
                    "sec_ch_ua": "Chromium",
                    "user_agent": "Mozilla/5.0",
                    "aFieldAddedLater": 7
                }
            })
            .to_string(),
        )
        .create_async()
        .await;

    let client = test_client(ClientConfig::default(), &server)?;
    let task = RecaptchaV2Task {
        website_url: "https://example.com".to_owned(),
        website_key: "site-key".to_owned(),
        ..Default::default()
    };
    let solved = client.sync_solve_recaptcha_v2_task_proxyless(&task).await?;

    assert_eq!(solved.solution.token, "token-value");
    // This endpoint assigns no task id, so the field has to be None rather than a
    // placeholder empty string.
    assert!(solved.task_id.is_none());
    // Unmodelled fields stay recoverable from raw.
    assert_eq!(
        solved
            .raw
            .as_value()
            .and_then(|v| v.get("aFieldAddedLater")),
        Some(&json!(7))
    );
    sync_mock.assert_async().await;
    Ok(())
}

/// The mirror of the above: the unprefixed method creates and polls a type the
/// catalog documents as synchronous.
#[tokio::test]
async fn unprefixed_method_polls_even_a_type_documented_as_synchronous()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut server = Server::new_async().await;
    let create_mock = server
        .mock("POST", "/createTask")
        .match_body(Matcher::PartialJson(
            json!({"task": {"type": "ReCaptchaV2Classification"}}),
        ))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(json!({"errorId": 0, "taskId": "task-77"}).to_string())
        .create_async()
        .await;
    let result_mock = server
        .mock("POST", "/getTaskResult")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "errorId": 0,
                "status": "ready",
                "solution": {"type": "multi", "objects": [1, 5]}
            })
            .to_string(),
        )
        .create_async()
        .await;

    let config =
        ClientConfig::default().with_polling(PollingConfig::new(Duration::from_millis(1), 2));
    let client = test_client(config, &server)?;
    let task = RecaptchaV2ClassificationTask {
        image: "aW1n".to_owned(),
        question: "question".to_owned(),
        ..Default::default()
    };
    let solved = client.solve_recaptcha_v2_classification(&task).await?;

    assert!(solved.solution.is_multi());
    assert_eq!(solved.solution.objects, vec![1, 5]);
    assert_eq!(solved.task_id.as_deref(), Some("task-77"));
    create_mock.assert_async().await;
    result_mock.assert_async().await;
    Ok(())
}

#[tokio::test]
async fn create_task_accepts_dynamic_json_parameters()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut server = Server::new_async().await;
    let create_mock = server
        .mock("POST", "/createTask")
        .match_body(Matcher::Json(json!({
            "clientKey": "client-key",
            "task": {
                "type": "HCaptcha",
                "websiteURL": "https://example.com",
                "websiteKey": "site-key",
                "futureWorkerOption": true
            }
        })))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"errorId":0,"requestId":"request-dynamic","taskId":"task-dynamic"}"#)
        .create_async()
        .await;

    let client = test_client(ClientConfig::default(), &server)?;
    let params = json!({
        "websiteURL": "https://example.com",
        "websiteKey": "site-key",
        "futureWorkerOption": true
    });

    let created = client.create_task(TaskType::Hcaptcha, &params).await?;

    assert_eq!(created.task_id, "task-dynamic");
    assert_eq!(created.meta.request_id.as_deref(), Some("request-dynamic"));
    create_mock.assert_async().await;
    Ok(())
}

#[tokio::test]
async fn balance_returns_the_value_the_service_reported()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    // Balances carry at most four decimal places, and the API sends them as a
    // JSON number. Every value in that range must survive unchanged.
    for raw in [
        "0",
        "250",
        "0.0001",
        "12.3456",
        "1234.5678",
        "9999999999.9999",
    ] {
        let mut server = Server::new_async().await;
        let mock = server
            .mock("POST", "/getBalance")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(format!(r#"{{"errorId":0,"balance":{raw}}}"#))
            .create_async()
            .await;

        let client = test_client(ClientConfig::default(), &server)?;
        let balance = client.balance().await?;

        assert_eq!(balance.to_string(), raw, "balance {raw} changed");
        mock.assert_async().await;
    }
    Ok(())
}

#[tokio::test]
async fn validation_errors_are_visible_in_the_error_message()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut server = Server::new_async().await;
    let create_mock = server
        .mock("POST", "/createTask")
        .with_status(400)
        .with_header("content-type", "application/json")
        .with_body(
            r#"{
                "errorId":1,
                "errorCode":"ERROR_REQUEST_PARAMETERS",
                "errorDescription":"Invalid parameters",
                "errors":{"task.websiteKey":"must not be blank"}
            }"#,
        )
        .create_async()
        .await;

    let client = test_client(ClientConfig::default(), &server)?;
    let error = client
        .create_task(TaskType::Hcaptcha, &json!({}))
        .await
        .err()
        .ok_or_else(|| std::io::Error::other("expected an API error"))?;

    // Displaying the error alone must name the offending field.
    let message = error.to_string();
    assert!(message.contains("ERROR_REQUEST_PARAMETERS"), "{message}");
    assert!(message.contains("task.websiteKey"), "{message}");
    assert!(message.contains("must not be blank"), "{message}");
    create_mock.assert_async().await;
    Ok(())
}

#[tokio::test]
async fn an_empty_error_code_does_not_turn_success_into_failure()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut server = Server::new_async().await;
    let create_mock = server
        .mock("POST", "/createTask")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"errorId":0,"errorCode":"","errorDescription":"","taskId":"task-ok"}"#)
        .create_async()
        .await;

    let client = test_client(ClientConfig::default(), &server)?;
    let created = client.create_task(TaskType::Hcaptcha, &json!({})).await?;

    assert_eq!(created.task_id, "task-ok");
    create_mock.assert_async().await;
    Ok(())
}

#[tokio::test]
async fn specialized_solver_preserves_task_api_error_details()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut server = Server::new_async().await;
    let create_mock = server
        .mock("POST", "/createTask")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"errorId":0,"requestId":"request-create","taskId":"task-123"}"#)
        .create_async()
        .await;
    let result_mock = server
        .mock("POST", "/getTaskResult")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"{
                "errorId":1,
                "status":"error",
                "errorCode":"ERROR_CAPTCHA_UNSOLVABLE",
                "errorDescription":"Captcha could not be solved",
                "requestId":"request-3"
            }"#,
        )
        .create_async()
        .await;

    let config =
        ClientConfig::default().with_polling(PollingConfig::new(Duration::from_millis(1), 2));
    let client = test_client(config, &server)?;
    let task = RecaptchaV2Task {
        website_url: "https://example.com".to_owned(),
        website_key: "site-key".to_owned(),
        ..Default::default()
    };
    let error = client
        .solve_recaptcha_v2_task_proxyless(&task)
        .await
        .unwrap_err();

    let Error::EzError(api_error) = error else {
        return Err(std::io::Error::other("expected a structured API error").into());
    };
    assert_eq!(
        api_error.error_code.as_deref(),
        Some("ERROR_CAPTCHA_UNSOLVABLE")
    );
    assert_eq!(
        api_error.error_description.as_deref(),
        Some("Captcha could not be solved")
    );
    assert_eq!(api_error.task_id.as_deref(), Some("task-123"));
    assert_eq!(api_error.request_id.as_deref(), Some("request-create"));
    create_mock.assert_async().await;
    result_mock.assert_async().await;
    Ok(())
}

#[tokio::test]
async fn api_errors_are_preserved_for_non_success_http_statuses()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut server = Server::new_async().await;
    let balance_mock = server
        .mock("POST", "/getBalance")
        .match_body(Matcher::Json(json!({"clientKey": "client-key"})))
        .with_status(429)
        .with_header("content-type", "application/json")
        .with_body(
            r#"{
                "errorId":1,
                "errorCode":"ERROR_RATE_LIMIT",
                "errorDescription":"Too many requests",
                "requestId":"request-2"
            }"#,
        )
        .create_async()
        .await;

    let client = test_client(ClientConfig::default(), &server)?;
    let error = client
        .balance()
        .await
        .err()
        .ok_or_else(|| std::io::Error::other("expected an API error"))?;

    let Error::EzError(api_error) = error else {
        return Err(std::io::Error::other("expected a structured API error").into());
    };
    assert_eq!(api_error.error_code.as_deref(), Some("ERROR_RATE_LIMIT"));
    assert_eq!(
        api_error.error_description.as_deref(),
        Some("Too many requests")
    );
    assert_eq!(api_error.task_id, None);
    assert_eq!(api_error.request_id.as_deref(), Some("request-2"));
    assert_eq!(api_error.http_status, 429);
    balance_mock.assert_async().await;
    Ok(())
}

#[tokio::test]
async fn polling_stops_after_the_configured_attempt_limit()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut server = Server::new_async().await;
    let result_mock = server
        .mock("POST", "/getTaskResult")
        .match_body(Matcher::Json(json!({
            "clientKey": "client-key",
            "taskId": "task-123"
        })))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"errorId":0,"status":"processing"}"#)
        .expect(2)
        .create_async()
        .await;

    let client = test_client(ClientConfig::default(), &server)?;
    let error = client
        .wait_for_result_with("task-123", PollingConfig::new(Duration::from_millis(1), 2))
        .await
        .err()
        .ok_or_else(|| std::io::Error::other("expected polling to be exhausted"))?;

    let Error::PollingExhausted {
        task_id, attempts, ..
    } = error
    else {
        return Err(std::io::Error::other("expected a polling exhaustion error").into());
    };
    assert_eq!(task_id, "task-123");
    assert_eq!(attempts, 2);
    result_mock.assert_async().await;
    Ok(())
}

/// A throttled poll refuses the query, not the task: the task is still queued
/// and has already been billed, so failing the wait would throw away a result
/// that was about to arrive.
#[tokio::test]
async fn a_throttled_poll_is_retried_instead_of_failing_the_task()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut server = Server::new_async().await;
    let throttled = server
        .mock("POST", "/getTaskResult")
        .with_status(429)
        .with_header("content-type", "application/json")
        .with_body(r#"{"errorId":1,"errorCode":"ERROR_REQUEST_LIMIT"}"#)
        .expect(1)
        .create_async()
        .await;
    let ready = server
        .mock("POST", "/getTaskResult")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"errorId":0,"status":"ready","solution":{"token":"t"}}"#)
        .expect(1)
        .create_async()
        .await;

    let client = test_client(ClientConfig::default(), &server)?;
    let result = client
        .wait_for_result_with("task-123", PollingConfig::new(Duration::from_millis(1), 3))
        .await?;

    assert_eq!(result.status, TaskStatus::Ready);
    throttled.assert_async().await;
    ready.assert_async().await;
    Ok(())
}

/// Only the throttling codes are retried. Every other API error is the answer
/// to the poll, so the wait has to end on it rather than burn the budget.
#[tokio::test]
async fn a_non_throttling_error_ends_the_wait_at_once()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut server = Server::new_async().await;
    let result_mock = server
        .mock("POST", "/getTaskResult")
        .with_status(500)
        .with_header("content-type", "application/json")
        .with_body(r#"{"errorId":1,"errorCode":"ERROR_TASK_NOT_EXIST"}"#)
        .expect(1)
        .create_async()
        .await;

    let client = test_client(ClientConfig::default(), &server)?;
    let error = client
        .wait_for_result_with("task-123", PollingConfig::new(Duration::from_millis(1), 5))
        .await
        .err()
        .ok_or_else(|| std::io::Error::other("expected the wait to fail"))?;

    let Error::EzError(api_error) = error else {
        return Err(std::io::Error::other("expected a structured API error").into());
    };
    assert_eq!(
        api_error.error_code.as_deref(),
        Some("ERROR_TASK_NOT_EXIST")
    );
    result_mock.assert_async().await;
    Ok(())
}

#[tokio::test]
async fn the_first_result_query_waits_for_one_polling_interval()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut server = Server::new_async().await;
    let result_mock = server
        .mock("POST", "/getTaskResult")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"errorId":0,"status":"ready","solution":{"gRecaptchaResponse":"t"}}"#)
        .create_async()
        .await;

    let client = test_client(ClientConfig::default(), &server)?;
    let polling = PollingConfig::new(Duration::from_millis(150), 1);

    let started = std::time::Instant::now();
    let result = client.wait_for_result_with("task-123", polling).await?;
    let elapsed = started.elapsed();

    assert!(result.is_ready());
    assert!(
        elapsed >= Duration::from_millis(150),
        "the first query ran after {elapsed:?}, expected it to wait one 150ms interval"
    );
    result_mock.assert_async().await;
    Ok(())
}

#[test]
fn invalid_client_configuration_is_rejected() {
    let blank_key = AsyncEzCapSolverClient::with_client_key(" ").err();
    assert!(matches!(
        blank_key,
        Some(Error::Message(message)) if message.contains("client key")
    ));

    let zero_timeout = ClientConfig::default().with_timeout(Duration::from_secs(0));
    let invalid_timeout =
        AsyncEzCapSolverClient::with_config(zero_timeout.with_client_key("client-key")).err();
    assert!(matches!(
        invalid_timeout,
        Some(Error::Message(message)) if message.contains("timeout")
    ));
}

#[test]
fn client_can_be_created_with_an_explicit_key() -> Result<()> {
    let client = AsyncEzCapSolverClient::with_client_key("client-key")?;

    assert_eq!(client.client_key, "client-key");
    Ok(())
}

/// Every endpoint carries the client key in the `X-API-Key` header, on
/// top of the `clientKey` body field. It sends no `X-Request-Id`: the
/// gateway assigns the correlation id and the SDK only reads it back.
#[tokio::test]
async fn every_request_carries_the_client_key_header()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut server = Server::new_async().await;
    let ready = r#"{"errorId":0,"status":"ready","solution":{"token":"token-value"}}"#;
    let mut mocks = Vec::new();
    for (path, body) in [
        ("/createTask", r#"{"errorId":0,"taskId":"task-123"}"#),
        ("/getTaskResult", ready),
        ("/createSyncTask", ready),
        ("/getBalance", r#"{"errorId":0,"balance":1.5}"#),
    ] {
        let mock = server
            .mock("POST", path)
            .match_header("X-API-Key", "client-key")
            .match_header("X-Request-Id", Matcher::Missing)
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(body)
            .expect(1)
            .create_async()
            .await;
        mocks.push(mock);
    }

    let client = test_client(
        ClientConfig::default().with_polling(PollingConfig::new(Duration::from_millis(1), 1)),
        &server,
    )?;
    client.solve("CustomTask", &json!({})).await?;
    client.sync_solve("CustomTask", &json!({})).await?;
    client.balance().await?;

    for mock in mocks {
        mock.assert_async().await;
    }
    Ok(())
}

/// A key that cannot travel as a header value fails when the client is built,
/// not on every request afterwards.
#[test]
fn a_client_key_that_is_not_a_valid_header_value_is_rejected() {
    let error = AsyncEzCapSolverClient::with_client_key("client\nkey").err();

    assert!(matches!(
        error,
        Some(Error::Message(message)) if message.contains("invalid characters")
    ));
}

fn test_client(
    config: ClientConfig,
    server: &mockito::ServerGuard,
) -> Result<AsyncEzCapSolverClient> {
    AsyncEzCapSolverClient::with_config(
        config
            .with_client_key("client-key")
            .with_base_urls(server.url(), server.url()),
    )
}

/// Supplying an HTTP client is what makes reusing a connection pool, installing
/// middleware, or changing the TLS configuration possible.
#[tokio::test]
async fn a_supplied_http_client_is_used_as_is()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut server = Server::new_async().await;
    let mock = server
        .mock("POST", "/getBalance")
        .match_header("user-agent", "caller-supplied/9.9")
        // The supplied client has no default headers, so the key header must
        // still be added per request.
        .match_header("X-API-Key", "client-key")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"errorId":0,"balance":12.5}"#)
        .expect(1)
        .create_async()
        .await;

    // The configured user_agent is deliberately something else: the supplied
    // client brings its own header, which must not be overwritten.
    let client = AsyncEzCapSolverClient::with_http_client(
        ClientConfig::default()
            .with_client_key("client-key")
            .with_user_agent("ignored-because-the-client-brings-its-own")
            .with_base_urls(server.url(), server.url()),
        reqwest::Client::builder()
            .user_agent("caller-supplied/9.9")
            .build()?,
    )?;

    assert_eq!(client.balance().await?, 12.5);
    mock.assert_async().await;
    Ok(())
}

/// One client instance serving many concurrent tasks.
///
/// Every method takes `&self` and the client holds no interior mutable state, so
/// sharing needs nothing beyond an `Arc`. `reqwest::Client` is itself `Arc`-backed,
/// so neither cloning nor sharing duplicates the connection pool.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn one_client_serves_many_concurrent_callers()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    const CALLERS: usize = 64;

    // A compile-time proof of everything sharing across threads requires.
    const fn assert_shareable<T: Send + Sync + Clone + 'static>() {}
    assert_shareable::<AsyncEzCapSolverClient>();
    assert_shareable::<crate::EzCapSolverClient>();

    let mut server = Server::new_async().await;
    let mock = server
        .mock("POST", "/getBalance")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"errorId":0,"balance":12.5}"#)
        .expect(CALLERS)
        .create_async()
        .await;

    let client = Arc::new(AsyncEzCapSolverClient::with_config(
        ClientConfig::default()
            .with_client_key("client-key")
            .with_base_urls(server.url(), server.url()),
    )?);

    let mut tasks = tokio::task::JoinSet::new();
    for _ in 0..CALLERS {
        let client = Arc::clone(&client);
        tasks.spawn(async move { client.balance().await });
    }

    let mut completed = 0;
    while let Some(joined) = tasks.join_next().await {
        assert_eq!(joined??, 12.5);
        completed += 1;
    }

    assert_eq!(completed, CALLERS);
    mock.assert_async().await;
    Ok(())
}

#[tokio::test]
async fn a_wait_failure_carries_the_identifier_of_the_billed_task()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut server = Server::new_async().await;
    let create_mock = server
        .mock("POST", "/createTask")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"errorId":0,"requestId":"request-1","taskId":"task-123"}"#)
        .create_async()
        .await;
    // A success envelope with no status violates the contract. The task has been
    // created and billed by now, and a caller using the one-shot solve has never
    // seen its id, so an error that does not carry it writes the charge off.
    let result_mock = server
        .mock("POST", "/getTaskResult")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"errorId":0}"#)
        .create_async()
        .await;

    let client = test_client(
        ClientConfig::default().with_polling(PollingConfig::new(Duration::from_millis(1), 3)),
        &server,
    )?;
    let error = client
        .solve(
            TaskType::RecaptchaV2TaskProxyless,
            &RecaptchaV2Task::default(),
        )
        .await
        .err()
        .ok_or_else(|| std::io::Error::other("expected the wait to fail"))?;

    let Error::WaitInterrupted {
        task_id,
        request_id,
        source,
    } = error
    else {
        return Err(std::io::Error::other("expected the task identifier to survive").into());
    };
    assert_eq!(task_id, "task-123");
    assert_eq!(request_id.as_deref(), Some("request-1"));
    assert!(
        matches!(*source, Error::UnexpectedResponse(_)),
        "the underlying failure must stay reachable"
    );

    create_mock.assert_async().await;
    result_mock.assert_async().await;
    Ok(())
}

#[tokio::test]
async fn an_exhausted_budget_is_not_wrapped_a_second_time()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut server = Server::new_async().await;
    let create_mock = server
        .mock("POST", "/createTask")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"errorId":0,"taskId":"task-123"}"#)
        .create_async()
        .await;
    let result_mock = server
        .mock("POST", "/getTaskResult")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"errorId":0,"status":"processing"}"#)
        .expect(2)
        .create_async()
        .await;

    let client = test_client(
        ClientConfig::default().with_polling(PollingConfig::new(Duration::from_millis(1), 2)),
        &server,
    )?;
    let error = client
        .solve(
            TaskType::RecaptchaV2TaskProxyless,
            &RecaptchaV2Task::default(),
        )
        .await
        .err()
        .ok_or_else(|| std::io::Error::other("expected polling to be exhausted"))?;

    // This variant carries a task_id already, so wrapping it would only make a
    // caller unwrap one more layer to read the same value.
    assert!(
        matches!(error, Error::PollingExhausted { ref task_id, .. } if task_id == "task-123"),
        "got {error:?}"
    );

    create_mock.assert_async().await;
    result_mock.assert_async().await;
    Ok(())
}

#[tokio::test]
async fn an_api_error_while_waiting_keeps_its_own_identifier_field()
-> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut server = Server::new_async().await;
    let create_mock = server
        .mock("POST", "/createTask")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"errorId":0,"requestId":"request-1","taskId":"task-123"}"#)
        .create_async()
        .await;
    let result_mock = server
        .mock("POST", "/getTaskResult")
        .with_status(500)
        .with_header("content-type", "application/json")
        .with_body(r#"{"errorId":1,"errorCode":"ERROR_TASK_NOT_EXIST"}"#)
        .create_async()
        .await;

    let client = test_client(
        ClientConfig::default().with_polling(PollingConfig::new(Duration::from_millis(1), 3)),
        &server,
    )?;
    let error = client
        .solve(
            TaskType::RecaptchaV2TaskProxyless,
            &RecaptchaV2Task::default(),
        )
        .await
        .err()
        .ok_or_else(|| std::io::Error::other("expected an API error"))?;

    // A business error has a task_id field of its own, so it is filled in rather
    // than wrapped; wrapping would make an existing match miss.
    let Error::EzError(api_error) = error else {
        return Err(std::io::Error::other("a business error must not be wrapped").into());
    };
    assert_eq!(api_error.task_id.as_deref(), Some("task-123"));
    assert_eq!(api_error.request_id.as_deref(), Some("request-1"));

    create_mock.assert_async().await;
    result_mock.assert_async().await;
    Ok(())
}