Files
reqlite/crates/app/tests/watcher.rs
T
Torbjörn MolinandClaude Opus 5.5 d5e7b484fc Multiple collections, tab close-all and rename, tab scroll bar fix
- Keep several collections open at once. Each has its own environments,
  selected environment, cookie jar and file watcher, and they are reopened
  on start. The environment picker acts on the current collection (the
  active tab's, or the one last chosen). Overlapping folders are refused.
- Close all / close others for tabs (Ctrl+Shift+W, tab menu, File menu),
  asking once about tabs with unsaved changes.
- Rename a request from its tab (double-click, tab menu, or the … menu).
- Give the tab strip's scroll bar its own row so it no longer covers the
  tab names.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 10:55:20 +02:00

110 lines
4.1 KiB
Rust

//! The file watcher reports external edits to .http files.
use std::sync::Arc;
use std::time::Duration;
use reqlite_app::action::Action;
use reqlite_app::effects::{Effect, Runner};
use reqlite_core::http::{HttpClient, HttpError, ResolvedRequest, Response};
use reqlite_core::CancelToken;
struct NoClient;
impl HttpClient for NoClient {
async fn send(&self, _r: ResolvedRequest, _c: CancelToken) -> Result<Response, HttpError> {
Err(HttpError::Other("unused".into()))
}
}
#[test]
fn reports_changes_to_http_and_env_files() {
let rt = tokio::runtime::Runtime::new().unwrap();
let dir = tempfile::tempdir().unwrap();
let root = dir.path().canonicalize().unwrap();
std::fs::write(root.join("a.http"), "GET /a\n").unwrap();
let (runner, mut rx) = Runner::new(rt.handle().clone(), None, Arc::new(NoClient), None, 4);
runner.run(Effect::Watch(root.clone()));
std::thread::sleep(Duration::from_millis(200));
std::fs::write(root.join("a.http"), "GET /changed\n").unwrap();
std::fs::write(root.join("ignored.txt"), "x").unwrap();
std::fs::create_dir(root.join("sub")).unwrap();
std::fs::write(root.join("sub/new.http"), "GET /new\n").unwrap();
std::fs::write(root.join("http-client.env.json"), "{}").unwrap();
let mut seen = std::collections::HashMap::new();
let mut rescan = false;
rt.block_on(async {
let deadline = tokio::time::Instant::now() + Duration::from_secs(5);
while seen.len() < 2 || !rescan {
match tokio::time::timeout_at(deadline, rx.recv()).await {
Ok(Some(Action::FileChangedOnDisk { path, text })) => {
seen.insert(path, text);
}
Ok(Some(Action::Rescan(r))) if r == root => rescan = true,
Ok(Some(_)) => {}
_ => break,
}
}
});
assert_eq!(
seen.get(&root.join("a.http")),
Some(&Some("GET /changed\n".to_string()))
);
// A new folder triggers a rescan (its files may race the new watch).
assert!(rescan);
assert!(seen.contains_key(&root.join("http-client.env.json")));
assert!(!seen.contains_key(&root.join("ignored.txt")));
std::fs::remove_file(root.join("a.http")).unwrap();
let deleted = rt.block_on(async {
loop {
match tokio::time::timeout(Duration::from_secs(5), rx.recv()).await {
Ok(Some(Action::FileChangedOnDisk { path, text })) if path == root.join("a.http") => break text,
Ok(Some(_)) => {}
_ => panic!("no delete event"),
}
}
});
assert_eq!(deleted, None);
// Stopping the watch stops the events.
runner.run(Effect::Unwatch(root.clone()));
std::thread::sleep(Duration::from_millis(100));
while rx.try_recv().is_ok() {}
std::fs::write(root.join("b.http"), "GET /b\n").unwrap();
std::thread::sleep(Duration::from_millis(400));
assert!(rx.try_recv().is_err());
}
#[test]
fn watches_several_collections_at_once() {
let rt = tokio::runtime::Runtime::new().unwrap();
let (a, b) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
let (a, b) = (a.path().canonicalize().unwrap(), b.path().canonicalize().unwrap());
let (runner, mut rx) = Runner::new(rt.handle().clone(), None, Arc::new(NoClient), None, 4);
runner.run(Effect::Watch(a.clone()));
runner.run(Effect::Watch(b.clone()));
std::thread::sleep(Duration::from_millis(200));
std::fs::write(a.join("a.http"), "GET /a\n").unwrap();
std::fs::write(b.join("b.http"), "GET /b\n").unwrap();
let mut seen = std::collections::HashSet::new();
rt.block_on(async {
let deadline = tokio::time::Instant::now() + Duration::from_secs(5);
while seen.len() < 2 {
match tokio::time::timeout_at(deadline, rx.recv()).await {
Ok(Some(Action::FileChangedOnDisk { path, .. })) => {
seen.insert(path);
}
Ok(Some(_)) => {}
_ => break,
}
}
});
assert!(
seen.contains(&a.join("a.http")) && seen.contains(&b.join("b.http")),
"{seen:?}"
);
}