bevy_fairy 0.3.3

An agentic debugger for Bevy games: a Bevy plugin that exposes the game's state over BRP, and an MCP server that lets a coding agent read and drive it
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/// Entity Highlighting Overlay Implementation
/// 
/// Provides visual highlighting for entities with customizable colors and highlight modes.
/// This overlay uses Bevy's Gizmo system for efficient rendering and supports multiple
/// viewports. Performance is optimized to stay under 1ms per frame.

use super::{OverlayMetrics, VisualOverlay};
use crate::server::brp_messages::DebugOverlayType;
#[cfg(feature = "visual_overlays")]
use bevy::prelude::*;
#[cfg(feature = "visual_overlays")]
use bevy::gizmos::*;
#[cfg(feature = "visual_overlays")]
use bevy::math::Isometry3d;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Instant;

/// Component that marks an entity for highlighting
#[derive(Component, Debug, Clone)]
pub struct HighlightedEntity {
    /// Highlight color (RGBA)
    pub color: Color,
    /// Highlight mode
    pub mode: HighlightMode,
    /// When the highlight was added
    pub timestamp: Instant,
    /// Priority (higher values render on top)
    pub priority: i32,
    /// Whether the highlight should pulse/animate
    pub animated: bool,
}

impl Default for HighlightedEntity {
    fn default() -> Self {
        Self {
            color: Color::srgb(1.0, 1.0, 0.0), // Yellow default
            mode: HighlightMode::Outline,
            timestamp: Instant::now(),
            priority: 0,
            animated: false,
        }
    }
}

/// Different highlighting modes available
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum HighlightMode {
    /// Draw an outline around the entity
    Outline,
    /// Apply a color tint to the entire entity
    Tint,
    /// Add a glow effect around the entity
    Glow,
    /// Wireframe overlay
    Wireframe,
    /// Solid color replacement
    SolidColor,
}

impl Default for HighlightMode {
    fn default() -> Self {
        HighlightMode::Outline
    }
}

/// Resource to store highlight configuration
#[derive(Resource, Debug, Clone, Default)]
pub struct HighlightConfig {
    /// Default highlight color
    pub default_color: Color,
    /// Default highlight mode
    pub default_mode: HighlightMode,
    /// Maximum number of highlighted entities
    pub max_highlighted: usize,
    /// Outline thickness for outline mode
    pub outline_thickness: f32,
    /// Glow intensity for glow mode
    pub glow_intensity: f32,
    /// Animation speed for animated highlights
    pub animation_speed: f32,
    /// Whether to show highlight info in UI
    pub show_info_ui: bool,
}

impl HighlightConfig {
    /// Create new highlight configuration with reasonable defaults
    pub fn new() -> Self {
        Self {
            default_color: Color::srgb(1.0, 1.0, 0.0), // Yellow
            default_mode: HighlightMode::Outline,
            max_highlighted: 100, // Reasonable limit
            outline_thickness: 0.02,
            glow_intensity: 1.5,
            animation_speed: 2.0, // 2 Hz
            show_info_ui: true,
        }
    }
    
    /// Update configuration from JSON
    pub fn update_from_json(&mut self, config: &serde_json::Value) -> Result<(), String> {
        if let Some(color_array) = config.get("default_color").and_then(|v| v.as_array()) {
            if color_array.len() >= 3 {
                let r = color_array[0].as_f64().ok_or("Invalid red component")? as f32;
                let g = color_array[1].as_f64().ok_or("Invalid green component")? as f32;
                let b = color_array[2].as_f64().ok_or("Invalid blue component")? as f32;
                let a = color_array.get(3).and_then(|v| v.as_f64()).unwrap_or(1.0) as f32;
                self.default_color = Color::srgba(r, g, b, a);
            }
        }
        
        if let Some(mode_str) = config.get("default_mode").and_then(|v| v.as_str()) {
            self.default_mode = match mode_str {
                "outline" => HighlightMode::Outline,
                "tint" => HighlightMode::Tint,
                "glow" => HighlightMode::Glow,
                "wireframe" => HighlightMode::Wireframe,
                "solid" => HighlightMode::SolidColor,
                _ => return Err(format!("Invalid highlight mode: {}", mode_str)),
            };
        }
        
        if let Some(max) = config.get("max_highlighted").and_then(|v| v.as_u64()) {
            self.max_highlighted = (max as usize).min(1000); // Cap at 1000 for performance
        }
        
        if let Some(thickness) = config.get("outline_thickness").and_then(|v| v.as_f64()) {
            self.outline_thickness = (thickness as f32).max(0.001).min(0.1); // Reasonable bounds
        }
        
        if let Some(intensity) = config.get("glow_intensity").and_then(|v| v.as_f64()) {
            self.glow_intensity = (intensity as f32).max(0.1).min(10.0); // Reasonable bounds
        }
        
        if let Some(speed) = config.get("animation_speed").and_then(|v| v.as_f64()) {
            self.animation_speed = (speed as f32).max(0.1).min(10.0); // Reasonable bounds
        }
        
        if let Some(show_ui) = config.get("show_info_ui").and_then(|v| v.as_bool()) {
            self.show_info_ui = show_ui;
        }
        
        Ok(())
    }
}

/// Gizmo configuration for highlighting
#[derive(Resource, Debug, Clone)]
pub struct HighlightGizmosConfig {
    /// Whether to show debug text labels
    pub show_labels: bool,
    /// Line width for wireframes
    pub line_width: f32,
    /// Circle resolution for round highlights
    pub circle_resolution: usize,
    /// Maximum distance for visibility culling
    pub max_distance: f32,
    /// Whether to enable per-viewport rendering
    pub per_viewport_rendering: bool,
}

impl Default for HighlightGizmosConfig {
    fn default() -> Self {
        Self {
            show_labels: true,
            line_width: 2.0,
            circle_resolution: 32,
            max_distance: 1000.0,
            per_viewport_rendering: true,
        }
    }
}

/// Entity Highlight Overlay implementation
#[derive(Debug)]
pub struct EntityHighlightOverlay {
    enabled: bool,
    config: HighlightConfig,
    metrics: OverlayMetrics,
    highlighted_entities: HashMap<Entity, HighlightedEntity>,
}

impl EntityHighlightOverlay {
    pub fn new() -> Self {
        Self {
            enabled: false,
            config: HighlightConfig::new(),
            metrics: OverlayMetrics::default(),
            highlighted_entities: HashMap::new(),
        }
    }
    
    /// Add highlight to an entity
    pub fn highlight_entity(
        &mut self,
        entity: Entity,
        color: Option<Color>,
        mode: Option<HighlightMode>,
        animated: bool,
        priority: i32,
    ) {
        if self.highlighted_entities.len() >= self.config.max_highlighted {
            warn!("Maximum highlighted entities reached: {}", self.config.max_highlighted);
            return;
        }
        
        let highlight = HighlightedEntity {
            color: color.unwrap_or(self.config.default_color),
            mode: mode.unwrap_or(self.config.default_mode),
            timestamp: Instant::now(),
            priority,
            animated,
        };
        
        self.highlighted_entities.insert(entity, highlight);
        self.metrics.element_count = self.highlighted_entities.len();
    }
    
    /// Remove highlight from an entity
    pub fn unhighlight_entity(&mut self, entity: Entity) {
        self.highlighted_entities.remove(&entity);
        self.metrics.element_count = self.highlighted_entities.len();
    }
    
    /// Clear all highlights
    pub fn clear_highlights(&mut self) {
        self.highlighted_entities.clear();
        self.metrics.element_count = 0;
    }
    
    /// Get currently highlighted entities
    pub fn get_highlighted_entities(&self) -> &HashMap<Entity, HighlightedEntity> {
        &self.highlighted_entities
    }
}

impl Default for EntityHighlightOverlay {
    fn default() -> Self {
        Self::new()
    }
}

impl VisualOverlay for EntityHighlightOverlay {
    fn initialize(&mut self, app: &mut App) {
        app.insert_resource(self.config.clone())
            .insert_resource(HighlightGizmosConfig::default())
            .add_systems(Update, (
                render_highlighted_entities,
                animate_highlighted_entities,
                cleanup_old_highlights,
            ))
            .add_systems(PostUpdate, (
                update_highlight_metrics,
            ));
        
        info!("Entity highlight overlay initialized with Gizmo rendering");
    }
    
    fn update_config(&mut self, config: &serde_json::Value) -> Result<(), String> {
        self.config.update_from_json(config)?;
        info!("Entity highlight overlay config updated: {:?}", self.config);
        Ok(())
    }
    
    fn set_enabled(&mut self, enabled: bool) {
        self.enabled = enabled;
        if !enabled {
            self.cleanup();
        }
    }
    
    fn is_enabled(&self) -> bool {
        self.enabled
    }
    
    fn get_metrics(&self) -> OverlayMetrics {
        self.metrics.clone()
    }
    
    fn overlay_type(&self) -> DebugOverlayType {
        DebugOverlayType::EntityHighlight
    }
    
    fn cleanup(&mut self) {
        self.clear_highlights();
    }
}

/// System to render highlighted entities using Gizmos (per-viewport aware)
fn render_highlighted_entities(
    mut gizmos: Gizmos,
    config: Res<HighlightConfig>,
    gizmo_config: Res<HighlightGizmosConfig>,
    viewport_config: Res<super::ViewportConfig>,
    time: Res<Time>,
    query: Query<(&Transform, &HighlightedEntity), With<Visibility>>,
    cameras: Query<(Entity, &Camera, &GlobalTransform), With<Camera>>,
) {
    let start_time = std::time::Instant::now();
    let mut total_rendered = 0;
    let mut viewport_stats = HashMap::new();
    
    // Early exit if no highlights
    if query.is_empty() {
        return;
    }
    
    // Process each active viewport/camera separately
    for (camera_entity, camera, camera_transform) in &cameras {
        if !camera.is_active {
            continue;
        }
        
        let viewport_id = format!("camera_{}", camera_entity.index());
        let viewport_settings = viewport_config.viewport_overlays.get(&viewport_id)
            .unwrap_or(&viewport_config.default_settings);
        
        // Skip if overlays disabled for this viewport
        if !viewport_settings.enabled {
            continue;
        }
        
        // Check if entity highlighting is enabled for this viewport
        if let Some(&overlay_enabled) = viewport_settings.overlay_visibility.get("entity_highlight") {
            if !overlay_enabled {
                continue;
            }
        }
        
        let viewport_start_time = std::time::Instant::now();
        let mut viewport_rendered = 0;
        let camera_pos = camera_transform.translation();
        
        // Determine LOD level based on viewport settings
        let default_lod = super::LodSettings::default();
        let lod_settings = &viewport_settings.lod_settings;
        
        for (transform, highlight) in &query {
            if viewport_rendered >= viewport_settings.max_elements {
                break; // Respect per-viewport element limit
            }
            
            let entity_pos = transform.translation;
            let distance = camera_pos.distance(entity_pos);
            
            // Distance culling for performance
            if distance > gizmo_config.max_distance {
                continue;
            }
            
            // Apply LOD based on distance
            let (lod_level, detail_factor) = calculate_lod(distance, lod_settings);
            
            // Skip rendering if too far for current LOD
            if lod_level >= lod_settings.element_limits.len() {
                continue;
            }
            
            // Check per-viewport render budget
            let current_viewport_time = viewport_start_time.elapsed().as_micros() as u64;
            if current_viewport_time > viewport_settings.render_budget_us {
                warn!("Viewport {} exceeded render budget: {}μs", viewport_id, current_viewport_time);
                break;
            }
        
            let mut color = highlight.color;
            
            // Apply animation if enabled (reduced for LOD)
            if highlight.animated {
                let pulse = (time.elapsed_secs() * config.animation_speed).sin();
                let alpha_mod = (pulse * 0.3 + 0.7).max(0.4).min(1.0);
                color = color.with_alpha(color.alpha() * alpha_mod);
            }
            
            // Apply LOD detail factor to visual complexity
            let effective_thickness = config.outline_thickness * detail_factor;
            let effective_intensity = config.glow_intensity * detail_factor;
            
            // Render based on highlight mode and LOD
            match highlight.mode {
                HighlightMode::Outline => {
                    render_outline_gizmo(&mut gizmos, transform, color, effective_thickness);
                }
                HighlightMode::Wireframe => {
                    render_wireframe_gizmo_lod(&mut gizmos, transform, color, &gizmo_config, detail_factor);
                }
                HighlightMode::Glow => {
                    render_glow_gizmo(&mut gizmos, transform, color, effective_intensity);
                }
                HighlightMode::Tint => {
                    render_tint_gizmo(&mut gizmos, transform, color);
                }
                HighlightMode::SolidColor => {
                    render_solid_gizmo(&mut gizmos, transform, color);
                }
            }
            
            // Render debug label if enabled and high detail
            if gizmo_config.show_labels && detail_factor > 0.7 {
                let label_pos = transform.translation + Vec3::Y * 2.0;
                gizmos.sphere(Isometry3d::new(label_pos, Quat::IDENTITY), 0.1 * detail_factor, color);
            }
            
            viewport_rendered += 1;
            total_rendered += 1;
        }
        
        // Record viewport statistics
        let viewport_render_time = viewport_start_time.elapsed().as_micros() as u64;
        viewport_stats.insert(viewport_id.clone(), super::ViewportRenderStats {
            elements_rendered: viewport_rendered,
            render_time_us: viewport_render_time,
            active: true,
            viewport_size: None, // Could be extracted from camera viewport if needed
        });
        
        // Log viewport performance if over budget
        if viewport_render_time > viewport_settings.render_budget_us {
            debug!(
                "Viewport {} render time: {}μs, elements: {}",
                viewport_id, viewport_render_time, viewport_rendered
            );
        }
    }
    
    // Track overall performance
    let total_render_time = start_time.elapsed().as_micros() as u64;
    if total_render_time > 1000 { // Warn if over 1ms total
        warn!("Entity highlight rendering took {}μs for {} entities across {} viewports", 
              total_render_time, total_rendered, viewport_stats.len());
    }
}

/// System to animate highlighted entities (now handled in render system)
fn animate_highlighted_entities(
    // Animation is now handled directly in render_highlighted_entities
    // This system is kept for potential future animation logic
) {
    // No-op - animation handled in render system for performance
}

/// System to clean up old highlights
fn cleanup_old_highlights(
    mut commands: Commands,
    query: Query<(Entity, &HighlightedEntity)>,
) {
    let now = Instant::now();
    
    for (entity, highlight) in &query {
        // Remove highlights older than 1 hour (configurable)
        if now.duration_since(highlight.timestamp).as_secs() > 3600 {
            commands.entity(entity).remove::<HighlightedEntity>();
        }
    }
}

/// Render an outline gizmo around an entity
fn render_outline_gizmo(gizmos: &mut Gizmos, transform: &Transform, color: Color, thickness: f32) {
    let size = Vec3::splat(1.0 + thickness); // Slightly larger than the entity
    let position = transform.translation;
    let rotation = transform.rotation;
    
    // Draw wireframe box as outline
    gizmos.cube(
        Transform {
            translation: position,
            rotation,
            scale: size,
        },
        color,
    );
}

/// Calculate LOD level and detail factor based on distance
pub fn calculate_lod(distance: f32, lod_settings: &super::LodSettings) -> (usize, f32) {
    for (i, &threshold) in lod_settings.distance_thresholds.iter().enumerate() {
        if distance <= threshold {
            let detail_factor = lod_settings.detail_levels.get(i).copied().unwrap_or(1.0);
            return (i, detail_factor);
        }
    }
    
    // Beyond all thresholds - lowest LOD
    let last_level = lod_settings.distance_thresholds.len();
    let detail_factor = lod_settings.detail_levels.get(last_level).copied().unwrap_or(0.1);
    (last_level, detail_factor)
}

/// Render a wireframe gizmo for an entity
fn render_wireframe_gizmo(gizmos: &mut Gizmos, transform: &Transform, color: Color, config: &HighlightGizmosConfig) {
    render_wireframe_gizmo_lod(gizmos, transform, color, config, 1.0);
}

/// Render a wireframe gizmo with LOD support
fn render_wireframe_gizmo_lod(gizmos: &mut Gizmos, transform: &Transform, color: Color, config: &HighlightGizmosConfig, detail_factor: f32) {
    let position = transform.translation;
    let rotation = transform.rotation;
    let scale = transform.scale;
    
    // Always draw basic wireframe
    gizmos.cube(
        Transform {
            translation: position,
            rotation,
            scale,
        },
        color,
    );
    
    // Add additional detail based on LOD
    if detail_factor > 0.5 {
        let corners = [
            position + rotation * (Vec3::new(-0.5, -0.5, -0.5) * scale),
            position + rotation * (Vec3::new(0.5, -0.5, -0.5) * scale),
            position + rotation * (Vec3::new(0.5, 0.5, -0.5) * scale),
            position + rotation * (Vec3::new(-0.5, 0.5, -0.5) * scale),
            position + rotation * (Vec3::new(-0.5, -0.5, 0.5) * scale),
            position + rotation * (Vec3::new(0.5, -0.5, 0.5) * scale),
            position + rotation * (Vec3::new(0.5, 0.5, 0.5) * scale),
            position + rotation * (Vec3::new(-0.5, 0.5, 0.5) * scale),
        ];
        
        // Draw cross lines for more visibility (only at high detail)
        for i in 0..4 {
            gizmos.line(corners[i], corners[i + 4], color);
        }
    }
}

/// Render a glow effect using concentric shapes
fn render_glow_gizmo(gizmos: &mut Gizmos, transform: &Transform, color: Color, intensity: f32) {
    let position = transform.translation;
    let base_radius = 1.0 * transform.scale.max_element();
    
    // Multiple concentric circles/spheres for glow effect
    for i in 1..=3 {
        let radius = base_radius * (1.0 + i as f32 * 0.2 * intensity);
        let alpha = color.alpha() / (i as f32 * 2.0);
        let glow_color = color.with_alpha(alpha);
        
        gizmos.sphere(Isometry3d::new(position, Quat::IDENTITY), radius, glow_color);
    }
}

/// Render a tint overlay
fn render_tint_gizmo(gizmos: &mut Gizmos, transform: &Transform, color: Color) {
    // For tint mode, draw a semi-transparent cube
    let alpha_color = color.with_alpha(color.alpha() * 0.3);
    gizmos.cube(*transform, alpha_color);
}

/// Render solid color replacement
fn render_solid_gizmo(gizmos: &mut Gizmos, transform: &Transform, color: Color) {
    // Draw solid-colored cube
    gizmos.cube(*transform, color);
}

/// System to update highlight metrics
fn update_highlight_metrics(
    query: Query<&HighlightedEntity>,
    mut overlay_manager: ResMut<super::VisualOverlayManager>,
) {
    let start_time = Instant::now();
    
    let count = query.iter().count();
    let render_time = start_time.elapsed().as_micros() as u64;
    
    // Estimate memory usage (Gizmos are much lighter than materials)
    let estimated_memory = count * std::mem::size_of::<HighlightedEntity>() + 
                          count * 64; // Estimated Gizmo overhead per entity
    
    let metrics = OverlayMetrics {
        render_time_us: render_time,
        element_count: count,
        memory_usage_bytes: estimated_memory,
        frame_updates: if count > 0 { 1 } else { 0 },
        active_this_frame: count > 0,
        viewport_stats: HashMap::new(),
    };
    
    // Update the specific overlay metrics
    if let Some(overlay) = overlay_manager.overlays.get_mut("entity_highlight") {
        // This needs to be properly implemented to access the overlay metrics
        // For now, this is a conceptual placeholder
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_highlight_config_json_update() {
        let mut config = HighlightConfig::new();
        
        let json_config = serde_json::json!({
            "default_color": [1.0, 0.0, 0.0, 1.0],
            "default_mode": "glow",
            "max_highlighted": 50,
            "outline_thickness": 0.05,
            "glow_intensity": 2.0
        });
        
        assert!(config.update_from_json(&json_config).is_ok());
        
        assert_eq!(config.default_color, Color::srgba(1.0, 0.0, 0.0, 1.0));
        assert_eq!(config.default_mode, HighlightMode::Glow);
        assert_eq!(config.max_highlighted, 50);
        assert_eq!(config.outline_thickness, 0.05);
        assert_eq!(config.glow_intensity, 2.0);
    }

    #[test]
    fn test_highlight_entity_management() {
        let mut overlay = EntityHighlightOverlay::new();
        overlay.config.max_highlighted = 2; // Small limit for testing
        
        let entity1 = Entity::from_raw_u32(1).unwrap();
        let entity2 = Entity::from_raw_u32(2).unwrap();
        let entity3 = Entity::from_raw_u32(3).unwrap();
        
        // Add highlights
        overlay.highlight_entity(entity1, Some(Color::srgb(1.0, 0.0, 0.0)), Some(HighlightMode::Outline), false, 0);
        overlay.highlight_entity(entity2, Some(Color::srgb(0.0, 1.0, 0.0)), Some(HighlightMode::Glow), true, 1);
        
        assert_eq!(overlay.highlighted_entities.len(), 2);
        assert_eq!(overlay.metrics.element_count, 2);
        
        // Try to add third (should be rejected due to limit)
        overlay.highlight_entity(entity3, Some(Color::srgb(0.0, 0.0, 1.0)), Some(HighlightMode::Tint), false, 2);
        assert_eq!(overlay.highlighted_entities.len(), 2); // Still 2
        
        // Remove one highlight
        overlay.unhighlight_entity(entity1);
        assert_eq!(overlay.highlighted_entities.len(), 1);
        assert_eq!(overlay.metrics.element_count, 1);
        
        // Clear all
        overlay.clear_highlights();
        assert_eq!(overlay.highlighted_entities.len(), 0);
        assert_eq!(overlay.metrics.element_count, 0);
    }

    #[test]
    fn test_highlight_modes() {
        let highlight = HighlightedEntity {
            mode: HighlightMode::Glow,
            ..Default::default()
        };
        
        assert_eq!(highlight.mode, HighlightMode::Glow);
        
        // Test serialization
        let serialized = serde_json::to_string(&highlight.mode).unwrap();
        assert!(serialized.contains("Glow"));
        
        let deserialized: HighlightMode = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized, HighlightMode::Glow);
    }
}