mod common;
use cooklang_sync_client::file_watcher::async_watcher;
use futures::StreamExt;
use notify::RecursiveMode;
use std::time::Duration;
use tempfile::TempDir;
use tokio::time::timeout;
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn async_watcher_reports_file_creation() {
let dir = TempDir::new().unwrap();
let dir_real = dir.path().canonicalize().unwrap();
let (mut debouncer, mut rx) = async_watcher().expect("build debouncer");
debouncer
.watcher()
.watch(&dir_real, RecursiveMode::Recursive)
.expect("watch tempdir");
let path = dir_real.join("hello.cook");
tokio::spawn({
let path = path.clone();
async move {
tokio::time::sleep(Duration::from_millis(100)).await;
tokio::fs::write(&path, b"content").await.expect("write");
}
});
let result = timeout(Duration::from_secs(6), rx.next())
.await
.expect("watcher event should arrive before timeout");
let events = result
.expect("channel should deliver at least one event")
.expect("watcher result should not be Err");
assert!(
events.iter().any(|e| e.path == path || path.starts_with(&e.path)),
"event list should include the created file or its parent dir; got: {:?}",
events
);
}