dirgrab-lib 0.4.0

Core library for dirgrab: concatenates file contents from directories, respecting Git context.
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
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]

// Declare modules
mod config;
mod errors;
mod listing;
mod processing;
mod tree;
mod utils;

// Necessary imports for lib.rs itself
use log::{debug, error, info, warn};
use std::io; // For io::ErrorKind // For logging within grab_contents
use std::ops::Range;
use std::path::{Path, PathBuf};

// Re-export public API components
pub use config::GrabConfig;
pub use errors::{GrabError, GrabResult};
pub use listing::normalize_glob;

#[derive(Debug, Clone)]
pub struct GrabbedFile {
    pub display_path: String,
    pub full_range: Range<usize>,
    pub header_range: Option<Range<usize>>,
    pub body_range: Range<usize>,
}

#[derive(Debug, Clone)]
pub struct GrabOutput {
    pub content: String,
    pub files: Vec<GrabbedFile>,
}

// --- Internal helpers ---

/// Shared file-discovery logic: canonicalizes target, detects git repo,
/// lists files. Returns (absolute file paths, repo root if any, canonical target).
fn discover_files(config: &GrabConfig) -> GrabResult<(Vec<PathBuf>, Option<PathBuf>, PathBuf)> {
    let target_path = config.target_path.canonicalize().map_err(|e| {
        if e.kind() == io::ErrorKind::NotFound {
            GrabError::TargetPathNotFound(config.target_path.clone())
        } else {
            GrabError::IoError {
                path: config.target_path.clone(),
                source: e,
            }
        }
    })?;
    debug!("Canonical target path: {:?}", target_path);

    let (files, maybe_repo_root) = if config.no_git {
        info!("Ignoring Git context due to --no-git flag.");
        let files = listing::list_files_walkdir(&target_path, config)?;
        (files, None)
    } else {
        let git_repo_root = listing::detect_git_repo(&target_path)?;
        let scope_subdir = git_repo_root
            .as_ref()
            .and_then(|root| derive_scope_subdir(root, &target_path, config));

        let files = match &git_repo_root {
            Some(root) => {
                info!("Operating in Git mode. Repo root: {:?}", root);
                if let Some(scope) = scope_subdir.as_deref() {
                    info!("Limiting Git file listing to sub-path: {:?}", scope);
                } else if !config.all_repo {
                    debug!(
                        "Scope calculation yielded full repository; processing entire repo contents."
                    );
                }
                listing::list_files_git(root, config, scope_subdir.as_deref())?
            }
            None => {
                info!("Operating in Non-Git mode. Target path: {:?}", target_path);
                listing::list_files_walkdir(&target_path, config)?
            }
        };
        (files, git_repo_root)
    };

    info!("Found {} files.", files.len());
    Ok((files, maybe_repo_root, target_path))
}

/// Computes a display path for a file (relative to repo root or target path).
fn display_path(file_path: &Path, repo_root: Option<&Path>, target_path: &Path) -> String {
    let base = repo_root.unwrap_or(target_path);
    let rel = file_path.strip_prefix(base).unwrap_or(file_path);
    let raw = rel.to_string_lossy();
    if std::path::MAIN_SEPARATOR == '\\' && raw.contains('\\') {
        raw.replace('\\', "/")
    } else {
        raw.into_owned()
    }
}

// --- Main Public Functions ---

/// Lists the files that would be included by `dirgrab` without reading their contents.
/// Returns display paths (relative to repo root in Git mode, or target path otherwise).
pub fn list_files(config: &GrabConfig) -> GrabResult<Vec<String>> {
    info!("Listing files with config: {:?}", config);
    let (files, maybe_repo_root, target_path) = discover_files(config)?;
    Ok(files
        .iter()
        .map(|f| display_path(f, maybe_repo_root.as_deref(), &target_path))
        .collect())
}

/// Performs the main `dirgrab` operation based on the provided configuration.
pub fn grab_contents(config: &GrabConfig) -> GrabResult<String> {
    grab_contents_detailed(config).map(|output| output.content)
}

/// Performs the main `dirgrab` operation and returns file-level metadata along with the content.
pub fn grab_contents_detailed(config: &GrabConfig) -> GrabResult<GrabOutput> {
    info!("Starting dirgrab operation with config: {:?}", config);

    let (files_to_process, maybe_repo_root, target_path) = discover_files(config)?;

    // Initialize output buffer
    let mut output_buffer = String::new();
    let mut file_segments = Vec::new();

    // Generate and prepend tree if requested
    if config.include_tree {
        if files_to_process.is_empty() {
            warn!("--include-tree specified, but no files were selected for processing. Tree will be empty.");
            // Keep explicit tree header even if empty
            output_buffer.push_str("---\nDIRECTORY STRUCTURE (No files selected)\n---\n\n");
            return Ok(GrabOutput {
                content: output_buffer,
                files: Vec::new(),
            });
        } else {
            // Determine base path for tree (repo root if git mode, target path otherwise)
            let base_path_for_tree = if !config.no_git && maybe_repo_root.is_some() {
                maybe_repo_root.as_deref().unwrap() // Safe unwrap due to is_some() check
            } else {
                &target_path
            };
            debug!(
                "Generating directory tree relative to: {:?}",
                base_path_for_tree
            );

            match tree::generate_indented_tree(&files_to_process, base_path_for_tree) {
                Ok(tree_str) => {
                    output_buffer.push_str("---\nDIRECTORY STRUCTURE\n---\n");
                    output_buffer.push_str(&tree_str);
                    output_buffer.push_str("\n---\nFILE CONTENTS\n---\n\n");
                }
                Err(e) => {
                    error!("Failed to generate directory tree: {}", e);
                    // Still add header indicating failure
                    output_buffer.push_str("---\nERROR GENERATING DIRECTORY STRUCTURE\n---\n\n");
                }
            }
        }
    }

    // Process files and append content (only if files exist)
    if !files_to_process.is_empty() {
        // Updated call to process_files to pass the whole config struct
        let processed = processing::process_files(
            &files_to_process,
            config, // Pass config struct
            maybe_repo_root.as_deref(),
            &target_path,
        )?;
        let base_offset = output_buffer.len();
        output_buffer.push_str(&processed.content);
        for segment in processed.files {
            file_segments.push(GrabbedFile {
                display_path: segment.display_path,
                full_range: offset_range(&segment.full_range, base_offset),
                header_range: segment
                    .header_range
                    .map(|range| offset_range(&range, base_offset)),
                body_range: offset_range(&segment.body_range, base_offset),
            });
        }
    } else if !config.include_tree {
        // If no files AND no tree was requested
        warn!("No files selected for processing based on current configuration.");
        // Return empty string only if no files were found AND tree wasn't requested/generated.
        return Ok(GrabOutput {
            content: String::new(),
            files: Vec::new(),
        });
    }

    // Return the combined buffer (might contain only tree, or tree + content, or just content)
    Ok(GrabOutput {
        content: output_buffer,
        files: file_segments,
    })
}

fn derive_scope_subdir(
    repo_root: &Path,
    target_path: &Path,
    config: &GrabConfig,
) -> Option<PathBuf> {
    if config.all_repo {
        return None;
    }

    match target_path.strip_prefix(repo_root) {
        Ok(rel) => {
            if rel.as_os_str().is_empty() {
                None
            } else {
                Some(rel.to_path_buf())
            }
        }
        Err(_) => None,
    }
}

fn offset_range(range: &Range<usize>, offset: usize) -> Range<usize> {
    (range.start + offset)..(range.end + offset)
}

// --- FILE: dirgrab-lib/src/lib.rs ---
// (Showing only the tests module and its necessary imports)

// ... (rest of lib.rs code above) ...

// --- Tests ---
#[cfg(test)]
mod tests {
    // Use super::* to bring everything from lib.rs into scope for tests
    // This now includes GrabConfig, GrabError, GrabResult because they are re-exported.
    use super::*;
    // Also need direct imports for helpers/types used *only* in tests
    use anyhow::{Context, Result}; // Ensure Context and Result are imported from anyhow
    use std::collections::HashSet;
    use std::fs::{self}; // Ensure File is imported if needed by helpers
    use std::path::{Path, PathBuf}; // Need these for helpers defined within tests mod
    use std::process::Command;
    use tempfile::{tempdir, TempDir};

    // --- Test Setup Helpers ---
    fn setup_test_dir() -> Result<(TempDir, PathBuf)> {
        let dir = tempdir()?;
        let path = dir.path().to_path_buf();

        fs::write(path.join("file1.txt"), "Content of file 1.")?;
        fs::write(path.join("file2.rs"), "fn main() {}")?;
        fs::create_dir_all(path.join("subdir"))?; // Use create_dir_all
        fs::write(path.join("subdir").join("file3.log"), "Log message.")?;
        fs::write(
            path.join("subdir").join("another.txt"),
            "Another text file.",
        )?;
        fs::write(path.join("binary.dat"), [0x80, 0x81, 0x82])?;
        fs::write(path.join("dirgrab.txt"), "Previous dirgrab output.")?;
        Ok((dir, path))
    }

    fn setup_git_repo(path: &Path) -> Result<bool> {
        if Command::new("git").arg("--version").output().is_err() {
            eprintln!("WARN: 'git' command not found, skipping Git-related test setup.");
            return Ok(false);
        }
        // Use crate:: path now because utils is not in super::* scope
        crate::utils::run_command("git", &["init", "-b", "main"], path)?;
        crate::utils::run_command("git", &["config", "user.email", "test@example.com"], path)?;
        crate::utils::run_command("git", &["config", "user.name", "Test User"], path)?;
        // Configure Git to handle potential CRLF issues on Windows in tests if needed
        crate::utils::run_command("git", &["config", "core.autocrlf", "false"], path)?;

        fs::write(path.join(".gitignore"), "*.log\nbinary.dat\nfile1.txt")?;
        crate::utils::run_command(
            "git",
            &["add", ".gitignore", "file2.rs", "subdir/another.txt"],
            path,
        )?;
        crate::utils::run_command("git", &["commit", "-m", "Initial commit"], path)?;

        fs::write(path.join("untracked.txt"), "This file is not tracked.")?;
        fs::write(path.join("ignored.log"), "This should be ignored by git.")?;
        fs::create_dir_all(path.join("deep/sub"))?;
        fs::write(path.join("deep/sub/nested.txt"), "Nested content")?;
        crate::utils::run_command("git", &["add", "deep/sub/nested.txt"], path)?;
        crate::utils::run_command("git", &["commit", "-m", "Add nested file"], path)?;
        Ok(true)
    }

    fn run_test_command(
        cmd: &str,
        args: &[&str],
        current_dir: &Path,
    ) -> Result<std::process::Output> {
        let output = crate::utils::run_command(cmd, args, current_dir)?;
        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            let stdout = String::from_utf8_lossy(&output.stdout);
            anyhow::bail!(
                "Command failed: {} {:?}\nStatus: {}\nStdout: {}\nStderr: {}",
                cmd,
                args,
                output.status,
                stdout,
                stderr
            );
        }
        Ok(output)
    }

    fn get_expected_set(base_path: &Path, relative_paths: &[&str]) -> HashSet<PathBuf> {
        relative_paths.iter().map(|p| base_path.join(p)).collect()
    }

    fn assert_paths_eq(actual: Vec<PathBuf>, expected: HashSet<PathBuf>) {
        let actual_set: HashSet<PathBuf> = actual.into_iter().collect();
        assert_eq!(
            actual_set, expected,
            "Path sets differ.\nActual paths: {:?}\nExpected paths: {:?}",
            actual_set, expected
        );
    }

    // --- Tests ---
    // Tests calling listing functions need crate:: prefix
    #[test]
    fn test_detect_git_repo_inside() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        if !setup_git_repo(&path)? {
            println!("Skipping Git test: git not found or setup failed.");
            return Ok(());
        }
        let maybe_root = crate::listing::detect_git_repo(&path)?; // Use crate:: path
        assert!(maybe_root.is_some());
        assert_eq!(maybe_root.unwrap().canonicalize()?, path.canonicalize()?);
        let subdir_path = path.join("subdir");
        let maybe_root_from_subdir = crate::listing::detect_git_repo(&subdir_path)?; // Use crate:: path
        assert!(maybe_root_from_subdir.is_some());
        assert_eq!(
            maybe_root_from_subdir.unwrap().canonicalize()?,
            path.canonicalize()?
        );
        Ok(())
    }

    #[test]
    fn test_detect_git_repo_outside() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        // Ensure no git repo exists here
        let maybe_root = crate::listing::detect_git_repo(&path)?; // Use crate:: path
        assert!(maybe_root.is_none());
        Ok(())
    }

    #[test]
    fn test_list_files_walkdir_no_exclude_default_excludes_dirgrab_txt() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: false,
            exclude_patterns: vec![],
            include_untracked: false,      // No effect in walkdir
            include_default_output: false, // Exclude dirgrab.txt
            no_git: true,                  // Force walkdir
            include_tree: false,
            convert_pdf: false,
            all_repo: false,
        };
        let files = crate::listing::list_files_walkdir(&path, &config)?; // Use crate:: path
        let expected_set = get_expected_set(
            &path,
            &[
                "file1.txt",
                "file2.rs",
                "subdir/file3.log",
                "subdir/another.txt",
                "binary.dat",
                // "dirgrab.txt" should be excluded by default
            ],
        );
        assert_paths_eq(files, expected_set);
        Ok(())
    }

    #[test]
    fn test_list_files_walkdir_with_exclude() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: false,
            exclude_patterns: vec!["*.log".to_string(), "subdir/".to_string()], // User excludes
            include_untracked: false,
            include_default_output: false,
            no_git: true, // Force walkdir
            include_tree: false,
            convert_pdf: false,
            all_repo: false,
        };
        let files = crate::listing::list_files_walkdir(&path, &config)?; // Use crate:: path
        let expected_set = get_expected_set(
            &path,
            &[
                "file1.txt",
                "file2.rs",
                "binary.dat",
                // subdir/* excluded
                // dirgrab.txt excluded by default
            ],
        );
        assert_paths_eq(files, expected_set);
        Ok(())
    }

    #[test]
    fn test_list_files_git_tracked_only_default_excludes_dirgrab_txt() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        if !setup_git_repo(&path)? {
            println!("Skipping Git test: git not found or setup failed.");
            return Ok(());
        }
        let config = GrabConfig {
            target_path: path.clone(), // Target doesn't matter as much as root for list_files_git
            add_headers: false,
            exclude_patterns: vec![],
            include_untracked: false,      // Tracked only
            include_default_output: false, // Exclude dirgrab.txt
            no_git: false,                 // Use Git
            include_tree: false,
            convert_pdf: false,
            all_repo: false,
        };
        let files = crate::listing::list_files_git(&path, &config, None)?; // Use crate:: path, pass repo root
        let expected_set = get_expected_set(
            &path,
            &[
                ".gitignore",
                "file2.rs",
                "subdir/another.txt",
                "deep/sub/nested.txt",
                // file1.txt ignored by .gitignore
                // file3.log ignored by .gitignore
                // binary.dat ignored by .gitignore
                // dirgrab.txt not tracked and default excluded
                // untracked.txt not tracked
                // ignored.log not tracked
            ],
        );
        assert_paths_eq(files, expected_set);
        Ok(())
    }

    #[test]
    fn test_list_files_git_include_untracked_default_excludes_dirgrab_txt() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        if !setup_git_repo(&path)? {
            println!("Skipping Git test: git not found or setup failed.");
            return Ok(());
        }
        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: false,
            exclude_patterns: vec![],
            include_untracked: true,       // Include untracked
            include_default_output: false, // Exclude dirgrab.txt
            no_git: false,                 // Use Git
            include_tree: false,
            convert_pdf: false,
            all_repo: false,
        };
        let files = crate::listing::list_files_git(&path, &config, None)?; // Use crate:: path
        let expected_set = get_expected_set(
            &path,
            &[
                ".gitignore",
                "file2.rs",
                "subdir/another.txt",
                "deep/sub/nested.txt",
                "untracked.txt", // Included now
                                 // file1.txt ignored by .gitignore
                                 // file3.log ignored by .gitignore
                                 // binary.dat ignored by .gitignore
                                 // ignored.log ignored by .gitignore (via --exclude-standard)
                                 // dirgrab.txt untracked and default excluded
            ],
        );
        assert_paths_eq(files, expected_set);
        Ok(())
    }

    #[test]
    fn test_list_files_git_with_exclude() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        if !setup_git_repo(&path)? {
            println!("Skipping Git test: git not found or setup failed.");
            return Ok(());
        }
        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: false,
            exclude_patterns: vec![
                "*.rs".to_string(),    // Exclude rust files
                "subdir/".to_string(), // Exclude subdir/
                "deep/".to_string(),   // Exclude deep/
            ],
            include_untracked: false, // Tracked only
            include_default_output: false,
            no_git: false, // Use Git
            include_tree: false,
            convert_pdf: false,
            all_repo: false,
        };
        let files = crate::listing::list_files_git(&path, &config, None)?; // Use crate:: path
        let expected_set = get_expected_set(&path, &[".gitignore"]); // Only .gitignore remains
        assert_paths_eq(files, expected_set);
        Ok(())
    }

    #[test]
    fn test_list_files_git_untracked_with_exclude() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        if !setup_git_repo(&path)? {
            println!("Skipping Git test: git not found or setup failed.");
            return Ok(());
        }
        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: false,
            exclude_patterns: vec!["*.txt".to_string()], // Exclude all .txt files
            include_untracked: true,                     // Include untracked
            include_default_output: false,
            no_git: false, // Use Git
            include_tree: false,
            convert_pdf: false,
            all_repo: false,
        };
        let files = crate::listing::list_files_git(&path, &config, None)?; // Use crate:: path
        let expected_set = get_expected_set(
            &path,
            &[
                ".gitignore",
                "file2.rs",
                // subdir/another.txt excluded by *.txt
                // deep/sub/nested.txt excluded by *.txt
                // untracked.txt excluded by *.txt
                // dirgrab.txt excluded by default
            ],
        );
        assert_paths_eq(files, expected_set);
        Ok(())
    }

    #[test]
    fn test_list_files_walkdir_include_default_output() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: false,
            exclude_patterns: vec![],
            include_untracked: false,
            include_default_output: true, // Include dirgrab.txt
            no_git: true,                 // Force walkdir
            include_tree: false,
            convert_pdf: false,
            all_repo: false,
        };
        let files = crate::listing::list_files_walkdir(&path, &config)?; // Use crate:: path
        let expected_set = get_expected_set(
            &path,
            &[
                "file1.txt",
                "file2.rs",
                "subdir/file3.log",
                "subdir/another.txt",
                "binary.dat",
                "dirgrab.txt", // Included now
            ],
        );
        assert_paths_eq(files, expected_set);
        Ok(())
    }

    #[test]
    fn test_list_files_git_include_default_output_tracked_only() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        if !setup_git_repo(&path)? {
            println!("Skipping Git test: git not found or setup failed.");
            return Ok(());
        }
        // Make dirgrab.txt tracked
        fs::write(path.join("dirgrab.txt"), "Tracked dirgrab output.")?;
        run_test_command("git", &["add", "dirgrab.txt"], &path)?;
        run_test_command("git", &["commit", "-m", "Add dirgrab.txt"], &path)?;

        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: false,
            exclude_patterns: vec![],
            include_untracked: false,     // Tracked only
            include_default_output: true, // Include dirgrab.txt
            no_git: false,                // Use Git
            include_tree: false,
            convert_pdf: false,
            all_repo: false,
        };
        let files = crate::listing::list_files_git(&path, &config, None)?; // Use crate:: path
        let expected_set = get_expected_set(
            &path,
            &[
                ".gitignore",
                "file2.rs",
                "subdir/another.txt",
                "deep/sub/nested.txt",
                "dirgrab.txt", // Included because tracked and override flag set
            ],
        );
        assert_paths_eq(files, expected_set);
        Ok(())
    }

    #[test]
    fn test_list_files_git_include_default_output_with_untracked() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        if !setup_git_repo(&path)? {
            println!("Skipping Git test: git not found or setup failed.");
            return Ok(());
        }
        // dirgrab.txt is untracked in this setup
        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: false,
            exclude_patterns: vec![],
            include_untracked: true,      // Include untracked
            include_default_output: true, // Include dirgrab.txt
            no_git: false,                // Use Git
            include_tree: false,
            convert_pdf: false,
            all_repo: false,
        };
        let files = crate::listing::list_files_git(&path, &config, None)?; // Use crate:: path
        let expected_set = get_expected_set(
            &path,
            &[
                ".gitignore",
                "file2.rs",
                "subdir/another.txt",
                "deep/sub/nested.txt",
                "untracked.txt", // Included
                "dirgrab.txt",   // Included because untracked and override flag set
            ],
        );
        assert_paths_eq(files, expected_set);
        Ok(())
    }

    #[test]
    fn test_list_files_git_include_default_output_but_excluded_by_user() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        if !setup_git_repo(&path)? {
            println!("Skipping Git test: git not found or setup failed.");
            return Ok(());
        }
        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: false,
            exclude_patterns: vec!["dirgrab.txt".to_string()], // User explicitly excludes
            include_untracked: true,
            include_default_output: true, // Override default exclusion, but user exclusion takes precedence
            no_git: false,                // Use Git
            include_tree: false,
            convert_pdf: false,
            all_repo: false,
        };
        let files = crate::listing::list_files_git(&path, &config, None)?; // Use crate:: path
        let expected_set = get_expected_set(
            &path,
            &[
                ".gitignore",
                "file2.rs",
                "subdir/another.txt",
                "deep/sub/nested.txt",
                "untracked.txt",
                // dirgrab.txt excluded by user pattern
            ],
        );
        assert_paths_eq(files, expected_set);
        Ok(())
    }

    #[test]
    fn test_list_files_git_scoped_to_subdir() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        if !setup_git_repo(&path)? {
            println!("Skipping Git test: git not found or setup failed.");
            return Ok(());
        }

        fs::write(path.join("deep/untracked_inside.txt"), "scoped content")?;

        let config = GrabConfig {
            target_path: path.join("deep"),
            add_headers: false,
            exclude_patterns: vec![],
            include_untracked: true,
            include_default_output: false,
            no_git: false,
            include_tree: false,
            convert_pdf: false,
            all_repo: false,
        };
        let scope = Path::new("deep");
        let files = crate::listing::list_files_git(&path, &config, Some(scope))?;
        let expected_set =
            get_expected_set(&path, &["deep/sub/nested.txt", "deep/untracked_inside.txt"]);
        assert_paths_eq(files, expected_set);
        Ok(())
    }

    #[test]
    fn test_no_git_flag_forces_walkdir_in_git_repo() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        if !setup_git_repo(&path)? {
            println!("Skipping Git test: git not found or setup failed.");
            return Ok(());
        }
        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: false, // No headers for easier content check
            exclude_patterns: vec![],
            include_untracked: false,      // No effect
            include_default_output: false, // Exclude dirgrab.txt
            no_git: true,                  // Force walkdir
            include_tree: false,           // No tree for easier content check
            convert_pdf: false,
            all_repo: false,
        };
        let result_string = grab_contents(&config)?;

        // Check content from files that would be ignored by git but included by walkdir
        assert!(
            result_string.contains("Content of file 1."),
            "file1.txt content missing"
        ); // Ignored by .gitignore, but walkdir includes
        assert!(
            result_string.contains("Log message."),
            "file3.log content missing"
        ); // Ignored by .gitignore, but walkdir includes
        assert!(
            result_string.contains("fn main() {}"),
            "file2.rs content missing"
        ); // Tracked by git, included by walkdir
        assert!(
            result_string.contains("Another text file."),
            "another.txt content missing"
        ); // Tracked by git, included by walkdir
        assert!(
            !result_string.contains("Previous dirgrab output."),
            "dirgrab.txt included unexpectedly"
        ); // Excluded by default

        // The binary file binary.dat is skipped because it's not valid UTF-8.
        // The processing function logs a warning. We don't need to assert its absence
        // in the final string, as it cannot be represented in a valid Rust String anyway.
        // The fact that grab_contents completes successfully and includes the text files is sufficient.

        Ok(())
    }

    #[test]
    fn test_no_git_flag_still_respects_exclude_patterns() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        if !setup_git_repo(&path)? {
            println!("Skipping Git test: git not found or setup failed.");
            return Ok(());
        }
        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: false,
            exclude_patterns: vec!["*.txt".to_string(), "*.rs".to_string()], // Exclude .txt and .rs
            include_untracked: false,
            include_default_output: false,
            no_git: true, // Force walkdir
            include_tree: false,
            convert_pdf: false,
            all_repo: false,
        };
        let result_string = grab_contents(&config)?;

        assert!(result_string.contains("Log message."), "file3.log missing"); // Included
        assert!(
            !result_string.contains("Content of file 1."),
            "file1.txt included unexpectedly"
        ); // Excluded by *.txt
        assert!(
            !result_string.contains("fn main() {}"),
            "file2.rs included unexpectedly"
        ); // Excluded by *.rs
        assert!(
            !result_string.contains("Another text file."),
            "another.txt included unexpectedly"
        ); // Excluded by *.txt
        assert!(
            !result_string.contains("Nested content"),
            "nested.txt included unexpectedly"
        ); // Excluded by *.txt
        assert!(
            !result_string.contains("Previous dirgrab output."),
            "dirgrab.txt included unexpectedly"
        ); // Excluded by default & *.txt

        Ok(())
    }

    #[test]
    fn test_no_git_flag_with_include_default_output() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        if !setup_git_repo(&path)? {
            println!("Skipping Git test: git not found or setup failed.");
            return Ok(());
        }
        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: false,
            exclude_patterns: vec![],
            include_untracked: false,
            include_default_output: true, // Include dirgrab.txt
            no_git: true,                 // Force walkdir
            include_tree: false,
            convert_pdf: false,
            all_repo: false,
        };
        let result_string = grab_contents(&config)?;
        assert!(
            result_string.contains("Previous dirgrab output."),
            "Should include dirgrab.txt due to override"
        );
        Ok(())
    }

    #[test]
    fn test_no_git_flag_headers_relative_to_target() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        if !setup_git_repo(&path)? {
            println!("Skipping Git test: git not found or setup failed.");
            return Ok(());
        }
        let config = GrabConfig {
            target_path: path.clone(), // Target is repo root
            add_headers: true,         // Enable headers
            exclude_patterns: vec![
                "*.log".to_string(),
                "*.dat".to_string(),
                "dirgrab.txt".to_string(),
            ], // Simplify output
            include_untracked: false,
            include_default_output: false,
            no_git: true,        // Force walkdir
            include_tree: false, // No tree
            convert_pdf: false,
            all_repo: false,
        };
        let result_string = grab_contents(&config)?;

        // file1.txt is ignored by .gitignore but included here because no_git=true
        let expected_header_f1 = format!("--- FILE: {} ---", Path::new("file1.txt").display());
        assert!(
            result_string.contains(&expected_header_f1),
            "Header path should be relative to target_path. Expected '{}' in output:\n{}",
            expected_header_f1,
            result_string
        );

        // .gitignore itself is not usually listed by walkdir unless explicitly targeted? Let's check file2.rs
        let expected_header_f2 = format!("--- FILE: {} ---", Path::new("file2.rs").display());
        assert!(
            result_string.contains(&expected_header_f2),
            "Header path should be relative to target_path. Expected '{}' in output:\n{}",
            expected_header_f2,
            result_string
        );

        let expected_nested_header = format!(
            "--- FILE: {} ---",
            Path::new("deep/sub/nested.txt").display()
        );
        assert!(
            result_string.contains(&expected_nested_header),
            "Nested header path relative to target_path. Expected '{}' in output:\n{}",
            expected_nested_header,
            result_string
        );
        Ok(())
    }

    #[test]
    fn test_git_mode_headers_relative_to_repo_root() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        if !setup_git_repo(&path)? {
            println!("Skipping Git test: git not found or setup failed.");
            return Ok(());
        }
        let subdir_target = path.join("deep"); // Target is inside the repo
        fs::create_dir_all(&subdir_target)?; // Ensure target exists

        let config = GrabConfig {
            target_path: subdir_target.clone(), // Target is 'deep' subdir
            add_headers: true,                  // Enable headers
            exclude_patterns: vec![],
            include_untracked: false, // Tracked only
            include_default_output: false,
            no_git: false,       // Use Git mode
            include_tree: false, // No tree
            convert_pdf: false,
            all_repo: false,
        };
        let result_string = grab_contents(&config)?; // Should still find files relative to repo root

        // Check headers are relative to repo root (path), not target_path (subdir_target)
        let expected_nested_header = format!(
            "--- FILE: {} ---",
            Path::new("deep/sub/nested.txt").display()
        );
        assert!(
            result_string.contains(&expected_nested_header),
            "Header path should be relative to repo root. Expected '{}' in output:\n{}",
            expected_nested_header,
            result_string
        );

        // Check other files outside the target dir are also included and relative to root
        let unexpected_root_header = format!("--- FILE: {} ---", Path::new(".gitignore").display());
        assert!(
            !result_string.contains(&unexpected_root_header),
            "Scoped results should not include repo-root files. Unexpected '{}' in output:\n{}",
            unexpected_root_header,
            result_string
        );
        let unexpected_rs_header = format!("--- FILE: {} ---", Path::new("file2.rs").display());
        assert!(
            !result_string.contains(&unexpected_rs_header),
            "Scoped results should not include repo-root files. Unexpected '{}' in output:\n{}",
            unexpected_rs_header,
            result_string
        );
        Ok(())
    }

    #[test]
    fn test_grab_contents_with_tree_no_git() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        // Don't need git repo setup for no_git test, but keep files consistent
        fs::write(path.join(".gitignore"), "*.log\nbinary.dat")?; // Create dummy .gitignore
        fs::create_dir_all(path.join("deep/sub"))?;
        fs::write(path.join("deep/sub/nested.txt"), "Nested content")?;
        fs::write(path.join("untracked.txt"), "Untracked content")?; // File exists

        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: true,
            exclude_patterns: vec![
                "*.log".to_string(),       // Exclude logs
                "*.dat".to_string(),       // Exclude binary
                ".gitignore".to_string(),  // Exclude .gitignore itself
                "dirgrab.txt".to_string(), // Exclude default output file explicitly too
            ],
            include_untracked: false,      // No effect
            include_default_output: false, // Also excluded above
            no_git: true,                  // Force walkdir
            include_tree: true,            // THE flag to test
            convert_pdf: false,
            all_repo: false,
        };
        let result = grab_contents(&config)?;

        // Expected tree for walkdir with excludes applied
        // file1.txt, file2.rs, another.txt, nested.txt, untracked.txt should remain
        let expected_tree_part = "\
---
DIRECTORY STRUCTURE
---
- deep/
  - sub/
    - nested.txt
- file1.txt
- file2.rs
- subdir/
  - another.txt
- untracked.txt
";

        assert!(
            result.contains(expected_tree_part),
            "Expected tree structure not found in output:\nTree Section:\n---\n{}\n---",
            result
                .split("---\nFILE CONTENTS\n---")
                .next()
                .unwrap_or("TREE NOT FOUND")
        );

        assert!(
            result.contains("\n---\nFILE CONTENTS\n---\n\n"),
            "Expected file content separator not found"
        );
        // Check presence of headers and content for included files
        assert!(
            result.contains("--- FILE: file1.txt ---"),
            "Header for file1.txt missing"
        );
        assert!(
            result.contains("Content of file 1."),
            "Content of file1.txt missing"
        );
        assert!(
            result.contains("--- FILE: deep/sub/nested.txt ---"),
            "Header for nested.txt missing"
        );
        assert!(
            result.contains("Nested content"),
            "Content of nested.txt missing"
        );
        // Check absence of excluded file content
        assert!(
            !result.contains("Previous dirgrab output."),
            "dirgrab.txt content included unexpectedly"
        );
        assert!(
            !result.contains("Log message"),
            "Log content included unexpectedly"
        );

        Ok(())
    }

    #[test]
    fn test_grab_contents_with_tree_git_mode() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        if !setup_git_repo(&path)? {
            println!("Skipping Git test: git not found or setup failed.");
            return Ok(());
        }
        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: true,
            exclude_patterns: vec![".gitignore".to_string()], // Exclude .gitignore
            include_untracked: true,                          // Include untracked
            include_default_output: false,                    // Exclude dirgrab.txt (default)
            no_git: false,                                    // Use Git
            include_tree: true,                               // Include tree
            convert_pdf: false,
            all_repo: false,
        };
        let result = grab_contents(&config)?;

        // Expected tree for git ls-files -ou --exclude-standard :!.gitignore :!dirgrab.txt
        // Should include: file2.rs, another.txt, nested.txt, untracked.txt
        let expected_tree_part = "\
---
DIRECTORY STRUCTURE
---
- deep/
  - sub/
    - nested.txt
- file2.rs
- subdir/
  - another.txt
- untracked.txt
";
        assert!(
            result.contains(expected_tree_part),
            "Expected tree structure not found in output:\nTree Section:\n---\n{}\n---",
            result
                .split("---\nFILE CONTENTS\n---")
                .next()
                .unwrap_or("TREE NOT FOUND")
        );
        assert!(
            result.contains("\n---\nFILE CONTENTS\n---\n\n"),
            "Separator missing"
        );
        // Check content
        assert!(
            result.contains("--- FILE: file2.rs ---"),
            "file2.rs header missing"
        );
        assert!(result.contains("fn main() {}"), "file2.rs content missing");
        assert!(
            result.contains("--- FILE: untracked.txt ---"),
            "untracked.txt header missing"
        );
        assert!(
            result.contains("This file is not tracked."),
            "untracked.txt content missing"
        );
        assert!(
            !result.contains("--- FILE: .gitignore ---"),
            ".gitignore included unexpectedly"
        );

        Ok(())
    }

    #[test]
    fn test_grab_contents_with_tree_empty() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        // No need for files if we exclude everything
        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: true,
            exclude_patterns: vec!["*".to_string(), "*/".to_string()], // Exclude everything
            include_untracked: true,
            include_default_output: true,
            no_git: true,       // Use walkdir
            include_tree: true, // Ask for tree
            convert_pdf: false,
            all_repo: false,
        };
        let result = grab_contents(&config)?;
        // Expect only the empty tree message
        let expected = "---\nDIRECTORY STRUCTURE (No files selected)\n---\n\n";
        assert_eq!(result, expected);
        Ok(())
    }

    // Tests calling internal helpers need crate:: prefix
    #[test]
    fn test_generate_indented_tree_simple() -> Result<()> {
        let tmp_dir = tempdir()?;
        let proj_dir = tmp_dir.path().join("project");
        fs::create_dir_all(proj_dir.join("src"))?;
        fs::create_dir_all(proj_dir.join("tests"))?;
        fs::write(proj_dir.join("src/main.rs"), "")?;
        fs::write(proj_dir.join("README.md"), "")?;
        fs::write(proj_dir.join("src/lib.rs"), "")?;
        fs::write(proj_dir.join("tests/basic.rs"), "")?;

        // Simulate paths relative to a base (doesn't have to exist for this test)
        let base = PathBuf::from("/project"); // Logical base
        let files_logical = [
            // Use array for BTreeSet later if needed
            base.join("src/main.rs"),
            base.join("README.md"),
            base.join("src/lib.rs"),
            base.join("tests/basic.rs"),
        ];

        // Map logical paths to actual paths in temp dir for is_dir() check
        let files_in_tmp = files_logical
            .iter()
            .map(|p| tmp_dir.path().join(p.strip_prefix("/").unwrap()))
            .collect::<Vec<_>>();
        let base_in_tmp = tmp_dir.path().join("project"); // The actual base path

        let tree = crate::tree::generate_indented_tree(&files_in_tmp, &base_in_tmp)?; // Use crate:: path
        let expected = "\
- README.md
- src/
  - lib.rs
  - main.rs
- tests/
  - basic.rs
";
        assert_eq!(tree, expected);
        Ok(())
    }

    #[test]
    fn test_generate_indented_tree_deeper() -> Result<()> {
        let tmp_dir = tempdir()?;
        let proj_dir = tmp_dir.path().join("project");
        fs::create_dir_all(proj_dir.join("a/b/c"))?;
        fs::create_dir_all(proj_dir.join("a/d"))?;
        fs::write(proj_dir.join("a/b/c/file1.txt"), "")?;
        fs::write(proj_dir.join("a/d/file2.txt"), "")?;
        fs::write(proj_dir.join("top.txt"), "")?;
        fs::write(proj_dir.join("a/b/file3.txt"), "")?;

        let base = PathBuf::from("/project"); // Logical base
        let files_logical = [
            base.join("a/b/c/file1.txt"),
            base.join("a/d/file2.txt"),
            base.join("top.txt"),
            base.join("a/b/file3.txt"),
        ];

        let files_in_tmp = files_logical
            .iter()
            .map(|p| tmp_dir.path().join(p.strip_prefix("/").unwrap()))
            .collect::<Vec<_>>();
        let base_in_tmp = tmp_dir.path().join("project"); // Actual base

        let tree = crate::tree::generate_indented_tree(&files_in_tmp, &base_in_tmp)?; // Use crate:: path
        let expected = "\
- a/
  - b/
    - c/
      - file1.txt
    - file3.txt
  - d/
    - file2.txt
- top.txt
";
        assert_eq!(tree, expected);
        Ok(())
    }

    // --- Tests for processing.rs (Updated to pass GrabConfig) ---
    #[test]
    fn test_process_files_no_headers_skip_binary() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        let files_to_process = vec![
            path.join("file1.txt"),
            path.join("binary.dat"), // Should be skipped as non-utf8
            path.join("file2.rs"),
        ];
        let config = GrabConfig {
            // Create dummy config
            target_path: path.clone(),
            add_headers: false, // Key part of this test
            exclude_patterns: vec![],
            include_untracked: false,
            include_default_output: false,
            no_git: true, // Assume non-git mode for simplicity here
            include_tree: false,
            convert_pdf: false, // PDF conversion off
            all_repo: false,
        };
        let result = crate::processing::process_files(&files_to_process, &config, None, &path)?;
        let expected_content = "Content of file 1.\n\nfn main() {}\n\n";
        assert_eq!(result.content, expected_content);
        assert_eq!(result.files.len(), 2);
        assert_eq!(result.files[0].display_path, "file1.txt");
        assert!(result.files[0].header_range.is_none());
        assert_eq!(
            &result.content[result.files[0].body_range.clone()],
            "Content of file 1.\n\n"
        );
        assert_eq!(
            &result.content[result.files[1].body_range.clone()],
            "fn main() {}\n\n"
        );
        Ok(())
    }

    #[test]
    fn test_process_files_with_headers_git_mode() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        // Don't need full git setup if we just provide repo_root
        let files_to_process = vec![path.join("file1.txt"), path.join("file2.rs")];
        let repo_root = Some(path.as_path());
        let config = GrabConfig {
            target_path: path.clone(), // target can be same as root for this test
            add_headers: true,         // Key part of this test
            exclude_patterns: vec![],
            include_untracked: false,
            include_default_output: false,
            no_git: false, // Git mode ON
            include_tree: false,
            convert_pdf: false,
            all_repo: false,
        };
        let result =
            crate::processing::process_files(&files_to_process, &config, repo_root, &path)?;
        let expected_content = format!(
            "--- FILE: {} ---\nContent of file 1.\n\n--- FILE: {} ---\nfn main() {{}}\n\n",
            Path::new("file1.txt").display(), // Paths relative to repo_root (which is path)
            Path::new("file2.rs").display()
        );
        assert_eq!(result.content, expected_content);
        assert_eq!(result.files.len(), 2);
        assert!(result.files.iter().all(|seg| seg.header_range.is_some()));
        let first = &result.files[0];
        assert_eq!(first.display_path, "file1.txt");
        assert_eq!(
            &result.content[first.header_range.clone().unwrap()],
            "--- FILE: file1.txt ---\n"
        );
        assert_eq!(
            &result.content[first.body_range.clone()],
            "Content of file 1.\n\n"
        );
        Ok(())
    }

    #[test]
    fn test_process_files_headers_no_git_mode() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        let files_to_process = vec![path.join("file1.txt"), path.join("subdir/another.txt")];
        let config = GrabConfig {
            target_path: path.clone(), // Target path is the base
            add_headers: true,         // Key part of this test
            exclude_patterns: vec![],
            include_untracked: false,
            include_default_output: false,
            no_git: true, // Git mode OFF
            include_tree: false,
            convert_pdf: false,
            all_repo: false,
        };
        let result = crate::processing::process_files(&files_to_process, &config, None, &path)?;
        let expected_content = format!(
            "--- FILE: {} ---\nContent of file 1.\n\n--- FILE: {} ---\nAnother text file.\n\n",
            Path::new("file1.txt").display(), // Paths relative to target_path
            Path::new("subdir/another.txt").display()
        );
        assert_eq!(result.content, expected_content);
        assert_eq!(result.files.len(), 2);
        Ok(())
    }

    #[test]
    fn test_grab_contents_with_pdf_conversion_enabled() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        let base_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let fixtures_dir = base_dir.join("tests/fixtures");
        fs::create_dir_all(&fixtures_dir)?;
        let fixture_pdf_src = fixtures_dir.join("sample.pdf");

        if !fixture_pdf_src.exists() {
            anyhow::bail!("Fixture PDF not found at {:?}", fixture_pdf_src);
        }

        let fixture_pdf_dest = path.join("sample.pdf");
        fs::copy(&fixture_pdf_src, &fixture_pdf_dest).with_context(|| {
            format!(
                "Failed to copy fixture PDF from {:?} to {:?}",
                fixture_pdf_src, fixture_pdf_dest
            )
        })?;

        fs::write(path.join("normal.txt"), "Normal text content.")?;

        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: true,
            exclude_patterns: vec![
                "dirgrab.txt".into(),
                "*.log".into(),
                "*.dat".into(),
                "*.rs".into(),
                "subdir/".into(),
                ".gitignore".into(),
                "deep/".into(),
                "untracked.txt".into(),
            ],
            include_untracked: false,
            include_default_output: false,
            no_git: true,
            include_tree: false,
            convert_pdf: true,
            all_repo: false,
        };

        let result_string = grab_contents(&config)?;

        // Check PDF header
        let expected_pdf_header = "--- FILE: sample.pdf (extracted text) ---";
        assert!(
            result_string.contains(expected_pdf_header),
            "Missing or incorrect PDF header. Output:\n{}",
            result_string
        );

        // *** Update expected content based on actual PDF text - try a different snippet ***
        // let expected_pdf_content = "Pines are the largest and most"; // Original snippet
        let expected_pdf_content = "Pinaceae family"; // Try this snippet instead

        // Add a println to see exactly what is being searched for and in what
        println!("Searching for: '{}'", expected_pdf_content);
        println!("Within: '{}'", result_string);

        assert!(
            result_string.contains(expected_pdf_content),
            "Missing extracted PDF content ('{}'). Output:\n{}",
            expected_pdf_content,
            result_string
        );

        // Check normal text file header and content
        let expected_txt_header = "--- FILE: normal.txt ---";
        let expected_txt_content = "Normal text content.";
        assert!(
            result_string.contains(expected_txt_header),
            "Missing or incorrect TXT header. Output:\n{}",
            result_string
        );
        assert!(
            result_string.contains(expected_txt_content),
            "Missing TXT content. Output:\n{}",
            result_string
        );

        // Check that file1.txt (not excluded) is present
        let expected_file1_header = "--- FILE: file1.txt ---";
        assert!(
            result_string.contains(expected_file1_header),
            "Missing file1.txt header. Output:\n{}",
            result_string
        );

        Ok(())
    }

    #[test]
    fn test_grab_contents_with_pdf_conversion_disabled() -> Result<()> {
        let (_dir, path) = setup_test_dir()?; // Use existing helper
        let base_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let fixtures_dir = base_dir.join("tests/fixtures");
        fs::create_dir_all(&fixtures_dir)?; // Ensure exists
        let fixture_pdf_src = fixtures_dir.join("sample.pdf");

        // Create dummy if needed
        if !fixture_pdf_src.exists() {
            let basic_pdf_content = "%PDF-1.4\n1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj\n2 0 obj<</Type/Pages/Count 1/Kids[3 0 R]>>endobj\n3 0 obj<</Type/Page/MediaBox[0 0 612 792]/Contents 4 0 R/Resources<<>>>>endobj\n4 0 obj<</Length 52>>stream\nBT /F1 12 Tf 72 712 Td (This is sample PDF text content.) Tj ET\nendstream\nendobj\nxref\n0 5\n0000000000 65535 f \n0000000010 00000 n \n0000000063 00000 n \n0000000117 00000 n \n0000000198 00000 n \ntrailer<</Size 5/Root 1 0 R>>\nstartxref\n315\n%%EOF";
            fs::write(&fixture_pdf_src, basic_pdf_content)?;
            println!(
                "Created dummy sample.pdf for testing at {:?}",
                fixture_pdf_src
            );
        }

        let fixture_pdf_dest = path.join("sample.pdf");
        fs::copy(&fixture_pdf_src, &fixture_pdf_dest).with_context(|| {
            format!(
                "Failed to copy fixture PDF from {:?} to {:?}",
                fixture_pdf_src, fixture_pdf_dest
            )
        })?;
        fs::write(path.join("normal.txt"), "Normal text content.")?;

        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: true,
            // Exclude many things to simplify output check
            exclude_patterns: vec![
                "dirgrab.txt".into(),
                "*.log".into(),
                "*.dat".into(),
                "*.rs".into(),
                "subdir/".into(),
                ".gitignore".into(),
                "deep/".into(),
                "untracked.txt".into(),
            ],
            include_untracked: false,
            include_default_output: false,
            no_git: true,
            include_tree: false,
            convert_pdf: false, // Disable PDF conversion
            all_repo: false,
        };

        let result_string = grab_contents(&config)?;

        // Check PDF is NOT processed as text
        let unexpected_pdf_header_part = "(extracted text)"; // Check for the specific part of the header
        let unexpected_pdf_content = "This is sample PDF text content.";
        assert!(
            !result_string.contains(unexpected_pdf_header_part),
            "PDF extracted text header part present unexpectedly. Output:\n{}",
            result_string
        );
        assert!(
            !result_string.contains(unexpected_pdf_content),
            "Extracted PDF content present unexpectedly. Output:\n{}",
            result_string
        );

        // Check normal text file is still included
        let expected_txt_header = "--- FILE: normal.txt ---";
        let expected_txt_content = "Normal text content.";
        assert!(
            result_string.contains(expected_txt_header),
            "Missing or incorrect TXT header. Output:\n{}",
            result_string
        );
        assert!(
            result_string.contains(expected_txt_content),
            "Missing TXT content. Output:\n{}",
            result_string
        );

        // Check that file1.txt (not excluded) is present
        let expected_file1_header = "--- FILE: file1.txt ---";
        assert!(
            result_string.contains(expected_file1_header),
            "Missing file1.txt header. Output:\n{}",
            result_string
        );

        // With convert_pdf: false, the PDF should be skipped as non-UTF8 by the fallback logic.
        // Check that the standard PDF header does NOT appear either.
        let regular_pdf_header = "--- FILE: sample.pdf ---";
        assert!(
            !result_string.contains(regular_pdf_header),
            "Regular PDF header present when it should have been skipped as non-utf8. Output:\n{}",
            result_string
        );

        Ok(())
    }
    #[test]
    fn test_list_files_returns_display_paths() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: false,
            exclude_patterns: vec![
                "*.log".to_string(),
                "*.dat".to_string(),
                "dirgrab.txt".to_string(),
            ],
            include_untracked: false,
            include_default_output: false,
            no_git: true,
            include_tree: false,
            convert_pdf: false,
            all_repo: false,
        };
        let paths = list_files(&config)?;

        // Should return relative display paths, sorted
        assert!(paths.contains(&"file1.txt".to_string()));
        assert!(paths.contains(&"file2.rs".to_string()));
        assert!(paths.contains(&"subdir/another.txt".to_string()));
        // Excluded files should not appear
        assert!(!paths.iter().any(|p| p.ends_with(".log")));
        assert!(!paths.iter().any(|p| p.ends_with(".dat")));
        assert!(!paths.iter().any(|p| p.contains("dirgrab.txt")));

        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn test_walkdir_follows_symlinks() -> Result<()> {
        let dir = tempdir()?;
        let path = dir.path().to_path_buf();

        fs::write(path.join("real_file.txt"), "real content")?;
        std::os::unix::fs::symlink(path.join("real_file.txt"), path.join("link.txt"))?;

        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: false,
            exclude_patterns: vec![],
            include_untracked: false,
            include_default_output: true,
            no_git: true,
            include_tree: false,
            convert_pdf: false,
            all_repo: false,
        };
        let files = crate::listing::list_files_walkdir(&path, &config)?;
        let filenames: Vec<String> = files
            .iter()
            .filter_map(|p| p.file_name().map(|n| n.to_string_lossy().to_string()))
            .collect();

        assert!(
            filenames.contains(&"real_file.txt".to_string()),
            "real file should be included"
        );
        assert!(
            filenames.contains(&"link.txt".to_string()),
            "symlink should be followed and included"
        );
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn test_walkdir_rejects_symlinks_outside_target() -> Result<()> {
        let outer_dir = tempdir()?;
        let outside = outer_dir.path().join("outside");
        fs::create_dir_all(&outside)?;
        fs::write(outside.join("secret.txt"), "secret content")?;

        let target = outer_dir.path().join("project");
        fs::create_dir_all(&target)?;
        fs::write(target.join("local.txt"), "local content")?;

        // Create a symlink inside project/ that points to the outside directory
        std::os::unix::fs::symlink(&outside, target.join("escape_link"))?;

        let config = GrabConfig {
            target_path: target.clone(),
            add_headers: false,
            exclude_patterns: vec![],
            include_untracked: false,
            include_default_output: true,
            no_git: true,
            include_tree: false,
            convert_pdf: false,
            all_repo: false,
        };
        let files = crate::listing::list_files_walkdir(&target, &config)?;
        let filenames: Vec<String> = files
            .iter()
            .filter_map(|p| p.file_name().map(|n| n.to_string_lossy().to_string()))
            .collect();

        assert!(
            filenames.contains(&"local.txt".to_string()),
            "local file should be included"
        );
        assert!(
            !filenames.contains(&"secret.txt".to_string()),
            "file from outside target directory should NOT be included via symlink"
        );
        Ok(())
    }

    #[test]
    fn test_pdf_failure_segment_consistency() -> Result<()> {
        let (_dir, path) = setup_test_dir()?;
        // Create a file with .pdf extension but invalid PDF content
        fs::write(path.join("bad.pdf"), "this is not a valid pdf")?;
        fs::write(path.join("good.txt"), "hello world")?;

        let files = vec![path.join("bad.pdf"), path.join("good.txt")];
        let config = GrabConfig {
            target_path: path.clone(),
            add_headers: true,
            exclude_patterns: vec![],
            include_untracked: false,
            include_default_output: false,
            no_git: true,
            include_tree: false,
            convert_pdf: true, // Enable PDF extraction (will fail on bad.pdf)
            all_repo: false,
        };

        let result = crate::processing::process_files(&files, &config, None, &path)?;

        // Both files should produce segments
        assert_eq!(result.files.len(), 2, "Expected 2 file segments");

        let pdf_seg = &result.files[0];
        let txt_seg = &result.files[1];

        // PDF failure: header should end with \n (not \n\n)
        let header = &result.content[pdf_seg.header_range.clone().unwrap()];
        assert!(
            header.ends_with("---\n"),
            "PDF failure header should end with ---\\n, got: {:?}",
            header
        );
        // PDF failure: body_range should be non-empty (contains trailing \n)
        assert!(
            !pdf_seg.body_range.is_empty(),
            "PDF failure body_range should not be empty"
        );
        let body = &result.content[pdf_seg.body_range.clone()];
        assert_eq!(body, "\n", "PDF failure body should be a single newline");

        // Successful file: header should also end with \n
        let txt_header = &result.content[txt_seg.header_range.clone().unwrap()];
        assert!(
            txt_header.ends_with("---\n"),
            "Normal header should end with ---\\n, got: {:?}",
            txt_header
        );
        // Successful file: body_range should be non-empty
        assert!(
            !txt_seg.body_range.is_empty(),
            "Normal body_range should not be empty"
        );

        Ok(())
    }
} // End of mod tests