Auto reload changed files

This commit is contained in:
2026-09-11 08:43:33 +02:00
parent 2bab19a1ae
commit d574680321
6 changed files with 353 additions and 3 deletions
+61
View File
@@ -91,3 +91,64 @@ fn close_and_fit_sizes_the_card_and_closes_the_editor() {
);
assert_eq!(app.doc.undo_label(), Some("Fit to text"));
}
/// A file node follows its file when something else writes it.
#[test]
fn a_card_follows_a_file_changed_outside_the_editor() -> Result<(), Box<dyn std::error::Error>> {
let dir =
std::env::temp_dir().join(format!("jsoncanvas-outside-{}", crate::model::random_id()));
std::fs::create_dir_all(&dir)?;
let path = dir.join("note.md");
std::fs::write(&path, "before the change\n")?;
let mut canvas = Canvas::default();
canvas.nodes.push(Node::new(
"f",
NodeKind::File {
file: path.display().to_string(),
subpath: None,
},
0,
0,
400,
300,
));
let mut harness = Harness::builder()
.with_size(egui::vec2(1200.0, 900.0))
.build_eframe(|cc| App::new(cc, None));
harness.state_mut().doc = Document::new();
harness.state_mut().doc.canvas = canvas;
harness.run();
assert!(harness.query_by_label("before the change").is_some());
// Another program writes the file while the canvas is open.
std::fs::write(&path, "after the change\n")?;
// The check is on a timer; this is the same as waiting for it to come up.
harness.state_mut().last_file_check = f64::MIN;
harness.run();
assert!(harness.query_by_label("after the change").is_some());
assert!(
harness.query_by_label("before the change").is_none(),
"the card kept showing the text the file no longer holds"
);
// With an edit made here the card keeps it, and says the file moved on.
harness
.state_mut()
.view
.files
.set(&path, "edited here\n".to_owned());
std::fs::write(&path, "changed again, elsewhere\n")?;
harness.state_mut().last_file_check = f64::MIN;
harness.run();
assert!(harness.query_by_label("edited here").is_some());
assert!(
harness.query_by_label("\u{26a0}").is_some(),
"the card said nothing about the file changing under the edit"
);
std::fs::remove_dir_all(&dir)?;
Ok(())
}