chipzen-bot 0.3.0

Build, test, and deploy poker bots for the Chipzen AI competition platform
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
//! Tests for the external-API remote-play path (`run_external_bot`),
//! mirroring the Python suite `tests/test_external.py`.
//!
//! The websocket layer is faked: a scripted [`LobbyTransport`] routes lobby
//! vs gateway connections and replays canned frames, so the whole
//! lobby → matched → gateway → match_end flow (plus reconnect-resume) runs
//! without a server — the Rust analogue of the Python suite's monkeypatched
//! `websockets.connect`.

use async_trait::async_trait;
use chipzen_bot::{
    _set_lobby_recv_timeout_ms, bot_token_subprotocols, resolve_gateway_url,
    run_external_with_transport, Action, Bot, ChipzenConfig, EnvName, Error, GameState,
    LobbyTransport, MatchResult, MessageReader, MessageWriter, RetryPolicy, RunExternalOptions,
    BOT_TOKEN_SUBPROTOCOL,
};
use serde_json::{json, Value};
use std::sync::{Arc, Mutex};

const LOBBY_URL: &str = "wss://staging.chipzen.ai/ws/external/bot/test-bot-uuid";

// ---------------------------------------------------------------------------
// Bots
// ---------------------------------------------------------------------------

/// Records lifecycle hooks + decision latencies; checks when it can.
#[derive(Clone, Default)]
struct CollectBot {
    events: Arc<Mutex<Vec<String>>>,
    latencies: Arc<Mutex<Vec<f64>>>,
}

impl Bot for CollectBot {
    fn decide(&mut self, state: &GameState) -> Action {
        self.events.lock().unwrap().push("decide".into());
        if state.valid_actions.iter().any(|a| a == "check") {
            Action::Check
        } else {
            Action::Fold
        }
    }
    fn on_match_start(&mut self, _msg: &Value) {
        self.events.lock().unwrap().push("match_start".into());
    }
    fn on_match_end(&mut self, _msg: &Value) {
        self.events.lock().unwrap().push("match_end".into());
    }
    fn on_decision_latency(&mut self, latency_ms: f64) {
        self.latencies.lock().unwrap().push(latency_ms);
    }
}

struct CrashBot;
impl Bot for CrashBot {
    fn decide(&mut self, _state: &GameState) -> Action {
        panic!("boom");
    }
}

// ---------------------------------------------------------------------------
// Canned frames
// ---------------------------------------------------------------------------

fn server_hello() -> Value {
    json!({ "type": "hello", "selected_version": "1.0", "game_type": "nlhe_6max" })
}

/// The lobby's server hello (endpoint=lobby); no client hello on the lobby leg.
fn server_hello_lobby() -> Value {
    json!({ "type": "hello", "endpoint": "lobby" })
}

fn match_start() -> Value {
    json!({
        "type": "match_start",
        "match_id": "m1",
        "seats": [{"seat": 0, "is_self": true}, {"seat": 1, "is_self": false}],
        "turn_timeout_ms": 5000,
    })
}

fn turn_request() -> Value {
    json!({
        "type": "turn_request",
        "match_id": "m1",
        "request_id": "req_1",
        "valid_actions": ["fold", "call", "raise"],
        "state": {
            "phase": "preflop",
            "board": [],
            "your_hole_cards": ["Ah", "Kd"],
            "pot": 15,
            "to_call": 5,
            "min_raise": 20,
            "max_raise": 995,
        },
    })
}

fn match_end() -> Value {
    json!({ "type": "match_end", "match_id": "m1", "reason": "complete" })
}

fn full_match() -> Vec<Value> {
    vec![server_hello(), match_start(), turn_request(), match_end()]
}

fn matched() -> Value {
    json!({
        "type": "matched",
        "match_id": "m1",
        "participant_id": "p1",
        "gateway_ws_url": "/ws/external/match/m1/p1",
        "rated": false,
    })
}

// ---------------------------------------------------------------------------
// Scripted transport
// ---------------------------------------------------------------------------

/// Lobby reader: replays scripted frames, then parks forever (the loop's
/// recv-timeout wakes it to re-check the stop signal). A `None` entry in the
/// script simulates a socket drop (returns `Ok(None)` = clean close).
struct ScriptedReader {
    frames: Vec<Option<String>>,
    idx: usize,
    block_when_done: bool,
}

#[async_trait]
impl MessageReader for ScriptedReader {
    async fn next(&mut self) -> Result<Option<String>, Error> {
        if self.idx < self.frames.len() {
            let frame = self.frames[self.idx].clone();
            self.idx += 1;
            // `None` in the script = drop.
            return Ok(frame);
        }
        if self.block_when_done {
            // Park until the session's recv-timeout cancels us and the loop
            // re-checks `stop`. Mirrors the Python fake's `sleep(3600)`.
            std::future::pending::<()>().await;
            unreachable!();
        }
        Ok(None)
    }
}

#[derive(Clone, Default)]
struct CapturingWriter {
    sent: Arc<Mutex<Vec<Value>>>,
}

#[async_trait]
impl MessageWriter for CapturingWriter {
    async fn send(&mut self, payload: String) -> Result<(), Error> {
        let parsed: Value = serde_json::from_str(&payload).unwrap_or(Value::Null);
        self.sent.lock().unwrap().push(parsed);
        Ok(())
    }
}

#[derive(Clone)]
struct Calls {
    lobby_urls: Arc<Mutex<Vec<String>>>,
    gateway_urls: Arc<Mutex<Vec<String>>>,
    subprotocols: Arc<Mutex<Vec<Vec<String>>>>,
    user_agents: Arc<Mutex<Vec<String>>>,
    lobby_sent: Arc<Mutex<Vec<Value>>>,
}

impl Calls {
    fn new() -> Self {
        Self {
            lobby_urls: Arc::new(Mutex::new(Vec::new())),
            gateway_urls: Arc::new(Mutex::new(Vec::new())),
            subprotocols: Arc::new(Mutex::new(Vec::new())),
            user_agents: Arc::new(Mutex::new(Vec::new())),
            lobby_sent: Arc::new(Mutex::new(Vec::new())),
        }
    }
}

/// Scripted transport: pops a fresh lobby/gateway script per connect, so
/// successive connects can behave differently (reconnect tests). The shared
/// `lobby_sent` buffer aggregates everything sent on every lobby leg.
struct ScriptedTransport {
    lobby_scripts: Mutex<std::vec::IntoIter<Vec<Option<Value>>>>,
    gateway_scripts: Mutex<std::vec::IntoIter<Vec<Value>>>,
    calls: Calls,
}

impl ScriptedTransport {
    fn new(
        lobby_scripts: Vec<Vec<Option<Value>>>,
        gateway_scripts: Vec<Vec<Value>>,
    ) -> (Arc<Self>, Calls) {
        let calls = Calls::new();
        let t = Arc::new(Self {
            lobby_scripts: Mutex::new(lobby_scripts.into_iter()),
            gateway_scripts: Mutex::new(gateway_scripts.into_iter()),
            calls: calls.clone(),
        });
        (t, calls)
    }
}

#[async_trait]
impl LobbyTransport for ScriptedTransport {
    async fn connect_lobby(
        &self,
        url: &str,
        user_agent: &str,
    ) -> Result<(Box<dyn MessageReader>, Box<dyn MessageWriter>), Error> {
        self.calls.lobby_urls.lock().unwrap().push(url.to_string());
        self.calls
            .user_agents
            .lock()
            .unwrap()
            .push(user_agent.to_string());
        let script = self
            .lobby_scripts
            .lock()
            .unwrap()
            .next()
            .expect("ran out of lobby scripts");
        let frames = script
            .into_iter()
            .map(|f| f.map(|v| v.to_string()))
            .collect();
        let reader = ScriptedReader {
            frames,
            idx: 0,
            block_when_done: true,
        };
        // The writer aggregates into the shared lobby_sent buffer.
        let writer = CapturingWriter {
            sent: Arc::clone(&self.calls.lobby_sent),
        };
        Ok((Box::new(reader), Box::new(writer)))
    }

    async fn connect_gateway(
        &self,
        url: &str,
        token: &str,
        user_agent: &str,
    ) -> Result<(Box<dyn MessageReader>, Box<dyn MessageWriter>), Error> {
        self.calls
            .gateway_urls
            .lock()
            .unwrap()
            .push(url.to_string());
        self.calls
            .subprotocols
            .lock()
            .unwrap()
            .push(bot_token_subprotocols(token));
        self.calls
            .user_agents
            .lock()
            .unwrap()
            .push(user_agent.to_string());
        let script = self
            .gateway_scripts
            .lock()
            .unwrap()
            .next()
            .expect("ran out of gateway scripts");
        let frames = script.into_iter().map(|v| Some(v.to_string())).collect();
        let reader = ScriptedReader {
            frames,
            idx: 0,
            block_when_done: false, // gateway closes cleanly when exhausted
        };
        let writer = CapturingWriter::default();
        Ok((Box::new(reader), Box::new(writer)))
    }
}

/// Build a transport with a single lobby script and a default-or-given
/// gateway script. `None` entries in the lobby script are drops.
fn install(
    lobby_frames: Vec<Option<Value>>,
    gateway_frames: Option<Vec<Value>>,
) -> (Arc<ScriptedTransport>, Calls) {
    _set_lobby_recv_timeout_ms(5);
    ScriptedTransport::new(
        vec![lobby_frames],
        vec![gateway_frames.unwrap_or_else(full_match)],
    )
}

/// Helper: run with the bot factory + options + transport.
async fn run<B, F>(
    factory: F,
    options: RunExternalOptions,
    transport: Arc<ScriptedTransport>,
) -> Result<Vec<MatchResult>, Error>
where
    B: Bot,
    F: Fn() -> B + Send + Sync + 'static,
{
    run_external_with_transport(factory, options, transport).await
}

fn opts_with_token() -> RunExternalOptions {
    RunExternalOptions {
        url: Some(LOBBY_URL.to_string()),
        token: Some("cz_extbot_x".to_string()),
        ..Default::default()
    }
}

// ---------------------------------------------------------------------------
// URL helpers (pure)
// ---------------------------------------------------------------------------

#[test]
fn test_bot_token_subprotocols() {
    assert_eq!(
        bot_token_subprotocols("cz_extbot_x"),
        vec![BOT_TOKEN_SUBPROTOCOL.to_string(), "cz_extbot_x".to_string()]
    );
}

#[test]
fn test_resolve_gateway_url_joins_path_to_lobby_origin() {
    let out = resolve_gateway_url(
        "wss://staging.chipzen.ai/ws/external/bot/abc",
        "/ws/external/match/m1/p1",
    )
    .unwrap();
    assert_eq!(out, "wss://staging.chipzen.ai/ws/external/match/m1/p1");
}

#[test]
fn test_resolve_gateway_url_passes_through_same_origin_absolute_url() {
    let full = "wss://staging.chipzen.ai/ws/external/match/m1/p1";
    assert_eq!(
        resolve_gateway_url("wss://staging.chipzen.ai/x", full).unwrap(),
        full
    );
}

#[test]
fn test_resolve_gateway_url_rejects_cross_origin() {
    // The bot token must not follow a redirect to another host.
    let err = resolve_gateway_url(
        "wss://staging.chipzen.ai/x",
        "wss://attacker.example/ws/external/match/m1/p1",
    )
    .unwrap_err();
    assert!(matches!(err, chipzen_bot::Error::UntrustedGateway(_)));
}

#[test]
fn test_resolve_gateway_url_rejects_wss_to_ws_downgrade() {
    let err = resolve_gateway_url(
        "wss://staging.chipzen.ai/x",
        "ws://staging.chipzen.ai/ws/external/match/m1/p1",
    )
    .unwrap_err();
    assert!(matches!(err, chipzen_bot::Error::UntrustedGateway(_)));
}

// ---------------------------------------------------------------------------
// Connection resolution + validation
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_requires_a_token() {
    let cfg = ChipzenConfig {
        token: None,
        ..Default::default()
    };
    let opts = RunExternalOptions {
        url: Some(LOBBY_URL.to_string()),
        config: Some(cfg),
        ..Default::default()
    };
    let (transport, _) = install(vec![], None);
    let err = run(CollectBot::default, opts, transport)
        .await
        .expect_err("expected a token error");
    assert!(
        format!("{err}").contains("requires an external-API token"),
        "unexpected error: {err}"
    );
}

#[tokio::test]
async fn test_requires_url_or_bot_id() {
    let cfg = ChipzenConfig {
        token: Some("cz_extbot_x".to_string()),
        bot_id: None,
        ..Default::default()
    };
    let opts = RunExternalOptions {
        config: Some(cfg),
        ..Default::default()
    };
    let (transport, _) = install(vec![], None);
    let err = run(CollectBot::default, opts, transport)
        .await
        .expect_err("expected a url/bot_id error");
    assert!(
        format!("{err}").contains("needs a lobby URL"),
        "unexpected error: {err}"
    );
}

#[tokio::test]
async fn test_token_from_config_when_no_kwarg() {
    let cfg = ChipzenConfig {
        token: Some("cz_extbot_from_config".to_string()),
        ..Default::default()
    };
    let opts = RunExternalOptions {
        url: Some(LOBBY_URL.to_string()),
        config: Some(cfg),
        max_matches: Some(1),
        ..Default::default()
    };
    let (transport, calls) = install(vec![Some(server_hello_lobby()), Some(matched())], None);
    run(CollectBot::default, opts, transport).await.unwrap();
    let auth = &calls.lobby_sent.lock().unwrap()[0];
    assert_eq!(auth["type"], "authenticate");
    assert_eq!(auth["token"], "cz_extbot_from_config");
}

#[tokio::test]
async fn test_explicit_token_overrides_config() {
    let cfg = ChipzenConfig {
        token: Some("cz_extbot_config".to_string()),
        ..Default::default()
    };
    let opts = RunExternalOptions {
        url: Some(LOBBY_URL.to_string()),
        token: Some("cz_extbot_explicit".to_string()),
        config: Some(cfg),
        max_matches: Some(1),
        ..Default::default()
    };
    let (transport, calls) = install(vec![Some(server_hello_lobby()), Some(matched())], None);
    run(CollectBot::default, opts, transport).await.unwrap();
    assert_eq!(
        calls.lobby_sent.lock().unwrap()[0]["token"],
        "cz_extbot_explicit"
    );
}

#[tokio::test]
async fn test_bot_id_plus_env_builds_lobby_url() {
    let cfg = ChipzenConfig {
        token: Some("cz_extbot_x".to_string()),
        ..Default::default()
    };
    let opts = RunExternalOptions {
        bot_id: Some("abc".to_string()),
        env: Some(EnvName::Staging),
        config: Some(cfg),
        max_matches: Some(1),
        ..Default::default()
    };
    let (transport, calls) = install(vec![Some(server_hello_lobby()), Some(matched())], None);
    run(CollectBot::default, opts, transport).await.unwrap();
    assert_eq!(
        calls.lobby_urls.lock().unwrap()[0],
        "wss://staging.chipzen.ai/ws/external/bot/abc"
    );
}

// ---------------------------------------------------------------------------
// Lobby -> gateway happy path
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_plays_one_match_end_to_end() {
    let bot = CollectBot::default();
    let events = Arc::clone(&bot.events);
    let latencies = Arc::clone(&bot.latencies);
    let opts = RunExternalOptions {
        max_matches: Some(1),
        ..opts_with_token()
    };
    let (transport, calls) = install(vec![Some(server_hello_lobby()), Some(matched())], None);
    // Reuse the SAME bot instance across matches (factory clones the handle).
    let results = run(move || bot.clone(), opts, transport).await.unwrap();

    assert_eq!(results.len(), 1);
    assert_eq!(results[0].end.as_ref().unwrap()["reason"], "complete");
    assert_eq!(results[0].match_id.as_deref(), Some("m1"));

    assert_eq!(
        *events.lock().unwrap(),
        vec!["match_start", "decide", "match_end"]
    );
    assert_eq!(latencies.lock().unwrap().len(), 1);

    // Gateway leg carried the token in the Sec-WebSocket-Protocol offer.
    assert_eq!(
        calls.subprotocols.lock().unwrap()[0],
        vec![BOT_TOKEN_SUBPROTOCOL.to_string(), "cz_extbot_x".to_string()]
    );
    // Non-default UA.
    assert!(calls.user_agents.lock().unwrap()[0].starts_with("chipzen-sdk-rust/"));
}

#[tokio::test]
async fn test_lobby_answers_ping_with_pong() {
    let opts = RunExternalOptions {
        max_matches: Some(1),
        ..opts_with_token()
    };
    let (transport, calls) = install(
        vec![
            Some(server_hello_lobby()),
            Some(json!({ "type": "ping" })),
            Some(matched()),
        ],
        None,
    );
    run(CollectBot::default, opts, transport).await.unwrap();
    let sent_types: Vec<String> = calls
        .lobby_sent
        .lock()
        .unwrap()
        .iter()
        .map(|m| m["type"].as_str().unwrap_or("").to_string())
        .collect();
    assert!(
        sent_types.iter().any(|t| t == "pong"),
        "no pong in {sent_types:?}"
    );
}

#[tokio::test]
async fn test_evict_ends_session_with_no_match() {
    let (transport, _) = install(
        vec![Some(server_hello_lobby()), Some(json!({ "type": "evict" }))],
        None,
    );
    let results = run(CollectBot::default, opts_with_token(), transport)
        .await
        .unwrap();
    assert!(results.is_empty());
}

#[tokio::test]
async fn test_max_matches_stops_after_one() {
    let opts = RunExternalOptions {
        max_matches: Some(1),
        ..opts_with_token()
    };
    let (transport, _) = install(vec![Some(server_hello_lobby()), Some(matched())], None);
    let results = run(CollectBot::default, opts, transport).await.unwrap();
    assert_eq!(results.len(), 1);
}

// ---------------------------------------------------------------------------
// safe_mode
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_safe_mode_false_propagates_bot_error() {
    let opts = RunExternalOptions {
        safe_mode: Some(false),
        max_matches: Some(1),
        ..opts_with_token()
    };
    let (transport, _) = install(vec![Some(server_hello_lobby()), Some(matched())], None);
    let err = run(|| CrashBot, opts, transport)
        .await
        .expect_err("expected BotDecision error");
    assert!(matches!(err, Error::BotDecision(_)), "got {err:?}");
}

#[tokio::test]
async fn test_safe_mode_true_folds_bot_error() {
    let opts = RunExternalOptions {
        safe_mode: Some(true),
        max_matches: Some(1),
        ..opts_with_token()
    };
    let (transport, _) = install(vec![Some(server_hello_lobby()), Some(matched())], None);
    let results = run(|| CrashBot, opts, transport).await.unwrap();
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].end.as_ref().unwrap()["reason"], "complete");
}

// ---------------------------------------------------------------------------
// Reconnect behavior (gateway mid-match drop + lobby drop)
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_gateway_reconnects_and_resumes() {
    // First gateway connect drops mid-match (no match_end); the SDK reconnects
    // and the second connect plays to match_end. Same bot instance is reused.
    _set_lobby_recv_timeout_ms(5);
    let policy = RetryPolicy::new(5, 1, 1, 2.0).unwrap();
    let (transport, calls) = ScriptedTransport::new(
        vec![vec![Some(server_hello_lobby()), Some(matched())]],
        vec![
            vec![server_hello(), match_start(), turn_request()], // drops, no match_end
            vec![server_hello(), match_end()],                   // reconnect → completes
        ],
    );
    let opts = RunExternalOptions {
        retry_policy: Some(policy),
        max_matches: Some(1),
        ..opts_with_token()
    };
    let results = run(CollectBot::default, opts, transport).await.unwrap();
    assert_eq!(calls.gateway_urls.lock().unwrap().len(), 2); // reconnected once
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].end.as_ref().unwrap()["reason"], "complete");
}

#[tokio::test]
async fn test_gateway_reconnect_budget_exhausted_abandons_match() {
    // Gateway never reaches match_end. After max_reconnect_attempts the match
    // is abandoned (end=None) rather than hanging or looping forever.
    _set_lobby_recv_timeout_ms(5);
    let policy = RetryPolicy::new(2, 1, 1, 2.0).unwrap();
    let (transport, calls) = ScriptedTransport::new(
        vec![vec![Some(server_hello_lobby()), Some(matched())]],
        vec![
            vec![server_hello(), match_start()], // initial
            vec![server_hello(), match_start()], // retry 1
            vec![server_hello(), match_start()], // retry 2
        ],
    );
    let opts = RunExternalOptions {
        retry_policy: Some(policy),
        max_matches: Some(1),
        ..opts_with_token()
    };
    let results = run(CollectBot::default, opts, transport).await.unwrap();
    assert_eq!(calls.gateway_urls.lock().unwrap().len(), 3); // initial + 2 retries
    assert_eq!(results.len(), 1);
    assert!(results[0].end.is_none());
}

#[tokio::test]
async fn test_lobby_reconnects_after_close() {
    // The lobby socket drops after connecting; the SDK reconnects the lobby
    // and then plays the match it's dispatched on the second session.
    _set_lobby_recv_timeout_ms(5);
    let policy = RetryPolicy::new(5, 1, 1, 2.0).unwrap();
    let (transport, calls) = ScriptedTransport::new(
        vec![
            vec![Some(server_hello_lobby()), None], // connects, then drops
            vec![Some(server_hello_lobby()), Some(matched())], // reconnect → a match
        ],
        vec![full_match()],
    );
    let opts = RunExternalOptions {
        retry_policy: Some(policy),
        max_matches: Some(1),
        ..opts_with_token()
    };
    let results = run(CollectBot::default, opts, transport).await.unwrap();
    assert_eq!(calls.lobby_urls.lock().unwrap().len(), 2); // lobby reconnected
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].end.as_ref().unwrap()["reason"], "complete");
}