kobectl 0.38.0

kobe — CLI for the kobe cluster-pool operator: lease, inspect and manage instant CI/dev Kubernetes clusters
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
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::io::Read;
use std::path::PathBuf;

use super::session;
use super::{OutputFormat, print_json};

/// Where a target definition lives. Computed during `CliConfig::load`
/// based on which file each target appears in. Not serialized — pure
/// runtime metadata for `kobe config list` / `current` UX.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Scope {
    /// Defined only in the global config (`~/.config/kobe/config.json`).
    Global,
    /// Defined only in the local project config (`./.kobe.toml`).
    Local,
    /// Defined in BOTH global and local. The local definition wins
    /// when resolving (overlay order). `kobe config list` flags these
    /// so users see the conflict instead of being silently surprised.
    Both,
}

impl std::fmt::Display for Scope {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Scope::Global => write!(f, "global"),
            Scope::Local => write!(f, "local"),
            Scope::Both => write!(f, "both"),
        }
    }
}

/// Authentication mode.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum AuthMode {
    /// No authentication (local dev / port-forward)
    None,
    /// Static bearer token
    Token,
    /// OIDC browser login (default)
    #[default]
    Oidc,
    /// SSH key signing (SSHSIG)
    Ssh,
}

impl std::fmt::Display for AuthMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AuthMode::None => write!(f, "none"),
            AuthMode::Token => write!(f, "token"),
            AuthMode::Oidc => write!(f, "oidc"),
            AuthMode::Ssh => write!(f, "ssh"),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KobeTarget {
    /// Kobe API endpoint.
    pub endpoint: String,

    /// Authentication mode.
    #[serde(default)]
    pub auth: AuthMode,

    /// Static bearer token (when auth = token).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub token: Option<String>,

    /// SSH key fingerprint (when auth = ssh). If None, first Ed25519 key is used.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ssh_fingerprint: Option<String>,
}

#[derive(Debug, Clone)]
pub struct ResolvedConfig {
    pub target: Option<String>,
    pub endpoint: String,
    pub auth: AuthMode,
    pub token: Option<String>,
    pub ssh_fingerprint: Option<String>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct ConfigLegacyOutput {
    endpoint: Option<String>,
    auth: AuthMode,
    #[serde(skip_serializing_if = "Option::is_none")]
    token: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    ssh_fingerprint: Option<String>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct ConfigTargetOutput {
    endpoint: String,
    auth: AuthMode,
    #[serde(skip_serializing_if = "Option::is_none")]
    token: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    ssh_fingerprint: Option<String>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct ConfigViewOutput {
    path: String,
    exists: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    resolved: Option<ResolvedConfigOutput>,
    #[serde(skip_serializing_if = "Option::is_none")]
    current_target: Option<String>,
    targets: BTreeMap<String, ConfigTargetOutput>,
    #[serde(skip_serializing_if = "Option::is_none")]
    legacy: Option<ConfigLegacyOutput>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct ResolvedConfigOutput {
    #[serde(skip_serializing_if = "Option::is_none")]
    target: Option<String>,
    endpoint: String,
    auth: AuthMode,
    #[serde(skip_serializing_if = "Option::is_none")]
    token: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    ssh_fingerprint: Option<String>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct ConfigListEntry<'a> {
    name: &'a str,
    current: bool,
    endpoint: &'a str,
    auth: &'a AuthMode,
    /// Where this target is defined (global / local / both). Pre-1.0
    /// shape — clients that parse this JSON should expect the field to
    /// be present from v0.12 onward.
    scope: Scope,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct TargetMutationOutput<'a> {
    name: &'a str,
    current: bool,
}

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct CliConfig {
    /// Current named target. **Legacy** — historically lived alongside
    /// the targets map; today the per-shell session file is the source
    /// of truth (see `session.rs`). We still parse this field for
    /// backward compat with old configs and the `kobe config import`
    /// payload, but writes go to the session file instead.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        alias = "current_context"
    )]
    pub current_target: Option<String>,

    /// Named endpoint/auth configurations.
    #[serde(
        default,
        skip_serializing_if = "BTreeMap::is_empty",
        alias = "contexts"
    )]
    pub targets: BTreeMap<String, KobeTarget>,

    /// Per-target scope (Global/Local/Both). Populated during
    /// `load()` and used by `config list` / `config current`. Not
    /// serialized — pure runtime metadata.
    #[serde(skip)]
    pub target_scopes: BTreeMap<String, Scope>,

    /// Kobe API endpoint.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub endpoint: Option<String>,

    /// Authentication mode.
    #[serde(default, skip_serializing_if = "is_default_auth")]
    pub auth: AuthMode,

    /// Static bearer token (when auth = token).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub token: Option<String>,

    /// SSH key fingerprint (when auth = ssh). If None, first Ed25519 key is used.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ssh_fingerprint: Option<String>,
}

fn is_default_auth(auth: &AuthMode) -> bool {
    auth == &AuthMode::default()
}

impl CliConfig {
    pub fn load() -> Result<Self> {
        let mut config = Self::load_global()?;
        // Tag every target seen in the global file as Global. The
        // overlay step below promotes any name that ALSO appears in
        // local to `Both`.
        for name in config.targets.keys() {
            config.target_scopes.insert(name.clone(), Scope::Global);
        }
        if let Some(local) = Self::load_local()? {
            config.overlay(local);
        }
        Ok(config)
    }

    pub fn save(&self) -> Result<()> {
        let path = global_config_path()?;
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let data = serde_json::to_string_pretty(self)?;
        std::fs::write(&path, data)?;
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600))?;
        }
        Ok(())
    }

    pub(crate) fn load_global() -> Result<Self> {
        let path = global_config_path()?;
        if !path.exists() {
            return Ok(Self::default());
        }
        let data = std::fs::read_to_string(&path)?;
        let mut config: Self = serde_json::from_str(&data)?;
        if config.migrate_legacy_to_default_target() {
            config.save()?;
        }
        Ok(config)
    }

    fn load_local() -> Result<Option<Self>> {
        let Some(path) = local_config_path()? else {
            return Ok(None);
        };
        if !path.exists() {
            return Ok(None);
        }
        let data = std::fs::read_to_string(&path)?;
        let mut config: Self = toml::from_str(&data)?;
        config.migrate_legacy_to_default_target();
        Ok(Some(config))
    }

    fn overlay(&mut self, local: Self) {
        if local.current_target.is_some() {
            self.current_target = local.current_target;
        }

        for (name, target) in local.targets {
            // Scope bookkeeping: anything that was already Global
            // becomes Both; anything new is Local.
            let scope = match self.target_scopes.get(&name) {
                Some(Scope::Global) => Scope::Both,
                _ => Scope::Local,
            };
            self.target_scopes.insert(name.clone(), scope);
            self.targets.insert(name, target);
        }

        if local.endpoint.is_some() {
            self.endpoint = local.endpoint;
        }
        if local.auth != AuthMode::default() {
            self.auth = local.auth;
        }
        if local.token.is_some() {
            self.token = local.token;
        }
        if local.ssh_fingerprint.is_some() {
            self.ssh_fingerprint = local.ssh_fingerprint;
        }
    }

    fn migrate_legacy_to_default_target(&mut self) -> bool {
        if !self.targets.is_empty() || self.current_target.is_some() {
            return false;
        }

        let has_legacy = self.endpoint.is_some()
            || self.auth != AuthMode::default()
            || self.token.is_some()
            || self.ssh_fingerprint.is_some();

        if !has_legacy {
            return false;
        }

        self.targets.insert(
            "default".to_string(),
            KobeTarget {
                endpoint: match self.endpoint.clone() {
                    Some(endpoint) => endpoint,
                    None => return false,
                },
                auth: self.auth.clone(),
                token: self.token.clone(),
                ssh_fingerprint: self.ssh_fingerprint.clone(),
            },
        );
        self.current_target = Some("default".to_string());
        self.endpoint = None;
        self.auth = AuthMode::default();
        self.token = None;
        self.ssh_fingerprint = None;
        true
    }

    /// Resolve the active target for the current invocation.
    ///
    /// Priority order:
    ///
    /// 1. `--target <name>` flag (one-shot, no persistence).
    /// 2. **Per-shell session file** at `<cache>/sessions/<ppid>.json`
    ///    (set by `kobe config use <name>`). Different terminal
    ///    windows resolve independently because their parent shells
    ///    have distinct PIDs. See `session.rs`.
    /// 3. Legacy `current_target` field in the config file (kept for
    ///    backward compat with configs from before per-shell sessions
    ///    existed and with `kobe config import` payloads).
    /// 4. Legacy flat `endpoint` (pre-targets configs).
    ///
    /// `endpoint_override` lets `kobe -e https://...` flag short-circuit
    /// the endpoint without touching auth (auth still comes from the
    /// resolved target).
    pub fn resolve(
        &self,
        target_override: Option<&str>,
        endpoint_override: Option<&str>,
    ) -> Result<ResolvedConfig> {
        let session_target = session::load()
            .ok()
            .and_then(|opt| opt.map(|(state, _, _)| state.current_target));

        if let Some(endpoint) = endpoint_override {
            let target_name = target_override
                .map(|s| s.to_string())
                .or_else(|| session_target.clone())
                .or_else(|| self.current_target.clone());
            if let Some(name) = target_name {
                let target = self.targets.get(&name).ok_or_else(|| {
                    anyhow::anyhow!("Unknown target '{name}'. Run: kobe config list")
                })?;
                return Ok(ResolvedConfig {
                    target: Some(name),
                    endpoint: endpoint.to_string(),
                    auth: target.auth.clone(),
                    token: target.token.clone(),
                    ssh_fingerprint: target.ssh_fingerprint.clone(),
                });
            }

            return Ok(ResolvedConfig {
                target: None,
                endpoint: endpoint.to_string(),
                auth: self.auth.clone(),
                token: self.token.clone(),
                ssh_fingerprint: self.ssh_fingerprint.clone(),
            });
        }

        let target_name = target_override
            .map(|s| s.to_string())
            .or(session_target)
            .or_else(|| self.current_target.clone());

        if let Some(name) = target_name {
            let target = self
                .targets
                .get(&name)
                .ok_or_else(|| anyhow::anyhow!("Unknown target '{name}'. Run: kobe config list"))?;

            return Ok(ResolvedConfig {
                target: Some(name),
                endpoint: endpoint_override.unwrap_or(&target.endpoint).to_string(),
                auth: target.auth.clone(),
                token: target.token.clone(),
                ssh_fingerprint: target.ssh_fingerprint.clone(),
            });
        }

        if let Some(endpoint) = self.endpoint.as_deref() {
            return Ok(ResolvedConfig {
                target: None,
                endpoint: endpoint.to_string(),
                auth: self.auth.clone(),
                token: self.token.clone(),
                ssh_fingerprint: self.ssh_fingerprint.clone(),
            });
        }

        if !self.targets.is_empty() {
            anyhow::bail!(
                "No current target configured for this shell. \
                 Run: kobe config use <name> (active for this terminal only) \
                 or pass --target <name>. \
                 Available targets: kobe config list."
            );
        }

        anyhow::bail!(
            "No endpoint configured. Run: kobe config set <name> --endpoint <url> ..., use kobe config import, or pass --endpoint <url>"
        )
    }
}

fn global_config_path() -> Result<PathBuf> {
    let dir =
        dirs::config_dir().ok_or_else(|| anyhow::anyhow!("Cannot determine config directory"))?;
    Ok(dir.join("kobe").join("config.json"))
}

fn local_config_path() -> Result<Option<PathBuf>> {
    let cwd = std::env::current_dir()
        .map_err(|e| anyhow::anyhow!("Cannot determine current directory: {e}"))?;
    Ok(Some(cwd.join(".kobe.toml")))
}

/// Show current config.
pub async fn config_show(target_override: Option<&str>, output: OutputFormat) -> Result<()> {
    let config = CliConfig::load()?;
    match output {
        OutputFormat::Text => print_config(&config, target_override)?,
        OutputFormat::Json => print_json(&config_view_output(&config, target_override))?,
    }
    Ok(())
}

pub async fn config_export(path: Option<&str>, output: OutputFormat) -> Result<()> {
    let config = CliConfig::load()?;
    let serialized = serde_json::to_string_pretty(&config)?;

    match path {
        Some("-") => {
            println!("{serialized}");
        }
        Some(path) => {
            std::fs::write(path, format!("{serialized}\n"))?;
            match output {
                OutputFormat::Text => println!("Exported config to {path}"),
                OutputFormat::Json => print_json(&serde_json::json!({ "path": path }))?,
            }
        }
        None => match output {
            OutputFormat::Text => println!("{serialized}"),
            OutputFormat::Json => print_json(&config_view_output(&config, None))?,
        },
    }

    Ok(())
}

pub async fn config_import(path: Option<&str>, output: OutputFormat) -> Result<()> {
    let source = path.unwrap_or("-");
    let mut input = String::new();

    if source == "-" {
        std::io::stdin().read_to_string(&mut input)?;
    } else {
        input = std::fs::read_to_string(source)?;
    }

    let mut config: CliConfig = serde_json::from_str(&input)?;
    if let Some(current) = config.current_target.as_deref()
        && !config.targets.contains_key(current)
    {
        anyhow::bail!("Imported config references unknown current_target '{current}'");
    }
    if config.migrate_legacy_to_default_target() {
        // Preserve migration behavior for older exported configs.
    }
    config.save()?;

    match output {
        OutputFormat::Text => println!("Imported config into {}", global_config_path()?.display()),
        OutputFormat::Json => print_json(&config_view_output(&config, None))?,
    }

    Ok(())
}

/// Define or update a target.
///
/// By default writes to **local** `./.kobe.toml` so the definition
/// follows the project (and can be committed to the repo if the
/// endpoint is non-secret). Pass `global = true` to write to the
/// global library at `~/.config/kobe/config.json` instead — useful for
/// endpoints you reuse across many projects.
///
/// Does NOT touch the active-target session file. Defining a target
/// and switching to it are separate operations; run `kobe config use
/// <name>` afterwards to make it active for this shell.
pub async fn config_set_target(
    name: &str,
    endpoint: &str,
    auth: Option<&str>,
    token: Option<&str>,
    ssh_fingerprint: Option<&str>,
    global: bool,
    output: OutputFormat,
) -> Result<()> {
    let auth = match auth {
        Some(auth) => parse_auth_mode(auth)?,
        None if token.is_some() => AuthMode::Token,
        None if ssh_fingerprint.is_some() => AuthMode::Ssh,
        None => AuthMode::Oidc,
    };

    if auth == AuthMode::Token && token.is_none() {
        anyhow::bail!("Token targets require --token <value>");
    }

    let target = KobeTarget {
        endpoint: endpoint.to_string(),
        auth,
        token: token.map(str::to_string),
        ssh_fingerprint: ssh_fingerprint.map(str::to_string),
    };

    let written_path = if global {
        let mut config = CliConfig::load_global()?;
        config.targets.insert(name.to_string(), target);
        config.save()?;
        global_config_path()?
    } else {
        write_target_to_local(name, target)?
    };

    match output {
        OutputFormat::Text => {
            println!("Set target {name}");
            println!("Wrote: {}", written_path.display());
            println!("(use this target now: kobe config use {name})");
        }
        OutputFormat::Json => print_json(&TargetMutationOutput {
            name,
            current: false,
        })?,
    }
    Ok(())
}

/// Insert/update a target in the local `./.kobe.toml`, creating the
/// file if it doesn't exist. Returns the absolute path written.
fn write_target_to_local(name: &str, target: KobeTarget) -> Result<PathBuf> {
    let path = local_config_path()?
        .ok_or_else(|| anyhow::anyhow!("Cannot determine current directory for .kobe.toml"))?;

    let mut local: CliConfig = if path.exists() {
        let raw = std::fs::read_to_string(&path)?;
        toml::from_str(&raw)?
    } else {
        CliConfig::default()
    };

    local.targets.insert(name.to_string(), target);

    let toml_str = toml::to_string_pretty(&local)?;
    std::fs::write(&path, toml_str)
        .map_err(|e| anyhow::anyhow!("Failed to write {}: {e}", path.display()))?;
    Ok(path)
}

/// Make `<name>` the active target for **this terminal window only**.
///
/// Writes the choice to `<cache>/sessions/<ppid>.json`, keyed by the
/// parent shell's PID. Other windows have different parent PIDs and
/// keep whatever they had. The session file is reaped automatically
/// when the parent shell exits (see `session::gc_dead_sessions`).
///
/// Validates that the target exists in the merged config (global +
/// local) before writing.
pub async fn config_use_target(name: &str, output: OutputFormat) -> Result<()> {
    let config = CliConfig::load()?;
    if !config.targets.contains_key(name) {
        anyhow::bail!(
            "Unknown target '{name}'. Run: kobe config list (or define one with: kobe config set {name} --endpoint <url>)"
        );
    }

    let saved_path = session::save(&session::SessionState {
        current_target: name.to_string(),
    })?;

    match output {
        OutputFormat::Text => {
            println!("Active target for this shell: {name}");
            println!("State: {}", saved_path.display());
        }
        OutputFormat::Json => print_json(&TargetMutationOutput {
            name,
            current: true,
        })?,
    }
    Ok(())
}

/// Print the active target for this shell, plus where the answer came
/// from. Helps users debug "why is kobe pointing at X?" without
/// needing to know the resolution rules by heart.
pub async fn config_current_target(output: OutputFormat) -> Result<()> {
    let config = CliConfig::load()?;
    let session = session::load()?;

    let (current_target, source) = match session {
        Some((state, path, ppid)) => (state.current_target.clone(), Some((path, ppid))),
        None => match config.current_target.clone() {
            Some(t) => (t, None),
            None => {
                anyhow::bail!(
                    "No active target set for this shell. \
                     Run: kobe config use <name>. \
                     Available targets: kobe config list."
                )
            }
        },
    };

    if !config.targets.contains_key(&current_target) {
        anyhow::bail!(
            "Active target '{current_target}' is not defined. Run: kobe config list (or remove the stale state with: kobe config use <other>)."
        );
    }

    match output {
        OutputFormat::Text => match source {
            Some((path, ppid)) => println!(
                "{current_target}\n  source: session file (ppid={ppid}, {})",
                path.display()
            ),
            None => println!(
                "{current_target}\n  source: legacy config file (consider running: kobe config use {current_target})"
            ),
        },
        OutputFormat::Json => {
            let scope = config
                .target_scopes
                .get(&current_target)
                .map(|s| s.to_string());
            let source_str = source
                .as_ref()
                .map(|(p, ppid)| format!("session-file:{}:{}", ppid, p.display()))
                .unwrap_or_else(|| "config-file".to_string());
            print_json(&serde_json::json!({
                "name": current_target,
                "source": source_str,
                "scope": scope,
            }))?
        }
    }
    Ok(())
}

pub async fn config_list_targets(output: OutputFormat) -> Result<()> {
    let config = CliConfig::load()?;
    let session = session::load().ok().flatten();
    let active = session
        .as_ref()
        .map(|(state, _, _)| state.current_target.clone())
        .or_else(|| config.current_target.clone());

    match output {
        OutputFormat::Text => {
            if config.targets.is_empty() {
                println!("No targets configured.");
                return Ok(());
            }

            // Compute column widths so the table looks tidy on real
            // terminals (long endpoints don't push the SCOPE column off
            // the screen).
            let name_w = config
                .targets
                .keys()
                .map(|s| s.len())
                .max()
                .unwrap_or(4)
                .max(4);
            let endpoint_w = config
                .targets
                .values()
                .map(|t| t.endpoint.len())
                .max()
                .unwrap_or(8)
                .max(8);

            println!(
                "{:<8}{:<width_n$}  {:<width_e$}  {:<6}  SCOPE",
                "ACTIVE",
                "NAME",
                "ENDPOINT",
                "AUTH",
                width_n = name_w,
                width_e = endpoint_w,
            );
            let mut overlap_targets: Vec<&str> = Vec::new();
            for (name, target) in &config.targets {
                let marker = if active.as_deref() == Some(name) {
                    "  *  "
                } else {
                    "     "
                };
                let scope = config
                    .target_scopes
                    .get(name)
                    .copied()
                    .unwrap_or(Scope::Global);
                if scope == Scope::Both {
                    overlap_targets.push(name);
                }
                println!(
                    "{:<8}{:<width_n$}  {:<width_e$}  {:<6}  {}",
                    marker,
                    name,
                    target.endpoint,
                    target.auth.to_string(),
                    scope,
                    width_n = name_w,
                    width_e = endpoint_w,
                );
            }
            if !overlap_targets.is_empty() {
                eprintln!();
                eprintln!(
                    "warning: {} target{} defined in BOTH global and local — local wins:",
                    overlap_targets.len(),
                    if overlap_targets.len() == 1 { "" } else { "s" }
                );
                for n in &overlap_targets {
                    eprintln!("  - {n}");
                }
                eprintln!("  to inspect: cat ~/.config/kobe/config.json and ./.kobe.toml");
            }
        }
        OutputFormat::Json => {
            let targets = config
                .targets
                .iter()
                .map(|(name, target)| {
                    let scope = config
                        .target_scopes
                        .get(name)
                        .copied()
                        .unwrap_or(Scope::Global);
                    ConfigListEntry {
                        name,
                        current: active.as_deref() == Some(name.as_str()),
                        endpoint: &target.endpoint,
                        auth: &target.auth,
                        scope,
                    }
                })
                .collect::<Vec<_>>();
            print_json(&targets)?;
        }
    }
    Ok(())
}

fn config_view_output(config: &CliConfig, target_override: Option<&str>) -> ConfigViewOutput {
    let path = global_config_path()
        .map(|p| p.display().to_string())
        .unwrap_or_else(|_| "(unknown)".to_string());
    let exists = global_config_path().map(|p| p.exists()).unwrap_or(false);
    let resolved =
        config
            .resolve(target_override, None)
            .ok()
            .map(|resolved| ResolvedConfigOutput {
                target: resolved.target,
                endpoint: resolved.endpoint,
                auth: resolved.auth,
                token: resolved.token,
                ssh_fingerprint: resolved.ssh_fingerprint,
            });

    ConfigViewOutput {
        path,
        exists,
        resolved,
        current_target: config.current_target.clone(),
        targets: config
            .targets
            .iter()
            .map(|(name, target)| {
                (
                    name.clone(),
                    ConfigTargetOutput {
                        endpoint: target.endpoint.clone(),
                        auth: target.auth.clone(),
                        token: target.token.clone(),
                        ssh_fingerprint: target.ssh_fingerprint.clone(),
                    },
                )
            })
            .collect(),
        legacy: legacy_output(config),
    }
}

fn legacy_output(config: &CliConfig) -> Option<ConfigLegacyOutput> {
    let has_legacy = config.endpoint.is_some()
        || config.auth != AuthMode::default()
        || config.token.is_some()
        || config.ssh_fingerprint.is_some();
    if !has_legacy {
        return None;
    }

    Some(ConfigLegacyOutput {
        endpoint: config.endpoint.clone(),
        auth: config.auth.clone(),
        token: config.token.clone(),
        ssh_fingerprint: config.ssh_fingerprint.clone(),
    })
}

fn print_config(config: &CliConfig, target_override: Option<&str>) -> Result<()> {
    let path = global_config_path()?;
    let exists = path.exists();

    println!("config:   {}", path.display());
    println!("exists:   {}", if exists { "yes" } else { "no" });

    let resolved = config.resolve(target_override, None);

    if !exists {
        println!();
        println!("No saved config found.");
        if let Ok(resolved) = resolved {
            println!("resolved-endpoint: {}", resolved.endpoint);
            print_auth(
                &resolved.auth,
                resolved.token.as_deref(),
                resolved.ssh_fingerprint.as_deref(),
            );
        } else {
            println!("resolved: none");
            println!(
                "hint:     run 'kobe config set <name> --endpoint <url> ...' or pass --endpoint"
            );
        }
        return Ok(());
    }

    let resolved = resolved?;

    if let Some(target) = &resolved.target {
        println!("current-target: {target}");
        println!("endpoint: {}", resolved.endpoint);
        print_auth(
            &resolved.auth,
            resolved.token.as_deref(),
            resolved.ssh_fingerprint.as_deref(),
        );
    } else if config.targets.is_empty() {
        println!("mode:     legacy");
        println!("endpoint: {}", resolved.endpoint);
        print_auth(
            &resolved.auth,
            resolved.token.as_deref(),
            resolved.ssh_fingerprint.as_deref(),
        );
    }

    if !config.targets.is_empty() {
        println!();
        println!("targets:");
        for (name, target) in &config.targets {
            let marker = if config.current_target.as_deref() == Some(name) {
                "*"
            } else {
                " "
            };
            println!(
                "  {marker} {name}  {}  auth={}",
                target.endpoint, target.auth
            );
        }
    } else if exists {
        println!();
        println!("targets:  (none)");
    }

    Ok(())
}

/// Render a bearer token for display, never revealing enough of it to
/// be usable.
///
/// Tokens longer than 8 characters keep a 4-char head and tail so a user
/// can tell *which* token is configured; anything shorter is replaced
/// wholesale, because a 4+4 elision of an 8-char secret would print the
/// entire value. `None` renders as `(not set)`.
fn mask_token(token: Option<&str>) -> String {
    match token {
        Some(t) if t.len() > 8 => format!("{}...{}", &t[..4], &t[t.len() - 4..]),
        Some(_) => "****".to_string(),
        None => "(not set)".to_string(),
    }
}

fn print_auth(auth: &AuthMode, token: Option<&str>, ssh_fingerprint: Option<&str>) {
    println!("auth:     {auth}");
    if auth == &AuthMode::Token {
        let masked = mask_token(token);
        println!("token:    {masked}");
    }
    if auth == &AuthMode::Ssh {
        let fp = ssh_fingerprint.unwrap_or("(not set — will use ~/.ssh/id_ed25519)");
        println!("ssh-fingerprint: {fp}");
    }
}

pub fn parse_auth_mode(value: &str) -> Result<AuthMode> {
    match value {
        "none" => Ok(AuthMode::None),
        "token" => Ok(AuthMode::Token),
        "oidc" => Ok(AuthMode::Oidc),
        "ssh" => Ok(AuthMode::Ssh),
        _ => anyhow::bail!("Invalid auth mode: {value}. Valid: none, token, oidc, ssh"),
    }
}

#[cfg(test)]
mod tests {
    use super::{AuthMode, CliConfig, KobeTarget, Scope, mask_token, parse_auth_mode};
    use std::path::PathBuf;
    use std::sync::{Mutex, MutexGuard, OnceLock};

    fn target(endpoint: &str) -> KobeTarget {
        KobeTarget {
            endpoint: endpoint.to_string(),
            auth: AuthMode::Oidc,
            token: None,
            ssh_fingerprint: None,
        }
    }

    fn config_with(targets: &[(&str, &str)]) -> CliConfig {
        let mut c = CliConfig::default();
        for (name, endpoint) in targets {
            c.targets.insert(name.to_string(), target(endpoint));
            c.target_scopes.insert(name.to_string(), Scope::Global);
        }
        c
    }

    // ── Session isolation ─────────────────────────────────────────────
    //
    // `CliConfig::resolve` consults the per-shell session file, so the
    // resolve tests must not read (or write) the developer's real one.
    // `KUNOBI_SESSIONS_DIR` redirects it; the mutex serializes the tests
    // that touch that process-global.

    struct SessionSandbox {
        _lock: MutexGuard<'static, ()>,
        previous: Option<std::ffi::OsString>,
        dir: PathBuf,
    }

    impl SessionSandbox {
        /// Point session storage at a fresh empty directory. Nothing in
        /// it, so `session::load()` reports "no active target".
        fn new(tag: &str) -> Self {
            static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
            let lock = LOCK
                .get_or_init(|| Mutex::new(()))
                .lock()
                .unwrap_or_else(|e| e.into_inner());

            let dir = std::env::temp_dir().join(format!("kobe-cfg-{}-{tag}", std::process::id()));
            let _ = std::fs::remove_dir_all(&dir);
            std::fs::create_dir_all(&dir).unwrap();
            let previous = std::env::var_os("KUNOBI_SESSIONS_DIR");
            // SAFETY: the mutex above guarantees no other test in this
            // binary reads or writes this variable concurrently, and
            // nothing else in kobectl touches it.
            unsafe { std::env::set_var("KUNOBI_SESSIONS_DIR", &dir) };
            Self {
                _lock: lock,
                previous,
                dir,
            }
        }
    }

    impl Drop for SessionSandbox {
        fn drop(&mut self) {
            // SAFETY: same as in `new` — still holding the lock.
            unsafe {
                match self.previous.take() {
                    Some(v) => std::env::set_var("KUNOBI_SESSIONS_DIR", v),
                    None => std::env::remove_var("KUNOBI_SESSIONS_DIR"),
                }
            }
            let _ = std::fs::remove_dir_all(&self.dir);
        }
    }

    // ── overlay: local `.kobe.toml` on top of the global library ──────

    /// A target defined in both files resolves to the **local**
    /// definition and is flagged `Both`, which is what makes `kobe
    /// config list` warn about the shadowing instead of silently
    /// pointing at a different cluster than the user's global config
    /// says.
    #[test]
    fn overlay_local_target_shadows_the_global_one_and_is_marked_both() {
        let mut global = config_with(&[("prod", "https://global.test")]);
        let mut local = CliConfig::default();
        local
            .targets
            .insert("prod".to_string(), target("https://local.test"));

        global.overlay(local);

        assert_eq!(global.targets["prod"].endpoint, "https://local.test");
        assert_eq!(global.target_scopes["prod"], Scope::Both);
    }

    /// A target that only the project file defines is `Local`, and one
    /// that only the global library defines stays `Global`. These three
    /// scopes are the entire contract of the SCOPE column.
    #[test]
    fn overlay_tags_local_only_and_global_only_targets_distinctly() {
        let mut global = config_with(&[("prod", "https://global.test")]);
        let mut local = CliConfig::default();
        local
            .targets
            .insert("staging".to_string(), target("https://staging.test"));

        global.overlay(local);

        assert_eq!(global.target_scopes["prod"], Scope::Global);
        assert_eq!(global.target_scopes["staging"], Scope::Local);
        assert_eq!(global.targets.len(), 2);
    }

    /// Local values only override when they are actually set. An
    /// endpoint-only `.kobe.toml` must not blank out a token that lives
    /// in the global config.
    #[test]
    fn overlay_only_applies_local_fields_that_are_set() {
        let mut global = CliConfig {
            current_target: Some("prod".to_string()),
            endpoint: Some("https://global.test".to_string()),
            token: Some("global-token".to_string()),
            ssh_fingerprint: Some("SHA256:global".to_string()),
            ..CliConfig::default()
        };

        global.overlay(CliConfig::default());

        assert_eq!(global.current_target.as_deref(), Some("prod"));
        assert_eq!(global.endpoint.as_deref(), Some("https://global.test"));
        assert_eq!(global.token.as_deref(), Some("global-token"));
        assert_eq!(global.ssh_fingerprint.as_deref(), Some("SHA256:global"));
    }

    /// …and when they *are* set, local wins for every legacy field plus
    /// the active-target name.
    #[test]
    fn overlay_local_values_win_when_present() {
        let mut global = CliConfig {
            current_target: Some("prod".to_string()),
            endpoint: Some("https://global.test".to_string()),
            auth: AuthMode::Oidc,
            token: Some("global-token".to_string()),
            ssh_fingerprint: Some("SHA256:global".to_string()),
            ..CliConfig::default()
        };
        let local = CliConfig {
            current_target: Some("staging".to_string()),
            endpoint: Some("https://local.test".to_string()),
            auth: AuthMode::Token,
            token: Some("local-token".to_string()),
            ssh_fingerprint: Some("SHA256:local".to_string()),
            ..CliConfig::default()
        };

        global.overlay(local);

        assert_eq!(global.current_target.as_deref(), Some("staging"));
        assert_eq!(global.endpoint.as_deref(), Some("https://local.test"));
        assert_eq!(global.auth, AuthMode::Token);
        assert_eq!(global.token.as_deref(), Some("local-token"));
        assert_eq!(global.ssh_fingerprint.as_deref(), Some("SHA256:local"));
    }

    /// Documented asymmetry: `auth` is overlaid only when the local
    /// value differs from the default (`oidc`), because TOML cannot tell
    /// "absent" from "explicitly oidc". A project file that spells out
    /// `auth = "oidc"` therefore cannot pull a global `token` target
    /// back to OIDC. Pinned so the limitation is a decision, not a
    /// surprise.
    #[test]
    fn overlay_cannot_reset_auth_to_the_default_mode() {
        let mut global = CliConfig {
            auth: AuthMode::Token,
            ..CliConfig::default()
        };
        let local = CliConfig {
            auth: AuthMode::Oidc,
            ..CliConfig::default()
        };

        global.overlay(local);

        assert_eq!(global.auth, AuthMode::Token);
    }

    // ── legacy migration ──────────────────────────────────────────────

    #[test]
    fn migrates_legacy_flat_config_into_default_target() {
        let mut config = CliConfig {
            endpoint: Some("https://example.test".to_string()),
            auth: AuthMode::Ssh,
            token: None,
            ssh_fingerprint: Some("SHA256:test".to_string()),
            ..CliConfig::default()
        };

        assert!(config.migrate_legacy_to_default_target());
        assert_eq!(config.current_target.as_deref(), Some("default"));
        let target = config.targets.get("default").expect("default target");
        assert_eq!(target.endpoint, "https://example.test");
        assert_eq!(target.auth, AuthMode::Ssh);
        assert_eq!(target.ssh_fingerprint.as_deref(), Some("SHA256:test"));
        assert!(config.endpoint.is_none());
        assert_eq!(config.auth, AuthMode::Oidc);
        assert!(config.token.is_none());
        assert!(config.ssh_fingerprint.is_none());
    }

    #[test]
    fn does_not_migrate_when_targets_already_exist() {
        let mut config = CliConfig::default();
        config.targets.insert(
            "prod".to_string(),
            super::KobeTarget {
                endpoint: "https://prod.example.test".to_string(),
                auth: AuthMode::Oidc,
                token: None,
                ssh_fingerprint: None,
            },
        );
        config.endpoint = Some("https://legacy.example.test".to_string());

        assert!(!config.migrate_legacy_to_default_target());
        assert!(config.targets.contains_key("prod"));
        assert_eq!(
            config.endpoint.as_deref(),
            Some("https://legacy.example.test")
        );
    }

    /// A config that already names a current target has been through the
    /// migration (or was written by a modern kobe). Re-running it would
    /// clobber that choice with a synthesised `default`.
    #[test]
    fn does_not_migrate_when_a_current_target_is_already_set() {
        let mut config = CliConfig {
            current_target: Some("prod".to_string()),
            endpoint: Some("https://legacy.example.test".to_string()),
            ..CliConfig::default()
        };

        assert!(!config.migrate_legacy_to_default_target());
        assert!(config.targets.is_empty());
        assert_eq!(config.current_target.as_deref(), Some("prod"));
    }

    /// A pristine config has nothing to migrate, and `save()` must not
    /// be triggered — `load_global` writes the file back whenever this
    /// returns true, so a spurious `true` would rewrite (and re-chmod)
    /// the user's config on every single command.
    #[test]
    fn does_not_migrate_an_empty_config() {
        let mut config = CliConfig::default();
        assert!(!config.migrate_legacy_to_default_target());
        assert!(config.targets.is_empty());
        assert!(config.current_target.is_none());
    }

    /// A legacy config with credentials but no endpoint cannot become a
    /// target (a target requires an endpoint). It must be left exactly
    /// as it was — in particular *not* half-migrated with the flat
    /// fields cleared, which would silently destroy the user's token.
    #[test]
    fn does_not_migrate_a_legacy_config_that_has_no_endpoint() {
        let mut config = CliConfig {
            endpoint: None,
            auth: AuthMode::Token,
            token: Some("secret-token".to_string()),
            ..CliConfig::default()
        };

        assert!(!config.migrate_legacy_to_default_target());
        assert!(config.targets.is_empty());
        assert!(config.current_target.is_none());
        assert_eq!(config.auth, AuthMode::Token);
        assert_eq!(config.token.as_deref(), Some("secret-token"));
    }

    // ── resolve: which endpoint does this invocation talk to? ─────────

    /// `--target` beats the per-shell session *and* the legacy
    /// `current_target`. It is the documented one-shot escape hatch, so
    /// it has to sit at the top of the precedence chain.
    #[test]
    fn resolve_prefers_the_explicit_target_override() {
        let _sandbox = SessionSandbox::new("override");
        let mut config = config_with(&[("prod", "https://prod.test"), ("dev", "https://dev.test")]);
        config.current_target = Some("prod".to_string());
        super::session::save(&super::session::SessionState {
            current_target: "prod".to_string(),
        })
        .ok();

        let resolved = config.resolve(Some("dev"), None).unwrap();
        assert_eq!(resolved.target.as_deref(), Some("dev"));
        assert_eq!(resolved.endpoint, "https://dev.test");
    }

    /// With no `--target`, the per-shell session file wins over the
    /// legacy `current_target` in the config. That is the whole point of
    /// per-shell sessions: two terminals pointing at different clusters
    /// while sharing one config file.
    #[test]
    fn resolve_prefers_the_session_target_over_the_config_field() {
        let sandbox = SessionSandbox::new("session-wins");
        if super::session::save(&super::session::SessionState {
            current_target: "dev".to_string(),
        })
        .is_err()
        {
            // Parent PID unavailable (no shell parent) — per-shell state
            // is genuinely unusable here, nothing to assert.
            drop(sandbox);
            return;
        }

        let mut config = config_with(&[("prod", "https://prod.test"), ("dev", "https://dev.test")]);
        config.current_target = Some("prod".to_string());

        let resolved = config.resolve(None, None).unwrap();
        assert_eq!(resolved.target.as_deref(), Some("dev"));
        assert_eq!(resolved.endpoint, "https://dev.test");
    }

    /// Without a session file the legacy `current_target` still works —
    /// configs written before per-shell sessions existed (and `kobe
    /// config import` payloads) must keep resolving.
    #[test]
    fn resolve_falls_back_to_the_legacy_current_target() {
        let _sandbox = SessionSandbox::new("legacy-current");
        let mut config = config_with(&[("prod", "https://prod.test")]);
        config.current_target = Some("prod".to_string());

        let resolved = config.resolve(None, None).unwrap();
        assert_eq!(resolved.target.as_deref(), Some("prod"));
        assert_eq!(resolved.endpoint, "https://prod.test");
    }

    /// The pre-targets flat config still resolves, with no target name.
    #[test]
    fn resolve_falls_back_to_the_flat_legacy_endpoint() {
        let _sandbox = SessionSandbox::new("flat");
        let config = CliConfig {
            endpoint: Some("https://legacy.test".to_string()),
            auth: AuthMode::Token,
            token: Some("t".to_string()),
            ..CliConfig::default()
        };

        let resolved = config.resolve(None, None).unwrap();
        assert!(resolved.target.is_none());
        assert_eq!(resolved.endpoint, "https://legacy.test");
        assert_eq!(resolved.auth, AuthMode::Token);
        assert_eq!(resolved.token.as_deref(), Some("t"));
    }

    /// A name that isn't defined is an error, not a silent fallback to
    /// some other target — pointing a CI job at the wrong cluster is far
    /// worse than failing.
    #[test]
    fn resolve_rejects_an_unknown_target_name() {
        let _sandbox = SessionSandbox::new("unknown");
        let config = config_with(&[("prod", "https://prod.test")]);

        let err = config.resolve(Some("nope"), None).unwrap_err().to_string();
        assert!(err.contains("Unknown target 'nope'"), "got: {err}");
    }

    /// `-e/--endpoint` overrides only the URL: auth mode, token and SSH
    /// fingerprint still come from the resolved target. Dropping the
    /// auth would silently downgrade an authenticated call to anonymous.
    #[test]
    fn resolve_endpoint_override_keeps_the_targets_credentials() {
        let _sandbox = SessionSandbox::new("endpoint-override");
        let mut config = CliConfig::default();
        config.targets.insert(
            "prod".to_string(),
            KobeTarget {
                endpoint: "https://prod.test".to_string(),
                auth: AuthMode::Token,
                token: Some("prod-token".to_string()),
                ssh_fingerprint: None,
            },
        );
        config.current_target = Some("prod".to_string());

        let resolved = config.resolve(None, Some("http://127.0.0.1:8080")).unwrap();
        assert_eq!(resolved.endpoint, "http://127.0.0.1:8080");
        assert_eq!(resolved.target.as_deref(), Some("prod"));
        assert_eq!(resolved.auth, AuthMode::Token);
        assert_eq!(resolved.token.as_deref(), Some("prod-token"));
    }

    /// `--endpoint` with no target at all resolves anonymously against
    /// the legacy top-level auth — this is the `kobe -e http://localhost`
    /// port-forward path, which must work on a machine with no config.
    #[test]
    fn resolve_endpoint_override_works_with_no_target_configured() {
        let _sandbox = SessionSandbox::new("endpoint-only");
        let config = CliConfig::default();

        let resolved = config.resolve(None, Some("http://127.0.0.1:8080")).unwrap();
        assert_eq!(resolved.endpoint, "http://127.0.0.1:8080");
        assert!(resolved.target.is_none());
        assert_eq!(resolved.auth, AuthMode::Oidc);
    }

    /// The two "nothing resolved" failures give different advice:
    /// with targets defined the user needs `kobe config use`, without
    /// them they need `kobe config set`. Pin both, because the message
    /// *is* the UX for a first-run user.
    #[test]
    fn resolve_error_distinguishes_no_active_target_from_no_config_at_all() {
        let _sandbox = SessionSandbox::new("errors");

        let with_targets = config_with(&[("prod", "https://prod.test")]);
        let err = with_targets.resolve(None, None).unwrap_err().to_string();
        assert!(err.contains("kobe config use <name>"), "got: {err}");

        let empty = CliConfig::default();
        let err = empty.resolve(None, None).unwrap_err().to_string();
        assert!(err.contains("kobe config set <name>"), "got: {err}");
        assert!(
            !err.contains("kobe config use"),
            "must not suggest selecting among zero targets: {err}"
        );
    }

    // ── Serde: on-disk compatibility of the config file ───────────────

    /// `contexts` / `current_context` are the pre-rename field names.
    /// Configs written by those versions are still on developers' disks;
    /// dropping the aliases would silently present them with an empty
    /// target list.
    #[test]
    fn config_still_reads_the_pre_rename_context_field_names() {
        let config: CliConfig = serde_json::from_str(
            r#"{
                "current_context": "prod",
                "contexts": {
                    "prod": { "endpoint": "https://prod.test", "auth": "token", "token": "t" }
                }
            }"#,
        )
        .unwrap();

        assert_eq!(config.current_target.as_deref(), Some("prod"));
        assert_eq!(config.targets["prod"].endpoint, "https://prod.test");
        assert_eq!(config.targets["prod"].auth, AuthMode::Token);
    }

    /// A target with no `auth` key predates the auth modes and must
    /// default to OIDC — the same default a freshly created target gets.
    #[test]
    fn target_without_an_auth_key_defaults_to_oidc() {
        let t: KobeTarget = serde_json::from_str(r#"{ "endpoint": "https://x.test" }"#).unwrap();
        assert_eq!(t.auth, AuthMode::Oidc);
        assert!(t.token.is_none());
        assert!(t.ssh_fingerprint.is_none());
    }

    /// The saved file stays minimal: the default auth mode, an empty
    /// target map and unset secrets are all omitted. `target_scopes` is
    /// runtime-only metadata and must never be written — round-tripping
    /// it would make a *local* target look global on the next load.
    #[test]
    fn saved_config_omits_defaults_and_runtime_only_metadata() {
        let mut config = config_with(&[("prod", "https://prod.test")]);
        config.target_scopes.insert("prod".into(), Scope::Local);

        let v: serde_json::Value = serde_json::to_value(&config).unwrap();
        assert!(v.get("auth").is_none(), "default auth must be omitted: {v}");
        assert!(v.get("endpoint").is_none(), "unset endpoint omitted: {v}");
        assert!(v.get("token").is_none(), "unset token omitted: {v}");
        assert!(
            v.get("target_scopes").is_none() && v.get("targetScopes").is_none(),
            "runtime-only scope metadata must never be persisted: {v}"
        );
        assert!(v["targets"]["prod"].get("token").is_none());

        let empty = serde_json::to_value(CliConfig::default()).unwrap();
        assert_eq!(empty, serde_json::json!({}), "a pristine config is `{{}}`");
    }

    /// Serialized configs round-trip through the JSON the `config
    /// export` / `config import` pair uses.
    #[test]
    fn config_round_trips_through_export_import_json() {
        let mut config = CliConfig {
            current_target: Some("prod".to_string()),
            ..CliConfig::default()
        };
        config.targets.insert(
            "prod".to_string(),
            KobeTarget {
                endpoint: "https://prod.test".to_string(),
                auth: AuthMode::Ssh,
                token: None,
                ssh_fingerprint: Some("SHA256:abc".to_string()),
            },
        );

        let json = serde_json::to_string(&config).unwrap();
        let back: CliConfig = serde_json::from_str(&json).unwrap();

        assert_eq!(back.current_target.as_deref(), Some("prod"));
        assert_eq!(back.targets["prod"].auth, AuthMode::Ssh);
        assert_eq!(
            back.targets["prod"].ssh_fingerprint.as_deref(),
            Some("SHA256:abc")
        );
        assert!(
            back.target_scopes.is_empty(),
            "scopes are recomputed on load, never read from the file"
        );
    }

    /// The local project file is TOML. A target defined there must
    /// survive `toml::to_string_pretty` → `toml::from_str`, which is
    /// exactly what `write_target_to_local` does on every
    /// `kobe config set`.
    #[test]
    fn local_target_round_trips_through_toml() {
        let mut local = CliConfig::default();
        local.targets.insert(
            "ci".to_string(),
            KobeTarget {
                endpoint: "https://ci.test".to_string(),
                auth: AuthMode::Token,
                token: Some("ci-token".to_string()),
                ssh_fingerprint: None,
            },
        );

        let rendered = toml::to_string_pretty(&local).expect("local config must serialize to TOML");
        let back: CliConfig = toml::from_str(&rendered).unwrap();
        assert_eq!(back.targets["ci"].endpoint, "https://ci.test");
        assert_eq!(back.targets["ci"].auth, AuthMode::Token);
        assert_eq!(back.targets["ci"].token.as_deref(), Some("ci-token"));
    }

    /// `write_target_to_local` re-serializes whatever is already in
    /// `./.kobe.toml` before adding the new target. If that file still
    /// carries pre-targets flat keys (`endpoint`, `token`, …), the
    /// struct now holds a scalar *after* a table — the classic TOML
    /// "values must be emitted before tables" hazard. `kobe config set`
    /// must not blow up on such a file.
    #[test]
    fn local_config_with_both_flat_legacy_keys_and_targets_still_serializes() {
        let mut local = CliConfig {
            endpoint: Some("https://legacy.test".to_string()),
            auth: AuthMode::Token,
            token: Some("legacy-token".to_string()),
            ..CliConfig::default()
        };
        local
            .targets
            .insert("ci".to_string(), target("https://ci.test"));

        let rendered = toml::to_string_pretty(&local)
            .expect("a legacy-plus-targets local config must still serialize");
        let back: CliConfig = toml::from_str(&rendered).unwrap();
        assert_eq!(back.endpoint.as_deref(), Some("https://legacy.test"));
        assert_eq!(back.token.as_deref(), Some("legacy-token"));
        assert_eq!(back.targets["ci"].endpoint, "https://ci.test");
    }

    // ── Auth mode + scope vocabulary ──────────────────────────────────

    /// The four accepted `--auth` spellings, and the exact set. A typo
    /// must be rejected with a message that lists the valid values
    /// rather than silently falling back to OIDC.
    #[test]
    fn parse_auth_mode_accepts_exactly_the_four_documented_modes() {
        assert_eq!(parse_auth_mode("none").unwrap(), AuthMode::None);
        assert_eq!(parse_auth_mode("token").unwrap(), AuthMode::Token);
        assert_eq!(parse_auth_mode("oidc").unwrap(), AuthMode::Oidc);
        assert_eq!(parse_auth_mode("ssh").unwrap(), AuthMode::Ssh);

        for bad in ["OIDC", "bearer", "", "oidc "] {
            let err = parse_auth_mode(bad).unwrap_err().to_string();
            assert!(
                err.contains("Valid: none, token, oidc, ssh"),
                "`{bad}` must be rejected with the valid list; got: {err}"
            );
        }
    }

    /// `AuthMode`'s `Display` (used in `config list`'s AUTH column) and
    /// its serde form (written to disk and to `--output json`) must
    /// agree — otherwise the value a user copies out of the table is not
    /// the value `--auth` accepts back.
    #[test]
    fn auth_mode_display_matches_its_serde_form() {
        for mode in [
            AuthMode::None,
            AuthMode::Token,
            AuthMode::Oidc,
            AuthMode::Ssh,
        ] {
            let wire = serde_json::to_value(&mode).unwrap();
            assert_eq!(wire, serde_json::json!(mode.to_string()));
            assert_eq!(parse_auth_mode(&mode.to_string()).unwrap(), mode);
        }
        assert_eq!(AuthMode::default(), AuthMode::Oidc);
    }

    /// Same contract for `Scope`, which appears both in the text table
    /// and in the `config list --output json` payload.
    #[test]
    fn scope_display_matches_its_serde_form() {
        for scope in [Scope::Global, Scope::Local, Scope::Both] {
            assert_eq!(
                serde_json::to_value(scope).unwrap(),
                serde_json::json!(scope.to_string())
            );
        }
        assert_eq!(Scope::Global.to_string(), "global");
        assert_eq!(Scope::Local.to_string(), "local");
        assert_eq!(Scope::Both.to_string(), "both");
    }

    // ── Secret handling ───────────────────────────────────────────────

    /// `kobe config show` prints the configured bearer token. It must
    /// never print enough of it to be reusable: short tokens are fully
    /// elided, and long ones keep only a 4+4 fingerprint. A regression
    /// here leaks a credential into terminal scrollback and CI logs.
    #[test]
    fn token_display_never_reveals_a_usable_secret() {
        assert_eq!(mask_token(None), "(not set)");
        // 8 chars or fewer: a 4+4 elision would print the whole thing.
        assert_eq!(mask_token(Some("")), "****");
        assert_eq!(mask_token(Some("short")), "****");
        assert_eq!(mask_token(Some("12345678")), "****");
        // 9+ chars: head and tail only, and never the middle.
        assert_eq!(mask_token(Some("123456789")), "1234...6789");
        let secret = "kobe_live_ABCDEFGHIJKLMNOP";
        let masked = mask_token(Some(secret));
        assert_eq!(masked, "kobe...MNOP");
        assert!(
            !masked.contains("live_ABCDEFGHIJKL"),
            "the body of the token must not appear: {masked}"
        );
    }

    // ── JSON output shapes ────────────────────────────────────────────

    /// The `legacy` block in `config show --output json` exists only to
    /// tell a user they still have a pre-targets config. It must be
    /// absent for a modern config so scripts can use its presence as the
    /// "needs migration" signal.
    #[test]
    fn legacy_json_block_appears_only_for_pre_targets_configs() {
        assert!(super::legacy_output(&config_with(&[("prod", "https://p.test")])).is_none());
        assert!(super::legacy_output(&CliConfig::default()).is_none());

        let legacy = CliConfig {
            endpoint: Some("https://legacy.test".to_string()),
            ..CliConfig::default()
        };
        let out = super::legacy_output(&legacy).expect("legacy block expected");
        assert_eq!(out.endpoint.as_deref(), Some("https://legacy.test"));

        // A non-default auth alone is enough to count as legacy.
        let auth_only = CliConfig {
            auth: AuthMode::Token,
            ..CliConfig::default()
        };
        assert!(super::legacy_output(&auth_only).is_some());
    }

    /// `config show --output json` is a scripted surface: keys are
    /// camelCase, absent optionals are omitted (not null), and the
    /// resolved block reflects the same precedence `resolve` applies.
    #[test]
    fn config_view_json_uses_camel_case_and_omits_absent_optionals() {
        let _sandbox = SessionSandbox::new("view");
        let mut config = CliConfig::default();
        config.targets.insert(
            "prod".to_string(),
            KobeTarget {
                endpoint: "https://prod.test".to_string(),
                auth: AuthMode::Ssh,
                token: None,
                ssh_fingerprint: Some("SHA256:abc".to_string()),
            },
        );
        config.current_target = Some("prod".to_string());

        let v = serde_json::to_value(super::config_view_output(&config, None)).unwrap();

        assert_eq!(v["currentTarget"], "prod");
        assert_eq!(v["targets"]["prod"]["sshFingerprint"], "SHA256:abc");
        assert!(
            v["targets"]["prod"].get("token").is_none(),
            "an unset token must be omitted, not null: {v}"
        );
        assert!(v.get("legacy").is_none(), "no legacy block expected: {v}");
        assert_eq!(v["resolved"]["target"], "prod");
        assert_eq!(v["resolved"]["endpoint"], "https://prod.test");
        assert_eq!(v["resolved"]["auth"], "ssh");
    }

    /// When nothing resolves, the `resolved` key is omitted entirely
    /// rather than emitted as null — `config show --output json` on a
    /// fresh machine must still be valid, parseable output.
    #[test]
    fn config_view_json_omits_resolved_when_nothing_is_configured() {
        let _sandbox = SessionSandbox::new("view-empty");
        let v =
            serde_json::to_value(super::config_view_output(&CliConfig::default(), None)).unwrap();

        assert!(v.get("resolved").is_none(), "got: {v}");
        assert!(v.get("currentTarget").is_none(), "got: {v}");
        assert_eq!(v["targets"], serde_json::json!({}));
        assert!(v["path"].is_string());
        assert!(v["exists"].is_boolean());
    }

    /// Guard against `BTreeMap` being swapped for a `HashMap`: the
    /// targets map is rendered directly into `config list`, so a
    /// non-deterministic order would reshuffle the table (and the JSON)
    /// on every invocation.
    #[test]
    fn targets_iterate_in_stable_alphabetical_order() {
        let config = config_with(&[
            ("zeta", "https://z.test"),
            ("alpha", "https://a.test"),
            ("mid", "https://m.test"),
        ]);
        let names: Vec<&str> = config.targets.keys().map(String::as_str).collect();
        assert_eq!(names, vec!["alpha", "mid", "zeta"]);
    }
}