designguard 0.1.1

KiCad schematic and PCB validation library
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
//! Comprehensive tests for Capacitor Classifier
//!
//! Tests verify the classification logic for various capacitor scenarios:
//! - Timing caps near crystals
//! - Filtering caps on signal lines
//! - Unknown caps with no net connectivity
//! - Shared via detection (handled by DRS, but verified here)

#[cfg(test)]
mod comprehensive_tests {
    use crate::analyzer::capacitor_classifier::{CapacitorClassifier, CapacitorFunction};
    use crate::parser::schema::{Component, Schematic, Position, Pin, Net, Connection};
    use crate::compliance::power_net_registry::PowerNetRegistry;
    use crate::parser::netlist::PinNetConnection;
    use std::collections::HashMap;

    /// Helper: Create a test schematic with components
    fn create_test_schematic() -> Schematic {
        Schematic {
            uuid: "test-schematic".to_string(),
            filename: "test.kicad_sch".to_string(),
            version: None,
            components: Vec::new(),
            wires: Vec::new(),
            labels: Vec::new(),
            nets: Vec::new(),
            power_symbols: Vec::new(),
        }
    }

    /// Helper: Create a capacitor component
    fn create_capacitor(
        ref_des: &str,
        value: &str,
        position: Position,
        footprint: Option<&str>,
    ) -> Component {
        Component {
            uuid: format!("uuid-{}", ref_des),
            reference: ref_des.to_string(),
            value: value.to_string(),
            lib_id: "Device:C".to_string(),
            footprint: footprint.map(|s| s.to_string()),
            position,
            rotation: 0.0,
            properties: HashMap::new(),
            pins: vec![
                Pin {
                    number: "1".to_string(),
                    uuid: format!("pin1-{}", ref_des),
                },
                Pin {
                    number: "2".to_string(),
                    uuid: format!("pin2-{}", ref_des),
                },
            ],
        }
    }

    /// Helper: Create a crystal component
    fn create_crystal(ref_des: &str, position: Position) -> Component {
        Component {
            uuid: format!("uuid-{}", ref_des),
            reference: ref_des.to_string(),
            value: "16MHz".to_string(),
            lib_id: "Device:Crystal".to_string(),
            footprint: None,
            position,
            rotation: 0.0,
            properties: HashMap::new(),
            pins: vec![
                Pin {
                    number: "1".to_string(),
                    uuid: format!("pin1-{}", ref_des),
                },
                Pin {
                    number: "2".to_string(),
                    uuid: format!("pin2-{}", ref_des),
                },
            ],
        }
    }

    /// Helper: Create pin-to-net mapping
    fn create_pin_to_net(
        component_ref: &str,
        pin1_net: &str,
        pin2_net: &str,
    ) -> HashMap<String, Vec<PinNetConnection>> {
        let mut map = HashMap::new();
        map.insert(
            format!("{}:1", component_ref),
            vec![PinNetConnection {
                component_ref: component_ref.to_string(),
                pin_number: "1".to_string(),
                net_name: pin1_net.to_string(),
            }],
        );
        map.insert(
            format!("{}:2", component_ref),
            vec![PinNetConnection {
                component_ref: component_ref.to_string(),
                pin_number: "2".to_string(),
                net_name: pin2_net.to_string(),
            }],
        );
        map
    }

    /// Case 1: 22pF cap near a crystal (Should be Timing)
    #[test]
    fn test_timing_cap_near_crystal() {
        let mut schematic = create_test_schematic();
        
        // Create crystal at (100, 100)
        let crystal = create_crystal("Y1", Position { x: 100.0, y: 100.0 });
        schematic.components.push(crystal);
        
        // Create 22pF capacitor at (105, 100) - 5mm from crystal
        let cap = create_capacitor(
            "C1",
            "22pF",
            Position { x: 105.0, y: 100.0 },
            Some("0402"),
        );
        schematic.components.push(cap);
        
        // Create pin-to-net mapping: C1 pin1 -> XTAL_IN, pin2 -> GND
        let pin_to_net = create_pin_to_net("C1", "XTAL_IN", "GND");
        
        // Build power registry
        let power_registry = PowerNetRegistry::new(&schematic);
        
        // Classify
        let classifications = CapacitorClassifier::classify_capacitors(
            &schematic,
            &power_registry,
            &pin_to_net,
        );
        
        // Verify
        assert!(!classifications.is_empty(), "Should have at least one classification");
        let c1_class = classifications.iter().find(|c| c.component_ref == "C1");
        assert!(c1_class.is_some(), "C1 should be classified");
        
        let classification = c1_class.unwrap();
        assert_eq!(
            classification.function,
            CapacitorFunction::Timing,
            "C1 (22pF near crystal) should be classified as Timing"
        );
        assert!(
            classification.confidence > 0.5,
            "Confidence should be > 0.5, got {}",
            classification.confidence
        );
        assert!(
            classification.reasoning.contains("Timing") || classification.reasoning.contains("crystal"),
            "Reasoning should mention timing or crystal: {}",
            classification.reasoning
        );
    }

    /// Case 2: 100nF cap between a signal line and GND (Should be Filtering)
    #[test]
    fn test_filtering_cap_signal_to_gnd() {
        let mut schematic = create_test_schematic();
        
        // Create 100nF capacitor
        let cap = create_capacitor(
            "C2",
            "100nF",
            Position { x: 50.0, y: 50.0 },
            Some("0603"),
        );
        schematic.components.push(cap);
        
        // Create a connector nearby (to increase confidence)
        let connector = Component {
            uuid: "uuid-J1".to_string(),
            reference: "J1".to_string(),
            value: "USB-A".to_string(),
            lib_id: "Connector:USB".to_string(),
            footprint: None,
            position: Position { x: 55.0, y: 50.0 },
            rotation: 0.0,
            properties: HashMap::new(),
            pins: Vec::new(),
        };
        schematic.components.push(connector);
        
        // Create pin-to-net mapping: C2 pin1 -> SIGNAL_IN, pin2 -> GND
        let pin_to_net = create_pin_to_net("C2", "SIGNAL_IN", "GND");
        
        // Build power registry
        let power_registry = PowerNetRegistry::new(&schematic);
        
        // Classify
        let classifications = CapacitorClassifier::classify_capacitors(
            &schematic,
            &power_registry,
            &pin_to_net,
        );
        
        // Verify
        assert!(!classifications.is_empty(), "Should have at least one classification");
        let c2_class = classifications.iter().find(|c| c.component_ref == "C2");
        assert!(c2_class.is_some(), "C2 should be classified");
        
        let classification = c2_class.unwrap();
        assert_eq!(
            classification.function,
            CapacitorFunction::Filtering,
            "C2 (100nF, SIGNAL_IN to GND) should be classified as Filtering"
        );
        assert!(
            classification.reasoning.contains("Filtering") || classification.reasoning.contains("low-pass"),
            "Reasoning should mention filtering: {}",
            classification.reasoning
        );
    }

    /// Case 3: 0.1µF cap with no net names (Should be Unknown)
    #[test]
    fn test_unknown_cap_no_nets() {
        let mut schematic = create_test_schematic();
        
        // Create 0.1µF capacitor
        let cap = create_capacitor(
            "C3",
            "0.1uF",
            Position { x: 30.0, y: 30.0 },
            Some("0603"),
        );
        schematic.components.push(cap);
        
        // No pin-to-net mapping (empty)
        let pin_to_net = HashMap::new();
        
        // Build power registry
        let power_registry = PowerNetRegistry::new(&schematic);
        
        // Classify
        let classifications = CapacitorClassifier::classify_capacitors(
            &schematic,
            &power_registry,
            &pin_to_net,
        );
        
        // Verify
        let c3_class = classifications.iter().find(|c| c.component_ref == "C3");
        
        // Should either be Unknown or not classified at all (if no nets)
        if let Some(classification) = c3_class {
            assert_eq!(
                classification.function,
                CapacitorFunction::Unknown,
                "C3 (0.1µF with no nets) should be classified as Unknown"
            );
        } else {
            // If not classified, that's also acceptable (no nets = can't classify)
            // This is fine - the classifier may skip caps with no net connectivity
        }
    }

    /// Case 4: Two 0.1µF caps sharing a single via to GND
    /// Note: Shared via detection is handled by DRS, but we verify the caps are classified correctly
    #[test]
    fn test_two_caps_sharing_via() {
        let mut schematic = create_test_schematic();
        
        // Create two 0.1µF capacitors close together
        let cap1 = create_capacitor(
            "C4",
            "0.1uF",
            Position { x: 100.0, y: 100.0 },
            Some("0603"),
        );
        let cap2 = create_capacitor(
            "C5",
            "0.1uF",
            Position { x: 102.0, y: 100.0 },
            Some("0603"),
        );
        schematic.components.push(cap1);
        schematic.components.push(cap2);
        
        // Create an IC nearby
        let ic = Component {
            uuid: "uuid-U1".to_string(),
            reference: "U1".to_string(),
            value: "STM32F4".to_string(),
            lib_id: "MCU:STM32".to_string(),
            footprint: None,
            position: Position { x: 105.0, y: 100.0 },
            rotation: 0.0,
            properties: HashMap::new(),
            pins: Vec::new(),
        };
        schematic.components.push(ic);
        
        // Create pin-to-net mapping: Both caps connected to VCC and GND
        let mut pin_to_net = HashMap::new();
        pin_to_net.extend(create_pin_to_net("C4", "VCC", "GND"));
        pin_to_net.extend(create_pin_to_net("C5", "VCC", "GND"));
        
        // Build power registry
        let mut schematic_with_vcc = schematic.clone();
        schematic_with_vcc.nets.push(Net {
            name: "VCC".to_string(),
            connections: vec![
                Connection {
                    component_ref: "C4".to_string(),
                    pin_number: "1".to_string(),
                },
                Connection {
                    component_ref: "C5".to_string(),
                    pin_number: "1".to_string(),
                },
            ],
        });
        let power_registry = PowerNetRegistry::new(&schematic_with_vcc);
        
        // Classify
        let classifications = CapacitorClassifier::classify_capacitors(
            &schematic,
            &power_registry,
            &pin_to_net,
        );
        
        // Verify both caps are classified as Decoupling
        let c4_class = classifications.iter().find(|c| c.component_ref == "C4");
        let c5_class = classifications.iter().find(|c| c.component_ref == "C5");
        
        assert!(c4_class.is_some(), "C4 should be classified");
        assert!(c5_class.is_some(), "C5 should be classified");
        
        // Both should be Decoupling (0.1µF = 100nF, which is in the 10nF-2.2µF range)
        if let Some(classification) = c4_class {
            assert_eq!(
                classification.function,
                CapacitorFunction::Decoupling,
                "C4 (0.1µF, VCC to GND) should be classified as Decoupling"
            );
        }
        
        if let Some(classification) = c5_class {
            assert_eq!(
                classification.function,
                CapacitorFunction::Decoupling,
                "C5 (0.1µF, VCC to GND) should be classified as Decoupling"
            );
        }
        
        // Note: Shared via detection would be done by DRS when PCB is available
        // This test verifies the classification is correct, not the shared via warning
    }

    /// Additional test: Verify decoupling cap classification
    #[test]
    fn test_decoupling_cap() {
        let mut schematic = create_test_schematic();
        
        // Create 100nF capacitor between VCC and GND
        let cap = create_capacitor(
            "C6",
            "100nF",
            Position { x: 50.0, y: 50.0 },
            Some("0402"),
        );
        schematic.components.push(cap);
        
        // Create pin-to-net mapping: C6 pin1 -> VCC, pin2 -> GND
        let pin_to_net = create_pin_to_net("C6", "VCC", "GND");
        
        // Build power registry with VCC as power net
        let mut schematic_with_vcc = schematic.clone();
        schematic_with_vcc.nets.push(Net {
            name: "VCC".to_string(),
            connections: vec![Connection {
                component_ref: "C6".to_string(),
                pin_number: "1".to_string(),
            }],
        });
        let power_registry = PowerNetRegistry::new(&schematic_with_vcc);
        
        // Classify
        let classifications = CapacitorClassifier::classify_capacitors(
            &schematic,
            &power_registry,
            &pin_to_net,
        );
        
        // Verify
        let c6_class = classifications.iter().find(|c| c.component_ref == "C6");
        assert!(c6_class.is_some(), "C6 should be classified");
        
        let classification = c6_class.unwrap();
        assert_eq!(
            classification.function,
            CapacitorFunction::Decoupling,
            "C6 (100nF, VCC to GND) should be classified as Decoupling"
        );
    }

    /// Additional test: Verify bulk cap classification
    #[test]
    fn test_bulk_cap() {
        let mut schematic = create_test_schematic();
        
        // Create 10µF capacitor between VCC and GND
        let cap = create_capacitor(
            "C7",
            "10uF",
            Position { x: 50.0, y: 50.0 },
            Some("0805"),
        );
        schematic.components.push(cap);
        
        // Create pin-to-net mapping: C7 pin1 -> VCC, pin2 -> GND
        let pin_to_net = create_pin_to_net("C7", "VCC", "GND");
        
        // Build power registry
        let mut schematic_with_vcc = schematic.clone();
        schematic_with_vcc.nets.push(Net {
            name: "VCC".to_string(),
            connections: vec![Connection {
                component_ref: "C7".to_string(),
                pin_number: "1".to_string(),
            }],
        });
        let power_registry = PowerNetRegistry::new(&schematic_with_vcc);
        
        // Classify
        let classifications = CapacitorClassifier::classify_capacitors(
            &schematic,
            &power_registry,
            &pin_to_net,
        );
        
        // Verify
        let c7_class = classifications.iter().find(|c| c.component_ref == "C7");
        assert!(c7_class.is_some(), "C7 should be classified");
        
        let classification = c7_class.unwrap();
        assert_eq!(
            classification.function,
            CapacitorFunction::Bulk,
            "C7 (10µF, VCC to GND) should be classified as Bulk"
        );
    }

    /// Test: Create a board with 10 capacitors (as requested)
    #[test]
    fn test_board_with_10_capacitors() {
        let mut schematic = create_test_schematic();
        
        // C1: 22pF near crystal (Timing)
        let crystal = create_crystal("Y1", Position { x: 100.0, y: 100.0 });
        schematic.components.push(crystal);
        let c1 = create_capacitor("C1", "22pF", Position { x: 105.0, y: 100.0 }, Some("0402"));
        schematic.components.push(c1);
        
        // C2: 100nF signal to GND (Filtering)
        let c2 = create_capacitor("C2", "100nF", Position { x: 50.0, y: 50.0 }, Some("0603"));
        schematic.components.push(c2);
        
        // C3: 0.1µF no nets (Unknown)
        let c3 = create_capacitor("C3", "0.1uF", Position { x: 30.0, y: 30.0 }, Some("0603"));
        schematic.components.push(c3);
        
        // C4, C5: Two 0.1µF caps sharing VCC/GND (Decoupling, potential shared via)
        let c4 = create_capacitor("C4", "0.1uF", Position { x: 100.0, y: 100.0 }, Some("0603"));
        let c5 = create_capacitor("C5", "0.1uF", Position { x: 102.0, y: 100.0 }, Some("0603"));
        schematic.components.push(c4);
        schematic.components.push(c5);
        
        // C6: 100nF VCC to GND (Decoupling)
        let c6 = create_capacitor("C6", "100nF", Position { x: 70.0, y: 70.0 }, Some("0402"));
        schematic.components.push(c6);
        
        // C7: 10µF VCC to GND (Bulk)
        let c7 = create_capacitor("C7", "10uF", Position { x: 80.0, y: 80.0 }, Some("0805"));
        schematic.components.push(c7);
        
        // C8: 47pF near crystal (Timing)
        let c8 = create_capacitor("C8", "47pF", Position { x: 95.0, y: 100.0 }, Some("0402"));
        schematic.components.push(c8);
        
        // C9: 1nF signal to signal (Filtering in-series)
        let c9 = create_capacitor("C9", "1nF", Position { x: 60.0, y: 60.0 }, Some("0402"));
        schematic.components.push(c9);
        
        // C10: 220nF VCC to GND (Decoupling)
        let c10 = create_capacitor("C10", "220nF", Position { x: 90.0, y: 90.0 }, Some("0603"));
        schematic.components.push(c10);
        
        // Create pin-to-net mappings
        let mut pin_to_net = HashMap::new();
        pin_to_net.extend(create_pin_to_net("C1", "XTAL_IN", "GND"));  // Timing
        pin_to_net.extend(create_pin_to_net("C2", "SIGNAL_IN", "GND"));  // Filtering
        // C3: no nets (Unknown)
        pin_to_net.extend(create_pin_to_net("C4", "VCC", "GND"));  // Decoupling
        pin_to_net.extend(create_pin_to_net("C5", "VCC", "GND"));  // Decoupling
        pin_to_net.extend(create_pin_to_net("C6", "VCC", "GND"));  // Decoupling
        pin_to_net.extend(create_pin_to_net("C7", "VCC", "GND"));  // Bulk
        pin_to_net.extend(create_pin_to_net("C8", "XTAL_OUT", "GND"));  // Timing
        pin_to_net.extend(create_pin_to_net("C9", "SIGNAL_A", "SIGNAL_B"));  // Filtering
        pin_to_net.extend(create_pin_to_net("C10", "VCC", "GND"));  // Decoupling
        
        // Build power registry
        let mut schematic_with_nets = schematic.clone();
        schematic_with_nets.nets.push(Net {
            name: "VCC".to_string(),
            connections: vec![],
        });
        let power_registry = PowerNetRegistry::new(&schematic_with_nets);
        
        // Classify all capacitors
        let classifications = CapacitorClassifier::classify_capacitors(
            &schematic,
            &power_registry,
            &pin_to_net,
        );
        
        // Verify we classified all capacitors with nets
        assert_eq!(classifications.len(), 9, "Should classify 9 capacitors (C3 has no nets)");
        
        // Verify specific classifications
        let c1_class = classifications.iter().find(|c| c.component_ref == "C1");
        assert!(c1_class.is_some() && c1_class.unwrap().function == CapacitorFunction::Timing,
                "C1 should be Timing");
        
        let c2_class = classifications.iter().find(|c| c.component_ref == "C2");
        assert!(c2_class.is_some() && c2_class.unwrap().function == CapacitorFunction::Filtering,
                "C2 should be Filtering");
        
        // C3 should not be classified (no nets)
        let c3_class = classifications.iter().find(|c| c.component_ref == "C3");
        assert!(c3_class.is_none(), "C3 should not be classified (no nets)");
        
        let c4_class = classifications.iter().find(|c| c.component_ref == "C4");
        assert!(c4_class.is_some() && c4_class.unwrap().function == CapacitorFunction::Decoupling,
                "C4 should be Decoupling");
        
        let c5_class = classifications.iter().find(|c| c.component_ref == "C5");
        assert!(c5_class.is_some() && c5_class.unwrap().function == CapacitorFunction::Decoupling,
                "C5 should be Decoupling");
        
        let c7_class = classifications.iter().find(|c| c.component_ref == "C7");
        assert!(c7_class.is_some() && c7_class.unwrap().function == CapacitorFunction::Bulk,
                "C7 should be Bulk");
        
        println!("✓ All 10 capacitors processed correctly!");
        println!("  - C1 (22pF near crystal): {:?}", c1_class.map(|c| c.function));
        println!("  - C2 (100nF signal-GND): {:?}", c2_class.map(|c| c.function));
        println!("  - C3 (0.1µF no nets): Not classified");
        println!("  - C4, C5 (0.1µF VCC-GND): {:?}, {:?}", 
                 c4_class.map(|c| c.function), c5_class.map(|c| c.function));
    }
}