crispy-xtream 0.1.1

Async Xtream Codes API client
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
//! Integration tests for `XtreamClient` using wiremock.
//!
//! Each test spins up a mock server that simulates Xtream Codes API responses.

use wiremock::matchers::{method, path, query_param};
use wiremock::{Mock, MockServer, ResponseTemplate};

use crispy_xtream::{StreamType, XtreamClient, XtreamClientConfig, XtreamCredentials};

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

async fn setup() -> (MockServer, XtreamClient) {
    let server = MockServer::start().await;
    let creds = XtreamCredentials::new(server.uri(), "testuser", "testpass");
    let client = XtreamClient::with_config(creds, XtreamClientConfig::default()).unwrap();
    (server, client)
}

fn profile_json() -> &'static str {
    r#"{
        "user_info": {
            "username": "testuser",
            "password": "testpass",
            "message": "Welcome",
            "auth": 1,
            "status": "Active",
            "exp_date": "1735689600",
            "is_trial": "0",
            "active_cons": 0,
            "created_at": "1704067200",
            "max_connections": "1",
            "allowed_output_formats": ["ts", "m3u8"]
        },
        "server_info": {
            "xui": true,
            "version": "1.5.12",
            "revision": null,
            "url": "example.com",
            "port": "80",
            "https_port": "443",
            "server_protocol": "http",
            "rtmp_port": "8880",
            "timezone": "UTC",
            "timestamp_now": 1704067200,
            "time_now": "2024-01-01 00:00:00"
        }
    }"#
}

/// Mount the profile mock (needed by methods that auto-authenticate).
async fn mount_profile(server: &MockServer) {
    Mock::given(method("GET"))
        .and(path("/player_api.php"))
        .and(query_param("action", "get_profile"))
        .respond_with(ResponseTemplate::new(200).set_body_raw(profile_json(), "application/json"))
        .mount(server)
        .await;
}

// ---------------------------------------------------------------------------
// 1. Authenticate
// ---------------------------------------------------------------------------

#[tokio::test]
async fn authenticate_parses_profile() {
    let (server, client) = setup().await;
    mount_profile(&server).await;

    let profile = client.authenticate().await.unwrap();
    assert_eq!(profile.user_info.username, "testuser");
    assert_eq!(profile.user_info.auth, 1);
    assert_eq!(profile.user_info.status, "Active");
    assert_eq!(profile.user_info.allowed_output_formats, vec!["ts", "m3u8"]);
    assert_eq!(profile.server_info.timezone.as_deref(), Some("UTC"));
}

// ---------------------------------------------------------------------------
// 2. Get live categories
// ---------------------------------------------------------------------------

#[tokio::test]
async fn get_live_categories() {
    let (server, client) = setup().await;

    let body = r#"[
        {"category_id": "1", "category_name": "Sports", "parent_id": 0},
        {"category_id": "2", "category_name": "News", "parent_id": 0}
    ]"#;

    Mock::given(method("GET"))
        .and(path("/player_api.php"))
        .and(query_param("action", "get_live_categories"))
        .respond_with(ResponseTemplate::new(200).set_body_raw(body, "application/json"))
        .mount(&server)
        .await;

    let cats = client.get_live_categories().await.unwrap();
    assert_eq!(cats.len(), 2);
    assert_eq!(cats[0].category_id, "1");
    assert_eq!(cats[0].category_name, "Sports");
    assert_eq!(cats[1].category_name, "News");
}

// ---------------------------------------------------------------------------
// 3. Get live streams
// ---------------------------------------------------------------------------

#[tokio::test]
async fn get_live_streams_with_urls() {
    let (server, client) = setup().await;
    mount_profile(&server).await;

    let body = r#"[
        {
            "num": 1,
            "name": "BBC One",
            "stream_type": "live",
            "stream_id": 42,
            "stream_icon": "http://img.example.com/bbc1.png",
            "thumbnail": "",
            "epg_channel_id": "bbc1.uk",
            "added": "1704067200",
            "category_id": "1",
            "category_ids": [1],
            "custom_sid": "",
            "tv_archive": 1,
            "direct_source": "",
            "tv_archive_duration": 7
        }
    ]"#;

    Mock::given(method("GET"))
        .and(path("/player_api.php"))
        .and(query_param("action", "get_live_streams"))
        .respond_with(ResponseTemplate::new(200).set_body_raw(body, "application/json"))
        .mount(&server)
        .await;

    let channels = client.get_live_streams(None).await.unwrap();
    assert_eq!(channels.len(), 1);
    assert_eq!(channels[0].name, "BBC One");
    assert_eq!(channels[0].stream_id, 42);

    // Verify generated stream URL.
    let url = channels[0].url.as_ref().unwrap();
    assert!(
        url.ends_with("/live/testuser/testpass/42.ts"),
        "url was: {url}"
    );
}

// ---------------------------------------------------------------------------
// 4. Get VOD streams
// ---------------------------------------------------------------------------

#[tokio::test]
async fn get_vod_streams_with_urls() {
    let (server, client) = setup().await;
    mount_profile(&server).await;

    let body = r#"[
        {
            "num": 1,
            "name": "Test Movie",
            "stream_id": 99,
            "stream_icon": "",
            "rating": 7.5,
            "rating_5based": 3.75,
            "container_extension": "mp4",
            "category_id": "5",
            "category_ids": [5],
            "added": "1704067200"
        }
    ]"#;

    Mock::given(method("GET"))
        .and(path("/player_api.php"))
        .and(query_param("action", "get_vod_streams"))
        .respond_with(ResponseTemplate::new(200).set_body_raw(body, "application/json"))
        .mount(&server)
        .await;

    let movies = client.get_vod_streams(None).await.unwrap();
    assert_eq!(movies.len(), 1);
    assert_eq!(movies[0].name, "Test Movie");
    assert_eq!(movies[0].stream_id, 99);

    let url = movies[0].url.as_ref().unwrap();
    assert!(
        url.ends_with("/movie/testuser/testpass/99.mp4"),
        "url was: {url}"
    );
}

// ---------------------------------------------------------------------------
// 5. Get series + series_info
// ---------------------------------------------------------------------------

#[tokio::test]
async fn get_series_info_with_episodes() {
    let (server, client) = setup().await;

    let body = r#"{
        "seasons": [
            {
                "id": 1,
                "name": "Season 1",
                "episode_count": 2,
                "season_number": 1
            }
        ],
        "info": {
            "name": "Test Show",
            "title": "Test Show",
            "cover": "http://img.example.com/show.png",
            "category_id": "10",
            "category_ids": [10]
        },
        "episodes": {
            "1": [
                {
                    "id": "501",
                    "episode_num": "1",
                    "title": "Pilot",
                    "container_extension": "mkv",
                    "season": 1,
                    "added": "1704067200",
                    "info": {
                        "duration_secs": 2700,
                        "duration": "00:45:00",
                        "rating": 8.0
                    }
                },
                {
                    "id": "502",
                    "episode_num": "2",
                    "title": "Second",
                    "container_extension": "mkv",
                    "season": 1
                }
            ]
        }
    }"#;

    Mock::given(method("GET"))
        .and(path("/player_api.php"))
        .and(query_param("action", "get_series_info"))
        .and(query_param("series_id", "100"))
        .respond_with(ResponseTemplate::new(200).set_body_raw(body, "application/json"))
        .mount(&server)
        .await;

    let show = client.get_series_info(100).await.unwrap();
    assert_eq!(
        show.info.as_ref().unwrap().name.as_deref(),
        Some("Test Show")
    );
    assert_eq!(show.info.as_ref().unwrap().series_id, Some(100));
    assert_eq!(show.seasons.len(), 1);

    let eps = show.episodes.get("1").unwrap();
    assert_eq!(eps.len(), 2);
    assert_eq!(eps[0].title.as_deref(), Some("Pilot"));

    // Episode URLs attached.
    let ep_url = eps[0].url.as_ref().unwrap();
    assert!(
        ep_url.ends_with("/series/testuser/testpass/501.mkv"),
        "url was: {ep_url}"
    );
}

// ---------------------------------------------------------------------------
// 6. Get short EPG with base64 decoding
// ---------------------------------------------------------------------------

#[tokio::test]
async fn get_short_epg_decodes_base64() {
    let (server, client) = setup().await;

    // "News" = TmV3cw==, "Daily news" = RGFpbHkgbmV3cw==
    let body = r#"{
        "epg_listings": [
            {
                "id": "123",
                "epg_id": "bbc1.uk",
                "title": "TmV3cw==",
                "lang": "en",
                "start": "2024-01-01 10:00:00",
                "end": "2024-01-01 11:00:00",
                "description": "RGFpbHkgbmV3cw==",
                "channel_id": "bbc1.uk",
                "start_timestamp": "1704106800",
                "stop_timestamp": "1704110400",
                "stop": "2024-01-01 11:00:00"
            }
        ]
    }"#;

    Mock::given(method("GET"))
        .and(path("/player_api.php"))
        .and(query_param("action", "get_short_epg"))
        .and(query_param("stream_id", "42"))
        .and(query_param("limit", "10"))
        .respond_with(ResponseTemplate::new(200).set_body_raw(body, "application/json"))
        .mount(&server)
        .await;

    let epg = client.get_short_epg(42, Some(10)).await.unwrap();
    assert_eq!(epg.epg_listings.len(), 1);
    assert_eq!(epg.epg_listings[0].title.as_deref(), Some("News"));
    assert_eq!(
        epg.epg_listings[0].description.as_deref(),
        Some("Daily news")
    );
}

// ---------------------------------------------------------------------------
// 7. Build stream URL format
// ---------------------------------------------------------------------------

#[tokio::test]
async fn build_stream_url_format() {
    let (_, client) = setup().await;
    let url = client.stream_url(StreamType::Channel, 42, "ts");
    assert!(url.contains("/live/testuser/testpass/42.ts"));
}

// ---------------------------------------------------------------------------
// 8. Build XMLTV URL format
// ---------------------------------------------------------------------------

#[tokio::test]
async fn build_xmltv_url_format() {
    let (_, client) = setup().await;
    let url = client.xmltv_url();
    assert!(url.contains("/xmltv.php?username=testuser&password=testpass"));
}

// ---------------------------------------------------------------------------
// 9. Build catchup/timeshift URL format
// ---------------------------------------------------------------------------

#[tokio::test]
async fn build_catchup_url_format() {
    let (_, client) = setup().await;
    let url = client.timeshift_url(42, 120, "2024-01-01:10-00");
    assert!(url.contains("/timeshift/testuser/testpass/120/2024-01-01:10-00/42.ts"));
}

// ---------------------------------------------------------------------------
// 10. Auth failure (401/403)
// ---------------------------------------------------------------------------

#[tokio::test]
async fn auth_failure_returns_error() {
    let (server, client) = setup().await;

    Mock::given(method("GET"))
        .and(path("/player_api.php"))
        .and(query_param("action", "get_profile"))
        .respond_with(ResponseTemplate::new(403).set_body_string("Forbidden"))
        .mount(&server)
        .await;

    let result = client.authenticate().await;
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(
        matches!(err, crispy_xtream::XtreamError::Auth(_)),
        "got: {err:?}"
    );
}

// ---------------------------------------------------------------------------
// 11. Rate limiting (429)
// ---------------------------------------------------------------------------

#[tokio::test]
async fn rate_limited_returns_error() {
    let (server, client) = setup().await;

    Mock::given(method("GET"))
        .and(path("/player_api.php"))
        .and(query_param("action", "get_live_categories"))
        .respond_with(
            ResponseTemplate::new(429)
                .insert_header("Retry-After", "30")
                .set_body_string("Too Many Requests"),
        )
        .mount(&server)
        .await;

    let result = client.get_live_categories().await;
    assert!(result.is_err());

    match result.unwrap_err() {
        crispy_xtream::XtreamError::RateLimited { retry_after_secs } => {
            assert_eq!(retry_after_secs, 30);
        }
        other => panic!("expected RateLimited, got: {other:?}"),
    }
}

// ---------------------------------------------------------------------------
// 12. Malformed JSON → graceful error
// ---------------------------------------------------------------------------

#[tokio::test]
async fn malformed_json_returns_error() {
    let (server, client) = setup().await;

    Mock::given(method("GET"))
        .and(path("/player_api.php"))
        .and(query_param("action", "get_live_categories"))
        .respond_with(
            ResponseTemplate::new(200).set_body_raw("not json at all", "application/json"),
        )
        .mount(&server)
        .await;

    let result = client.get_live_categories().await;
    assert!(result.is_err());
    assert!(matches!(
        result.unwrap_err(),
        crispy_xtream::XtreamError::UnexpectedResponse(_)
    ),);
}