opencrabs 0.3.16

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Install with: cargo install opencrabs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
use super::*;
use crossterm::event::{KeyCode, KeyEvent};

#[test]
fn test_wizard_creation() {
    let wizard = OnboardingWizard::new();
    assert_eq!(wizard.step, OnboardingStep::ModeSelect);
    assert_eq!(wizard.mode, WizardMode::QuickStart);
    assert_eq!(wizard.channel_toggles.len(), CHANNEL_NAMES.len());
}

#[test]
fn test_step_navigation() {
    let mut wizard = OnboardingWizard::new();
    wizard.ps.api_key_input = "test-key".to_string();

    assert_eq!(wizard.step, OnboardingStep::ModeSelect);
    wizard.next_step(); // ModeSelect -> Workspace
    assert_eq!(wizard.step, OnboardingStep::Workspace);
}

#[test]
fn test_advanced_mode_all_steps() {
    let mut wizard = OnboardingWizard::new();
    wizard.mode = WizardMode::Advanced;

    wizard.next_step(); // ModeSelect -> Workspace
    assert_eq!(wizard.step, OnboardingStep::Workspace);
    wizard.next_step(); // Workspace -> ProviderAuth (detect_existing_key clears api_key_input)
    assert_eq!(wizard.step, OnboardingStep::ProviderAuth);
    wizard.ps.api_key_input = "test-key".to_string(); // set key AFTER reaching ProviderAuth
    wizard.next_step(); // ProviderAuth -> Channels
    assert_eq!(wizard.step, OnboardingStep::Channels);
    wizard.next_step(); // Channels -> VoiceSetup
    assert_eq!(wizard.step, OnboardingStep::VoiceSetup);
    wizard.next_step(); // VoiceSetup -> ImageSetup (Advanced)
    assert_eq!(wizard.step, OnboardingStep::ImageSetup);
    wizard.next_step(); // ImageSetup -> Daemon
    assert_eq!(wizard.step, OnboardingStep::Daemon);
    wizard.next_step(); // Daemon -> HealthCheck
    assert_eq!(wizard.step, OnboardingStep::HealthCheck);
}

#[test]
fn test_channels_telegram_goes_to_telegram_setup() {
    let mut wizard = clean_wizard();
    wizard.mode = WizardMode::Advanced;
    wizard.step = OnboardingStep::Channels;

    // Enable Telegram in channel toggles
    wizard.channel_toggles[0].1 = true;

    // Enter Telegram setup (focus on Telegram, press Enter)
    wizard.focused_field = 0;
    wizard.handle_key(key(KeyCode::Enter));
    assert_eq!(wizard.step, OnboardingStep::TelegramSetup);

    // Complete Telegram → back to Channels
    wizard.next_step();
    assert_eq!(wizard.step, OnboardingStep::Channels);

    // Continue to VoiceSetup
    wizard.focused_field = wizard.channel_toggles.len();
    wizard.handle_key(key(KeyCode::Enter));
    assert_eq!(wizard.step, OnboardingStep::VoiceSetup);
}

#[test]
fn test_channels_whatsapp_skips_to_voice() {
    let mut wizard = OnboardingWizard::new();
    wizard.mode = WizardMode::Advanced;

    wizard.next_step(); // ModeSelect -> Workspace
    wizard.next_step(); // Workspace -> ProviderAuth
    wizard.ps.api_key_input = "test-key".to_string();
    wizard.next_step(); // ProviderAuth -> Channels

    // Enable WhatsApp only (no token sub-step)
    wizard.channel_toggles[2].1 = true;
    wizard.next_step(); // Channels -> VoiceSetup (WhatsApp has no sub-step)
    assert_eq!(wizard.step, OnboardingStep::VoiceSetup);
    // Verify channel_toggles WhatsApp is enabled
    assert!(wizard.channel_toggles[2].1);
}

#[test]
fn test_channels_full_chain_telegram_discord_slack() {
    let mut wizard = clean_wizard();
    wizard.mode = WizardMode::Advanced;
    wizard.step = OnboardingStep::Channels;

    // Enable all three token-based channels
    wizard.channel_toggles[0].1 = true; // Telegram
    wizard.channel_toggles[1].1 = true; // Discord
    wizard.channel_toggles[3].1 = true; // Slack

    // Enter Telegram setup
    wizard.focused_field = 0;
    wizard.handle_key(key(KeyCode::Enter));
    assert_eq!(wizard.step, OnboardingStep::TelegramSetup);

    // Complete Telegram → back to Channels
    wizard.next_step();
    assert_eq!(wizard.step, OnboardingStep::Channels);

    // Enter Discord setup
    wizard.focused_field = 1;
    wizard.handle_key(key(KeyCode::Enter));
    assert_eq!(wizard.step, OnboardingStep::DiscordSetup);

    // Complete Discord → back to Channels
    wizard.next_step();
    assert_eq!(wizard.step, OnboardingStep::Channels);

    // Enter Slack setup
    wizard.focused_field = 3;
    wizard.handle_key(key(KeyCode::Enter));
    assert_eq!(wizard.step, OnboardingStep::SlackSetup);

    // Complete Slack → back to Channels
    wizard.next_step();
    assert_eq!(wizard.step, OnboardingStep::Channels);

    // Continue to VoiceSetup
    wizard.focused_field = wizard.channel_toggles.len();
    wizard.handle_key(key(KeyCode::Enter));
    assert_eq!(wizard.step, OnboardingStep::VoiceSetup);
}

#[test]
fn test_voice_setup_defaults() {
    let wizard = OnboardingWizard::new();
    assert!(wizard.groq_api_key_input.is_empty());
    assert!(!wizard.tts_enabled);
    assert_eq!(wizard.voice_field, VoiceField::SttModeSelect);
}

#[test]
fn test_step_numbers() {
    assert_eq!(OnboardingStep::ModeSelect.number(), 1);
    assert_eq!(OnboardingStep::Channels.number(), 4);
    assert_eq!(OnboardingStep::TelegramSetup.number(), 4); // sub-step of Channels
    assert_eq!(OnboardingStep::VoiceSetup.number(), 5);
    assert_eq!(OnboardingStep::ImageSetup.number(), 6);
    assert_eq!(OnboardingStep::HealthCheck.number(), 8);
    assert_eq!(OnboardingStep::BrainSetup.number(), 9);
    assert_eq!(OnboardingStep::total(), 9);
}

#[test]
fn test_prev_step_cancel() {
    let mut wizard = OnboardingWizard::new();
    // Going back from step 1 signals cancel
    assert!(wizard.prev_step());
}

#[test]
fn test_provider_auth_defaults() {
    let wizard = clean_wizard();
    assert_eq!(wizard.ps.selected_provider, 0);
    assert_eq!(wizard.auth_field, AuthField::Provider);
    assert!(wizard.ps.api_key_input.is_empty());
    assert_eq!(wizard.ps.selected_model, 0);
    // First provider is Anthropic Claude
    assert_eq!(
        PROVIDERS[wizard.ps.selected_provider].name,
        "Anthropic Claude"
    );
    assert!(!PROVIDERS[wizard.ps.selected_provider].help_lines.is_empty());
}

#[test]
fn test_channel_toggles_default_off() {
    let wizard = OnboardingWizard::new();
    assert_eq!(wizard.channel_toggles.len(), CHANNEL_NAMES.len());
    // All channels default to disabled
    for (name, enabled) in &wizard.channel_toggles {
        assert!(!enabled, "Channel {} should default to disabled", name);
    }
    // Verify all expected channels are present
    let toggle_names: Vec<&str> = wizard
        .channel_toggles
        .iter()
        .map(|(n, _)| n.as_str())
        .collect();
    assert!(toggle_names.contains(&"Telegram"));
    assert!(toggle_names.contains(&"Discord"));
    assert!(toggle_names.contains(&"Trello"));
}

/// Create a wizard with clean defaults (no config auto-detection).
/// `OnboardingWizard::new()` loads existing config from disk, which
/// pollutes provider/brain fields when a real config exists.
fn clean_wizard() -> OnboardingWizard {
    let mut w = OnboardingWizard::new();
    w.ps.selected_provider = 0;
    w.ps.api_key_input = String::new();
    w.ps.base_url = String::new();
    w.ps.custom_model = String::new();
    w.about_me = String::new();
    w.about_opencrabs = String::new();
    w.original_about_me = String::new();
    w.original_about_opencrabs = String::new();
    w
}

// ── handle_key tests ──

fn key(code: KeyCode) -> KeyEvent {
    KeyEvent::new(code, crossterm::event::KeyModifiers::empty())
}

#[test]
fn test_handle_key_mode_select_up_down() {
    let mut wizard = OnboardingWizard::new();
    assert_eq!(wizard.mode, WizardMode::QuickStart);

    wizard.handle_key(key(KeyCode::Down));
    assert_eq!(wizard.mode, WizardMode::Advanced);

    wizard.handle_key(key(KeyCode::Up));
    assert_eq!(wizard.mode, WizardMode::QuickStart);
}

#[test]
fn test_handle_key_mode_select_number_keys() {
    let mut wizard = OnboardingWizard::new();

    wizard.handle_key(key(KeyCode::Char('2')));
    assert_eq!(wizard.mode, WizardMode::Advanced);

    wizard.handle_key(key(KeyCode::Char('1')));
    assert_eq!(wizard.mode, WizardMode::QuickStart);
}

#[test]
fn test_handle_key_mode_select_enter_advances() {
    let mut wizard = OnboardingWizard::new();
    let action = wizard.handle_key(key(KeyCode::Enter));
    assert_eq!(action, WizardAction::None);
    assert_eq!(wizard.step, OnboardingStep::Workspace);
}

#[test]
fn test_handle_key_escape_from_step1_cancels() {
    let mut wizard = OnboardingWizard::new();
    let action = wizard.handle_key(key(KeyCode::Esc));
    assert_eq!(action, WizardAction::Cancel);
}

#[test]
fn test_handle_key_escape_from_step2_goes_back() {
    let mut wizard = OnboardingWizard::new();
    wizard.handle_key(key(KeyCode::Enter)); // ModeSelect -> Workspace
    assert_eq!(wizard.step, OnboardingStep::Workspace);

    let action = wizard.handle_key(key(KeyCode::Esc));
    assert_eq!(action, WizardAction::None);
    assert_eq!(wizard.step, OnboardingStep::ModeSelect);
}

#[test]
fn test_handle_key_provider_navigation() {
    let mut wizard = clean_wizard();
    wizard.step = OnboardingStep::ProviderAuth;
    wizard.auth_field = AuthField::Provider;
    assert_eq!(wizard.ps.selected_provider, 0);

    wizard.handle_key(key(KeyCode::Down));
    // Next alphabetically after Anthropic: Claude CLI(7)
    assert_eq!(wizard.ps.selected_provider, 7);

    wizard.handle_key(key(KeyCode::Up));
    assert_eq!(wizard.ps.selected_provider, 0);

    // Can't go below 0
    wizard.handle_key(key(KeyCode::Up));
    assert_eq!(wizard.ps.selected_provider, 0);
}

#[test]
fn test_handle_key_api_key_typing() {
    let mut wizard = clean_wizard();
    wizard.step = OnboardingStep::ProviderAuth;
    wizard.auth_field = AuthField::Provider;

    // Enter to select provider -> goes to ApiKey field
    wizard.handle_key(key(KeyCode::Enter));
    assert_eq!(wizard.auth_field, AuthField::ApiKey);

    // Type a key
    wizard.handle_key(key(KeyCode::Char('s')));
    wizard.handle_key(key(KeyCode::Char('k')));
    assert_eq!(wizard.ps.api_key_input, "sk");

    // Backspace
    wizard.handle_key(key(KeyCode::Backspace));
    assert_eq!(wizard.ps.api_key_input, "s");
}

#[test]
fn test_handle_key_provider_auth_field_flow() {
    let mut wizard = clean_wizard();
    wizard.step = OnboardingStep::ProviderAuth;
    wizard.auth_field = AuthField::Provider;
    assert_eq!(wizard.auth_field, AuthField::Provider);

    // Enter goes to ApiKey
    wizard.handle_key(key(KeyCode::Enter));
    assert_eq!(wizard.auth_field, AuthField::ApiKey);

    // Tab goes to Model
    wizard.handle_key(key(KeyCode::Tab));
    assert_eq!(wizard.auth_field, AuthField::Model);

    // BackTab goes back to ApiKey
    wizard.handle_key(key(KeyCode::BackTab));
    assert_eq!(wizard.auth_field, AuthField::ApiKey);

    // BackTab from ApiKey goes to Provider
    wizard.handle_key(key(KeyCode::BackTab));
    assert_eq!(wizard.auth_field, AuthField::Provider);
}

#[test]
fn test_handle_key_complete_step_returns_complete() {
    let mut wizard = OnboardingWizard::new();
    wizard.step = OnboardingStep::Complete;
    let action = wizard.handle_key(key(KeyCode::Enter));
    assert_eq!(action, WizardAction::Complete);
}

#[test]
fn test_quickstart_skips_channels_voice() {
    let mut wizard = OnboardingWizard::new();
    wizard.mode = WizardMode::QuickStart;

    wizard.next_step(); // ModeSelect -> Workspace
    assert_eq!(wizard.step, OnboardingStep::Workspace);
    wizard.next_step(); // Workspace -> ProviderAuth
    assert_eq!(wizard.step, OnboardingStep::ProviderAuth);
    wizard.ps.api_key_input = "test-key".to_string();
    wizard.next_step(); // ProviderAuth -> Daemon (QuickStart skips Channels & Voice)
    assert_eq!(wizard.step, OnboardingStep::Daemon);
}

#[test]
fn test_provider_auth_validation_empty_key() {
    let mut wizard = clean_wizard();
    wizard.step = OnboardingStep::ProviderAuth;
    // api_key_input is empty
    wizard.next_step();
    // Should stay on ProviderAuth with error
    assert_eq!(wizard.step, OnboardingStep::ProviderAuth);
    assert!(wizard.error_message.is_some());
    assert!(
        wizard
            .error_message
            .as_ref()
            .is_some_and(|m| m.contains("required"))
    );
}

#[test]
fn test_model_selection() {
    let mut wizard = OnboardingWizard::new();
    wizard.step = OnboardingStep::ProviderAuth;
    wizard.auth_field = AuthField::Model;
    // Set up config models for selection testing
    wizard.ps.config_models = vec!["model-a".into(), "model-b".into(), "model-c".into()];

    assert_eq!(wizard.ps.selected_model, 0);
    wizard.handle_key(key(KeyCode::Down));
    assert_eq!(wizard.ps.selected_model, 1);
    wizard.handle_key(key(KeyCode::Down));
    assert_eq!(wizard.ps.selected_model, 2);
    // Should clamp to max
    for _ in 0..20 {
        wizard.handle_key(key(KeyCode::Down));
    }
    // Provider selection stays within bounds (7 static + existing custom providers)
    let max_idx = PROVIDERS.len() + wizard.ps.custom_names.len();
    assert!(wizard.ps.selected_provider < max_idx);
}

#[test]
fn test_workspace_path_default() {
    let wizard = OnboardingWizard::new();
    // Should have a default workspace path
    assert!(!wizard.workspace_path.is_empty());
}

#[test]
fn test_health_check_initial_state() {
    let wizard = OnboardingWizard::new();
    // health_results starts empty (populated on start_health_check)
    assert!(wizard.health_results.is_empty());
}

#[test]
fn test_brain_setup_defaults() {
    let wizard = clean_wizard();
    assert!(wizard.about_me.is_empty());
    assert!(wizard.about_opencrabs.is_empty());
    assert_eq!(wizard.brain_field, BrainField::AboutMe);
}

// --- Model fetching helpers ---

#[test]
fn test_openrouter_provider_index() {
    // OpenRouter is index 4, Custom is last
    assert_eq!(PROVIDERS[4].name, "OpenRouter");
    assert_eq!(PROVIDERS.last().unwrap().name, "Custom OpenAI-Compatible");
}

#[test]
fn test_model_count_uses_fetched_when_available() {
    let mut wizard = OnboardingWizard::new();
    // Clear any models loaded from existing config
    wizard.ps.config_models.clear();
    wizard.ps.models.clear();
    // Anthropic (0) has no static models — fetched from API
    wizard.ps.selected_provider = 0;
    assert_eq!(wizard.ps.model_count(), 0);

    // After fetching
    wizard.ps.models = vec![
        "model-a".into(),
        "model-b".into(),
        "model-c".into(),
        "model-d".into(),
    ];
    assert_eq!(wizard.ps.model_count(), 4);
}

#[test]
fn test_selected_model_name_uses_fetched() {
    let mut wizard = OnboardingWizard::new();
    // No static models - should use fetched or show placeholder
    assert!(wizard.ps.selected_model_name().is_empty() || wizard.ps.models.is_empty());

    wizard.ps.models = vec!["live-model-1".into(), "live-model-2".into()];
    wizard.ps.selected_model = 1;
    assert_eq!(wizard.ps.selected_model_name(), "live-model-2");
}

#[test]
fn test_supports_model_fetch() {
    let mut wizard = OnboardingWizard::new();
    wizard.ps.selected_provider = 0; // Anthropic
    assert!(wizard.ps.supports_model_fetch());
    wizard.ps.selected_provider = 1; // OpenAI
    assert!(wizard.ps.supports_model_fetch());
    wizard.ps.selected_provider = 2; // GitHub Copilot (has /models endpoint)
    assert!(wizard.ps.supports_model_fetch());
    wizard.ps.selected_provider = 3; // Gemini
    assert!(wizard.ps.supports_model_fetch());
    wizard.ps.selected_provider = 4; // OpenRouter
    assert!(wizard.ps.supports_model_fetch());
    wizard.ps.selected_provider = 5; // Minimax
    assert!(!wizard.ps.supports_model_fetch());
    wizard.ps.selected_provider = 6; // z.ai GLM (supports fetch)
    assert!(wizard.ps.supports_model_fetch());
    wizard.ps.selected_provider = 7; // Claude CLI
    assert!(!wizard.ps.supports_model_fetch());
    wizard.ps.selected_provider = 8; // OpenCode CLI (supports fetch)
    assert!(wizard.ps.supports_model_fetch());
    wizard.ps.selected_provider = 9; // OpenCode API (supports fetch)
    assert!(wizard.ps.supports_model_fetch());
    wizard.ps.selected_provider = 10; // Qwen — DashScope has no /v1/models
    assert!(!wizard.ps.supports_model_fetch());
    wizard.ps.selected_provider = 11; // Ollama (supports fetch via /api/tags)
    assert!(wizard.ps.supports_model_fetch());
    wizard.ps.selected_provider = 12; // Custom — no base_url = no fetch
    wizard.ps.base_url.clear();
    assert!(!wizard.ps.supports_model_fetch());
    // Custom with base_url set = supports fetch
    wizard.ps.base_url = "http://localhost:11434".to_string();
    assert!(wizard.ps.supports_model_fetch());
}

#[test]
fn test_fetch_models_unsupported_provider_returns_empty() {
    let rt = tokio::runtime::Runtime::new().unwrap();
    let result = rt.block_on(fetch_provider_models(99, None, None, None));
    assert!(result.is_empty());
}

// --- Live API integration tests (skipped if env var not set) ---

#[test]
fn test_fetch_anthropic_models_with_api_key() {
    let key = match std::env::var("ANTHROPIC_API_KEY") {
        Ok(k) if !k.is_empty() => k,
        _ => return, // ANTHROPIC_API_KEY not set, skip
    };
    let rt = tokio::runtime::Runtime::new().unwrap();
    let models = rt.block_on(fetch_provider_models(0, Some(&key), None, None));
    assert!(
        !models.is_empty(),
        "Anthropic should return models with API key"
    );
    // Should contain at least one claude model
    assert!(
        models.iter().any(|m| m.contains("claude")),
        "Expected claude model, got: {:?}",
        models
    );
}

#[test]
fn test_fetch_anthropic_models_with_setup_token() {
    let key = match std::env::var("ANTHROPIC_MAX_SETUP_TOKEN") {
        Ok(k) if !k.is_empty() && k.starts_with("sk-ant-oat") => k,
        _ => return, // ANTHROPIC_MAX_SETUP_TOKEN not set, skip
    };
    let rt = tokio::runtime::Runtime::new().unwrap();
    let models = rt.block_on(fetch_provider_models(0, Some(&key), None, None));
    assert!(
        !models.is_empty(),
        "Anthropic should return models with setup token"
    );
    assert!(
        models.iter().any(|m| m.contains("claude")),
        "Expected claude model, got: {:?}",
        models
    );
}

#[test]
fn test_fetch_openai_models_with_api_key() {
    let key = match std::env::var("OPENAI_API_KEY") {
        Ok(k) if !k.is_empty() => k,
        _ => return, // OPENAI_API_KEY not set, skip
    };
    let rt = tokio::runtime::Runtime::new().unwrap();
    let models = rt.block_on(fetch_provider_models(1, Some(&key), None, None));
    assert!(
        !models.is_empty(),
        "OpenAI should return models with API key"
    );
    assert!(
        models.iter().any(|m| m.contains("gpt")),
        "Expected gpt model, got: {:?}",
        models
    );
}

#[test]
fn test_fetch_openrouter_models_with_api_key() {
    let key = match std::env::var("OPENROUTER_API_KEY") {
        Ok(k) if !k.is_empty() => k,
        _ => return, // OPENROUTER_API_KEY not set, skip
    };
    let rt = tokio::runtime::Runtime::new().unwrap();
    let models = rt.block_on(fetch_provider_models(4, Some(&key), None, None));
    assert!(!models.is_empty(), "OpenRouter should return models");
    // OpenRouter has 400+ models
    assert!(
        models.len() > 50,
        "Expected 50+ models from OpenRouter, got {}",
        models.len()
    );
}

#[test]
fn test_fetch_models_bad_key_returns_empty() {
    let rt = tokio::runtime::Runtime::new().unwrap();
    // Bad key should fail gracefully (empty vec, not panic)
    let models = rt.block_on(fetch_provider_models(
        0,
        Some("sk-bad-key-definitely-invalid"),
        None,
        None,
    ));
    assert!(
        models.is_empty(),
        "Bad key should return empty, got {} models",
        models.len()
    );
}

// ── handle_text_input / handle_text_paste tests ──

use super::helpers::{handle_text_input, handle_text_paste};

fn key_mod(code: KeyCode, modifiers: crossterm::event::KeyModifiers) -> KeyEvent {
    KeyEvent::new(code, modifiers)
}

#[test]
fn test_text_input_char_insert_at_cursor() {
    let mut buf = "hello".to_string();
    let mut cursor = 3; // between 'l' and 'l'
    let event = key(KeyCode::Char('X'));
    assert!(handle_text_input(
        &event,
        &mut buf,
        &mut cursor,
        false,
        None
    ));
    assert_eq!(buf, "helXlo");
    assert_eq!(cursor, 4);
}

#[test]
fn test_text_input_backspace_at_cursor() {
    let mut buf = "hello".to_string();
    let mut cursor = 3;
    let event = key(KeyCode::Backspace);
    assert!(handle_text_input(
        &event,
        &mut buf,
        &mut cursor,
        false,
        None
    ));
    assert_eq!(buf, "helo");
    assert_eq!(cursor, 2);
}

#[test]
fn test_text_input_backspace_at_start_noop() {
    let mut buf = "hello".to_string();
    let mut cursor = 0;
    let event = key(KeyCode::Backspace);
    assert!(handle_text_input(
        &event,
        &mut buf,
        &mut cursor,
        false,
        None
    ));
    assert_eq!(buf, "hello");
    assert_eq!(cursor, 0);
}

#[test]
fn test_text_input_delete_at_cursor() {
    let mut buf = "hello".to_string();
    let mut cursor = 2;
    let event = key(KeyCode::Delete);
    assert!(handle_text_input(
        &event,
        &mut buf,
        &mut cursor,
        false,
        None
    ));
    assert_eq!(buf, "helo");
    assert_eq!(cursor, 2);
}

#[test]
fn test_text_input_delete_at_end_noop() {
    let mut buf = "hello".to_string();
    let mut cursor = 5;
    let event = key(KeyCode::Delete);
    assert!(handle_text_input(
        &event,
        &mut buf,
        &mut cursor,
        false,
        None
    ));
    assert_eq!(buf, "hello");
    assert_eq!(cursor, 5);
}

#[test]
fn test_text_input_left_right_cursor() {
    let mut buf = "abc".to_string();
    let mut cursor = 2;

    let left = key(KeyCode::Left);
    assert!(handle_text_input(&left, &mut buf, &mut cursor, false, None));
    assert_eq!(cursor, 1);

    let right = key(KeyCode::Right);
    assert!(handle_text_input(
        &right,
        &mut buf,
        &mut cursor,
        false,
        None
    ));
    assert_eq!(cursor, 2);
}

#[test]
fn test_text_input_home_end() {
    let mut buf = "hello world".to_string();
    let mut cursor = 5;

    let home = key(KeyCode::Home);
    assert!(handle_text_input(&home, &mut buf, &mut cursor, false, None));
    assert_eq!(cursor, 0);

    let end = key(KeyCode::End);
    assert!(handle_text_input(&end, &mut buf, &mut cursor, false, None));
    assert_eq!(cursor, buf.len());
}

#[test]
fn test_text_input_sentinel_clears_on_char() {
    let mut buf = "__EXISTING_KEY__".to_string();
    let mut cursor = 16;
    let event = key(KeyCode::Char('a'));
    assert!(handle_text_input(&event, &mut buf, &mut cursor, true, None));
    assert_eq!(buf, "a");
    assert_eq!(cursor, 1);
}

#[test]
fn test_text_input_sentinel_clears_on_backspace() {
    let mut buf = "__EXISTING_KEY__".to_string();
    let mut cursor = 16;
    let event = key(KeyCode::Backspace);
    assert!(handle_text_input(&event, &mut buf, &mut cursor, true, None));
    assert_eq!(buf, "");
    assert_eq!(cursor, 0);
}

#[test]
fn test_text_input_sentinel_clears_on_delete() {
    let mut buf = "__EXISTING_KEY__".to_string();
    let mut cursor = 0;
    let event = key(KeyCode::Delete);
    assert!(handle_text_input(&event, &mut buf, &mut cursor, true, None));
    assert_eq!(buf, "");
    assert_eq!(cursor, 0);
}

#[test]
fn test_text_input_ctrl_backspace_clears_all() {
    let mut buf = "hello world".to_string();
    let mut cursor = 5;
    let event = key_mod(KeyCode::Backspace, crossterm::event::KeyModifiers::CONTROL);
    assert!(handle_text_input(
        &event,
        &mut buf,
        &mut cursor,
        false,
        None
    ));
    assert_eq!(buf, "");
    assert_eq!(cursor, 0);
}

#[test]
fn test_text_input_char_filter() {
    let mut buf = String::new();
    let mut cursor = 0;
    let digits_only: Option<fn(char) -> bool> = Some(|c: char| c.is_ascii_digit());

    // Letter rejected
    let event = key(KeyCode::Char('a'));
    assert!(handle_text_input(
        &event,
        &mut buf,
        &mut cursor,
        false,
        digits_only
    ));
    assert_eq!(buf, "");

    // Digit accepted
    let event = key(KeyCode::Char('5'));
    assert!(handle_text_input(
        &event,
        &mut buf,
        &mut cursor,
        false,
        digits_only
    ));
    assert_eq!(buf, "5");
    assert_eq!(cursor, 1);
}

#[test]
fn test_text_input_enter_not_consumed() {
    let mut buf = "hello".to_string();
    let mut cursor = 5;
    let event = key(KeyCode::Enter);
    assert!(!handle_text_input(
        &event,
        &mut buf,
        &mut cursor,
        false,
        None
    ));
    assert_eq!(buf, "hello"); // unchanged
}

#[test]
fn test_text_input_word_jump_ctrl_left() {
    let mut buf = "hello world foo".to_string();
    let mut cursor = 15; // end
    let event = key_mod(KeyCode::Left, crossterm::event::KeyModifiers::CONTROL);
    assert!(handle_text_input(
        &event,
        &mut buf,
        &mut cursor,
        false,
        None
    ));
    assert_eq!(cursor, 12); // start of "foo"
    assert!(handle_text_input(
        &event,
        &mut buf,
        &mut cursor,
        false,
        None
    ));
    assert_eq!(cursor, 6); // start of "world"
}

#[test]
fn test_text_input_word_jump_ctrl_right() {
    let mut buf = "hello world foo".to_string();
    let mut cursor = 0;
    let event = key_mod(KeyCode::Right, crossterm::event::KeyModifiers::CONTROL);
    assert!(handle_text_input(
        &event,
        &mut buf,
        &mut cursor,
        false,
        None
    ));
    assert_eq!(cursor, 6); // after "hello "
    assert!(handle_text_input(
        &event,
        &mut buf,
        &mut cursor,
        false,
        None
    ));
    assert_eq!(cursor, 12); // after "world "
}

// ── handle_text_paste tests ──

#[test]
fn test_text_paste_at_cursor() {
    let mut buf = "helo".to_string();
    let mut cursor = 2;
    handle_text_paste("ll", &mut buf, &mut cursor, false, None);
    assert_eq!(buf, "helllo");
    assert_eq!(cursor, 4);
}

#[test]
fn test_text_paste_sentinel_clears_first() {
    let mut buf = "__EXISTING_KEY__".to_string();
    let mut cursor = 16;
    handle_text_paste("new-token-123", &mut buf, &mut cursor, true, None);
    assert_eq!(buf, "new-token-123");
    assert_eq!(cursor, 13);
}

#[test]
fn test_text_paste_with_filter() {
    let mut buf = String::new();
    let mut cursor = 0;
    handle_text_paste(
        "abc123def456",
        &mut buf,
        &mut cursor,
        false,
        Some(|c: char| c.is_ascii_digit()),
    );
    assert_eq!(buf, "123456");
    assert_eq!(cursor, 6);
}

#[test]
fn test_text_paste_sentinel_then_filtered() {
    let mut buf = "__EXISTING_KEY__".to_string();
    let mut cursor = 16;
    handle_text_paste(
        "+1-555-1234",
        &mut buf,
        &mut cursor,
        true,
        Some(|c: char| c.is_ascii_digit() || c == '+' || c == '-'),
    );
    assert_eq!(buf, "+1-555-1234");
    assert_eq!(cursor, 11);
}

// ── Channel input cursor integration tests ──

#[test]
fn test_telegram_sentinel_paste_replaces() {
    let mut wizard = clean_wizard();
    wizard.step = OnboardingStep::TelegramSetup;
    wizard.telegram_field = TelegramField::BotToken;
    wizard.telegram_token_input = EXISTING_KEY_SENTINEL.to_string();
    wizard.channel_input_cursor = EXISTING_KEY_SENTINEL.len();

    // Paste new token — should replace sentinel, not append
    wizard.handle_paste("123456:ABC-DEF");
    assert_eq!(wizard.telegram_token_input, "123456:ABC-DEF");
    assert_eq!(wizard.channel_input_cursor, 14);
}

#[test]
fn test_telegram_sentinel_backspace_clears() {
    let mut wizard = clean_wizard();
    wizard.step = OnboardingStep::TelegramSetup;
    wizard.telegram_field = TelegramField::BotToken;
    wizard.telegram_token_input = EXISTING_KEY_SENTINEL.to_string();
    wizard.channel_input_cursor = EXISTING_KEY_SENTINEL.len();

    wizard.handle_key(key(KeyCode::Backspace));
    assert_eq!(wizard.telegram_token_input, "");
    assert_eq!(wizard.channel_input_cursor, 0);
}

#[test]
fn test_telegram_sentinel_enter_preserves() {
    let mut wizard = clean_wizard();
    wizard.mode = WizardMode::Advanced;
    wizard.step = OnboardingStep::TelegramSetup;
    wizard.telegram_field = TelegramField::BotToken;
    wizard.telegram_token_input = EXISTING_KEY_SENTINEL.to_string();
    wizard.channel_input_cursor = EXISTING_KEY_SENTINEL.len();

    // Enter advances to next field, sentinel stays
    wizard.handle_key(key(KeyCode::Enter));
    assert_eq!(wizard.telegram_token_input, EXISTING_KEY_SENTINEL);
    assert_eq!(wizard.telegram_field, TelegramField::UserID);
}

#[test]
fn test_discord_cursor_movement_in_field() {
    let mut wizard = clean_wizard();
    wizard.step = OnboardingStep::DiscordSetup;
    wizard.discord_field = DiscordField::ChannelID;
    wizard.discord_channel_id_input = "12345".to_string();
    wizard.channel_input_cursor = 5;

    // Move left twice
    wizard.handle_key(key(KeyCode::Left));
    wizard.handle_key(key(KeyCode::Left));
    assert_eq!(wizard.channel_input_cursor, 3);

    // Type a char at cursor position
    wizard.handle_key(key(KeyCode::Char('X')));
    assert_eq!(wizard.discord_channel_id_input, "123X45");
    assert_eq!(wizard.channel_input_cursor, 4);
}

// --- Provider display order & navigation with custom providers ---

#[test]
fn test_provider_display_order_no_customs() {
    let mut wizard = clean_wizard();
    wizard.ps.custom_names.clear();
    let order = wizard.ps.provider_display_order();
    // Named providers sorted alphabetically, then 12 ("+ New Custom") last
    // Anthropic(0), Claude CLI(7), GitHub(2), Gemini(3), Minimax(5), Ollama(11),
    // OpenAI(1), OpenCode CLI(8), OpenCode(9), OpenRouter(4), Qwen(10), z.ai GLM(6)
    assert_eq!(order, vec![0, 7, 2, 3, 5, 11, 1, 9, 8, 4, 10, 6, 12]);
}

#[test]
fn test_provider_display_order_with_customs() {
    let mut wizard = clean_wizard();
    wizard.ps.custom_names = vec!["nvidia".into(), "opus".into(), "opusdistil".into()];
    let order = wizard.ps.provider_display_order();
    // Named providers sorted alphabetically, then 13,14,15 existing customs, 12 ("+ New Custom") last
    assert_eq!(
        order,
        vec![0, 7, 2, 3, 5, 11, 1, 9, 8, 4, 10, 6, 13, 14, 15, 12]
    );
}

#[test]
fn test_provider_nav_down_from_last_static_goes_to_first_custom() {
    let mut wizard = clean_wizard();
    wizard.step = OnboardingStep::ProviderAuth;
    wizard.auth_field = AuthField::Provider;
    wizard.ps.custom_names = vec!["nvidia".into(), "opus".into()];
    wizard.ps.selected_provider = 6; // z.ai GLM (last named alphabetically)

    wizard.handle_key(key(KeyCode::Down));
    // Should go to nvidia (index 13 = CUSTOM_INSTANCES_START), not "+ New Custom" (index 12)
    assert_eq!(
        wizard.ps.selected_provider, 13,
        "Down from z.ai GLM should go to first custom provider, not +New Custom"
    );
}

#[test]
fn test_provider_nav_down_through_customs_to_new() {
    let mut wizard = clean_wizard();
    wizard.step = OnboardingStep::ProviderAuth;
    wizard.auth_field = AuthField::Provider;
    wizard.ps.custom_names = vec!["nvidia".into()];
    wizard.ps.selected_provider = 13; // nvidia

    wizard.handle_key(key(KeyCode::Down));
    // Should go to "+ New Custom" (index 12) which is visually last
    assert_eq!(
        wizard.ps.selected_provider, 12,
        "Down from last custom should go to +New Custom"
    );
}

#[test]
fn test_provider_nav_up_from_new_custom_goes_to_last_custom() {
    let mut wizard = clean_wizard();
    wizard.step = OnboardingStep::ProviderAuth;
    wizard.auth_field = AuthField::Provider;
    wizard.ps.custom_names = vec!["nvidia".into(), "opus".into()];
    wizard.ps.selected_provider = 12; // "+ New Custom"

    wizard.handle_key(key(KeyCode::Up));
    // Should go to opus (index 14), not Ollama (index 11)
    assert_eq!(
        wizard.ps.selected_provider, 14,
        "Up from +New Custom should go to last custom provider"
    );
}

#[test]
fn test_provider_nav_up_from_first_custom_goes_to_last_static() {
    let mut wizard = clean_wizard();
    wizard.step = OnboardingStep::ProviderAuth;
    wizard.auth_field = AuthField::Provider;
    wizard.ps.custom_names = vec!["nvidia".into(), "opus".into()];
    wizard.ps.selected_provider = 13; // nvidia (first custom)
    // CUSTOM_INSTANCES_START is now 13, not 12

    wizard.handle_key(key(KeyCode::Up));
    assert_eq!(
        wizard.ps.selected_provider, 6,
        "Up from first custom should go to z.ai GLM (last named alphabetically)"
    );
}

#[test]
fn test_provider_nav_clamps_at_top_and_bottom() {
    let mut wizard = clean_wizard();
    wizard.step = OnboardingStep::ProviderAuth;
    wizard.auth_field = AuthField::Provider;
    wizard.ps.custom_names = vec!["nvidia".into()];

    // At top, Up stays at 0
    wizard.ps.selected_provider = 0;
    wizard.handle_key(key(KeyCode::Up));
    assert_eq!(wizard.ps.selected_provider, 0);

    // At bottom ("+ New Custom" = CUSTOM_PROVIDER_IDX = 12), Down stays
    wizard.ps.selected_provider = 12;
    wizard.handle_key(key(KeyCode::Down));
    assert_eq!(wizard.ps.selected_provider, 12);
}

#[test]
fn test_provider_nav_full_cycle_matches_display_order() {
    let mut wizard = clean_wizard();
    wizard.step = OnboardingStep::ProviderAuth;
    wizard.auth_field = AuthField::Provider;
    wizard.ps.custom_names = vec!["nvidia".into(), "opus".into()];
    wizard.ps.selected_provider = 0;

    let expected_order = wizard.ps.provider_display_order();
    let mut visited = vec![wizard.ps.selected_provider];

    for _ in 1..expected_order.len() {
        wizard.handle_key(key(KeyCode::Down));
        visited.push(wizard.ps.selected_provider);
    }

    assert_eq!(
        visited, expected_order,
        "Navigation order must match display order"
    );
}