File size: 2,937 Bytes
c981f27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use anyhow::Result;
use forge_app::{WalkedFile, Walker};

pub struct ForgeWalkerService;

impl ForgeWalkerService {
    pub fn new() -> Self {
        Self
    }

    pub async fn walk(&self, config: Walker) -> Result<Vec<WalkedFile>> {
        // Convert domain config to forge_walker config
        let mut walker = if config.max_depth.is_none()
            && config.max_breadth.is_none()
            && config.max_file_size.is_none()
            && config.max_files.is_none()
            && config.max_total_size.is_none()
        {
            // Agent-facing walker: keep hidden files excluded by default.
            forge_walker::Walker::max_all().hidden(true)
        } else {
            forge_walker::Walker::min_all()
        };

        walker = walker.cwd(config.cwd);

        if let Some(depth) = config.max_depth {
            walker = walker.max_depth(depth);
        }
        if let Some(breadth) = config.max_breadth {
            walker = walker.max_breadth(breadth);
        }
        if let Some(file_size) = config.max_file_size {
            walker = walker.max_file_size(file_size);
        }
        if let Some(files) = config.max_files {
            walker = walker.max_files(files);
        }
        if let Some(total_size) = config.max_total_size {
            walker = walker.max_total_size(total_size);
        }
        walker = walker.skip_binary(config.skip_binary);

        // Execute the walker and convert results
        let files = walker.get().await?;
        let walked_files = files
            .into_iter()
            .map(|f| WalkedFile { path: f.path, file_name: f.file_name, size: f.size })
            .collect();

        Ok(walked_files)
    }
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;
    use tempfile::tempdir;

    use super::*;

    #[tokio::test]
    async fn test_walker_service_basic_functionality() {
        let fixture = tempdir().unwrap();
        std::fs::write(fixture.path().join("test.txt"), "test content").unwrap();

        let service = ForgeWalkerService::new();
        let config = Walker::conservative().cwd(fixture.path().to_path_buf());

        let actual = service.walk(config).await.unwrap();

        let expected = 1; // Should find the test file
        let file_count = actual.iter().filter(|f| !f.is_dir()).count();
        assert_eq!(file_count, expected);
    }

    #[tokio::test]
    async fn test_walker_service_unlimited_config() {
        let fixture = tempdir().unwrap();
        std::fs::write(fixture.path().join("test.txt"), "test content").unwrap();

        let service = ForgeWalkerService::new();
        let config = Walker::unlimited().cwd(fixture.path().to_path_buf());

        let actual = service.walk(config).await.unwrap();

        let expected = 1; // Should find the test file
        let file_count = actual.iter().filter(|f| !f.is_dir()).count();
        assert_eq!(file_count, expected);
    }
}