story-kit: start 269_story_file_references_in_web_ui_chat_input
This commit is contained in:
@@ -727,6 +727,42 @@ pub async fn create_directory_absolute(path: String) -> Result<bool, String> {
|
||||
.map_err(|e| format!("Task failed: {}", e))?
|
||||
}
|
||||
|
||||
/// List all files in the project recursively, respecting .gitignore.
|
||||
/// Returns relative paths from the project root (files only, not directories).
|
||||
pub async fn list_project_files(state: &SessionState) -> Result<Vec<String>, String> {
|
||||
let root = state.get_project_root()?;
|
||||
list_project_files_impl(root).await
|
||||
}
|
||||
|
||||
pub async fn list_project_files_impl(root: PathBuf) -> Result<Vec<String>, String> {
|
||||
use ignore::WalkBuilder;
|
||||
|
||||
let root_clone = root.clone();
|
||||
let files = tokio::task::spawn_blocking(move || {
|
||||
let mut result = Vec::new();
|
||||
let walker = WalkBuilder::new(&root_clone).git_ignore(true).build();
|
||||
|
||||
for entry in walker.flatten() {
|
||||
if entry.file_type().map(|ft| ft.is_file()).unwrap_or(false) {
|
||||
let relative = entry
|
||||
.path()
|
||||
.strip_prefix(&root_clone)
|
||||
.unwrap_or(entry.path())
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
result.push(relative);
|
||||
}
|
||||
}
|
||||
|
||||
result.sort();
|
||||
result
|
||||
})
|
||||
.await
|
||||
.map_err(|e| format!("Task failed: {e}"))?;
|
||||
|
||||
Ok(files)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -1588,4 +1624,68 @@ mod tests {
|
||||
"scaffold should not overwrite existing project.toml"
|
||||
);
|
||||
}
|
||||
|
||||
// --- list_project_files_impl ---
|
||||
|
||||
#[tokio::test]
|
||||
async fn list_project_files_returns_all_files() {
|
||||
let dir = tempdir().unwrap();
|
||||
fs::create_dir(dir.path().join("src")).unwrap();
|
||||
fs::write(dir.path().join("src/main.rs"), "fn main() {}").unwrap();
|
||||
fs::write(dir.path().join("README.md"), "# readme").unwrap();
|
||||
|
||||
let files = list_project_files_impl(dir.path().to_path_buf())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(files.contains(&"README.md".to_string()));
|
||||
assert!(files.contains(&"src/main.rs".to_string()));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn list_project_files_excludes_dirs_from_output() {
|
||||
let dir = tempdir().unwrap();
|
||||
fs::create_dir(dir.path().join("subdir")).unwrap();
|
||||
fs::write(dir.path().join("file.txt"), "").unwrap();
|
||||
|
||||
let files = list_project_files_impl(dir.path().to_path_buf())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(files.contains(&"file.txt".to_string()));
|
||||
assert!(!files.iter().any(|f| f == "subdir"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn list_project_files_returns_sorted() {
|
||||
let dir = tempdir().unwrap();
|
||||
fs::write(dir.path().join("z.txt"), "").unwrap();
|
||||
fs::write(dir.path().join("a.txt"), "").unwrap();
|
||||
|
||||
let files = list_project_files_impl(dir.path().to_path_buf())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let a_idx = files.iter().position(|f| f == "a.txt").unwrap();
|
||||
let z_idx = files.iter().position(|f| f == "z.txt").unwrap();
|
||||
assert!(a_idx < z_idx);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn list_project_files_with_state() {
|
||||
let dir = tempdir().unwrap();
|
||||
fs::write(dir.path().join("hello.rs"), "").unwrap();
|
||||
let state = make_state_with_root(dir.path().to_path_buf());
|
||||
|
||||
let files = list_project_files(&state).await.unwrap();
|
||||
|
||||
assert!(files.contains(&"hello.rs".to_string()));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn list_project_files_errors_without_project() {
|
||||
let state = SessionState::default();
|
||||
let result = list_project_files(&state).await;
|
||||
assert!(result.is_err());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user