beam-daemon 0.5.0

Daemon process for beam that receives Feishu/Lark events and manages coding sessions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
//! Directory selection card for new Feishu sessions.
//!
//! When a new Feishu message would create a new agent session, instead of
//! immediately starting the worker, we present a directory selection card.
//! The user must pick a working directory under the bot's root working dir
//! before the session starts.

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::card_i18n;
use beam_core::SessionScope;

// --- Constants ---

const MAX_SCAN_DEPTH: usize = 3;
const MAX_SCAN_CANDIDATES: usize = 500;
const MAX_RECENT_DIRS: usize = 10;
const MAX_RECOMMENDED_DIRS: usize = 8;
/// Maximum number of directory buttons rendered in the card.
/// Beyond this, the user should use the select_static dropdown.
const MAX_BUTTON_DIRS: usize = 40;
/// Maximum number of options in the select_static dropdown.
const MAX_SELECT_DIRS: usize = 150;
/// TTL for pending create entries (30 minutes in milliseconds).
/// Entries older than this are pruned and the user must send a new message.
pub const PENDING_CREATE_TTL_MS: i64 = 30 * 60 * 1000;

const SKIP_DIR_NAMES: &[&str] = &[
    ".git",
    ".beam",
    "target",
    "node_modules",
    ".venv",
    "__pycache__",
    ".DS_Store",
    "dist",
    "build",
    "vendor",
    "bin",
    "obj",
    ".svn",
    ".hg",
    ".idea",
    ".vscode",
    ".cache",
    ".npm",
    ".yarn",
    ".next",
    ".nuxt",
    "coverage",
    ".tox",
    ".eggs",
    ".mypy_cache",
    ".pytest_cache",
];

// --- Data Structures ---

/// Pending session creation context, stored in memory until the user picks a working dir.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PendingCreateSession {
    pub pending_id: String,
    pub lark_app_id: String,
    pub chat_id: String,
    pub chat_type: Option<String>,
    pub message_id: String,
    pub anchor: String,
    pub scope: SessionScope,
    /// Feishu thread_id (omt_*), stable topic identifier for session matching.
    #[serde(default)]
    pub thread_id: Option<String>,
    /// Root message ID (from Feishu root_id field) used for reply and quote hint.
    /// Falls back to message_id when not available.
    #[serde(default)]
    pub root_id: Option<String>,
    pub title: String,
    pub text: String,
    pub sender_open_id: Option<String>,
    pub sender_type: Option<String>,
    #[serde(default)]
    pub locale: Option<String>,
    pub parent_id: Option<String>,
    /// Serialized Vec<LarkEventMention>
    #[serde(default)]
    pub mentions_json: String,
    pub quota_key: Option<String>,
    pub created_at: i64,
    // Bot info for session creation
    pub cli_id: String,
    pub cli_bin: String,
    #[serde(default)]
    pub cli_args: Vec<String>,
    pub root_working_dir: String,
    /// All scanned candidate dirs (relative paths from root)
    pub candidate_dirs: Vec<String>,
    /// The card's message_id so we can update it later
    #[serde(default)]
    pub card_message_id: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecentDirEntry {
    pub dir: String,
    pub used_at: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct RecentDirsStore {
    pub entries: HashMap<String, Vec<RecentDirEntry>>,
}

// --- Root Working Dir ---

/// Determine the root working directory for a bot.
/// Priority: bot.workingDir > daemon.working_dirs[0] > "."
pub fn determine_root_working_dir(
    bot_working_dir: Option<&str>,
    daemon_working_dirs: &[String],
) -> String {
    let raw = bot_working_dir
        .map(|s| s.to_string())
        .or_else(|| daemon_working_dirs.first().cloned())
        .unwrap_or_else(|| ".".to_string());
    expand_tilde(&raw)
}

fn expand_tilde(path: &str) -> String {
    if path.starts_with("~/") || path == "~" {
        if let Ok(home) = std::env::var("HOME") {
            if path == "~" {
                return home;
            }
            return format!("{}/{}", home, &path[2..]);
        }
    }
    path.to_string()
}

// --- Directory Scanning ---

/// Scan the root directory for candidate subdirectories.
/// Returns relative paths (from root), including "." for root itself.
/// Skips common noise directories and limits depth/quantity.
pub fn scan_candidate_dirs(root: &Path) -> Vec<String> {
    let mut dirs: Vec<String> = Vec::new();
    // Include root itself
    dirs.push(".".to_string());
    scan_dirs_recursive(root, root, 1, &mut dirs);
    dirs
}

fn scan_dirs_recursive(base: &Path, current: &Path, depth: usize, dirs: &mut Vec<String>) {
    if depth > MAX_SCAN_DEPTH || dirs.len() >= MAX_SCAN_CANDIDATES {
        return;
    }

    let entries = match std::fs::read_dir(current) {
        Ok(entries) => entries,
        Err(_) => return,
    };

    for entry in entries.flatten() {
        if dirs.len() >= MAX_SCAN_CANDIDATES {
            return;
        }
        let path = entry.path();
        let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");

        // Check file_type first: skip symlinks to prevent escaping root.
        // path.is_dir() follows symlinks, which could point outside root.
        let ft = match entry.file_type() {
            Ok(ft) => ft,
            Err(_) => continue,
        };
        if ft.is_symlink() {
            continue;
        }
        if !ft.is_dir() || should_skip_dir(file_name) {
            continue;
        }
        // Compute relative path from base
        if let Ok(rel) = path.strip_prefix(base) {
            dirs.push(rel.to_string_lossy().to_string());
        }
        scan_dirs_recursive(base, &path, depth + 1, dirs);
    }
}

fn should_skip_dir(name: &str) -> bool {
    SKIP_DIR_NAMES.contains(&name) || name.starts_with('.')
}

// --- Directory Filtering & Matching ---

/// Tokenize a keyword string into individual words (split by whitespace).
pub fn tokenize_keywords(input: &str) -> Vec<String> {
    input
        .split_whitespace()
        .map(|s| s.to_lowercase())
        .filter(|s| !s.is_empty())
        .collect()
}

/// Filter directories by keywords using AND matching (case-insensitive).
/// Returns dirs that match ALL keywords in the path.
pub fn match_dirs(dirs: &[String], keywords: &[&str]) -> Vec<String> {
    if keywords.is_empty() {
        return dirs.to_vec();
    }
    let lower_keywords: Vec<String> = keywords.iter().map(|k| k.to_lowercase()).collect();
    dirs.iter()
        .filter(|dir| {
            let lower_dir = dir.to_lowercase();
            lower_keywords.iter().all(|kw| lower_dir.contains(kw))
        })
        .cloned()
        .collect()
}

/// Filter directories by a single keyword search string (multi-word AND).
/// If the search string is empty, returns all dirs.
pub fn filter_dirs(dirs: &[String], search: &str) -> Vec<String> {
    let keywords = tokenize_keywords(search);
    let kw_refs: Vec<&str> = keywords.iter().map(|s| s.as_str()).collect();
    match_dirs(dirs, &kw_refs)
}

/// Find the best match from a list of directories given keywords.
/// Returns Some only when there is exactly ONE match (excluding "." root).
/// Multiple matches or zero matches → None (let user pick manually).
pub fn find_best_match(dirs: &[String], search: &str) -> Option<String> {
    let keywords = tokenize_keywords(search);
    if keywords.is_empty() {
        return None;
    }
    let kw_refs: Vec<&str> = keywords.iter().map(|s| s.as_str()).collect();
    let matched = match_dirs(dirs, &kw_refs);
    // Exclude root from match consideration
    let non_root: Vec<&String> = matched.iter().filter(|d| d.as_str() != ".").collect();
    if non_root.len() == 1 {
        Some(non_root[0].clone())
    } else {
        None
    }
}

// --- Security Validation ---

/// Check if a directory path (absolute or relative) is under the given root.
/// Uses pure path manipulation; does NOT require the paths to exist on disk.
/// Handles boundary cases like `/tmp/rootX` NOT being under `/tmp/root`,
/// and relative roots like `"."`.
pub fn is_dir_under_root(dir: &str, root: &str) -> bool {
    let root_path = Path::new(root);
    let dir_path = Path::new(dir);

    // Reject paths that attempt to escape via ".."
    if dir_path
        .components()
        .any(|c| c == std::path::Component::ParentDir)
    {
        return false;
    }

    // Reject absolute dir when root is relative (can't be under it)
    if dir_path.is_absolute() && !root_path.is_absolute() {
        return false;
    }

    // Normalize both paths (resolve ".", "..", etc.)
    let normalized_root = normalize_path(root_path);
    let normalized_dir = if dir_path.is_absolute() {
        normalize_path(dir_path)
    } else {
        normalize_path(&root_path.join(dir_path))
    };

    if normalized_dir == normalized_root {
        return true;
    }

    // If root is empty/current-dir (e.g., "."), accept any non-absolute,
    // non-escape relative path (already verified above).
    let root_str = normalized_root.to_string_lossy();
    if root_str.is_empty() {
        return true;
    }

    // Check that dir starts with root + separator
    let dir_str = normalized_dir.to_string_lossy();
    if dir_str.len() > root_str.len() {
        let remainder = &dir_str[root_str.len()..];
        remainder.starts_with(std::path::MAIN_SEPARATOR)
    } else {
        false
    }
}

/// Normalize a path by resolving components where possible.
/// For non-existing paths, this does a best-effort normalization
/// by collapsing ".." and "." components.
fn normalize_path(path: &Path) -> PathBuf {
    let mut result = PathBuf::new();
    for component in path.components() {
        match component {
            std::path::Component::ParentDir => {
                result.pop();
            }
            std::path::Component::CurDir => {}
            c => {
                result.push(c.as_os_str());
            }
        }
    }
    result
}

/// Check if a directory is a valid candidate (exists in candidate list and under root).
pub fn is_valid_candidate(dir: &str, root: &str, candidates: &[String]) -> bool {
    if !is_dir_under_root(dir, root) {
        return false;
    }
    // dir should be in the candidate list (or be root itself)
    candidates.contains(&dir.to_string()) || dir == "."
}

/// Resolve a relative dir (from candidate list) to an absolute path.
pub fn resolve_dir(root: &str, rel: &str) -> String {
    if rel == "." {
        root.to_string()
    } else {
        Path::new(root).join(rel).to_string_lossy().to_string()
    }
}

// --- Recent Directories ---

/// Build a key for the recent dirs map.
/// Format: {app_id}:{chat_id}:{operator}
pub fn build_recent_dir_key(app_id: &str, chat_id: &str, operator: Option<&str>) -> String {
    match operator {
        Some(op) if !op.is_empty() => format!("{}:{}:{}", app_id, chat_id, op),
        _ => format!("{}:{}", app_id, chat_id),
    }
}

/// Get recent directories for a key, filtered to those under root.
pub fn get_recent_dirs(store: &RecentDirsStore, key: &str, root: &str) -> Vec<String> {
    let entries = match store.entries.get(key) {
        Some(entries) => entries,
        None => return Vec::new(),
    };
    entries
        .iter()
        .map(|e| e.dir.clone())
        .filter(|d| d == "." || is_dir_under_root(d, root))
        .take(MAX_RECOMMENDED_DIRS)
        .collect()
}

/// Record a directory selection as recent.
pub fn record_recent_dir(store: &mut RecentDirsStore, key: &str, dir: &str) {
    let entries = store.entries.entry(key.to_string()).or_default();
    // Remove existing entry for the same dir
    entries.retain(|e| e.dir != dir);
    // Insert at front
    entries.insert(
        0,
        RecentDirEntry {
            dir: dir.to_string(),
            used_at: chrono::Utc::now().to_rfc3339(),
        },
    );
    // Trim
    entries.truncate(MAX_RECENT_DIRS);
}

/// Load recent dirs from disk.
pub async fn load_recent_dirs(path: &Path) -> Result<RecentDirsStore> {
    match tokio::fs::read_to_string(path).await {
        Ok(content) => Ok(serde_json::from_str(&content).unwrap_or_default()),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(RecentDirsStore::default()),
        Err(e) => Err(e.into()),
    }
}

/// Save recent dirs to disk.
pub async fn save_recent_dirs(path: &Path, store: &RecentDirsStore) -> Result<()> {
    if let Some(parent) = path.parent() {
        tokio::fs::create_dir_all(parent).await?;
    }
    let tmp = path.with_extension("json.tmp");
    let payload = serde_json::to_vec_pretty(store)?;
    tokio::fs::write(&tmp, &payload).await?;
    tokio::fs::rename(&tmp, path).await?;
    Ok(())
}

// --- Pending Create Pruning ---

/// Remove expired pending create entries.
/// Returns the number of entries pruned.
pub fn prune_expired_pending_creates(
    map: &mut HashMap<String, PendingCreateSession>,
    now_ms: i64,
) -> usize {
    let before = map.len();
    map.retain(|_, pending| now_ms - pending.created_at < PENDING_CREATE_TTL_MS);
    before - map.len()
}

/// Load pending creates from disk, pruning expired entries.
pub(crate) async fn load_pending_creates(
    paths: &beam_core::BeamPaths,
) -> HashMap<String, PendingCreateSession> {
    let path = paths.pending_creates_json();
    let entries: Vec<PendingCreateSession> = match beam_core::persist::read_json(&path) {
        Ok(Some(entries)) => entries,
        _ => return Default::default(),
    };
    let total_loaded = entries.len();
    let now_ms = chrono::Utc::now().timestamp_millis();
    let mut map = HashMap::new();
    let mut retained = Vec::new();
    for entry in &entries {
        if now_ms - entry.created_at > PENDING_CREATE_TTL_MS {
            continue;
        }
        retained.push(entry.clone());
        map.insert(entry.pending_id.clone(), entry.clone());
    }
    // Prune expired entries
    if retained.len() < total_loaded {
        if retained.is_empty() {
            let _ = tokio::fs::remove_file(&path).await;
        } else {
            let path_clone = path.clone();
            let _ = tokio::task::spawn_blocking(move || {
                beam_core::persist::atomic_write_json(&path_clone, &retained)
            })
            .await;
        }
    }
    map
}

/// Save pending creates to disk.
#[allow(dead_code)]
pub(crate) async fn save_pending_creates(
    paths: &beam_core::BeamPaths,
    map: &HashMap<String, PendingCreateSession>,
) {
    let entries: Vec<PendingCreateSession> = map.values().cloned().collect();
    let path = paths.pending_creates_json();
    if entries.is_empty() {
        let _ = tokio::fs::remove_file(&path).await;
        return;
    }
    let path_clone = path.clone();
    let _ = tokio::task::spawn_blocking(move || {
        beam_core::persist::atomic_write_json(&path_clone, &entries)
    })
    .await;
}

// --- Card Building ---

fn is_zh_locale(locale: Option<&str>) -> bool {
    locale
        .map(|value| {
            let normalized = value.to_ascii_lowercase().replace('-', "_");
            normalized == "zh" || normalized.starts_with("zh_")
        })
        .unwrap_or(false)
}

fn card_text<'a>(locale: Option<&str>, zh: &'a str, en: &'a str) -> &'a str {
    if is_zh_locale(locale) { zh } else { en }
}

/// Build the directory selection card JSON string.
///
/// The card has two directory-picking entry points:
/// 1. **Buttons** — primary for recommended/filtered dirs (capped at MAX_BUTTON_DIRS).
/// 2. **select_static dropdown** — "more directories / more matches" fallback
///    (capped at MAX_SELECT_DIRS). Each option value is a JSON string with
///    `{ action, pending_id, working_dir }`, parsed by `parse_lark_card_action`
///    via the `/action/option` fallback in lib.rs.
///
/// Parameters:
/// - pending_id: unique ID for this pending session creation
/// - root_dir: the root working directory (displayed to user)
/// - title: the session title (user message summary)
/// - recommended_dirs: list of recommended directories (relative paths from root)
/// - all_candidates: all candidate directories (for the select_static dropdown)
/// - filter_result: optional filtered subset to show as current results
/// - search_keyword: current search keyword (for restoring input field value)
/// - message: optional info/warning message to display
pub fn build_dir_select_card(
    pending_id: &str,
    root_dir: &str,
    title: &str,
    recommended_dirs: &[String],
    all_candidates: &[String],
    filter_result: Option<&[String]>,
    search_keyword: Option<&str>,
    message: Option<&str>,
    locale: Option<&str>,
) -> String {
    let mut elements: Vec<Value> = Vec::new();

    // Header: root dir display (sanitize: escape backticks to avoid lark_md code tag errors)
    let display_root = sanitize_lark_md(&truncate_str_tail(root_dir, 60));
    elements.push(serde_json::json!({
        "tag": "div",
        "text": {
            "tag": "lark_md",
            "content": format!("📁 **{}:** {}", card_text(locale, "根目录", "Root"), display_root),
            "i18n_content": {
                "zh_cn": format!("📁 **{}:** {}", "根目录", display_root),
                "en_us": format!("📁 **{}:** {}", "Root", display_root),
            }
        }
    }));

    // Message summary (sanitize: escape backticks in user text)
    let display_title = sanitize_lark_md(&truncate_str_head(title, 60));
    elements.push(serde_json::json!({
        "tag": "div",
        "text": {
            "tag": "lark_md",
            "content": format!("💬 **{}:** {}", card_text(locale, "消息", "Message"), display_title),
            "i18n_content": {
                "zh_cn": format!("💬 **{}:** {}", "消息", display_title),
                "en_us": format!("💬 **{}:** {}", "Message", display_title),
            }
        }
    }));

    // Optional message
    if let Some(msg) = message {
        elements.push(serde_json::json!({
            "tag": "div",
            "text": {
                "tag": "lark_md",
                "content": msg,
                "i18n_content": {
                    "zh_cn": msg,
                    "en_us": msg,
                }
            }
        }));
    }

    let is_filtered = filter_result.is_some();

    // --- Determine button dirs and select_static dirs ---
    let (button_dirs, select_dirs) = if let Some(fr) = filter_result {
        // Filtered view: both buttons and select_static draw from filter results.
        let btn: Vec<&String> = fr.iter().take(MAX_BUTTON_DIRS).collect();
        let sel: Vec<&String> = fr.iter().take(MAX_SELECT_DIRS).collect();
        (btn, sel)
    } else {
        // Initial view: buttons from recommended, select_static from all_candidates.
        let btn: Vec<&String> = recommended_dirs.iter().take(MAX_BUTTON_DIRS).collect();
        let sel: Vec<&String> = all_candidates.iter().take(MAX_SELECT_DIRS).collect();
        (btn, sel)
    };

    // --- Section label ---
    let total_count = if let Some(fr) = filter_result {
        fr.len()
    } else {
        select_dirs.len()
    };
    let section_label = if is_filtered {
        if total_count > MAX_BUTTON_DIRS && button_dirs.len() < select_dirs.len() {
            if is_zh_locale(locale) {
                format!(
                    "📋 **当前结果(共 {} 个,按钮显示前 {} 个):**",
                    total_count, MAX_BUTTON_DIRS
                )
            } else {
                format!(
                    "📋 **Current results ({} total, showing first {} as buttons):**",
                    total_count, MAX_BUTTON_DIRS
                )
            }
        } else {
            if is_zh_locale(locale) {
                format!("📋 **当前结果({} 个):**", total_count)
            } else {
                format!("📋 **Current results ({}):**", total_count)
            }
        }
    } else {
        format!(
            "📋 **{}:**",
            card_text(locale, "推荐目录", "Recommended directories")
        )
    };

    // --- Build button labels with smart display ---
    // Recommended section: always short names (even if conflicting).
    // Filtered section: conflict-aware (short name if unique, relative path if duplicate).
    let button_labels = build_dir_labels(
        &button_dirs
            .iter()
            .map(|d| d.to_string())
            .collect::<Vec<String>>(),
        root_dir,
        is_filtered, // detect conflicts only in filtered mode
    );

    // --- Buttons ---
    if !button_dirs.is_empty() {
        elements.push(serde_json::json!({
            "tag": "div",
            "text": {
                "tag": "lark_md",
                "content": section_label,
                "i18n_content": {
                    "zh_cn": if is_filtered {
                        if total_count > MAX_BUTTON_DIRS && button_dirs.len() < select_dirs.len() {
                            format!("📋 **当前结果(共 {} 个,按钮显示前 {} 个):**", total_count, MAX_BUTTON_DIRS)
                        } else {
                            format!("📋 **当前结果({} 个):**", total_count)
                        }
                    } else {
                        "📋 **推荐目录:**".to_string()
                    },
                    "en_us": if is_filtered {
                        if total_count > MAX_BUTTON_DIRS && button_dirs.len() < select_dirs.len() {
                            format!("📋 **Current results ({} total, showing first {} as buttons):**", total_count, MAX_BUTTON_DIRS)
                        } else {
                            format!("📋 **Current results ({}):**", total_count)
                        }
                    } else {
                        "📋 **Recommended directories:**".to_string()
                    },
                }
            }
        }));

        // Each directory gets its own action row with a single button
        // (one button per row avoids the "two buttons per row" issue).
        for (i, dir) in button_dirs.iter().enumerate() {
            let display = &button_labels[i];
            let conflict = display.1;
            let label = &display.0;

            let truncated = if !conflict || dir.as_str() == "." {
                truncate_str(label, 22)
            } else {
                truncate_str_tail(label, 22)
            };

            let pick_value = serde_json::json!({
                "action": "dir_select_pick",
                "pending_id": pending_id,
                "working_dir": dir
            });
            elements.push(serde_json::json!({
                "tag": "action",
                "actions": [
                    {
                        "tag": "button",
                        "text": card_i18n::plain_text(locale, truncated.clone(), truncated),
                        "type": if dir.as_str() == "." { "primary" } else { "default" },
                        "value": pick_value
                    }
                ]
            }));
        }
    } else {
        elements.push(serde_json::json!({
            "tag": "div",
            "text": {
                "tag": "lark_md",
                "content": card_text(
                    locale,
                    "⚠️ 没有匹配的目录,请尝试其他关键词。",
                    "⚠️ No matching directories. Try another keyword."
                ),
                "i18n_content": {
                    "zh_cn": "⚠️ 没有匹配的目录,请尝试其他关键词。",
                    "en_us": "⚠️ No matching directories. Try another keyword.",
                }
            }
        }));
    }

    // --- select_static: "more directories / more matches" ---
    if !select_dirs.is_empty() {
        let select_total = if is_filtered {
            if let Some(fr) = filter_result {
                fr.len()
            } else {
                select_dirs.len()
            }
        } else {
            all_candidates.len()
        };
        let select_shown = select_dirs.len();
        let select_label_zh = if is_filtered {
            if select_total > select_shown {
                format!(
                    "📋 **更多匹配(共 {} 个,下拉显示前 {} 个):**",
                    select_total, select_shown
                )
            } else {
                format!("📋 **{}:**", "下拉选择")
            }
        } else if select_total > select_shown {
            format!(
                "📋 **更多目录(共 {} 个,下拉显示前 {} 个):**",
                select_total, select_shown
            )
        } else {
            format!("📋 **更多目录(共 {} 个):**", select_total)
        };
        let select_label_en = if is_filtered {
            if select_total > select_shown {
                format!(
                    "📋 **More matches ({} total, showing first {} in dropdown):**",
                    select_total, select_shown
                )
            } else {
                format!("📋 **{}:**", "Dropdown selection")
            }
        } else if select_total > select_shown {
            format!(
                "📋 **More directories ({} total, showing first {} in dropdown):**",
                select_total, select_shown
            )
        } else {
            format!("📋 **More directories ({} total):**", select_total)
        };
        elements.push(serde_json::json!({
            "tag": "div",
            "text": card_i18n::lark_md(locale, select_label_zh, select_label_en)
        }));

        let select_labels = build_dir_labels(
            &select_dirs
                .iter()
                .map(|d| d.to_string())
                .collect::<Vec<String>>(),
            root_dir,
            is_filtered, // detect conflicts only in filtered mode
        );

        let mut options: Vec<Value> = Vec::new();
        for (i, dir) in select_dirs.iter().enumerate() {
            let display = &select_labels[i];
            let label = &display.0;

            // Option value is a JSON string containing action/pending_id/working_dir.
            // Parsed by try_parse_select_option → parse_lark_card_action in lib.rs.
            let option_value = serde_json::json!({
                "action": "dir_select_pick",
                "pending_id": pending_id,
                "working_dir": dir,
            });
            let option_value_str = serde_json::to_string(&option_value).unwrap_or_default();

            options.push(serde_json::json!({
                "text": card_i18n::plain_text(locale, label.clone(), label),
                "value": option_value_str
            }));
        }

        // Wrap select_static in an action module so it actually renders and
        // dispatches events. A bare select_static as a top-level card element
        // is not valid in the Feishu card schema and will be silently ignored.
        elements.push(serde_json::json!({
            "tag": "action",
            "actions": [
                {
                "tag": "select_static",
                "placeholder": {
                    "tag": "plain_text",
                    "content": card_text(locale, "请选择目录...", "Select a directory..."),
                    "i18n_content": {
                        "zh_cn": "请选择目录...",
                        "en_us": "Select a directory...",
                    }
                },
                "options": options
            }
            ]
        }));
    }

    // Separator before search section
    elements.push(serde_json::json!({ "tag": "hr" }));

    // Search hint: must be a standalone div outside the form.
    // Feishu card forms only accept input + button; div is not allowed inside form.
    // Use plain instructional text (not a bold title) so users don't mistake
    // the label for a clickable element.
    elements.push(serde_json::json!({
        "tag": "div",
        "text": {
            "tag": "lark_md",
            "content": card_text(
                locale,
                "🔍 在下方输入关键词,点击「筛选」过滤目录,或点击「使用最优匹配启动」自动选择最佳目录",
                "🔍 Enter a keyword below, click \"Filter\" to narrow directories, or click \"Start with best match\" to choose automatically"
            ),
            "i18n_content": {
                "zh_cn": "🔍 在下方输入关键词,点击「筛选」过滤目录,或点击「使用最优匹配启动」自动选择最佳目录",
                "en_us": "🔍 Enter a keyword below, click \"Filter\" to narrow directories, or click \"Start with best match\" to choose automatically",
            }
        }
    }));

    // Form container: input + two form_submit buttons.
    // Must be a single "tag": "form" so that the input value is submitted
    // as /action/form_value/dir_search_keyword when either button is clicked.
    let mut form_elements: Vec<Value> = Vec::new();

    form_elements.push(serde_json::json!({
        "tag": "input",
        "name": "dir_search_keyword",
        "placeholder": {
            "tag": "plain_text",
            "content": card_text(locale, "输入关键词筛选目录...", "Type a keyword to filter directories..."),
            "i18n_content": {
                "zh_cn": "输入关键词筛选目录...",
                "en_us": "Type a keyword to filter directories...",
            }
        },
        "default_value": search_keyword.unwrap_or("")
    }));

    form_elements.push(serde_json::json!({
        "tag": "button",
        "text": {
            "tag": "plain_text",
            "content": card_text(locale, "🔍 筛选", "🔍 Filter"),
            "i18n_content": {
                "zh_cn": "🔍 筛选",
                "en_us": "🔍 Filter",
            }
        },
        "type": "primary",
        "action_type": "form_submit",
        "name": "dir_select_filter_btn",
        "value": {
            "action": "dir_select_filter",
            "pending_id": pending_id
        }
    }));

    form_elements.push(serde_json::json!({
        "tag": "button",
        "text": {
            "tag": "plain_text",
            "content": card_text(locale, "🚀 使用最优匹配启动", "🚀 Start with best match"),
            "i18n_content": {
                "zh_cn": "🚀 使用最优匹配启动",
                "en_us": "🚀 Start with best match",
            }
        },
        "type": "default",
        "action_type": "form_submit",
        "name": "dir_select_best_btn",
        "value": {
            "action": "dir_select_best",
            "pending_id": pending_id
        }
    }));

    elements.push(serde_json::json!({
        "tag": "form",
        "name": "dir_search_form",
        "elements": form_elements
    }));

    // Build card with config
    let card = serde_json::json!({
        "config": {
            "wide_screen_mode": true
        },
        "header": {
            "title": {
                "tag": "plain_text",
                "content": card_text(locale, "请选择工作目录", "Choose a working directory"),
                "i18n_content": {
                    "zh_cn": "请选择工作目录",
                    "en_us": "Choose a working directory",
                }
            },
            "template": "blue"
        },
        "elements": elements
    });

    serde_json::to_string(&card).unwrap_or_default()
}

/// Build a simple "session starting" card to replace the dir select card.
pub fn build_dir_session_starting_card(
    working_dir: &str,
    title: &str,
    locale: Option<&str>,
) -> String {
    let body_zh = format!(
        "✅ 已选择工作目录:{}\n\n正在启动会话:_{}_\n\n等待终端就绪...",
        sanitize_lark_md(working_dir),
        sanitize_lark_md(title)
    );
    let body_en = format!(
        "✅ Selected working directory: {}\n\nStarting session: _{}_\n\nWaiting for terminal readiness...",
        sanitize_lark_md(working_dir),
        sanitize_lark_md(title)
    );
    let card = serde_json::json!({
        "config": {
            "wide_screen_mode": true
        },
        "header": {
            "title": {
                "tag": "plain_text",
                "content": card_text(locale, "正在启动会话", "Starting session"),
                "i18n_content": {
                    "zh_cn": "正在启动会话",
                    "en_us": "Starting session",
                }
            },
            "template": "blue"
        },
        "elements": [
            {
                "tag": "div",
                "text": {
                    "tag": "lark_md",
                    "content": body_en,
                    "i18n_content": {
                        "zh_cn": body_zh,
                        "en_us": body_en,
                    }
                }
            }
        ]
    });
    serde_json::to_string(&card).unwrap_or_default()
}

// --- Helpers ---

/// Escape backticks in lark_md content to prevent Feishu from interpreting them
/// as code tags (which triggers "unsupported html tag code" errors).
fn sanitize_lark_md(s: &str) -> String {
    s.replace('`', "'")
}

fn root_dir_basename(root_dir: &str) -> String {
    Path::new(root_dir)
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or(root_dir)
        .to_string()
}

fn dir_display_name(rel_path: &str) -> String {
    if rel_path == "." {
        return ".".to_string();
    }
    // Show the last component
    Path::new(rel_path)
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or(rel_path)
        .to_string()
}

/// Build display labels for a list of directories.
///
/// Returns a vector of `(label, is_conflict)` tuples.
///
/// - `detect_conflicts`: when true, short-name conflicts (multiple dirs sharing
///   the same base name) are resolved by showing the full relative path for
///   conflicting entries. When false, short names are always used.
/// - The root directory "." is always shown as the root basename with a 📁 prefix.
/// - Labels are NOT truncated here; callers apply truncation as needed.
fn build_dir_labels(
    dirs: &[String],
    root_dir: &str,
    detect_conflicts: bool,
) -> Vec<(String, bool)> {
    if dirs.is_empty() {
        return Vec::new();
    }

    let short_names: Vec<String> = dirs
        .iter()
        .map(|dir| {
            if dir == "." {
                root_dir_basename(root_dir)
            } else {
                dir_display_name(dir)
            }
        })
        .collect();

    let conflict_map: HashMap<String, usize> = if detect_conflicts {
        let mut map: HashMap<String, usize> = HashMap::new();
        for sn in &short_names {
            *map.entry(sn.clone()).or_insert(0) += 1;
        }
        map
    } else {
        HashMap::new()
    };

    dirs.iter()
        .enumerate()
        .map(|(i, dir)| {
            let short = &short_names[i];
            let conflict = detect_conflicts && conflict_map.get(short).copied().unwrap_or(1) > 1;

            let display = if dir == "." {
                format!("📁 {}", root_dir_basename(root_dir))
            } else if conflict {
                format!("📁 {}", dir)
            } else {
                format!("📁 {}", short)
            };

            (display, conflict)
        })
        .collect()
}

fn truncate_str(s: &str, max_len: usize) -> String {
    if s.chars().count() <= max_len {
        s.to_string()
    } else {
        let truncated: String = s.chars().take(max_len.saturating_sub(2)).collect();
        format!("{}..", truncated)
    }
}

/// Char-safe truncation: keep the first `max_chars` characters.
fn truncate_str_head(s: &str, max_chars: usize) -> String {
    let chars: Vec<char> = s.chars().collect();
    if chars.len() <= max_chars {
        s.to_string()
    } else {
        let truncated: String = chars
            .into_iter()
            .take(max_chars.saturating_sub(1))
            .collect();
        format!("{}", truncated)
    }
}

/// Char-safe truncation: keep the last `max_chars` characters, prefix with "…".
fn truncate_str_tail(s: &str, max_chars: usize) -> String {
    let chars: Vec<char> = s.chars().collect();
    if chars.len() <= max_chars {
        s.to_string()
    } else {
        let truncated: String = chars
            .into_iter()
            .rev()
            .take(max_chars.saturating_sub(1))
            .collect::<Vec<_>>()
            .into_iter()
            .rev()
            .collect();
        format!("{}", truncated)
    }
}

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

    /// Helper: find a select_static element nested inside an action.actions array.
    /// Since select_static must be wrapped in an action component to render
    /// in Feishu cards, callers should not search for bare select_static at
    /// the top level of card elements.
    fn find_select_static_in_elements(elements: &[Value]) -> Option<&Value> {
        elements
            .iter()
            .filter(|e| e["tag"].as_str() == Some("action"))
            .find_map(|e| {
                e["actions"].as_array().and_then(|actions| {
                    actions
                        .iter()
                        .find(|a| a["tag"].as_str() == Some("select_static"))
                })
            })
    }

    #[test]
    fn test_expand_tilde_home() {
        let home = std::env::var("HOME").unwrap_or_else(|_| "/home/user".to_string());
        let result = expand_tilde("~/projects");
        assert_eq!(result, format!("{}/projects", home));
    }

    #[test]
    fn test_expand_tilde_alone() {
        let home = std::env::var("HOME").unwrap_or_else(|_| "/home/user".to_string());
        let result = expand_tilde("~");
        assert_eq!(result, home);
    }

    #[test]
    fn test_expand_tilde_no_tilde() {
        let result = expand_tilde("/abs/path");
        assert_eq!(result, "/abs/path");
    }

    #[test]
    fn test_tokenize_keywords() {
        let tokens = tokenize_keywords("beam daemon");
        assert_eq!(tokens, vec!["beam", "daemon"]);
    }

    #[test]
    fn test_tokenize_keywords_extra_spaces() {
        let tokens = tokenize_keywords("  beam   daemon  ");
        assert_eq!(tokens, vec!["beam", "daemon"]);
    }

    #[test]
    fn test_tokenize_keywords_empty() {
        let tokens = tokenize_keywords("");
        assert!(tokens.is_empty());
    }

    #[test]
    fn test_match_dirs_and() {
        let dirs = vec![
            ".".to_string(),
            "beam-daemon".to_string(),
            "beam-core".to_string(),
            "beam-cli".to_string(),
            "docs/design/beam.md".to_string(),
            "README.md".to_string(),
        ];
        let matched = match_dirs(&dirs, &["beam", "daemon"]);
        assert_eq!(matched, vec!["beam-daemon".to_string()]);
    }

    #[test]
    fn test_match_dirs_case_insensitive() {
        let dirs = vec!["MyProject".to_string(), "myproject".to_string()];
        let matched = match_dirs(&dirs, &["myproject"]);
        assert_eq!(matched.len(), 2);
    }

    #[test]
    fn test_match_dirs_empty_keywords() {
        let dirs = vec!["a".to_string(), "b".to_string()];
        let matched = match_dirs(&dirs, &[]);
        assert_eq!(matched, dirs);
    }

    #[test]
    fn test_filter_dirs() {
        let dirs = vec![
            ".".to_string(),
            "crates/beam-daemon".to_string(),
            "crates/beam-core".to_string(),
            "docs".to_string(),
        ];
        let result = filter_dirs(&dirs, "crates beam");
        assert_eq!(
            result,
            vec![
                "crates/beam-daemon".to_string(),
                "crates/beam-core".to_string(),
            ]
        );
    }

    #[test]
    fn test_find_best_match_unique() {
        let dirs = vec![
            ".".to_string(),
            "projects/foo".to_string(),
            "projects/bar".to_string(),
            "projects".to_string(),
        ];
        let best = find_best_match(&dirs, "foo");
        assert_eq!(best, Some("projects/foo".to_string()));
    }

    #[test]
    fn test_find_best_match_ambiguous() {
        let dirs = vec![
            ".".to_string(),
            "foo/bar".to_string(),
            "foo/baz".to_string(),
        ];
        let best = find_best_match(&dirs, "foo");
        assert_eq!(best, None);
    }

    #[test]
    fn test_find_best_match_multiple_different_lengths_returns_none() {
        // Even though "foo" is shorter than "foo/bar/baz", there are 2 matches
        // and the new conservative logic requires exactly 1 match.
        let dirs = vec![
            ".".to_string(),
            "foo".to_string(),
            "foo/bar/baz".to_string(),
        ];
        let best = find_best_match(&dirs, "foo");
        assert_eq!(
            best, None,
            "2 matches (different lengths) should return None"
        );
    }

    #[test]
    fn test_find_best_match_no_match() {
        let dirs = vec![".".to_string(), "a".to_string(), "b".to_string()];
        let best = find_best_match(&dirs, "xyz");
        assert_eq!(best, None);
    }

    #[test]
    fn test_find_best_match_empty_search() {
        let dirs = vec![".".to_string(), "a".to_string()];
        let best = find_best_match(&dirs, "");
        assert_eq!(best, None);
    }

    #[test]
    fn test_is_dir_under_root_absolute() {
        let result = is_dir_under_root("/tmp/root/sub", "/tmp/root");
        assert!(result);
    }

    #[test]
    fn test_is_dir_under_root_not_under() {
        let result = is_dir_under_root("/etc/passwd", "/tmp/root");
        assert!(!result);
    }

    #[test]
    fn test_is_dir_under_root_equal() {
        let result = is_dir_under_root("/tmp/root", "/tmp/root");
        assert!(result);
    }

    #[test]
    fn test_is_dir_under_root_relative() {
        let result = is_dir_under_root("sub/dir", "/tmp/root");
        assert!(result);
    }

    #[test]
    fn test_is_dir_under_root_dot_root_accepts_relative() {
        // root="." should accept relative paths like "crates"
        assert!(is_dir_under_root("crates", "."));
        assert!(is_dir_under_root("crates/beam-daemon", "."));
    }

    #[test]
    fn test_is_dir_under_root_dot_root_rejects_escape() {
        assert!(!is_dir_under_root("../x", "."), ".. should be rejected");
        assert!(
            !is_dir_under_root("crates/../../etc", "."),
            ".. should be rejected"
        );
    }

    #[test]
    fn test_is_dir_under_root_dot_root_rejects_absolute() {
        assert!(
            !is_dir_under_root("/tmp/x", "."),
            "absolute path should be rejected when root is '.'"
        );
    }

    #[test]
    fn test_is_valid_candidate_dot_root() {
        let candidates = vec![
            "crates".to_string(),
            "crates/beam-daemon".to_string(),
            "src".to_string(),
        ];
        assert!(is_valid_candidate("crates", ".", &candidates));
        assert!(is_valid_candidate("src", ".", &candidates));
        assert!(!is_valid_candidate("nonexistent", ".", &candidates));
        assert!(!is_valid_candidate("../x", ".", &candidates));
        assert!(!is_valid_candidate("/tmp/x", ".", &candidates));
    }

    #[test]
    fn test_resolve_dir() {
        assert_eq!(resolve_dir("/root", "."), "/root");
        assert_eq!(resolve_dir("/root", "sub"), "/root/sub");
        assert_eq!(resolve_dir("/root", "sub/deep"), "/root/sub/deep");
    }

    #[test]
    fn test_record_recent_dir() {
        let mut store = RecentDirsStore::default();
        let key = "app:chat:user";
        record_recent_dir(&mut store, key, "project-a");
        record_recent_dir(&mut store, key, "project-b");
        record_recent_dir(&mut store, key, "project-a"); // should move to front
        let entries = &store.entries[key];
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].dir, "project-a");
        assert_eq!(entries[1].dir, "project-b");
    }

    #[test]
    fn test_record_recent_dir_trims() {
        let mut store = RecentDirsStore::default();
        let key = "app:chat:user";
        for i in 0..MAX_RECENT_DIRS + 5 {
            record_recent_dir(&mut store, key, &format!("dir-{}", i));
        }
        assert_eq!(store.entries[key].len(), MAX_RECENT_DIRS);
        // Most recent first
        assert_eq!(
            store.entries[key][0].dir,
            format!("dir-{}", MAX_RECENT_DIRS + 4)
        );
    }

    #[test]
    fn test_build_recent_dir_key() {
        assert_eq!(
            build_recent_dir_key("app1", "chat1", Some("user1")),
            "app1:chat1:user1"
        );
        assert_eq!(build_recent_dir_key("app1", "chat1", None), "app1:chat1");
        assert_eq!(
            build_recent_dir_key("app1", "chat1", Some("")),
            "app1:chat1"
        );
    }

    #[test]
    fn test_determine_root_working_dir() {
        let result = determine_root_working_dir(Some("/my/project"), &[]);
        assert_eq!(result, "/my/project");

        let result = determine_root_working_dir(None, &["/daemon/dir".to_string()]);
        assert_eq!(result, "/daemon/dir");

        let result = determine_root_working_dir(None, &[]);
        // Fallback is ".", expand_tilde(".") = "." (no tilde to expand)
        assert_eq!(result, ".");
    }

    #[test]
    fn test_scan_candidate_dirs_includes_root() {
        let tmp = std::env::temp_dir().join("beam_dir_select_test");
        let _ = std::fs::remove_dir_all(&tmp);
        std::fs::create_dir_all(&tmp).unwrap();
        std::fs::create_dir_all(tmp.join("sub_a")).unwrap();
        std::fs::create_dir_all(tmp.join("sub_b")).unwrap();
        std::fs::create_dir_all(tmp.join(".hidden_dir")).unwrap();
        std::fs::create_dir_all(tmp.join("__pycache__")).unwrap();
        std::fs::create_dir_all(tmp.join(".git")).unwrap();

        let dirs = scan_candidate_dirs(&tmp);
        assert!(dirs.contains(&".".to_string()));
        assert!(dirs.contains(&"sub_a".to_string()));
        assert!(dirs.contains(&"sub_b".to_string()));
        // Hidden and skipped dirs should not be included
        assert!(!dirs.iter().any(|d| d.contains(".hidden_dir")));
        assert!(!dirs.iter().any(|d| d.contains("__pycache__")));
        assert!(!dirs.iter().any(|d| d.contains(".git")));

        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_build_dir_select_card_contains_required_elements() {
        let recommended = vec![".".to_string(), "project-a".to_string()];
        let all = recommended.clone();
        let card = build_dir_select_card(
            "pending-1",
            "/home/user/projects",
            "帮我修复这个 bug",
            &recommended,
            &all,
            None,
            None,
            None,
            Some("zh"),
        );
        // Card should be valid JSON
        let _v: Value = serde_json::from_str(&card).expect("card should be valid JSON");
        assert!(card.contains("请选择工作目录"));
        assert!(card.contains("/home/user/projects"));
        assert!(card.contains("帮我修复这个 bug"));
        assert!(card.contains("dir_select_pick"));
        assert!(card.contains("dir_select_filter"));
        assert!(card.contains("dir_select_best"));
        assert!(card.contains("pending-1"));
        assert!(card.contains("dir_search_keyword"));
        // Verify directory button structure
        let v: Value = serde_json::from_str(&card).expect("card should be valid JSON");
        let elements = v["elements"]
            .as_array()
            .expect("elements should be an array");

        let action_groups: Vec<&Value> = elements
            .iter()
            .filter(|e| e["tag"].as_str() == Some("action"))
            .collect();
        assert!(
            !action_groups.is_empty(),
            "card should contain directory action groups"
        );
        let first_button = action_groups[0]["actions"]
            .as_array()
            .and_then(|actions| actions.first())
            .expect("action group should contain directory buttons");
        assert_eq!(
            first_button
                .pointer("/value/action")
                .and_then(Value::as_str),
            Some("dir_select_pick")
        );
        assert_eq!(
            first_button
                .pointer("/value/pending_id")
                .and_then(Value::as_str),
            Some("pending-1")
        );

        // Card should contain select_static dropdown inside an action.actions array.
        // A bare select_static as a top-level card element is not valid in the
        // Feishu card schema.
        let select_action = elements
            .iter()
            .filter(|e| e["tag"].as_str() == Some("action"))
            .find(|e| {
                e["actions"]
                    .as_array()
                    .map(|actions| {
                        actions
                            .iter()
                            .any(|a| a["tag"].as_str() == Some("select_static"))
                    })
                    .unwrap_or(false)
            })
            .expect("card should contain an action wrapping select_static dropdown");
        let select_static = select_action["actions"]
            .as_array()
            .unwrap()
            .iter()
            .find(|a| a["tag"].as_str() == Some("select_static"))
            .expect("action.actions should contain select_static");
        // Verify no bare select_static as a top-level element
        let top_level_select = elements
            .iter()
            .find(|e| e["tag"].as_str() == Some("select_static"));
        assert!(
            top_level_select.is_none(),
            "select_static must NOT be a bare top-level element; it must be inside action.actions"
        );
        let options = select_static["options"]
            .as_array()
            .expect("select_static should have options");
        assert!(!options.is_empty(), "select_static should have options");
        // Verify first option value is valid JSON with action/pending_id/working_dir
        let first_opt_val = options[0]["value"]
            .as_str()
            .expect("option value should be a string");
        let opt_parsed: Value =
            serde_json::from_str(first_opt_val).expect("option value should be valid JSON");
        assert_eq!(
            opt_parsed["action"].as_str(),
            Some("dir_select_pick"),
            "select option should have action=dir_select_pick"
        );
        assert_eq!(
            opt_parsed["pending_id"].as_str(),
            Some("pending-1"),
            "select option should have pending_id"
        );
        assert!(
            opt_parsed["working_dir"].as_str().is_some(),
            "select option should have working_dir"
        );

        // Verify form container structure
        let form = elements
            .iter()
            .find(|e| e["tag"].as_str() == Some("form"))
            .expect("card should contain a form element");
        assert_eq!(form["name"].as_str(), Some("dir_search_form"));

        let form_els = form["elements"]
            .as_array()
            .expect("form should have elements");

        // form elements must only contain input + buttons (no div)
        let tags: Vec<&str> = form_els
            .iter()
            .map(|e| e["tag"].as_str().unwrap_or(""))
            .collect();
        assert!(
            !tags.contains(&"div"),
            "form should NOT contain div elements, got: {:?}",
            tags
        );
        assert!(
            tags.contains(&"input"),
            "form should contain an input, got: {:?}",
            tags
        );
        assert!(
            tags.contains(&"button"),
            "form should contain buttons, got: {:?}",
            tags
        );

        // input must have default_value (not value dict)
        let input = form_els
            .iter()
            .find(|e| e["tag"].as_str() == Some("input"))
            .expect("form should contain an input");
        assert!(
            input["default_value"].is_string() || input["default_value"].is_null(),
            "input must have default_value, got value: {:?}",
            input.get("value")
        );

        // all buttons must have action_type=form_submit
        for btn in form_els
            .iter()
            .filter(|e| e["tag"].as_str() == Some("button"))
        {
            assert_eq!(
                btn["action_type"].as_str(),
                Some("form_submit"),
                "all form buttons must be form_submit"
            );
        }
    }

    #[test]
    fn test_build_dir_select_card_uses_english_for_en_locale() {
        let recommended = vec![".".to_string(), "project-a".to_string()];
        let all = recommended.clone();
        let card: Value = serde_json::from_str(&build_dir_select_card(
            "pending-1",
            "/home/user/projects",
            "你好",
            &recommended,
            &all,
            None,
            None,
            None,
            Some("en"),
        ))
        .expect("valid dir select card");
        assert_eq!(
            card.pointer("/elements/0/text/content")
                .and_then(Value::as_str),
            Some("📁 **Root:** /home/user/projects")
        );
        assert_eq!(
            card.pointer("/elements/0/text/i18n_content/zh_cn")
                .and_then(Value::as_str),
            Some("📁 **根目录:** /home/user/projects")
        );
        assert_eq!(
            card.pointer("/elements/1/text/content")
                .and_then(Value::as_str),
            Some("💬 **Message:** 你好")
        );
        assert_eq!(
            card.pointer("/elements/1/text/i18n_content/zh_cn")
                .and_then(Value::as_str),
            Some("💬 **消息:** 你好")
        );
        assert_eq!(
            card.pointer("/elements/2/text/content")
                .and_then(Value::as_str),
            Some("📋 **Recommended directories:**")
        );
        assert_eq!(
            card.pointer("/elements/2/text/i18n_content/zh_cn")
                .and_then(Value::as_str),
            Some("📋 **推荐目录:**")
        );
    }

    #[test]
    fn test_build_dir_select_card_with_message() {
        let card = build_dir_select_card(
            "p1",
            "/root",
            "test",
            &[],
            &[],
            None,
            None,
            Some("请先选择目录"),
            Some("zh"),
        );
        assert!(card.contains("请先选择目录"));
    }

    #[test]
    fn test_build_dir_session_starting_card() {
        let card = build_dir_session_starting_card("/home/user/projects", "my title", Some("zh"));
        assert!(card.contains("正在启动会话"));
        assert!(card.contains("/home/user/projects"));
        assert!(card.contains("my title"));
    }

    #[test]
    fn test_truncate_str() {
        assert_eq!(truncate_str("hello", 10), "hello");
        assert_eq!(truncate_str("hello world", 8), "hello ..");
        assert_eq!(truncate_str("hello", 5), "hello");
    }

    #[test]
    fn test_truncate_str_head_chinese() {
        // Chinese chars (3 bytes each) — byte slicing would panic
        let s = "你好世界这是一个很长的标题需要截断测试";
        let result = truncate_str_head(s, 10);
        assert!(result.chars().count() <= 10);
        assert!(result.ends_with(''));
        // Should not panic
    }

    #[test]
    fn test_truncate_str_tail_emoji() {
        let s = "/home/user/很长的路径/包含中文/and/emoji/🌟/test";
        let result = truncate_str_tail(s, 20);
        assert!(result.chars().count() <= 20);
        assert!(result.starts_with(''));
        // Should not panic
    }

    #[test]
    fn test_build_dir_select_card_utf8_safe() {
        // Chinese title + long root path must not panic
        let recommended = vec![".".to_string()];
        let all = recommended.clone();
        let long_root =
            "/home/user/这是一个很长的路径用来测试截断功能/包含中文字符/abc/def/ghi/jkl/mno";
        let chinese_title = "帮我修复这个生产环境的紧急bug非常着急请尽快处理谢谢";
        let card = build_dir_select_card(
            "p-utf8",
            long_root,
            chinese_title,
            &recommended,
            &all,
            None,
            None,
            None,
            Some("zh"),
        );
        // Should be valid JSON
        let _v: Value = serde_json::from_str(&card).expect("card should be valid JSON");
        assert!(card.contains("请选择工作目录"));
    }

    #[test]
    fn test_build_dir_select_card_truncates_excess_options() {
        // When there are more directories than MAX_BUTTON_DIRS (40),
        // the directory buttons are capped at 40, while the select_static
        // dropdown shows up to MAX_SELECT_DIRS (150).
        let many_dirs: Vec<String> = (0..200).map(|i| format!("project-{:03}", i)).collect();
        let card = build_dir_select_card(
            "pid",
            "/root",
            "test",
            &many_dirs,
            &many_dirs,
            None,
            None,
            None,
            Some("zh"),
        );
        let v: Value = serde_json::from_str(&card).expect("valid card JSON");

        let pick_button_count: usize = v["elements"]
            .as_array()
            .unwrap()
            .iter()
            .filter(|e| e["tag"].as_str() == Some("action"))
            .flat_map(|e| e["actions"].as_array().into_iter().flatten())
            .filter(|button| {
                button.pointer("/value/action").and_then(Value::as_str) == Some("dir_select_pick")
            })
            .count();
        assert_eq!(
            pick_button_count, 40,
            "directory buttons should be capped at MAX_BUTTON_DIRS"
        );
        // select_static should be inside action.actions and contain up to MAX_SELECT_DIRS options
        let select_static = find_select_static_in_elements(v["elements"].as_array().unwrap())
            .expect("card should have select_static inside action.actions");
        let option_count = select_static["options"].as_array().unwrap().len();
        assert_eq!(
            option_count, 150,
            "select_static options should be capped at MAX_SELECT_DIRS"
        );
    }

    #[test]
    fn test_build_dir_select_card_filtered_truncation_shows_count_in_label() {
        // When filtering produces more results than MAX_BUTTON_DIRS (40),
        // the section label should indicate the total count and button cap.
        let many_dirs: Vec<String> = (0..200).map(|i| format!("project-{:03}", i)).collect();
        let card = build_dir_select_card(
            "pid",
            "/root",
            "test",
            &[],
            &many_dirs,
            Some(&many_dirs),
            Some("proj"),
            None,
            Some("zh"),
        );
        // Section label should mention total count and button cap
        assert!(card.contains("共 200"), "label should show total count");
        assert!(
            card.contains("显示前 40"),
            "label should show button truncation limit"
        );
        // select_static label should also mention the total
        assert!(
            card.contains("更多匹配"),
            "should have select_static more-matches section"
        );
        assert!(
            card.contains("显示前 150"),
            "select_static label should show dropdown limit"
        );
        let v: Value = serde_json::from_str(&card).expect("valid card JSON");
        // Count only button action rows (not the select_static action wrapper)
        let button_row_count = v["elements"]
            .as_array()
            .unwrap()
            .iter()
            .filter(|e| e["tag"].as_str() == Some("action"))
            .filter(|e| {
                e["actions"]
                    .as_array()
                    .and_then(|a| a.first())
                    .and_then(|first| first["tag"].as_str())
                    == Some("button")
            })
            .count();
        assert_eq!(
            button_row_count, 40,
            "filtered result button rows capped at MAX_BUTTON_DIRS"
        );
        let select_opts = find_select_static_in_elements(v["elements"].as_array().unwrap())
            .expect("should have select_static inside action.actions")["options"]
            .as_array()
            .unwrap()
            .len();
        assert_eq!(
            select_opts, 150,
            "select_static options capped at MAX_SELECT_DIRS"
        );
    }

    #[test]
    fn test_build_dir_select_card_no_truncation_when_under_limit() {
        // When there are fewer directories than MAX_BUTTON_DIRS (40),
        // all fit in buttons and no truncation label.
        let few_dirs: Vec<String> = (0..10).map(|i| format!("project-{:02}", i)).collect();
        let card = build_dir_select_card(
            "pid",
            "/root",
            "test",
            &few_dirs,
            &few_dirs,
            Some(&few_dirs),
            Some("proj"),
            None,
            Some("zh"),
        );
        // No "显示前" message when under limit (10 <= 40)
        assert!(
            !card.contains("显示前"),
            "no truncation label when under button limit"
        );
        let v: Value = serde_json::from_str(&card).expect("valid card JSON");
        // Count only button action rows (not the select_static action wrapper)
        let result_row_count = v["elements"]
            .as_array()
            .unwrap()
            .iter()
            .filter(|e| e["tag"].as_str() == Some("action"))
            .filter(|e| {
                e["actions"]
                    .as_array()
                    .and_then(|a| a.first())
                    .and_then(|first| first["tag"].as_str())
                    == Some("button")
            })
            .count();
        assert_eq!(result_row_count, 10, "all 10 result rows should be present");
        // select_static should exist inside action.actions (showing all results as dropdown)
        assert!(
            find_select_static_in_elements(v["elements"].as_array().unwrap()).is_some(),
            "select_static should be present even with few results"
        );
    }

    #[test]
    fn test_build_dir_select_card_filtered_unique_short_names_single_button() {
        // Filtered results with unique short names should show a single button
        // per directory displaying the short name.
        let dirs = vec!["project-a".to_string(), "project-b".to_string()];
        let card = build_dir_select_card(
            "pid",
            "/home/user/workspace",
            "test",
            &[],
            &dirs,
            Some(&dirs),
            Some("proj"),
            None,
            Some("zh"),
        );
        let v: Value = serde_json::from_str(&card).expect("valid card JSON");
        // Filter only action elements that contain buttons (exclude select_static wrapper)
        let actions: Vec<&Value> = v["elements"]
            .as_array()
            .unwrap()
            .iter()
            .filter(|e| {
                e["tag"].as_str() == Some("action")
                    && e["actions"]
                        .as_array()
                        .and_then(|a| a.first())
                        .and_then(|first| first["tag"].as_str())
                        == Some("button")
            })
            .collect();
        // Each dir → exactly 1 action row
        assert_eq!(actions.len(), 2, "should have 2 action rows");

        let mut working_dirs: Vec<String> = Vec::new();
        for action in &actions {
            let buttons = action["actions"]
                .as_array()
                .expect("action row should have buttons");
            assert_eq!(
                buttons.len(),
                1,
                "filtered row should have exactly 1 button (no extra full-path button)"
            );
            let content = buttons[0]
                .pointer("/text/content")
                .and_then(Value::as_str)
                .unwrap_or_default();
            // Short name should be shown (not relative path like "project-a")
            assert!(
                content.contains("project-a") || content.contains("project-b"),
                "button should display short name, got: {}",
                content
            );
            // No full resolved path in button text
            assert!(
                !content.contains("/home/user/workspace"),
                "button text should NOT contain full resolved path"
            );
            // Value must have correct action, pending_id, and working_dir
            assert_eq!(
                buttons[0].pointer("/value/action").and_then(Value::as_str),
                Some("dir_select_pick"),
                "button action should be dir_select_pick"
            );
            assert_eq!(
                buttons[0]
                    .pointer("/value/pending_id")
                    .and_then(Value::as_str),
                Some("pid"),
                "button pending_id should be pid"
            );
            let wd = buttons[0]
                .pointer("/value/working_dir")
                .and_then(Value::as_str)
                .expect("button should have working_dir");
            working_dirs.push(wd.to_string());
        }
        working_dirs.sort();
        let mut expected_dirs = dirs.clone();
        expected_dirs.sort();
        assert_eq!(
            working_dirs, expected_dirs,
            "collected working_dirs should match input dirs"
        );
    }

    #[test]
    fn test_build_dir_select_card_filtered_duplicate_short_names_shows_relative_path() {
        // When filtered results contain dirs with the same short name
        // (e.g. a/foo and b/foo both resolve to "foo"), conflicting entries
        // should display the relative path to distinguish them.
        let dirs = vec![
            "group-a/project".to_string(),
            "group-b/project".to_string(),
            "group-a/unique".to_string(),
        ];
        let card = build_dir_select_card(
            "pid",
            "/root",
            "test",
            &[],
            &dirs,
            Some(&dirs),
            Some("proj"),
            None,
            Some("zh"),
        );
        let v: Value = serde_json::from_str(&card).expect("valid card JSON");
        // Filter only action elements that contain buttons (exclude select_static wrapper)
        let actions: Vec<&Value> = v["elements"]
            .as_array()
            .unwrap()
            .iter()
            .filter(|e| {
                e["tag"].as_str() == Some("action")
                    && e["actions"]
                        .as_array()
                        .and_then(|a| a.first())
                        .and_then(|first| first["tag"].as_str())
                        == Some("button")
            })
            .collect();
        assert_eq!(actions.len(), 3, "should have 3 action rows");

        for action in &actions {
            let buttons = action["actions"]
                .as_array()
                .expect("action row should have buttons");
            assert_eq!(buttons.len(), 1, "each row must have exactly 1 button");

            let working_dir = buttons[0]
                .pointer("/value/working_dir")
                .and_then(Value::as_str)
                .unwrap_or_default();
            let content = buttons[0]
                .pointer("/text/content")
                .and_then(Value::as_str)
                .unwrap_or_default();

            match working_dir {
                "group-a/project" | "group-b/project" => {
                    // Conflicting short name "project" → must show relative path
                    assert!(
                        content.contains("group"),
                        "conflicting dir '{}' should show relative path, got: {}",
                        working_dir,
                        content
                    );
                    assert!(
                        !content.ends_with("project") || content.contains("group"),
                        "conflicting dir '{}' should NOT show bare short name, got: {}",
                        working_dir,
                        content
                    );
                }
                "group-a/unique" => {
                    // Unique short name "unique" → short name is fine
                    assert!(
                        content.contains("unique"),
                        "unique dir should show short name, got: {}",
                        content
                    );
                }
                _ => panic!("unexpected working_dir: {}", working_dir),
            }
        }
    }

    #[test]
    fn test_build_dir_select_card_recommended_duplicate_short_names_stays_short() {
        // Even when recommended dirs have duplicate short names, the
        // recommended section should keep showing short names.
        let dirs = vec!["group-a/project".to_string(), "group-b/project".to_string()];
        let card = build_dir_select_card(
            "pid",
            "/root",
            "test",
            &dirs,
            &dirs,
            None, // recommended section
            None,
            None,
            Some("zh"),
        );
        let v: Value = serde_json::from_str(&card).expect("valid card JSON");
        let all_buttons: Vec<&Value> = v["elements"]
            .as_array()
            .unwrap()
            .iter()
            .filter(|e| e["tag"].as_str() == Some("action"))
            .flat_map(|e| e["actions"].as_array().into_iter().flatten())
            .filter(|child| child["tag"].as_str() == Some("button"))
            .collect();

        // Both buttons should show "project" (short name), not the full rel path
        for button in &all_buttons {
            let content = button
                .pointer("/text/content")
                .and_then(Value::as_str)
                .unwrap_or_default();
            assert!(
                content.contains("project"),
                "recommended section should show short name, got: {}",
                content
            );
            assert!(
                !content.contains("group"),
                "recommended section should NOT show full relative path, got: {}",
                content
            );
        }
    }

    #[test]
    fn test_build_dir_select_card_filtered_working_dir_value_correct() {
        // The button value must always carry the real working_dir (relative path),
        // regardless of what is displayed in the button text.
        let dirs = vec![
            "deep/nested/path/api".to_string(),
            "another/deep/nested/path/api".to_string(),
        ];
        let card = build_dir_select_card(
            "pid",
            "/home/user/workspace",
            "test",
            &[],
            &dirs,
            Some(&dirs),
            Some("api"),
            None,
            Some("zh"),
        );
        let v: Value = serde_json::from_str(&card).expect("valid card JSON");
        // Filter only action elements that contain buttons (exclude select_static wrapper)
        let actions: Vec<&Value> = v["elements"]
            .as_array()
            .unwrap()
            .iter()
            .filter(|e| {
                e["tag"].as_str() == Some("action")
                    && e["actions"]
                        .as_array()
                        .and_then(|a| a.first())
                        .and_then(|first| first["tag"].as_str())
                        == Some("button")
            })
            .collect();

        let mut found_dirs: Vec<String> = Vec::new();
        for action in &actions {
            let buttons = action["actions"]
                .as_array()
                .expect("action row should have buttons");
            assert_eq!(buttons.len(), 1, "each row must have exactly 1 button");
            let wd = buttons[0]
                .pointer("/value/working_dir")
                .and_then(Value::as_str)
                .unwrap()
                .to_string();
            let action_val = buttons[0]
                .pointer("/value/action")
                .and_then(Value::as_str)
                .unwrap();
            assert_eq!(action_val, "dir_select_pick");
            let pending = buttons[0]
                .pointer("/value/pending_id")
                .and_then(Value::as_str)
                .unwrap();
            assert_eq!(pending, "pid");
            found_dirs.push(wd);
        }
        found_dirs.sort();
        let mut expected: Vec<String> = dirs.clone();
        expected.sort();
        assert_eq!(
            found_dirs, expected,
            "button values must contain the correct relative working_dir"
        );
    }

    #[test]
    #[cfg(unix)]
    fn test_scan_candidate_dirs_skips_symlinks() {
        use std::os::unix::fs as unix_fs;

        let tmp = std::env::temp_dir().join("beam_dir_select_symlink_test");
        let _ = std::fs::remove_dir_all(&tmp);
        std::fs::create_dir_all(&tmp).unwrap();
        // real subdirectory
        std::fs::create_dir_all(tmp.join("real_subdir")).unwrap();
        // symlink pointing outside root
        let external = std::env::temp_dir().join("beam_dir_select_external_target");
        std::fs::create_dir_all(&external).unwrap();
        unix_fs::symlink(&external, tmp.join("symlink_out")).unwrap();
        // symlink pointing inside root
        unix_fs::symlink(tmp.join("real_subdir"), tmp.join("symlink_in")).unwrap();

        let dirs = scan_candidate_dirs(&tmp);
        // Root "." must be present
        assert!(dirs.contains(&".".to_string()), "root must be included");
        // Real directory must be present
        assert!(
            dirs.contains(&"real_subdir".to_string()),
            "real_subdir must be included, got: {:?}",
            dirs
        );
        // Symlink to external must NOT be present
        assert!(
            !dirs
                .iter()
                .any(|d| d == "symlink_out" || d.contains("symlink_out")),
            "symlink to external must be excluded, got: {:?}",
            dirs
        );
        // Symlink to internal must NOT be present (all symlinks skipped)
        assert!(
            !dirs
                .iter()
                .any(|d| d == "symlink_in" || d.contains("symlink_in")),
            "symlink to internal must be excluded, got: {:?}",
            dirs
        );

        let _ = std::fs::remove_dir_all(&tmp);
        let _ = std::fs::remove_dir_all(&external);
    }

    #[test]
    fn test_prune_expired_pending_creates_removes_expired() {
        use std::collections::HashMap;

        let mut map: HashMap<String, PendingCreateSession> = HashMap::new();

        // Helper to make a minimal pending with given age
        let make_pending = |id: &str, created_at_ms: i64| -> PendingCreateSession {
            PendingCreateSession {
                pending_id: id.to_string(),
                lark_app_id: "app".to_string(),
                chat_id: "chat".to_string(),
                chat_type: None,
                message_id: "msg".to_string(),
                anchor: "anchor".to_string(),
                scope: SessionScope::Chat,
                thread_id: None,
                root_id: None,
                title: "t".to_string(),
                text: "".to_string(),
                sender_open_id: None,
                sender_type: None,
                locale: None,
                parent_id: None,
                mentions_json: "[]".to_string(),
                quota_key: None,
                created_at: created_at_ms,
                cli_id: "codex".to_string(),
                cli_bin: "codex".to_string(),
                cli_args: Vec::new(),
                root_working_dir: "/tmp".to_string(),
                candidate_dirs: vec![".".to_string()],
                card_message_id: None,
            }
        };

        let now: i64 = 1_700_000_000_000; // some fixed timestamp in ms

        // fresh: created 5 min ago
        map.insert(
            "fresh".to_string(),
            make_pending("fresh", now - 5 * 60 * 1000),
        );
        // borderline: created 29 min ago (within TTL)
        map.insert(
            "borderline".to_string(),
            make_pending("borderline", now - 29 * 60 * 1000),
        );
        // expired: created 31 min ago
        map.insert(
            "expired".to_string(),
            make_pending("expired", now - 31 * 60 * 1000),
        );
        // very old: created 2 hours ago
        map.insert(
            "old".to_string(),
            make_pending("old", now - 2 * 60 * 60 * 1000),
        );

        assert_eq!(map.len(), 4);

        let pruned = prune_expired_pending_creates(&mut map, now);
        assert_eq!(pruned, 2, "should prune 2 expired entries");
        assert_eq!(map.len(), 2);
        assert!(map.contains_key("fresh"));
        assert!(map.contains_key("borderline"));
        assert!(!map.contains_key("expired"));
        assert!(!map.contains_key("old"));
    }

    #[test]
    fn test_pending_create_session_defaults_missing_cli_args() {
        let raw = r#"{
            "pending_id":"pid",
            "lark_app_id":"app",
            "chat_id":"chat",
            "message_id":"msg",
            "anchor":"anchor",
            "scope":"chat",
            "title":"title",
            "text":"",
            "created_at":123,
            "cli_id":"codex",
            "cli_bin":"codex",
            "root_working_dir":"/tmp",
            "candidate_dirs":["."]
        }"#;
        let pending: PendingCreateSession = serde_json::from_str(raw).expect("deserialize");
        assert!(pending.cli_args.is_empty());
    }

    #[test]
    fn test_prune_expired_pending_creates_empty_map() {
        let mut map: HashMap<String, PendingCreateSession> = HashMap::new();
        let pruned = prune_expired_pending_creates(&mut map, 1_700_000_000_000);
        assert_eq!(pruned, 0);
        assert!(map.is_empty());
    }

    #[test]
    fn test_select_static_is_inside_action_actions_not_top_level() {
        // Verify that the "more directories" select_static dropdown is placed
        // inside an action.actions array, NOT as a bare top-level element.
        // A bare select_static would not render in Feishu cards.
        let dirs: Vec<String> = (0..10).map(|i| format!("project-{:03}", i)).collect();
        let card = build_dir_select_card(
            "pid",
            "/root",
            "test",
            &dirs,
            &dirs,
            None,
            None,
            None,
            Some("zh"),
        );
        let v: Value = serde_json::from_str(&card).expect("valid card JSON");
        let elements = v["elements"].as_array().unwrap();

        // Must NOT have select_static as a bare top-level element
        let bare_select = elements
            .iter()
            .find(|e| e["tag"].as_str() == Some("select_static"));
        assert!(
            bare_select.is_none(),
            "select_static must NOT be a bare top-level card element"
        );

        // Must have select_static inside action.actions
        let found = find_select_static_in_elements(elements);
        assert!(
            found.is_some(),
            "select_static must be wrapped inside action.actions"
        );

        // Verify the select_static has the correct content
        let select = found.unwrap();
        let placeholder = select["placeholder"]["content"].as_str().unwrap_or("");
        assert!(
            placeholder.contains("选择"),
            "select_static should have a meaningful placeholder"
        );
        let options = select["options"].as_array().unwrap();
        assert!(!options.is_empty(), "select_static must have options");

        // Each option value must be valid JSON with action/pending_id/working_dir
        for opt in options {
            let val_str = opt["value"]
                .as_str()
                .expect("option value must be a string");
            let parsed: Value =
                serde_json::from_str(val_str).expect("option value must be valid JSON");
            assert_eq!(
                parsed["action"].as_str(),
                Some("dir_select_pick"),
                "each select option must have action=dir_select_pick"
            );
            assert_eq!(
                parsed["pending_id"].as_str(),
                Some("pid"),
                "each select option must have pending_id"
            );
            assert!(
                parsed["working_dir"].as_str().is_some(),
                "each select option must have working_dir"
            );
        }
    }

    #[test]
    fn test_search_area_has_interactive_input_and_button() {
        // Verify the search area is interactive: it has a form with an input
        // and at least one button. The standalone div before the form is purely
        // instructional text — it should NOT look like a clickable title.
        let dirs = vec!["project-a".to_string(), "project-b".to_string()];
        let card = build_dir_select_card(
            "pid",
            "/root",
            "test",
            &dirs,
            &dirs,
            None,
            Some("test"),
            None,
            Some("zh"),
        );
        let v: Value = serde_json::from_str(&card).expect("valid card JSON");
        let elements = v["elements"].as_array().unwrap();

        // Find the form element (must exist and be interactive)
        let form = elements
            .iter()
            .find(|e| e["tag"].as_str() == Some("form"))
            .expect("card must contain a form element for search");
        assert_eq!(form["name"].as_str(), Some("dir_search_form"));

        let form_els = form["elements"]
            .as_array()
            .expect("form must have elements");

        // Must have an input element
        let input = form_els
            .iter()
            .find(|e| e["tag"].as_str() == Some("input"))
            .expect("search form must have an input element");
        assert_eq!(
            input["name"].as_str(),
            Some("dir_search_keyword"),
            "input name should be dir_search_keyword"
        );
        // Input should have a default_value reflecting the search keyword
        assert_eq!(
            input["default_value"].as_str(),
            Some("test"),
            "input default_value should reflect the search keyword"
        );
        // Placeholder should be instructive
        let placeholder = input["placeholder"]["content"].as_str().unwrap_or("");
        assert!(
            placeholder.contains("关键词"),
            "input placeholder should mention keywords"
        );

        // Must have form_submit buttons
        let buttons: Vec<&Value> = form_els
            .iter()
            .filter(|e| e["tag"].as_str() == Some("button"))
            .collect();
        assert!(!buttons.is_empty(), "form must have at least one button");

        // One button must be the filter button with action=dir_select_filter
        let filter_btn = buttons
            .iter()
            .find(|b| {
                b.pointer("/value/action").and_then(Value::as_str) == Some("dir_select_filter")
            })
            .expect("must have a dir_select_filter button");
        assert_eq!(
            filter_btn["action_type"].as_str(),
            Some("form_submit"),
            "filter button must be form_submit"
        );

        // One button must be the best-match button with action=dir_select_best
        let best_btn = buttons
            .iter()
            .find(|b| b.pointer("/value/action").and_then(Value::as_str) == Some("dir_select_best"))
            .expect("must have a dir_select_best button");
        assert_eq!(
            best_btn["action_type"].as_str(),
            Some("form_submit"),
            "best-match button must be form_submit"
        );

        // The instructional div before the form should be plain text,
        // not a bold title that looks clickable
        let instructional_div = elements
            .iter()
            .filter(|e| e["tag"].as_str() == Some("div"))
            .find(|e| {
                e.pointer("/text/content")
                    .and_then(Value::as_str)
                    .map(|s| s.contains("下方输入") && s.contains("筛选"))
                    .unwrap_or(false)
            })
            .expect("card must have instructional text for the search area");
        let div_content = instructional_div
            .pointer("/text/content")
            .and_then(Value::as_str)
            .unwrap_or("");
        // The text should be instructional, not a bold clickable-looking title
        assert!(
            !div_content.contains("**搜索目录:**"),
            "instructional div should NOT use bold title '搜索目录:' that looks clickable"
        );
        assert!(
            div_content.contains("输入关键词") || div_content.contains("筛选"),
            "instructional div should explain how to search"
        );
    }
}