yek 0.25.5

A tool to serialize a repository into chunks of text files
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
#[cfg(test)]
mod lib_tests {
    use std::fs;
    use std::io::Write;
    #[cfg(unix)]
    use std::os::unix::fs::PermissionsExt;
    use tempfile::tempdir;

    use tracing_subscriber::{EnvFilter, FmtSubscriber};

    use yek::{
        concat_files, config::YekConfig, count_tokens, is_text_file, models::ProcessedFile,
        parse_token_limit, priority::PriorityRule, serialize_repo,
    };

    #[cfg(unix)]
    fn make_unreadable(path: &std::path::Path) -> std::io::Result<()> {
        let mut permissions = fs::metadata(path)?.permissions();
        permissions.set_mode(0o000);
        fs::set_permissions(path, permissions)
    }

    #[cfg(not(unix))]
    fn make_unreadable(_path: &std::path::Path) -> std::io::Result<()> {
        // On Windows, we can't easily make files unreadable in the same way
        // Skip this test functionality on Windows
        Ok(())
    }

    #[cfg(unix)]
    fn make_readable(path: &std::path::Path) -> std::io::Result<()> {
        let mut permissions = fs::metadata(path)?.permissions();
        permissions.set_mode(0o644);
        fs::set_permissions(path, permissions)
    }

    #[cfg(not(unix))]
    fn make_readable(_path: &std::path::Path) -> std::io::Result<()> {
        // On Windows, files are readable by default
        Ok(())
    }

    // Initialize tracing subscriber for tests
    fn init_tracing() {
        let _ = FmtSubscriber::builder()
            .with_env_filter(EnvFilter::from_default_env())
            .try_init();
    }

    fn create_test_config(input_dirs: Vec<String>) -> YekConfig {
        let mut config = YekConfig::extend_config_with_defaults(
            input_dirs,
            std::env::temp_dir().to_string_lossy().to_string(),
        );
        config.ignore_patterns = vec!["*.log".to_string()];
        config.priority_rules = vec![PriorityRule {
            pattern: "src/.*\\.rs".to_string(),
            score: 100,
        }];
        config.binary_extensions = vec!["bin".to_string()];
        config.output_template = Some(">>>> FILE_PATH\nFILE_CONTENT".to_string());
        config
    }

    #[test]
    fn test_serialize_repo_empty_dir() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        let result = serialize_repo(&config);
        assert!(result.is_ok());
    }

    #[test]
    fn test_serialize_repo_with_files() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        std::fs::write(temp_dir.path().join("test.txt"), "test content").unwrap();

        let config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        let result = serialize_repo(&config);
        assert!(result.is_ok());
    }

    #[test]
    fn test_serialize_repo_multiple_dirs() {
        init_tracing();
        let dir1 = tempdir().unwrap();
        let dir2 = tempdir().unwrap();

        std::fs::write(dir1.path().join("test1.txt"), "content1").unwrap();
        std::fs::write(dir2.path().join("test2.txt"), "content2").unwrap();

        let config = create_test_config(vec![
            dir1.path().to_string_lossy().to_string(),
            dir2.path().to_string_lossy().to_string(),
        ]);

        let result = serialize_repo(&config);
        assert!(result.is_ok());
    }

    #[test]
    fn test_serialize_repo_with_git() {
        init_tracing();
        let temp_dir = tempdir().unwrap();

        // Initialize git repo
        std::process::Command::new("git")
            .args(["init"])
            .current_dir(temp_dir.path())
            .output()
            .unwrap();

        // Create and commit a file
        std::fs::write(temp_dir.path().join("test.txt"), "test content").unwrap();
        std::process::Command::new("git")
            .args(["add", "test.txt"])
            .current_dir(temp_dir.path())
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["commit", "-m", "test commit"])
            .current_dir(temp_dir.path())
            .output()
            .unwrap();

        let config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        let result = serialize_repo(&config);
        assert!(result.is_ok());
    }

    #[test]
    fn test_is_text_file_with_extension() {
        let temp_dir = tempdir().unwrap();
        let text_file = temp_dir.path().join("test.txt");
        let binary_file = temp_dir.path().join("test.bin");

        fs::write(&text_file, "This is a text file.").unwrap();
        fs::write(&binary_file, b"\x00\x01\x02\x03").unwrap();

        assert!(is_text_file(&text_file, &[]).unwrap());
        assert!(!is_text_file(&binary_file, &[]).unwrap());

        // Test with a custom binary extension
        let custom_binary_file = temp_dir.path().join("test.custom");
        fs::write(&custom_binary_file, "This is a text file.").unwrap();
        assert!(!is_text_file(&custom_binary_file, &["custom".to_string()]).unwrap());
    }

    #[test]
    fn test_is_text_file_no_extension() {
        let dir = tempdir().unwrap();
        let text_file = dir.path().join("text_no_ext");
        let binary_file = dir.path().join("binary_no_ext");

        fs::write(&text_file, "This is text.").unwrap();
        fs::write(&binary_file, [0, 1, 2, 3, 4, 5]).unwrap(); // Binary content

        assert!(is_text_file(&text_file, &[]).unwrap());
        assert!(!is_text_file(&binary_file, &[]).unwrap());
    }

    #[test]
    fn test_is_text_file_empty_file() {
        let dir = tempdir().unwrap();
        let empty_file = dir.path().join("empty");

        fs::File::create(&empty_file).unwrap();

        assert!(is_text_file(&empty_file, &[]).unwrap()); // Empty file is considered text
    }

    #[test]
    fn test_is_text_file_with_user_binary_extensions() {
        let dir = tempdir().unwrap();
        let custom_bin_file = dir.path().join("data.dat");

        fs::write(&custom_bin_file, "binary data").unwrap();

        assert!(
            !is_text_file(&custom_bin_file, &["dat".to_string()]).unwrap(),
            "Custom binary extension should be detected as binary"
        );
    }

    #[test]
    fn test_is_text_file_mixed_content() {
        let dir = tempdir().unwrap();
        let mixed_file = dir.path().join("mixed.xyz");

        // Create a file with mostly text but one null byte
        let mut file = fs::File::create(&mixed_file).unwrap();
        file.write_all(b"This is mostly text.\0But with a null byte.")
            .unwrap();

        assert!(!is_text_file(&mixed_file, &[]).unwrap());
    }

    #[test]
    fn test_is_text_file_utf8_content() {
        let dir = tempdir().unwrap();
        let utf8_file = dir.path().join("utf8.txt");

        fs::write(&utf8_file, "こんにちは世界").unwrap(); // Japanese characters

        assert!(is_text_file(&utf8_file, &[]).unwrap());
    }

    #[test]
    fn test_typescript_files_not_treated_as_binary() {
        // Test that .ts files (TypeScript) are correctly treated as text files
        // and not confused with .ts video transport stream files
        use yek::defaults::BINARY_FILE_EXTENSIONS;

        let dir = tempdir().unwrap();
        let ts_file = dir.path().join("example.ts");

        // Create a typical TypeScript file with text content
        fs::write(
            &ts_file,
            "interface User {\n  name: string;\n  age: number;\n}\n",
        )
        .unwrap();

        // Check that "ts" is NOT in the binary extensions list
        assert!(
            !BINARY_FILE_EXTENSIONS.contains(&"ts"),
            "TypeScript extension 'ts' should not be in binary extensions list"
        );

        // Verify the file is detected as text
        assert!(
            is_text_file(
                &ts_file,
                BINARY_FILE_EXTENSIONS
                    .iter()
                    .map(|s| s.to_string())
                    .collect::<Vec<_>>()
                    .as_slice()
            )
            .unwrap(),
            "TypeScript files should be detected as text files"
        );
    }

    #[test]
    fn test_is_text_file_large_text_file() {
        let dir = tempdir().unwrap();
        let large_text_file = dir.path().join("large.txt");

        // Create a 1MB text file
        let content = "a".repeat(1024 * 1024);
        fs::write(&large_text_file, &content).unwrap();

        assert!(is_text_file(&large_text_file, &[]).unwrap());
    }

    #[test]
    fn test_is_text_file_with_shebang() {
        let dir = tempdir().unwrap();
        let script_file = dir.path().join("script.sh");

        // Write a shebang as the first line
        fs::write(&script_file, "#!/bin/bash\necho 'Hello'").unwrap();

        assert!(is_text_file(&script_file, &[]).unwrap());
    }

    // Output format tests
    #[test]
    fn test_serialize_repo_json_output() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        std::fs::write(temp_dir.path().join("test.txt"), "test content").unwrap();

        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.json = true;
        let result = serialize_repo(&config).unwrap();
        let output_string = result.0;
        assert!(output_string.contains(r#""filename": "test.txt""#));
        assert!(output_string.contains(r##""content": "test content"##));
    }

    #[test]
    fn test_serialize_repo_template_output() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        std::fs::write(temp_dir.path().join("test.txt"), "test content").unwrap();

        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.output_template =
            Some("Custom template:\nPath: FILE_PATH\nContent: FILE_CONTENT".to_string());
        let result = serialize_repo(&config).unwrap();
        let output_string = result.0;
        assert!(output_string.contains("Custom template:"));
        assert!(output_string.contains("Path: test.txt"));
        assert!(output_string.contains("Content: test content"));
    }

    #[test]
    fn test_serialize_repo_json_output_multiple_files() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        std::fs::write(temp_dir.path().join("file1.txt"), "content1").unwrap();
        std::fs::write(temp_dir.path().join("file2.txt"), "content2").unwrap();

        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.json = true;
        let result = serialize_repo(&config).unwrap();
        let output_string = result.0;
        assert!(output_string.contains(r#""filename": "file1.txt""#));
        assert!(output_string.contains(r##""content": "content1"##));
        assert!(output_string.contains(r#""filename": "file2.txt""#));
        assert!(output_string.contains(r##""content": "content2"##));
    }

    #[test]
    fn test_serialize_repo_template_output_no_files() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        let result = serialize_repo(&config).unwrap();
        let output_string = result.0;
        assert_eq!(output_string, ""); // Should be empty string when no files
    }

    #[test]
    fn test_serialize_repo_json_output_no_files() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.json = true;
        let result = serialize_repo(&config).unwrap();
        let output_string = result.0;
        assert_eq!(output_string, "[]"); // Should be empty JSON array when no files
    }

    #[test]
    fn test_serialize_repo_template_output_special_chars() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let file_path = "file with spaces and ünicöde.txt";
        let file_content = "content with <special> & \"chars\"\nand newlines";
        std::fs::write(temp_dir.path().join(file_path), file_content).unwrap();

        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.output_template = Some("Path: FILE_PATH\nContent:\nFILE_CONTENT".to_string());
        let result = serialize_repo(&config).unwrap();
        let output_string = result.0;

        assert!(output_string.contains(&format!("Path: {}", file_path)));
        assert!(output_string.contains(&format!("Content:\n{}", file_content)));
    }

    #[test]
    fn test_serialize_repo_json_output_special_chars() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let file_path = "file with spaces and ünicöde.txt";
        let file_content = "content with <special> & \"chars\"\nand newlines";
        std::fs::write(temp_dir.path().join(file_path), file_content).unwrap();

        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.json = true;
        let result = serialize_repo(&config).unwrap();
        let output_string = result.0;

        assert!(output_string.contains(r#""filename": "file with spaces and ünicöde.txt""#));
        assert!(output_string
            .contains(r##""content": "content with <special> & \"chars\"\nand newlines"##));
    }

    #[test]
    fn test_serialize_repo_template_backslash_n_replace() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        std::fs::write(temp_dir.path().join("test.txt"), "test content").unwrap();

        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.output_template = Some("Path: FILE_PATH\\nContent: FILE_CONTENT".to_string()); // Using literal "\\n"
        let result = serialize_repo(&config).unwrap();
        let output_string = result.0;
        assert!(output_string.contains("Path: test.txt\\nContent: test content")); // Should not replace "\\n" literally

        let mut config_replace =
            create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config_replace.output_template =
            Some("Path: FILE_PATH\\\\nContent: FILE_CONTENT".to_string()); // Using literal "\\\\n" to represent escaped backslash n
        let result_replace = serialize_repo(&config_replace).unwrap();
        let output_string_replace = result_replace.0;
        assert!(output_string_replace.contains("Path: test.txt\nContent: test content"));
        // Should replace "\\\\n" with newline
    }

    // Sort order tests
    #[test]
    fn test_serialize_repo_sort_order() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        // Create files with different priorities and names to check sort order
        std::fs::write(temp_dir.path().join("file_b.txt"), "content").unwrap(); // Default priority 0, index 1
        std::fs::write(temp_dir.path().join("file_a.txt"), "content").unwrap(); // Default priority 0, index 0
        std::fs::create_dir(temp_dir.path().join("src")).unwrap();
        std::fs::write(temp_dir.path().join("src/file_c.rs"), "content").unwrap(); // Priority 100, index 0

        let config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        let result = serialize_repo(&config).unwrap();

        // print results
        for file in result.1.iter() {
            println!("{}: {}", file.rel_path, file.priority);
        }
        let files = result.1;

        assert_eq!(files.len(), 3);
        assert_eq!(files[0].rel_path, "file_a.txt"); // Priority 0, index 0
        assert_eq!(files[1].rel_path, "file_b.txt"); // Priority 0, index 1
        assert_eq!(files[2].rel_path, "src/file_c.rs"); // Highest priority (100) comes last
    }

    // Error handling tests

    #[test]
    fn test_serialize_repo_file_read_error() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let file_path = temp_dir.path().join("test.txt");
        std::fs::write(&file_path, "test content").unwrap();
        let config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);

        if cfg!(unix) {
            // Make the file unreadable (Unix only)
            make_unreadable(&file_path).unwrap();

            let result = serialize_repo(&config);
            // In case of read error, it should still return Ok but skip the file
            assert!(result.is_ok());
            let output = result.unwrap();
            assert_eq!(output.1.len(), 0); // No files processed due to read error

            // Restore permissions so temp dir can be deleted
            make_readable(&file_path).unwrap();
        } else {
            // On Windows, just test normal processing
            let result = serialize_repo(&config);
            assert!(result.is_ok());
            let output = result.unwrap();
            assert_eq!(output.1.len(), 1); // File should be processed normally
        }
    }

    #[test]
    fn test_serialize_repo_json_error() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        std::fs::write(temp_dir.path().join("test.txt"), "test content").unwrap();

        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.json = true;
        // Simulate a JSON serialization error by making files empty, which might cause issues if content is not handled properly
        let result = serialize_repo(&config);
        assert!(result.is_ok(), "serialize_repo should not error even if JSON serialization might have issues with empty content");
    }

    #[test]
    fn test_is_text_file_io_error() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let file_path = temp_dir.path().join("unreadable.txt");
        fs::write(&file_path, "test content").unwrap();

        if cfg!(unix) {
            // Make the file unreadable (Unix only)
            make_unreadable(&file_path).unwrap();

            let result = is_text_file(&file_path, &[]);
            assert!(
                result.is_err(),
                "is_text_file should return Err for unreadable file"
            );

            // Restore permissions so temp dir can be deleted
            make_readable(&file_path).unwrap();
        } else {
            // On Windows, just test that the function works normally
            let result = is_text_file(&file_path, &[]);
            assert!(result.is_ok(), "is_text_file should succeed on Windows");
        }
    }

    #[test]
    fn test_serialize_repo_with_priority_rules() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        std::fs::write(temp_dir.path().join("file.data"), "content").unwrap();
        std::fs::write(temp_dir.path().join("src_file.rs"), "content").unwrap();

        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.priority_rules = vec![PriorityRule {
            pattern: "src_.*".to_string(),
            score: 500,
        }];
        let result = serialize_repo(&config).unwrap();
        let files = result.1;
        assert_eq!(files.len(), 2);
        assert_eq!(files[0].rel_path, "file.data");
        // file.data gets category "Other" (priority offset: 1) + no rule matches = 1
        assert_eq!(files[0].priority, 1);
        assert_eq!(files[1].rel_path, "src_file.rs"); // Highest priority comes last
                                                      // src_file.rs gets category "Source" (priority offset: 20) + rule match (500) = 520
        assert_eq!(files[1].priority, 520);
    }

    #[test]
    fn test_serialize_repo_with_ignore_patterns_config() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        std::fs::write(temp_dir.path().join("file.txt"), "content").unwrap();
        std::fs::write(temp_dir.path().join("log.log"), "log content").unwrap();

        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.ignore_patterns = vec!["*.log".to_string()];
        let result = serialize_repo(&config).unwrap();
        let files = result.1;
        assert_eq!(files.len(), 1);
        assert_eq!(files[0].rel_path, "file.txt"); // log.log should be ignored
    }

    #[test]
    fn test_serialize_repo_with_binary_extensions_config() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        std::fs::write(temp_dir.path().join("file.txt"), "content").unwrap();
        std::fs::write(temp_dir.path().join("data.bin"), [0u8, 1u8, 2u8]).unwrap();

        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.binary_extensions = vec!["bin".to_string()];
        let result = serialize_repo(&config).unwrap();
        let files = result.1;
        assert_eq!(files.len(), 1);
        assert_eq!(files[0].rel_path, "file.txt"); // data.bin should be ignored
    }

    #[test]
    fn test_concat_files_empty_files() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        let files = vec![];
        let output = yek::concat_files(&files, &config).unwrap();
        assert_eq!(output, "");
    }

    #[test]
    fn test_concat_files_json_output_empty_files() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.json = true;
        let files = vec![];
        let output = yek::concat_files(&files, &config).unwrap();
        assert_eq!(output, "[]");
    }

    #[test]
    fn test_concat_files_various_inputs() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);

        let files = vec![
            ProcessedFile::new(
                "src/main.rs".to_string(),
                "fn main() {}".to_string(),
                100,
                0,
            ),
            ProcessedFile::new("README.md".to_string(), "# Yek".to_string(), 50, 1),
        ];

        // Test default template
        let output_default = yek::concat_files(&files, &config).unwrap();
        assert!(output_default.contains(">>>> src/main.rs\nfn main() {}"));
        assert!(output_default.contains(">>>> README.md\n# Yek"));

        // Test JSON output
        config.json = true;
        let output_json = yek::concat_files(&files, &config).unwrap();
        assert!(output_json.contains(r#""filename": "src/main.rs""#));
        assert!(output_json.contains(r#""content": "fn main() {}""#));
        assert!(output_json.contains(r#""filename": "README.md""#));
        assert!(output_json.contains(r##""content": "# Yek"##));

        // Test custom template
        config.json = false;
        config.output_template = Some("==FILE_PATH==\n---\nFILE_CONTENT\n====".to_string());
        let output_custom = yek::concat_files(&files, &config).unwrap();
        assert!(output_custom.contains("==src/main.rs==\n---\nfn main() {}\n===="));
        assert!(output_custom.contains("==README.md==\n---\n# Yek\n===="));
    }

    #[test]
    fn test_concat_files_json_output_special_chars_in_filename() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.json = true;

        let files = vec![ProcessedFile::new(
            "file with ünicöde.txt".to_string(),
            "content".to_string(),
            100,
            0,
        )];
        let output_json = yek::concat_files(&files, &config).unwrap();
        assert!(output_json.contains(r#""filename": "file with ünicöde.txt""#));
    }

    #[test]
    fn test_concat_files_template_output_empty_content() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.json = false;

        let files = vec![ProcessedFile::new(
            "file.txt".to_string(),
            "".to_string(),
            100,
            0,
        )];
        let output_template = yek::concat_files(&files, &config).unwrap();
        assert!(output_template.contains(">>>> file.txt\n")); // Should handle empty content
    }

    #[test]
    fn test_concat_files_json_output_empty_content() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.json = true;

        let files = vec![ProcessedFile::new(
            "file.txt".to_string(),
            "".to_string(),
            100,
            0,
        )];
        let output_json = yek::concat_files(&files, &config).unwrap();
        assert!(output_json.contains(r#""content": """#)); // Should handle empty content in JSON
    }

    #[test]
    fn test_token_counting_basic() {
        let text = "Hello, world! This is a test.";
        let tokens = count_tokens(text);
        // GPT tokenizer has its own tokenization rules that may not match our assumptions
        assert_eq!(tokens, 9);
    }

    #[test]
    fn test_token_counting_with_template() {
        let config = YekConfig {
            output_template: Some("File: FILE_PATH\nContent:\nFILE_CONTENT".to_string()),
            ..Default::default()
        };
        let files = vec![ProcessedFile::new(
            "test.txt".to_string(),
            "Hello world".to_string(),
            0,
            0,
        )];
        let output = concat_files(&files, &config).unwrap();
        let tokens = count_tokens(&output);
        // Verify token count includes template overhead
        assert!(tokens > count_tokens("Hello world"));
    }

    #[test]
    fn test_token_counting_with_json() {
        let config = YekConfig {
            json: true,
            ..Default::default()
        };
        let files = vec![ProcessedFile::new(
            "test.txt".to_string(),
            "Hello world".to_string(),
            0,
            0,
        )];
        let output = concat_files(&files, &config).unwrap();
        let tokens = count_tokens(&output);
        // Verify token count includes JSON structure overhead
        assert!(tokens > count_tokens("Hello world"));
    }

    #[test]
    fn test_token_limit_enforcement() {
        let config = YekConfig {
            token_mode: true,
            tokens: "10".to_string(), // Set a very low token limit
            // Include filename in template so we can verify which files are included
            output_template: Some(">>>> FILE_PATH\nFILE_CONTENT".to_string()),
            ..Default::default()
        };
        let files = vec![
            ProcessedFile::new(
                "test1.txt".to_string(),
                "This is a short test".to_string(),
                0,
                0,
            ),
            ProcessedFile::new(
                "test2.txt".to_string(),
                "This is another test that should be excluded".to_string(),
                0,
                1,
            ),
        ];
        let output = concat_files(&files, &config).unwrap();
        // Check that only the first file is included in the output
        assert!(
            output.contains("test1.txt"),
            "Expected file test1.txt to be present"
        );
        assert!(
            !output.contains("test2.txt"),
            "Expected file test2.txt to be excluded"
        );
    }

    #[test]
    fn test_parse_token_limit() {
        assert_eq!(parse_token_limit("1000").unwrap(), 1000);
        assert_eq!(parse_token_limit("1k").unwrap(), 1000);
        assert_eq!(parse_token_limit("1K").unwrap(), 1000);
        assert!(parse_token_limit("-1").is_err());
        assert!(parse_token_limit("invalid").is_err());
    }

    // Bug validation tests
    #[test]
    fn test_bug_119_cannot_handle_emojis() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let file_name = "file_with_emoji_😀.txt";
        std::fs::write(temp_dir.path().join(file_name), "content with emoji 😀").unwrap();

        let config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        let result = serialize_repo(&config);
        // If bug is present, this might fail
        assert!(
            result.is_ok(),
            "serialize_repo should handle files with emojis in names"
        );
        let (output, files) = result.unwrap();
        assert_eq!(files.len(), 1);
        // The rel_path should be relative to the current directory
        assert_eq!(files[0].rel_path, file_name);
        assert!(output.contains(&format!(">>>> {}\ncontent with emoji 😀", file_name)));
    }

    #[test]
    fn test_bug_125_file_paths_relativity_unreliable_with_globs() {
        init_tracing();
        let dir1 = tempdir().unwrap();
        let dir2 = tempdir().unwrap();

        std::fs::write(dir1.path().join("file.txt"), "content1").unwrap();
        std::fs::write(dir2.path().join("file.txt"), "content2").unwrap();

        // Use globs for multiple sources
        let config = create_test_config(vec![
            format!("{}/*.txt", dir1.path().to_string_lossy()),
            format!("{}/*.txt", dir2.path().to_string_lossy()),
        ]);

        let result = serialize_repo(&config);
        assert!(result.is_ok());
        let (output, files) = result.unwrap();
        // This test verifies that each file's rel_path is unique, which is the expected correct behavior.
        // If the bug is present (rel_path is unreliable), this assertion will fail.
        // The output should contain both file contents, and rel_paths should be unique.
        assert!(output.contains("content1"));
        assert!(output.contains("content2"));
        // Assert that rel_path is unique for each file.
        let rel_paths: std::collections::HashSet<_> = files.iter().map(|f| &f.rel_path).collect();
        assert_eq!(
            rel_paths.len(),
            files.len(),
            "rel_path should be unique for each file"
        );
    }

    #[test]
    fn test_bug_multiple_input_dirs_preserve_root_names_in_output() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let dir1 = temp_dir.path().join("dir_1");
        let dir2 = temp_dir.path().join("dir_2");
        let dir3 = temp_dir.path().join("dir_3");

        std::fs::create_dir_all(&dir1).unwrap();
        std::fs::create_dir_all(&dir2).unwrap();
        std::fs::create_dir_all(&dir3).unwrap();
        std::fs::write(dir1.join("file_1"), "content1").unwrap();
        std::fs::write(dir2.join("file_1"), "content2").unwrap();
        std::fs::write(dir3.join("file_1"), "content3").unwrap();

        let config = create_test_config(vec![
            dir1.to_string_lossy().to_string(),
            dir2.to_string_lossy().to_string(),
            dir3.to_string_lossy().to_string(),
        ]);

        let (output, files) = serialize_repo(&config).unwrap();

        assert!(files.iter().any(|file| file.rel_path == "dir_1/file_1"));
        assert!(files.iter().any(|file| file.rel_path == "dir_2/file_1"));
        assert!(files.iter().any(|file| file.rel_path == "dir_3/file_1"));
        assert!(output.contains(">>>> dir_1/file_1\ncontent1"));
        assert!(output.contains(">>>> dir_2/file_1\ncontent2"));
        assert!(output.contains(">>>> dir_3/file_1\ncontent3"));
    }

    #[test]
    fn test_bug_144_missing_file_paths_in_output() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        std::fs::write(temp_dir.path().join("test.txt"), "content").unwrap();

        let config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        let result = serialize_repo(&config).unwrap();
        let output = result.0;
        // Check that FILE_PATH is not empty
        assert!(
            output.contains(">>>> test.txt\ncontent"),
            "File path should not be missing in output"
        );
    }

    #[test]
    fn test_line_numbers_feature() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let content = "line 1\nline 2\nline 3";
        std::fs::write(temp_dir.path().join("test.txt"), content).unwrap();

        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.line_numbers = true;
        let result = serialize_repo(&config).unwrap();
        let output = result.0;

        // Check that line numbers are included
        assert!(output.contains("  1 | line 1"));
        assert!(output.contains("  2 | line 2"));
        assert!(output.contains("  3 | line 3"));
    }

    #[test]
    fn test_line_numbers_feature_json_output() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let content = "line 1\nline 2";
        std::fs::write(temp_dir.path().join("test.txt"), content).unwrap();

        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.line_numbers = true;
        config.json = true;
        let result = serialize_repo(&config).unwrap();
        let output = result.0;

        // Check that line numbers are included in JSON content
        assert!(output.contains(r#""content": "  1 | line 1\n  2 | line 2""#));
    }

    #[test]
    fn test_line_numbers_feature_empty_file() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        std::fs::write(temp_dir.path().join("empty.txt"), "").unwrap();

        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.line_numbers = true;
        let result = serialize_repo(&config).unwrap();
        let output = result.0;

        // Empty file should still have the file header
        assert!(output.contains(">>>> empty.txt\n"));
    }

    #[test]
    fn test_line_numbers_feature_single_line() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        std::fs::write(temp_dir.path().join("single.txt"), "single line").unwrap();

        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.line_numbers = true;
        let result = serialize_repo(&config).unwrap();
        let output = result.0;

        // Single line should have line number 1
        assert!(output.contains("  1 | single line"));
    }
    #[test]
    fn test_serialize_repo_with_nonexistent_paths() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let config = create_test_config(vec![
            temp_dir
                .path()
                .join("nonexistent1")
                .to_string_lossy()
                .to_string(),
            temp_dir
                .path()
                .join("nonexistent2")
                .to_string_lossy()
                .to_string(),
        ]);

        let result = serialize_repo(&config);
        // Should succeed but with warnings
        assert!(result.is_ok());
        let (output, files) = result.unwrap();
        assert!(files.is_empty()); // No files processed
        assert_eq!(output, ""); // Empty output
    }

    #[test]
    fn test_serialize_repo_with_mixed_existent_nonexistent() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        std::fs::write(temp_dir.path().join("existent.txt"), "content").unwrap();

        let config = create_test_config(vec![
            temp_dir
                .path()
                .join("existent.txt")
                .to_string_lossy()
                .to_string(),
            temp_dir
                .path()
                .join("nonexistent.txt")
                .to_string_lossy()
                .to_string(),
        ]);

        let result = serialize_repo(&config);
        assert!(result.is_ok());
        let (output, files) = result.unwrap();
        assert_eq!(files.len(), 1);
        assert!(output.contains("content"));
    }

    #[test]
    fn test_parse_token_limit_edge_cases() {
        // Test with very large numbers
        assert_eq!(parse_token_limit("999999k").unwrap(), 999999000);

        // Test with zero (parse_token_limit allows 0, validation happens elsewhere)
        assert_eq!(parse_token_limit("0").unwrap(), 0);
        assert_eq!(parse_token_limit("0k").unwrap(), 0); // 0k = 0 * 1000 = 0

        // Test with invalid format
        assert!(parse_token_limit("k").is_err());
        assert!(parse_token_limit("123k456").is_err());
    }

    #[test]
    fn test_concat_files_with_token_limit_exceeded() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.token_mode = true;
        config.tokens = "5".to_string(); // Very low token limit

        let files = vec![
            ProcessedFile::new(
                "long.txt".to_string(),
                "This is a very long piece of content that should exceed the token limit."
                    .to_string(),
                100,
                0,
            ),
            ProcessedFile::new("short.txt".to_string(), "Short".to_string(), 50, 1),
        ];

        let result = concat_files(&files, &config);
        assert!(result.is_ok());
        let output = result.unwrap();
        // Should include only the short file or part of the long one
        assert!(output.contains("Short") || output.len() < 100);
    }

    #[test]
    fn test_serialize_repo_with_debug_logging() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        std::fs::write(temp_dir.path().join("test.txt"), "test content").unwrap();

        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.debug = true; // Enable debug logging

        let result = serialize_repo(&config);
        assert!(result.is_ok());
        // The function should work with debug enabled
    }

    #[test]
    fn test_concat_files_with_tree_header_and_token_mode() {
        init_tracing();
        let temp_dir = tempdir().unwrap();
        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.tree_header = true;
        config.token_mode = true;
        config.tokens = "1000".to_string();

        let files = vec![ProcessedFile::new(
            "test.txt".to_string(),
            "content".to_string(),
            100,
            0,
        )];

        let result = concat_files(&files, &config);
        assert!(result.is_ok());
        let output = result.unwrap();
        // Should include tree header and content
        assert!(output.contains("test.txt"));
        assert!(output.contains("content"));
    }

    // Priority 3: Output generation tests
    #[test]
    fn test_template_processing_with_special_escaping() {
        init_tracing();
        let temp_dir = tempdir().unwrap();

        // Create file with content that needs escaping
        let content = "Line with \"quotes\" and \\backslash\\ and $special {chars}";
        fs::write(temp_dir.path().join("special.txt"), content).unwrap();

        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.output_template = Some("=== FILE_PATH ===\n{{FILE_CONTENT}}".to_string());

        let result = serialize_repo(&config);
        assert!(result.is_ok());
        let (output, _) = result.unwrap();

        // Template should handle special characters
        assert!(output.contains("=== special.txt ==="));
        assert!(output.contains("{{Line with \"quotes\""));
    }

    #[test]
    fn test_json_output_with_special_characters() {
        init_tracing();
        let temp_dir = tempdir().unwrap();

        // Create file with JSON special characters
        let content = r#"{"key": "value with \"quotes\" and \n newline"}"#;
        fs::write(temp_dir.path().join("data.json"), content).unwrap();

        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.json = true;

        let result = serialize_repo(&config);
        assert!(result.is_ok());
        let (output, _) = result.unwrap();

        // JSON output should properly escape nested JSON
        assert!(output.contains(r#""filename": "data.json""#));
        // Content should be properly escaped
        assert!(output.contains(r#"\"quotes\""#) || output.contains(r#"\\\"quotes\\\""#));
    }

    #[test]
    fn test_token_limit_overflow_handling() {
        init_tracing();
        let temp_dir = tempdir().unwrap();

        // Create multiple files that exceed token limit
        for i in 0..10 {
            let content = format!(
                "This is file {} with some content that will contribute to token count",
                i
            );
            fs::write(temp_dir.path().join(format!("file{}.txt", i)), content).unwrap();
        }

        let mut config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);
        config.token_mode = true;
        config.tokens = "50".to_string(); // Very low limit

        let result = concat_files(
            &[
                ProcessedFile::new(
                    "file0.txt".to_string(),
                    "This is file 0 with some content that will contribute to token count"
                        .to_string(),
                    0,
                    0,
                ),
                ProcessedFile::new(
                    "file1.txt".to_string(),
                    "This is file 1 with some content that will contribute to token count"
                        .to_string(),
                    0,
                    1,
                ),
            ],
            &config,
        );

        assert!(result.is_ok());
        let output = result.unwrap();
        // Should include at least one file but not all due to token limit
        assert!(output.contains("file0.txt") || output.contains("file1.txt"));
        assert!(output.len() < 500); // Should be truncated
    }

    #[test]
    fn test_tree_rendering_with_unicode_paths() {
        use std::path::PathBuf;
        use yek::tree::generate_tree;

        let paths = vec![
            PathBuf::from("文档/说明.txt"),
            PathBuf::from("código/main.rs"),
            PathBuf::from("файлы/данные.json"),
        ];

        let result = generate_tree(&paths);

        // Should handle Unicode paths correctly
        assert!(result.contains("文档/"));
        assert!(result.contains("说明.txt"));
        assert!(result.contains("código/"));
        assert!(result.contains("main.rs"));
        assert!(result.contains("файлы/"));
        assert!(result.contains("данные.json"));
    }

    #[test]
    fn test_tree_rendering_empty_prefix() {
        use std::path::PathBuf;
        use yek::tree::generate_tree;

        // Test with single root file (empty prefix case)
        let paths = vec![PathBuf::from("single.txt")];
        let result = generate_tree(&paths);

        assert!(result.contains("Directory structure:"));
        assert!(result.contains("└── single.txt"));
        // Should not have any prefix before the root item
        let lines: Vec<&str> = result.lines().collect();
        for line in lines {
            if line.contains("single.txt") {
                assert!(line.starts_with("└──"));
                break;
            }
        }
    }

    #[test]
    fn test_tree_rendering_root_only_paths() {
        use std::path::PathBuf;
        use yek::tree::generate_tree;

        // Test with only root-level files (no nested directories)
        let paths = vec![
            PathBuf::from("a.txt"),
            PathBuf::from("b.txt"),
            PathBuf::from("c.txt"),
        ];

        let result = generate_tree(&paths);

        assert!(result.contains("├── a.txt"));
        assert!(result.contains("├── b.txt"));
        assert!(result.contains("└── c.txt")); // Last item uses └──
    }
}