concord 2.3.1

A terminal user interface client for Discord
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
use crate::{
    AppError,
    discord::{
        ActivityInfo, AppEvent, ChannelInfo, MemberInfo, MentionInfo, MessageAttachmentUpload,
        RoleInfo, UserProfileInfo, VoiceScope, VoiceSoundKind, VoiceStateInfo,
        gateway::GatewayCommand,
        ids::{
            Id,
            marker::{ChannelMarker, GuildMarker, RoleMarker, UserMarker},
        },
        test_builders::{
            GuildCreateFixture, MessageCreateFixture, MessageHistoryLoadedFixture,
            UserProfileLoadFailedFixture, guild_create_event, guild_message_create_fixture,
            message_create_event as build_message_create_event, message_history_loaded_event,
            user_profile_load_failed_event,
        },
    },
};
use serde_json::{Value, json};

use super::{
    DiscordClient, MEMBER_SEARCH_MAX_LIMIT, MEMBER_SEARCH_MAX_QUERY_CHARS,
    OFFICIAL_WORDLE_APPLICATION_ID, validate_token_header,
};

#[tokio::test]
async fn publish_event_sends_matching_snapshot_and_effect_revisions() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    let mut effects = client.take_effects();
    let mut snapshots = client.subscribe_snapshots();

    client
        .publish_event(message_history_loaded_event(MessageHistoryLoadedFixture {
            channel_id: Id::new(1),
            ..MessageHistoryLoadedFixture::new()
        }))
        .await;

    snapshots.changed().await.expect("snapshot is published");
    let snapshot = *snapshots.borrow_and_update();
    let effect = effects.recv().await.expect("effect is published");
    let state_snapshot = client.current_discord_snapshot();

    assert_eq!(snapshot.global, 1);
    assert_eq!(snapshot.message, 1);
    assert_eq!(snapshot.navigation, 0);
    assert_eq!(snapshot.detail, 0);
    assert_eq!(effect.revision, 1);
    assert_eq!(state_snapshot.revision.global, 1);
    assert_eq!(state_snapshot.revision.message, 1);
}

#[tokio::test]
async fn message_create_publishes_matching_snapshot_and_effect_revisions() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    let mut effects = client.take_effects();
    let mut snapshots = client.subscribe_snapshots();

    client.publish_event(message_create_event(1)).await;

    snapshots.changed().await.expect("snapshot is published");
    let snapshot = *snapshots.borrow_and_update();
    let effect = effects.recv().await.expect("effect is published");

    assert_eq!(snapshot.global, 1);
    assert_eq!(snapshot.navigation, 1);
    assert_eq!(snapshot.message, 1);
    assert_eq!(snapshot.detail, 0);
    assert_eq!(effect.revision, 1);
    assert!(matches!(effect.event, AppEvent::MessageCreate { .. }));
}

#[tokio::test]
async fn current_user_message_create_advances_detail_revision() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    let mut effects = client.take_effects();
    let mut snapshots = client.subscribe_snapshots();

    client
        .publish_event(AppEvent::Ready {
            user: "neo".to_owned(),
            user_id: Some(Id::new(99)),
        })
        .await;
    snapshots
        .changed()
        .await
        .expect("ready snapshot is published");
    drop(snapshots.borrow_and_update());

    client.publish_event(message_create_event(1)).await;

    snapshots
        .changed()
        .await
        .expect("message snapshot is published");
    let snapshot = *snapshots.borrow_and_update();
    let effect = effects.recv().await.expect("message effect is published");

    assert_eq!(snapshot.global, 2);
    assert_eq!(snapshot.navigation, 2);
    assert_eq!(snapshot.message, 2);
    assert_eq!(snapshot.detail, 2);
    assert_eq!(effect.revision, 2);
    assert!(matches!(effect.event, AppEvent::MessageCreate { .. }));
}

#[tokio::test]
async fn mentioned_message_create_advances_detail_revision() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    let mut effects = client.take_effects();
    let mut snapshots = client.subscribe_snapshots();

    client
        .publish_event(AppEvent::Ready {
            user: "neo".to_owned(),
            user_id: Some(Id::new(42)),
        })
        .await;
    snapshots
        .changed()
        .await
        .expect("ready snapshot is published");
    drop(snapshots.borrow_and_update());

    let mut event = message_create_event(1);
    if let AppEvent::MessageCreate { message } = &mut event {
        message.content = Some("hello <@42>".to_owned());
        message
            .mentions
            .push(MentionInfo::test(Id::new(42), "neo".to_owned()));
    }
    client.publish_event(event).await;

    snapshots
        .changed()
        .await
        .expect("message snapshot is published");
    let snapshot = *snapshots.borrow_and_update();
    let effect = effects.recv().await.expect("message effect is published");

    assert_eq!(snapshot.global, 2);
    assert_eq!(snapshot.navigation, 2);
    assert_eq!(snapshot.message, 2);
    assert_eq!(snapshot.detail, 2);
    assert_eq!(effect.revision, 2);
    assert!(matches!(effect.event, AppEvent::MessageCreate { .. }));
}

#[tokio::test]
async fn normal_channel_upsert_updates_snapshot_without_effect_delivery() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    let mut effects = client.take_effects();
    let mut snapshots = client.subscribe_snapshots();

    client.publish_event(channel_upsert_event()).await;

    snapshots.changed().await.expect("snapshot is published");
    let snapshot = *snapshots.borrow_and_update();

    assert_eq!(snapshot.global, 1);
    assert_eq!(snapshot.navigation, 1);
    assert_eq!(snapshot.message, 1);
    assert_eq!(snapshot.detail, 1);
    assert!(matches!(
        effects.try_recv(),
        Err(tokio::sync::mpsc::error::TryRecvError::Empty)
    ));
}

#[tokio::test]
async fn thread_channel_upsert_is_delivered_as_effect_for_tui_derived_state() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    let mut effects = client.take_effects();
    let mut snapshots = client.subscribe_snapshots();

    client.publish_event(thread_channel_upsert_event()).await;

    snapshots.changed().await.expect("snapshot is published");
    let snapshot = *snapshots.borrow_and_update();
    let effect = effects.recv().await.expect("effect is published");

    assert_eq!(snapshot.global, 1);
    assert_eq!(snapshot.navigation, 1);
    assert_eq!(snapshot.message, 1);
    assert_eq!(snapshot.detail, 1);
    assert_eq!(effect.revision, 1);
    assert!(matches!(effect.event, AppEvent::ChannelUpsert(_)));
}

#[tokio::test]
async fn concurrent_publishers_emit_ordered_effect_revisions() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    let mut effects = client.take_effects();
    let mut snapshots = client.subscribe_snapshots();

    let mut tasks = Vec::new();
    for index in 0..32_u64 {
        let client = client.clone();
        tasks.push(tokio::spawn(async move {
            client
                .publish_event(message_history_loaded_event(MessageHistoryLoadedFixture {
                    channel_id: Id::new(index + 1),
                    ..MessageHistoryLoadedFixture::new()
                }))
                .await;
        }));
    }

    for task in tasks {
        task.await.expect("publish task completes");
    }

    for expected_revision in 1..=32 {
        let effect = effects.recv().await.expect("effect is published");
        assert_eq!(effect.revision, expected_revision);
    }

    snapshots.changed().await.expect("snapshot is published");
    let snapshot = *snapshots.borrow_and_update();
    assert_eq!(snapshot.global, 32);
    assert_eq!(snapshot.message, 32);
    assert_eq!(client.current_discord_snapshot().revision.global, 32);
}

#[tokio::test]
async fn effect_only_events_are_delivered_without_snapshots() {
    for event in [
        AppEvent::GatewayError {
            message: "boom".to_owned(),
        },
        AppEvent::ActivateChannel {
            channel_id: Id::new(42),
        },
    ] {
        let _ = rustls::crypto::ring::default_provider().install_default();
        let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
        let mut effects = client.take_effects();
        let snapshots = client.subscribe_snapshots();

        client.publish_event(event.clone()).await;

        let effect = effects.recv().await.expect("effect is published");
        assert_eq!(effect.revision, 0);
        assert_eq!(format!("{:?}", effect.event), format!("{event:?}"));
        assert!(!snapshots.has_changed().expect("snapshot stream is open"));
    }
}

#[tokio::test]
async fn current_user_activities_returns_cached_presence_activity() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    let user_id = Id::new(10);
    let activity = ActivityInfo::playing("Concord");

    client
        .publish_event(AppEvent::Ready {
            user: "neo".to_owned(),
            user_id: Some(user_id),
        })
        .await;
    client
        .publish_event(AppEvent::PresenceUpdate {
            guild_id: None,
            presence: crate::discord::PresenceEventFields {
                user_id,
                status: crate::discord::PresenceStatus::Online,
                activities: vec![activity.clone()],
            },
        })
        .await;

    assert_eq!(client.current_user_activities(), vec![activity]);
}

#[test]
fn selected_rich_presence_round_trips_and_clears() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");

    assert_eq!(client.selected_rich_presence(), None);
    client.select_rich_presence(Some("client-123".to_owned()));
    assert_eq!(
        client.selected_rich_presence().as_deref(),
        Some("client-123")
    );
    client.select_rich_presence(None);
    assert_eq!(client.selected_rich_presence(), None);
}

#[test]
fn requested_voice_state_tracks_shutdown_fallback() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");

    client
        .update_voice_state(
            VoiceScope::Guild(Id::new(1)),
            Some(Id::new(10)),
            true,
            false,
        )
        .expect("gateway command should queue");
    let voice = client
        .requested_voice_connection()
        .expect("requested voice state should be tracked");

    assert_eq!(voice.guild_id(), Some(Id::new(1)));
    assert_eq!(voice.channel_id, Id::new(10));
    assert!(voice.self_mute);
    assert!(!voice.self_deaf);

    client
        .update_voice_state(VoiceScope::Guild(Id::new(1)), None, false, false)
        .expect("gateway command should queue");

    assert_eq!(client.requested_voice_connection(), None);
}

#[test]
fn requested_voice_state_skips_duplicate_gateway_updates() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    let mut gateway_commands = client
        .gateway_commands_rx
        .lock()
        .expect("gateway command receiver mutex is not poisoned")
        .take()
        .expect("gateway commands can be taken once");

    client
        .update_voice_state(
            VoiceScope::Guild(Id::new(1)),
            Some(Id::new(10)),
            true,
            false,
        )
        .expect("initial join should queue");
    assert_voice_update(
        &mut gateway_commands,
        Id::new(1),
        Some(Id::new(10)),
        true,
        false,
    );

    client
        .update_voice_state(
            VoiceScope::Guild(Id::new(1)),
            Some(Id::new(10)),
            true,
            false,
        )
        .expect("duplicate join is ignored without closing channel");
    assert!(matches!(
        gateway_commands.try_recv(),
        Err(tokio::sync::mpsc::error::TryRecvError::Empty)
    ));

    client
        .update_voice_state(
            VoiceScope::Guild(Id::new(1)),
            Some(Id::new(10)),
            false,
            false,
        )
        .expect("mute change should queue");
    assert_voice_update(
        &mut gateway_commands,
        Id::new(1),
        Some(Id::new(10)),
        false,
        false,
    );

    client
        .update_voice_state(VoiceScope::Guild(Id::new(1)), None, false, false)
        .expect("leave should queue");
    assert_voice_update(&mut gateway_commands, Id::new(1), None, false, false);

    client
        .update_voice_state(VoiceScope::Guild(Id::new(1)), None, false, false)
        .expect("duplicate leave is ignored without closing channel");
    assert!(matches!(
        gateway_commands.try_recv(),
        Err(tokio::sync::mpsc::error::TryRecvError::Empty)
    ));
}

#[tokio::test]
async fn send_message_rejects_explicit_missing_send_permission() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    publish_permission_fixture(&client, "GuildText", VIEW_CHANNEL).await;

    let error = client
        .send_message(Id::new(2), "hello", None, &[])
        .await
        .expect_err("missing SEND_MESSAGES should stop before REST");

    assert!(matches!(
        error,
        AppError::DiscordRequest(message) if message == "cannot send message in channel"
    ));
}

#[tokio::test]
async fn send_message_rejects_explicit_missing_attach_permission() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    publish_permission_fixture(&client, "GuildText", VIEW_CHANNEL | SEND_MESSAGES).await;
    let attachment = MessageAttachmentUpload::from_bytes("note.txt".to_owned(), b"x".to_vec());

    let error = client
        .send_message(Id::new(2), "hello", None, &[attachment])
        .await
        .expect_err("missing ATTACH_FILES should stop before REST");

    assert!(matches!(
        error,
        AppError::DiscordRequest(message) if message == "cannot attach files in channel"
    ));
}

#[test]
fn send_message_guard_allows_unknown_channels_while_state_hydrates() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");

    client
        .ensure_can_send_message(Id::new(99), &[])
        .expect("unknown channel should stay optimistic");
}

#[tokio::test]
async fn voice_join_rejects_explicit_missing_connect_permission() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    publish_permission_fixture(&client, "GuildVoice", VIEW_CHANNEL).await;
    let mut gateway_commands = client
        .gateway_commands_rx
        .lock()
        .expect("gateway command receiver mutex is not poisoned")
        .take()
        .expect("gateway commands can be taken once");

    let error = client
        .update_voice_state(
            VoiceScope::Guild(Id::new(1)),
            Some(Id::new(2)),
            false,
            false,
        )
        .expect_err("missing CONNECT should stop before gateway command");

    assert_eq!(error, "cannot connect to voice channel");
    assert_eq!(client.requested_voice_connection(), None);
    assert!(matches!(
        gateway_commands.try_recv(),
        Err(tokio::sync::mpsc::error::TryRecvError::Empty)
    ));
}

#[tokio::test]
async fn voice_state_update_allows_current_channel_mute_change_without_connect_permission() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    publish_permission_fixture(&client, "GuildVoice", VIEW_CHANNEL).await;
    client
        .publish_event(AppEvent::VoiceStateUpdate {
            state: VoiceStateInfo {
                session_id: Some("current-voice-session".to_owned()),
                ..VoiceStateInfo::test(Id::new(1), Some(Id::new(2)), Id::new(10))
            },
        })
        .await;
    let mut gateway_commands = client
        .gateway_commands_rx
        .lock()
        .expect("gateway command receiver mutex is not poisoned")
        .take()
        .expect("gateway commands can be taken once");

    client
        .update_voice_state(VoiceScope::Guild(Id::new(1)), Some(Id::new(2)), true, true)
        .expect("current channel mute and deaf changes should still queue");

    assert_voice_update(
        &mut gateway_commands,
        Id::new(1),
        Some(Id::new(2)),
        true,
        true,
    );
}

#[test]
fn application_command_requests_are_deduped_until_loaded() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    let guild_id = Some(Id::new(1));

    assert!(client.begin_application_command_request(guild_id));
    assert!(!client.begin_application_command_request(guild_id));

    client.record_application_commands_loaded(guild_id);
    assert!(!client.begin_application_command_request(guild_id));

    let retry_guild_id = Some(Id::new(2));
    assert!(client.begin_application_command_request(retry_guild_id));
    assert!(!client.begin_application_command_request(retry_guild_id));
    client.clear_application_command_request(retry_guild_id);
    assert!(client.begin_application_command_request(retry_guild_id));

    assert!(client.begin_application_command_request(None));
    assert!(!client.begin_application_command_request(None));
    client.record_application_commands_loaded(None);
    assert!(!client.begin_application_command_request(None));
}

#[test]
fn application_command_metadata_keeps_raw_backend_owned() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    let guild_id = Some(Id::new(1));
    let command = application_command("echo");
    let selected_command = application_command_with_ids("echo", "TestBot", 101, 201);
    let third_party_play = application_command_with_ids("play", "MusicBot", 102, 202);
    let third_party_wordle = application_command_with_ids("wordle", "WordleBot", 103, 203);
    let discord_play = application_command_with_ids("play", "Discord", 104, 204);
    let official_wordle =
        application_command_with_ids("wordle", "Wordle", 105, OFFICIAL_WORDLE_APPLICATION_ID);

    let tui_commands = client.record_application_commands_for_tui(
        guild_id,
        vec![
            command,
            selected_command.clone(),
            third_party_play,
            third_party_wordle,
            discord_play,
            official_wordle,
        ],
    );

    assert_eq!(tui_commands[0].raw, Value::Null);
    assert_eq!(
        command_sources(&tui_commands),
        vec![
            ("echo", Some("TestBot")),
            ("echo", Some("TestBot")),
            ("play", Some("MusicBot")),
            ("wordle", Some("WordleBot")),
        ]
    );
    let commands = client
        .application_commands
        .lock()
        .expect("application command cache lock is not poisoned");
    let cached_commands = commands.get(&guild_id).expect("backend cache");
    assert_eq!(cached_commands[0].raw["name"], "echo");
    assert_eq!(
        command_sources(cached_commands),
        vec![
            ("echo", Some("TestBot")),
            ("echo", Some("TestBot")),
            ("play", Some("MusicBot")),
            ("wordle", Some("WordleBot")),
        ]
    );
    drop(commands);

    let interaction = client
        .application_command_interaction(&crate::discord::ApplicationCommandInvocation {
            guild_id,
            channel_id: Id::new(2),
            command_identity: Some(selected_command.identity()),
            command_name: "echo".to_owned(),
            content: "/echo".to_owned(),
        })
        .expect("selected command identity should resolve");
    assert_eq!(interaction.command.identity(), selected_command.identity());
}

#[tokio::test]
async fn user_profile_requests_are_gated_by_backend_lifecycle_and_cache() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    let user_id = Id::new(10);
    let guild_id = Some(Id::new(1));

    assert_eq!(
        client.next_user_profile_request(user_id, guild_id),
        Some((user_id, guild_id, false))
    );
    assert_eq!(client.next_user_profile_request(user_id, guild_id), None);

    client
        .publish_event(user_profile_load_failed_event(
            UserProfileLoadFailedFixture {
                user_id,
                guild_id,
                message: "temporary failure".to_owned(),
            },
        ))
        .await;
    assert_eq!(
        client.next_user_profile_request(user_id, guild_id),
        Some((user_id, guild_id, false))
    );

    client
        .publish_event(AppEvent::UserProfileLoaded {
            guild_id,
            profile: user_profile(user_id),
        })
        .await;
    assert_eq!(client.next_user_profile_request(user_id, guild_id), None);
}

#[tokio::test]
async fn user_note_requests_are_gated_by_backend_lifecycle_and_cache() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    let user_id = Id::new(10);

    assert_eq!(client.next_user_note_request(user_id), Some(user_id));
    assert_eq!(client.next_user_note_request(user_id), None);

    client.mark_user_note_request_failed(user_id);
    assert_eq!(client.next_user_note_request(user_id), Some(user_id));

    client
        .publish_event(AppEvent::UserNoteLoaded {
            user_id,
            note: Some("note".to_owned()),
        })
        .await;
    assert_eq!(client.next_user_note_request(user_id), None);
}

#[test]
fn guild_member_search_validates_query_and_caps_limit() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    let mut gateway_commands = client
        .gateway_commands_rx
        .lock()
        .expect("gateway command receiver mutex is not poisoned")
        .take()
        .expect("gateway commands can be taken once");

    client
        .search_guild_members(Id::new(1), " a ".to_owned(), 10)
        .expect("short search is ignored without closing channel");
    assert!(matches!(
        gateway_commands.try_recv(),
        Err(tokio::sync::mpsc::error::TryRecvError::Empty)
    ));

    let long_query = "İ".repeat(MEMBER_SEARCH_MAX_QUERY_CHARS + 10);
    client
        .search_guild_members(Id::new(1), long_query, 99)
        .expect("valid search should queue");

    let command = gateway_commands
        .try_recv()
        .expect("search command should be queued");
    let GatewayCommand::RequestGuildMembers {
        guild_id,
        query,
        limit,
        presences,
        nonce,
    } = command
    else {
        panic!("expected guild member search command");
    };
    assert_eq!(guild_id, Id::new(1));
    assert_eq!(query.chars().count(), MEMBER_SEARCH_MAX_QUERY_CHARS);
    assert_eq!(limit, MEMBER_SEARCH_MAX_LIMIT);
    assert!(presences);
    let nonce = nonce.expect("member search should include nonce");
    assert!(nonce.starts_with("mention-ac-1-"));
    assert!(!nonce.contains(&query));
}

#[test]
fn guild_member_request_by_ids_queues_gateway_command() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    let mut gateway_commands = client
        .gateway_commands_rx
        .lock()
        .expect("gateway command receiver mutex is not poisoned")
        .take()
        .expect("gateway commands can be taken once");

    client
        .request_guild_members_by_ids(Id::new(1), Vec::new())
        .expect("empty request is ignored without closing channel");
    assert!(matches!(
        gateway_commands.try_recv(),
        Err(tokio::sync::mpsc::error::TryRecvError::Empty)
    ));

    client
        .request_guild_members_by_ids(Id::new(1), vec![Id::new(20), Id::new(30)])
        .expect("valid request should queue");

    let command = gateway_commands
        .try_recv()
        .expect("member request should be queued");
    let GatewayCommand::RequestGuildMembersByIds {
        guild_id,
        user_ids,
        presences,
    } = command
    else {
        panic!("expected guild member id request command");
    };
    assert_eq!(guild_id, Id::new(1));
    assert_eq!(user_ids, vec![Id::new(20), Id::new(30)]);
    assert!(!presences);
}

#[tokio::test]
async fn requested_voice_state_ignores_observed_other_client_voice() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");

    client
        .publish_event(AppEvent::Ready {
            user: "me".to_owned(),
            user_id: Some(Id::new(10)),
        })
        .await;
    client
        .publish_event(AppEvent::VoiceStateUpdate {
            state: VoiceStateInfo {
                session_id: Some("other-client-voice-session".to_owned()),
                ..VoiceStateInfo::test(Id::new(1), Some(Id::new(10)), Id::new(10))
            },
        })
        .await;

    assert_eq!(client.requested_voice_connection(), None);
    assert!(client.current_or_requested_voice_connection().is_some());
}

#[tokio::test]
async fn voice_state_transitions_publish_join_and_leave_sound_effects() {
    let _ = rustls::crypto::ring::default_provider().install_default();
    let client = DiscordClient::new("test-token".to_owned()).expect("token is valid header");
    let mut effects = client.take_effects();

    client
        .publish_event(AppEvent::Ready {
            user: "me".to_owned(),
            user_id: Some(Id::new(10)),
        })
        .await;
    client
        .publish_event(AppEvent::VoiceStateUpdate {
            state: voice_state(10, Some(11)),
        })
        .await;
    assert_voice_sound(&mut effects, VoiceSoundKind::Join).await;

    client
        .publish_event(AppEvent::VoiceStateUpdate {
            state: voice_state(20, Some(11)),
        })
        .await;
    assert_voice_sound(&mut effects, VoiceSoundKind::Join).await;

    client
        .publish_event(AppEvent::VoiceStateUpdate {
            state: voice_state(20, None),
        })
        .await;
    assert_voice_sound(&mut effects, VoiceSoundKind::Leave).await;

    client
        .publish_event(AppEvent::VoiceStateUpdate {
            state: voice_state(10, None),
        })
        .await;
    assert_voice_sound(&mut effects, VoiceSoundKind::Leave).await;
}

#[test]
fn validates_token_header_values() {
    validate_token_header("raw-user-token").expect("raw user token must be accepted");
    validate_token_header("invalid\nuser-token")
        .expect_err("newlines are not valid authorization header values");
}

fn message_create_event(message_id: u64) -> AppEvent {
    build_message_create_event(MessageCreateFixture {
        message_id: Id::new(message_id),
        content: Some(format!("msg {message_id}")),
        ..guild_message_create_fixture()
    })
}

const VIEW_CHANNEL: u64 = 0x0000_0000_0000_0400;
const SEND_MESSAGES: u64 = 0x0000_0000_0000_0800;

async fn publish_permission_fixture(
    client: &DiscordClient,
    channel_kind: &str,
    everyone_permissions: u64,
) {
    client
        .publish_event(AppEvent::Ready {
            user: "me".to_owned(),
            user_id: Some(Id::new(10)),
        })
        .await;
    client
        .publish_event(guild_create_event(GuildCreateFixture {
            member_count: Some(1),
            owner_id: Some(Id::new(99)),
            channels: vec![permission_fixture_channel(
                Id::new(1),
                Id::new(2),
                channel_kind,
            )],
            members: vec![permission_fixture_member(Id::new(10))],
            roles: vec![permission_fixture_role(
                Id::new(1),
                "@everyone",
                everyone_permissions,
            )],
            ..GuildCreateFixture::new(Id::new(1))
        }))
        .await;
}

fn permission_fixture_channel(
    guild_id: Id<GuildMarker>,
    channel_id: Id<ChannelMarker>,
    kind: &str,
) -> ChannelInfo {
    ChannelInfo {
        guild_id: Some(guild_id),
        position: Some(0),
        name: "guarded".to_owned(),
        ..ChannelInfo::test(channel_id, kind)
    }
}

fn permission_fixture_member(user_id: Id<UserMarker>) -> MemberInfo {
    MemberInfo {
        username: Some("me".to_owned()),
        ..MemberInfo::test(user_id, "me")
    }
}

fn permission_fixture_role(id: Id<RoleMarker>, name: &str, permissions: u64) -> RoleInfo {
    RoleInfo {
        permissions,
        ..RoleInfo::test(id, name)
    }
}

fn user_profile(user_id: Id<UserMarker>) -> UserProfileInfo {
    UserProfileInfo::test(user_id, "neo")
}

fn application_command(name: &str) -> crate::discord::ApplicationCommandInfo {
    application_command_with_app_name(name, "TestBot")
}

fn application_command_with_app_name(
    name: &str,
    application_name: &str,
) -> crate::discord::ApplicationCommandInfo {
    application_command_with_ids(name, application_name, 100, 200)
}

fn application_command_with_ids(
    name: &str,
    application_name: &str,
    command_id: u64,
    application_id: u64,
) -> crate::discord::ApplicationCommandInfo {
    crate::discord::ApplicationCommandInfo {
        application_id: Id::new(application_id),
        version: "1".to_owned(),
        application_name: Some(application_name.to_owned()),
        description: format!("{name} command"),
        raw: json!({
            "id": command_id.to_string(),
            "application_id": application_id.to_string(),
            "version": "1",
            "name": name,
        }),
        ..crate::discord::ApplicationCommandInfo::test(Id::new(command_id), name)
    }
}

fn command_sources(
    commands: &[crate::discord::ApplicationCommandInfo],
) -> Vec<(&str, Option<&str>)> {
    commands
        .iter()
        .map(|command| (command.name.as_str(), command.application_name.as_deref()))
        .collect()
}

fn channel_upsert_event() -> AppEvent {
    AppEvent::ChannelUpsert(ChannelInfo {
        guild_id: Some(Id::new(1)),
        parent_id: Some(Id::new(10)),
        name: "general".to_owned(),
        ..ChannelInfo::test(Id::new(2), "GuildText")
    })
}

fn voice_state(user_id: u64, channel_id: Option<u64>) -> VoiceStateInfo {
    VoiceStateInfo::test(Id::new(1), channel_id.map(Id::new), Id::new(user_id))
}

async fn assert_voice_sound(
    effects: &mut tokio::sync::mpsc::Receiver<crate::discord::SequencedAppEvent>,
    expected: VoiceSoundKind,
) {
    let effect = effects
        .recv()
        .await
        .expect("voice sound effect is published");
    assert!(matches!(effect.event, AppEvent::VoiceSound { kind } if kind == expected));
}

fn assert_voice_update(
    gateway_commands: &mut tokio::sync::mpsc::UnboundedReceiver<GatewayCommand>,
    expected_guild_id: Id<crate::discord::ids::marker::GuildMarker>,
    expected_channel_id: Option<Id<crate::discord::ids::marker::ChannelMarker>>,
    expected_self_mute: bool,
    expected_self_deaf: bool,
) {
    let command = gateway_commands
        .try_recv()
        .expect("voice command should be queued");
    let GatewayCommand::UpdateVoiceState {
        guild_id,
        channel_id,
        self_mute,
        self_deaf,
    } = command
    else {
        panic!("expected voice update command");
    };

    assert_eq!(guild_id, Some(expected_guild_id));
    assert_eq!(channel_id, expected_channel_id);
    assert_eq!(self_mute, expected_self_mute);
    assert_eq!(self_deaf, expected_self_deaf);
}

fn thread_channel_upsert_event() -> AppEvent {
    AppEvent::ChannelUpsert(ChannelInfo {
        guild_id: Some(Id::new(1)),
        parent_id: Some(Id::new(2)),
        name: "new-thread".to_owned(),
        ..ChannelInfo::test(Id::new(3), "GuildPublicThread")
    })
}