scope-bca-cli 0.8.4

Scope Blockchain Analysis - Command-line interface library
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
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
//! `scope venues` subcommands for managing venue descriptors.
//!
//! Provides venue discovery, schema documentation, initialisation of user
//! venue directories, and YAML validation.

use crate::cli::output::Output;
use crate::outln;
use clap::{Args, Subcommand};
use scope::config::Config;
use scope::display::terminal::{
    check_fail, check_pass, info_row, kv_row, section_footer, section_header, separator,
};
use scope::error::Result;
use scope::http::HttpClient;
use scope::market::catalog;
use scope::market::registry::VenueSource;
use scope::market::{MarketVenue, VenueRegistry};
use std::sync::Arc;

/// Venue management subcommands.
///
/// List available exchange venues, view the YAML schema, initialise the
/// user venues directory, or validate a custom descriptor file.
///
/// # Examples
///
/// ```text
/// scope venues list
/// scope venues list --format json
/// scope venues schema
/// scope venues init
/// scope venues validate my-exchange.yaml
/// ```
#[derive(Debug, Subcommand)]
pub enum VenuesCommands {
    /// List all available exchange venues and their capabilities.
    ///
    /// Shows the catalog's and your own CEX venues with their supported
    /// API capabilities (order_book, ticker, trades, ohlc), then the DEX
    /// venues.
    ///
    /// # Examples
    ///
    /// ```text
    /// scope venues list
    /// scope venues list --format json
    /// ```
    List(ListArgs),

    /// Display the YAML schema for venue descriptors.
    ///
    /// Prints the expected structure, field descriptions, and an annotated
    /// example you can copy to create your own venue descriptor.
    ///
    /// # Examples
    ///
    /// ```text
    /// scope venues schema
    /// scope venues schema --format json
    /// ```
    Schema(SchemaArgs),

    /// Fetch the venue catalog: new and fixed venues without a new scope.
    ///
    /// Downloads the catalog index and every venue file, checks each
    /// against its SHA-256, and replaces ~/.scope-bca/venues/catalog/.
    /// Your own files in ~/.scope-bca/venues/ are not touched and still
    /// override a catalog venue with the same id.
    ///
    /// # Examples
    ///
    /// ```text
    /// scope venues update
    /// scope venues update --url https://example.com/my-catalog
    /// ```
    Update(UpdateArgs),

    /// Copy the catalog's venue files into your venues folder to customise.
    ///
    /// Copies each catalog YAML file as it is to ~/.scope-bca/venues/,
    /// where your copy overrides the catalog's. Fetches the catalog first
    /// if it is not there.
    ///
    /// # Examples
    ///
    /// ```text
    /// scope venues init
    /// scope venues init --force
    /// ```
    Init(InitArgs),

    /// Validate a venue descriptor YAML file.
    ///
    /// Parses the file against the VenueDescriptor schema and reports
    /// any errors or warnings. Exits with code 0 on success, 1 on failure.
    ///
    /// # Examples
    ///
    /// ```text
    /// scope venues validate my-exchange.yaml
    /// scope venues validate ~/.scope-bca/venues/custom.yaml
    /// ```
    Validate(ValidateArgs),
}

/// Arguments for `scope venues update`.
#[derive(Debug, Args)]
#[command(after_help = "\x1b[1mExamples:\x1b[0m
  scope venues update
  scope venues update --url https://example.com/my-catalog")]
pub struct UpdateArgs {
    /// Fetch from this catalog folder URL (holding index.json) instead of
    /// the configured one (`market.venue_catalog_url`).
    #[arg(long, value_name = "URL")]
    pub url: Option<String>,
}

/// Arguments for `scope venues list`.
#[derive(Debug, Args)]
#[command(after_help = "\x1b[1mExamples:\x1b[0m
  scope venues list
  scope venues list --format json")]
pub struct ListArgs {
    /// Output format.
    #[arg(short, long, default_value = "table")]
    pub format: ListFormat,
}

/// Output format for venue listing.
#[derive(Debug, Clone, Copy, Default, clap::ValueEnum)]
pub enum ListFormat {
    /// Human-readable table (default).
    #[default]
    Table,
    /// JSON for programmatic consumption.
    Json,
}

/// Arguments for `scope venues schema`.
#[derive(Debug, Args)]
#[command(after_help = "\x1b[1mExamples:\x1b[0m
  scope venues schema
  scope venues schema --format json")]
pub struct SchemaArgs {
    /// Output format.
    #[arg(short, long, default_value = "text")]
    pub format: SchemaFormat,
}

/// Output format for schema display.
#[derive(Debug, Clone, Copy, Default, clap::ValueEnum)]
pub enum SchemaFormat {
    /// Human-readable annotated text (default).
    #[default]
    Text,
    /// JSON Schema representation.
    Json,
}

/// Arguments for `scope venues init`.
#[derive(Debug, Args)]
#[command(after_help = "\x1b[1mExamples:\x1b[0m
  scope venues init
  scope venues init --force")]
pub struct InitArgs {
    /// Overwrite existing files in the user venues directory.
    #[arg(long)]
    pub force: bool,
}

/// Arguments for `scope venues validate`.
#[derive(Debug, Args)]
#[command(after_help = "\x1b[1mExamples:\x1b[0m
  scope venues validate my-exchange.yaml
  scope venues validate ~/.scope-bca/venues/custom.yaml")]
pub struct ValidateArgs {
    /// Path to the YAML file to validate.
    pub file: std::path::PathBuf,
}

/// Run the venues command.
pub async fn run(
    cmd: VenuesCommands,
    config: &Config,
    http: Arc<dyn HttpClient>,
    out: &Output,
) -> Result<()> {
    match cmd {
        VenuesCommands::List(args) => run_list(args, out),
        VenuesCommands::Schema(args) => run_schema(args, out),
        VenuesCommands::Update(args) => {
            let url = args.url.as_deref().unwrap_or(config.market.catalog_url());
            run_update(url, http.as_ref(), &VenueRegistry::catalog_dir(), out).await?;
            // A newer catalog may now be hidden by the user's own copies.
            let scripted =
                crate::cli::ghola_offer::scripted(&crate::cli::ghola_offer::process_args(), config);
            offer_catalog_updates(scripted, out).await;
            Ok(())
        }
        VenuesCommands::Init(args) => {
            if catalog::installed(&VenueRegistry::catalog_dir()).is_empty() {
                run_update(
                    config.market.catalog_url(),
                    http.as_ref(),
                    &VenueRegistry::catalog_dir(),
                    out,
                )
                .await?;
            }
            run_init(args, out)
        }
        VenuesCommands::Validate(args) => run_validate(args, out),
    }
}

// =============================================================================
// Update
// =============================================================================

/// Syncs the catalog from `url` into `dest` and reports what changed. The
/// example file goes next to the user's venues either way.
async fn run_update(
    url: &str,
    http: &dyn HttpClient,
    dest: &std::path::Path,
    out: &Output,
) -> Result<()> {
    let example = catalog::example_path();
    let _ = catalog::write_example(&example);
    let report = catalog::sync(http, url, dest).await?;
    outln!(out, "{}", section_header("Venue Catalog"))?;
    outln!(out, "{}", kv_row("Updated", &report.summary()))?;
    outln!(out, "{}", kv_row("From", url))?;
    outln!(out, "{}", kv_row("Catalog", &dest.display().to_string()))?;
    outln!(
        out,
        "{}",
        kv_row(
            "Your venues",
            &VenueRegistry::user_venues_dir().display().to_string()
        )
    )?;
    outln!(out, "{}", section_footer())?;
    Ok(())
}

/// Before a command that needs venues: when no catalog is installed, fetch
/// it once (first use), saying so. Offline, it says how to get venues later
/// and leaves the example file; the command then runs with what there is.
pub async fn ensure_catalog(config: &Config, http: &dyn HttpClient, out: &Output) {
    static TRIED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
    ensure_catalog_at(
        config.market.catalog_url(),
        http,
        &VenueRegistry::catalog_dir(),
        &catalog::example_path(),
        &TRIED,
        out,
    )
    .await;
}

/// [`ensure_catalog`] for the catalog `dest`, the `example` file and a
/// once-per-process `tried` flag.
async fn ensure_catalog_at(
    url: &str,
    http: &dyn HttpClient,
    dest: &std::path::Path,
    example: &std::path::Path,
    tried: &std::sync::atomic::AtomicBool,
    out: &Output,
) {
    if !catalog::installed(dest).is_empty() || tried.swap(true, std::sync::atomic::Ordering::SeqCst)
    {
        return;
    }
    let _ = catalog::write_example(example);
    crate::note!(out, "Fetching the venue catalog (first use)…");
    match catalog::sync(http, url, dest).await {
        Ok(report) => crate::note!(out, "Venue catalog installed: {}", report.summary()),
        Err(e) => crate::note!(
            out,
            "Could not fetch the venue catalog ({e}). Run `scope venues update` when online, or describe an exchange yourself: see {}",
            example.display()
        ),
    }
}

// =============================================================================
// Stale copies: your files that hide a newer catalog venue
// =============================================================================

/// The question, in the user's words.
const OVERRIDE_QUESTION: &str = "Would you like me to save your existing copies and overwrite? [Y]es/[N]o/[O]verwrite, don't save: ";

/// What to do with the user's copies that hide catalog capabilities.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OverrideAnswer {
    /// Move the copies to a backup folder; the catalog's apply.
    SaveAndReplace,
    /// Keep them, and do not ask again until the catalog changes.
    Keep,
    /// Remove the copies without a backup; the catalog's apply.
    Replace,
}

/// `y`, `n` or `o` (any case, a word starting with one); anything else,
/// an empty line included, keeps the copies.
pub fn parse_override_answer(answer: &str) -> OverrideAnswer {
    match answer.trim().chars().next().map(|c| c.to_ascii_lowercase()) {
        Some('y') => OverrideAnswer::SaveAndReplace,
        Some('o') => OverrideAnswer::Replace,
        _ => OverrideAnswer::Keep,
    }
}

/// One of the user's files that hides a newer catalog venue.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StaleCopy {
    pub id: String,
    pub file: std::path::PathBuf,
    /// What the catalog's version has that the copy lacks.
    pub hidden: Vec<&'static str>,
    /// The catalog file's checksum, so a "no" lasts until it changes.
    pub catalog_sha: String,
}

/// The user's copies in `registry` that hide catalog capabilities, minus
/// those already declined for the same catalog version (`declined`).
pub fn stale_copies(
    registry: &VenueRegistry,
    catalog_dir: &std::path::Path,
    declined: &std::collections::HashMap<String, String>,
) -> Vec<StaleCopy> {
    let shas = catalog::installed(catalog_dir);
    registry
        .list()
        .into_iter()
        .filter_map(|id| {
            let hidden = registry.hidden_capabilities(id);
            let file = registry.user_file(id)?.to_path_buf();
            let catalog_sha = shas.iter().find(|e| e.id == id)?.sha256.clone();
            (!hidden.is_empty() && declined.get(id) != Some(&catalog_sha)).then(|| StaleCopy {
                id: id.to_string(),
                file,
                hidden,
                catalog_sha,
            })
        })
        .collect()
}

/// Where the "no" answers are kept: `{id: catalog sha256}`.
fn declined_path() -> std::path::PathBuf {
    scope::paths::state_dir().join("venue-overrides-declined.json")
}

fn read_declined(path: &std::path::Path) -> std::collections::HashMap<String, String> {
    std::fs::read_to_string(path)
        .ok()
        .and_then(|t| serde_json::from_str(&t).ok())
        .unwrap_or_default()
}

/// Carries out `answer` for `stale`: moves the copies to a backup folder
/// under `home` (named by `stamp`), removes them, or records the "no" in
/// `declined_file`. Returns what it did, for the user.
pub fn apply_override_answer(
    answer: OverrideAnswer,
    stale: &[StaleCopy],
    home: &std::path::Path,
    stamp: &str,
    declined_file: &std::path::Path,
) -> Result<String> {
    let names = stale
        .iter()
        .map(|c| c.id.as_str())
        .collect::<Vec<_>>()
        .join(", ");
    match answer {
        OverrideAnswer::SaveAndReplace => {
            let backup = home.join(format!("venues-backup-{stamp}"));
            std::fs::create_dir_all(&backup).map_err(|e| {
                scope::error::ScopeError::io_context(
                    e,
                    format!("Cannot create {}", backup.display()),
                )
            })?;
            for c in stale {
                let to = backup.join(c.file.file_name().unwrap_or_default());
                std::fs::rename(&c.file, &to).map_err(|e| {
                    scope::error::ScopeError::io_context(
                        e,
                        format!("Cannot move {}", c.file.display()),
                    )
                })?;
            }
            Ok(format!(
                "Saved your copies of {names} to {} and switched to the catalog's.",
                backup.display()
            ))
        }
        OverrideAnswer::Replace => {
            for c in stale {
                std::fs::remove_file(&c.file).map_err(|e| {
                    scope::error::ScopeError::io_context(
                        e,
                        format!("Cannot remove {}", c.file.display()),
                    )
                })?;
            }
            Ok(format!(
                "Removed your copies of {names}; the catalog's apply."
            ))
        }
        OverrideAnswer::Keep => {
            let mut declined = read_declined(declined_file);
            for c in stale {
                declined.insert(c.id.clone(), c.catalog_sha.clone());
            }
            if let Some(dir) = declined_file.parent() {
                let _ = std::fs::create_dir_all(dir);
            }
            let json = serde_json::to_string_pretty(&declined).unwrap_or_default();
            std::fs::write(declined_file, json).map_err(|e| {
                scope::error::ScopeError::io_context(
                    e,
                    format!("Cannot write {}", declined_file.display()),
                )
            })?;
            Ok(format!(
                "Kept your copies of {names}; scope will ask again when the catalog's change."
            ))
        }
    }
}

/// The question's text: which venues, and what the catalog adds.
fn override_prompt(stale: &[StaleCopy]) -> String {
    let lines: Vec<String> = stale
        .iter()
        .map(|c| {
            format!(
                "  {} ({}): the catalog's also has {}",
                c.id,
                c.file.display(),
                c.hidden.join(", ")
            )
        })
        .collect();
    format!(
        "New version of the venues catalog found. Your own venue files hide what it adds:\n{}\n",
        lines.join("\n")
    )
}

/// Before a command that reads venues: when the user's copies hide a newer
/// catalog venue, ask (once a process) whether to save and replace them,
/// replace them, or keep them. In the TUI the question is a list; on a
/// terminal, the `[Y]es/[N]o/[O]verwrite` prompt; scripted runs never ask.
pub async fn offer_catalog_updates(scripted: bool, out: &Output) {
    static ASKED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
    if scripted || ASKED.load(std::sync::atomic::Ordering::SeqCst) {
        return;
    }
    let Ok(registry) = VenueRegistry::load() else {
        return;
    };
    let asked = offer_with(
        &registry,
        &VenueRegistry::catalog_dir(),
        &declined_path(),
        &scope::paths::home(),
        out,
    )
    .await;
    if asked {
        ASKED.store(true, std::sync::atomic::Ordering::SeqCst);
    }
}

/// [`offer_catalog_updates`] for `registry`, its catalog folder, the file
/// of "no" answers and the scope home (where backups go). Returns whether
/// it asked.
async fn offer_with(
    registry: &VenueRegistry,
    catalog_dir: &std::path::Path,
    declined_file: &std::path::Path,
    home: &std::path::Path,
    out: &Output,
) -> bool {
    let stale = stale_copies(registry, catalog_dir, &read_declined(declined_file));
    if stale.is_empty() {
        return false;
    }
    let answer = if let Some(asker) = out.asker() {
        let picker = crate::cli::resolve::TuiPicker { asker };
        let choice = picker
            .ask(
                format!("{}Use the catalog's versions?", override_prompt(&stale)),
                vec![
                    "Yes: save my copies, then use the catalog's".to_string(),
                    "No: keep my copies".to_string(),
                    "Overwrite: use the catalog's, don't save my copies".to_string(),
                ],
            )
            .await;
        match choice {
            Some(0) => OverrideAnswer::SaveAndReplace,
            Some(2) => OverrideAnswer::Replace,
            // A cancelled question: nothing changes, and it was asked.
            None => return true,
            _ => OverrideAnswer::Keep,
        }
    } else {
        if !crate::cli::ghola_offer::may_ask(out) {
            return false;
        }
        let question = format!("{}{OVERRIDE_QUESTION}", override_prompt(&stale));
        match crate::cli::prompt::ask_stdin(out, &question) {
            Ok(a) => parse_override_answer(&a),
            Err(_) => return false,
        }
    };
    let stamp = chrono::Local::now().format("%Y-%m-%d-%H%M%S").to_string();
    match apply_override_answer(answer, &stale, home, &stamp, declined_file) {
        Ok(done) => crate::note!(out, "{done}"),
        Err(e) => crate::note!(out, "Could not update your venue files: {e}"),
    }
    true
}

// =============================================================================
// List
// =============================================================================

fn run_list(args: ListArgs, out: &Output) -> Result<()> {
    run_list_with(&VenueRegistry::load()?, args, out)
}

/// [`run_list`] for `registry`.
fn run_list_with(registry: &VenueRegistry, args: ListArgs, out: &Output) -> Result<()> {
    match args.format {
        ListFormat::Table => {
            outln!(out, "{}", section_header("Available Venues"))?;
            for listing in MarketVenue::listings(registry) {
                let caps = listing.capabilities.join(", ");
                let detail = match (listing.chain, registry.source(&listing.id)) {
                    (Some(chain), _) => format!("{caps} (DEX on {chain})"),
                    (None, Some(VenueSource::User)) => {
                        match registry.hidden_capabilities(&listing.id).as_slice() {
                            [] => format!("{caps} (yours)"),
                            hidden => format!(
                                "{caps} (yours; the catalog's also has {})",
                                hidden.join(", ")
                            ),
                        }
                    }
                    (None, _) => caps,
                };
                outln!(out, "{}", kv_row(&listing.id, &detail))?;
            }
            outln!(out, "{}", separator())?;
            let user_dir = VenueRegistry::user_venues_dir();
            let ids = registry.list();
            let yours = ids
                .iter()
                .filter(|id| registry.source(id) == Some(VenueSource::User))
                .count();
            outln!(
                out,
                "{}",
                kv_row(
                    "Loaded",
                    &format!(
                        "{} venues ({} from the catalog, {} yours)",
                        ids.len(),
                        ids.len() - yours,
                        yours
                    )
                )
            )?;
            if ids.is_empty() {
                outln!(
                    out,
                    "{}",
                    info_row("No exchange venues yet: run `scope venues update`")
                )?;
            }
            for id in &ids {
                if let Some(hint) = registry.override_hint(id) {
                    outln!(out, "{}", info_row(&hint))?;
                }
            }
            outln!(
                out,
                "{}",
                kv_row(
                    "Catalog",
                    &VenueRegistry::catalog_dir().display().to_string()
                )
            )?;
            outln!(
                out,
                "{}",
                kv_row("Your venues", &user_dir.display().to_string())
            )?;
            outln!(out, "{}", section_footer())?;
        }
        ListFormat::Json => {
            // The same list the web serves at /api/venues.
            let venues = MarketVenue::listings(registry);
            let output = serde_json::json!({
                "total": venues.len(),
                "venues": venues,
                "user_venues_dir": VenueRegistry::user_venues_dir().display().to_string(),
                "catalog_dir": VenueRegistry::catalog_dir().display().to_string(),
            });
            outln!(out, "{}", serde_json::to_string_pretty(&output).unwrap())?;
        }
    }

    Ok(())
}

// =============================================================================
// Schema
// =============================================================================

fn run_schema(args: SchemaArgs, out: &Output) -> Result<()> {
    match args.format {
        SchemaFormat::Text => print_annotated_schema(out)?,
        SchemaFormat::Json => print_json_schema(out)?,
    }
    Ok(())
}

fn print_annotated_schema(out: &Output) -> std::io::Result<()> {
    outln!(out, "{}", section_header("Venue Descriptor Schema"))?;
    outln!(
        out,
        r#"
A venue descriptor is a YAML file that tells Scope how to talk to
an exchange API. Place custom descriptors in:

  {}

Each file defines one venue with the following structure:

  id: my_exchange              # Unique ID (lowercase, underscores ok)
  name: My Exchange            # Human-readable display name
  base_url: https://api.example.com  # API base URL

  symbol:
    template: "{{base}}{{quote}}"  # Pair format (placeholders: {{base}}, {{quote}})
    default_quote: USDT          # Quote currency when user omits it
    case: upper                  # "upper" or "lower" (default: upper)

  error_envelope:                # Optional: errors sent with HTTP 200
    code_key: retCode            # Status field; omit to test message_key only
    ok_codes: ["0"]              # Status values that mean success
    message_key: retMsg          # Error text (a string, or an array like Kraken's)

  capabilities:
    order_book:                  # Omit if the venue has no depth API
      method: GET                # GET (default) or POST
      path: /api/v1/depth       # URL path appended to base_url
      params:                    # Query parameters (GET) — values can use {{pair}}, {{limit}}
        symbol: "{{pair}}"
        limit: "{{limit}}"
      response_root: data        # JSON path to the relevant data (optional)
      response:
        asks_key: asks           # JSON key for asks array
        bids_key: bids           # JSON key for bids array
        level_format: positional # "positional" for [price, qty] or "object"
        level_price_field: price # Only needed when level_format = object
        level_size_field: size   # Only needed when level_format = object

    ticker:                      # Omit if the venue has no ticker API
      path: /api/v1/ticker
      params:
        symbol: "{{pair}}"
      response:
        last_price: lastPrice
        high_24h: highPrice
        low_24h: lowPrice
        volume_24h: volume
        quote_volume_24h: quoteVolume
        price_change_24h: priceChange
        price_change_pct_24h: priceChangePercent

    trades:                      # Omit if the venue has no trades API
      path: /api/v1/trades
      params:
        symbol: "{{pair}}"
        limit: "{{limit}}"
      response:
        items_key: data          # JSON key containing the trades array (omit if root)
        price: price             # Field for trade price
        quantity: qty            # Field for trade quantity
        timestamp_ms: time       # Field for trade timestamp (epoch ms)
        side:                    # Side detection
          field: side            # JSON field containing buy/sell indicator
          mapping:
            buy: buy
            sell: sell

Validate your file with:  scope venues validate <file>
"#,
        VenueRegistry::user_venues_dir().display()
    )?;
    outln!(out, "{}", section_footer())?;
    Ok(())
}

fn print_json_schema(out: &Output) -> std::io::Result<()> {
    use serde_json::{Map, Value};

    fn str_prop(desc: &str) -> Value {
        let mut m = Map::new();
        m.insert("type".into(), Value::String("string".into()));
        m.insert("description".into(), Value::String(desc.into()));
        Value::Object(m)
    }

    fn str_type() -> Value {
        serde_json::json!({"type": "string"})
    }

    // Build response properties
    let mut resp_props = Map::new();
    for key in &[
        "asks_key",
        "bids_key",
        "level_format",
        "level_price_field",
        "level_size_field",
        "last_price",
        "high_24h",
        "low_24h",
        "volume_24h",
        "quote_volume_24h",
        "best_bid",
        "best_ask",
        "items_key",
        "price",
        "quantity",
        "quote_quantity",
        "timestamp_ms",
        "id",
    ] {
        resp_props.insert((*key).into(), str_type());
    }
    resp_props.insert(
        "filter".into(),
        serde_json::json!({
            "type": "object",
            "properties": {"field": {"type": "string"}, "value": {"type": "string"}}
        }),
    );
    resp_props.insert(
        "side".into(),
        serde_json::json!({
            "type": "object",
            "properties": {
                "field": {"type": "string"},
                "mapping": {"type": "object", "additionalProperties": {"type": "string"}}
            }
        }),
    );

    // Build endpoint def
    let endpoint_def = serde_json::json!({
        "type": "object",
        "required": ["path", "response"],
        "properties": {
            "method": {"type": "string", "enum": ["GET", "POST"], "default": "GET"},
            "path": {"type": "string"},
            "params": {"type": "object", "additionalProperties": {"type": "string"}},
            "request_body": {"description": "JSON body template for POST requests"},
            "response_root": {"type": "string", "description": "Dot-path to navigate JSON response"},
            "response": {"type": "object", "properties": Value::Object(resp_props)}
        }
    });

    let endpoint_ref = serde_json::json!({"$ref": "#/$defs/endpoint"});

    let schema = serde_json::json!({
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "title": "VenueDescriptor",
        "description": "Schema for Scope exchange venue descriptor YAML files.",
        "type": "object",
        "required": ["id", "name", "base_url", "symbol", "capabilities"],
        "properties": {
            "id": str_prop("Unique venue identifier (e.g., 'binance')"),
            "name": str_prop("Human-readable venue name (e.g., 'Binance Spot')"),
            "base_url": str_prop("API base URL (e.g., 'https://api.binance.com')"),
            "symbol": {
                "type": "object",
                "required": ["template", "default_quote"],
                "properties": {
                    "template": str_prop("Pair template with {base} and {quote} placeholders"),
                    "default_quote": str_prop("Default quote currency (e.g., 'USDT')"),
                    "case": {"type": "string", "enum": ["upper", "lower"], "default": "upper"}
                }
            },
            "error_envelope": {
                "type": "object",
                "description": "How the venue reports an error inside an HTTP 200 reply",
                "required": ["message_key"],
                "properties": {
                    "code_key": str_prop("Status field; a missing field means success"),
                    "ok_codes": {"type": "array", "items": {"type": "string"}, "description": "Status values that mean success, compared as strings"},
                    "message_key": str_prop("Field with the error message (string or array)")
                }
            },
            "capabilities": {
                "type": "object",
                "properties": {
                    "order_book": endpoint_ref.clone(),
                    "ticker": endpoint_ref.clone(),
                    "trades": endpoint_ref
                }
            }
        },
        "$defs": {
            "endpoint": endpoint_def
        }
    });

    outln!(out, "{}", serde_json::to_string_pretty(&schema).unwrap())?;
    Ok(())
}

// =============================================================================
// Init
// =============================================================================

fn run_init(args: InitArgs, out: &Output) -> Result<()> {
    run_init_impl(args, VenueRegistry::user_venues_dir(), out)
}

/// Core init logic with explicit destination path (used by tests).
fn run_init_impl(args: InitArgs, dest: std::path::PathBuf, out: &Output) -> Result<()> {
    // Ensure directory exists
    if !dest.exists() {
        std::fs::create_dir_all(&dest).map_err(|e| {
            let context = format!("Failed to create venues directory {}", dest.display());
            scope::error::ScopeError::io_context(e, context)
        })?;
        outln!(out, "Created {}", dest.display())?;
    }

    // The catalog's files, copied as they are: a re-serialised copy lost
    // what the serialiser did not know (the `ohlc` capability).
    let source = VenueRegistry::catalog_dir();
    let mut files: Vec<std::path::PathBuf> = std::fs::read_dir(&source)
        .map(|entries| {
            entries
                .flatten()
                .map(|e| e.path())
                .filter(|p| p.extension().is_some_and(|ext| ext == "yaml"))
                .collect()
        })
        .unwrap_or_default();
    files.sort();
    if files.is_empty() {
        return Err(scope::error::ScopeError::InvalidInput(
            "The venue catalog is not installed: run `scope venues update` first.".into(),
        ));
    }
    let mut copied = 0;
    let mut skipped = 0;

    for path in files {
        let filename = path.file_name().unwrap().to_string_lossy().into_owned();
        let target = dest.join(&filename);

        if target.exists() && !args.force {
            skipped += 1;
            outln!(
                out,
                "  skip {} (exists, use --force to overwrite)",
                filename
            )?;
            continue;
        }

        std::fs::copy(&path, &target).map_err(|e| {
            scope::error::ScopeError::io_context(e, format!("Failed to write {}", target.display()))
        })?;
        copied += 1;
        outln!(out, "  {}", check_pass(&filename))?;
    }

    outln!(out)?;
    outln!(out, "{}", section_header("Venues Init"))?;
    outln!(out, "{}", kv_row("Directory", &dest.display().to_string()))?;
    outln!(out, "{}", kv_row("Copied", &format!("{} files", copied)))?;
    if skipped > 0 {
        outln!(
            out,
            "{}",
            kv_row("Skipped", &format!("{} files (already exist)", skipped))
        )?;
    }
    outln!(out, "{}", section_footer())?;

    Ok(())
}

// =============================================================================
// Validate
// =============================================================================

fn run_validate(args: ValidateArgs, out: &Output) -> Result<()> {
    let path = &args.file;

    if !path.exists() {
        outln!(
            out,
            "{}",
            check_fail(&format!("File not found: {}", path.display()))
        )?;
        return Err(scope::error::ScopeError::Chain(format!(
            "File not found: {}",
            path.display()
        )));
    }

    let content = std::fs::read_to_string(path).map_err(|e| {
        scope::error::ScopeError::io_context(e, format!("Failed to read {}", path.display()))
    })?;

    outln!(out, "{}", section_header("Venue Validation"))?;
    outln!(out, "{}", kv_row("File", &path.display().to_string()))?;
    outln!(out, "{}", separator())?;

    match VenueRegistry::validate_yaml(&content) {
        Ok(desc) => {
            outln!(out, "{}", check_pass("Valid YAML syntax"))?;
            outln!(out, "{}", check_pass(&format!("Venue ID: {}", desc.id)))?;
            outln!(out, "{}", check_pass(&format!("Name: {}", desc.name)))?;
            outln!(
                out,
                "{}",
                check_pass(&format!("Base URL: {}", desc.base_url))
            )?;

            // Check capabilities
            let caps = desc.capability_names();
            if caps.is_empty() {
                outln!(
                    out,
                    "{}",
                    check_fail(
                        "No capabilities defined (need at least one of: order_book, ticker, trades)"
                    )
                )?;
            } else {
                for cap in &caps {
                    outln!(out, "{}", check_pass(&format!("Capability: {}", cap)))?;
                }
            }

            // Validate symbol template
            if desc.symbol.template.contains("{base}") {
                outln!(
                    out,
                    "{}",
                    check_pass("Symbol template contains {base} placeholder")
                )?;
            } else {
                outln!(
                    out,
                    "{}",
                    check_fail("Symbol template missing {base} placeholder")
                )?;
            }

            outln!(out, "{}", separator())?;
            if caps.is_empty() {
                outln!(out, "{}", check_fail("Validation completed with warnings"))?;
            } else {
                outln!(out, "{}", check_pass("Validation passed"))?;
            }
            outln!(out, "{}", section_footer())?;
            Ok(())
        }
        Err(e) => {
            outln!(out, "{}", check_fail("Invalid YAML"))?;
            outln!(out, "{}", check_fail(&format!("Error: {}", e)))?;
            outln!(out, "{}", separator())?;
            outln!(
                out,
                "{}",
                kv_row(
                    "Hint",
                    "Run `scope venues schema` to see the expected format"
                )
            )?;
            outln!(out, "{}", section_footer())?;
            Err(e)
        }
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    fn quiet() -> Output {
        Output::capture().0
    }

    #[test]
    fn test_list_format_default() {
        let fmt = ListFormat::default();
        assert!(matches!(fmt, ListFormat::Table));
    }

    #[test]
    fn test_schema_format_default() {
        let fmt = SchemaFormat::default();
        assert!(matches!(fmt, SchemaFormat::Text));
    }

    #[test]
    fn test_run_list_table() {
        let args = ListArgs {
            format: ListFormat::Table,
        };
        let result = run_list(args, &quiet());
        assert!(result.is_ok());
    }

    #[test]
    fn test_run_list_json() {
        let args = ListArgs {
            format: ListFormat::Json,
        };
        let (o, cap) = Output::capture();
        run_list(args, &o).unwrap();
        // Scripts parse this: it must be one JSON document on the data channel.
        let v: serde_json::Value = serde_json::from_str(&cap.out()).unwrap();
        assert!(v["venues"].as_array().is_some_and(|a| !a.is_empty()));
        assert_eq!(
            v["total"].as_u64().unwrap() as usize,
            v["venues"].as_array().unwrap().len()
        );
        // DEX venues are listed too, marked by kind.
        assert!(
            v["venues"]
                .as_array()
                .unwrap()
                .iter()
                .any(|venue| venue["kind"] == "dex")
        );
    }

    #[test]
    fn test_run_schema_text() {
        let args = SchemaArgs {
            format: SchemaFormat::Text,
        };
        let result = run_schema(args, &quiet());
        assert!(result.is_ok());
    }

    #[test]
    fn test_run_schema_json() {
        let args = SchemaArgs {
            format: SchemaFormat::Json,
        };
        let (o, cap) = Output::capture();
        run_schema(args, &o).unwrap();
        let v: serde_json::Value = serde_json::from_str(&cap.out()).unwrap();
        assert!(v.is_object());
    }

    #[test]
    fn test_validate_missing_file() {
        let args = ValidateArgs {
            file: std::path::PathBuf::from("/tmp/nonexistent_venue_test.yaml"),
        };
        let result = run_validate(args, &quiet());
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_valid_file() {
        let yaml = r#"
id: test_venue
name: Test Exchange
base_url: https://api.test.com
symbol:
  template: "{base}{quote}"
  default_quote: USDT
capabilities:
  order_book:
    path: /depth
    params:
      symbol: "{pair}"
    response:
      asks_key: asks
      bids_key: bids
      level_format: positional
"#;
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("test.yaml");
        std::fs::write(&path, yaml).unwrap();

        let args = ValidateArgs { file: path };
        let (o, cap) = Output::capture();
        run_validate(args, &o).unwrap();
        assert!(cap.out().contains("Venue ID: test_venue"));
        assert!(cap.out().contains("Validation passed"));
    }

    #[test]
    fn test_validate_invalid_file() {
        let yaml = "this is not valid yaml: [";
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("bad.yaml");
        std::fs::write(&path, yaml).unwrap();

        let args = ValidateArgs { file: path };
        let result = run_validate(args, &quiet());
        assert!(result.is_err());
    }

    #[test]
    fn test_run_init_to_temp_dir() {
        let dir = tempfile::tempdir().unwrap();
        let dest = dir.path().to_path_buf();
        let args = InitArgs { force: true };
        let result = run_init_impl(args, dest.clone(), &quiet());
        assert!(result.is_ok());

        // Verify venue files were created (registry has 11 built-in venues)
        let registry = VenueRegistry::load().unwrap();
        for id in registry.list() {
            let filename = format!("{}.yaml", id);
            let target = dest.join(&filename);
            assert!(target.exists(), "Expected {} to exist", filename);
            let content = std::fs::read_to_string(&target).unwrap();
            assert!(content.contains(&format!("id: {}", id)));
        }
    }

    #[test]
    fn test_run_init_skips_existing() {
        let dir = tempfile::tempdir().unwrap();
        let dest = dir.path().to_path_buf();

        // Write an existing binance.yaml before init
        let existing_path = dest.join("binance.yaml");
        std::fs::create_dir_all(&dest).unwrap();
        let original_content = "id: binance\n# pre-existing file\n";
        std::fs::write(&existing_path, original_content).unwrap();

        // Run init with force=false
        let args = InitArgs { force: false };
        let result = run_init_impl(args, dest.clone(), &quiet());
        assert!(result.is_ok());

        // Verify binance.yaml was NOT overwritten (content unchanged)
        let content = std::fs::read_to_string(&existing_path).unwrap();
        assert_eq!(
            content, original_content,
            "Existing file should not be overwritten when force=false"
        );
    }

    #[test]
    fn test_run_init_creates_directory_when_missing() {
        let dir = tempfile::tempdir().unwrap();
        let dest = dir.path().join("nested").join("venues");
        // Ensure parent doesn't exist
        assert!(!dest.exists());
        let args = InitArgs { force: true };
        let result = run_init_impl(args, dest.clone(), &quiet());
        assert!(result.is_ok());
        assert!(dest.exists());
        assert!(dest.is_dir());
    }

    #[test]
    fn test_validate_file_template_missing_base() {
        let yaml = r#"
id: bad_template
name: Bad Template Exchange
base_url: https://api.test.com
symbol:
  template: "nobase{quote}"
  default_quote: USDT
capabilities:
  order_book:
    path: /depth
    params:
      symbol: "{pair}"
    response:
      asks_key: asks
      bids_key: bids
      level_format: positional
"#;
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("bad_template.yaml");
        std::fs::write(&path, yaml).unwrap();

        let args = ValidateArgs { file: path };
        let result = run_validate(args, &quiet());
        assert!(result.is_ok()); // YAML parses; check_fail is printed for missing {base}
    }

    #[test]
    fn test_validate_file_empty_capabilities() {
        let yaml = r#"
id: no_caps
name: No Capabilities
base_url: https://api.test.com
symbol:
  template: "{base}{quote}"
  default_quote: USDT
capabilities: {}
"#;
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("no_caps.yaml");
        std::fs::write(&path, yaml).unwrap();

        let args = ValidateArgs { file: path };
        let result = run_validate(args, &quiet());
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_run_venues_command_routes_to_subcommands() {
        let config = Config::default();
        let http = scope::http::test_transport();
        let result = run(
            VenuesCommands::List(ListArgs {
                format: ListFormat::Table,
            }),
            &config,
            http.clone(),
            &quiet(),
        )
        .await;
        assert!(result.is_ok());
        let result = run(
            VenuesCommands::Schema(SchemaArgs {
                format: SchemaFormat::Text,
            }),
            &config,
            http,
            &quiet(),
        )
        .await;
        assert!(result.is_ok());
    }

    /// A catalog served by a local server: `index.json` and one venue.
    async fn catalog_server(venue: &str) -> mockito::ServerGuard {
        let mut server = mockito::Server::new_async().await;
        let index = serde_json::json!({
            "schema": 1,
            "venues": [{"id": "weex", "file": "weex.yaml", "sha256": catalog::checksum(venue)}],
        });
        server
            .mock("GET", "/c/index.json")
            .with_body(index.to_string())
            .create_async()
            .await;
        server
            .mock("GET", "/c/weex.yaml")
            .with_body(venue)
            .create_async()
            .await;
        server
    }

    const WEEX: &str = "id: weex\nname: WEEX Spot\nbase_url: https://api-spot.weex.com\nsymbol:\n  template: \"{base}{quote}\"\n  default_quote: USDT\ncapabilities:\n  ohlc:\n    path: /api/v3/market/klines\n    response:\n      ohlc_format: array_of_arrays\n      ohlc_fields: [open_time, open, high, low, close, volume, close_time]\n";

    #[tokio::test]
    async fn test_update_installs_the_catalog_and_reports_it() {
        let server = catalog_server(WEEX).await;
        let home = tempfile::tempdir().unwrap();
        let dest = home.path().join("catalog");
        let (out, cap) = Output::capture();
        run_update(
            &format!("{}/c", server.url()),
            scope::http::test_transport().as_ref(),
            &dest,
            &out,
        )
        .await
        .unwrap();
        let text = crate::cli::ansi::strip(&cap.out());
        assert!(
            text.contains("1 venues: 1 added (weex), 0 updated, 0 removed"),
            "{text}"
        );
        let registry = VenueRegistry::load_dirs(&dest, &home.path().join("none")).unwrap();
        // The file is the catalog's, byte for byte: nothing is re-serialised.
        assert_eq!(
            std::fs::read_to_string(dest.join("weex.yaml")).unwrap(),
            WEEX
        );
        assert!(registry.get("weex").unwrap().capabilities.ohlc.is_some());
    }

    #[test]
    fn test_init_copies_the_catalog_files_verbatim() {
        let dir = tempfile::tempdir().unwrap();
        let dest = dir.path().join("venues");
        run_init_impl(InitArgs { force: false }, dest.clone(), &quiet()).unwrap();
        let catalog = VenueRegistry::catalog_dir();
        for name in ["binance.yaml", "kraken.yaml"] {
            assert_eq!(
                std::fs::read(dest.join(name)).unwrap(),
                std::fs::read(catalog.join(name)).unwrap(),
                "{name} is copied as it is"
            );
        }
        // ohlc survives, which the old re-serialised copies lost.
        let copied = VenueRegistry::load_dirs(&dir.path().join("none"), &dest).unwrap();
        assert!(copied.get("binance").unwrap().capabilities.ohlc.is_some());
    }

    #[test]
    fn test_list_marks_your_own_venues() {
        let (out, cap) = Output::capture();
        run_list(
            ListArgs {
                format: ListFormat::Table,
            },
            &out,
        )
        .unwrap();
        let text = crate::cli::ansi::strip(&cap.out());
        assert!(text.contains("from the catalog"), "{text}");
        assert!(text.contains("Catalog"), "{text}");
    }

    #[tokio::test]
    async fn test_first_use_fetches_the_catalog_once_and_says_so() {
        let server = catalog_server(WEEX).await;
        let home = tempfile::tempdir().unwrap();
        let dest = home.path().join("catalog");
        let example = home.path().join(catalog::EXAMPLE_FILE);
        let tried = std::sync::atomic::AtomicBool::new(false);
        let http = scope::http::test_transport();
        let url = format!("{}/c", server.url());
        let (out, cap) = Output::capture();
        ensure_catalog_at(&url, http.as_ref(), &dest, &example, &tried, &out).await;
        let notes = cap.err();
        assert!(
            notes.contains("Fetching the venue catalog (first use)"),
            "{notes}"
        );
        assert!(notes.contains("1 added (weex)"), "{notes}");
        assert!(example.exists(), "the example is written");
        // Installed: nothing more, even with a fresh flag.
        let (out, cap) = Output::capture();
        let fresh = std::sync::atomic::AtomicBool::new(false);
        ensure_catalog_at(&url, http.as_ref(), &dest, &example, &fresh, &out).await;
        assert!(cap.err().is_empty(), "{}", cap.err());
    }

    #[tokio::test]
    async fn test_first_use_offline_says_how_to_get_venues_and_tries_once() {
        let home = tempfile::tempdir().unwrap();
        let dest = home.path().join("catalog");
        let example = home.path().join(catalog::EXAMPLE_FILE);
        let tried = std::sync::atomic::AtomicBool::new(false);
        let http = scope::http::test_transport();
        let (out, cap) = Output::capture();
        ensure_catalog_at(
            "http://127.0.0.1:9/none",
            http.as_ref(),
            &dest,
            &example,
            &tried,
            &out,
        )
        .await;
        let notes = cap.err();
        assert!(
            notes.contains("Could not fetch the venue catalog"),
            "{notes}"
        );
        assert!(notes.contains("scope venues update"), "{notes}");
        assert!(notes.contains(catalog::EXAMPLE_FILE), "{notes}");
        // Once a process: the second command does not try again.
        let (out, cap) = Output::capture();
        ensure_catalog_at(
            "http://127.0.0.1:9/none",
            http.as_ref(),
            &dest,
            &example,
            &tried,
            &out,
        )
        .await;
        assert!(cap.err().is_empty(), "{}", cap.err());
    }

    #[tokio::test]
    async fn test_update_from_an_unreachable_url_fails_and_keeps_the_catalog() {
        let before = catalog::installed(&VenueRegistry::catalog_dir());
        let result = run(
            VenuesCommands::Update(UpdateArgs {
                url: Some("http://127.0.0.1:9/none".into()),
            }),
            &Config::default(),
            scope::http::test_transport(),
            &quiet(),
        )
        .await;
        assert!(result.is_err());
        assert_eq!(catalog::installed(&VenueRegistry::catalog_dir()), before);
    }

    #[test]
    fn test_list_with_no_venues_says_how_to_get_them_and_marks_yours() {
        let (out, cap) = Output::capture();
        run_list_with(
            &VenueRegistry::default(),
            ListArgs {
                format: ListFormat::Table,
            },
            &out,
        )
        .unwrap();
        let text = crate::cli::ansi::strip(&cap.out());
        assert!(
            text.contains("0 venues (0 from the catalog, 0 yours)"),
            "{text}"
        );
        assert!(text.contains("No exchange venues yet"), "{text}");

        let user = tempfile::tempdir().unwrap();
        std::fs::write(user.path().join("weex.yaml"), WEEX).unwrap();
        let registry =
            VenueRegistry::load_dirs(&VenueRegistry::catalog_dir(), user.path()).unwrap();
        let (out, cap) = Output::capture();
        run_list_with(
            &registry,
            ListArgs {
                format: ListFormat::Table,
            },
            &out,
        )
        .unwrap();
        let text = crate::cli::ansi::strip(&cap.out());
        // This weex describes only candles: it hides the catalog's book,
        // ticker and trades, and the list says so (wrapped at any width).
        let words = text
            .split_whitespace()
            .filter(|w| *w != "│")
            .collect::<Vec<_>>()
            .join(" ");
        assert!(
            words.contains("ohlc (yours; the catalog's also has order_book, ticker, trades)"),
            "{text}"
        );
        assert!(text.contains(", 1 yours)"), "{text}");
    }

    #[test]
    fn test_list_says_when_your_file_hides_catalog_capabilities() {
        let user = tempfile::tempdir().unwrap();
        let without_ohlc = &WEEX[..WEEX.find("  ohlc:").unwrap()];
        let weex_catalog = tempfile::tempdir().unwrap();
        std::fs::write(weex_catalog.path().join("weex.yaml"), WEEX).unwrap();
        std::fs::write(user.path().join("weex.yaml"), without_ohlc).unwrap();
        let registry = VenueRegistry::load_dirs(weex_catalog.path(), user.path()).unwrap();
        let (out, cap) = Output::capture();
        run_list_with(
            &registry,
            ListArgs {
                format: ListFormat::Table,
            },
            &out,
        )
        .unwrap();
        let text = crate::cli::ansi::strip(&cap.out());
        // The hint wraps under the box rail.
        let words = text
            .split_whitespace()
            .filter(|w| *w != "│")
            .collect::<Vec<_>>()
            .join(" ");
        assert!(
            words.contains("(yours; the catalog's also has ohlc)"),
            "{text}"
        );
        assert!(
            words.contains("overrides the catalog's weex, which also has ohlc"),
            "{text}"
        );
    }

    #[test]
    fn test_override_answers_parse_like_the_prompt_says() {
        for (input, want) in [
            ("y", OverrideAnswer::SaveAndReplace),
            ("Yes\n", OverrideAnswer::SaveAndReplace),
            ("o", OverrideAnswer::Replace),
            (" Overwrite", OverrideAnswer::Replace),
            ("n", OverrideAnswer::Keep),
            ("", OverrideAnswer::Keep),
            ("maybe", OverrideAnswer::Keep),
        ] {
            assert_eq!(parse_override_answer(input), want, "{input:?}");
        }
    }

    /// A catalog with `weex` (with ohlc) and its index, and a user folder
    /// with a weex that lacks it.
    fn stale_setup() -> (tempfile::TempDir, std::path::PathBuf, std::path::PathBuf) {
        let home = tempfile::tempdir().unwrap();
        let catalog_dir = home.path().join("venues").join("catalog");
        let user = home.path().join("venues");
        std::fs::create_dir_all(&catalog_dir).unwrap();
        std::fs::write(catalog_dir.join("weex.yaml"), WEEX).unwrap();
        let index = serde_json::json!({"schema": 1, "venues": [
            {"id": "weex", "file": "weex.yaml", "sha256": catalog::checksum(WEEX)}
        ]});
        std::fs::write(catalog_dir.join("index.json"), index.to_string()).unwrap();
        std::fs::write(
            user.join("weex.yaml"),
            &WEEX[..WEEX.find("  ohlc:").unwrap()],
        )
        .unwrap();
        (home, catalog_dir, user)
    }

    #[test]
    fn test_stale_copies_are_found_until_declined_for_that_catalog_version() {
        let (_home, catalog_dir, user) = stale_setup();
        let registry = VenueRegistry::load_dirs(&catalog_dir, &user).unwrap();
        let none = std::collections::HashMap::new();
        let stale = stale_copies(&registry, &catalog_dir, &none);
        assert_eq!(stale.len(), 1);
        assert_eq!(
            (stale[0].id.as_str(), stale[0].hidden.as_slice()),
            ("weex", &["ohlc"][..])
        );
        assert!(
            override_prompt(&stale).contains("weex"),
            "{}",
            override_prompt(&stale)
        );
        // Declined for this catalog version: not asked again.
        let declined =
            std::collections::HashMap::from([("weex".to_string(), catalog::checksum(WEEX))]);
        assert!(stale_copies(&registry, &catalog_dir, &declined).is_empty());
        // Declined for an older one: asked again.
        let old = std::collections::HashMap::from([("weex".to_string(), "old".to_string())]);
        assert_eq!(stale_copies(&registry, &catalog_dir, &old).len(), 1);
    }

    #[test]
    fn test_yes_saves_and_replaces_overwrite_removes_no_remembers() {
        // Yes: the copy moves to a backup; the catalog's applies.
        let (home, catalog_dir, user) = stale_setup();
        let registry = VenueRegistry::load_dirs(&catalog_dir, &user).unwrap();
        let stale = stale_copies(&registry, &catalog_dir, &Default::default());
        let declined = home.path().join("state").join("declined.json");
        let done = apply_override_answer(
            OverrideAnswer::SaveAndReplace,
            &stale,
            home.path(),
            "t",
            &declined,
        )
        .unwrap();
        assert!(done.contains("Saved your copies of weex"), "{done}");
        assert!(
            home.path()
                .join("venues-backup-t")
                .join("weex.yaml")
                .exists()
        );
        let after = VenueRegistry::load_dirs(&catalog_dir, &user).unwrap();
        assert_eq!(after.source("weex"), Some(VenueSource::Catalog));
        assert!(after.get("weex").unwrap().has_ohlc());

        // Overwrite: removed, no backup.
        let (home, catalog_dir, user) = stale_setup();
        let registry = VenueRegistry::load_dirs(&catalog_dir, &user).unwrap();
        let stale = stale_copies(&registry, &catalog_dir, &Default::default());
        apply_override_answer(OverrideAnswer::Replace, &stale, home.path(), "t", &declined)
            .unwrap();
        assert!(!user.join("weex.yaml").exists());
        assert!(!home.path().join("venues-backup-t").exists());

        // No: kept, and the answer recorded for this catalog version.
        let (home, catalog_dir, user) = stale_setup();
        let registry = VenueRegistry::load_dirs(&catalog_dir, &user).unwrap();
        let stale = stale_copies(&registry, &catalog_dir, &Default::default());
        let declined = home.path().join("state").join("declined.json");
        let done = apply_override_answer(OverrideAnswer::Keep, &stale, home.path(), "t", &declined)
            .unwrap();
        assert!(
            done.contains("ask again when the catalog's change"),
            "{done}"
        );
        assert!(user.join("weex.yaml").exists());
        assert!(stale_copies(&registry, &catalog_dir, &read_declined(&declined)).is_empty());
    }

    /// Answers the TUI's next list question with `choice`.
    fn answer_with(asker: crate::cli::output::Asker, choice: Option<usize>) {
        tokio::spawn(async move {
            loop {
                let ask = asker.lock().unwrap().take();
                if let Some(ask) = ask {
                    assert!(
                        ask.prompt
                            .contains("New version of the venues catalog found")
                    );
                    assert_eq!(ask.options.len(), 3);
                    let _ = ask.reply.send(choice);
                    return;
                }
                tokio::time::sleep(std::time::Duration::from_millis(5)).await;
            }
        });
    }

    #[tokio::test]
    async fn test_the_tui_offer_saves_and_replaces_on_yes() {
        let (home, catalog_dir, user) = stale_setup();
        let registry = VenueRegistry::load_dirs(&catalog_dir, &user).unwrap();
        let declined = home.path().join("declined.json");
        let asker: crate::cli::output::Asker = Default::default();
        let (out, cap) = Output::capture();
        let out = out.with_asker(asker.clone());
        answer_with(asker, Some(0));
        assert!(offer_with(&registry, &catalog_dir, &declined, home.path(), &out).await);
        assert!(
            cap.err().contains("Saved your copies of weex"),
            "{}",
            cap.err()
        );
        assert!(!user.join("weex.yaml").exists());
    }

    #[tokio::test]
    async fn test_the_tui_offer_overwrites_keeps_or_cancels() {
        // Overwrite.
        let (home, catalog_dir, user) = stale_setup();
        let registry = VenueRegistry::load_dirs(&catalog_dir, &user).unwrap();
        let declined = home.path().join("declined.json");
        let asker: crate::cli::output::Asker = Default::default();
        let (out, cap) = Output::capture();
        let out = out.with_asker(asker.clone());
        answer_with(asker, Some(2));
        assert!(offer_with(&registry, &catalog_dir, &declined, home.path(), &out).await);
        assert!(
            cap.err().contains("Removed your copies of weex"),
            "{}",
            cap.err()
        );

        // No: kept and recorded.
        let (home, catalog_dir, user) = stale_setup();
        let registry = VenueRegistry::load_dirs(&catalog_dir, &user).unwrap();
        let declined = home.path().join("declined.json");
        let asker: crate::cli::output::Asker = Default::default();
        let (out, _) = Output::capture();
        let out = out.with_asker(asker.clone());
        answer_with(asker, Some(1));
        assert!(offer_with(&registry, &catalog_dir, &declined, home.path(), &out).await);
        assert!(user.join("weex.yaml").exists() && declined.exists());

        // Cancelled: nothing changes.
        let (home, catalog_dir, user) = stale_setup();
        let registry = VenueRegistry::load_dirs(&catalog_dir, &user).unwrap();
        let declined = home.path().join("declined.json");
        let asker: crate::cli::output::Asker = Default::default();
        let (out, cap) = Output::capture();
        let out = out.with_asker(asker.clone());
        answer_with(asker, None);
        assert!(offer_with(&registry, &catalog_dir, &declined, home.path(), &out).await);
        assert!(cap.err().is_empty(), "{}", cap.err());
        assert!(user.join("weex.yaml").exists() && !declined.exists());
    }

    #[tokio::test]
    async fn test_no_offer_without_stale_copies_or_a_way_to_ask() {
        let (home, catalog_dir, user) = stale_setup();
        let declined = home.path().join("declined.json");
        // Nothing stale: the catalog alone.
        let clean = VenueRegistry::load_dirs(&catalog_dir, &home.path().join("none")).unwrap();
        let (out, _) = Output::capture();
        assert!(!offer_with(&clean, &catalog_dir, &declined, home.path(), &out).await);
        // Stale, but no TUI and no terminal: not asked, nothing changes.
        let registry = VenueRegistry::load_dirs(&catalog_dir, &user).unwrap();
        assert!(!offer_with(&registry, &catalog_dir, &declined, home.path(), &out).await);
        assert!(user.join("weex.yaml").exists());
        // A scripted run returns before reading anything.
        offer_catalog_updates(true, &out).await;
    }

    #[test]
    fn test_a_failed_move_or_removal_is_an_error_not_a_panic() {
        let (home, catalog_dir, user) = stale_setup();
        let registry = VenueRegistry::load_dirs(&catalog_dir, &user).unwrap();
        let stale = stale_copies(&registry, &catalog_dir, &Default::default());
        std::fs::remove_file(user.join("weex.yaml")).unwrap();
        let declined = home.path().join("declined.json");
        for answer in [OverrideAnswer::SaveAndReplace, OverrideAnswer::Replace] {
            let err = apply_override_answer(answer, &stale, home.path(), "t", &declined)
                .unwrap_err()
                .to_string();
            assert!(err.contains("weex.yaml"), "{err}");
        }
        // The answers file cannot be written where a folder is.
        std::fs::create_dir_all(&declined).unwrap();
        assert!(
            apply_override_answer(OverrideAnswer::Keep, &stale, home.path(), "t", &declined)
                .is_err()
        );
    }
}