agnix-core 0.23.0

Core validation engine for agent configurations
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
//! FileSystem abstraction for testability
//!
//! This module provides a `FileSystem` trait that abstracts file system operations,
//! enabling validators to be unit tested with `MockFileSystem` instead of requiring
//! real temp files.
//!
//! ## Usage
//!
//! For production code, use `RealFileSystem` which delegates to `std::fs` and
//! the safe file reading utilities in `file_utils`.
//!
//! For tests, use `MockFileSystem` which provides an in-memory HashMap-based
//! storage with `RwLock` for thread safety.
//!
//! ## Security
//!
//! ### Symlink Handling
//!
//! Both implementations reject symlinks in `read_to_string()` to prevent path
//! traversal attacks. The `MockFileSystem` also implements depth limiting in
//! `metadata()` and `canonicalize()` to detect symlink loops.
//!
//! ### TOCTOU (Time-of-Check-Time-of-Use)
//!
//! There is an inherent TOCTOU window between checking file properties and
//! reading content. An attacker with local filesystem access could potentially
//! replace a regular file with a symlink between the check and read operations.
//! This is acceptable for a linter because:
//!
//! 1. The attack requires local filesystem access
//! 2. The impact is limited to reading unexpected content
//! 3. Eliminating TOCTOU entirely would require platform-specific APIs
//!    (O_NOFOLLOW on Unix, FILE_FLAG_OPEN_REPARSE_POINT on Windows)
//!
//! For high-security environments, users should run agnix in a sandboxed
//! environment or on trusted input only.
//!
//! ## Example
//!
//! ```rust,ignore
//! use agnix_core::fs::{FileSystem, MockFileSystem, RealFileSystem};
//! use std::path::Path;
//!
//! // In production code
//! let fs = RealFileSystem;
//! assert!(fs.exists(Path::new("Cargo.toml")));
//!
//! // In tests
//! let mock_fs = MockFileSystem::new();
//! mock_fs.add_file("/test/file.txt", "content");
//! assert!(mock_fs.exists(Path::new("/test/file.txt")));
//! ```

use crate::diagnostics::{CoreError, FileError, LintResult};
use std::collections::HashMap;
use std::fs::Metadata;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::RwLock;

/// Metadata information returned by the FileSystem trait.
///
/// This provides a subset of `std::fs::Metadata` that can be mocked.
#[derive(Debug, Clone)]
pub struct FileMetadata {
    /// Whether this is a regular file
    pub is_file: bool,
    /// Whether this is a directory
    pub is_dir: bool,
    /// Whether this is a symlink
    pub is_symlink: bool,
    /// File size in bytes
    pub len: u64,
}

impl FileMetadata {
    /// Create metadata for a regular file
    pub fn file(len: u64) -> Self {
        Self {
            is_file: true,
            is_dir: false,
            is_symlink: false,
            len,
        }
    }

    /// Create metadata for a directory
    pub fn directory() -> Self {
        Self {
            is_file: false,
            is_dir: true,
            is_symlink: false,
            len: 0,
        }
    }

    /// Create metadata for a symlink
    pub fn symlink() -> Self {
        Self {
            is_file: false,
            is_dir: false,
            is_symlink: true,
            len: 0,
        }
    }
}

impl From<&Metadata> for FileMetadata {
    fn from(meta: &Metadata) -> Self {
        Self {
            is_file: meta.is_file(),
            is_dir: meta.is_dir(),
            is_symlink: meta.file_type().is_symlink(),
            len: meta.len(),
        }
    }
}

/// Directory entry returned by `read_dir`.
#[derive(Debug, Clone)]
pub struct DirEntry {
    /// Path to this entry
    pub path: PathBuf,
    /// Metadata for this entry
    pub metadata: FileMetadata,
}

/// Trait for abstracting file system operations.
///
/// This trait must be `Send + Sync` to support rayon parallel validation.
/// It also requires `Debug` for use in config structs that derive Debug.
pub trait FileSystem: Send + Sync + std::fmt::Debug {
    /// Check if a path exists
    fn exists(&self, path: &Path) -> bool;

    /// Check if a path is a file
    fn is_file(&self, path: &Path) -> bool;

    /// Check if a path is a directory
    fn is_dir(&self, path: &Path) -> bool;

    /// Check if a path is a symlink
    fn is_symlink(&self, path: &Path) -> bool;

    /// Get metadata for a path (follows symlinks)
    fn metadata(&self, path: &Path) -> io::Result<FileMetadata>;

    /// Get metadata for a path without following symlinks
    fn symlink_metadata(&self, path: &Path) -> io::Result<FileMetadata>;

    /// Read file contents to string (with security checks)
    fn read_to_string(&self, path: &Path) -> LintResult<String>;

    /// Write content to file (with security checks)
    fn write(&self, path: &Path, content: &str) -> LintResult<()>;

    /// Canonicalize a path
    fn canonicalize(&self, path: &Path) -> io::Result<PathBuf>;

    /// Read directory contents
    fn read_dir(&self, path: &Path) -> io::Result<Vec<DirEntry>>;
}

/// Real file system implementation that delegates to `std::fs` and `file_utils`.
#[derive(Debug, Clone, Copy, Default)]
pub struct RealFileSystem;

impl FileSystem for RealFileSystem {
    fn exists(&self, path: &Path) -> bool {
        path.exists()
    }

    fn is_file(&self, path: &Path) -> bool {
        path.is_file()
    }

    fn is_dir(&self, path: &Path) -> bool {
        path.is_dir()
    }

    fn is_symlink(&self, path: &Path) -> bool {
        path.is_symlink()
    }

    fn metadata(&self, path: &Path) -> io::Result<FileMetadata> {
        std::fs::metadata(path).map(|m| FileMetadata::from(&m))
    }

    fn symlink_metadata(&self, path: &Path) -> io::Result<FileMetadata> {
        std::fs::symlink_metadata(path).map(|m| FileMetadata::from(&m))
    }

    fn read_to_string(&self, path: &Path) -> LintResult<String> {
        crate::file_utils::safe_read_file(path)
    }

    fn write(&self, path: &Path, content: &str) -> LintResult<()> {
        crate::file_utils::safe_write_file(path, content)
    }

    fn canonicalize(&self, path: &Path) -> io::Result<PathBuf> {
        std::fs::canonicalize(path)
    }

    fn read_dir(&self, path: &Path) -> io::Result<Vec<DirEntry>> {
        Ok(std::fs::read_dir(path)?
            .filter_map(|entry_res| {
                // Skip entries that fail to read (permission denied, etc.)
                // This matches the previous AS-015 behavior of tolerating bad entries
                let entry = entry_res.ok()?;
                let path = entry.path();
                // Use symlink_metadata to avoid following symlinks
                // Skip entries where metadata fails (transient errors)
                let metadata = std::fs::symlink_metadata(&path).ok()?;
                Some(DirEntry {
                    path,
                    metadata: FileMetadata::from(&metadata),
                })
            })
            .collect())
    }
}

/// Mock entry type for the in-memory file system.
#[derive(Debug, Clone)]
enum MockEntry {
    File { content: String },
    Directory,
    Symlink { target: PathBuf },
}

/// Mock file system for testing.
///
/// Provides an in-memory HashMap-based storage with `RwLock` for thread safety.
/// This enables unit testing validators without requiring real temp files.
#[derive(Debug, Default)]
pub struct MockFileSystem {
    entries: RwLock<HashMap<PathBuf, MockEntry>>,
}

impl MockFileSystem {
    /// Create a new empty mock file system
    pub fn new() -> Self {
        Self {
            entries: RwLock::new(HashMap::new()),
        }
    }

    /// Add a file with the given content
    pub fn add_file(&self, path: impl AsRef<Path>, content: impl Into<String>) {
        let path = normalize_mock_path(path.as_ref());
        let mut entries = self.entries.write().expect("MockFileSystem lock poisoned");
        entries.insert(
            path,
            MockEntry::File {
                content: content.into(),
            },
        );
    }

    /// Add a directory
    pub fn add_dir(&self, path: impl AsRef<Path>) {
        let path = normalize_mock_path(path.as_ref());
        let mut entries = self.entries.write().expect("MockFileSystem lock poisoned");
        entries.insert(path, MockEntry::Directory);
    }

    /// Add a symlink pointing to target
    pub fn add_symlink(&self, path: impl AsRef<Path>, target: impl AsRef<Path>) {
        let path = normalize_mock_path(path.as_ref());
        let target = normalize_mock_path(target.as_ref());
        let mut entries = self.entries.write().expect("MockFileSystem lock poisoned");
        entries.insert(path, MockEntry::Symlink { target });
    }

    /// Remove an entry
    pub fn remove(&self, path: impl AsRef<Path>) {
        let path = normalize_mock_path(path.as_ref());
        let mut entries = self.entries.write().expect("MockFileSystem lock poisoned");
        entries.remove(&path);
    }

    /// Clear all entries
    pub fn clear(&self) {
        let mut entries = self.entries.write().expect("MockFileSystem lock poisoned");
        entries.clear();
    }

    fn get_entry(&self, path: &Path) -> Option<MockEntry> {
        let path = normalize_mock_path(path);
        let entries = self.entries.read().expect("MockFileSystem lock poisoned");
        entries.get(&path).cloned()
    }

    fn resolve_symlink(&self, path: &Path) -> Option<PathBuf> {
        let path = normalize_mock_path(path);
        let entries = self.entries.read().expect("MockFileSystem lock poisoned");
        match entries.get(&path) {
            Some(MockEntry::Symlink { target }) => Some(target.clone()),
            _ => None,
        }
    }

    /// Maximum depth for symlink resolution to prevent infinite loops.
    ///
    /// This matches the typical OS limit (Linux ELOOP is triggered at 40 levels).
    /// Value chosen to match POSIX `SYMLOOP_MAX` and Linux's internal limit.
    /// See: https://man7.org/linux/man-pages/man3/fpathconf.3.html
    pub const MAX_SYMLINK_DEPTH: u32 = 40;

    /// Internal helper for metadata with depth tracking
    fn metadata_with_depth(&self, path: &Path, depth: u32) -> io::Result<FileMetadata> {
        if depth > Self::MAX_SYMLINK_DEPTH {
            return Err(io::Error::other("too many levels of symbolic links"));
        }

        // Follow symlinks - use an enum to handle the result outside the lock
        enum MetaResult {
            Found(FileMetadata),
            FollowSymlink(PathBuf),
        }

        let path = normalize_mock_path(path);

        let result: io::Result<MetaResult> = {
            let entries = self.entries.read().expect("MockFileSystem lock poisoned");
            match entries.get(&path) {
                None => Err(io::Error::new(
                    io::ErrorKind::NotFound,
                    format!("path not found: {}", path.display()),
                )),
                Some(MockEntry::File { content }) => {
                    Ok(MetaResult::Found(FileMetadata::file(content.len() as u64)))
                }
                Some(MockEntry::Directory) => Ok(MetaResult::Found(FileMetadata::directory())),
                Some(MockEntry::Symlink { target }) => {
                    Ok(MetaResult::FollowSymlink(target.clone()))
                }
            }
        };

        match result? {
            MetaResult::Found(meta) => Ok(meta),
            MetaResult::FollowSymlink(target) => self.metadata_with_depth(&target, depth + 1),
        }
    }

    /// Internal helper for canonicalize with depth tracking
    fn canonicalize_with_depth(&self, path: &Path, depth: u32) -> io::Result<PathBuf> {
        if depth > Self::MAX_SYMLINK_DEPTH {
            return Err(io::Error::other("too many levels of symbolic links"));
        }

        let path_normalized = normalize_mock_path(path);

        if !self.exists(&path_normalized) {
            return Err(io::Error::new(
                io::ErrorKind::NotFound,
                format!("path not found: {}", path.display()),
            ));
        }

        // Follow symlinks if present
        if let Some(target) = self.resolve_symlink(&path_normalized) {
            self.canonicalize_with_depth(&target, depth + 1)
        } else {
            Ok(path_normalized)
        }
    }
}

/// Normalize a path for mock file system storage.
/// Converts backslashes to forward slashes for cross-platform consistency.
fn normalize_mock_path(path: &Path) -> PathBuf {
    let path_str = path.to_string_lossy();
    PathBuf::from(path_str.replace('\\', "/"))
}

impl FileSystem for MockFileSystem {
    fn exists(&self, path: &Path) -> bool {
        self.get_entry(path).is_some()
    }

    fn is_file(&self, path: &Path) -> bool {
        matches!(self.get_entry(path), Some(MockEntry::File { .. }))
    }

    fn is_dir(&self, path: &Path) -> bool {
        matches!(self.get_entry(path), Some(MockEntry::Directory))
    }

    fn is_symlink(&self, path: &Path) -> bool {
        matches!(self.get_entry(path), Some(MockEntry::Symlink { .. }))
    }

    fn metadata(&self, path: &Path) -> io::Result<FileMetadata> {
        self.metadata_with_depth(path, 0)
    }

    fn symlink_metadata(&self, path: &Path) -> io::Result<FileMetadata> {
        // Don't follow symlinks
        let entry = self.get_entry(path).ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::NotFound,
                format!("path not found: {}", path.display()),
            )
        })?;

        match entry {
            MockEntry::File { content } => Ok(FileMetadata::file(content.len() as u64)),
            MockEntry::Directory => Ok(FileMetadata::directory()),
            MockEntry::Symlink { .. } => Ok(FileMetadata::symlink()),
        }
    }

    fn read_to_string(&self, path: &Path) -> LintResult<String> {
        let path_normalized = normalize_mock_path(path);
        let entries = self.entries.read().expect("MockFileSystem lock poisoned");

        let entry = entries.get(&path_normalized).ok_or_else(|| {
            CoreError::File(FileError::Read {
                path: path.to_path_buf(),
                source: io::Error::new(io::ErrorKind::NotFound, "file not found"),
            })
        })?;

        match entry {
            MockEntry::File { content } => Ok(content.clone()),
            MockEntry::Directory => Err(CoreError::File(FileError::NotRegular {
                path: path.to_path_buf(),
            })),
            MockEntry::Symlink { target } => {
                // Follow symlink to target (mirrors real fs::metadata behavior)
                let target_entry = entries.get(target).ok_or_else(|| {
                    CoreError::File(FileError::Read {
                        path: path.to_path_buf(),
                        source: io::Error::new(io::ErrorKind::NotFound, "symlink target not found"),
                    })
                })?;
                match target_entry {
                    MockEntry::File { content } => Ok(content.clone()),
                    _ => Err(CoreError::File(FileError::NotRegular {
                        path: path.to_path_buf(),
                    })),
                }
            }
        }
    }

    fn write(&self, path: &Path, content: &str) -> LintResult<()> {
        let path_normalized = normalize_mock_path(path);
        let mut entries = self.entries.write().expect("MockFileSystem lock poisoned");

        // Check if path exists and is valid for writing
        match entries.get(&path_normalized) {
            Some(MockEntry::File { .. }) => {
                // Overwrite existing file
                entries.insert(
                    path_normalized,
                    MockEntry::File {
                        content: content.to_string(),
                    },
                );
                Ok(())
            }
            Some(MockEntry::Directory) => Err(CoreError::File(FileError::NotRegular {
                path: path.to_path_buf(),
            })),
            Some(MockEntry::Symlink { .. }) => Err(CoreError::File(FileError::Symlink {
                path: path.to_path_buf(),
            })),
            None => {
                // File doesn't exist - error like safe_write_file
                Err(CoreError::File(FileError::Write {
                    path: path.to_path_buf(),
                    source: io::Error::new(io::ErrorKind::NotFound, "file not found"),
                }))
            }
        }
    }

    fn canonicalize(&self, path: &Path) -> io::Result<PathBuf> {
        self.canonicalize_with_depth(path, 0)
    }

    fn read_dir(&self, path: &Path) -> io::Result<Vec<DirEntry>> {
        let path_normalized = normalize_mock_path(path);

        // Check if it's a directory
        match self.get_entry(&path_normalized) {
            Some(MockEntry::Directory) => {}
            Some(_) => {
                return Err(io::Error::new(
                    io::ErrorKind::NotADirectory,
                    "not a directory",
                ));
            }
            None => {
                return Err(io::Error::new(
                    io::ErrorKind::NotFound,
                    "directory not found",
                ));
            }
        }

        let entries = self.entries.read().expect("MockFileSystem lock poisoned");
        let mut result = Vec::new();

        // Normalize the path string for prefix matching
        let prefix = if path_normalized.to_string_lossy().ends_with('/') {
            path_normalized.to_string_lossy().to_string()
        } else {
            format!("{}/", path_normalized.display())
        };

        for (entry_path, entry) in entries.iter() {
            let entry_str = entry_path.to_string_lossy();

            // Check if this entry is a direct child of the directory
            if let Some(rest) = entry_str.strip_prefix(&prefix) {
                // Only include direct children (no further slashes)
                if !rest.contains('/') && !rest.is_empty() {
                    let metadata = match entry {
                        MockEntry::File { content } => FileMetadata::file(content.len() as u64),
                        MockEntry::Directory => FileMetadata::directory(),
                        MockEntry::Symlink { .. } => FileMetadata::symlink(),
                    };
                    result.push(DirEntry {
                        path: entry_path.clone(),
                        metadata,
                    });
                }
            }
        }

        Ok(result)
    }
}

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

    // ===== RealFileSystem tests =====

    #[test]
    fn test_real_fs_exists() {
        let fs = RealFileSystem;
        // Cargo.toml should exist in the project root
        assert!(fs.exists(Path::new("Cargo.toml")));
        assert!(!fs.exists(Path::new("nonexistent_file_xyz.txt")));
    }

    #[test]
    fn test_real_fs_is_file() {
        let fs = RealFileSystem;
        assert!(fs.is_file(Path::new("Cargo.toml")));
        assert!(!fs.is_file(Path::new("src")));
    }

    #[test]
    fn test_real_fs_is_dir() {
        let fs = RealFileSystem;
        assert!(fs.is_dir(Path::new("src")));
        assert!(!fs.is_dir(Path::new("Cargo.toml")));
    }

    #[test]
    fn test_real_fs_read_to_string() {
        let fs = RealFileSystem;
        let content = fs.read_to_string(Path::new("Cargo.toml"));
        assert!(content.is_ok());
        assert!(content.unwrap().contains("[package]"));
    }

    #[test]
    fn test_real_fs_read_nonexistent() {
        let fs = RealFileSystem;
        let result = fs.read_to_string(Path::new("nonexistent_file_xyz.txt"));
        assert!(result.is_err());
    }

    // ===== MockFileSystem tests =====

    #[test]
    fn test_mock_fs_add_and_exists() {
        let fs = MockFileSystem::new();
        assert!(!fs.exists(Path::new("/test/file.txt")));

        fs.add_file("/test/file.txt", "content");
        assert!(fs.exists(Path::new("/test/file.txt")));
    }

    #[test]
    fn test_mock_fs_is_file() {
        let fs = MockFileSystem::new();
        fs.add_file("/test/file.txt", "content");
        fs.add_dir("/test/dir");

        assert!(fs.is_file(Path::new("/test/file.txt")));
        assert!(!fs.is_file(Path::new("/test/dir")));
    }

    #[test]
    fn test_mock_fs_is_dir() {
        let fs = MockFileSystem::new();
        fs.add_file("/test/file.txt", "content");
        fs.add_dir("/test/dir");

        assert!(!fs.is_dir(Path::new("/test/file.txt")));
        assert!(fs.is_dir(Path::new("/test/dir")));
    }

    #[test]
    fn test_mock_fs_is_symlink() {
        let fs = MockFileSystem::new();
        fs.add_file("/test/file.txt", "content");
        fs.add_symlink("/test/link.txt", "/test/file.txt");

        assert!(!fs.is_symlink(Path::new("/test/file.txt")));
        assert!(fs.is_symlink(Path::new("/test/link.txt")));
    }

    #[test]
    fn test_mock_fs_read_to_string() {
        let fs = MockFileSystem::new();
        fs.add_file("/test/file.txt", "hello world");

        let content = fs.read_to_string(Path::new("/test/file.txt"));
        assert!(content.is_ok());
        assert_eq!(content.unwrap(), "hello world");
    }

    #[test]
    fn test_mock_fs_read_nonexistent() {
        let fs = MockFileSystem::new();
        let result = fs.read_to_string(Path::new("/test/file.txt"));
        assert!(result.is_err());
    }

    #[test]
    fn test_mock_fs_read_directory_fails() {
        let fs = MockFileSystem::new();
        fs.add_dir("/test/dir");

        let result = fs.read_to_string(Path::new("/test/dir"));
        assert!(matches!(
            result,
            Err(CoreError::File(FileError::NotRegular { .. }))
        ));
    }

    #[test]
    fn test_mock_fs_read_symlink_follows_target() {
        let fs = MockFileSystem::new();
        fs.add_file("/test/file.txt", "content");
        fs.add_symlink("/test/link.txt", "/test/file.txt");

        let result = fs.read_to_string(Path::new("/test/link.txt"));
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "content");
    }

    #[test]
    fn test_mock_fs_write() {
        let fs = MockFileSystem::new();
        fs.add_file("/test/file.txt", "original");

        let result = fs.write(Path::new("/test/file.txt"), "updated");
        assert!(result.is_ok());

        let content = fs.read_to_string(Path::new("/test/file.txt")).unwrap();
        assert_eq!(content, "updated");
    }

    #[test]
    fn test_mock_fs_write_nonexistent_fails() {
        let fs = MockFileSystem::new();

        let result = fs.write(Path::new("/test/file.txt"), "content");
        assert!(matches!(
            result,
            Err(CoreError::File(FileError::Write { .. }))
        ));
    }

    #[test]
    fn test_mock_fs_metadata_file() {
        let fs = MockFileSystem::new();
        fs.add_file("/test/file.txt", "12345");

        let meta = fs.metadata(Path::new("/test/file.txt")).unwrap();
        assert!(meta.is_file);
        assert!(!meta.is_dir);
        assert!(!meta.is_symlink);
        assert_eq!(meta.len, 5);
    }

    #[test]
    fn test_mock_fs_metadata_directory() {
        let fs = MockFileSystem::new();
        fs.add_dir("/test/dir");

        let meta = fs.metadata(Path::new("/test/dir")).unwrap();
        assert!(!meta.is_file);
        assert!(meta.is_dir);
        assert!(!meta.is_symlink);
    }

    #[test]
    fn test_mock_fs_symlink_metadata() {
        let fs = MockFileSystem::new();
        fs.add_file("/test/file.txt", "content");
        fs.add_symlink("/test/link.txt", "/test/file.txt");

        // symlink_metadata should not follow symlinks
        let meta = fs.symlink_metadata(Path::new("/test/link.txt")).unwrap();
        assert!(meta.is_symlink);

        // metadata should follow symlinks
        let meta = fs.metadata(Path::new("/test/link.txt")).unwrap();
        assert!(meta.is_file);
        assert!(!meta.is_symlink);
    }

    #[test]
    fn test_mock_fs_read_dir() {
        let fs = MockFileSystem::new();
        fs.add_dir("/test");
        fs.add_file("/test/file1.txt", "content1");
        fs.add_file("/test/file2.txt", "content2");
        fs.add_dir("/test/subdir");

        let entries = fs.read_dir(Path::new("/test")).unwrap();
        assert_eq!(entries.len(), 3);

        let names: Vec<_> = entries
            .iter()
            .map(|e| e.path.file_name().unwrap().to_string_lossy().to_string())
            .collect();
        assert!(names.contains(&"file1.txt".to_string()));
        assert!(names.contains(&"file2.txt".to_string()));
        assert!(names.contains(&"subdir".to_string()));
    }

    #[test]
    fn test_mock_fs_read_dir_nonexistent() {
        let fs = MockFileSystem::new();
        let result = fs.read_dir(Path::new("/nonexistent"));
        assert!(result.is_err());
    }

    #[test]
    fn test_mock_fs_read_dir_not_directory() {
        let fs = MockFileSystem::new();
        fs.add_file("/test/file.txt", "content");

        let result = fs.read_dir(Path::new("/test/file.txt"));
        assert!(result.is_err());
    }

    #[test]
    fn test_mock_fs_canonicalize() {
        let fs = MockFileSystem::new();
        fs.add_file("/test/file.txt", "content");

        let canonical = fs.canonicalize(Path::new("/test/file.txt")).unwrap();
        assert_eq!(canonical, PathBuf::from("/test/file.txt"));
    }

    #[test]
    fn test_mock_fs_canonicalize_follows_symlink() {
        let fs = MockFileSystem::new();
        fs.add_file("/test/file.txt", "content");
        fs.add_symlink("/test/link.txt", "/test/file.txt");

        let canonical = fs.canonicalize(Path::new("/test/link.txt")).unwrap();
        assert_eq!(canonical, PathBuf::from("/test/file.txt"));
    }

    #[test]
    fn test_mock_fs_clear() {
        let fs = MockFileSystem::new();
        fs.add_file("/test/file.txt", "content");
        assert!(fs.exists(Path::new("/test/file.txt")));

        fs.clear();
        assert!(!fs.exists(Path::new("/test/file.txt")));
    }

    #[test]
    fn test_mock_fs_remove() {
        let fs = MockFileSystem::new();
        fs.add_file("/test/file.txt", "content");
        assert!(fs.exists(Path::new("/test/file.txt")));

        fs.remove("/test/file.txt");
        assert!(!fs.exists(Path::new("/test/file.txt")));
    }

    #[test]
    fn test_mock_fs_windows_path_normalization() {
        let fs = MockFileSystem::new();
        fs.add_file("C:/test/file.txt", "content");

        // Should work with either path separator
        assert!(fs.exists(Path::new("C:/test/file.txt")));
        assert!(fs.exists(Path::new("C:\\test\\file.txt")));
    }

    #[test]
    fn test_mock_fs_thread_safety() {
        use std::sync::Arc;
        use std::thread;

        let fs = Arc::new(MockFileSystem::new());
        let mut handles = vec![];

        // Spawn multiple threads that read and write
        for i in 0..10 {
            let fs_clone = Arc::clone(&fs);
            let handle = thread::spawn(move || {
                let path = format!("/test/file{}.txt", i);
                fs_clone.add_file(&path, format!("content{}", i));
                assert!(fs_clone.exists(Path::new(&path)));
                let content = fs_clone.read_to_string(Path::new(&path)).unwrap();
                assert_eq!(content, format!("content{}", i));
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().unwrap();
        }

        // Verify all files exist
        for i in 0..10 {
            let path = format!("/test/file{}.txt", i);
            assert!(fs.exists(Path::new(&path)));
        }
    }

    #[test]
    fn test_mock_fs_circular_symlink_metadata() {
        let fs = MockFileSystem::new();
        // Create circular symlinks: a -> b -> a
        fs.add_symlink("/test/a", "/test/b");
        fs.add_symlink("/test/b", "/test/a");

        // metadata() follows symlinks and should detect the cycle
        let result = fs.metadata(Path::new("/test/a"));
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("too many levels of symbolic links")
        );
    }

    #[test]
    fn test_mock_fs_circular_symlink_canonicalize() {
        let fs = MockFileSystem::new();
        // Create circular symlinks: a -> b -> a
        fs.add_symlink("/test/a", "/test/b");
        fs.add_symlink("/test/b", "/test/a");

        // canonicalize() follows symlinks and should detect the cycle
        let result = fs.canonicalize(Path::new("/test/a"));
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("too many levels of symbolic links")
        );
    }

    #[test]
    fn test_mock_fs_chained_symlinks() {
        let fs = MockFileSystem::new();
        // Create chain: link1 -> link2 -> link3 -> file
        fs.add_file("/test/file.txt", "content");
        fs.add_symlink("/test/link3", "/test/file.txt");
        fs.add_symlink("/test/link2", "/test/link3");
        fs.add_symlink("/test/link1", "/test/link2");

        // metadata() should follow the chain and return file metadata
        let meta = fs.metadata(Path::new("/test/link1")).unwrap();
        assert!(meta.is_file);
        assert_eq!(meta.len, 7); // "content".len()

        // canonicalize() should return the final target
        let canonical = fs.canonicalize(Path::new("/test/link1")).unwrap();
        assert_eq!(canonical, PathBuf::from("/test/file.txt"));
    }

    #[test]
    fn test_mock_fs_max_symlink_depth_boundary() {
        // Test that we can handle chains up to MAX_SYMLINK_DEPTH
        let fs = MockFileSystem::new();
        fs.add_file("/test/target.txt", "content");

        // Create a chain of exactly MAX_SYMLINK_DEPTH links
        let mut prev = PathBuf::from("/test/target.txt");
        for i in 0..MockFileSystem::MAX_SYMLINK_DEPTH {
            let link = PathBuf::from(format!("/test/link{}", i));
            fs.add_symlink(&link, &prev);
            prev = link;
        }

        // Should succeed at the boundary
        let result = fs.metadata(&prev);
        assert!(result.is_ok(), "Should handle MAX_SYMLINK_DEPTH links");
    }

    #[test]
    fn test_mock_fs_exceeds_max_symlink_depth() {
        // Test that MAX_SYMLINK_DEPTH + 1 links fails
        let fs = MockFileSystem::new();
        fs.add_file("/test/target.txt", "content");

        // Create a chain of MAX_SYMLINK_DEPTH + 1 links
        let mut prev = PathBuf::from("/test/target.txt");
        for i in 0..=MockFileSystem::MAX_SYMLINK_DEPTH {
            let link = PathBuf::from(format!("/test/link{}", i));
            fs.add_symlink(&link, &prev);
            prev = link;
        }

        // Should fail beyond the limit
        let result = fs.metadata(&prev);
        assert!(
            result.is_err(),
            "Should fail when exceeding MAX_SYMLINK_DEPTH"
        );
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("too many levels of symbolic links")
        );
    }

    // ===== Unix-specific symlink tests for RealFileSystem =====

    #[cfg(unix)]
    mod unix_tests {
        use super::*;
        use std::os::unix::fs::symlink;
        use tempfile::TempDir;

        #[test]
        fn test_real_fs_follows_symlink_read() {
            let temp = TempDir::new().unwrap();
            let target = temp.path().join("target.txt");
            let link = temp.path().join("link.txt");

            std::fs::write(&target, "content").unwrap();
            symlink(&target, &link).unwrap();

            let fs = RealFileSystem;
            let result = fs.read_to_string(&link);

            assert!(result.is_ok());
            assert_eq!(result.unwrap(), "content");
        }

        #[test]
        fn test_real_fs_symlink_metadata() {
            let temp = TempDir::new().unwrap();
            let target = temp.path().join("target.txt");
            let link = temp.path().join("link.txt");

            std::fs::write(&target, "content").unwrap();
            symlink(&target, &link).unwrap();

            let fs = RealFileSystem;

            // symlink_metadata should show symlink
            let meta = fs.symlink_metadata(&link).unwrap();
            assert!(meta.is_symlink);

            // metadata follows symlink and shows file
            let meta = fs.metadata(&link).unwrap();
            assert!(meta.is_file);
            assert!(!meta.is_symlink);
        }

        #[test]
        fn test_real_fs_dangling_symlink() {
            let temp = TempDir::new().unwrap();
            let link = temp.path().join("dangling.txt");

            symlink("/nonexistent/target", &link).unwrap();

            let fs = RealFileSystem;
            let result = fs.read_to_string(&link);

            // Dangling symlinks produce a read error (target not found)
            assert!(result.is_err());
            assert!(matches!(
                result.unwrap_err(),
                CoreError::File(FileError::Read { .. })
            ));
        }

        #[test]
        fn test_real_fs_is_symlink() {
            let temp = TempDir::new().unwrap();
            let target = temp.path().join("target.txt");
            let link = temp.path().join("link.txt");

            std::fs::write(&target, "content").unwrap();
            symlink(&target, &link).unwrap();

            let fs = RealFileSystem;

            assert!(!fs.is_symlink(&target));
            assert!(fs.is_symlink(&link));
        }

        #[test]
        fn test_real_fs_read_dir_skips_symlinks_in_metadata() {
            let temp = TempDir::new().unwrap();

            // Create a regular file
            std::fs::write(temp.path().join("file.txt"), "content").unwrap();

            // Create a symlink
            symlink(temp.path().join("file.txt"), temp.path().join("link.txt")).unwrap();

            let fs = RealFileSystem;
            let entries = fs.read_dir(temp.path()).unwrap();

            // Both should be returned
            assert_eq!(entries.len(), 2);

            // But the symlink should have is_symlink = true in metadata
            let symlink_entry = entries
                .iter()
                .find(|e| e.path.file_name().unwrap().to_str().unwrap() == "link.txt");
            assert!(symlink_entry.is_some());
            assert!(symlink_entry.unwrap().metadata.is_symlink);

            // And the file should have is_file = true
            let file_entry = entries
                .iter()
                .find(|e| e.path.file_name().unwrap().to_str().unwrap() == "file.txt");
            assert!(file_entry.is_some());
            assert!(file_entry.unwrap().metadata.is_file);
        }
    }
}