arf-console 0.3.3-alpha.2

A cross-platform R console written in Rust
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
//! History import functionality for migrating from other R environments.
//!
//! This module provides importers for:
//! - **radian**: Parse `~/.radian_history` format with timestamps and modes
//! - **R native**: Parse `.Rhistory` plain text format
//! - **arf**: Copy from another arf SQLite database
//!
//! # Radian History Format
//!
//! ```text
//! # time: 2024-01-15 10:30:00 UTC
//! # mode: r
//! +library(dplyr)
//! +iris %>%
//! +  filter(Species == "setosa")
//!
//! # time: 2024-01-15 10:31:00 UTC
//! # mode: shell
//! +ls -la
//! ```
//!
//! # R Native History Format
//!
//! Simple text file with one command per line (no metadata):
//! ```text
//! library(dplyr)
//! print("hello")
//! ```

use anyhow::{Context, Result, bail};
use chrono::{DateTime, NaiveDateTime, Utc};
use reedline::{HistoryItem, SqliteBackedHistory};
use std::collections::HashSet;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};

/// A parsed history entry ready for import.
#[derive(Debug, Clone)]
pub struct ImportEntry {
    /// The command text.
    pub command: String,
    /// Timestamp when the command was executed (if available).
    pub timestamp: Option<DateTime<Utc>>,
    /// Mode in which the command was executed (r, shell, browse).
    pub mode: Option<String>,
}

/// Result of an import operation.
#[derive(Debug, Default)]
pub struct ImportResult {
    /// Number of R entries successfully imported.
    pub r_imported: usize,
    /// Number of shell entries successfully imported.
    pub shell_imported: usize,
    /// Number of entries skipped (empty, unknown mode, errors).
    pub skipped: usize,
    /// Number of duplicate entries skipped.
    pub duplicates_skipped: usize,
    /// Warning messages for non-fatal issues.
    pub warnings: Vec<String>,
}

impl ImportResult {
    /// Total number of entries imported.
    #[allow(dead_code)]
    pub fn total_imported(&self) -> usize {
        self.r_imported + self.shell_imported
    }
}

/// Get the default radian history file path.
pub fn default_radian_path() -> PathBuf {
    dirs::home_dir()
        .map(|h| h.join(".radian_history"))
        .unwrap_or_else(|| PathBuf::from(".radian_history"))
}

/// Get the default R history file path.
///
/// Checks R_HISTFILE environment variable first, then falls back to .Rhistory
/// in the current directory.
pub fn default_r_history_path() -> PathBuf {
    if let Ok(path) = std::env::var("R_HISTFILE") {
        return PathBuf::from(path);
    }
    PathBuf::from(".Rhistory")
}

/// Parse a radian history file.
///
/// The radian format uses:
/// - `# time: YYYY-MM-DD HH:MM:SS UTC` for timestamps
/// - `# mode: <mode>` for the input mode
/// - `+<line>` for command lines (may span multiple lines)
/// - Blank lines separate entries
pub fn parse_radian_history(path: &Path) -> Result<Vec<ImportEntry>> {
    let file = File::open(path)
        .with_context(|| format!("Failed to open radian history: {}", path.display()))?;
    let reader = BufReader::new(file);

    let mut entries = Vec::new();
    let mut current_timestamp: Option<DateTime<Utc>> = None;
    let mut current_mode: Option<String> = None;
    let mut current_lines: Vec<String> = Vec::new();

    for line_result in reader.lines() {
        let line = line_result.with_context(|| "Failed to read line from radian history")?;

        if line.starts_with("# time: ") {
            // Finalize previous entry if we have one
            if !current_lines.is_empty() {
                let command = current_lines.join("\n");
                entries.push(ImportEntry {
                    command,
                    timestamp: current_timestamp,
                    mode: current_mode.take(),
                });
                current_lines.clear();
            }

            // Reset mode on new timestamp boundary to prevent carryover
            // (e.g., if previous entry had "# mode: shell" but new entry has no mode line)
            current_mode = None;

            // Parse timestamp: "# time: 2024-01-15 10:30:00 UTC"
            let time_str = line.trim_start_matches("# time: ").trim();
            let time_str = time_str.trim_end_matches(" UTC");
            current_timestamp = NaiveDateTime::parse_from_str(time_str, "%Y-%m-%d %H:%M:%S")
                .ok()
                .map(|naive| naive.and_utc());
        } else if line.starts_with("# mode: ") {
            current_mode = Some(line.trim_start_matches("# mode: ").trim().to_string());
        } else if let Some(content) = line.strip_prefix('+') {
            // Handle CRLF line endings - strip trailing \r
            let content = content.strip_suffix('\r').unwrap_or(content);
            current_lines.push(content.to_string());
        } else if line.trim().is_empty() {
            // Empty line can separate entries
            if !current_lines.is_empty() {
                let command = current_lines.join("\n");
                entries.push(ImportEntry {
                    command,
                    timestamp: current_timestamp,
                    mode: current_mode.take(),
                });
                current_lines.clear();
                current_timestamp = None;
            }
        }
        // Ignore other lines (comments, etc.)
    }

    // Don't forget the last entry
    if !current_lines.is_empty() {
        let command = current_lines.join("\n");
        entries.push(ImportEntry {
            command,
            timestamp: current_timestamp,
            mode: current_mode.take(),
        });
    }

    Ok(entries)
}

/// Parse an R native history file (.Rhistory).
///
/// The R native format is simply one command per line, no metadata.
/// Multi-line commands are NOT supported by R's native history.
pub fn parse_r_history(path: &Path) -> Result<Vec<ImportEntry>> {
    let file = File::open(path)
        .with_context(|| format!("Failed to open R history: {}", path.display()))?;
    let reader = BufReader::new(file);

    let mut entries = Vec::new();

    for line_result in reader.lines() {
        let line = line_result.with_context(|| "Failed to read line from R history")?;
        // Only trim line endings, preserve leading whitespace (e.g., indented code)
        let content = line.trim_end();
        // Skip empty/whitespace-only lines
        if !content.trim().is_empty() {
            entries.push(ImportEntry {
                command: content.to_string(),
                timestamp: None,
                mode: Some("r".to_string()),
            });
        }
    }

    Ok(entries)
}

/// Copy entries from another arf SQLite history database.
///
/// The mode is inferred from the filename:
/// - Files named `shell.db` are treated as shell history
/// - All other files are treated as R history
pub fn parse_arf_history(path: &Path) -> Result<Vec<ImportEntry>> {
    use reedline::History;

    if !path.exists() {
        bail!("arf history database not found: {}", path.display());
    }

    // Infer mode from filename
    let is_shell = path
        .file_name()
        .and_then(|n| n.to_str())
        .is_some_and(|n| n == "shell.db");
    let mode = if is_shell {
        Some("shell".to_string())
    } else {
        Some("r".to_string())
    };

    // Open source history database
    let source = SqliteBackedHistory::with_file(path.to_path_buf(), None, None)
        .with_context(|| format!("Failed to open arf history database: {}", path.display()))?;

    // Query all history items
    let query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
    let items = source
        .search(query)
        .with_context(|| "Failed to query arf history")?;

    let entries: Vec<ImportEntry> = items
        .into_iter()
        .map(|item| ImportEntry {
            command: item.command_line,
            timestamp: item.start_timestamp,
            mode: mode.clone(),
        })
        .collect();

    Ok(entries)
}

/// Target databases for import.
pub struct ImportTargets {
    /// R history database.
    pub r_history: SqliteBackedHistory,
    /// Shell history database.
    pub shell_history: SqliteBackedHistory,
}

/// Determine the target database for an entry based on its mode.
///
/// Returns `Some(true)` for shell, `Some(false)` for R/browse, `None` for unknown modes.
fn classify_mode(mode: Option<&str>) -> Option<bool> {
    match mode {
        Some("shell") => Some(true),               // shell database
        Some("r") | Some("browse") => Some(false), // R database
        None => Some(false),                       // Default to R database
        Some(_) => None,                           // Unknown mode - skip
    }
}

/// Pre-loaded set of existing history entries for duplicate detection (anti-join).
///
/// For entries with timestamps, duplicates are detected by `(command_line, timestamp)`.
/// For entries without timestamps, duplicates are detected by `command_line` alone.
///
/// Note: `commands` intentionally contains **all** command_lines from the database,
/// including those that also have timestamps. This is because a no-timestamp import
/// entry (e.g., from `.Rhistory`) should be considered a duplicate if the same command
/// text already exists in the DB with any timestamp (e.g., from a prior radian import).
/// The `.Rhistory` import is typically a one-time migration, so this conservative
/// approach is acceptable.
pub struct DedupSet {
    /// `(command_line, unix_timestamp_millis)` pairs for matching entries with timestamps.
    command_timestamps: HashSet<(String, i64)>,
    /// All distinct `command_line` values for matching entries without timestamps.
    commands: HashSet<String>,
}

impl DedupSet {
    /// Build a dedup set from an existing history database opened for writing.
    ///
    /// Used in the non-dry-run import path where the database is already opened
    /// via `SqliteBackedHistory::with_file()` for writing.
    ///
    /// Note: reedline's deserialization falls back to `Utc::now()` when a
    /// stored timestamp is not a valid millisecond value. This means the
    /// millis round-trip (`DateTime → i64 → DateTime → i64`) could
    /// theoretically differ from the raw DB value for corrupt rows. In
    /// practice this cannot happen because reedline always writes
    /// `timestamp_millis()`, but [`from_db`] avoids this entirely by
    /// reading the raw i64 directly.
    pub fn from_history(history: &SqliteBackedHistory) -> Result<Self> {
        use reedline::History;

        let query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let items = history
            .search(query)
            .context("Failed to query existing history for dedup")?;

        let mut command_timestamps = HashSet::new();
        let mut commands = HashSet::new();

        // INVARIANT: `commands` must contain every command_line that appears
        // in `command_timestamps`, because `is_duplicate` uses `commands` as
        // a fast-path filter for both timestamped and non-timestamped lookups.
        for item in items {
            commands.insert(item.command_line.clone());
            if let Some(ts) = item.start_timestamp {
                command_timestamps.insert((item.command_line, ts.timestamp_millis()));
            }
        }

        Ok(DedupSet {
            command_timestamps,
            commands,
        })
    }

    /// Build a dedup set by opening a history database in read-only mode.
    ///
    /// Used in the dry-run path to avoid WAL/shm side-effect files that
    /// `SqliteBackedHistory::with_file()` would create.
    pub fn from_db(path: &Path) -> Result<Self> {
        use rusqlite::{Connection, OpenFlags};

        let db = Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_ONLY)
            .with_context(|| format!("Failed to open history database: {}", path.display()))?;

        // reedline stores start_timestamp as Unix milliseconds (i64) in SQLite.
        // We read the raw value directly to stay consistent with from_history(),
        // which converts DateTime<Utc> back to millis via timestamp_millis().
        let mut stmt = db
            .prepare("SELECT command_line, start_timestamp FROM history")
            .with_context(|| {
                format!(
                    "Failed to query history table in '{}' (not an arf database?)",
                    path.display()
                )
            })?;

        let mut command_timestamps = HashSet::new();
        let mut commands = HashSet::new();

        // INVARIANT: `commands` must contain every command_line that appears
        // in `command_timestamps`, because `is_duplicate` uses `commands` as
        // a fast-path filter for both timestamped and non-timestamped lookups.
        let rows = stmt
            .query_map([], |row| {
                let command: String = row.get(0)?;
                let ts_millis: Option<i64> = row.get(1)?;
                Ok((command, ts_millis))
            })
            .context("Failed to query history for dedup")?;

        for row in rows {
            let (command, ts_millis) = row.context("Failed to read history row")?;
            commands.insert(command.clone());
            if let Some(ms) = ts_millis {
                command_timestamps.insert((command, ms));
            }
        }

        Ok(DedupSet {
            command_timestamps,
            commands,
        })
    }

    /// Check if an entry already exists in the set.
    fn is_duplicate(&self, command: &str, timestamp: Option<&DateTime<Utc>>) -> bool {
        // Fast path: if the command doesn't exist at all, skip the allocation
        // needed for the (String, i64) HashSet lookup.
        if !self.commands.contains(command) {
            return false;
        }
        if let Some(ts) = timestamp {
            self.command_timestamps
                .contains(&(command.to_string(), ts.timestamp_millis()))
        } else {
            true // command exists in commands set (checked above)
        }
    }
}

/// Simulate importing entries without accessing databases.
///
/// Uses the same classification logic as `import_entries` to provide
/// accurate counts and warnings for `--dry-run` mode.
///
/// If dedup sets are provided, duplicate entries will be counted in
/// `duplicates_skipped` instead of being "imported". Each dedup set
/// is optional independently, so dedup works even if only one target
/// database exists.
pub fn import_entries_dry_run(
    entries: &[ImportEntry],
    r_dedup: Option<&DedupSet>,
    shell_dedup: Option<&DedupSet>,
) -> ImportResult {
    let mut result = ImportResult::default();

    for entry in entries {
        if entry.command.trim().is_empty() {
            result.skipped += 1;
            continue;
        }

        // Classify mode and skip unknown modes
        let is_shell = match classify_mode(entry.mode.as_deref()) {
            Some(is_shell) => is_shell,
            None => {
                let mode = entry.mode.as_deref().unwrap_or("?");
                let cmd_preview: String = entry.command.chars().take(30).collect();
                result.warnings.push(format!(
                    "Skipped unknown mode '{}': {}...",
                    mode, cmd_preview
                ));
                result.skipped += 1;
                continue;
            }
        };

        // Check for duplicates if the corresponding dedup set is available
        let dedup_set = if is_shell { shell_dedup } else { r_dedup };
        if let Some(dedup) = dedup_set
            && dedup.is_duplicate(&entry.command, entry.timestamp.as_ref())
        {
            result.duplicates_skipped += 1;
            continue;
        }

        if is_shell {
            result.shell_imported += 1;
        } else {
            result.r_imported += 1;
        }
    }

    result
}

/// Import entries into arf history databases, routing by mode.
///
/// - Entries with mode "shell" go to the shell history database
/// - Entries with mode "r", "browse", or None go to the R history database
/// - Entries with unknown modes are skipped with a warning
///
/// If `hostname_override` is provided, all imported entries will have their
/// hostname field set to this value, making them distinguishable from native
/// arf entries.
///
/// If `skip_duplicates` is true, entries that already exist in the target
/// database are skipped (anti-join on command + timestamp).
///
/// Note: The dedup set is built once from the database state at the start
/// of the import. Duplicates *within* the import batch are not detected
/// (e.g., if the source file contains the same entry twice, both will be
/// imported). This is acceptable because real-world history files rarely
/// contain exact duplicates, and the primary use case is idempotent
/// re-import across separate invocations.
///
/// For dry-run previews, use [`import_entries_dry_run`] instead.
pub fn import_entries(
    targets: &mut ImportTargets,
    entries: Vec<ImportEntry>,
    hostname_override: Option<&str>,
    skip_duplicates: bool,
) -> Result<ImportResult> {
    use reedline::History;

    // Build dedup sets if duplicate skipping is enabled
    let (r_dedup, shell_dedup) = if skip_duplicates {
        (
            Some(DedupSet::from_history(&targets.r_history)?),
            Some(DedupSet::from_history(&targets.shell_history)?),
        )
    } else {
        (None, None)
    };

    let mut result = ImportResult::default();

    for entry in entries {
        if entry.command.trim().is_empty() {
            result.skipped += 1;
            continue;
        }

        // Classify mode and skip unknown modes
        let is_shell = match classify_mode(entry.mode.as_deref()) {
            Some(is_shell) => is_shell,
            None => {
                let mode = entry.mode.as_deref().unwrap_or("?");
                let cmd_preview: String = entry.command.chars().take(30).collect();
                result.warnings.push(format!(
                    "Skipped unknown mode '{}': {}...",
                    mode, cmd_preview
                ));
                result.skipped += 1;
                continue;
            }
        };

        // Check for duplicates if enabled
        if let Some(dedup_set) = if is_shell { &shell_dedup } else { &r_dedup }
            && dedup_set.is_duplicate(&entry.command, entry.timestamp.as_ref())
        {
            result.duplicates_skipped += 1;
            continue;
        }

        // Create a HistoryItem for import
        let item = HistoryItem {
            id: None, // Will be assigned by the database
            command_line: entry.command,
            start_timestamp: entry.timestamp,
            session_id: None,
            hostname: hostname_override.map(|s| s.to_string()),
            cwd: None,
            duration: None,
            exit_status: None,
            more_info: None,
        };

        // Route to appropriate database based on mode
        let save_result = if is_shell {
            targets.shell_history.save(item)
        } else {
            targets.r_history.save(item)
        };

        match save_result {
            Ok(_) => {
                if is_shell {
                    result.shell_imported += 1;
                } else {
                    result.r_imported += 1;
                }
            }
            Err(e) => {
                result
                    .warnings
                    .push(format!("Failed to import entry: {}", e));
                result.skipped += 1;
            }
        }
    }

    Ok(result)
}

/// Validate that a table name is safe for use in SQL queries.
///
/// Table names must contain only alphanumeric characters and underscores,
/// must not be empty, and must contain at least one alphanumeric character.
/// This prevents SQL injection attacks and avoids confusing names like `_` or `___`.
pub fn validate_table_name(name: &str) -> Result<()> {
    if name.is_empty() {
        bail!("Table name cannot be empty");
    }
    if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
        bail!(
            "Invalid table name '{}': must contain only alphanumeric characters and underscores",
            name
        );
    }
    // SQLite identifiers cannot start with a digit
    if name.chars().next().is_some_and(|c| c.is_ascii_digit()) {
        bail!("Invalid table name '{}': cannot start with a digit", name);
    }
    // Require at least one alphanumeric character to avoid confusing names like "_" or "___"
    if !name.chars().any(|c| c.is_ascii_alphanumeric()) {
        bail!(
            "Invalid table name '{}': must contain at least one alphanumeric character",
            name
        );
    }
    Ok(())
}

/// Parse entries from a unified arf export file that contains both R and shell history.
///
/// This function reads from a SQLite file that has separate tables for R and shell history,
/// as created by `export_history`. The table names are specified by the caller.
///
/// If a table doesn't exist, it's silently skipped (no error).
pub fn parse_unified_arf_history(
    path: &Path,
    r_table: &str,
    shell_table: &str,
) -> Result<Vec<ImportEntry>> {
    use rusqlite::{Connection, OpenFlags};

    // Validate table names to prevent SQL injection
    validate_table_name(r_table)?;
    validate_table_name(shell_table)?;

    // Ensure the R and shell tables have different names to avoid duplicate entries
    if r_table == shell_table {
        bail!(
            "R table name and shell table name must be different (both are '{}')",
            r_table
        );
    }

    if !path.exists() {
        bail!("arf export file not found: {}", path.display());
    }

    let db = Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_ONLY)
        .with_context(|| format!("Failed to open arf export file: {}", path.display()))?;

    let mut entries = Vec::new();

    // Try to read R history table
    if table_exists(&db, r_table)? {
        let r_entries = read_history_table(&db, r_table, "r")?;
        entries.extend(r_entries);
    }

    // Try to read shell history table
    if table_exists(&db, shell_table)? {
        let shell_entries = read_history_table(&db, shell_table, "shell")?;
        entries.extend(shell_entries);
    }

    Ok(entries)
}

/// Check if a table exists in the database.
fn table_exists(db: &rusqlite::Connection, table_name: &str) -> Result<bool> {
    let count: i32 = db
        .query_row(
            "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=?",
            [table_name],
            |row| row.get(0),
        )
        .context("Failed to check if table exists")?;
    Ok(count > 0)
}

/// Read history entries from a table.
fn read_history_table(
    db: &rusqlite::Connection,
    table_name: &str,
    mode: &str,
) -> Result<Vec<ImportEntry>> {
    use chrono::TimeZone;

    // Use format! for table name since it can't be parameterized in SQL.
    // Table names are validated by validate_table_name() before reaching here.
    let query = format!(
        "SELECT command_line, start_timestamp FROM \"{}\" ORDER BY id",
        table_name
    );

    let mut stmt = db.prepare(&query).with_context(|| {
        format!(
            "Failed to query table '{}' (not a valid history table?)",
            table_name
        )
    })?;

    let rows = stmt
        .query_map([], |row| {
            let command: String = row.get(0)?;
            let ts_millis: Option<i64> = row.get(1)?;
            Ok((command, ts_millis))
        })
        .context("Failed to query history")?;

    let mut entries = Vec::new();
    for row in rows {
        let (command, ts_millis) = row.context("Failed to read history row")?;
        let timestamp = ts_millis.and_then(|ms| Utc.timestamp_millis_opt(ms).single());
        entries.push(ImportEntry {
            command,
            timestamp,
            mode: Some(mode.to_string()),
        });
    }

    Ok(entries)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_parse_radian_history_basic() {
        let mut file = NamedTempFile::new().unwrap();
        writeln!(file, "# time: 2024-01-15 10:30:00 UTC").unwrap();
        writeln!(file, "# mode: r").unwrap();
        writeln!(file, "+library(dplyr)").unwrap();
        writeln!(file).unwrap();
        writeln!(file, "# time: 2024-01-15 10:31:00 UTC").unwrap();
        writeln!(file, "# mode: shell").unwrap();
        writeln!(file, "+ls -la").unwrap();

        let entries = parse_radian_history(file.path()).unwrap();
        assert_eq!(entries.len(), 2);

        assert_eq!(entries[0].command, "library(dplyr)");
        assert_eq!(entries[0].mode, Some("r".to_string()));
        assert!(entries[0].timestamp.is_some());

        assert_eq!(entries[1].command, "ls -la");
        assert_eq!(entries[1].mode, Some("shell".to_string()));
    }

    #[test]
    fn test_parse_radian_history_multiline() {
        let mut file = NamedTempFile::new().unwrap();
        writeln!(file, "# time: 2024-01-15 10:30:00 UTC").unwrap();
        writeln!(file, "# mode: r").unwrap();
        writeln!(file, "+iris %>%").unwrap();
        writeln!(file, "+  filter(Species == \"setosa\") %>%").unwrap();
        writeln!(file, "+  head()").unwrap();

        let entries = parse_radian_history(file.path()).unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(
            entries[0].command,
            "iris %>%\n  filter(Species == \"setosa\") %>%\n  head()"
        );
    }

    #[test]
    fn test_parse_radian_history_empty_file() {
        let file = NamedTempFile::new().unwrap();
        let entries = parse_radian_history(file.path()).unwrap();
        assert!(entries.is_empty());
    }

    #[test]
    fn test_parse_r_history_basic() {
        let mut file = NamedTempFile::new().unwrap();
        writeln!(file, "library(dplyr)").unwrap();
        writeln!(file, "print(\"hello\")").unwrap();
        writeln!(file).unwrap(); // Empty line should be skipped
        writeln!(file, "summary(iris)").unwrap();

        let entries = parse_r_history(file.path()).unwrap();
        assert_eq!(entries.len(), 3);
        assert_eq!(entries[0].command, "library(dplyr)");
        assert_eq!(entries[1].command, "print(\"hello\")");
        assert_eq!(entries[2].command, "summary(iris)");

        // All entries should have mode "r" and no timestamp
        for entry in &entries {
            assert_eq!(entry.mode, Some("r".to_string()));
            assert!(entry.timestamp.is_none());
        }
    }

    #[test]
    fn test_parse_r_history_empty_file() {
        let file = NamedTempFile::new().unwrap();
        let entries = parse_r_history(file.path()).unwrap();
        assert!(entries.is_empty());
    }

    #[test]
    fn test_parse_r_history_preserves_leading_whitespace() {
        let mut file = NamedTempFile::new().unwrap();
        // Simulate indented code that might appear in .Rhistory
        writeln!(file, "if (TRUE) {{").unwrap();
        writeln!(file, r#"  print("indented")"#).unwrap();
        writeln!(file, "}}").unwrap();

        let entries = parse_r_history(file.path()).unwrap();
        assert_eq!(entries.len(), 3);
        // Leading whitespace should be preserved
        assert_eq!(entries[1].command, r#"  print("indented")"#);
    }

    #[test]
    fn test_default_paths() {
        // These just verify the functions don't panic
        let radian_path = default_radian_path();
        assert!(radian_path.to_string_lossy().contains("radian_history"));

        let r_path = default_r_history_path();
        assert!(
            r_path.to_string_lossy().contains("Rhistory") || std::env::var("R_HISTFILE").is_ok()
        );
    }

    #[test]
    fn test_import_entry_struct() {
        let entry = ImportEntry {
            command: "test".to_string(),
            timestamp: Some(Utc::now()),
            mode: Some("r".to_string()),
        };
        assert_eq!(entry.command, "test");
        assert!(entry.timestamp.is_some());
        assert_eq!(entry.mode, Some("r".to_string()));
    }

    #[test]
    fn test_parse_radian_history_timestamp_parsing() {
        let mut file = NamedTempFile::new().unwrap();
        writeln!(file, "# time: 2024-06-15 14:30:45 UTC").unwrap();
        writeln!(file, "# mode: r").unwrap();
        writeln!(file, "+test()").unwrap();

        let entries = parse_radian_history(file.path()).unwrap();
        assert_eq!(entries.len(), 1);

        let ts = entries[0].timestamp.unwrap();
        assert_eq!(
            ts.format("%Y-%m-%d %H:%M:%S").to_string(),
            "2024-06-15 14:30:45"
        );
    }

    #[test]
    fn test_parse_radian_history_browse_mode() {
        let mut file = NamedTempFile::new().unwrap();
        writeln!(file, "# time: 2024-01-15 10:30:00 UTC").unwrap();
        writeln!(file, "# mode: browse").unwrap();
        writeln!(file, "+n").unwrap();

        let entries = parse_radian_history(file.path()).unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].mode, Some("browse".to_string()));
    }

    fn create_test_targets(temp_dir: &tempfile::TempDir) -> ImportTargets {
        let r_path = temp_dir.path().join("r.db");
        let shell_path = temp_dir.path().join("shell.db");
        ImportTargets {
            r_history: SqliteBackedHistory::with_file(r_path, None, None).unwrap(),
            shell_history: SqliteBackedHistory::with_file(shell_path, None, None).unwrap(),
        }
    }

    #[test]
    fn test_import_entries_to_sqlite() {
        use reedline::History;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let mut targets = create_test_targets(&temp_dir);

        // Create test entries (R mode)
        let entries = vec![
            ImportEntry {
                command: "library(ggplot2)".to_string(),
                timestamp: Some(Utc::now()),
                mode: Some("r".to_string()),
            },
            ImportEntry {
                command: "print('hello')".to_string(),
                timestamp: None,
                mode: Some("r".to_string()),
            },
        ];

        let result = import_entries(&mut targets, entries, None, false).unwrap();

        assert_eq!(result.r_imported, 2);
        assert_eq!(result.shell_imported, 0);
        assert_eq!(result.skipped, 0);
        assert!(result.warnings.is_empty());

        // Verify entries were imported to R history
        let query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let items = targets.r_history.search(query).unwrap();
        assert_eq!(items.len(), 2);

        let commands: Vec<&str> = items.iter().map(|i| i.command_line.as_str()).collect();
        assert!(commands.contains(&"library(ggplot2)"));
        assert!(commands.contains(&"print('hello')"));
    }

    #[test]
    fn test_import_entries_routes_shell_mode() {
        use reedline::History;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let mut targets = create_test_targets(&temp_dir);

        // Create mixed mode entries
        let entries = vec![
            ImportEntry {
                command: "library(dplyr)".to_string(),
                timestamp: None,
                mode: Some("r".to_string()),
            },
            ImportEntry {
                command: "ls -la".to_string(),
                timestamp: None,
                mode: Some("shell".to_string()),
            },
            ImportEntry {
                command: "pwd".to_string(),
                timestamp: None,
                mode: Some("shell".to_string()),
            },
        ];

        let result = import_entries(&mut targets, entries, None, false).unwrap();

        assert_eq!(result.r_imported, 1);
        assert_eq!(result.shell_imported, 2);
        assert_eq!(result.skipped, 0);

        // Verify R history
        let r_query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let r_items = targets.r_history.search(r_query).unwrap();
        assert_eq!(r_items.len(), 1);
        assert_eq!(r_items[0].command_line, "library(dplyr)");

        // Verify shell history
        let shell_query =
            reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let shell_items = targets.shell_history.search(shell_query).unwrap();
        assert_eq!(shell_items.len(), 2);
        let shell_commands: Vec<&str> = shell_items
            .iter()
            .map(|i| i.command_line.as_str())
            .collect();
        assert!(shell_commands.contains(&"ls -la"));
        assert!(shell_commands.contains(&"pwd"));
    }

    #[test]
    fn test_import_entries_dry_run() {
        // Create mixed mode entries
        let entries = vec![
            ImportEntry {
                command: "test_r".to_string(),
                timestamp: None,
                mode: Some("r".to_string()),
            },
            ImportEntry {
                command: "test_shell".to_string(),
                timestamp: None,
                mode: Some("shell".to_string()),
            },
            ImportEntry {
                command: "unknown_mode".to_string(),
                timestamp: None,
                mode: Some("python".to_string()), // Unknown mode
            },
            ImportEntry {
                command: "   ".to_string(), // Whitespace-only, should be skipped
                timestamp: None,
                mode: Some("r".to_string()),
            },
        ];

        // import_entries_dry_run doesn't need database handles
        let result = import_entries_dry_run(&entries, None, None);

        assert_eq!(result.r_imported, 1);
        assert_eq!(result.shell_imported, 1);
        assert_eq!(result.skipped, 2); // unknown mode + whitespace-only
        assert_eq!(result.warnings.len(), 1); // warning for unknown mode
        assert!(result.warnings[0].contains("python"));
    }

    #[test]
    fn test_import_entries_skips_empty() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let mut targets = create_test_targets(&temp_dir);

        let entries = vec![
            ImportEntry {
                command: "valid".to_string(),
                timestamp: None,
                mode: None,
            },
            ImportEntry {
                command: "   ".to_string(), // Whitespace only - should be skipped
                timestamp: None,
                mode: None,
            },
            ImportEntry {
                command: "".to_string(), // Empty - should be skipped
                timestamp: None,
                mode: None,
            },
        ];

        let result = import_entries(&mut targets, entries, None, false).unwrap();

        assert_eq!(result.r_imported, 1); // "valid" goes to R (mode: None)
        assert_eq!(result.shell_imported, 0);
        assert_eq!(result.skipped, 2);
    }

    #[test]
    fn test_import_entries_skips_unknown_modes() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let mut targets = create_test_targets(&temp_dir);

        let entries = vec![
            ImportEntry {
                command: "valid_r".to_string(),
                timestamp: None,
                mode: Some("r".to_string()),
            },
            ImportEntry {
                command: "valid_shell".to_string(),
                timestamp: None,
                mode: Some("shell".to_string()),
            },
            ImportEntry {
                command: "unknown_mode_cmd".to_string(),
                timestamp: None,
                mode: Some("python".to_string()), // Unknown mode
            },
            ImportEntry {
                command: "another_unknown".to_string(),
                timestamp: None,
                mode: Some("jupyter".to_string()), // Unknown mode
            },
        ];

        let result = import_entries(&mut targets, entries, None, false).unwrap();

        assert_eq!(result.r_imported, 1);
        assert_eq!(result.shell_imported, 1);
        assert_eq!(result.skipped, 2);
        assert_eq!(result.warnings.len(), 2);
        assert!(result.warnings[0].contains("python"));
        assert!(result.warnings[1].contains("jupyter"));
    }

    #[test]
    fn test_import_entries_handles_browse_mode() {
        use reedline::History;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let mut targets = create_test_targets(&temp_dir);

        let entries = vec![
            ImportEntry {
                command: "n".to_string(), // debug step
                timestamp: None,
                mode: Some("browse".to_string()),
            },
            ImportEntry {
                command: "c".to_string(), // continue
                timestamp: None,
                mode: Some("browse".to_string()),
            },
        ];

        let result = import_entries(&mut targets, entries, None, false).unwrap();

        // browse mode should go to R database
        assert_eq!(result.r_imported, 2);
        assert_eq!(result.shell_imported, 0);
        assert_eq!(result.skipped, 0);

        // Verify entries are in R history
        let query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let items = targets.r_history.search(query).unwrap();
        assert_eq!(items.len(), 2);
    }

    #[test]
    fn test_parse_arf_history_not_found() {
        use tempfile::TempDir;

        // Use TempDir to guarantee a non-existent file path
        let temp_dir = TempDir::new().unwrap();
        let missing_path = temp_dir.path().join("nonexistent.db");

        let result = parse_arf_history(&missing_path);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("not found"));
    }

    #[test]
    fn test_parse_arf_history_infers_mode_from_filename() {
        use reedline::History;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();

        // Create an R history database
        let r_path = temp_dir.path().join("r.db");
        let mut r_db = SqliteBackedHistory::with_file(r_path.clone(), None, None).unwrap();
        r_db.save(HistoryItem {
            id: None,
            command_line: "summary(iris)".to_string(),
            start_timestamp: None,
            session_id: None,
            hostname: None,
            cwd: None,
            duration: None,
            exit_status: None,
            more_info: None,
        })
        .unwrap();
        drop(r_db); // Close the database

        // Create a shell history database
        let shell_path = temp_dir.path().join("shell.db");
        let mut shell_db = SqliteBackedHistory::with_file(shell_path.clone(), None, None).unwrap();
        shell_db
            .save(HistoryItem {
                id: None,
                command_line: "ls -la".to_string(),
                start_timestamp: None,
                session_id: None,
                hostname: None,
                cwd: None,
                duration: None,
                exit_status: None,
                more_info: None,
            })
            .unwrap();
        drop(shell_db);

        // Parse R history - should have mode "r"
        let r_entries = parse_arf_history(&r_path).unwrap();
        assert_eq!(r_entries.len(), 1);
        assert_eq!(r_entries[0].mode, Some("r".to_string()));

        // Parse shell history - should have mode "shell"
        let shell_entries = parse_arf_history(&shell_path).unwrap();
        assert_eq!(shell_entries.len(), 1);
        assert_eq!(shell_entries[0].mode, Some("shell".to_string()));
    }

    #[test]
    fn test_arf_shell_to_shell_import() {
        use reedline::History;
        use tempfile::TempDir;

        // Use separate directories for source and target to avoid conflicts
        let source_dir = TempDir::new().unwrap();
        let target_dir = TempDir::new().unwrap();

        // Create a source shell history database
        let source_path = source_dir.path().join("old_shell.db");
        let mut source_db =
            SqliteBackedHistory::with_file(source_path.clone(), None, None).unwrap();
        source_db
            .save(HistoryItem {
                id: None,
                command_line: "git status".to_string(),
                start_timestamp: Some(Utc::now()),
                session_id: None,
                hostname: None,
                cwd: None,
                duration: None,
                exit_status: None,
                more_info: None,
            })
            .unwrap();
        drop(source_db);

        // Note: filename doesn't end with "shell.db" so it will be treated as R
        // This tests that only exact "shell.db" filename triggers shell mode
        let entries = parse_arf_history(&source_path).unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].mode, Some("r".to_string())); // Not shell!

        // Now test with exact "shell.db" filename
        let shell_source_path = source_dir.path().join("shell.db");
        std::fs::copy(&source_path, &shell_source_path).unwrap();

        let shell_entries = parse_arf_history(&shell_source_path).unwrap();
        assert_eq!(shell_entries.len(), 1);
        assert_eq!(shell_entries[0].mode, Some("shell".to_string()));

        // Import to target databases (in separate directory)
        let mut targets = create_test_targets(&target_dir);
        let result = import_entries(&mut targets, shell_entries, None, false).unwrap();

        assert_eq!(result.r_imported, 0);
        assert_eq!(result.shell_imported, 1);

        // Verify it went to shell database
        let query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let items = targets.shell_history.search(query).unwrap();
        assert_eq!(items.len(), 1);
        assert_eq!(items[0].command_line, "git status");
    }

    #[test]
    fn test_end_to_end_radian_import_with_shell() {
        use reedline::History;
        use tempfile::TempDir;

        // Create a radian history file with both R and shell commands
        let mut source_file = NamedTempFile::new().unwrap();
        writeln!(source_file, "# time: 2024-03-15 09:00:00 UTC").unwrap();
        writeln!(source_file, "# mode: r").unwrap();
        writeln!(source_file, "+summary(mtcars)").unwrap();
        writeln!(source_file).unwrap();
        writeln!(source_file, "# time: 2024-03-15 09:01:00 UTC").unwrap();
        writeln!(source_file, "# mode: shell").unwrap();
        writeln!(source_file, "+git status").unwrap();
        writeln!(source_file).unwrap();
        writeln!(source_file, "# time: 2024-03-15 09:02:00 UTC").unwrap();
        writeln!(source_file, "# mode: r").unwrap();
        writeln!(source_file, "+plot(mtcars$mpg, mtcars$hp)").unwrap();

        // Parse the radian history
        let entries = parse_radian_history(source_file.path()).unwrap();
        assert_eq!(entries.len(), 3);

        // Import to SQLite
        let temp_dir = TempDir::new().unwrap();
        let mut targets = create_test_targets(&temp_dir);

        let result = import_entries(&mut targets, entries, None, false).unwrap();
        assert_eq!(result.r_imported, 2);
        assert_eq!(result.shell_imported, 1);

        // Verify R history
        let r_query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let r_items = targets.r_history.search(r_query).unwrap();
        assert_eq!(r_items.len(), 2);
        // Check timestamps were preserved
        assert!(r_items.iter().all(|i| i.start_timestamp.is_some()));

        // Verify shell history
        let shell_query =
            reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let shell_items = targets.shell_history.search(shell_query).unwrap();
        assert_eq!(shell_items.len(), 1);
        assert_eq!(shell_items[0].command_line, "git status");
        assert!(shell_items[0].start_timestamp.is_some());
    }

    #[test]
    fn test_import_entries_with_hostname_override() {
        use reedline::History;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let mut targets = create_test_targets(&temp_dir);

        let entries = vec![
            ImportEntry {
                command: "library(dplyr)".to_string(),
                timestamp: None,
                mode: Some("r".to_string()),
            },
            ImportEntry {
                command: "ls -la".to_string(),
                timestamp: None,
                mode: Some("shell".to_string()),
            },
        ];

        // Import with custom hostname
        let result = import_entries(&mut targets, entries, Some("radian-import"), false).unwrap();

        assert_eq!(result.r_imported, 1);
        assert_eq!(result.shell_imported, 1);

        // Verify R history has the custom hostname
        let r_query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let r_items = targets.r_history.search(r_query).unwrap();
        assert_eq!(r_items.len(), 1);
        assert_eq!(r_items[0].hostname, Some("radian-import".to_string()));

        // Verify shell history also has the custom hostname
        let shell_query =
            reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let shell_items = targets.shell_history.search(shell_query).unwrap();
        assert_eq!(shell_items.len(), 1);
        assert_eq!(shell_items[0].hostname, Some("radian-import".to_string()));
    }

    // === Edge case tests for regression prevention ===

    #[test]
    fn test_parse_radian_history_mode_not_carried_over() {
        // Regression test: mode should NOT carry over from previous entry
        // when a new timestamp boundary is encountered without a mode line.
        let mut file = NamedTempFile::new().unwrap();
        // First entry with explicit shell mode
        writeln!(file, "# time: 2024-01-15 10:30:00 UTC").unwrap();
        writeln!(file, "# mode: shell").unwrap();
        writeln!(file, "+ls -la").unwrap();
        writeln!(file).unwrap();
        // Second entry WITHOUT mode line - should NOT inherit "shell" from previous
        writeln!(file, "# time: 2024-01-15 10:31:00 UTC").unwrap();
        writeln!(file, "+library(dplyr)").unwrap();

        let entries = parse_radian_history(file.path()).unwrap();
        assert_eq!(entries.len(), 2);

        // First entry should be shell
        assert_eq!(entries[0].command, "ls -la");
        assert_eq!(entries[0].mode, Some("shell".to_string()));

        // Second entry should have no mode (None), not "shell"
        assert_eq!(entries[1].command, "library(dplyr)");
        assert_eq!(entries[1].mode, None);
    }

    #[test]
    fn test_parse_radian_history_consecutive_timestamps_without_commands() {
        // Edge case: consecutive timestamp headers without commands should not cause issues
        let mut file = NamedTempFile::new().unwrap();
        // First timestamp with no command lines
        writeln!(file, "# time: 2024-01-15 10:30:00 UTC").unwrap();
        writeln!(file, "# mode: r").unwrap();
        // Empty line acts as separator
        writeln!(file).unwrap();
        // Second timestamp immediately follows
        writeln!(file, "# time: 2024-01-15 10:31:00 UTC").unwrap();
        writeln!(file, "# mode: shell").unwrap();
        writeln!(file, "+git status").unwrap();

        let entries = parse_radian_history(file.path()).unwrap();
        // Only one entry should be parsed (the one with actual command)
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].command, "git status");
        assert_eq!(entries[0].mode, Some("shell".to_string()));
    }

    #[test]
    fn test_parse_radian_history_file_not_found() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let missing_path = temp_dir.path().join("nonexistent_radian_history");

        let result = parse_radian_history(&missing_path);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("Failed to open radian history"));
    }

    #[test]
    fn test_parse_r_history_file_not_found() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let missing_path = temp_dir.path().join("nonexistent_Rhistory");

        let result = parse_r_history(&missing_path);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("Failed to open R history"));
    }

    #[test]
    fn test_parse_radian_history_mode_reset_between_entries() {
        // Another regression test: ensure mode is properly reset between entries
        // even when separated by empty lines
        let mut file = NamedTempFile::new().unwrap();
        writeln!(file, "# time: 2024-01-15 10:30:00 UTC").unwrap();
        writeln!(file, "# mode: shell").unwrap();
        writeln!(file, "+pwd").unwrap();
        writeln!(file).unwrap(); // Empty line separator
        writeln!(file, "# time: 2024-01-15 10:31:00 UTC").unwrap();
        // No mode line for this entry
        writeln!(file, "+summary(iris)").unwrap();
        writeln!(file).unwrap();
        writeln!(file, "# time: 2024-01-15 10:32:00 UTC").unwrap();
        writeln!(file, "# mode: browse").unwrap();
        writeln!(file, "+n").unwrap();

        let entries = parse_radian_history(file.path()).unwrap();
        assert_eq!(entries.len(), 3);

        assert_eq!(entries[0].mode, Some("shell".to_string()));
        assert_eq!(entries[1].mode, None); // Mode was reset, not carried over
        assert_eq!(entries[2].mode, Some("browse".to_string()));
    }

    #[test]
    fn test_parse_r_history_whitespace_only_lines_skipped() {
        let mut file = NamedTempFile::new().unwrap();
        writeln!(file, "library(dplyr)").unwrap();
        writeln!(file, "   ").unwrap(); // Whitespace-only line
        writeln!(file, "\t").unwrap(); // Tab-only line
        writeln!(file, "print(1)").unwrap();

        let entries = parse_r_history(file.path()).unwrap();
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].command, "library(dplyr)");
        assert_eq!(entries[1].command, "print(1)");
    }

    #[test]
    fn test_import_entries_none_mode_goes_to_r_database() {
        // Entries with mode=None should go to R database (default behavior)
        use reedline::History;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let mut targets = create_test_targets(&temp_dir);

        let entries = vec![ImportEntry {
            command: "summary(mtcars)".to_string(),
            timestamp: None,
            mode: None, // No mode specified
        }];

        let result = import_entries(&mut targets, entries, None, false).unwrap();
        assert_eq!(result.r_imported, 1);
        assert_eq!(result.shell_imported, 0);

        // Verify it's in R history
        let query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let items = targets.r_history.search(query).unwrap();
        assert_eq!(items.len(), 1);
        assert_eq!(items[0].command_line, "summary(mtcars)");
    }

    #[test]
    fn test_parse_radian_history_crlf_line_endings() {
        // Test that CRLF line endings (Windows) are handled correctly
        let mut file = NamedTempFile::new().unwrap();
        // Write with explicit \r\n
        file.write_all(b"# time: 2024-01-15 10:30:00 UTC\r\n")
            .unwrap();
        file.write_all(b"# mode: r\r\n").unwrap();
        file.write_all(b"+print(1)\r\n").unwrap();

        let entries = parse_radian_history(file.path()).unwrap();
        assert_eq!(entries.len(), 1);
        // Command should not have trailing \r
        assert_eq!(entries[0].command, "print(1)");
        assert!(!entries[0].command.ends_with('\r'));
    }

    // === Dedup (anti-join) tests ===

    #[test]
    fn test_import_skips_duplicates_with_timestamp() {
        use reedline::History;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let mut targets = create_test_targets(&temp_dir);

        let ts = DateTime::parse_from_rfc3339("2024-06-15T14:30:45Z")
            .unwrap()
            .with_timezone(&Utc);

        // First import: should succeed
        let entries = vec![ImportEntry {
            command: "library(dplyr)".to_string(),
            timestamp: Some(ts),
            mode: Some("r".to_string()),
        }];
        let result = import_entries(&mut targets, entries, None, true).unwrap();
        assert_eq!(result.r_imported, 1);
        assert_eq!(result.duplicates_skipped, 0);

        // Second import of the same entry: should be skipped as duplicate
        let entries = vec![ImportEntry {
            command: "library(dplyr)".to_string(),
            timestamp: Some(ts),
            mode: Some("r".to_string()),
        }];
        let result = import_entries(&mut targets, entries, None, true).unwrap();
        assert_eq!(result.r_imported, 0);
        assert_eq!(result.duplicates_skipped, 1);

        // Verify only one entry exists in the database
        let query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let items = targets.r_history.search(query).unwrap();
        assert_eq!(items.len(), 1);
    }

    #[test]
    fn test_import_skips_duplicates_without_timestamp() {
        use reedline::History;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let mut targets = create_test_targets(&temp_dir);

        // First import (no timestamp)
        let entries = vec![ImportEntry {
            command: "summary(iris)".to_string(),
            timestamp: None,
            mode: Some("r".to_string()),
        }];
        let result = import_entries(&mut targets, entries, None, true).unwrap();
        assert_eq!(result.r_imported, 1);
        assert_eq!(result.duplicates_skipped, 0);

        // Second import of the same command (no timestamp): should be skipped
        let entries = vec![ImportEntry {
            command: "summary(iris)".to_string(),
            timestamp: None,
            mode: Some("r".to_string()),
        }];
        let result = import_entries(&mut targets, entries, None, true).unwrap();
        assert_eq!(result.r_imported, 0);
        assert_eq!(result.duplicates_skipped, 1);

        // Verify only one entry exists
        let query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let items = targets.r_history.search(query).unwrap();
        assert_eq!(items.len(), 1);
    }

    #[test]
    fn test_import_allows_same_command_different_timestamp() {
        use reedline::History;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let mut targets = create_test_targets(&temp_dir);

        let ts1 = DateTime::parse_from_rfc3339("2024-06-15T14:30:45Z")
            .unwrap()
            .with_timezone(&Utc);
        let ts2 = DateTime::parse_from_rfc3339("2024-06-15T15:00:00Z")
            .unwrap()
            .with_timezone(&Utc);

        // Import the same command with two different timestamps
        let entries = vec![ImportEntry {
            command: "library(dplyr)".to_string(),
            timestamp: Some(ts1),
            mode: Some("r".to_string()),
        }];
        let result = import_entries(&mut targets, entries, None, true).unwrap();
        assert_eq!(result.r_imported, 1);

        let entries = vec![ImportEntry {
            command: "library(dplyr)".to_string(),
            timestamp: Some(ts2),
            mode: Some("r".to_string()),
        }];
        let result = import_entries(&mut targets, entries, None, true).unwrap();
        assert_eq!(result.r_imported, 1);
        assert_eq!(result.duplicates_skipped, 0);

        // Both should exist
        let query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let items = targets.r_history.search(query).unwrap();
        assert_eq!(items.len(), 2);
    }

    #[test]
    fn test_import_duplicates_flag_disables_dedup() {
        use reedline::History;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let mut targets = create_test_targets(&temp_dir);

        let ts = DateTime::parse_from_rfc3339("2024-06-15T14:30:45Z")
            .unwrap()
            .with_timezone(&Utc);

        // First import
        let entries = vec![ImportEntry {
            command: "library(dplyr)".to_string(),
            timestamp: Some(ts),
            mode: Some("r".to_string()),
        }];
        let result = import_entries(&mut targets, entries, None, false).unwrap();
        assert_eq!(result.r_imported, 1);

        // Second import with skip_duplicates=false (--import-duplicates)
        let entries = vec![ImportEntry {
            command: "library(dplyr)".to_string(),
            timestamp: Some(ts),
            mode: Some("r".to_string()),
        }];
        let result = import_entries(&mut targets, entries, None, false).unwrap();
        assert_eq!(result.r_imported, 1);
        assert_eq!(result.duplicates_skipped, 0);

        // Both entries should exist (duplicate allowed)
        let query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let items = targets.r_history.search(query).unwrap();
        assert_eq!(items.len(), 2);
    }

    #[test]
    fn test_import_dedup_works_per_database() {
        use reedline::History;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let mut targets = create_test_targets(&temp_dir);

        // Import an R command
        let entries = vec![ImportEntry {
            command: "ls -la".to_string(),
            timestamp: None,
            mode: Some("r".to_string()),
        }];
        let result = import_entries(&mut targets, entries, None, true).unwrap();
        assert_eq!(result.r_imported, 1);

        // Import the same command as shell — should NOT be a duplicate
        // because it's checked against the shell database, not R
        let entries = vec![ImportEntry {
            command: "ls -la".to_string(),
            timestamp: None,
            mode: Some("shell".to_string()),
        }];
        let result = import_entries(&mut targets, entries, None, true).unwrap();
        assert_eq!(result.shell_imported, 1);
        assert_eq!(result.duplicates_skipped, 0);

        // Verify both databases have the entry
        let r_query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let r_items = targets.r_history.search(r_query).unwrap();
        assert_eq!(r_items.len(), 1);

        let shell_query =
            reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let shell_items = targets.shell_history.search(shell_query).unwrap();
        assert_eq!(shell_items.len(), 1);
    }

    #[test]
    fn test_import_dry_run_with_dedup() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let mut targets = create_test_targets(&temp_dir);

        let ts = DateTime::parse_from_rfc3339("2024-06-15T14:30:45Z")
            .unwrap()
            .with_timezone(&Utc);

        // Pre-populate the database
        let entries = vec![ImportEntry {
            command: "library(dplyr)".to_string(),
            timestamp: Some(ts),
            mode: Some("r".to_string()),
        }];
        import_entries(&mut targets, entries, None, false).unwrap();

        // Build dedup sets
        let r_dedup = DedupSet::from_history(&targets.r_history).unwrap();
        let shell_dedup = DedupSet::from_history(&targets.shell_history).unwrap();

        // Dry run with existing + new entries
        let entries = vec![
            ImportEntry {
                command: "library(dplyr)".to_string(), // duplicate
                timestamp: Some(ts),
                mode: Some("r".to_string()),
            },
            ImportEntry {
                command: "print(1)".to_string(), // new
                timestamp: None,
                mode: Some("r".to_string()),
            },
        ];

        let result = import_entries_dry_run(&entries, Some(&r_dedup), Some(&shell_dedup));
        assert_eq!(result.r_imported, 1);
        assert_eq!(result.duplicates_skipped, 1);
    }

    #[test]
    fn test_import_mixed_dedup_new_and_existing() {
        use reedline::History;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let mut targets = create_test_targets(&temp_dir);

        let ts = DateTime::parse_from_rfc3339("2024-06-15T14:30:45Z")
            .unwrap()
            .with_timezone(&Utc);

        // Pre-populate with one entry
        let entries = vec![ImportEntry {
            command: "library(dplyr)".to_string(),
            timestamp: Some(ts),
            mode: Some("r".to_string()),
        }];
        import_entries(&mut targets, entries, None, false).unwrap();

        // Import a batch with duplicates and new entries
        let entries = vec![
            ImportEntry {
                command: "library(dplyr)".to_string(), // duplicate
                timestamp: Some(ts),
                mode: Some("r".to_string()),
            },
            ImportEntry {
                command: "print(1)".to_string(), // new
                timestamp: None,
                mode: Some("r".to_string()),
            },
            ImportEntry {
                command: "git status".to_string(), // new (shell)
                timestamp: None,
                mode: Some("shell".to_string()),
            },
        ];
        let result = import_entries(&mut targets, entries, None, true).unwrap();
        assert_eq!(result.r_imported, 1);
        assert_eq!(result.shell_imported, 1);
        assert_eq!(result.duplicates_skipped, 1);

        // Verify databases
        let r_query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let r_items = targets.r_history.search(r_query).unwrap();
        assert_eq!(r_items.len(), 2); // original + new

        let shell_query =
            reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let shell_items = targets.shell_history.search(shell_query).unwrap();
        assert_eq!(shell_items.len(), 1);
    }

    #[test]
    fn test_import_dry_run_with_partial_dedup() {
        // Regression test: dry-run dedup should work when only one database
        // has a dedup set (e.g., r.db exists but shell.db doesn't).
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let mut targets = create_test_targets(&temp_dir);

        // Pre-populate only the R database
        let entries = vec![ImportEntry {
            command: "library(dplyr)".to_string(),
            timestamp: None,
            mode: Some("r".to_string()),
        }];
        import_entries(&mut targets, entries, None, false).unwrap();

        // Build dedup set only for R (simulating shell.db not existing)
        let r_dedup = DedupSet::from_history(&targets.r_history).unwrap();

        let entries = vec![
            ImportEntry {
                command: "library(dplyr)".to_string(), // duplicate in R
                timestamp: None,
                mode: Some("r".to_string()),
            },
            ImportEntry {
                command: "print(1)".to_string(), // new R entry
                timestamp: None,
                mode: Some("r".to_string()),
            },
            ImportEntry {
                command: "ls -la".to_string(), // shell entry, no dedup set
                timestamp: None,
                mode: Some("shell".to_string()),
            },
        ];

        // Pass R dedup but None for shell
        let result = import_entries_dry_run(&entries, Some(&r_dedup), None);
        assert_eq!(result.r_imported, 1); // only "print(1)"
        assert_eq!(result.shell_imported, 1); // "ls -la" not checked (no shell dedup)
        assert_eq!(result.duplicates_skipped, 1); // "library(dplyr)"
    }

    #[test]
    fn test_import_skips_notimestamp_when_timestamped_exists() {
        // Regression test: a no-timestamp import entry should be skipped if
        // the same command already exists in the DB with any timestamp.
        // This is documented in lines 265-270.
        use reedline::History;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let mut targets = create_test_targets(&temp_dir);

        let ts = DateTime::parse_from_rfc3339("2024-06-15T14:30:45Z")
            .unwrap()
            .with_timezone(&Utc);

        // Pre-populate with a timestamped entry
        let entries = vec![ImportEntry {
            command: "library(dplyr)".to_string(),
            timestamp: Some(ts),
            mode: Some("r".to_string()),
        }];
        let result = import_entries(&mut targets, entries, None, true).unwrap();
        assert_eq!(result.r_imported, 1);

        // Try to import the same command without a timestamp: should be skipped
        let entries = vec![ImportEntry {
            command: "library(dplyr)".to_string(),
            timestamp: None,
            mode: Some("r".to_string()),
        }];
        let result = import_entries(&mut targets, entries, None, true).unwrap();
        assert_eq!(result.r_imported, 0);
        assert_eq!(result.duplicates_skipped, 1);

        // Verify only the original entry exists
        let query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
        let items = targets.r_history.search(query).unwrap();
        assert_eq!(items.len(), 1);
        assert!(items[0].start_timestamp.is_some());
    }

    #[test]
    fn test_from_db_matches_from_history() {
        // Verify that from_db (read-only SQLite) and from_history (via reedline)
        // produce the same dedup set for the same database contents.
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let mut targets = create_test_targets(&temp_dir);

        let ts1 = Utc::now();
        let ts2 = ts1 + chrono::Duration::seconds(60);

        let entries = vec![
            ImportEntry {
                command: "library(dplyr)".to_string(),
                timestamp: Some(ts1),
                mode: Some("r".to_string()),
            },
            ImportEntry {
                command: "print(1)".to_string(),
                timestamp: Some(ts2),
                mode: Some("r".to_string()),
            },
            ImportEntry {
                command: "summary(iris)".to_string(),
                timestamp: None, // no timestamp
                mode: Some("r".to_string()),
            },
        ];
        import_entries(&mut targets, entries, None, false).unwrap();

        let r_path = temp_dir.path().join("r.db");
        let from_history = DedupSet::from_history(&targets.r_history).unwrap();
        let from_db = DedupSet::from_db(&r_path).unwrap();

        // Both should have the same commands set
        assert_eq!(from_history.commands, from_db.commands);
        // Both should have the same command_timestamps set
        assert_eq!(from_history.command_timestamps, from_db.command_timestamps);

        // Verify dedup behavior is identical for both
        assert!(from_history.is_duplicate("library(dplyr)", Some(&ts1)));
        assert!(from_db.is_duplicate("library(dplyr)", Some(&ts1)));
        assert!(!from_history.is_duplicate("new_cmd", None));
        assert!(!from_db.is_duplicate("new_cmd", None));
        assert!(from_history.is_duplicate("summary(iris)", None));
        assert!(from_db.is_duplicate("summary(iris)", None));
    }

    #[test]
    fn test_parse_unified_arf_history_basic() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let unified_path = temp_dir.path().join("export.db");

        // Create a unified export file with r and shell tables
        let db = rusqlite::Connection::open(&unified_path).unwrap();
        db.execute(
            "CREATE TABLE r (id INTEGER PRIMARY KEY, command_line TEXT NOT NULL, start_timestamp INTEGER)",
            [],
        )
        .unwrap();
        db.execute(
            "CREATE TABLE shell (id INTEGER PRIMARY KEY, command_line TEXT NOT NULL, start_timestamp INTEGER)",
            [],
        )
        .unwrap();

        db.execute("INSERT INTO r (command_line, start_timestamp) VALUES ('library(dplyr)', 1705315800000)", []).unwrap();
        db.execute(
            "INSERT INTO r (command_line, start_timestamp) VALUES ('print(1)', NULL)",
            [],
        )
        .unwrap();
        db.execute(
            "INSERT INTO shell (command_line, start_timestamp) VALUES ('ls -la', 1705315860000)",
            [],
        )
        .unwrap();
        drop(db);

        // Parse the unified file
        let entries = parse_unified_arf_history(&unified_path, "r", "shell").unwrap();

        assert_eq!(entries.len(), 3);

        // R entries
        let r_entries: Vec<_> = entries
            .iter()
            .filter(|e| e.mode.as_deref() == Some("r"))
            .collect();
        assert_eq!(r_entries.len(), 2);
        assert_eq!(r_entries[0].command, "library(dplyr)");
        assert!(r_entries[0].timestamp.is_some());
        assert_eq!(r_entries[1].command, "print(1)");
        assert!(r_entries[1].timestamp.is_none());

        // Shell entries
        let shell_entries: Vec<_> = entries
            .iter()
            .filter(|e| e.mode.as_deref() == Some("shell"))
            .collect();
        assert_eq!(shell_entries.len(), 1);
        assert_eq!(shell_entries[0].command, "ls -la");
    }

    #[test]
    fn test_parse_unified_arf_history_custom_table_names() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let unified_path = temp_dir.path().join("custom.db");

        // Create a unified export file with custom table names
        let db = rusqlite::Connection::open(&unified_path).unwrap();
        db.execute(
            "CREATE TABLE my_r_history (id INTEGER PRIMARY KEY, command_line TEXT NOT NULL, start_timestamp INTEGER)",
            [],
        )
        .unwrap();
        db.execute(
            "INSERT INTO my_r_history (command_line) VALUES ('test_cmd')",
            [],
        )
        .unwrap();
        drop(db);

        // Parse with custom table names
        let entries =
            parse_unified_arf_history(&unified_path, "my_r_history", "my_shell_history").unwrap();

        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].command, "test_cmd");
        assert_eq!(entries[0].mode, Some("r".to_string()));
    }

    #[test]
    fn test_parse_unified_arf_history_missing_tables() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let unified_path = temp_dir.path().join("empty.db");

        // Create an empty database (no tables)
        let db = rusqlite::Connection::open(&unified_path).unwrap();
        drop(db);

        // Should return empty vec, not error
        let entries = parse_unified_arf_history(&unified_path, "r", "shell").unwrap();
        assert!(entries.is_empty());
    }

    #[test]
    fn test_validate_table_name_valid() {
        assert!(validate_table_name("r").is_ok());
        assert!(validate_table_name("shell").is_ok());
        assert!(validate_table_name("my_r_history").is_ok());
        assert!(validate_table_name("R_History_2024").is_ok());
        assert!(validate_table_name("_private").is_ok());
    }

    #[test]
    fn test_validate_table_name_invalid() {
        // Empty
        assert!(validate_table_name("").is_err());

        // SQL injection attempts
        assert!(validate_table_name("r; DROP TABLE history;--").is_err());
        assert!(validate_table_name("r' OR '1'='1").is_err());
        assert!(validate_table_name("table-name").is_err());
        assert!(validate_table_name("table.name").is_err());

        // Starts with digit
        assert!(validate_table_name("123table").is_err());

        // Special characters
        assert!(validate_table_name("table name").is_err());
        assert!(validate_table_name("table\nname").is_err());

        // Underscore-only names (confusing and should be rejected)
        assert!(validate_table_name("_").is_err());
        assert!(validate_table_name("___").is_err());
        assert!(validate_table_name("_____").is_err());
    }

    /// Test that parse_unified_arf_history works even when file is named r.db
    /// This verifies that the unified parser doesn't rely on filename.
    #[test]
    fn test_parse_unified_works_regardless_of_filename() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        // Name the file "r.db" - traditionally a single-database file
        let unified_path = temp_dir.path().join("r.db");

        // But create it as a unified file with both r and shell tables
        let db = rusqlite::Connection::open(&unified_path).unwrap();
        db.execute(
            "CREATE TABLE r (id INTEGER PRIMARY KEY, command_line TEXT NOT NULL, start_timestamp INTEGER)",
            [],
        )
        .unwrap();
        db.execute(
            "CREATE TABLE shell (id INTEGER PRIMARY KEY, command_line TEXT NOT NULL, start_timestamp INTEGER)",
            [],
        )
        .unwrap();
        db.execute("INSERT INTO r (command_line) VALUES ('r_cmd')", [])
            .unwrap();
        db.execute("INSERT INTO shell (command_line) VALUES ('shell_cmd')", [])
            .unwrap();
        drop(db);

        // parse_unified_arf_history should work regardless of filename
        let entries = parse_unified_arf_history(&unified_path, "r", "shell").unwrap();

        assert_eq!(entries.len(), 2);
        let r_entries: Vec<_> = entries
            .iter()
            .filter(|e| e.mode.as_deref() == Some("r"))
            .collect();
        let shell_entries: Vec<_> = entries
            .iter()
            .filter(|e| e.mode.as_deref() == Some("shell"))
            .collect();
        assert_eq!(r_entries.len(), 1);
        assert_eq!(r_entries[0].command, "r_cmd");
        assert_eq!(shell_entries.len(), 1);
        assert_eq!(shell_entries[0].command, "shell_cmd");
    }

    #[test]
    fn test_parse_unified_rejects_same_table_names() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let unified_path = temp_dir.path().join("backup.db");

        // Create a minimal database
        let db = rusqlite::Connection::open(&unified_path).unwrap();
        db.execute(
            "CREATE TABLE history (id INTEGER PRIMARY KEY, command_line TEXT)",
            [],
        )
        .unwrap();
        drop(db);

        // Parsing with same table names should fail
        let result = parse_unified_arf_history(&unified_path, "history", "history");

        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("must be different"));
    }

    #[test]
    fn test_parse_unified_with_sqlite_reserved_words_as_table_names() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let unified_path = temp_dir.path().join("export.db");

        // Create a database with SQLite reserved words as table names
        let db = rusqlite::Connection::open(&unified_path).unwrap();
        db.execute(
            r#"CREATE TABLE "select" (id INTEGER PRIMARY KEY, command_line TEXT NOT NULL, start_timestamp INTEGER)"#,
            [],
        )
        .unwrap();
        db.execute(
            r#"CREATE TABLE "from" (id INTEGER PRIMARY KEY, command_line TEXT NOT NULL, start_timestamp INTEGER)"#,
            [],
        )
        .unwrap();
        db.execute(
            r#"INSERT INTO "select" (command_line) VALUES ('r_cmd')"#,
            [],
        )
        .unwrap();
        db.execute(
            r#"INSERT INTO "from" (command_line) VALUES ('shell_cmd')"#,
            [],
        )
        .unwrap();
        drop(db);

        // SQLite reserved words should work when quoted
        let entries = parse_unified_arf_history(&unified_path, "select", "from").unwrap();

        assert_eq!(entries.len(), 2);
        let r_entries: Vec<_> = entries
            .iter()
            .filter(|e| e.mode.as_deref() == Some("r"))
            .collect();
        let shell_entries: Vec<_> = entries
            .iter()
            .filter(|e| e.mode.as_deref() == Some("shell"))
            .collect();
        assert_eq!(r_entries.len(), 1);
        assert_eq!(r_entries[0].command, "r_cmd");
        assert_eq!(shell_entries.len(), 1);
        assert_eq!(shell_entries[0].command, "shell_cmd");
    }
}