polyoxide-gamma 0.15.0

Rust client library for Polymarket Gamma (market data) 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
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
use mockito::{Matcher, Server};
use polyoxide_gamma::{Gamma, GammaError};

fn test_gamma(server: &mockito::ServerGuard) -> Gamma {
    Gamma::builder().base_url(server.url()).build().unwrap()
}

#[tokio::test]
async fn list_markets_with_query_params() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/markets")
        .match_query(Matcher::AllOf(vec![
            Matcher::UrlEncoded("limit".into(), "5".into()),
            Matcher::UrlEncoded("closed".into(), "false".into()),
        ]))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"[{
                "id": "mkt-1",
                "conditionId": "0xcond1",
                "question": "Will it rain?",
                "description": "Market about weather",
                "marketMakerAddress": "0xaddr1"
            }]"#,
        )
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let markets = gamma
        .markets()
        .list()
        .limit(5)
        .open(true)
        .send()
        .await
        .unwrap();

    assert_eq!(markets.len(), 1);
    assert_eq!(markets[0].id, "mkt-1");
    assert_eq!(markets[0].condition_id, "0xcond1");
    assert_eq!(markets[0].question, "Will it rain?");

    mock.assert_async().await;
}

#[tokio::test]
async fn list_markets_open_false_sends_closed_true() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/markets")
        .match_query(Matcher::AllOf(vec![Matcher::UrlEncoded(
            "closed".into(),
            "true".into(),
        )]))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body("[]")
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let markets = gamma.markets().list().open(false).send().await.unwrap();

    assert!(markets.is_empty());
    mock.assert_async().await;
}

#[tokio::test]
async fn market_deserializes_volume_renamed_fields() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/markets/vol-test")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"{
                "id": "vol-test",
                "conditionId": "0xcond",
                "question": "Volume test?",
                "description": "Testing serde renames",
                "marketMakerAddress": "0xaddr",
                "volume24hr": 1500.5,
                "volume1wk": 10000.0,
                "volume1mo": 50000.0,
                "volume1yr": 200000.0,
                "denomationToken": "USDC",
                "volume24hrAmm": 100.0,
                "volume1wkClob": 9900.0
            }"#,
        )
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let market = gamma.markets().get("vol-test").send().await.unwrap();

    assert_eq!(market.volume_24hr, Some(1500.5));
    assert_eq!(market.volume_1wk, Some(10000.0));
    assert_eq!(market.volume_1mo, Some(50000.0));
    assert_eq!(market.volume_1yr, Some(200000.0));
    assert_eq!(market.denomination_token, Some("USDC".into()));
    assert_eq!(market.volume_24hr_amm, Some(100.0));
    assert_eq!(market.volume_1wk_clob, Some(9900.0));

    mock.assert_async().await;
}

#[tokio::test]
async fn list_events_with_query_params() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/events")
        .match_query(Matcher::AllOf(vec![
            Matcher::UrlEncoded("limit".into(), "3".into()),
            Matcher::UrlEncoded("active".into(), "true".into()),
        ]))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"[{
                "id": "evt-1",
                "title": "Test Event",
                "slug": "test-event"
            }]"#,
        )
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let events = gamma
        .events()
        .list()
        .limit(3)
        .active(true)
        .send()
        .await
        .unwrap();

    assert_eq!(events.len(), 1);
    assert_eq!(events[0].id, "evt-1");
    assert_eq!(events[0].title, Some("Test Event".into()));

    mock.assert_async().await;
}

#[tokio::test]
async fn get_event_by_id() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/events/evt-42")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"{
                "id": "evt-42",
                "title": "Single Event",
                "slug": "single-event",
                "markets": [{
                    "id": "mkt-nested",
                    "conditionId": "0xcond",
                    "question": "Nested?",
                    "description": "Nested market",
                    "marketMakerAddress": "0xaddr"
                }]
            }"#,
        )
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let event = gamma.events().get("evt-42").send().await.unwrap();

    assert_eq!(event.id, "evt-42");
    assert_eq!(event.title, Some("Single Event".into()));
    assert_eq!(event.markets.len(), 1);
    assert_eq!(event.markets[0].id, "mkt-nested");

    mock.assert_async().await;
}

#[tokio::test]
async fn get_market_by_id() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/markets/abc")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"{
                "id": "abc",
                "conditionId": "0xcondabc",
                "question": "Single market?",
                "description": "A single market",
                "marketMakerAddress": "0xmaker"
            }"#,
        )
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let market = gamma.markets().get("abc").send().await.unwrap();

    assert_eq!(market.id, "abc");
    assert_eq!(market.condition_id, "0xcondabc");

    mock.assert_async().await;
}

#[tokio::test]
async fn error_404_returns_api_error() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/markets/nonexistent")
        .with_status(404)
        .with_header("content-type", "application/json")
        .with_body(r#"{"error": "not found"}"#)
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let err = gamma.markets().get("nonexistent").send().await.unwrap_err();

    match err {
        GammaError::Api(polyoxide_core::ApiError::Api { status, message }) => {
            assert_eq!(status, 404);
            assert_eq!(message, "not found");
        }
        other => panic!("Expected ApiError::Api(404), got: {:?}", other),
    }

    mock.assert_async().await;
}

#[tokio::test]
async fn error_401_returns_authentication_error() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/markets/secret")
        .with_status(401)
        .with_header("content-type", "application/json")
        .with_body(r#"{"error": "unauthorized"}"#)
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let err = gamma.markets().get("secret").send().await.unwrap_err();

    match err {
        GammaError::Api(polyoxide_core::ApiError::Authentication(msg)) => {
            assert_eq!(msg, "unauthorized");
        }
        other => panic!("Expected Authentication error, got: {:?}", other),
    }

    mock.assert_async().await;
}

#[tokio::test]
async fn error_400_returns_validation_error() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/markets")
        .with_status(400)
        .with_header("content-type", "application/json")
        .with_body(r#"{"error": "invalid limit parameter"}"#)
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let err = gamma.markets().list().send().await.unwrap_err();

    match err {
        GammaError::Api(polyoxide_core::ApiError::Validation(msg)) => {
            assert_eq!(msg, "invalid limit parameter");
        }
        other => panic!("Expected Validation error, got: {:?}", other),
    }

    mock.assert_async().await;
}

#[tokio::test]
async fn get_many_fans_out_closed_true_and_false() {
    let mut server = Server::new_async().await;

    // Regex (not Matcher::UrlEncoded) because mockito 1.7's UrlEncoded matcher
    // cannot assert multiple values for the same key (`id=1&id=2`) — anchored
    // regex is the reliable way to pin a specific repeated-key query pair.
    let mock_closed = server
        .mock("GET", "/markets")
        .match_query(Matcher::AllOf(vec![
            Matcher::Regex(r"(^|&)id=1(&|$)".into()),
            Matcher::Regex(r"(^|&)id=2(&|$)".into()),
            Matcher::Regex(r"(^|&)closed=true(&|$)".into()),
        ]))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"[{
                "id": "1",
                "conditionId": "0xcond1",
                "question": "Closed market?",
                "description": "A closed market",
                "marketMakerAddress": "0xaddr1",
                "closed": true
            }]"#,
        )
        .expect(1)
        .create_async()
        .await;

    let mock_open = server
        .mock("GET", "/markets")
        .match_query(Matcher::AllOf(vec![
            Matcher::Regex(r"(^|&)id=1(&|$)".into()),
            Matcher::Regex(r"(^|&)id=2(&|$)".into()),
            Matcher::Regex(r"(^|&)closed=false(&|$)".into()),
        ]))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"[{
                "id": "2",
                "conditionId": "0xcond2",
                "question": "Open market?",
                "description": "An open market",
                "marketMakerAddress": "0xaddr2",
                "closed": false
            }]"#,
        )
        .expect(1)
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let markets = gamma.markets().get_many([1i64, 2]).send().await.unwrap();

    assert_eq!(markets.len(), 2);
    let ids: Vec<&str> = markets.iter().map(|m| m.id.as_str()).collect();
    assert!(ids.contains(&"1"), "missing closed market in merged result");
    assert!(ids.contains(&"2"), "missing open market in merged result");
    assert!(
        markets.iter().any(|m| m.closed == Some(true)),
        "merged result should include the closed market"
    );

    mock_closed.assert_async().await;
    mock_open.assert_async().await;
}

#[tokio::test]
async fn get_many_empty_ids_short_circuits() {
    let mut server = Server::new_async().await;

    // Expect zero hits to /markets — an empty-id call must not touch the network.
    let mock = server
        .mock("GET", "/markets")
        .with_status(500)
        .expect(0)
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let markets = gamma
        .markets()
        .get_many(Vec::<i64>::new())
        .send()
        .await
        .unwrap();

    assert!(markets.is_empty());
    mock.assert_async().await;
}

#[tokio::test]
async fn list_event_creators_hits_endpoint_with_params() {
    let mut server = Server::new_async().await;
    let mock = server
        .mock("GET", "/events/creators")
        .match_query(Matcher::AllOf(vec![
            Matcher::UrlEncoded("limit".into(), "10".into()),
            Matcher::UrlEncoded("creator_handle".into(), "poly".into()),
        ]))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"[{
                "id": "1",
                "creatorName": "Polymarket",
                "creatorHandle": "poly"
            }]"#,
        )
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let creators = gamma
        .events()
        .list_creators()
        .limit(10)
        .creator_handle("poly")
        .send()
        .await
        .unwrap();

    assert_eq!(creators.len(), 1);
    assert_eq!(creators[0].id, "1");
    assert_eq!(creators[0].creator_handle.as_deref(), Some("poly"));
    mock.assert_async().await;
}

#[tokio::test]
async fn get_event_creator_by_id() {
    let mut server = Server::new_async().await;
    let mock = server
        .mock("GET", "/events/creators/42")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"id": "42", "creatorName": "Poly"}"#)
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let creator = gamma.events().get_creator("42").send().await.unwrap();
    assert_eq!(creator.id, "42");
    assert_eq!(creator.creator_name.as_deref(), Some("Poly"));
    mock.assert_async().await;
}

#[tokio::test]
async fn list_events_pagination_returns_wrapper() {
    let mut server = Server::new_async().await;
    let mock = server
        .mock("GET", "/events/pagination")
        .match_query(Matcher::AllOf(vec![
            Matcher::UrlEncoded("limit".into(), "5".into()),
            Matcher::UrlEncoded("include_chat".into(), "true".into()),
        ]))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"{
                "data": [{"id": "e1"}],
                "pagination": {"hasMore": true, "totalResults": 42}
            }"#,
        )
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let resp = gamma
        .events()
        .list_paginated()
        .limit(5)
        .include_chat(true)
        .send()
        .await
        .unwrap();
    assert_eq!(resp.data.len(), 1);
    assert_eq!(resp.data[0].id, "e1");
    let p = resp.pagination.unwrap();
    assert_eq!(p.has_more, Some(true));
    assert_eq!(p.total_results, Some(42));
    mock.assert_async().await;
}

#[tokio::test]
async fn list_events_results_returns_vec() {
    let mut server = Server::new_async().await;
    let mock = server
        .mock("GET", "/events/results")
        .match_query(Matcher::UrlEncoded("limit".into(), "3".into()))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"[{"id": "r1"}, {"id": "r2"}]"#)
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let events = gamma.events().list_results().limit(3).send().await.unwrap();
    assert_eq!(events.len(), 2);
    assert_eq!(events[0].id, "r1");
    mock.assert_async().await;
}

#[tokio::test]
async fn list_events_keyset_returns_cursor() {
    let mut server = Server::new_async().await;
    let mock = server
        .mock("GET", "/events/keyset")
        .match_query(Matcher::AllOf(vec![
            Matcher::UrlEncoded("limit".into(), "2".into()),
            Matcher::UrlEncoded("after_cursor".into(), "abc123".into()),
            Matcher::UrlEncoded("closed".into(), "false".into()),
        ]))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"{
                "events": [{"id": "k1", "title": "Keyset"}, {"id": "k2"}],
                "next_cursor": "next-token"
            }"#,
        )
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let resp = gamma
        .events()
        .list_keyset()
        .limit(2)
        .after_cursor("abc123")
        .closed(false)
        .send()
        .await
        .unwrap();
    assert_eq!(resp.events.len(), 2);
    assert_eq!(resp.events[0].id, "k1");
    assert_eq!(resp.events[0].title.as_deref(), Some("Keyset"));
    assert_eq!(resp.next_cursor.as_deref(), Some("next-token"));
    mock.assert_async().await;
}

#[tokio::test]
async fn list_events_keyset_last_page_has_no_cursor() {
    let mut server = Server::new_async().await;
    let mock = server
        .mock("GET", "/events/keyset")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"events": []}"#)
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let resp = gamma.events().list_keyset().send().await.unwrap();
    assert!(resp.events.is_empty());
    assert!(resp.next_cursor.is_none());
    mock.assert_async().await;
}

#[tokio::test]
async fn malformed_json_returns_serialization_error() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/markets/bad")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"not_valid_market_json": true"#)
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let err = gamma.markets().get("bad").send().await.unwrap_err();

    match err {
        GammaError::Api(polyoxide_core::ApiError::Serialization(_)) => {}
        other => panic!("Expected Serialization error, got: {:?}", other),
    }

    mock.assert_async().await;
}

// ── /markets/{id}/description ──────────────────────────────────

#[tokio::test]
async fn get_market_description_returns_text() {
    let mut server = Server::new_async().await;
    let mock = server
        .mock("GET", "/markets/12345/description")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"description": "Will X happen?"}"#)
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let desc = gamma
        .markets()
        .get_description("12345")
        .send()
        .await
        .unwrap();
    assert_eq!(desc.description.as_deref(), Some("Will X happen?"));
    mock.assert_async().await;
}

// ── /markets/information (POST) ────────────────────────────────

#[tokio::test]
async fn query_markets_by_information_posts_body() {
    use polyoxide_gamma::types::MarketsInformationBody;

    let mut server = Server::new_async().await;
    let mock = server
        .mock("POST", "/markets/information")
        .match_header("content-type", "application/json")
        .match_query(Matcher::UrlEncoded("limit".into(), "1000".into()))
        .match_body(Matcher::JsonString(
            r#"{"id":[1,2],"closed":true}"#.to_string(),
        ))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"[{
                "id": "1",
                "conditionId": "0xcond",
                "description": "Desc",
                "question": "Q?",
                "marketMakerAddress": "0xmm"
            }]"#,
        )
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let body = MarketsInformationBody {
        id: vec![1, 2],
        closed: Some(true),
        ..Default::default()
    };
    let markets = gamma
        .markets()
        .query_by_information(body)
        .send()
        .await
        .unwrap();
    assert_eq!(markets.len(), 1);
    assert_eq!(markets[0].id, "1");
    mock.assert_async().await;
}

#[tokio::test]
async fn query_markets_by_information_sends_explicit_pagination() {
    use polyoxide_gamma::types::MarketsInformationBody;

    let mut server = Server::new_async().await;
    let mock = server
        .mock("POST", "/markets/information")
        .match_query(Matcher::AllOf(vec![
            Matcher::UrlEncoded("limit".into(), "500".into()),
            Matcher::UrlEncoded("offset".into(), "1000".into()),
        ]))
        .match_body(Matcher::JsonString(r#"{"id":[42]}"#.to_string()))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body("[]")
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let body = MarketsInformationBody {
        id: vec![42],
        ..Default::default()
    };
    let markets = gamma
        .markets()
        .query_by_information(body)
        .limit(500)
        .offset(1000)
        .send()
        .await
        .unwrap();
    assert!(markets.is_empty());
    mock.assert_async().await;
}

// ── /markets/abridged (POST) ───────────────────────────────────

#[tokio::test]
async fn query_abridged_markets_posts_body() {
    use polyoxide_gamma::types::MarketsInformationBody;

    let mut server = Server::new_async().await;
    let mock = server
        .mock("POST", "/markets/abridged")
        .match_header("content-type", "application/json")
        .match_query(Matcher::UrlEncoded("limit".into(), "1000".into()))
        .match_body(Matcher::JsonString(
            r#"{"slug":["mkt-a"],"includeTags":true}"#.to_string(),
        ))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"[{
                "id": "7",
                "conditionId": "0xcond7",
                "description": "Desc",
                "question": "Q?",
                "marketMakerAddress": "0xmm"
            }]"#,
        )
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let body = MarketsInformationBody {
        slug: vec!["mkt-a".into()],
        include_tags: Some(true),
        ..Default::default()
    };
    let markets = gamma.markets().query_abridged(body).send().await.unwrap();
    assert_eq!(markets.len(), 1);
    assert_eq!(markets[0].id, "7");
    mock.assert_async().await;
}

// ── /markets/keyset ────────────────────────────────────────────

#[tokio::test]
async fn list_markets_keyset_returns_cursor() {
    let mut server = Server::new_async().await;
    let mock = server
        .mock("GET", "/markets/keyset")
        .match_query(Matcher::AllOf(vec![
            Matcher::UrlEncoded("limit".into(), "2".into()),
            Matcher::UrlEncoded("after_cursor".into(), "abc123".into()),
            Matcher::UrlEncoded("closed".into(), "false".into()),
        ]))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"{
                "markets": [
                    {
                        "id": "k1",
                        "conditionId": "0xcond1",
                        "description": "d1",
                        "question": "q1",
                        "marketMakerAddress": "0xmm1"
                    },
                    {
                        "id": "k2",
                        "conditionId": "0xcond2",
                        "description": "d2",
                        "question": "q2",
                        "marketMakerAddress": "0xmm2"
                    }
                ],
                "next_cursor": "next-token"
            }"#,
        )
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let resp = gamma
        .markets()
        .list_keyset()
        .limit(2)
        .after_cursor("abc123")
        .closed(false)
        .send()
        .await
        .unwrap();
    assert_eq!(resp.markets.len(), 2);
    assert_eq!(resp.markets[0].id, "k1");
    assert_eq!(resp.next_cursor.as_deref(), Some("next-token"));
    mock.assert_async().await;
}

#[tokio::test]
async fn list_markets_keyset_last_page_has_no_cursor() {
    let mut server = Server::new_async().await;
    let mock = server
        .mock("GET", "/markets/keyset")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"markets": []}"#)
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let resp = gamma.markets().list_keyset().send().await.unwrap();
    assert!(resp.markets.is_empty());
    assert!(resp.next_cursor.is_none());
    mock.assert_async().await;
}

// ── /series-summary/{id} and /series-summary/slug/{slug} ───────

#[tokio::test]
async fn get_series_summary_by_id() {
    let mut server = Server::new_async().await;
    let mock = server
        .mock("GET", "/series-summary/s-1")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"{
                "id": "s-1",
                "title": "NFL",
                "slug": "nfl",
                "eventDates": ["2025-09-01"],
                "eventWeeks": [1, 2],
                "earliest_open_week": 1
            }"#,
        )
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let summary = gamma.series().get_summary("s-1").send().await.unwrap();
    assert_eq!(summary.id, "s-1");
    assert_eq!(summary.title.as_deref(), Some("NFL"));
    assert_eq!(summary.event_weeks, vec![1, 2]);
    mock.assert_async().await;
}

#[tokio::test]
async fn get_series_summary_by_slug() {
    let mut server = Server::new_async().await;
    let mock = server
        .mock("GET", "/series-summary/slug/nfl")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"id": "s-1", "slug": "nfl"}"#)
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let summary = gamma
        .series()
        .get_summary_by_slug("nfl")
        .send()
        .await
        .unwrap();
    assert_eq!(summary.slug.as_deref(), Some("nfl"));
    mock.assert_async().await;
}

// ── /series/{id}/comments/count ────────────────────────────────

#[tokio::test]
async fn get_series_comment_count() {
    let mut server = Server::new_async().await;
    let mock = server
        .mock("GET", "/series/s-1/comments/count")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"count": 42}"#)
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let count = gamma.series().comment_count("s-1").send().await.unwrap();
    assert_eq!(count.count, 42);
    mock.assert_async().await;
}

// ── /teams/{id} ────────────────────────────────────────────────

#[tokio::test]
async fn get_team_by_id() {
    let mut server = Server::new_async().await;
    let mock = server
        .mock("GET", "/teams/100")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"{
                "id": 100,
                "name": "Team X",
                "league": "NFL",
                "abbreviation": "TX"
            }"#,
        )
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let team = gamma.sports().get_team("100").send().await.unwrap();
    assert_eq!(team.id, 100);
    assert_eq!(team.name.as_deref(), Some("Team X"));
    mock.assert_async().await;
}

// ── /profiles/user_address/{address} ───────────────────────────

#[tokio::test]
async fn get_profile_by_address() {
    let mut server = Server::new_async().await;
    let mock = server
        .mock("GET", "/profiles/user_address/0xdeadbeef")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"{
                "id": "p-1",
                "name": "Alice",
                "proxyWallet": "0xdeadbeef",
                "walletActivated": true
            }"#,
        )
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let profile = gamma
        .user()
        .get_by_address("0xdeadbeef")
        .send()
        .await
        .unwrap();
    assert_eq!(profile.id, "p-1");
    assert_eq!(profile.name.as_deref(), Some("Alice"));
    assert_eq!(profile.proxy_wallet.as_deref(), Some("0xdeadbeef"));
    assert_eq!(profile.wallet_activated, Some(true));
    mock.assert_async().await;
}

#[tokio::test]
async fn get_related_detailed_returns_vec_of_tags() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/tags/42/related-tags/tags")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"[
                {
                    "id": "101",
                    "slug": "politics",
                    "label": "Politics",
                    "forceShow": true,
                    "publishedAt": "2024-01-01T00:00:00Z",
                    "isCarousel": false
                },
                {
                    "id": "102",
                    "slug": "elections",
                    "label": "Elections"
                }
            ]"#,
        )
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let tags = gamma
        .tags()
        .get_related_detailed("42")
        .send()
        .await
        .unwrap();
    assert_eq!(tags.len(), 2);
    assert_eq!(tags[0].id, "101");
    assert_eq!(tags[0].slug, "politics");
    assert_eq!(tags[0].label, "Politics");
    assert_eq!(tags[0].force_show, Some(true));
    assert_eq!(tags[0].is_carousel, Some(false));
    assert_eq!(tags[1].id, "102");
    assert_eq!(tags[1].force_show, None);
    mock.assert_async().await;
}

#[tokio::test]
async fn get_related_detailed_by_slug_returns_vec_of_tags() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock("GET", "/tags/slug/politics/related-tags/tags")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"[
                {
                    "id": "55",
                    "slug": "us-election",
                    "label": "US Election"
                }
            ]"#,
        )
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let tags = gamma
        .tags()
        .get_related_detailed_by_slug("politics")
        .send()
        .await
        .unwrap();
    assert_eq!(tags.len(), 1);
    assert_eq!(tags[0].id, "55");
    assert_eq!(tags[0].slug, "us-election");
    assert_eq!(tags[0].label, "US Election");
    mock.assert_async().await;
}

#[tokio::test]
async fn get_related_detailed_by_slug_url_encodes_slug() {
    let mut server = Server::new_async().await;

    // Slug with characters that require URL encoding
    let mock = server
        .mock("GET", "/tags/slug/us%2Felection/related-tags/tags")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body("[]")
        .create_async()
        .await;

    let gamma = test_gamma(&server);
    let tags = gamma
        .tags()
        .get_related_detailed_by_slug("us/election")
        .send()
        .await
        .unwrap();
    assert!(tags.is_empty());
    mock.assert_async().await;
}