1use crate::hit_test::{
2 find_link_at, find_link_route_at, find_pin_at, links_in_selection_box, nodes_in_selection_box,
3 BezierLinkRoute, NodeGeometry, SimpleLinkGeometry, SimpleNodeGeometry, SimplePinGeometry,
4};
5use crate::path::{generate_bezier_path, CubicBezier};
6use std::collections::{HashMap, HashSet};
7
8#[derive(Clone, Copy, Debug)]
9pub struct StoredPin {
10 pub node_id: i32,
11 pub pin_type: i32,
12 pub rel_x: f32,
13 pub rel_y: f32,
14}
15
16pub struct GeometryCache<N = SimpleNodeGeometry> {
20 pub node_rects: HashMap<i32, N>,
21 pub pin_positions: HashMap<i32, StoredPin>,
22 non_hit_testable_pins: HashSet<i32>,
23 hidden_nodes: HashSet<i32>,
27 pub group_rects: HashMap<i32, SimpleNodeGeometry>,
30}
31
32impl<N> Default for GeometryCache<N> {
33 fn default() -> Self {
34 Self {
35 node_rects: HashMap::new(),
36 pin_positions: HashMap::new(),
37 non_hit_testable_pins: HashSet::new(),
38 hidden_nodes: HashSet::new(),
39 group_rects: HashMap::new(),
40 }
41 }
42}
43
44impl<N> GeometryCache<N> {
45 pub fn new() -> Self {
46 Self::default()
47 }
48
49 pub fn remove_pin(&mut self, pin_id: i32) -> bool {
51 self.non_hit_testable_pins.remove(&pin_id);
52 self.pin_positions.remove(&pin_id).is_some()
53 }
54
55 pub fn remove_node(&mut self, node_id: i32) -> Vec<i32> {
60 self.node_rects.remove(&node_id);
61 self.hidden_nodes.remove(&node_id);
62 let mut removed_pins: Vec<_> = self
63 .pin_positions
64 .iter()
65 .filter_map(|(&pin_id, pin)| (pin.node_id == node_id).then_some(pin_id))
66 .collect();
67 removed_pins.sort_unstable();
68 for pin_id in &removed_pins {
69 self.remove_pin(*pin_id);
70 }
71 removed_pins
72 }
73
74 pub fn clear(&mut self) {
76 self.node_rects.clear();
77 self.pin_positions.clear();
78 self.non_hit_testable_pins.clear();
79 self.hidden_nodes.clear();
80 self.group_rects.clear();
81 }
82
83 pub fn set_node_hidden(&mut self, node_id: i32, hidden: bool) {
87 if hidden {
88 self.hidden_nodes.insert(node_id);
89 } else {
90 self.hidden_nodes.remove(&node_id);
91 }
92 }
93
94 pub fn is_node_hidden(&self, node_id: i32) -> bool {
96 self.hidden_nodes.contains(&node_id)
97 }
98
99 fn touches_hidden(&self, a: i32, b: i32) -> bool {
102 !self.hidden_nodes.is_empty()
103 && (self.hidden_nodes.contains(&a) || self.hidden_nodes.contains(&b))
104 }
105
106 pub fn update_group_rect(&mut self, id: i32, x: f32, y: f32, width: f32, height: f32) {
108 self.group_rects.insert(
109 id,
110 SimpleNodeGeometry {
111 id,
112 x,
113 y,
114 width,
115 height,
116 },
117 );
118 }
119
120 pub fn remove_group(&mut self, group_id: i32) -> bool {
122 self.group_rects.remove(&group_id).is_some()
123 }
124
125 pub fn groups_in_selection_box(&self, x: f32, y: f32, width: f32, height: f32) -> Vec<i32> {
128 nodes_in_selection_box(x, y, width, height, self.group_rects.values().copied())
129 }
130}
131
132impl<N> GeometryCache<N>
133where
134 N: NodeGeometry + Copy,
135{
136 pub fn get_absolute_pins(&self) -> impl Iterator<Item = SimplePinGeometry> + '_ {
138 let any_hidden = !self.hidden_nodes.is_empty();
141 self.pin_positions
142 .iter()
143 .filter(|(pin_id, _)| !self.non_hit_testable_pins.contains(pin_id))
144 .filter(move |(_, pin)| !any_hidden || !self.hidden_nodes.contains(&pin.node_id))
145 .filter_map(move |(&pin_id, pin_pos)| {
146 let rect = self.node_rects.get(&pin_pos.node_id)?.rect();
147 Some(SimplePinGeometry {
148 id: pin_id,
149 x: rect.0 + pin_pos.rel_x,
150 y: rect.1 + pin_pos.rel_y,
151 })
152 })
153 }
154
155 pub fn get_absolute_links<'a, I>(
157 &'a self,
158 links: I,
159 ) -> impl Iterator<Item = SimpleLinkGeometry> + 'a
160 where
161 I: Iterator<Item = (i32, i32, i32)> + 'a,
162 {
163 links.filter_map(move |(id, start_pin, end_pin)| {
164 let start_pos = self.pin_positions.get(&start_pin)?;
165 let end_pos = self.pin_positions.get(&end_pin)?;
166 if self.touches_hidden(start_pos.node_id, end_pos.node_id) {
167 return None;
168 }
169
170 let start_rect = self.node_rects.get(&start_pos.node_id)?.rect();
171 let end_rect = self.node_rects.get(&end_pos.node_id)?.rect();
172
173 Some(SimpleLinkGeometry {
174 id,
175 start_x: start_rect.0 + start_pos.rel_x,
176 start_y: start_rect.1 + start_pos.rel_y,
177 end_x: end_rect.0 + end_pos.rel_x,
178 end_y: end_rect.1 + end_pos.rel_y,
179 })
180 })
181 }
182
183 pub fn find_pin_at(&self, x: f32, y: f32, hit_radius: f32) -> i32 {
186 find_pin_at(x, y, self.get_absolute_pins(), hit_radius)
187 }
188
189 #[allow(clippy::too_many_arguments)]
191 pub fn find_link_at<'a, I>(
192 &'a self,
193 x: f32,
194 y: f32,
195 links: I,
196 hover_distance: f32,
197 zoom: f32,
198 bezier_min_offset: f32,
199 hit_samples: usize,
200 ) -> i32
201 where
202 I: Iterator<Item = (i32, i32, i32)> + 'a,
203 {
204 find_link_at(
205 x,
206 y,
207 self.get_absolute_links(links),
208 hover_distance,
209 zoom,
210 bezier_min_offset,
211 hit_samples,
212 )
213 }
214
215 #[allow(clippy::too_many_arguments)]
220 pub fn find_bezier_link_at_world<'a, I>(
221 &'a self,
222 world_x: f32,
223 world_y: f32,
224 links: I,
225 world_hover_distance: f32,
226 bezier_min_offset: f32,
227 hit_samples: usize,
228 ) -> i32
229 where
230 I: Iterator<Item = (i32, i32, i32)> + 'a,
231 {
232 let routes = links.filter_map(|(id, start_pin, end_pin)| {
233 Some(BezierLinkRoute {
234 id,
235 curve: self.link_curve_world(start_pin, end_pin, bezier_min_offset)?,
236 })
237 });
238 find_link_route_at(
239 (world_x, world_y),
240 routes,
241 world_hover_distance,
242 hit_samples,
243 )
244 }
245
246 pub fn nodes_in_selection_box(&self, x: f32, y: f32, width: f32, height: f32) -> Vec<i32> {
248 nodes_in_selection_box(
249 x,
250 y,
251 width,
252 height,
253 self.node_rects
254 .values()
255 .filter(|node| !self.hidden_nodes.contains(&node.id()))
256 .copied(),
257 )
258 }
259
260 pub fn links_in_selection_box<'a, I>(
262 &'a self,
263 x: f32,
264 y: f32,
265 width: f32,
266 height: f32,
267 links: I,
268 ) -> Vec<i32>
269 where
270 I: Iterator<Item = (i32, i32, i32)> + 'a,
271 {
272 links_in_selection_box(x, y, width, height, self.get_absolute_links(links))
273 }
274
275 pub fn resolve_link_endpoints_world(
280 &self,
281 start_pin: i32,
282 end_pin: i32,
283 ) -> Option<(f32, f32, f32, f32)> {
284 let start_pos = self.pin_positions.get(&start_pin)?;
285 let end_pos = self.pin_positions.get(&end_pin)?;
286 if self.touches_hidden(start_pos.node_id, end_pos.node_id) {
287 return None;
288 }
289
290 let start_rect = self.node_rects.get(&start_pos.node_id)?.rect();
291 let end_rect = self.node_rects.get(&end_pos.node_id)?.rect();
292
293 Some((
294 start_rect.0 + start_pos.rel_x,
295 start_rect.1 + start_pos.rel_y,
296 end_rect.0 + end_pos.rel_x,
297 end_rect.1 + end_pos.rel_y,
298 ))
299 }
300
301 pub fn compute_link_path(
303 &self,
304 start_pin: i32,
305 end_pin: i32,
306 zoom: f32,
307 bezier_min_offset: f32,
308 ) -> Option<String> {
309 let (sx, sy, ex, ey) = self.resolve_link_endpoints_world(start_pin, end_pin)?;
310 Some(generate_bezier_path(
311 sx,
312 sy,
313 ex,
314 ey,
315 zoom,
316 bezier_min_offset,
317 ))
318 }
319
320 pub fn compute_link_path_screen(
324 &self,
325 start_pin: i32,
326 end_pin: i32,
327 zoom: f32,
328 pan_x: f32,
329 pan_y: f32,
330 bezier_min_offset: f32,
331 ) -> Option<String> {
332 let (sx, sy, ex, ey) = self.resolve_link_endpoints_world(start_pin, end_pin)?;
333 Some(generate_bezier_path(
334 sx * zoom + pan_x,
335 sy * zoom + pan_y,
336 ex * zoom + pan_x,
337 ey * zoom + pan_y,
338 zoom,
339 bezier_min_offset,
340 ))
341 }
342
343 pub fn link_curve_world(
348 &self,
349 start_pin: i32,
350 end_pin: i32,
351 bezier_min_offset: f32,
352 ) -> Option<CubicBezier> {
353 let (sx, sy, ex, ey) = self.resolve_link_endpoints_world(start_pin, end_pin)?;
354 Some(CubicBezier::from_endpoints(
355 sx,
356 sy,
357 ex,
358 ey,
359 1.0,
360 bezier_min_offset,
361 ))
362 }
363
364 pub fn compute_link_path_world(
366 &self,
367 start_pin: i32,
368 end_pin: i32,
369 bezier_min_offset: f32,
370 ) -> Option<String> {
371 Some(
372 self.link_curve_world(start_pin, end_pin, bezier_min_offset)?
373 .commands_from((0.0, 0.0)),
374 )
375 }
376
377 pub fn handle_pin_report(
379 &mut self,
380 pin_id: i32,
381 node_id: i32,
382 pin_type: i32,
383 rel_x: f32,
384 rel_y: f32,
385 ) {
386 self.handle_pin_report_with_hit_testable(pin_id, node_id, pin_type, rel_x, rel_y, true);
387 }
388
389 pub fn handle_pin_report_with_hit_testable(
395 &mut self,
396 pin_id: i32,
397 node_id: i32,
398 pin_type: i32,
399 rel_x: f32,
400 rel_y: f32,
401 hit_testable: bool,
402 ) {
403 self.pin_positions.insert(
404 pin_id,
405 StoredPin {
406 node_id,
407 pin_type,
408 rel_x,
409 rel_y,
410 },
411 );
412 if hit_testable {
413 self.non_hit_testable_pins.remove(&pin_id);
414 } else {
415 self.non_hit_testable_pins.insert(pin_id);
416 }
417 }
418}
419
420impl GeometryCache<SimpleNodeGeometry> {
422 pub fn update_node_rect(&mut self, id: i32, x: f32, y: f32, width: f32, height: f32) {
424 self.node_rects.insert(
425 id,
426 SimpleNodeGeometry {
427 id,
428 x,
429 y,
430 width,
431 height,
432 },
433 );
434 }
435
436 pub fn handle_node_rect_report(&mut self, id: i32, x: f32, y: f32, w: f32, h: f32) {
438 self.update_node_rect(id, x, y, w, h);
439 }
440}
441
442#[cfg(test)]
443mod tests {
444 use super::*;
445
446 fn setup_test_cache() -> GeometryCache<SimpleNodeGeometry> {
448 let mut cache = GeometryCache::new();
449
450 cache.update_node_rect(1, 0.0, 0.0, 100.0, 50.0);
452 cache.update_node_rect(2, 200.0, 100.0, 100.0, 50.0);
454
455 cache.handle_pin_report(1001, 1, 2, 100.0, 25.0);
457 cache.handle_pin_report(2001, 2, 1, 0.0, 25.0);
459
460 cache
461 }
462
463 #[test]
468 fn test_new_cache_is_empty() {
469 let cache: GeometryCache<SimpleNodeGeometry> = GeometryCache::new();
470 assert!(cache.node_rects.is_empty());
471 assert!(cache.pin_positions.is_empty());
472 }
473
474 #[test]
475 fn test_default_cache_is_empty() {
476 let cache: GeometryCache<SimpleNodeGeometry> = GeometryCache::default();
477 assert!(cache.node_rects.is_empty());
478 assert!(cache.pin_positions.is_empty());
479 }
480
481 #[test]
486 fn test_handle_pin_report_inserts_pin() {
487 let mut cache: GeometryCache<SimpleNodeGeometry> = GeometryCache::new();
488 cache.handle_pin_report(1001, 1, 2, 50.0, 25.0);
489
490 let pin = cache.pin_positions.get(&1001).expect("Pin should exist");
491 assert_eq!(pin.node_id, 1);
492 assert_eq!(pin.pin_type, 2);
493 assert_eq!(pin.rel_x, 50.0);
494 assert_eq!(pin.rel_y, 25.0);
495 }
496
497 #[test]
498 fn test_handle_pin_report_overwrites_existing() {
499 let mut cache: GeometryCache<SimpleNodeGeometry> = GeometryCache::new();
500 cache.handle_pin_report(1001, 1, 2, 50.0, 25.0);
501 cache.handle_pin_report(1001, 1, 2, 100.0, 30.0); let pin = cache.pin_positions.get(&1001).expect("Pin should exist");
504 assert_eq!(pin.rel_x, 100.0);
505 assert_eq!(pin.rel_y, 30.0);
506 }
507
508 #[test]
509 fn test_handle_pin_report_negative_coordinates() {
510 let mut cache: GeometryCache<SimpleNodeGeometry> = GeometryCache::new();
511 cache.handle_pin_report(1001, 1, 2, -10.0, -20.0);
512
513 let pin = cache.pin_positions.get(&1001).expect("Pin should exist");
514 assert_eq!(pin.rel_x, -10.0);
515 assert_eq!(pin.rel_y, -20.0);
516 }
517
518 #[test]
519 fn remove_pin_retires_its_geometry() {
520 let mut cache = setup_test_cache();
521
522 assert!(cache.remove_pin(1001));
523 assert!(!cache.remove_pin(1001));
524 assert!(!cache.pin_positions.contains_key(&1001));
525 }
526
527 #[test]
528 fn remove_node_cascades_to_owned_pins() {
529 let mut cache = setup_test_cache();
530
531 let removed_pins = cache.remove_node(1);
532
533 assert_eq!(removed_pins, vec![1001]);
534 assert!(!cache.node_rects.contains_key(&1));
535 assert!(!cache.pin_positions.contains_key(&1001));
536 assert!(cache.node_rects.contains_key(&2));
537 assert!(cache.pin_positions.contains_key(&2001));
538 }
539
540 #[test]
541 fn clear_removes_all_projected_geometry() {
542 let mut cache = setup_test_cache();
543
544 cache.clear();
545
546 assert!(cache.node_rects.is_empty());
547 assert!(cache.pin_positions.is_empty());
548 }
549
550 #[test]
551 fn hidden_node_keeps_geometry_but_leaves_every_hit_test_and_route() {
552 let mut cache = setup_test_cache();
553 cache.set_node_hidden(1, true);
554
555 assert!(cache.is_node_hidden(1));
556 assert!(cache.node_rects.contains_key(&1), "geometry kept");
557 assert!(cache.pin_positions.contains_key(&1001), "pins kept");
558 assert_eq!(cache.find_pin_at(100.0, 25.0, 10.0), 0, "pin not pickable");
559 assert!(!cache
560 .nodes_in_selection_box(0.0, 0.0, 50.0, 50.0)
561 .contains(&1));
562 assert!(cache.compute_link_path_world(1001, 2001, 50.0).is_none());
563 assert!(cache
564 .links_in_selection_box(90.0, 15.0, 20.0, 20.0, vec![(1, 1001, 2001)].into_iter())
565 .is_empty());
566
567 cache.set_node_hidden(1, false);
568 assert_eq!(cache.find_pin_at(100.0, 25.0, 10.0), 1001);
569 assert!(cache.compute_link_path_world(1001, 2001, 50.0).is_some());
570 }
571
572 #[test]
573 fn removing_or_clearing_a_node_drops_its_hidden_flag() {
574 let mut cache = setup_test_cache();
575 cache.set_node_hidden(1, true);
576 cache.remove_node(1);
577 assert!(!cache.is_node_hidden(1));
578
579 cache.set_node_hidden(2, true);
580 cache.update_group_rect(7, 0.0, 0.0, 10.0, 10.0);
581 cache.clear();
582 assert!(!cache.is_node_hidden(2));
583 assert!(cache.group_rects.is_empty());
584 }
585
586 #[test]
587 fn group_rects_are_marquee_selectable_by_intersection() {
588 let mut cache = setup_test_cache();
589 cache.update_group_rect(7, 500.0, 500.0, 100.0, 100.0);
590
591 assert_eq!(
592 cache.groups_in_selection_box(550.0, 550.0, 10.0, 10.0),
593 vec![7]
594 );
595 assert!(cache
596 .groups_in_selection_box(0.0, 0.0, 10.0, 10.0)
597 .is_empty());
598 assert!(cache.remove_group(7));
599 assert!(!cache.remove_group(7));
600 }
601
602 #[test]
603 fn hidden_pin_routes_but_is_not_hit_testable() {
604 let mut cache = setup_test_cache();
605 cache.handle_pin_report_with_hit_testable(1001, 1, 2, 100.0, 25.0, false);
606
607 assert_eq!(cache.find_pin_at(100.0, 25.0, 10.0), 0);
608 assert!(cache.compute_link_path_world(1001, 2001, 50.0).is_some());
609 }
610
611 #[test]
616 fn test_update_node_rect_inserts_node() {
617 let mut cache = GeometryCache::new();
618 cache.update_node_rect(1, 10.0, 20.0, 100.0, 50.0);
619
620 let node = cache.node_rects.get(&1).expect("Node should exist");
621 assert_eq!(node.id, 1);
622 assert_eq!(node.x, 10.0);
623 assert_eq!(node.y, 20.0);
624 assert_eq!(node.width, 100.0);
625 assert_eq!(node.height, 50.0);
626 }
627
628 #[test]
629 fn test_update_node_rect_overwrites_existing() {
630 let mut cache = GeometryCache::new();
631 cache.update_node_rect(1, 10.0, 20.0, 100.0, 50.0);
632 cache.update_node_rect(1, 50.0, 60.0, 150.0, 80.0);
633
634 let node = cache.node_rects.get(&1).expect("Node should exist");
635 assert_eq!(node.x, 50.0);
636 assert_eq!(node.y, 60.0);
637 assert_eq!(node.width, 150.0);
638 assert_eq!(node.height, 80.0);
639 }
640
641 #[test]
642 fn test_update_node_rect_negative_coordinates() {
643 let mut cache = GeometryCache::new();
644 cache.update_node_rect(1, -100.0, -200.0, 100.0, 50.0);
645
646 let node = cache.node_rects.get(&1).expect("Node should exist");
647 assert_eq!(node.x, -100.0);
648 assert_eq!(node.y, -200.0);
649 }
650
651 #[test]
656 fn test_get_absolute_pins_returns_absolute_positions() {
657 let cache = setup_test_cache();
658 let pins: Vec<SimplePinGeometry> = cache.get_absolute_pins().collect();
659
660 let pin1 = pins
662 .iter()
663 .find(|p| p.id == 1001)
664 .expect("Pin 1001 should exist");
665 assert_eq!(pin1.x, 100.0);
666 assert_eq!(pin1.y, 25.0);
667
668 let pin2 = pins
670 .iter()
671 .find(|p| p.id == 2001)
672 .expect("Pin 2001 should exist");
673 assert_eq!(pin2.x, 200.0);
674 assert_eq!(pin2.y, 125.0);
675 }
676
677 #[test]
678 fn test_get_absolute_pins_skips_orphan_pins() {
679 let mut cache = setup_test_cache();
680 cache.handle_pin_report(9999, 999, 1, 50.0, 25.0);
682
683 let pins: Vec<SimplePinGeometry> = cache.get_absolute_pins().collect();
684
685 assert_eq!(pins.len(), 2);
687 assert!(!pins.iter().any(|p| p.id == 9999));
688 }
689
690 #[test]
691 fn test_get_absolute_pins_empty_cache() {
692 let cache: GeometryCache<SimpleNodeGeometry> = GeometryCache::new();
693 let pins: Vec<SimplePinGeometry> = cache.get_absolute_pins().collect();
694 assert!(pins.is_empty());
695 }
696
697 #[test]
698 fn test_get_absolute_pins_node_at_negative_coords() {
699 let mut cache = GeometryCache::new();
700 cache.update_node_rect(1, -100.0, -50.0, 100.0, 50.0);
701 cache.handle_pin_report(1001, 1, 2, 50.0, 25.0);
702
703 let pins: Vec<SimplePinGeometry> = cache.get_absolute_pins().collect();
704 let pin = pins
705 .iter()
706 .find(|p| p.id == 1001)
707 .expect("Pin should exist");
708 assert_eq!(pin.x, -50.0); assert_eq!(pin.y, -25.0); }
711
712 #[test]
717 fn test_get_absolute_links_returns_absolute_positions() {
718 let cache = setup_test_cache();
719 let links_data = vec![(1, 1001, 2001)]; let links: Vec<SimpleLinkGeometry> =
721 cache.get_absolute_links(links_data.into_iter()).collect();
722
723 assert_eq!(links.len(), 1);
724 let link = &links[0];
725 assert_eq!(link.id, 1);
726 assert_eq!(link.start_x, 100.0);
728 assert_eq!(link.start_y, 25.0);
729 assert_eq!(link.end_x, 200.0);
731 assert_eq!(link.end_y, 125.0);
732 }
733
734 #[test]
735 fn test_get_absolute_links_skips_missing_start_pin() {
736 let cache = setup_test_cache();
737 let links_data = vec![(1, 9999, 2001)]; let links: Vec<SimpleLinkGeometry> =
739 cache.get_absolute_links(links_data.into_iter()).collect();
740
741 assert!(links.is_empty());
742 }
743
744 #[test]
745 fn test_get_absolute_links_skips_missing_end_pin() {
746 let cache = setup_test_cache();
747 let links_data = vec![(1, 1001, 9999)]; let links: Vec<SimpleLinkGeometry> =
749 cache.get_absolute_links(links_data.into_iter()).collect();
750
751 assert!(links.is_empty());
752 }
753
754 #[test]
755 fn test_get_absolute_links_skips_missing_start_node() {
756 let mut cache = setup_test_cache();
757 cache.pin_positions.insert(
759 3001,
760 StoredPin {
761 node_id: 999,
762 pin_type: 1,
763 rel_x: 0.0,
764 rel_y: 25.0,
765 },
766 );
767
768 let links_data = vec![(1, 3001, 2001)];
769 let links: Vec<SimpleLinkGeometry> =
770 cache.get_absolute_links(links_data.into_iter()).collect();
771
772 assert!(links.is_empty());
773 }
774
775 #[test]
776 fn test_get_absolute_links_empty_input() {
777 let cache = setup_test_cache();
778 let links_data: Vec<(i32, i32, i32)> = vec![];
779 let links: Vec<SimpleLinkGeometry> =
780 cache.get_absolute_links(links_data.into_iter()).collect();
781
782 assert!(links.is_empty());
783 }
784
785 #[test]
786 fn test_get_absolute_links_multiple_links() {
787 let mut cache = setup_test_cache();
788 cache.handle_pin_report(2002, 2, 1, 0.0, 40.0);
790
791 let links_data = vec![(1, 1001, 2001), (2, 1001, 2002)];
792 let links: Vec<SimpleLinkGeometry> =
793 cache.get_absolute_links(links_data.into_iter()).collect();
794
795 assert_eq!(links.len(), 2);
796 }
797
798 #[test]
803 fn test_compute_link_path_returns_valid_svg() {
804 let cache = setup_test_cache();
805 let path = cache
806 .compute_link_path(1001, 2001, 1.0, 50.0)
807 .expect("Path should be generated");
808
809 assert!(path.starts_with("M "));
810 assert!(path.contains(" C "));
811 }
812
813 #[test]
814 fn test_compute_link_path_returns_none_for_missing_start_pin() {
815 let cache = setup_test_cache();
816 let path = cache.compute_link_path(9999, 2001, 1.0, 50.0);
817 assert!(path.is_none());
818 }
819
820 #[test]
821 fn test_compute_link_path_returns_none_for_missing_end_pin() {
822 let cache = setup_test_cache();
823 let path = cache.compute_link_path(1001, 9999, 1.0, 50.0);
824 assert!(path.is_none());
825 }
826
827 #[test]
828 fn test_compute_link_path_returns_none_for_missing_start_node() {
829 let mut cache = setup_test_cache();
830 cache.pin_positions.insert(
831 3001,
832 StoredPin {
833 node_id: 999,
834 pin_type: 1,
835 rel_x: 0.0,
836 rel_y: 25.0,
837 },
838 );
839
840 let path = cache.compute_link_path(3001, 2001, 1.0, 50.0);
841 assert!(path.is_none());
842 }
843
844 #[test]
845 fn test_compute_link_path_different_zoom_levels() {
846 let cache = setup_test_cache();
847
848 let path1 = cache.compute_link_path(1001, 2001, 1.0, 50.0).unwrap();
849 let path2 = cache.compute_link_path(1001, 2001, 2.0, 50.0).unwrap();
850
851 assert_ne!(path1, path2);
853 }
854
855 #[test]
860 fn test_find_pin_at_hits_pin() {
861 let cache = setup_test_cache();
862 let pin_id = cache.find_pin_at(102.0, 27.0, 10.0);
864 assert_eq!(pin_id, 1001);
865 }
866
867 #[test]
868 fn test_find_pin_at_misses_all() {
869 let cache = setup_test_cache();
870 let pin_id = cache.find_pin_at(500.0, 500.0, 10.0);
871 assert_eq!(pin_id, 0);
872 }
873
874 #[test]
879 fn test_nodes_in_selection_box_finds_intersecting() {
880 let cache = setup_test_cache();
881 let selected = cache.nodes_in_selection_box(0.0, 0.0, 50.0, 50.0);
884 assert!(selected.contains(&1));
885 }
886
887 #[test]
888 fn test_nodes_in_selection_box_excludes_non_intersecting() {
889 let cache = setup_test_cache();
890 let selected = cache.nodes_in_selection_box(500.0, 500.0, 50.0, 50.0);
892 assert!(selected.is_empty());
893 }
894
895 #[test]
900 fn test_links_in_selection_box_finds_link_with_start_inside() {
901 let cache = setup_test_cache();
902 let links_data = vec![(1, 1001, 2001)];
903
904 let selected = cache.links_in_selection_box(90.0, 15.0, 20.0, 20.0, links_data.into_iter());
906 assert!(selected.contains(&1));
907 }
908
909 #[test]
910 fn test_links_in_selection_box_excludes_link_outside() {
911 let cache = setup_test_cache();
912 let links_data = vec![(1, 1001, 2001)];
913
914 let selected =
916 cache.links_in_selection_box(500.0, 500.0, 50.0, 50.0, links_data.into_iter());
917 assert!(selected.is_empty());
918 }
919
920 #[test]
925 fn test_compute_link_path_screen_zoom1_pan0() {
926 let cache = setup_test_cache();
927 let path = cache
930 .compute_link_path_screen(1001, 2001, 1.0, 0.0, 0.0, 50.0)
931 .expect("Path should be generated");
932 assert!(path.starts_with("M "));
933 assert!(path.contains(" C "));
934 }
935
936 #[test]
937 fn test_compute_link_path_screen_with_pan() {
938 let cache = setup_test_cache();
939 let path_no_pan = cache
941 .compute_link_path_screen(1001, 2001, 1.0, 0.0, 0.0, 50.0)
942 .unwrap();
943 let path_with_pan = cache
944 .compute_link_path_screen(1001, 2001, 1.0, 100.0, 50.0, 50.0)
945 .unwrap();
946 assert_ne!(path_no_pan, path_with_pan);
947 }
948
949 #[test]
950 fn test_compute_link_path_screen_with_zoom() {
951 let cache = setup_test_cache();
952 let path_z1 = cache
953 .compute_link_path_screen(1001, 2001, 1.0, 0.0, 0.0, 50.0)
954 .unwrap();
955 let path_z2 = cache
956 .compute_link_path_screen(1001, 2001, 2.0, 0.0, 0.0, 50.0)
957 .unwrap();
958 assert_ne!(path_z1, path_z2);
959 }
960
961 #[test]
962 fn test_compute_link_path_screen_missing_pin() {
963 let cache = setup_test_cache();
964 assert!(cache
965 .compute_link_path_screen(9999, 2001, 1.0, 0.0, 0.0, 50.0)
966 .is_none());
967 }
968}