modde-ui 0.2.1

GUI application for modde
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
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
//! Simulator-based UI tests for modde views.
//!
//! These tests use `iced_test::simulator` to render views headlessly and
//! interact with them (find widgets by text, click buttons, type into inputs).
//! They complement the pure state/logic tests in `app.rs` by verifying that
//! the rendered widget tree actually contains expected elements and that
//! click/input interactions produce the right `Message` variants.

use iced_test::core::Point;
use iced_test::selector;
use iced_test::simulator::{click, simulator};
use modde_ui::app::{
    ExecutableUiEntry, Message, SettingsState, ToolReleaseSupport, ToolState, ToolUiEntry,
    WabbajackInstallerState,
};
use modde_ui::semantics;
use modde_ui::views::data_tab::DataTabState;
use modde_ui::views::diagnostics::{
    DiagnosticEntry, DiagnosticSeverity, DiagnosticsReport, DiagnosticsState, IntegritySummary,
};
use modde_ui::views::downloads::{DownloadState, DownloadTask};
use std::path::PathBuf;

fn by_test_id(test_id: &str) -> impl iced_test::Selector<Output = iced_test::selector::Target> {
    selector::id(semantics::widget_id(test_id.to_string()))
}

// ─── Settings View ────────────────────────────────────────────

fn default_settings_state() -> SettingsState {
    SettingsState {
        nexus_api_key_draft: String::new(),
        nexus_api_key_visible: false,
        nexus_api_key_source: None,
        nexus_config_key_exists: false,
        game_install_paths: Vec::new(),
        download_dir: None,
        effective_download_dir: PathBuf::new(),
        has_stock_snapshot: false,
        theme_name: "Dark".to_string(),
        nexus_status: None,
    }
}

#[test]
fn settings_shows_sections() {
    let mut ui = simulator(modde_ui::views::settings::view(default_settings_state()));
    ui.find("Settings").expect("title");
    ui.find("Nexus Mods API Key").expect("API key section");
    ui.find("Game Install Path").expect("game path section");
    ui.find("Download Directory").expect("download dir section");
    ui.find("Stock Game Snapshot")
        .expect("stock snapshot section");
    ui.find("Theme").expect("theme section");
}

#[test]
fn settings_hides_nexus_key_by_default_and_can_toggle() {
    let mut ui = simulator(modde_ui::views::settings::view(default_settings_state()));
    ui.find("Show").expect("should show reveal button");
    ui.click("Show").expect("should click 'Show'");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages
            .iter()
            .any(|m| matches!(m, Message::ToggleNexusApiKeyVisibility)),
        "should emit ToggleNexusApiKeyVisibility, got: {messages:?}",
    );
}

#[test]
fn settings_click_replace_and_remove_emit_messages() {
    let mut ui = simulator(modde_ui::views::settings::view(default_settings_state()));
    ui.click("Replace").expect("should click 'Replace'");
    ui.click("Remove modde config")
        .expect("should click 'Remove modde config'");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages
            .iter()
            .any(|m| matches!(m, Message::ReplaceNexusApiKey)),
        "should emit ReplaceNexusApiKey, got: {messages:?}",
    );
    assert!(
        messages
            .iter()
            .any(|m| matches!(m, Message::RemoveNexusConfigKey)),
        "should emit RemoveNexusConfigKey, got: {messages:?}",
    );
}

#[test]
fn settings_shows_no_snapshot_status() {
    let mut ui = simulator(modde_ui::views::settings::view(default_settings_state()));
    ui.find("No snapshot created")
        .expect("should show 'No snapshot created'");
}

#[test]
fn settings_shows_snapshot_exists() {
    let state = SettingsState {
        has_stock_snapshot: true,
        ..default_settings_state()
    };
    let mut ui = simulator(modde_ui::views::settings::view(state));
    ui.find("Snapshot exists")
        .expect("should show 'Snapshot exists'");
}

#[test]
fn settings_click_validate_emits_message() {
    let mut ui = simulator(modde_ui::views::settings::view(default_settings_state()));
    ui.click("Validate").expect("should click 'Validate'");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages
            .iter()
            .any(|m| matches!(m, Message::ValidateNexusKey)),
        "should emit ValidateNexusKey, got: {messages:?}",
    );
}

#[test]
fn settings_click_create_snapshot_emits_message() {
    let mut ui = simulator(modde_ui::views::settings::view(default_settings_state()));
    ui.click("Create Snapshot")
        .expect("should click 'Create Snapshot'");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages
            .iter()
            .any(|m| matches!(m, Message::CreateStockSnapshot)),
        "should emit CreateStockSnapshot, got: {messages:?}",
    );
}

#[test]
fn settings_click_verify_snapshot_emits_message() {
    let mut ui = simulator(modde_ui::views::settings::view(default_settings_state()));
    ui.click("Verify Snapshot")
        .expect("should click 'Verify Snapshot'");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages
            .iter()
            .any(|m| matches!(m, Message::VerifyStockSnapshot)),
        "should emit VerifyStockSnapshot, got: {messages:?}",
    );
}

#[test]
fn wabbajack_shows_generated_hm_snippet_preview() {
    let state = WabbajackInstallerState {
        hm_snippet: "programs.modde.profiles.lotf = {\n  game = \"skyrim-se\";\n};\n".to_string(),
        ..Default::default()
    };
    let manifest = None;
    let available_games = vec![("skyrim-se".to_string(), "Skyrim SE".to_string())];
    let mut ui = simulator(modde_ui::views::wabbajack::view(
        &state,
        &manifest,
        &available_games,
        Some("skyrim-se"),
    ));

    ui.find("programs.modde.profiles.lotf = {\n  game = \"skyrim-se\";\n};\n")
        .expect("should show generated snippet preview");
}

// ─── Mod List View ────────────────────────────────────────────

/// Helper macro that declares filter state bindings at the caller's scope
/// and creates a `mut $ui` simulator. Use as:
///   `mod_list_simulator!(mods_expr => ui);`
macro_rules! mod_list_simulator {
    ($mods:expr => $ui:ident) => {
        let __collapsed = std::collections::HashSet::new();
        let __categories: Vec<(Option<i64>, String)> = vec![];
        let __criteria: Vec<modde_core::filter::FilterCriterion> = vec![];
        let __filter_keys: Vec<String> = vec![];
        let mut $ui = simulator(modde_ui::views::mod_list::view_filtered(
            $mods,
            &__filter_keys,
            "",
            None,
            modde_core::filter::FilterMode::default(),
            &__criteria,
            &__collapsed,
            &__categories,
            false,
            false,
        ));
    };
}

fn sample_mods() -> Vec<modde_core::profile::EnabledMod> {
    vec![
        modde_core::profile::EnabledMod {
            mod_id: "SkyUI".to_string(),
            enabled: true,
            version: Some("5.2".to_string()),
            fomod_config: None,
            ..Default::default()
        },
        modde_core::profile::EnabledMod {
            mod_id: "USSEP".to_string(),
            enabled: true,
            version: Some("4.2.8".to_string()),
            fomod_config: None,
            ..Default::default()
        },
        modde_core::profile::EnabledMod {
            mod_id: "EnhancedLights".to_string(),
            enabled: false,
            version: None,
            fomod_config: None,
            ..Default::default()
        },
    ]
}

#[test]
fn mod_list_empty_shows_placeholder() {
    let mods: Vec<modde_core::profile::EnabledMod> = vec![];
    mod_list_simulator!(&mods => ui);
    ui.find("No mods found. Click 'Add Mod' to get started.")
        .expect("should show empty placeholder");
}

#[test]
fn mod_list_shows_toolbar_buttons() {
    let mods = sample_mods();
    mod_list_simulator!(&mods => ui);
    ui.find("Add Mod").expect("should show 'Add Mod' button");
    ui.find("Remove").expect("should show 'Remove' button");
    ui.find("Deploy").expect("should show 'Deploy' button");
}

#[test]
fn mod_list_shows_mod_names() {
    let mods = sample_mods();
    mod_list_simulator!(&mods => ui);
    ui.find("SkyUI").expect("should show SkyUI");
    ui.find("USSEP").expect("should show USSEP");
    ui.find("EnhancedLights")
        .expect("should show EnhancedLights");
}

#[test]
fn mod_list_shows_mod_count() {
    let mods = sample_mods();
    mod_list_simulator!(&mods => ui);
    ui.find("3 mod(s) shown").expect("should show mod count");
}

#[test]
fn mod_list_click_add_mod_emits_message() {
    let mods = sample_mods();
    mod_list_simulator!(&mods => ui);
    ui.click("Add Mod").expect("should click 'Add Mod'");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages.iter().any(|m| matches!(m, Message::AddMod)),
        "should emit AddMod, got: {messages:?}",
    );
}

#[test]
fn mod_list_click_deploy_emits_message() {
    let mods = sample_mods();
    mod_list_simulator!(&mods => ui);
    ui.click("Deploy").expect("should click 'Deploy'");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages.iter().any(|m| matches!(m, Message::Deploy)),
        "should emit Deploy, got: {messages:?}",
    );
}

#[test]
fn mod_list_click_mod_name_emits_select() {
    let mods = sample_mods();
    mod_list_simulator!(&mods => ui);
    ui.click("USSEP").expect("should click 'USSEP'");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages.iter().any(|m| matches!(m, Message::SelectMod(1))),
        "should emit SelectMod(1), got: {messages:?}",
    );
}

// ─── Sidebar View ─────────────────────────────────────────────

macro_rules! sidebar_test {
    ($name:ident, profiles = $profiles:expr, active = $active:expr, depth = $depth:expr, |$ui:ident| $body:expr) => {
        #[test]
        fn $name() {
            let view = modde_ui::app::View::ModList;
            let collapsed_groups = std::collections::HashSet::new();
            let profiles = $profiles;
            let active = $active;
            let mut $ui = simulator(modde_ui::views::sidebar::view(
                &view,
                &collapsed_groups,
                &profiles,
                &active,
                $depth,
                true,
                None,
                None,
            ));
            $body
        }
    };
}

sidebar_test!(
    sidebar_shows_nav_items,
    profiles = vec![],
    active = None,
    depth = 0,
    |ui| {
        ui.find("Game").expect("group: Game");
        ui.find("Install").expect("group: Install");
        ui.find("General").expect("group: General");
        ui.find("Mod List").expect("nav: Mod List");
        ui.find("Saves").expect("nav: Saves");
        ui.find("Browse Nexus").expect("nav: Browse Nexus");
        ui.find("Collections").expect("nav: Collections");
        ui.find("Wabbajack").expect("nav: Wabbajack");
        ui.find("Downloads").expect("nav: Downloads");
        ui.find("Data Files").expect("nav: Data Files");
        ui.find("Diagnostics").expect("nav: Diagnostics");
        ui.find("Tools").expect("nav: Tools");
        ui.find("Executables").expect("nav: Executables");
        ui.find("Settings").expect("nav: Settings");
        assert!(ui.find("Maintenance").is_err(), "Maintenance group is gone");
        assert!(
            ui.find("Verify").is_err(),
            "Verify nav item is unified into Diagnostics"
        );
    }
);

#[test]
fn sidebar_default_collapsed_groups_hide_inactive_items() {
    let view = modde_ui::app::View::ModList;
    let collapsed_groups = std::collections::HashSet::from([modde_ui::app::SidebarGroup::General]);
    let profiles = Vec::new();
    let active = None;
    let mut ui = simulator(modde_ui::views::sidebar::view(
        &view,
        &collapsed_groups,
        &profiles,
        &active,
        0,
        true,
        None,
        None,
    ));

    ui.find("Mod List")
        .expect("active game item remains visible");
    ui.find("Diagnostics")
        .expect("Diagnostics remains visible in the Game group");
    assert!(
        ui.find("Settings").is_err(),
        "collapsed inactive General item should be hidden"
    );
}

#[test]
fn sidebar_collapsed_active_group_still_shows_active_view() {
    let view = modde_ui::app::View::Settings;
    let collapsed_groups = std::collections::HashSet::from([modde_ui::app::SidebarGroup::General]);
    let profiles = Vec::new();
    let active = None;
    let mut ui = simulator(modde_ui::views::sidebar::view(
        &view,
        &collapsed_groups,
        &profiles,
        &active,
        0,
        true,
        None,
        None,
    ));

    ui.find("Settings")
        .expect("active collapsed item remains visible");
}

#[test]
fn sidebar_group_toggle_emits_message() {
    let view = modde_ui::app::View::ModList;
    let collapsed_groups = std::collections::HashSet::new();
    let profiles = Vec::new();
    let active = None;
    let mut ui = simulator(modde_ui::views::sidebar::view(
        &view,
        &collapsed_groups,
        &profiles,
        &active,
        0,
        true,
        None,
        None,
    ));

    ui.click("Game").expect("should click Game group header");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages.iter().any(|m| matches!(
            m,
            Message::ToggleSidebarGroup(modde_ui::app::SidebarGroup::Game)
        )),
        "should emit ToggleSidebarGroup(Game), got: {messages:?}",
    );
}

#[test]
fn sidebar_hides_inactive_saves_when_save_profiles_are_unsupported() {
    let view = modde_ui::app::View::ModList;
    let collapsed_groups = std::collections::HashSet::new();
    let profiles = Vec::new();
    let active = None;
    let mut ui = simulator(modde_ui::views::sidebar::view(
        &view,
        &collapsed_groups,
        &profiles,
        &active,
        0,
        false,
        None,
        None,
    ));

    assert!(
        ui.find("Saves").is_err(),
        "inactive Saves item should be hidden for games without save profiles"
    );
}

#[test]
fn browse_nexus_tabs_keep_existing_messages() {
    let state = modde_ui::views::browse_nexus::NexusBrowseState {
        selected_game_id: Some("skyrim-se".to_string()),
        ..Default::default()
    };
    let games = vec![
        ("skyrim-se".to_string(), "Skyrim SE".to_string()),
        ("fallout4".to_string(), "Fallout 4".to_string()),
    ];
    let mut ui = simulator(modde_ui::views::browse_nexus::view(
        &state,
        &games,
        Some("skyrimspecialedition".to_string()),
    ));

    ui.click("Month").expect("should click Month tab");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages.iter().any(|m| matches!(
            m,
            Message::BrowseTabSwitched(modde_ui::views::browse_nexus::BrowseTab::Month)
        )),
        "should emit BrowseTabSwitched(Month), got: {messages:?}",
    );
}

#[test]
fn browse_nexus_with_supported_game_renders_browse_surface() {
    let state = modde_ui::views::browse_nexus::NexusBrowseState {
        selected_game_id: Some("fallout4".to_string()),
        ..Default::default()
    };
    let games = vec![
        ("skyrim-se".to_string(), "Skyrim SE".to_string()),
        ("fallout4".to_string(), "Fallout 4".to_string()),
        ("stellar-blade".to_string(), "Stellar Blade".to_string()),
    ];
    let mut ui = simulator(modde_ui::views::browse_nexus::view(
        &state,
        &games,
        Some("fallout4".to_string()),
    ));

    ui.find("Browse Nexus").expect("should show title");
    ui.find("Search Nexus mods & collections…")
        .expect("should show search input");
}

#[test]
fn wabbajack_tabs_keep_existing_messages() {
    let state = WabbajackInstallerState::default();
    let manifest = None;
    let available_games = Vec::new();
    let mut ui = simulator(modde_ui::views::wabbajack::view(
        &state,
        &manifest,
        &available_games,
        Some("skyrim-se"),
    ));

    ui.click("Manual").expect("should click Manual tab");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages.iter().any(|m| matches!(
            m,
            Message::WabbajackTabChanged(modde_ui::app::WabbajackTab::Manual)
        )),
        "should emit WabbajackTabChanged(Manual), got: {messages:?}",
    );
}

sidebar_test!(
    sidebar_click_executables_emits_switch_view,
    profiles = vec![],
    active = None,
    depth = 0,
    |ui| {
        ui.click("Executables").expect("should click 'Executables'");
        let messages: Vec<_> = ui.into_messages().collect();
        assert!(
            messages
                .iter()
                .any(|m| matches!(m, Message::SwitchView(modde_ui::app::View::Executables))),
            "should emit SwitchView(Executables), got: {messages:?}",
        );
    }
);

sidebar_test!(
    sidebar_click_settings_emits_switch_view,
    profiles = vec![],
    active = None,
    depth = 0,
    |ui| {
        ui.click("Settings").expect("should click 'Settings'");
        let messages: Vec<_> = ui.into_messages().collect();
        assert!(
            messages
                .iter()
                .any(|m| matches!(m, Message::SwitchView(modde_ui::app::View::Settings))),
            "should emit SwitchView(Settings), got: {messages:?}",
        );
    }
);

sidebar_test!(
    sidebar_click_downloads_emits_switch_view,
    profiles = vec![],
    active = None,
    depth = 0,
    |ui| {
        ui.click("Downloads").expect("should click 'Downloads'");
        let messages: Vec<_> = ui.into_messages().collect();
        assert!(
            messages
                .iter()
                .any(|m| matches!(m, Message::SwitchView(modde_ui::app::View::Downloads))),
            "should emit SwitchView(Downloads), got: {messages:?}",
        );
    }
);

sidebar_test!(
    sidebar_shows_new_profile_button,
    profiles = vec![],
    active = None,
    depth = 0,
    |ui| {
        ui.find("New").expect("should show 'New' button");
    }
);

fn test_profile_summary() -> modde_core::profile::ProfileSummary {
    modde_core::profile::ProfileSummary {
        id: 1,
        name: "test".to_string(),
        game_id: "skyrim-se".into(),
        mod_count: 0,
        source_type: "Manual".to_string(),
    }
}

sidebar_test!(
    sidebar_shows_try_profile_when_profile_active,
    profiles = vec![test_profile_summary()],
    active = Some("test".to_string()),
    depth = 0,
    |ui| {
        ui.find("Try Profile")
            .expect("should show 'Try Profile' when a profile is active");
    }
);

sidebar_test!(
    sidebar_experiment_mode_shows_rollback_commit,
    profiles = vec![test_profile_summary()],
    active = Some("test".to_string()),
    depth = 2,
    |ui| {
        ui.find("Rollback").expect("should show 'Rollback'");
        ui.find("Commit").expect("should show 'Commit'");
    }
);

// ─── Downloads / Data / Diagnostics / Tools Views ──────────────────

#[test]
fn downloads_empty_shows_connected_empty_state() {
    let mut ui = simulator(modde_ui::views::downloads::view(&[]));
    ui.find("Downloads").expect("should show title");
    ui.find("No downloads")
        .expect("should show empty downloads state");
}

#[test]
fn downloads_active_task_emits_pause_and_cancel_messages() {
    let tasks = vec![DownloadTask {
        id: 7,
        name: "SkyUI".to_string(),
        state: DownloadState::Active {
            bytes_downloaded: 512 * 1024,
            total_bytes: Some(1024 * 1024),
        },
    }];

    let mut pause_ui = simulator(modde_ui::views::downloads::view(&tasks));
    pause_ui.find("SkyUI").expect("should show download name");
    pause_ui
        .find("50% (512.0 KB downloaded)")
        .expect("should show progress");
    pause_ui.click("Pause").expect("should click Pause");
    let pause_messages: Vec<_> = pause_ui.into_messages().collect();
    assert!(
        pause_messages
            .iter()
            .any(|m| matches!(m, Message::PauseDownload(7))),
        "should emit PauseDownload(7), got: {pause_messages:?}",
    );

    let mut cancel_ui = simulator(modde_ui::views::downloads::view(&tasks));
    cancel_ui.click("Cancel").expect("should click Cancel");
    let cancel_messages: Vec<_> = cancel_ui.into_messages().collect();
    assert!(
        cancel_messages
            .iter()
            .any(|m| matches!(m, Message::CancelDownload(7))),
        "should emit CancelDownload(7), got: {cancel_messages:?}",
    );
}

#[test]
fn data_tab_shows_conflict_rows_and_count() {
    let state = DataTabState::default();
    let conflicts = vec![(
        "meshes/dragon.nif".to_string(),
        vec!["SkyUI".to_string(), "USSEP".to_string()],
    )];

    let mut ui = simulator(modde_ui::views::data_tab::view(&state, &conflicts));
    ui.find("Data Files").expect("should show title");
    ui.find("meshes/dragon.nif")
        .expect("should show conflict file");
    ui.find("SkyUI, USSEP")
        .expect("should show conflict providers");
    ui.find("1 file(s) shown").expect("should show file count");
}

#[test]
fn data_tab_reports_missing_store_mods_when_empty() {
    let state = DataTabState {
        missing_store_mod_count: 452,
        ..Default::default()
    };
    let conflicts = Vec::new();

    let mut ui = simulator(modde_ui::views::data_tab::view(&state, &conflicts));
    ui.find("452 enabled mod(s) are missing from the store; conflict data is incomplete.")
        .expect("should show incomplete conflict data message");
}

#[test]
fn diagnostics_idle_click_run_emits_message() {
    let mut ui = simulator(modde_ui::views::diagnostics::view(&DiagnosticsState::Idle));
    ui.find("Diagnostics").expect("should show title");
    ui.click("Run Diagnostics")
        .expect("should click Run Diagnostics");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages
            .iter()
            .any(|m| matches!(m, Message::RunDiagnostics)),
        "should emit RunDiagnostics, got: {messages:?}",
    );
}

#[test]
fn diagnostics_complete_shows_summary_and_findings() {
    let state = DiagnosticsState::Complete(DiagnosticsReport {
        profile_name: "test-profile".to_string(),
        game_id: "skyrim-se".to_string(),
        entries: vec![
            DiagnosticEntry {
                severity: DiagnosticSeverity::Error,
                message: "Missing master: Update.esm".to_string(),
            },
            DiagnosticEntry {
                severity: DiagnosticSeverity::Warning,
                message: "Loose file conflict detected".to_string(),
            },
        ],
        integrity: IntegritySummary {
            ok_count: 42,
            broken_symlinks: vec![PathBuf::from("/game/broken_link.esp")],
        },
    });

    let mut ui = simulator(modde_ui::views::diagnostics::view(&state));
    ui.find("test-profile (skyrim-se) - 1 error(s), 1 warning(s), 0 info(s), 1 broken symlink(s)")
        .expect("should show diagnostics summary");
    ui.find("Integrity: 42 staged file(s) OK, 1 broken symlink(s)")
        .expect("should show integrity summary");
    ui.find("/game/broken_link.esp")
        .expect("should show broken symlink path");
    ui.find("Missing master: Update.esm")
        .expect("should show error finding");
    ui.find("Loose file conflict detected")
        .expect("should show warning finding");
}

#[test]
fn tools_view_shows_entries_and_actions() {
    let state = sample_tools_state("reshade");

    let mut refresh_ui = simulator(modde_ui::views::tools::view(&state));
    refresh_ui
        .find("Gaming Tools - Skyrim SE")
        .expect("should show title");
    refresh_ui.find("ReShade").expect("should show tool name");
    refresh_ui
        .find("Source directory")
        .expect("should show setting label");
    refresh_ui
        .find("DLL overrides: dxgi")
        .expect("should show launch preview");
    refresh_ui
        .find("2 file(s) applied to game directory")
        .expect("should show applied files");
    refresh_ui
        .find("Applied successfully")
        .expect("should show tool status");
    assert!(
        refresh_ui.find("Executables").is_err(),
        "executables should not render inside Tools view",
    );
    refresh_ui.click("Refresh").expect("should click Refresh");
    let refresh_messages: Vec<_> = refresh_ui.into_messages().collect();
    assert!(
        refresh_messages
            .iter()
            .any(|m| matches!(m, Message::RefreshTools)),
        "should emit RefreshTools, got: {refresh_messages:?}",
    );

    let mut apply_ui = simulator(modde_ui::views::tools::view(&state));
    apply_ui.click("Apply").expect("should click Apply");
    let apply_messages: Vec<_> = apply_ui.into_messages().collect();
    assert!(
        apply_messages
            .iter()
            .any(|m| matches!(m, Message::ApplyTool(tool_id) if tool_id == "reshade")),
        "should emit ApplyTool(reshade), got: {apply_messages:?}",
    );

    let mut revert_ui = simulator(modde_ui::views::tools::view(&state));
    revert_ui.click("Revert").expect("should click Revert");
    let revert_messages: Vec<_> = revert_ui.into_messages().collect();
    assert!(
        revert_messages
            .iter()
            .any(|m| matches!(m, Message::RevertTool(tool_id) if tool_id == "reshade")),
        "should emit RevertTool(reshade), got: {revert_messages:?}",
    );
}

#[test]
fn tools_view_shows_loading_state_with_existing_entries() {
    let mut state = sample_tools_state("reshade");
    state.loading = true;

    let mut ui = simulator(modde_ui::views::tools::view(&state));

    ui.find("Loading tools...")
        .expect("loading status should render");
    ui.find("ReShade")
        .expect("existing tool entries should remain visible while loading");
}

#[test]
fn tools_actions_disable_while_loading() {
    let mut state = sample_tools_state("reshade");
    state.loading = true;

    let mut refresh_ui = simulator(modde_ui::views::tools::view(&state));
    refresh_ui
        .click("Refresh")
        .expect("refresh button remains visible");
    let messages: Vec<_> = refresh_ui.into_messages().collect();
    assert!(
        !messages.iter().any(|m| matches!(m, Message::RefreshTools)),
        "loading refresh should not emit RefreshTools, got: {messages:?}",
    );

    let mut apply_ui = simulator(modde_ui::views::tools::view(&state));
    apply_ui
        .click("Apply")
        .expect("loading apply button remains visible");
    let messages: Vec<_> = apply_ui.into_messages().collect();
    assert!(
        !messages
            .iter()
            .any(|m| matches!(m, Message::ApplyTool(tool_id) if tool_id == "reshade")),
        "loading apply should not emit ApplyTool, got: {messages:?}",
    );

    let mut revert_ui = simulator(modde_ui::views::tools::view(&state));
    revert_ui
        .click("Revert")
        .expect("revert button remains visible");
    let messages: Vec<_> = revert_ui.into_messages().collect();
    assert!(
        !messages
            .iter()
            .any(|m| matches!(m, Message::RevertTool(tool_id) if tool_id == "reshade")),
        "loading revert should not emit RevertTool, got: {messages:?}",
    );
}

#[test]
fn tools_view_shows_registered_tool_tabs_including_proton() {
    let state = ToolState {
        active_tool_id: Some("mangohud".to_string()),
        game_label: Some("Skyrim SE".to_string()),
        game_dir_configured: true,
        loading: false,
        load_error: None,
        load_generation: 0,
        show_advanced_settings: false,
        active_operations: std::collections::HashSet::new(),
        tool_option_catalog: std::collections::HashMap::from([
            (
                "optiscaler.release_tag".to_string(),
                vec!["v1.0.0".to_string()],
            ),
            (
                "optiscaler.release_asset".to_string(),
                vec!["OptiScaler.zip".to_string()],
            ),
            (
                "proton.selected_version".to_string(),
                vec!["latest".to_string()],
            ),
        ]),
        optiscaler_releases: Vec::new(),
        optiscaler_releases_loading: false,
        proton_versions_loading: false,
        executables: Vec::new(),
        executables_loading: false,
        executables_load_error: None,
        executables_load_generation: 0,
        executable_draft: Default::default(),
        executable_editor_open: false,
        executable_error: None,
        active_executable_operations: std::collections::HashSet::new(),
        entries: vec![
            sample_tool_entry("mangohud", "MangoHud"),
            sample_tool_entry("vkbasalt", "vkBasalt"),
            sample_tool_entry("gamemode", "GameMode"),
            sample_tool_entry("reshade", "ReShade"),
            sample_tool_entry("optiscaler", "OptiScaler"),
            sample_tool_entry("proton", "Proton"),
        ],
    };

    let mut ui = simulator(modde_ui::views::tools::view(&state));
    ui.find("MangoHud").expect("MangoHud tab");
    ui.find("vkBasalt").expect("vkBasalt tab");
    ui.find("GameMode").expect("GameMode tab");
    ui.find("ReShade").expect("ReShade tab");
    ui.find("OptiScaler").expect("OptiScaler tab");
    ui.find("Proton").expect("Proton tab");

    ui.click("Proton").expect("should click Proton tab");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages
            .iter()
            .any(|m| matches!(m, Message::SelectToolTab(tool_id) if tool_id == "proton")),
        "should emit SelectToolTab(proton), got: {messages:?}",
    );
}

#[test]
fn tools_optiscaler_scrollable_has_stable_test_id() {
    let mut state = sample_tools_state("optiscaler");
    state.game_dir_configured = true;
    state.entries = vec![ToolUiEntry {
        has_file_patching: true,
        optiscaler_state: Some("partially managed".to_string()),
        optiscaler_latest_backup: Some("/tmp/backup".to_string()),
        optiscaler_detected_files: 2,
        ..sample_tool_entry("optiscaler", "OptiScaler")
    }];

    let mut ui = simulator(modde_ui::views::tools::view(&state));

    ui.find(by_test_id("tools.optiscaler.scroll"))
        .expect("OptiScaler scrollable should have a stable test id");
    ui.find(by_test_id("tools.optiscaler.adopt"))
        .expect("Adopt button should have a stable test id");
    ui.find(by_test_id("tools.optiscaler.restore_backup"))
        .expect("Restore backup button should have a stable test id");
    ui.find(by_test_id("tools.optiscaler.reset_config"))
        .expect("Reset config button should have a stable test id");
}

#[test]
fn executables_view_renders_configured_executables() {
    let mut state = sample_tools_state("reshade");
    state.executables = vec![ExecutableUiEntry {
        name: "xEdit".to_string(),
        executable_path: "/tools/SSEEdit.exe".to_string(),
        arguments: "-IKnowWhatImDoing".to_string(),
        working_dir: "/games/skyrim".to_string(),
        environment: "WINESYNC=1".to_string(),
        wine_dll_overrides: "dinput8=n,b".to_string(),
        output_mod: "xedit-output".to_string(),
        enabled: true,
    }];

    let mut ui = simulator(modde_ui::views::executables::view(&state));
    ui.find("Executables - Skyrim SE")
        .expect("executables title");
    ui.find("Executables").expect("executables section");
    ui.find("xEdit").expect("saved executable name");
    ui.find("/tools/SSEEdit.exe")
        .expect("saved executable path");
    ui.find(
        "args: -IKnowWhatImDoing | cwd: /games/skyrim | dll: dinput8=n,b | output: xedit-output",
    )
    .expect("saved executable metadata");

    ui.click("Run").expect("run button");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages
            .iter()
            .any(|m| matches!(m, Message::RunExecutable(name) if name == "xEdit")),
        "should emit RunExecutable(xEdit), got: {messages:?}",
    );
}

#[test]
fn executables_view_empty_form_emits_save() {
    let state = sample_tools_state("reshade");
    let mut ui = simulator(modde_ui::views::executables::view(&state));

    ui.find("No executables configured")
        .expect("empty executable state");
    ui.click("Save").expect("save button");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages
            .iter()
            .any(|m| matches!(m, Message::SaveExecutable)),
        "should emit SaveExecutable, got: {messages:?}",
    );
}

#[test]
fn executables_view_refresh_emits_message() {
    let state = sample_tools_state("reshade");
    let mut ui = simulator(modde_ui::views::executables::view(&state));

    ui.click("Refresh").expect("refresh button");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages
            .iter()
            .any(|m| matches!(m, Message::RefreshExecutables)),
        "should emit RefreshExecutables, got: {messages:?}",
    );
}

#[test]
fn tools_optiscaler_release_buttons_emit_messages() {
    let mut state = sample_tools_state("optiscaler");
    state.entries = vec![sample_tool_entry("optiscaler", "OptiScaler")];
    state.tool_option_catalog.insert(
        "optiscaler.release_tag".to_string(),
        vec!["v0.7.7".to_string()],
    );
    state.tool_option_catalog.insert(
        "optiscaler.release_asset".to_string(),
        vec!["OptiScaler.zip".to_string()],
    );

    let mut refresh_ui = simulator(modde_ui::views::tools::view(&state));
    refresh_ui
        .click("Refresh releases")
        .expect("refresh releases button");
    let messages: Vec<_> = refresh_ui.into_messages().collect();
    assert!(
        messages
            .iter()
            .any(|m| matches!(m, Message::RefreshOptiScalerReleases)),
        "should emit RefreshOptiScalerReleases, got: {messages:?}",
    );

    let mut install_ui = simulator(modde_ui::views::tools::view(&state));
    install_ui
        .click("Install selected release")
        .expect("install release button");
    let messages: Vec<_> = install_ui.into_messages().collect();
    assert!(
        messages
            .iter()
            .any(|m| matches!(m, Message::InstallOptiScalerRelease)),
        "should emit InstallOptiScalerRelease, got: {messages:?}",
    );
}

#[test]
fn tools_optiscaler_release_buttons_disable_while_unready() {
    let mut state = sample_tools_state("optiscaler");
    state.entries = vec![sample_tool_entry("optiscaler", "OptiScaler")];
    state.optiscaler_releases_loading = true;
    state.tool_option_catalog.insert(
        "optiscaler.release_asset".to_string(),
        vec!["OptiScaler.zip".to_string()],
    );

    let mut refresh_ui = simulator(modde_ui::views::tools::view(&state));
    refresh_ui
        .click("Loading releases")
        .expect("loading releases button remains visible");
    let messages: Vec<_> = refresh_ui.into_messages().collect();
    assert!(
        !messages
            .iter()
            .any(|m| matches!(m, Message::RefreshOptiScalerReleases)),
        "loading releases button should not emit refresh, got: {messages:?}",
    );

    let mut install_ui = simulator(modde_ui::views::tools::view(&state));
    install_ui
        .click("Install selected release")
        .expect("install release button remains visible");
    let messages: Vec<_> = install_ui.into_messages().collect();
    assert!(
        !messages
            .iter()
            .any(|m| matches!(m, Message::InstallOptiScalerRelease)),
        "install button should not emit while releases are loading, got: {messages:?}",
    );
}

#[test]
fn tools_optiscaler_install_release_disables_without_assets() {
    let mut state = sample_tools_state("optiscaler");
    state.entries = vec![sample_tool_entry("optiscaler", "OptiScaler")];
    state
        .tool_option_catalog
        .insert("optiscaler.release_asset".to_string(), Vec::new());

    let mut ui = simulator(modde_ui::views::tools::view(&state));
    ui.click("Install selected release")
        .expect("install release button remains visible");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        !messages
            .iter()
            .any(|m| matches!(m, Message::InstallOptiScalerRelease)),
        "install button should not emit without assets, got: {messages:?}",
    );
}

#[test]
fn tools_apply_and_revert_buttons_disable_while_busy() {
    let mut state = sample_tools_state("reshade");
    state.active_operations.insert("reshade".to_string());

    let mut apply_ui = simulator(modde_ui::views::tools::view(&state));
    apply_ui
        .click("Applying")
        .expect("busy apply button remains visible");
    let messages: Vec<_> = apply_ui.into_messages().collect();
    assert!(
        !messages
            .iter()
            .any(|m| matches!(m, Message::ApplyTool(tool_id) if tool_id == "reshade")),
        "busy apply should not emit ApplyTool, got: {messages:?}",
    );

    let mut revert_ui = simulator(modde_ui::views::tools::view(&state));
    revert_ui
        .click("Revert")
        .expect("busy revert button remains visible");
    let messages: Vec<_> = revert_ui.into_messages().collect();
    assert!(
        !messages
            .iter()
            .any(|m| matches!(m, Message::RevertTool(tool_id) if tool_id == "reshade")),
        "busy revert should not emit RevertTool, got: {messages:?}",
    );
}

#[test]
fn tools_apply_button_disables_when_current_settings_are_applied() {
    let mut state = sample_tools_state("reshade");
    state.entries[0].apply_pending = false;

    let mut ui = simulator(modde_ui::views::tools::view(&state));
    ui.click("No changes")
        .expect("already-applied apply button remains visible");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        !messages
            .iter()
            .any(|m| matches!(m, Message::ApplyTool(tool_id) if tool_id == "reshade")),
        "already-applied settings should not emit ApplyTool, got: {messages:?}",
    );
}

#[test]
fn tools_apply_button_disables_when_preview_has_missing_inputs() {
    let mut state = sample_tools_state("reshade");
    state.entries[0].apply_pending = false;
    state.entries[0]
        .apply_missing_inputs
        .push("source DLL missing".to_string());

    let mut ui = simulator(modde_ui::views::tools::view(&state));
    ui.click("Apply")
        .expect("missing-input apply button remains visible");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        !messages
            .iter()
            .any(|m| matches!(m, Message::ApplyTool(tool_id) if tool_id == "reshade")),
        "missing inputs should not emit ApplyTool, got: {messages:?}",
    );
}

#[test]
fn tools_optiscaler_release_asset_selector_is_rendered() {
    let mut state = sample_tools_state("optiscaler");
    state.entries = vec![sample_tool_entry("optiscaler", "OptiScaler")];
    state.tool_option_catalog.insert(
        "optiscaler.release_tag".to_string(),
        vec!["v0.9.1".to_string()],
    );
    state.tool_option_catalog.insert(
        "optiscaler.release_asset".to_string(),
        vec!["Optiscaler_0.9.1-final.7z".to_string()],
    );
    state.entries[0].settings = serde_json::json!({
        "source_mode": "github_release",
        "release_tag": "v0.9.1",
        "release_asset": "Optiscaler_0.9.1-final.7z",
    });

    let mut ui = simulator(modde_ui::views::tools::view(&state));
    ui.find("OptiScaler Release")
        .expect("release panel title should render");
    ui.find("Source")
        .expect("source selector label should render");
    ui.find("Release tag")
        .expect("release tag selector label should render");
    ui.find("Release asset")
        .expect("release asset selector label should render");
    ui.find("Refresh releases")
        .expect("refresh release action should render in panel");
    ui.find("Install selected release")
        .expect("install release action should render in panel");
}

#[test]
fn tools_optiscaler_local_source_renders_in_release_panel() {
    let mut state = sample_tools_state("optiscaler");
    state.entries = vec![sample_tool_entry("optiscaler", "OptiScaler")];
    state.entries[0].setting_specs.push(
        modde_games::tools::ToolSettingSpec::path(
            "local_source_dir",
            "Local source directory",
            "Directory containing OptiScaler.dll and companion files.",
        )
        .section("Source"),
    );
    state.entries[0].settings = serde_json::json!({
        "source_mode": "local_dir",
        "local_source_dir": "/tmp/optiscaler",
        "release_tag": "v0.9.1",
        "release_asset": "Optiscaler_0.9.1-final.7z",
    });

    let mut ui = simulator(modde_ui::views::tools::view(&state));
    ui.find("OptiScaler Release")
        .expect("release panel title should render");
    ui.find("Source")
        .expect("source selector label should render");
    ui.find("Local source directory")
        .expect("local source control should render in release panel");
    assert!(
        ui.find("Release tag").is_err(),
        "release tag should not render in local directory mode",
    );
    assert!(
        ui.find("Release asset").is_err(),
        "release asset should not render in local directory mode",
    );
}

#[test]
fn tools_optiscaler_advanced_settings_are_hidden_by_default() {
    let mut state = sample_tools_state("optiscaler");
    state.entries = vec![sample_tool_entry("optiscaler", "OptiScaler")];
    state.entries[0].setting_specs.push(
        modde_games::tools::ToolSettingSpec::tri_state_bool(
            "ini_overrides.FSR.Fsr4Update",
            "FSR4 update",
            "FSR4 update.",
        )
        .section("Advanced")
        .advanced(),
    );

    let mut ui = simulator(modde_ui::views::tools::view(&state));
    ui.find("Show advanced")
        .expect("advanced toggle should render");
    assert!(
        ui.find("FSR4 update").is_err(),
        "advanced OptiScaler setting should be hidden by default",
    );

    ui.click("Show advanced").expect("advanced toggle");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages
            .iter()
            .any(|m| matches!(m, Message::ToggleToolAdvancedSettings)),
        "advanced toggle should emit ToggleToolAdvancedSettings, got: {messages:?}",
    );
}

#[test]
fn tools_optiscaler_basic_settings_are_visible_by_default() {
    let mut state = sample_tools_state("optiscaler");
    state.entries = vec![sample_tool_entry("optiscaler", "OptiScaler")];
    state.entries[0].setting_specs.extend([
        modde_games::tools::ToolSettingSpec::tri_state_bool(
            "ini_overrides.Spoofing.Dxgi",
            "Spoof DLSS / DXGI",
            "OptiScaler Dxgi override.",
        )
        .section("Basic"),
        modde_games::tools::ToolSettingSpec::labeled_select(
            "ini_overrides.FSR.UpscalerIndex",
            "FSR upscaler backend",
            "OptiScaler [FSR] UpscalerIndex override.",
            &[
                ("auto", "Auto"),
                ("0", "0 - FSR 4.0.2"),
                ("1", "1 - FSR 3.1.5"),
                ("2", "2 - FSR 2.3.4"),
            ],
        )
        .section("Basic"),
    ]);

    let mut ui = simulator(modde_ui::views::tools::view(&state));
    ui.find("Basic").expect("basic section should render");
    ui.find("Spoof DLSS / DXGI")
        .expect("basic DXGI setting should render");
    ui.find("FSR upscaler backend")
        .expect("basic FSR selector should render");
}

#[test]
fn tools_optiscaler_typed_controls_emit_typed_updates() {
    let mut state = sample_tools_state("optiscaler");
    state.entries = vec![sample_tool_entry("optiscaler", "OptiScaler")];
    state.entries[0].settings = serde_json::json!({
        "copy_companion_files": false,
        "ini_overrides": {
            "FSR": {
                "Fsr4Update": "auto"
            }
        }
    });
    state.entries[0].setting_specs = vec![
        modde_games::tools::ToolSettingSpec::bool(
            "copy_companion_files",
            "Copy companion files",
            "Copy companion files.",
        )
        .section("Deployment"),
        modde_games::tools::ToolSettingSpec::tri_state_bool(
            "ini_overrides.FSR.Fsr4Update",
            "FSR4 update",
            "FSR4 update.",
        )
        .section("OptiScaler Upscaling"),
    ];
    state.show_advanced_settings = true;

    let mut rendered_ui = simulator(modde_ui::views::tools::view(&state));
    rendered_ui
        .find("Copy companion files")
        .expect("boolean setting label should render");

    let mut tri_state_ui = simulator(modde_ui::views::tools::view(&state));
    tri_state_ui.click("On").expect("tri-state on option");
    let messages: Vec<_> = tri_state_ui.into_messages().collect();
    assert!(
        messages.iter().any(|m| matches!(
            m,
            Message::UpdateToolSetting { tool_id, key, value }
                if tool_id == "optiscaler"
                    && key == "ini_overrides.FSR.Fsr4Update"
                    && value == &serde_json::json!(true)
        )),
        "should emit typed tri-state update, got: {messages:?}",
    );
}

#[test]
fn tools_proton_install_button_emits_message() {
    let mut state = sample_tools_state("proton");
    state.entries = vec![sample_tool_entry("proton", "Proton")];
    state.tool_option_catalog.insert(
        "proton.selected_version".to_string(),
        vec!["latest".to_string(), "GE-Proton10-1".to_string()],
    );

    let mut ui = simulator(modde_ui::views::tools::view(&state));
    ui.click("Install with protonup-rs")
        .expect("install proton button");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages
            .iter()
            .any(|m| matches!(m, Message::InstallProtonVersion)),
        "should emit InstallProtonVersion, got: {messages:?}",
    );
}

#[test]
fn tools_header_separates_availability_from_enabled_state() {
    let mut state = sample_tools_state("gamemode");
    state.entries = vec![sample_tool_entry("gamemode", "GameMode")];
    state.entries[0].available = true;
    state.entries[0].availability_text = "available".to_string();
    state.entries[0].enabled = false;

    let mut ui = simulator(modde_ui::views::tools::view(&state));
    ui.find("available")
        .expect("system availability status should render");
    ui.find("Enabled")
        .expect("per-game enabled control should be explicitly labelled");

    let enabled_label = ui
        .find("Enabled")
        .expect("per-game enabled control should be explicitly labelled");
    let label_bounds = enabled_label.bounds();
    ui.point_at(Point::new(
        label_bounds.x + label_bounds.width + 26.0,
        label_bounds.center_y(),
    ));
    let _ = ui.simulate(click());
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages.iter().any(|m| matches!(
            m,
            Message::ToggleTool { tool_id, enabled }
                if tool_id == "gamemode" && *enabled
        )),
        "should emit ToggleTool(gamemode, true), got: {messages:?}",
    );
}

#[test]
fn tools_header_disables_enabled_toggle_for_missing_tool() {
    let mut state = sample_tools_state("gamemode");
    state.entries = vec![sample_tool_entry("gamemode", "GameMode")];
    state.entries[0].available = false;
    state.entries[0].availability_text = "missing".to_string();
    state.entries[0].enabled = false;

    let mut ui = simulator(modde_ui::views::tools::view(&state));
    ui.find("missing")
        .expect("missing availability status should render");
    let enabled_label = ui
        .find("Enabled")
        .expect("disabled toggler label should still be visible");
    let label_bounds = enabled_label.bounds();
    ui.point_at(Point::new(
        label_bounds.x + label_bounds.width + 26.0,
        label_bounds.center_y(),
    ));
    let _ = ui.simulate(click());
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        !messages
            .iter()
            .any(|m| matches!(m, Message::ToggleTool { .. })),
        "missing tool should not emit ToggleTool, got: {messages:?}",
    );
}

#[test]
fn tools_show_derived_facts_without_exe_subdir_setting() {
    let state = sample_tools_state("reshade");
    let mut ui = simulator(modde_ui::views::tools::view(&state));
    ui.find("Detected Game").expect("derived fact heading");
    ui.find("Executable directory")
        .expect("derived executable directory");
    assert!(
        ui.find("Executable subdir").is_err(),
        "boilerplate executable subdir setting should not be visible",
    );
}

#[test]
fn tools_setting_input_emits_update_message() {
    let state = sample_tools_state("reshade");
    let mut ui = simulator(modde_ui::views::tools::view(&state));

    ui.click("/tmp/reshade")
        .expect("should focus source directory input");
    ui.typewrite("/new");
    let messages: Vec<_> = ui.into_messages().collect();
    assert!(
        messages.iter().any(|m| matches!(
            m,
            Message::UpdateToolSetting { tool_id, key, .. }
                if tool_id == "reshade" && key == "source_dir"
        )),
        "should emit UpdateToolSetting for source_dir, got: {messages:?}",
    );
}

fn sample_tools_state(active_tool_id: &str) -> ToolState {
    ToolState {
        active_tool_id: Some(active_tool_id.to_string()),
        game_label: Some("Skyrim SE".to_string()),
        game_dir_configured: true,
        loading: false,
        load_error: None,
        load_generation: 0,
        show_advanced_settings: false,
        active_operations: std::collections::HashSet::new(),
        tool_option_catalog: std::collections::HashMap::from([(
            "proton.selected_version".to_string(),
            vec!["latest".to_string()],
        )]),
        optiscaler_releases: Vec::new(),
        optiscaler_releases_loading: false,
        proton_versions_loading: false,
        executables: Vec::new(),
        executables_loading: false,
        executables_load_error: None,
        executables_load_generation: 0,
        executable_draft: Default::default(),
        executable_editor_open: false,
        executable_error: None,
        active_executable_operations: std::collections::HashSet::new(),
        entries: vec![ToolUiEntry {
            tool_id: "reshade".to_string(),
            display_name: "ReShade".to_string(),
            description: "Shader injection".to_string(),
            category: "Graphics".to_string(),
            available: true,
            availability_text: "available".to_string(),
            enabled: true,
            settings: serde_json::json!({
                "source_dir": "/tmp/reshade",
                "dll_name": "dxgi.dll",
                "exe_subdir": "",
            }),
            setting_specs: vec![
                modde_games::tools::ToolSettingSpec::path(
                    "source_dir",
                    "Source directory",
                    "Directory containing ReShade files.",
                ),
                modde_games::tools::ToolSettingSpec::select(
                    "dll_name",
                    "Proxy DLL",
                    "DLL copied beside the executable.",
                    &["dxgi.dll", "d3d11.dll"],
                ),
            ],
            generated_config_path: None,
            applied_files: vec!["dxgi.dll".to_string(), "ReShade.ini".to_string()],
            has_file_patching: true,
            release_support: ToolReleaseSupport::None,
            status_message: Some("Applied successfully".to_string()),
            env_preview: Vec::new(),
            dll_overrides: vec!["dxgi".to_string()],
            wrapper_preview: Vec::new(),
            derived_facts: vec![
                ("Game".to_string(), "Skyrim SE".to_string()),
                (
                    "Executable directory".to_string(),
                    "/games/skyrim".to_string(),
                ),
            ],
            optiscaler_state: None,
            optiscaler_latest_backup: None,
            optiscaler_detected_files: 0,
            apply_pending: true,
            apply_missing_inputs: Vec::new(),
            setting_history: Vec::new(),
        }],
    }
}

fn sample_tool_entry(tool_id: &str, display_name: &str) -> ToolUiEntry {
    let setting_specs = if tool_id == "optiscaler" {
        vec![
            modde_games::tools::ToolSettingSpec::labeled_select(
                "source_mode",
                "Source",
                "Where modde should get OptiScaler files from.",
                &[
                    ("github_release", "Official GitHub releases"),
                    ("goverlay_builds", "GOverlay builds"),
                    ("goverlay_fgmod", "GOverlay fgmod directory"),
                    ("local_dir", "Local OptiScaler directory"),
                ],
            ),
            modde_games::tools::ToolSettingSpec::select(
                "release_tag",
                "Release tag",
                "OptiScaler GitHub release tag selected in the UI.",
                &["v0.9.1"],
            ),
            modde_games::tools::ToolSettingSpec::select(
                "release_asset",
                "Release asset",
                "Release asset selected from GitHub.",
                &["Optiscaler_0.9.1-final.7z"],
            ),
        ]
    } else {
        vec![modde_games::tools::ToolSettingSpec::read_only(
            "summary",
            "Summary",
            "Tool summary.",
        )]
    };
    ToolUiEntry {
        tool_id: tool_id.to_string(),
        display_name: display_name.to_string(),
        description: format!("{display_name} settings"),
        category: "Tool".to_string(),
        available: true,
        availability_text: "available".to_string(),
        enabled: false,
        settings: if tool_id == "optiscaler" {
            serde_json::json!({
                "source_mode": "github_release",
                "release_tag": "v0.9.1",
                "release_asset": "Optiscaler_0.9.1-final.7z",
            })
        } else {
            serde_json::json!({ "summary": display_name })
        },
        setting_specs,
        generated_config_path: None,
        applied_files: Vec::new(),
        has_file_patching: false,
        release_support: ToolReleaseSupport::from_supports_releases(tool_id == "optiscaler"),
        status_message: None,
        env_preview: Vec::new(),
        dll_overrides: Vec::new(),
        wrapper_preview: Vec::new(),
        derived_facts: Vec::new(),
        optiscaler_state: None,
        optiscaler_latest_backup: None,
        optiscaler_detected_files: 0,
        apply_pending: true,
        apply_missing_inputs: Vec::new(),
        setting_history: Vec::new(),
    }
}