crispy-stalker 0.1.1

Async Stalker/MAG portal 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
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
//! Integration tests for `StalkerClient` using wiremock.

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

use crispy_stalker::{StalkerClient, StalkerCredentials};

const TEST_MAC: &str = "00:1A:79:AB:CD:EF";

/// Build a client pointed at the given mock server.
fn test_client(server: &MockServer) -> StalkerClient {
    let creds = StalkerCredentials {
        base_url: server.uri(),
        mac_address: TEST_MAC.into(),
        timezone: None,
    };
    let http = reqwest::Client::builder()
        .build()
        .expect("failed to build http client");
    StalkerClient::with_http_client(creds, http)
}

/// Mount the standard auth flow: discovery at `/c/`, handshake, do_auth, get_profile.
async fn mount_full_auth(server: &MockServer, token: &str) {
    // Discovery: plain GET to /c/ returns 200
    Mock::given(method("GET"))
        .and(path("/c/"))
        .respond_with(ResponseTemplate::new(200).set_body_string("OK"))
        .up_to_n_times(1)
        .mount(server)
        .await;

    // Handshake
    Mock::given(method("GET"))
        .and(path("/c/"))
        .and(query_param("type", "stb"))
        .and(query_param("action", "handshake"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "js": { "token": token }
        })))
        .mount(server)
        .await;

    // do_auth
    Mock::given(method("GET"))
        .and(path("/c/"))
        .and(query_param("type", "stb"))
        .and(query_param("action", "do_auth"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"js": true})))
        .mount(server)
        .await;

    // get_profile (called during authentication to fully activate session)
    Mock::given(method("GET"))
        .and(path("/c/"))
        .and(query_param("type", "stb"))
        .and(query_param("action", "get_profile"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "js": { "token": token, "timezone": "Europe/Paris", "locale": "en" }
        })))
        .mount(server)
        .await;
}

/// Mount all auth mocks and authenticate the client.
async fn authenticate_client(server: &MockServer) -> StalkerClient {
    mount_full_auth(server, "test_token_123").await;
    let mut client = test_client(server);
    client.authenticate().await.expect("authentication failed");
    client
}

#[tokio::test]
async fn portal_discovery_finds_stalker_portal_path() {
    let server = MockServer::start().await;

    // /stalker_portal/c/ responds 200
    Mock::given(method("GET"))
        .and(path("/stalker_portal/c/"))
        .respond_with(ResponseTemplate::new(200).set_body_string("OK"))
        .up_to_n_times(1)
        .mount(&server)
        .await;

    // /c/ responds 404 so discovery skips it
    // (stalker_portal is first in the list, so it should be tried first)

    // Handshake at /stalker_portal/c/
    Mock::given(method("GET"))
        .and(path("/stalker_portal/c/"))
        .and(query_param("action", "handshake"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "js": { "token": "tk" }
        })))
        .mount(&server)
        .await;

    // do_auth at /stalker_portal/c/
    Mock::given(method("GET"))
        .and(path("/stalker_portal/c/"))
        .and(query_param("action", "do_auth"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"js": true})))
        .mount(&server)
        .await;

    // get_profile at /stalker_portal/c/
    Mock::given(method("GET"))
        .and(path("/stalker_portal/c/"))
        .and(query_param("action", "get_profile"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "js": { "token": "tk", "timezone": "Europe/Paris" }
        })))
        .mount(&server)
        .await;

    let mut client = test_client(&server);
    client.authenticate().await.unwrap();
    assert!(client.portal_url().unwrap().contains("/stalker_portal/c/"));
}

#[tokio::test]
async fn handshake_extracts_token_and_authenticates() {
    let server = MockServer::start().await;
    mount_full_auth(&server, "my_token_abc").await;

    let mut client = test_client(&server);
    client.authenticate().await.unwrap();
    assert!(client.is_authenticated());
}

#[tokio::test]
async fn get_genres_parses_categories() {
    let server = MockServer::start().await;
    let client = authenticate_client(&server).await;

    Mock::given(method("GET"))
        .and(query_param("type", "itv"))
        .and(query_param("action", "get_genres"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "js": [
                {"id": "1", "title": "News", "censored": "0"},
                {"id": "2", "title": "Sports", "censored": "0"},
                {"id": "99", "title": "Adult", "censored": "1"}
            ]
        })))
        .mount(&server)
        .await;

    let genres = client.get_genres().await.unwrap();
    assert_eq!(genres.len(), 3);
    assert_eq!(genres[0].title, "News");
    assert_eq!(genres[2].title, "Adult");
    assert!(genres[2].is_adult);
}

#[tokio::test]
async fn get_channels_single_page() {
    let server = MockServer::start().await;
    let client = authenticate_client(&server).await;

    Mock::given(method("GET"))
        .and(query_param("type", "itv"))
        .and(query_param("action", "get_ordered_list"))
        .and(query_param("genre", "5"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "js": {
                "total_items": "2",
                "max_page_items": "10",
                "data": [
                    {
                        "id": "1", "name": "Channel One", "number": "1",
                        "cmd": "ffrt http://stream.example.com/ch1",
                        "tv_genre_id": "5",
                        "logo": "http://example.com/ch1.png",
                        "xmltv_id": "ch1.example",
                        "tv_archive": "1", "tv_archive_duration": "3",
                        "censored": "0"
                    },
                    {
                        "id": "2", "name": "Channel Two", "number": "2",
                        "cmd": "http://stream.example.com/ch2",
                        "tv_genre_id": "5", "logo": "",
                        "tv_archive": "0", "tv_archive_duration": "0",
                        "censored": "0"
                    }
                ]
            }
        })))
        .mount(&server)
        .await;

    let result = client.get_channels_page("5", 1).await.unwrap();
    assert_eq!(result.total_items, 2);
    assert_eq!(result.items.len(), 2);

    let ch1 = &result.items[0];
    assert_eq!(ch1.name, "Channel One");
    assert_eq!(ch1.number, Some(1));
    assert!(ch1.has_archive);
    assert_eq!(ch1.archive_days, 3);
    assert_eq!(ch1.epg_channel_id.as_deref(), Some("ch1.example"));

    let ch2 = &result.items[1];
    assert!(ch2.logo.is_none()); // empty string filtered to None
}

#[tokio::test]
async fn get_all_channels_multi_page() {
    let server = MockServer::start().await;
    let client = authenticate_client(&server).await;

    Mock::given(method("GET"))
        .and(query_param("type", "itv"))
        .and(query_param("action", "get_ordered_list"))
        .and(query_param("genre", "1"))
        .and(query_param("p", "1"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "js": {
                "total_items": "3", "max_page_items": "2",
                "data": [
                    {"id": "1", "name": "Ch 1", "cmd": ""},
                    {"id": "2", "name": "Ch 2", "cmd": ""}
                ]
            }
        })))
        .mount(&server)
        .await;

    Mock::given(method("GET"))
        .and(query_param("type", "itv"))
        .and(query_param("action", "get_ordered_list"))
        .and(query_param("genre", "1"))
        .and(query_param("p", "2"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "js": {
                "total_items": "3", "max_page_items": "2",
                "data": [
                    {"id": "3", "name": "Ch 3", "cmd": ""}
                ]
            }
        })))
        .mount(&server)
        .await;

    let channels = client.get_all_channels("1", None).await.unwrap();
    assert_eq!(channels.len(), 3);
    assert_eq!(channels[0].name, "Ch 1");
    assert_eq!(channels[2].name, "Ch 3");
}

#[tokio::test]
async fn resolve_stream_url_all_formats() {
    // Full URL
    assert_eq!(
        crispy_stalker::resolve_stream_url("http://example.com/live/ch1.ts", "http://portal.com"),
        Some("http://example.com/live/ch1.ts".into())
    );

    // ffrt prefix
    assert_eq!(
        crispy_stalker::resolve_stream_url(
            "ffrt http://example.com/live/ch1.ts",
            "http://portal.com"
        ),
        Some("http://example.com/live/ch1.ts".into())
    );

    // Relative path
    assert_eq!(
        crispy_stalker::resolve_stream_url("/live/ch1.ts", "http://portal.com"),
        Some("http://portal.com/live/ch1.ts".into())
    );
}

#[tokio::test]
async fn mac_to_device_id_conversion() {
    use crispy_stalker::StalkerSession;
    assert_eq!(
        StalkerSession::mac_to_device_id("00:1A:79:AB:CD:EF"),
        "001A79ABCDEF"
    );
    assert_eq!(
        StalkerSession::mac_to_device_id("aa:bb:cc:dd:ee:ff"),
        "AABBCCDDEEFF"
    );
}

#[tokio::test]
async fn keepalive_sends_watchdog_request() {
    let server = MockServer::start().await;
    let client = authenticate_client(&server).await;

    Mock::given(method("GET"))
        .and(query_param("type", "watchdog"))
        .and(query_param("action", "get_events"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"js": 1})))
        .mount(&server)
        .await;

    client.keepalive().await.unwrap();
}

#[tokio::test]
async fn expired_session_returns_session_expired_error() {
    let server = MockServer::start().await;
    let client = authenticate_client(&server).await;

    Mock::given(method("GET"))
        .and(query_param("type", "itv"))
        .and(query_param("action", "get_genres"))
        .respond_with(ResponseTemplate::new(401))
        .mount(&server)
        .await;

    let result = client.get_genres().await;
    assert!(result.is_err());
    assert!(matches!(
        result.unwrap_err(),
        crispy_stalker::StalkerError::SessionExpired
    ),);
}

#[tokio::test]
async fn auth_failure_returns_auth_error() {
    let server = MockServer::start().await;

    // Discovery
    Mock::given(method("GET"))
        .and(path("/c/"))
        .respond_with(ResponseTemplate::new(200).set_body_string("OK"))
        .up_to_n_times(1)
        .mount(&server)
        .await;

    // Handshake succeeds
    Mock::given(method("GET"))
        .and(path("/c/"))
        .and(query_param("action", "handshake"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "js": { "token": "token123" }
        })))
        .mount(&server)
        .await;

    // do_auth returns false
    Mock::given(method("GET"))
        .and(path("/c/"))
        .and(query_param("action", "do_auth"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"js": false})))
        .mount(&server)
        .await;

    let mut client = test_client(&server);
    let result = client.authenticate().await;
    assert!(result.is_err());
    assert!(matches!(
        result.unwrap_err(),
        crispy_stalker::StalkerError::Auth(_)
    ));
}

#[tokio::test]
async fn not_authenticated_returns_error() {
    let server = MockServer::start().await;
    let client = test_client(&server);

    let result = client.get_genres().await;
    assert!(matches!(
        result.unwrap_err(),
        crispy_stalker::StalkerError::NotAuthenticated
    ));
}

#[tokio::test]
async fn get_account_info_parses_fields() {
    let server = MockServer::start().await;
    let client = authenticate_client(&server).await;

    Mock::given(method("GET"))
        .and(query_param("type", "account_info"))
        .and(query_param("action", "get_main_info"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "js": {
                "login": "user42",
                "mac": "00:1A:79:AB:CD:EF",
                "status": "1",
                "expire_billing_date": "2026-12-31",
                "subscribed_till": "2026-06-15",
                "phone": "+1234567890"
            }
        })))
        .mount(&server)
        .await;

    let info = client.get_account_info().await.unwrap();
    assert_eq!(info.login.as_deref(), Some("user42"));
    assert_eq!(info.mac.as_deref(), Some("00:1A:79:AB:CD:EF"));
    assert_eq!(info.status.as_deref(), Some("1"));
    assert_eq!(info.expiration.as_deref(), Some("2026-12-31"));
    assert_eq!(info.subscribed_till.as_deref(), Some("2026-06-15"));
}

#[tokio::test]
async fn progress_callback_receives_correct_counts() {
    let server = MockServer::start().await;
    let client = authenticate_client(&server).await;

    // Page 1: 3 total items, 2 per page = 2 pages
    Mock::given(method("GET"))
        .and(query_param("type", "itv"))
        .and(query_param("action", "get_ordered_list"))
        .and(query_param("genre", "pg"))
        .and(query_param("p", "1"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "js": {
                "total_items": "3", "max_page_items": "2",
                "data": [
                    {"id": "1", "name": "Ch 1", "cmd": ""},
                    {"id": "2", "name": "Ch 2", "cmd": ""}
                ]
            }
        })))
        .mount(&server)
        .await;

    Mock::given(method("GET"))
        .and(query_param("type", "itv"))
        .and(query_param("action", "get_ordered_list"))
        .and(query_param("genre", "pg"))
        .and(query_param("p", "2"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "js": {
                "total_items": "3", "max_page_items": "2",
                "data": [
                    {"id": "3", "name": "Ch 3", "cmd": ""}
                ]
            }
        })))
        .mount(&server)
        .await;

    let progress = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let progress_clone = progress.clone();
    let callback = move |completed: u32, total: u32| {
        progress_clone.lock().unwrap().push((completed, total));
    };

    let channels = client
        .get_all_channels("pg", Some(&callback))
        .await
        .unwrap();
    assert_eq!(channels.len(), 3);

    let calls = progress.lock().unwrap();
    assert_eq!(calls.len(), 2); // page 1 + page 2
    assert_eq!(calls[0], (1, 2));
    assert_eq!(calls[1], (2, 2));
}

#[tokio::test]
async fn get_series_info_returns_seasons_and_episodes() {
    let server = MockServer::start().await;
    let client = authenticate_client(&server).await;

    // get_seasons for movie_id=10
    Mock::given(method("GET"))
        .and(query_param("type", "vod"))
        .and(query_param("action", "get_ordered_list"))
        .and(query_param("movie_id", "10"))
        .and(query_param("season_id", "0"))
        .and(query_param("episode_id", "0"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "js": {
                "data": [
                    {"id": "s1", "name": "Season 1", "video_id": "10", "is_season": "1"},
                    {"id": "s2", "name": "Season 2", "video_id": "10", "is_season": "1"}
                ]
            }
        })))
        .mount(&server)
        .await;

    // get_episodes for season s1
    Mock::given(method("GET"))
        .and(query_param("type", "vod"))
        .and(query_param("action", "get_ordered_list"))
        .and(query_param("movie_id", "10"))
        .and(query_param("season_id", "s1"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "js": {
                "data": [
                    {"id": "e1", "name": "Pilot", "cmd": "http://s/s1e1", "series_number": "1"}
                ]
            }
        })))
        .mount(&server)
        .await;

    // get_episodes for season s2
    Mock::given(method("GET"))
        .and(query_param("type", "vod"))
        .and(query_param("action", "get_ordered_list"))
        .and(query_param("movie_id", "10"))
        .and(query_param("season_id", "s2"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "js": {
                "data": [
                    {"id": "e2", "name": "Premiere", "cmd": "http://s/s2e1", "series_number": "1"},
                    {"id": "e3", "name": "Ep 2", "cmd": "http://s/s2e2", "series_number": "2"}
                ]
            }
        })))
        .mount(&server)
        .await;

    let series = crispy_stalker::StalkerSeriesItem {
        id: "10".into(),
        name: "Test Series".into(),
        ..Default::default()
    };

    let detail = client.get_series_info(series).await.unwrap();
    assert_eq!(detail.series.name, "Test Series");
    assert_eq!(detail.seasons.len(), 2);
    assert_eq!(detail.seasons[0].name, "Season 1");
    assert_eq!(detail.seasons[1].name, "Season 2");
    assert_eq!(detail.episodes["s1"].len(), 1);
    assert_eq!(detail.episodes["s1"][0].name, "Pilot");
    assert_eq!(detail.episodes["s2"].len(), 2);
    assert_eq!(detail.episodes["s2"][0].name, "Premiere");
}