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
//! # TUI Command Execution
//!
//! Turns one line of TUI input into a [`Plan`]. Session commands (`chain`,
//! `format`, `ctx`, `tokens`, …) run here and change the [`SessionContext`].
//! Every other line is parsed by the CLI's own clap definition ([`Cli`]), so
//! commands, aliases and flags are the same as on the command line.
//!
//! The session context fills in flags the user did not type, for example
//! `--chain` when a chain is pinned. A flag the user types always wins.

use crate::cli::interactive::SessionContext;
use crate::cli::output::Output;
use crate::cli::{Cli, Commands};
use crate::{errln, out, outln};
use clap::error::ErrorKind;
use clap::{CommandFactory, Parser, ValueEnum};
use scope::config::{Config, OutputFormat};
use scope::tokens::TokenAliases;
use std::io;

/// Where a parsed command runs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Route {
    /// In the TUI: the output goes to the output pane.
    Pane,
    /// On the plain terminal: the TUI steps aside while the command runs,
    /// because the command reads from stdin or draws its own screen.
    Suspend,
    /// Not inside the TUI. The reason tells the user what to do instead.
    Refuse(&'static str),
    /// `monitor`: opens the monitor wizard in the TUI (#47).
    Wizard,
}

/// The commands the TUI does not run, by name, and what to do instead.
/// `route` and the TUI help both read this list (#66).
pub(crate) const TUI_REFUSED: &[(&str, &str)] = &[
    ("interactive", "you are already in the TUI"),
    (
        "web",
        "the web server runs in the foreground; start it from a shell with `scope web`",
    ),
    (
        "completions",
        "it prints a script for your shell; run `scope completions <shell>` in a shell",
    ),
];

/// The refusal reason for the command `name`, from [`TUI_REFUSED`].
fn refused(name: &str) -> Route {
    let reason = TUI_REFUSED
        .iter()
        .find(|(n, _)| *n == name)
        .map_or("it does not run in the TUI", |(_, why)| why);
    Route::Refuse(reason)
}

/// Decides where a command runs.
///
/// The match has no wildcard arm on purpose: a new CLI command does not
/// compile until it has a TUI route (issue #47, requirement 5).
pub fn route(command: &Commands) -> Route {
    match command {
        Commands::Address(_)
        | Commands::Tx(_)
        | Commands::Insights(_)
        | Commands::Contract(_)
        | Commands::TokenHealth(_)
        | Commands::Discover(_)
        | Commands::Market(_)
        | Commands::Venues(_)
        | Commands::Compliance(_)
        | Commands::AddressBook(_)
        | Commands::Export(_)
        // A crawl by name or symbol asks which token in the pane (#109).
        | Commands::Crawl(_)
        | Commands::Report(_) => Route::Pane,
        // Only --status is free of prompts.
        Commands::Setup(args) => {
            if args.status {
                Route::Pane
            } else {
                Route::Suspend
            }
        }
        // The monitor wizard (#47). The old dashboard draws its own screen
        // until it goes (Q1).
        Commands::Monitor(args) => {
            if args.watch.dashboard {
                Route::Suspend
            } else {
                Route::Wizard
            }
        }
        Commands::Interactive(_) => refused("interactive"),
        Commands::Web(_) => refused("web"),
        Commands::Completions(_) => refused("completions"),
    }
}

/// A CLI command that the TUI will run.
#[derive(Debug)]
pub struct Invocation {
    /// The parsed command.
    pub command: Commands,
    /// Where it runs.
    pub route: Route,
    /// `--ai` was given: use markdown output for this command.
    pub ai: bool,
    /// The argument vector that clap parsed, after context injection.
    /// Recorded in the event log.
    pub argv: Vec<String>,
}

/// What the TUI must do with one line of input.
#[derive(Debug)]
pub enum Plan {
    /// Nothing to run: the line was empty, a session command, or an error
    /// that is already written to the output.
    Done,
    /// Leave the TUI.
    Exit,
    /// Run a CLI command. Boxed: `Commands` is large.
    Run(Box<Invocation>),
    /// A `watch` session command for the grid (#47).
    Watch(WatchCommand),
}

/// What the `watch` session command asks the TUI to do.
#[derive(Debug)]
pub enum WatchCommand {
    /// Start this watchlist in the grid.
    Open(Box<scope::watch::Watchlist>),
    /// Stop the running watchlist.
    Stop,
    /// Show the grid.
    Show,
}

/// Plans one line of input.
///
/// Session commands run here and write to `out`. Parse errors and `--help`
/// text are written to `out` too.
///
/// # Errors
///
/// Returns the output's error when a message cannot be written.
pub fn plan(
    line: &str,
    ctx: &mut SessionContext,
    config: &Config,
    out: &Output,
) -> io::Result<Plan> {
    let Some(words) = split_line(line) else {
        errln!(out, "Unbalanced quotes in: {}", line)?;
        return Ok(Plan::Done);
    };
    let Some(first) = words.first() else {
        return Ok(Plan::Done);
    };
    let rest: Vec<&str> = words[1..].iter().map(String::as_str).collect();

    match session_command_name(first).as_str() {
        "exit" => return Ok(Plan::Exit),
        // Hidden debug command for the manual crash-log check. It is not in
        // SESSION_COMMANDS, so help, completion and the palette skip it.
        #[cfg(debug_assertions)]
        "__panic" => panic!("__panic: manual crash-log check"),
        "help" => {
            if rest.is_empty() {
                write_help(out)?;
                return Ok(Plan::Done);
            }
            // A session command has no clap help: show its own line.
            let name = session_command_name(&words[1]);
            if let Some((_, usage, what)) = SESSION_COMMANDS.iter().find(|(n, _, _)| *n == name) {
                outln!(out, "  {:<34} {}", usage, what)?;
                return Ok(Plan::Done);
            }
            // `help market summary` is `market summary --help`: the same
            // text as `scope help market summary`.
            let mut words: Vec<String> = words[1..].to_vec();
            words.push("--help".to_string());
            return parse_cli(words, ctx, config, out);
        }
        "ctx" => {
            out!(out, "{}", ctx)?;
            return Ok(Plan::Done);
        }
        "reset" => {
            *ctx = SessionContext::default();
            outln!(out, "Context reset to defaults.")?;
            return Ok(Plan::Done);
        }
        "chain" => {
            set_chain(ctx, rest.first().copied(), out)?;
            return Ok(Plan::Done);
        }
        "format" => {
            set_format(ctx, rest.first().copied(), out)?;
            return Ok(Plan::Done);
        }
        "+tokens" => {
            ctx.include_tokens = !ctx.include_tokens;
            outln!(out, "Include tokens: {}", on_off(ctx.include_tokens))?;
            return Ok(Plan::Done);
        }
        "+txs" => {
            ctx.include_txs = !ctx.include_txs;
            outln!(out, "Include transactions: {}", on_off(ctx.include_txs))?;
            return Ok(Plan::Done);
        }
        "trace" => {
            ctx.trace = !ctx.trace;
            outln!(out, "Trace: {}", on_off(ctx.trace))?;
            return Ok(Plan::Done);
        }
        "decode" => {
            ctx.decode = !ctx.decode;
            outln!(out, "Decode: {}", on_off(ctx.decode))?;
            return Ok(Plan::Done);
        }
        "limit" => {
            set_limit(ctx, rest.first().copied(), out)?;
            return Ok(Plan::Done);
        }
        "tokens" => {
            tokens_command(&rest, &mut TokenAliases::load(), &TokenAliases::save, out)?;
            return Ok(Plan::Done);
        }
        "watch" => {
            return watch_command(
                &rest,
                &scope::paths::watchlists_dir(),
                &crate::cli::watch::venue_ids(),
                out,
            );
        }
        _ => {}
    }

    parse_cli(words, ctx, config, out)
}

/// The usage of the `watch` session command.
const WATCH_USAGE: &str = "Usage: watch [list | open <name|file.yaml> | stop]";

/// Runs the `watch` session command. `dir` holds the saved watchlists;
/// `venues` are the venue IDs a watchlist may name.
fn watch_command(
    rest: &[&str],
    dir: &std::path::Path,
    venues: &[String],
    out: &Output,
) -> io::Result<Plan> {
    use scope::watch::file;
    match rest {
        [] | ["show"] => Ok(Plan::Watch(WatchCommand::Show)),
        ["stop"] => Ok(Plan::Watch(WatchCommand::Stop)),
        ["list"] => {
            let names = file::list(dir);
            if names.is_empty() {
                outln!(out, "No saved watchlists in {}.", dir.display())?;
            } else {
                for name in names {
                    outln!(out, "  {}", name)?;
                }
            }
            Ok(Plan::Done)
        }
        ["open", name] => {
            let is_path = name.ends_with(&format!(".{}", file::EXTENSION))
                || name.contains(['/', std::path::MAIN_SEPARATOR]);
            let path = if is_path {
                std::path::PathBuf::from(name)
            } else {
                file::path_in(dir, name)
            };
            match file::load(&path, Some(venues)) {
                Ok(w) => Ok(Plan::Watch(WatchCommand::Open(Box::new(w)))),
                Err(errors) => {
                    errln!(out, "{} is not a valid watchlist:", path.display())?;
                    for e in errors.0 {
                        errln!(out, "  {}", e)?;
                    }
                    Ok(Plan::Done)
                }
            }
        }
        _ => {
            errln!(out, "{}", WATCH_USAGE)?;
            Ok(Plan::Done)
        }
    }
}

/// Parses a CLI command line with the clap definition, after it adds the
/// session context.
fn parse_cli(
    words: Vec<String>,
    ctx: &mut SessionContext,
    config: &Config,
    out: &Output,
) -> io::Result<Plan> {
    let mut argv = vec!["scope".to_string()];
    argv.extend(inject_context(words, ctx, config));

    let cli = match Cli::try_parse_from(&argv) {
        Ok(cli) => cli,
        // `address` / `tx` with no target reuse the last one, as in the REPL.
        Err(e) if e.kind() == ErrorKind::MissingRequiredArgument => match last_target(&argv, ctx) {
            Some(target) => {
                argv.push(target);
                match Cli::try_parse_from(&argv) {
                    Ok(cli) => cli,
                    Err(e) => return write_clap_error(&e, &argv, out),
                }
            }
            None => return write_clap_error(&e, &argv, out),
        },
        Err(e) => return write_clap_error(&e, &argv, out),
    };

    if let Some(path) = &cli.config {
        errln!(
            out,
            "--config {} has no effect inside the TUI. Restart with `scope --config {} tui`.",
            path.display(),
            path.display()
        )?;
        return Ok(Plan::Done);
    }

    let route = route(&cli.command);
    if let Route::Refuse(reason) = route {
        errln!(out, "Not available here: {}.", reason)?;
        return Ok(Plan::Done);
    }

    remember_target(&cli.command, ctx);
    Ok(Plan::Run(Box::new(Invocation {
        command: cli.command,
        route,
        ai: cli.ai,
        argv,
    })))
}

/// Writes a clap error. `--help` and `--version` go to the data channel.
///
/// For a usage error, clap's `--help` hint is replaced by the TUI form,
/// `run 'help <command>' for its options`.
fn write_clap_error(e: &clap::Error, argv: &[String], out: &Output) -> io::Result<Plan> {
    let text = e.render().ansi().to_string();
    match e.kind() {
        ErrorKind::DisplayHelp
        | ErrorKind::DisplayVersion
        | ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand => out!(out, "{}", text)?,
        _ => {
            for line in text.lines().filter(|l| !l.contains(CLAP_HELP_HINT)) {
                errln!(out, "{}", line)?;
            }
            errln!(out, "{}", tui_help_hint(argv))?;
        }
    }
    Ok(Plan::Done)
}

/// The start of clap's own hint line, which names the CLI `--help` flag.
const CLAP_HELP_HINT: &str = "For more information";

/// The TUI next step for a usage error: `help` for the command the user
/// typed. The command is the longest run of subcommand words in `argv`.
fn tui_help_hint(argv: &[String]) -> String {
    let root = Cli::command();
    let mut cmd = &root;
    let mut words = Vec::new();
    for w in argv.iter().skip(1).filter(|w| !w.starts_with('-')) {
        match cmd.find_subcommand(w) {
            Some(sub) => {
                words.push(w.as_str());
                cmd = sub;
            }
            None => break,
        }
    }
    if words.is_empty() {
        "run 'help <command>' for its options".to_string()
    } else {
        format!("run 'help {}' for its options", words.join(" "))
    }
}

/// One session-context value that becomes a CLI flag.
struct ContextFlag {
    /// The long flag name.
    long: &'static str,
    /// The value, or `None` for a boolean flag.
    value: Option<String>,
    /// Commands the flag applies to. `None` means every command that has it.
    only: Option<&'static [&'static str]>,
}

/// Adds the session context as flags, for the flags the user did not type.
///
/// A flag is added only when the target command defines it and accepts the
/// value. The command is found by walking the clap subcommand tree, so
/// aliases (`addr`, `token`) work.
fn inject_context(mut words: Vec<String>, ctx: &SessionContext, config: &Config) -> Vec<String> {
    let mut flags = Vec::new();
    if !ctx.is_auto_chain() {
        flags.push(ContextFlag {
            long: "chain",
            value: Some(ctx.chain.clone()),
            only: None,
        });
    }
    // Only a format the user chose differs from the configured default.
    if ctx.format != config.output.format
        && let Some(v) = ctx.format.to_possible_value()
    {
        flags.push(ContextFlag {
            long: "format",
            value: Some(v.get_name().to_string()),
            only: None,
        });
    }
    for (on, long) in [
        (ctx.include_tokens, "include-tokens"),
        (ctx.include_txs, "include-txs"),
        (ctx.trace, "trace"),
        (ctx.decode, "decode"),
    ] {
        if on {
            flags.push(ContextFlag {
                long,
                value: None,
                only: None,
            });
        }
    }
    // Other commands also have --limit, with other meanings and defaults.
    if ctx.limit != SessionContext::default().limit {
        flags.push(ContextFlag {
            long: "limit",
            value: Some(ctx.limit.to_string()),
            only: Some(&["address"]),
        });
    }
    if flags.is_empty() {
        return words;
    }

    let root = Cli::command();
    let mut cmd = &root;
    for w in &words {
        if w.starts_with('-') {
            continue;
        }
        match cmd.find_subcommand(w) {
            Some(sub) => cmd = sub,
            None => break,
        }
    }
    if std::ptr::eq(cmd, &root) {
        return words;
    }

    for flag in flags {
        if flag
            .only
            .is_some_and(|names| !names.contains(&cmd.get_name()))
        {
            continue;
        }
        let Some(arg) = cmd
            .get_arguments()
            .find(|a| a.get_long() == Some(flag.long))
        else {
            continue;
        };
        let typed = words.iter().any(|w| {
            w == &format!("--{}", flag.long)
                || w.starts_with(&format!("--{}=", flag.long))
                || arg
                    .get_short()
                    .is_some_and(|s| w.starts_with(&format!("-{}", s)) && !w.starts_with("--"))
        });
        if typed {
            continue;
        }
        if let Some(value) = &flag.value {
            let values = arg.get_possible_values();
            if !values.is_empty() && !values.iter().any(|p| p.matches(value, true)) {
                continue;
            }
            words.push(format!("--{}", flag.long));
            words.push(value.clone());
        } else {
            words.push(format!("--{}", flag.long));
        }
    }
    words
}

/// The last address or transaction to reuse when `address` / `tx` has no
/// target.
fn last_target(argv: &[String], ctx: &SessionContext) -> Option<String> {
    let root = Cli::command();
    let name = argv.get(1)?;
    let sub = root.find_subcommand(name)?;
    match sub.get_name() {
        "address" => ctx.last_address.clone(),
        "tx" => ctx.last_tx.clone(),
        _ => None,
    }
}

/// Records the target of `address` / `tx` for later reuse.
fn remember_target(command: &Commands, ctx: &mut SessionContext) {
    match command {
        Commands::Address(args) => ctx.last_address = Some(args.address.clone()),
        Commands::Tx(args) => ctx.last_tx = Some(args.hash.clone()),
        _ => {}
    }
}

fn on_off(b: bool) -> &'static str {
    if b { "on" } else { "off" }
}

fn set_chain(ctx: &mut SessionContext, arg: Option<&str>, out: &Output) -> io::Result<()> {
    match arg.map(str::to_lowercase) {
        None if ctx.is_auto_chain() => outln!(out, "Current chain: auto (inferred from input)"),
        None => outln!(out, "Current chain: {} (pinned)", ctx.chain),
        Some(c) if c == "auto" => {
            ctx.chain = "auto".into();
            outln!(out, "Chain set to auto — will infer from each input")
        }
        Some(c) if scope::chains::registry::client_chain_ids().contains(&c.as_str()) => {
            outln!(out, "Chain pinned to: {}", c)?;
            ctx.chain = c;
            Ok(())
        }
        Some(c) => errln!(
            out,
            "Unknown chain: {}. Valid chains: auto, {}",
            c,
            scope::chains::registry::client_chain_list()
        ),
    }
}

fn set_format(ctx: &mut SessionContext, arg: Option<&str>, out: &Output) -> io::Result<()> {
    let Some(arg) = arg else {
        return outln!(out, "Current format: {:?}", ctx.format);
    };
    match OutputFormat::from_str(arg, true) {
        Ok(f) => {
            ctx.format = f;
            outln!(out, "Format set to: {}", arg.to_lowercase())
        }
        Err(_) => {
            let names: Vec<String> = OutputFormat::value_variants()
                .iter()
                .filter_map(|v| v.to_possible_value().map(|p| p.get_name().to_string()))
                .collect();
            errln!(
                out,
                "Unknown format: {}. Valid formats: {}",
                arg,
                names.join(", ")
            )
        }
    }
}

fn set_limit(ctx: &mut SessionContext, arg: Option<&str>, out: &Output) -> io::Result<()> {
    match arg.map(str::parse::<u32>) {
        None => outln!(out, "Current limit: {}", ctx.limit),
        Some(Ok(n)) if n > 0 => {
            ctx.limit = n;
            outln!(out, "Limit set to: {}", n)
        }
        Some(_) => errln!(
            out,
            "Invalid limit: {}. Must be a positive integer.",
            arg.unwrap_or_default()
        ),
    }
}

/// Saves the token aliases. Injected so tests never write the user's file.
type SaveAliases<'a> = dyn Fn(&TokenAliases) -> scope::error::Result<()> + 'a;

/// The `tokens` session command: list, add and remove saved token aliases.
fn tokens_command(
    args: &[&str],
    aliases: &mut TokenAliases,
    save: &SaveAliases<'_>,
    out: &Output,
) -> io::Result<()> {
    match args.first().map(|s| s.to_lowercase()).as_deref() {
        None | Some("list") | Some("ls") => {
            let tokens = aliases.list();
            if tokens.is_empty() {
                outln!(
                    out,
                    "{}",
                    scope::display::terminal::info_row("No saved token aliases.")
                )?;
                return outln!(
                    out,
                    "{}",
                    scope::display::terminal::info_row(
                        "Use 'crawl <token_name> --save' to save a token alias."
                    )
                );
            }
            write_token_table(out, "Saved Token Aliases", &tokens)
        }
        Some("recent") => {
            let recent = aliases.recent();
            if recent.is_empty() {
                return outln!(
                    out,
                    "{}",
                    scope::display::terminal::info_row("No recently used tokens.")
                );
            }
            write_token_table(out, "Recently Used Tokens", recent)
        }
        Some("remove") | Some("rm") | Some("delete") => {
            let Some(symbol) = args.get(1) else {
                return errln!(out, "Usage: tokens remove <symbol> [--chain <chain>]");
            };
            let chain = match (args.get(2), args.get(3)) {
                (Some(&"--chain"), Some(c)) => Some(*c),
                _ => None,
            };
            aliases.remove(symbol, chain);
            match save(aliases) {
                Ok(()) => outln!(out, "Removed alias: {}", symbol),
                Err(e) => errln!(out, "Failed to save the token aliases: {}", e),
            }
        }
        Some("add") => {
            let (Some(symbol), Some(chain), Some(address)) =
                (args.get(1), args.get(2), args.get(3))
            else {
                return errln!(out, "Usage: tokens add <symbol> <chain> <address> [name]");
            };
            let name = if args.len() > 4 {
                args[4..].join(" ")
            } else {
                symbol.to_string()
            };
            aliases.add(symbol, chain, address, &name);
            match save(aliases) {
                Ok(()) => outln!(out, "Added alias: {} -> {} on {}", symbol, address, chain),
                Err(e) => errln!(out, "Failed to save the token aliases: {}", e),
            }
        }
        Some(other) => {
            errln!(out, "Unknown tokens subcommand: {}", other)?;
            errln!(out, "Available: list, recent, add, remove")
        }
    }
}

fn write_token_table<T>(out: &Output, title: &str, tokens: &[T]) -> io::Result<()>
where
    T: std::borrow::Borrow<scope::tokens::TokenInfo>,
{
    use scope::display::terminal as t;
    let col = |label, width| t::Col {
        label,
        width,
        align: '<',
    };
    let cols = &[
        col("Symbol", 10),
        col("Chain", 12),
        col("Name", 20),
        col("Address", 42),
    ];
    outln!(out, "{}", t::section_header(title))?;
    outln!(out, "{}", t::table_header(cols))?;
    for token in tokens.iter().map(std::borrow::Borrow::borrow) {
        outln!(
            out,
            "{}",
            t::table_row(
                cols,
                &[&token.symbol, &token.chain, &token.name, &token.address]
            )
        )?;
    }
    outln!(out, "{}", t::section_footer())
}

/// The session commands: (word, usage, description). Help, completion and
/// the palette read this list.
pub(crate) const SESSION_COMMANDS: &[(&str, &str, &str)] = &[
    ("chain", "chain [name|auto]", "Show, pin or unpin the chain"),
    (
        "format",
        "format [table|json|csv|markdown]",
        "Show or set the output format",
    ),
    ("+tokens", "+tokens", "Toggle token balances for address"),
    ("+txs", "+txs", "Toggle transactions for address"),
    ("trace", "trace", "Toggle trace for tx"),
    ("decode", "decode", "Toggle decode for tx"),
    (
        "limit",
        "limit [n]",
        "Show or set the transaction limit for address",
    ),
    (
        "tokens",
        "tokens [list|recent|add|remove]",
        "Manage saved token aliases",
    ),
    (
        "watch",
        "watch [list|open <name>|stop]",
        "Run a saved watchlist in the grid (Ctrl-G shows it)",
    ),
    (
        "save",
        "save <file> [--force]",
        "Write the last output to a file",
    ),
    ("ctx", "ctx", "Show the session context"),
    ("clear", "clear", "Clear the output pane"),
    ("reset", "reset", "Reset the session context"),
    ("help", "help", "Show this help"),
    ("exit", "exit", "Leave the TUI (also Ctrl-D)"),
];

/// Other words for the session commands: `(alias, SESSION_COMMANDS name)`.
/// The one list: dispatch, help and the exit check all read it.
const SESSION_ALIASES: &[(&str, &str)] = &[
    ("quit", "exit"),
    ("q", "exit"),
    (".exit", "exit"),
    (".quit", "exit"),
    ("context", "ctx"),
    (".ctx", "ctx"),
    (".context", "ctx"),
    (".clear", "clear"),
    (".reset", "reset"),
    ("?", "help"),
    (".help", "help"),
    ("showtokens", "+tokens"),
    ("showtxs", "+txs"),
    ("txs", "+txs"),
    ("aliases", "tokens"),
];

/// The `SESSION_COMMANDS` name for a typed word (any case), or the word
/// itself when it is not an alias.
/// Splits a line of input into words; `None` when a quote is not closed.
///
/// On unix the rules are the shell's (shlex). On Windows a backslash is
/// a path separator, so `save C:\Users\me\out.json` keeps its path; see
/// [`split_backslash_literal`].
pub(crate) fn split_line(line: &str) -> Option<Vec<String>> {
    if cfg!(windows) {
        split_backslash_literal(line)
    } else {
        shlex::split(line)
    }
}

/// Splits `line` at whitespace outside quotes. Single and double quotes
/// group words and are removed; a backslash is an ordinary character, so
/// a quote cannot be escaped (quote it with the other kind). `None` when a
/// quote is not closed.
fn split_backslash_literal(line: &str) -> Option<Vec<String>> {
    let mut words = Vec::new();
    let mut word: Option<String> = None;
    let mut quote: Option<char> = None;
    for c in line.chars() {
        match quote {
            Some(q) if c == q => quote = None,
            Some(_) => word.get_or_insert_default().push(c),
            None if c == '"' || c == '\'' => {
                quote = Some(c);
                word.get_or_insert_default();
            }
            None if c.is_whitespace() => words.extend(word.take()),
            None => word.get_or_insert_default().push(c),
        }
    }
    if quote.is_some() {
        return None;
    }
    words.extend(word);
    Some(words)
}

pub(crate) fn session_command_name(word: &str) -> String {
    let word = word.to_lowercase();
    SESSION_ALIASES
        .iter()
        .find(|(alias, _)| *alias == word)
        .map_or(word, |(_, name)| (*name).to_string())
}

/// Writes the TUI help: the session commands, then every CLI command.
fn write_help(out: &Output) -> io::Result<()> {
    outln!(out, "Session commands:")?;
    for (name, usage, what) in SESSION_COMMANDS {
        // Name the other words for the command too (#66: `quit`, `q`).
        let aliases: Vec<&str> = SESSION_ALIASES
            .iter()
            .filter(|(_, n)| n == name)
            .map(|(alias, _)| *alias)
            .filter(|alias| !alias.starts_with('.'))
            .collect();
        if aliases.is_empty() {
            outln!(out, "  {:<34} {}", usage, what)?;
        } else {
            outln!(
                out,
                "  {:<34} {} (also: {})",
                usage,
                what,
                aliases.join(", ")
            )?;
        }
    }
    outln!(out)?;
    outln!(
        out,
        "CLI commands (same flags as on the command line; add --help for details):"
    )?;
    for sub in Cli::command().get_subcommands() {
        let refused = TUI_REFUSED.iter().any(|(n, _)| *n == sub.get_name());
        if sub.is_hide_set() || refused {
            continue;
        }
        let about = sub.get_about().map(|a| a.to_string()).unwrap_or_default();
        outln!(out, "  {:<34} {}", sub.get_name(), about)?;
    }
    outln!(out)?;
    // Key names in the key color, what they do dim. The ANSI becomes styles
    // in the pane; the copy and the log strip it.
    let painter = scope::display::theme::Painter::new(true);
    let key = |k: &str| painter.paint(scope::display::theme::Role::Key, k);
    let what = |w: &str| painter.paint(scope::display::theme::Role::Secondary, w);
    outln!(
        out,
        "Keys: {} {} · {} {} · {} {} · {} {} · {} {} · {} {}",
        key("Enter"),
        what("run"),
        key("Tab"),
        what("complete"),
        key("Ctrl-K"),
        what("palette"),
        key("↑/↓"),
        what("history"),
        key("PgUp/PgDn"),
        what("scroll"),
        format_args!("{} or {}", key("Esc"), key("Ctrl-C")),
        what("cancel"),
    )?;
    outln!(
        out,
        "      {} {} · {} or {} {} · {} {} · {} {}",
        key("Ctrl-Y"),
        what("copy last output"),
        key("Ctrl-F"),
        key("/"),
        what("search"),
        key("Ctrl-R"),
        what("history search"),
        key("Shift-drag"),
        what("selects text"),
    )?;
    outln!(
        out,
        "      {}",
        what("Copy needs a terminal that allows OSC 52; save <file> works everywhere")
    )?;
    outln!(
        out,
        "      {}",
        what(
            "A command with --every shows one live view: the latest report and its trends. After it ends, any key leaves the view; save <file>.csv writes the series."
        )
    )
}

// ============================================================================
// Unit Tests
// ============================================================================

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

    const ADDR: &str = "0x742d35Cc6634C0532925a3b844Bc9e7595f1b3c2";
    const TX: &str = "0xabc123def456789012345678901234567890123456789012345678901234abcd";

    #[test]
    fn test_backslash_literal_split_keeps_windows_paths() {
        let split = |s: &str| split_backslash_literal(s).unwrap();
        assert_eq!(
            split(r"save C:\Users\x\f.json"),
            ["save", r"C:\Users\x\f.json"]
        );
        assert_eq!(
            split(r#"save "C:\My Files\f.json""#),
            ["save", r"C:\My Files\f.json"]
        );
        assert_eq!(split(r"a 'b c'd  e"), ["a", "b cd", "e"]);
        assert_eq!(split(r#"x "" y"#), ["x", "", "y"]);
        assert_eq!(split("   "), Vec::<String>::new());
        assert_eq!(split_backslash_literal(r#"save "C:\x"#), None);
    }

    #[test]
    fn test_split_line_uses_the_platform_rules() {
        let words = split_line(r"save C:\x\f.json").unwrap();
        if cfg!(windows) {
            assert_eq!(words, ["save", r"C:\x\f.json"]);
        } else {
            assert_eq!(words, ["save", "C:xf.json"]);
        }
    }

    fn plan_line(line: &str, ctx: &mut SessionContext) -> (Plan, crate::cli::output::Captured) {
        let (o, cap) = Output::capture();
        let p = plan(line, ctx, &Config::default(), &o).unwrap();
        (p, cap)
    }

    fn run_of(p: Plan) -> Invocation {
        match p {
            Plan::Run(inv) => *inv,
            other => panic!("expected Plan::Run, got {:?}", other),
        }
    }

    // ---- Parity (issue #47, requirement 5) ----

    #[test]
    fn test_every_cli_command_parses_and_has_a_tui_route() {
        // Every CLI subcommand must reach a handler from the TUI, except
        // the ones TUI_REFUSED lists. A new command fails here (and in
        // `route`, at compile time) until it has a TUI path.
        let refused: Vec<&str> = TUI_REFUSED.iter().map(|(n, _)| *n).collect();
        for sub in Cli::command().get_subcommands() {
            let name = sub.get_name();
            let (o, cap) = Output::capture();
            let mut ctx = SessionContext::default();
            // `--help` parses for every command without its required args.
            let p = plan(
                &format!("{} --help", name),
                &mut ctx,
                &Config::default(),
                &o,
            )
            .unwrap();
            assert!(matches!(p, Plan::Done), "{}", name);
            assert!(cap.out().contains("Usage"), "no help for {}", name);

            let mut subcmds = sub.get_subcommands().map(|s| s.get_name()).peekable();
            let leaf_names: Vec<String> = if subcmds.peek().is_some() {
                subcmds.map(|s| format!("{} {}", name, s)).collect()
            } else {
                vec![name.to_string()]
            };
            for leaf in leaf_names {
                let line = minimal_line(&leaf);
                let (o, cap) = Output::capture();
                let p = plan(&line, &mut ctx, &Config::default(), &o).unwrap();
                if refused.contains(&name) {
                    assert!(matches!(p, Plan::Done), "{} must be refused", leaf);
                    assert!(cap.err().contains("Not available here"), "{}", cap.err());
                } else {
                    // A refused route never becomes Plan::Run.
                    assert!(
                        matches!(p, Plan::Run(_)),
                        "`{}` did not parse: {}",
                        line,
                        cap.err()
                    );
                }
            }
        }
    }

    /// The smallest valid command line for a leaf command.
    fn minimal_line(leaf: &str) -> String {
        match leaf {
            "address" | "insights" | "contract" => format!("{} {}", leaf, ADDR),
            "tx" => format!("tx {}", TX),
            "crawl" | "token-health" | "monitor" => format!("{} USDC", leaf),
            "compliance risk" | "compliance trace" | "compliance analyze" => {
                format!("{} {}", leaf, ADDR)
            }
            "compliance compliance-report" => {
                format!("{} {} --output r.md --jurisdiction us", leaf, ADDR)
            }
            "address-book add" | "address-book remove" => format!("{} {}", leaf, ADDR),
            "venues validate" => format!("{} venue.yaml", leaf),
            "market summary" | "market ohlc" | "market trades" => format!("{} USDC", leaf),
            "report batch" => format!("{} --addresses {} --output r.md", leaf, ADDR),
            "export" => format!("export --address {} --output out.json", ADDR),
            "completions" => "completions bash".into(),
            other => other.to_string(),
        }
    }

    #[test]
    fn test_clap_error_gives_tui_next_steps_not_the_cli_hint() {
        let (_, cap) = plan_line("market summary --bogus", &mut SessionContext::default());
        let text = cap.err();
        assert!(!text.contains("try '--help'"), "{text}");
        assert!(text.contains("help market summary"), "{text}");
    }

    // ---- Session commands ----

    #[test]
    fn test_exit_words() {
        for w in ["exit", "quit", "q", ".exit", ".quit", "EXIT"] {
            assert!(matches!(
                plan_line(w, &mut SessionContext::default()).0,
                Plan::Exit
            ));
        }
    }

    #[test]
    fn test_empty_line_does_nothing() {
        let (p, cap) = plan_line("   ", &mut SessionContext::default());
        assert!(matches!(p, Plan::Done));
        assert_eq!(cap.out() + &cap.err(), "");
    }

    #[test]
    fn test_chain_pin_and_unpin() {
        let mut ctx = SessionContext::default();
        plan_line("chain solana", &mut ctx);
        assert_eq!(ctx.chain, "solana");
        plan_line("chain auto", &mut ctx);
        assert!(ctx.is_auto_chain());
        let (_, cap) = plan_line("chain notachain", &mut ctx);
        assert!(ctx.is_auto_chain());
        assert!(cap.err().contains("Unknown chain"));
    }

    #[test]
    fn test_format_set_and_reject() {
        let mut ctx = SessionContext::default();
        plan_line("format json", &mut ctx);
        assert_eq!(ctx.format, OutputFormat::Json);
        let (_, cap) = plan_line("format xml", &mut ctx);
        assert_eq!(ctx.format, OutputFormat::Json);
        assert!(cap.err().contains("Unknown format"));
    }

    #[test]
    fn test_toggles_limit_and_clear() {
        let mut ctx = SessionContext::default();
        for w in ["+tokens", "+txs", "trace", "decode"] {
            plan_line(w, &mut ctx);
        }
        assert!(ctx.include_tokens && ctx.include_txs && ctx.trace && ctx.decode);
        plan_line("limit 25", &mut ctx);
        assert_eq!(ctx.limit, 25);
        let (_, cap) = plan_line("limit 0", &mut ctx);
        assert_eq!(ctx.limit, 25);
        assert!(cap.err().contains("Invalid limit"));
        plan_line("reset", &mut ctx);
        assert_eq!(ctx.limit, SessionContext::default().limit);
        assert!(!ctx.include_tokens);
    }

    #[test]
    fn test_bare_session_commands_show_current_values() {
        let mut ctx = SessionContext::default();
        let (_, cap) = plan_line("chain", &mut ctx);
        assert!(cap.out().contains("Current chain: auto"));
        plan_line("chain tron", &mut ctx);
        let (_, cap) = plan_line("chain", &mut ctx);
        assert!(cap.out().contains("Current chain: tron (pinned)"));
        let (_, cap) = plan_line("format", &mut ctx);
        assert!(cap.out().contains("Current format: Table"));
        let (_, cap) = plan_line("limit", &mut ctx);
        assert!(cap.out().contains("Current limit: 100"));
    }

    #[test]
    fn test_tokens_session_command_is_routed_not_parsed_by_clap() {
        // `tokens` is not a CLI command; it must reach the alias manager.
        // An unknown subcommand neither reads nor writes anything to save.
        let (p, cap) = plan_line("tokens frobnicate", &mut SessionContext::default());
        assert!(matches!(p, Plan::Done));
        assert!(cap.err().contains("Unknown tokens subcommand: frobnicate"));
    }

    #[test]
    fn test_missing_target_on_other_commands_is_not_filled_from_context() {
        // Only address and tx reuse the last target; crawl must still fail.
        let mut ctx = SessionContext {
            last_address: Some(ADDR.into()),
            ..Default::default()
        };
        let (p, cap) = plan_line("crawl", &mut ctx);
        assert!(matches!(p, Plan::Done));
        assert!(cap.err().contains("required"));
    }

    #[test]
    fn test_context_is_not_injected_before_a_subcommand_is_named() {
        // `--ai` alone names no command: nothing may be appended to it.
        let mut ctx = SessionContext {
            chain: "base".into(),
            ..Default::default()
        };
        let (p, cap) = plan_line("--ai", &mut ctx);
        assert!(matches!(p, Plan::Done));
        assert!(!cap.err().contains("--chain"), "{}", cap.err());
    }

    #[test]
    fn test_ctx_prints_context() {
        let mut ctx = SessionContext::default();
        plan_line("chain base", &mut ctx);
        let (_, cap) = plan_line("ctx", &mut ctx);
        assert!(cap.out().contains("base (pinned)"));
    }

    #[test]
    fn test_help_lists_session_and_cli_commands() {
        let (_, cap) = plan_line("help", &mut SessionContext::default());
        for word in ["+tokens", "address", "market", "compliance", "venues"] {
            assert!(cap.out().contains(word), "help is missing {}", word);
        }
    }

    #[test]
    fn test_help_lists_the_search_keys() {
        let (_, cap) = plan_line("help", &mut SessionContext::default());
        let help = crate::cli::ansi::strip(&cap.out());
        for keys in ["Ctrl-F or / search", "Ctrl-R history search"] {
            assert!(help.contains(keys), "help is missing {}", keys);
        }
    }

    #[test]
    fn test_help_with_a_command_shows_that_commands_help() {
        let (p, cap) = plan_line("help market summary", &mut SessionContext::default());
        assert!(matches!(p, Plan::Done));
        assert!(cap.out().contains("Usage:"), "{}", cap.out());
        assert!(cap.out().contains("--venue"), "{}", cap.out());
        assert!(!cap.out().contains("Session commands"), "{}", cap.out());
    }

    #[test]
    fn test_help_with_a_session_command_shows_its_line() {
        for (typed, usage) in [
            ("help ctx", "ctx"),
            ("help tokens", "tokens [list|recent|add|remove]"),
            ("help clear", "Clear the output pane"),
            ("help reset", "Reset the session context"),
            ("help save", "save <file> [--force]"),
            ("help quit", "exit"),
        ] {
            let (p, cap) = plan_line(typed, &mut SessionContext::default());
            assert!(matches!(p, Plan::Done));
            assert!(cap.out().contains(usage), "{typed}: {}", cap.out());
            assert!(cap.err().is_empty(), "{typed}: {}", cap.err());
            assert!(!cap.out().contains("Usage:"), "{typed}");
        }
    }

    #[test]
    fn test_help_with_an_unknown_command_is_a_clap_error() {
        let (p, cap) = plan_line("help nosuchcmd", &mut SessionContext::default());
        assert!(matches!(p, Plan::Done));
        assert!(cap.err().contains("nosuchcmd"), "{}", cap.err());
        assert!(cap.out().is_empty(), "{}", cap.out());
    }

    // ---- tokens ----

    /// Runs `tokens …` against an in-memory store; returns output and how
    /// many times it saved.
    fn tokens(line: &str, aliases: &mut TokenAliases, fail_save: bool) -> (String, String, usize) {
        let saves = std::cell::Cell::new(0);
        let save = |_: &TokenAliases| {
            saves.set(saves.get() + 1);
            if fail_save {
                Err(scope::error::ScopeError::IoError(std::io::Error::other(
                    "disk full",
                )))
            } else {
                Ok(())
            }
        };
        let (o, cap) = Output::capture();
        let args: Vec<&str> = line.split_whitespace().collect();
        tokens_command(&args, aliases, &save, &o).unwrap();
        (cap.out(), cap.err(), saves.get())
    }

    #[test]
    fn test_tokens_add_list_recent_remove() {
        let mut a = TokenAliases::default();
        let (out, _, saves) = tokens(
            &format!("add USDN ethereum {} Nova USD", ADDR),
            &mut a,
            false,
        );
        assert!(out.contains("Added alias: USDN"));
        assert_eq!(saves, 1);
        assert_eq!(a.get("USDN", None).unwrap().name, "Nova USD");

        for cmd in ["", "list", "ls"] {
            let (out, _, _) = tokens(cmd, &mut a, false);
            assert!(
                out.contains("Saved Token Aliases") && out.contains("USDN"),
                "{}",
                cmd
            );
        }
        let (out, _, _) = tokens("recent", &mut a, false);
        assert!(out.contains("No recently used tokens") || out.contains("USDN"));

        let (out, _, saves) = tokens("rm USDN --chain ethereum", &mut a, false);
        assert!(out.contains("Removed alias: USDN"));
        assert_eq!(saves, 1);
        assert!(a.get("USDN", None).is_none());
        let (out, _, _) = tokens("list", &mut a, false);
        assert!(out.contains("No saved token aliases"));
    }

    #[test]
    fn test_tokens_usage_errors_do_not_save() {
        let mut a = TokenAliases::default();
        for line in ["add USDN ethereum", "remove", "frobnicate"] {
            let (out, err, saves) = tokens(line, &mut a, false);
            assert_eq!(out, "", "{}", line);
            assert!(!err.is_empty(), "{}", line);
            assert_eq!(saves, 0, "{}", line);
        }
    }

    #[test]
    fn test_tokens_save_failure_is_reported() {
        let mut a = TokenAliases::default();
        let (out, err, _) = tokens(&format!("add USDN ethereum {}", ADDR), &mut a, true);
        assert!(!out.contains("Added"));
        assert!(
            err.contains("Failed to save the token aliases: "),
            "{}",
            err
        );
        assert!(err.contains("disk full"));
    }

    #[test]
    fn test_tokens_recent_and_remove_report_empty_and_failed_saves() {
        let mut a = TokenAliases::default();
        let (out, _, saves) = tokens("recent", &mut a, false);
        assert!(out.contains("No recently used tokens"), "{out}");
        assert_eq!(saves, 0, "listing never writes the file");
        let (out, _, _) = tokens("list", &mut a, false);
        assert!(out.contains("Use 'crawl <token_name> --save'"), "{out}");
        // A failed save after a remove must be loud, not "Removed".
        tokens(&format!("add USDN ethereum {}", ADDR), &mut a, false);
        let (out, err, _) = tokens("remove USDN", &mut a, true);
        assert!(!out.contains("Removed"), "{out}");
        assert!(err.contains("Failed to save the token aliases: "), "{err}");
    }

    // ---- CLI parsing and context injection ----

    #[test]
    fn test_cli_command_parses_with_aliases() {
        let inv = run_of(plan_line(&format!("addr {}", ADDR), &mut SessionContext::default()).0);
        assert!(matches!(inv.command, Commands::Address(_)));
        assert_eq!(inv.route, Route::Pane);
    }

    #[test]
    fn test_pinned_chain_is_injected_but_typed_flag_wins() {
        let mut ctx = SessionContext {
            chain: "polygon".into(),
            ..Default::default()
        };
        let inv = run_of(plan_line(&format!("address {}", ADDR), &mut ctx).0);
        let Commands::Address(a) = inv.command else {
            panic!()
        };
        assert_eq!(a.chain, "polygon");

        let inv = run_of(plan_line(&format!("address {} -c base", ADDR), &mut ctx).0);
        let Commands::Address(a) = inv.command else {
            panic!()
        };
        assert_eq!(a.chain, "base");
    }

    #[test]
    fn test_auto_chain_injects_nothing() {
        // The CLI infers the chain from the address; the TUI must not
        // override that with a default.
        let inv = run_of(plan_line(&format!("address {}", ADDR), &mut SessionContext::default()).0);
        assert_eq!(inv.argv, vec!["scope", "address", ADDR]);
    }

    #[test]
    fn test_context_flags_apply_only_where_defined() {
        let mut ctx = SessionContext {
            include_tokens: true,
            trace: true,
            limit: 7,
            ..Default::default()
        };
        let inv = run_of(plan_line(&format!("address {}", ADDR), &mut ctx).0);
        let Commands::Address(a) = inv.command else {
            panic!()
        };
        assert!(a.include_tokens);
        assert_eq!(a.limit, 7);
        // `address` has no --trace; it must not be injected there.
        assert!(!inv.argv.contains(&"--trace".to_string()));

        let inv = run_of(plan_line(&format!("tx {}", TX), &mut ctx).0);
        let Commands::Tx(t) = inv.command else {
            panic!()
        };
        assert!(t.trace);
        assert!(!inv.argv.contains(&"--limit".to_string()));
    }

    #[test]
    fn test_format_injected_only_where_value_is_accepted() {
        let mut ctx = SessionContext {
            format: OutputFormat::Csv,
            ..Default::default()
        };
        let inv = run_of(plan_line(&format!("address {}", ADDR), &mut ctx).0);
        let Commands::Address(a) = inv.command else {
            panic!()
        };
        assert_eq!(a.format, Some(OutputFormat::Csv));
        // market summary accepts text|json only: csv must not break it.
        let inv = run_of(plan_line("market summary USDC", &mut ctx).0);
        assert!(!inv.argv.contains(&"csv".to_string()));
    }

    #[test]
    fn test_last_address_and_tx_are_reused() {
        let mut ctx = SessionContext::default();
        run_of(plan_line(&format!("address {}", ADDR), &mut ctx).0);
        let inv = run_of(plan_line("address", &mut ctx).0);
        let Commands::Address(a) = inv.command else {
            panic!()
        };
        assert_eq!(a.address, ADDR);

        run_of(plan_line(&format!("tx {}", TX), &mut ctx).0);
        let inv = run_of(plan_line("tx", &mut ctx).0);
        let Commands::Tx(t) = inv.command else {
            panic!()
        };
        assert_eq!(t.hash, TX);
    }

    #[test]
    fn test_missing_target_without_history_is_a_clap_error() {
        let (p, cap) = plan_line("address", &mut SessionContext::default());
        assert!(matches!(p, Plan::Done));
        assert!(cap.err().contains("required"), "{}", cap.err());
    }

    #[test]
    fn test_unknown_command_suggests_like_the_cli() {
        let (p, cap) = plan_line("adress 0x1", &mut SessionContext::default());
        assert!(matches!(p, Plan::Done));
        assert!(cap.err().contains("address"), "{}", cap.err());
    }

    #[test]
    fn test_quoted_arguments_and_unbalanced_quotes() {
        let inv = run_of(
            plan_line(
                &format!("address-book add {} --label \"cold wallet\"", ADDR),
                &mut SessionContext::default(),
            )
            .0,
        );
        assert!(inv.argv.contains(&"cold wallet".to_string()));
        let (p, cap) = plan_line("address \"0x1", &mut SessionContext::default());
        assert!(matches!(p, Plan::Done));
        assert!(cap.err().contains("Unbalanced quotes"));
    }

    #[test]
    fn test_ai_flag_and_config_flag() {
        let inv = run_of(
            plan_line(
                &format!("--ai address {}", ADDR),
                &mut SessionContext::default(),
            )
            .0,
        );
        assert!(inv.ai);
        let (p, cap) = plan_line(
            &format!("--config x.yaml address {}", ADDR),
            &mut SessionContext::default(),
        );
        assert!(matches!(p, Plan::Done));
        assert!(cap.err().contains("no effect inside the TUI"));
    }

    // ---- watch ----

    fn watch(rest: &[&str], dir: &std::path::Path) -> (Plan, String) {
        let (o, cap) = Output::capture();
        let venues = vec!["binance".to_string()];
        let p = watch_command(rest, dir, &venues, &o).unwrap();
        (p, format!("{}{}", cap.out(), cap.err()))
    }

    #[test]
    fn test_watch_list_open_stop_and_usage() {
        let dir = tempfile::tempdir().unwrap();
        let (p, text) = watch(&["list"], dir.path());
        assert!(matches!(p, Plan::Done));
        assert!(text.contains("No saved watchlists"), "{text}");

        std::fs::write(
            dir.path().join("stables.yaml"),
            "targets:\n  - kind: pair\n    id: USDC\n    venue: binance\n    preset: market health\n",
        )
        .unwrap();
        let (_, text) = watch(&["list"], dir.path());
        assert!(text.contains("  stables"), "{text}");
        let (p, _) = watch(&["open", "stables"], dir.path());
        let Plan::Watch(WatchCommand::Open(w)) = p else {
            panic!("expected open, got {p:?}")
        };
        assert_eq!(w.name, "stables");
        assert_eq!(w.targets.len(), 1);

        assert!(matches!(
            watch(&["stop"], dir.path()).0,
            Plan::Watch(WatchCommand::Stop)
        ));
        assert!(matches!(
            watch(&[], dir.path()).0,
            Plan::Watch(WatchCommand::Show)
        ));
        let (p, text) = watch(&["nonsense"], dir.path());
        assert!(matches!(p, Plan::Done));
        assert!(text.contains(WATCH_USAGE), "{text}");
    }

    #[test]
    fn test_watch_open_of_a_bad_file_lists_every_error_and_starts_nothing() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("bad.yaml");
        std::fs::write(
            &path,
            "targets:\n  - kind: pair\n    id: USDC\n    venue: nowhere\n    rules: [\"holdrs > 1\"]\n",
        )
        .unwrap();
        let (p, text) = watch(&["open", path.to_str().unwrap()], dir.path());
        assert!(matches!(p, Plan::Done));
        assert!(text.contains("is not a valid watchlist"), "{text}");
        assert!(text.contains("unknown venue `nowhere`"), "{text}");
        assert!(text.contains("unknown metric `holdrs`"), "{text}");
        let (p, text) = watch(&["open", "missing"], dir.path());
        assert!(matches!(p, Plan::Done));
        assert!(text.contains("cannot read"), "{text}");
    }

    #[test]
    fn test_watch_is_a_session_command_in_help() {
        assert!(SESSION_COMMANDS.iter().any(|(n, _, _)| *n == "watch"));
    }

    // ---- Routing ----

    #[test]
    fn test_routes_for_prompting_commands() {
        let mut ctx = SessionContext::default();
        assert_eq!(
            run_of(plan_line("crawl USDC", &mut ctx).0).route,
            Route::Pane
        );
        assert_eq!(
            run_of(plan_line("crawl USDC --yes", &mut ctx).0).route,
            Route::Pane
        );
        assert_eq!(
            run_of(plan_line(&format!("crawl {}", ADDR), &mut ctx).0).route,
            Route::Pane
        );
        assert_eq!(run_of(plan_line("setup", &mut ctx).0).route, Route::Suspend);
        assert_eq!(
            run_of(plan_line("setup --status", &mut ctx).0).route,
            Route::Pane
        );
        // The monitor wizard (#47); the old dashboard still runs outside.
        assert_eq!(
            run_of(plan_line("monitor USDC", &mut ctx).0).route,
            Route::Wizard
        );
        assert_eq!(
            run_of(plan_line("monitor USDC --dashboard", &mut ctx).0).route,
            Route::Suspend
        );
    }

    #[test]
    fn test_monitor_never_gets_the_auto_chain() {
        // The REPL passed SessionContext.chain ("auto") straight to the
        // monitor, which has no "auto" handling. The TUI parses `monitor`
        // with clap: an auto context adds no --chain, so clap's default
        // applies; a pinned chain is passed as given.
        let inv = run_of(plan_line("monitor USDC", &mut SessionContext::default()).0);
        let Commands::Monitor(m) = inv.command else {
            panic!("expected monitor")
        };
        assert_eq!(m.chain, "ethereum");

        let mut ctx = SessionContext {
            chain: "solana".into(),
            ..Default::default()
        };
        let inv = run_of(plan_line("monitor USDC", &mut ctx).0);
        let Commands::Monitor(m) = inv.command else {
            panic!("expected monitor")
        };
        assert_eq!(m.chain, "solana");
    }

    #[test]
    fn test_hidden_panic_command_is_not_listed() {
        assert!(
            SESSION_COMMANDS
                .iter()
                .all(|(n, u, _)| *n != "__panic" && !u.contains("__panic"))
        );
    }

    #[test]
    fn test_nested_tui_and_web_are_refused() {
        for line in ["interactive", "tui", "shell", "web"] {
            let (p, cap) = plan_line(line, &mut SessionContext::default());
            assert!(matches!(p, Plan::Done), "{}", line);
            assert!(cap.err().contains("Not available here"), "{}", line);
        }
    }
}