wavetools 0.1.2

Command-line tools for digital simulation waveform analysis -- read, filter, and diff FST and VCD files
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
//------------------------------------------------------------------------------
// diff_test.rs
// Tests for waveform diffing
//
// SPDX-FileCopyrightText: Hudson River Trading
// SPDX-License-Identifier: MIT
//------------------------------------------------------------------------------

use wavetools::{compare_signal_meta, compare_signal_names, diff_waves, open_and_read_waves, NameOptions};

// Helper to check signal name differences
fn check_signal_names(file1: &str, file2: &str) -> (bool, String) {
    let name_options = NameOptions::default();
    let (_reader1, hier1, _reader2, hier2) =
        open_and_read_waves(file1, file2, &name_options)
            .expect("Failed to open wave files");

    let (only_in_1, only_in_2) = compare_signal_names(&hier1, &hier2);

    let has_differences = !only_in_1.is_empty() || !only_in_2.is_empty();
    let mut msg = String::new();
    if has_differences {
        if !only_in_1.is_empty() {
            msg.push_str(&format!("Only in {}: {:?}\n", file1, only_in_1));
        }
        if !only_in_2.is_empty() {
            msg.push_str(&format!("Only in {}: {:?}\n", file2, only_in_2));
        }
    }
    (has_differences, msg)
}

#[test]
fn test_diff_identical_files() {
    let (has_diff, output) = run_wave_diff_test(
        "tests/data/counter.fst",
        "tests/data/counter.fst",
    );
    assert!(!has_diff, "Identical files should have no differences");
    assert_eq!(output.len(), 0, "No output expected for identical files");
}

#[test]
fn test_diff_end_time() {
    let (has_diff, output) = run_wave_diff_test(
        "tests/data/counter.fst",
        "tests/data/counter.end_time.diff.fst",
    );
    assert!(has_diff, "counter.end_time.diff.fst should differ from counter.fst");

    let expected = "\
50 t.clk 1 (missing time in file2)
";
    assert_eq!(output, expected, "Expected exact diff output");
}

// Tests for files that should NOT differ from counter.fst

#[test]
fn test_change_reorder_no_diff() {
    let (has_diff, output) = run_wave_diff_test(
        "tests/data/counter.fst",
        "tests/data/counter.change_reorder.no_diff.fst",
    );
    assert!(!has_diff, "counter.change_reorder.no_diff.fst should not differ. Output:\n{}", output);
}

#[test]
fn test_identifier_no_diff() {
    let (has_diff, output) = run_wave_diff_test(
        "tests/data/counter.fst",
        "tests/data/counter.identifier.no_diff.fst",
    );
    assert!(!has_diff, "counter.identifier.no_diff.fst should not differ. Output:\n{}", output);
}

#[test]
fn test_scope_move_no_diff() {
    let (has_diff, output) = run_wave_diff_test(
        "tests/data/counter.fst",
        "tests/data/counter.scope_move.no_diff.fst",
    );
    assert!(!has_diff, "counter.scope_move.no_diff.fst should not differ. Output:\n{}", output);
}

#[test]
fn test_time_no_diff() {
    let (has_diff, output) = run_wave_diff_test(
        "tests/data/counter.fst",
        "tests/data/counter.time.no_diff.fst",
    );
    assert!(!has_diff, "counter.time.no_diff.fst should not differ. Output:\n{}", output);
}

#[test]
fn test_var_reorder_no_diff() {
    let (has_diff, output) = run_wave_diff_test(
        "tests/data/counter.fst",
        "tests/data/counter.var_reorder.no_diff.fst",
    );
    assert!(!has_diff, "counter.var_reorder.no_diff.fst should not differ. Output:\n{}", output);
}

#[test]
fn test_shared_handle_no_diff() {
    let (has_diff, output) = run_wave_diff_test(
        "tests/data/counter.fst",
        "tests/data/counter.shared_handle.no_diff.fst",
    );
    assert!(!has_diff, "counter.shared_handle.no_diff.fst should not differ. Output:\n{}", output);
}

#[test]
fn test_shared_handle_reverse_no_diff() {
    let (has_diff, output) = run_wave_diff_test(
        "tests/data/counter.shared_handle.no_diff.fst",
        "tests/data/counter.fst",
    );
    assert!(!has_diff, "counter.fst should not differ when compared in reverse. Output:\n{}", output);
}

// Tests for files that SHOULD differ from counter.fst

#[test]
fn test_edge_time_diff() {
    let (has_diff, output) = run_wave_diff_test(
        "tests/data/counter.fst",
        "tests/data/counter.edge_time.diff.fst",
    );
    assert!(has_diff, "counter.edge_time.diff.fst should differ from counter.fst");

    // The only difference should be: time 20 in FST1 vs time 21 in FST2
    let expected = "\
20 t.clk 0 (missing time in file2)
21 t.clk 0 (only in file2)
";
    assert_eq!(output, expected, "Expected exact diff output");
}

#[test]
fn test_new_sig_diff() {
    let (has_name_diff, msg) = check_signal_names(
        "tests/data/counter.fst",
        "tests/data/counter.new_sig.diff.fst",
    );
    assert!(has_name_diff, "counter.new_sig.diff.fst should have different signal names");

    let expected = "\
Only in tests/data/counter.new_sig.diff.fst: {\"t.the_sub.new_sig\"}
";
    assert_eq!(msg, expected, "Expected exact signal name difference");
}

#[test]
fn test_sig_name_diff() {
    let (has_name_diff, msg) = check_signal_names(
        "tests/data/counter.fst",
        "tests/data/counter.sig_name.diff.fst",
    );
    assert!(has_name_diff, "counter.sig_name.diff.fst should have different signal names");

    let expected = "\
Only in tests/data/counter.fst: {\"t.the_sub.cyc_plus_one\"}
Only in tests/data/counter.sig_name.diff.fst: {\"t.the_sub.blargh\"}
";
    assert_eq!(msg, expected, "Expected exact signal name difference");
}

#[test]
fn test_value_diff() {
    let (has_diff, output) = run_wave_diff_test(
        "tests/data/counter.fst",
        "tests/data/counter.value.diff.fst",
    );
    assert!(has_diff, "counter.value.diff.fst should differ from counter.fst");

    let expected = "\
10 t.the_sub.cyc_plus_one 00000000000000000000000000000010 != 00000000000000000000000000000100
";
    assert_eq!(output, expected, "Expected exact diff output");
}

// -- VCD and cross-format tests -----------------------------------------------

fn run_wave_diff_test(file1: &str, file2: &str) -> (bool, String) {
    let name_options = NameOptions::default();
    let (reader1, hier1, reader2, hier2) =
        open_and_read_waves(file1, file2, &name_options)
            .expect("Failed to open wave files");

    let mut output = Vec::new();
    let has_differences = diff_waves(
        &mut output,
        reader1,
        &hier1,
        reader2,
        &hier2,
        0,
        None,
        None,
    )
    .expect("Failed to diff files");

    let output_str = String::from_utf8(output).expect("Invalid UTF-8");
    (has_differences, output_str)
}

#[test]
fn test_diff_vcd_identical() {
    let (has_diff, output) =
        run_wave_diff_test("tests/data/counter.vcd", "tests/data/counter.vcd");
    assert!(!has_diff, "Identical VCD files should have no differences");
    assert_eq!(output.len(), 0);
}

#[test]
fn test_diff_cross_format_fst_vcd() {
    let (has_diff, output) =
        run_wave_diff_test("tests/data/counter.fst", "tests/data/counter.vcd");
    assert!(!has_diff, "FST and equivalent VCD should have no differences. Output:\n{}", output);
}

#[test]
fn test_diff_cross_format_vcd_fst() {
    let (has_diff, output) =
        run_wave_diff_test("tests/data/counter.vcd", "tests/data/counter.fst");
    assert!(!has_diff, "VCD and equivalent FST should have no differences. Output:\n{}", output);
}

#[test]
fn test_diff_vcd_value_diff() {
    let (has_diff, output) = run_wave_diff_test(
        "tests/data/counter.vcd",
        "tests/data/counter.value.diff.vcd",
    );
    assert!(has_diff, "counter.value.diff.vcd should differ from counter.vcd");
    let expected = "\
10 t.the_sub.cyc_plus_one 00000000000000000000000000000010 != 00000000000000000000000000000100
";
    assert_eq!(output, expected, "Expected exact diff output for VCD value diff");
}

#[test]
fn test_diff_vcd_end_time() {
    let (has_diff, output) = run_wave_diff_test(
        "tests/data/counter.vcd",
        "tests/data/counter.end_time.diff.vcd",
    );
    assert!(has_diff, "counter.end_time.diff.vcd should differ from counter.vcd");
    let expected = "\
50 t.clk 1 (missing time in file2)
";
    assert_eq!(output, expected, "Expected exact diff output for VCD end time diff");
}

// -- Real epsilon tests -------------------------------------------------------

fn run_wave_diff_test_with_epsilon(
    file1: &str,
    file2: &str,
    real_epsilon: Option<f64>,
) -> (bool, String) {
    let name_options = NameOptions::default();
    let (reader1, hier1, reader2, hier2) =
        open_and_read_waves(file1, file2, &name_options)
            .expect("Failed to open wave files");

    let mut output = Vec::new();
    let has_differences = diff_waves(
        &mut output,
        reader1,
        &hier1,
        reader2,
        &hier2,
        0,
        None,
        real_epsilon,
    )
    .expect("Failed to diff files");

    let output_str = String::from_utf8(output).expect("Invalid UTF-8");
    (has_differences, output_str)
}

#[test]
fn test_diff_real_no_epsilon_reports_diff() {
    let (has_diff, output) = run_wave_diff_test_with_epsilon(
        "tests/data/real_base.vcd",
        "tests/data/real_close.vcd",
        None,
    );
    assert!(has_diff, "Without epsilon, close real values should differ. Output:\n{}", output);
}

#[test]
fn test_diff_real_within_epsilon_no_diff() {
    let (has_diff, output) = run_wave_diff_test_with_epsilon(
        "tests/data/real_base.vcd",
        "tests/data/real_close.vcd",
        Some(0.001),
    );
    assert!(!has_diff, "Within epsilon, close real values should not differ. Output:\n{}", output);
}

#[test]
fn test_diff_real_outside_epsilon_reports_diff() {
    let (has_diff, output) = run_wave_diff_test_with_epsilon(
        "tests/data/real_base.vcd",
        "tests/data/real_far.vcd",
        Some(0.001),
    );
    assert!(has_diff, "Outside epsilon, far real values should differ. Output:\n{}", output);
}

#[test]
fn test_diff_real_large_epsilon_no_diff() {
    let (has_diff, output) = run_wave_diff_test_with_epsilon(
        "tests/data/real_base.vcd",
        "tests/data/real_far.vcd",
        Some(1.0),
    );
    assert!(!has_diff, "With large epsilon, even far real values should not differ. Output:\n{}", output);
}

// -- VCD id code aliasing tests -----------------------------------------------
// Reproducer for a vcddiff bug: when one file aliases signals (multiple signals
// sharing the same VCD id code) and the other assigns unique ids, vcddiff's
// per-code mapping array overwrites earlier entries, causing "Never found"
// false positives.  wavediff matches by signal name and handles this correctly.

#[test]
fn test_diff_vcd_aliased_idcodes_no_diff() {
    // idcode_a.vcd: signals a,b (code !) and c,d (code ") share ids across scopes
    // idcode_b.vcd: every signal gets a unique id (0-4)
    // Signal names and values are identical -- only the id codes differ.
    let (has_diff, output) = run_wave_diff_test(
        "tests/data/idcode_a.vcd",
        "tests/data/idcode_b.vcd",
    );
    assert!(!has_diff, "Files with aliased vs unique VCD id codes should not differ. Output:\n{}", output);
}

#[test]
fn test_diff_vcd_aliased_idcodes_reverse_no_diff() {
    let (has_diff, output) = run_wave_diff_test(
        "tests/data/idcode_b.vcd",
        "tests/data/idcode_a.vcd",
    );
    assert!(!has_diff, "Reversed aliased vs unique VCD id codes should not differ. Output:\n{}", output);
}

// -- Time range filtering tests -----------------------------------------------

fn run_wave_diff_test_with_range(
    file1: &str,
    file2: &str,
    start: u64,
    end: Option<u64>,
) -> (bool, String) {
    let name_options = NameOptions::default();
    let (reader1, hier1, reader2, hier2) =
        open_and_read_waves(file1, file2, &name_options)
            .expect("Failed to open wave files");

    let mut output = Vec::new();
    let has_differences = diff_waves(
        &mut output,
        reader1,
        &hier1,
        reader2,
        &hier2,
        start,
        end,
        None,
    )
    .expect("Failed to diff files");

    let output_str = String::from_utf8(output).expect("Invalid UTF-8");
    (has_differences, output_str)
}

#[test]
fn test_diff_start_skips_early_difference() {
    // counter.value.diff differs only at time 10
    let (has_diff, output) = run_wave_diff_test_with_range(
        "tests/data/counter.fst",
        "tests/data/counter.value.diff.fst",
        20,
        None,
    );
    assert!(!has_diff, "Starting at time 20 should skip the time-10 difference. Output:\n{}", output);
}

#[test]
fn test_diff_end_skips_late_difference() {
    // counter.end_time.diff differs only at time 50
    let (has_diff, output) = run_wave_diff_test_with_range(
        "tests/data/counter.fst",
        "tests/data/counter.end_time.diff.fst",
        0,
        Some(40),
    );
    assert!(!has_diff, "Ending at time 40 should skip the time-50 difference. Output:\n{}", output);
}

#[test]
fn test_diff_start_and_end_skip_difference() {
    // counter.edge_time.diff differs at times 20 and 21
    let (has_diff, output) = run_wave_diff_test_with_range(
        "tests/data/counter.fst",
        "tests/data/counter.edge_time.diff.fst",
        30,
        Some(50),
    );
    assert!(!has_diff, "Range 30-50 should skip differences at times 20-21. Output:\n{}", output);
}

#[test]
fn test_diff_start_beyond_all_data() {
    let (has_diff, output) = run_wave_diff_test_with_range(
        "tests/data/counter.fst",
        "tests/data/counter.value.diff.fst",
        1000,
        None,
    );
    assert!(!has_diff, "Starting beyond all data should show no differences. Output:\n{}", output);
}

#[test]
fn test_diff_vcd_start_skips_early_difference() {
    let (has_diff, output) = run_wave_diff_test_with_range(
        "tests/data/counter.vcd",
        "tests/data/counter.value.diff.vcd",
        20,
        None,
    );
    assert!(!has_diff, "VCD: starting at time 20 should skip the time-10 difference. Output:\n{}", output);
}

#[test]
fn test_diff_vcd_end_skips_late_difference() {
    let (has_diff, output) = run_wave_diff_test_with_range(
        "tests/data/counter.vcd",
        "tests/data/counter.end_time.diff.vcd",
        0,
        Some(40),
    );
    assert!(!has_diff, "VCD: ending at time 40 should skip the time-50 difference. Output:\n{}", output);
}

// -- Additional epsilon edge cases --------------------------------------------

#[test]
fn test_diff_zero_epsilon() {
    // Zero epsilon should require exact match, same as no epsilon
    let (has_diff, output) = run_wave_diff_test_with_epsilon(
        "tests/data/real_base.vcd",
        "tests/data/real_close.vcd",
        Some(0.0),
    );
    assert!(has_diff, "Zero epsilon should require exact match. Output:\n{}", output);
}

#[test]
fn test_diff_epsilon_wide_bitvector_no_false_positive() {
    // A 512-bit all-1s value parses as a float, but identical bit-vectors
    // must not be reported as different just because --epsilon is set.
    let (has_diff, output) = run_wave_diff_test_with_epsilon(
        "tests/data/wide_bits.vcd",
        "tests/data/wide_bits.vcd",
        Some(0.0000001),
    );
    assert!(!has_diff, "Identical wide bit-vectors should not differ with epsilon. Output:\n{}", output);
}

// -- Metadata comparison tests ------------------------------------------------

#[test]
fn test_diff_type_mismatch() {
    let name_options = NameOptions::default();
    let (_r1, hier1, _r2, hier2) =
        open_and_read_waves(
            "tests/data/type_mismatch.a.vcd",
            "tests/data/type_mismatch.b.vcd",
            &name_options,
        )
        .expect("Failed to open wave files");

    let diffs = compare_signal_meta(&hier1, &hier2);
    assert!(!diffs.is_empty(), "Should detect type mismatches");

    // clk: wire vs reg
    assert!(
        diffs.iter().any(|d| d.contains("top.clk") && d.contains("wire") && d.contains("reg")),
        "Should detect clk type mismatch: {:?}",
        diffs
    );
    // state: wire vs reg
    assert!(
        diffs.iter().any(|d| d.contains("top.state") && d.contains("wire") && d.contains("reg")),
        "Should detect state type mismatch: {:?}",
        diffs
    );
}

#[test]
fn test_diff_size_mismatch() {
    let name_options = NameOptions::default();
    let (_r1, hier1, _r2, hier2) =
        open_and_read_waves(
            "tests/data/type_mismatch.a.vcd",
            "tests/data/type_mismatch.b.vcd",
            &name_options,
        )
        .expect("Failed to open wave files");

    let diffs = compare_signal_meta(&hier1, &hier2);

    // data: size 8 vs 16
    assert!(
        diffs.iter().any(|d| d.contains("top.data") && d.contains("8") && d.contains("16")),
        "Should detect data size mismatch: {:?}",
        diffs
    );
}

#[test]
fn test_diff_identical_metadata() {
    let name_options = NameOptions::default();
    let (_r1, hier1, _r2, hier2) =
        open_and_read_waves(
            "tests/data/type_mismatch.a.vcd",
            "tests/data/type_mismatch.a.vcd",
            &name_options,
        )
        .expect("Failed to open wave files");

    let diffs = compare_signal_meta(&hier1, &hier2);
    assert!(diffs.is_empty(), "Same file should have no metadata diffs: {:?}", diffs);
}

#[test]
fn test_diff_cross_format_metadata() {
    // FST and VCD of the same design may have different var types (FST preserves
    // original types like "reg"/"integer" while VCD might use "wire"). Direction
    // comparison should be skipped since VCD has no direction info ("implicit").
    let name_options = NameOptions::default();
    let (_r1, hier1, _r2, hier2) =
        open_and_read_waves(
            "tests/data/counter.fst",
            "tests/data/counter.vcd",
            &name_options,
        )
        .expect("Failed to open wave files");

    let diffs = compare_signal_meta(&hier1, &hier2);
    // Direction diffs should NOT appear since VCD direction is "implicit"
    assert!(
        !diffs.iter().any(|d| d.contains("direction")),
        "Should not report direction diffs when VCD side is implicit: {:?}",
        diffs
    );
}

// -- Attribute comparison tests -----------------------------------------------

#[test]
fn test_diff_enum_attr_difference() {
    // Same signal names/types/values, but different enum table attributes:
    //   a: state has enum state_t (IDLE/ACTIVE/DONE)
    //   b: state has enum alt_state_t (OFF/ON/ERR)
    let name_options = NameOptions::default();
    let (_r1, hier1, _r2, hier2) =
        open_and_read_waves(
            "tests/data/enum_attrs.a.vcd",
            "tests/data/enum_attrs.b.vcd",
            &name_options,
        )
        .expect("Failed to open wave files");

    let diffs = compare_signal_meta(&hier1, &hier2);
    assert!(
        diffs.iter().any(|d| d.contains("top.state")),
        "Should detect enum attribute difference on top.state: {:?}",
        diffs
    );
}

#[test]
fn test_diff_misc_attr_difference() {
    // Same signal names/types/values, but different misc attributes:
    //   a: data has source path /path/to/source.v
    //   b: data has source path /different/path.v
    let name_options = NameOptions::default();
    let (_r1, hier1, _r2, hier2) =
        open_and_read_waves(
            "tests/data/enum_attrs.a.vcd",
            "tests/data/enum_attrs.b.vcd",
            &name_options,
        )
        .expect("Failed to open wave files");

    let diffs = compare_signal_meta(&hier1, &hier2);
    assert!(
        diffs.iter().any(|d| d.contains("top.data")),
        "Should detect misc attribute difference on top.data: {:?}",
        diffs
    );
}

#[test]
fn test_diff_attr_present_vs_absent() {
    // a: state has enum attr, data has source path attr
    // missing: no attrs on any signal
    let name_options = NameOptions::default();
    let (_r1, hier1, _r2, hier2) =
        open_and_read_waves(
            "tests/data/enum_attrs.a.vcd",
            "tests/data/enum_attrs.missing.vcd",
            &name_options,
        )
        .expect("Failed to open wave files");

    let diffs = compare_signal_meta(&hier1, &hier2);
    assert!(
        diffs.iter().any(|d| d.contains("top.state")),
        "Should detect missing enum attr on top.state: {:?}",
        diffs
    );
    assert!(
        diffs.iter().any(|d| d.contains("top.data")),
        "Should detect missing source attr on top.data: {:?}",
        diffs
    );
}

#[test]
fn test_diff_identical_attrs_no_diff() {
    // Same file compared to itself -- no attr differences
    let name_options = NameOptions::default();
    let (_r1, hier1, _r2, hier2) =
        open_and_read_waves(
            "tests/data/enum_attrs.a.vcd",
            "tests/data/enum_attrs.a.vcd",
            &name_options,
        )
        .expect("Failed to open wave files");

    let diffs = compare_signal_meta(&hier1, &hier2);
    assert!(diffs.is_empty(), "Same file should have no diffs: {:?}", diffs);
}

#[test]
fn test_diff_real_size_normalized_across_formats() {
    // FST stores real signal sizes in bytes (8), VCD in bits (64).
    // After normalization both should report 64 -- no size mismatch.
    let name_options = NameOptions::default();
    let (_r1, hier1, _r2, hier2) =
        open_and_read_waves(
            "tests/data/real_base.fst",
            "tests/data/real_base.vcd",
            &name_options,
        )
        .expect("Failed to open wave files");

    let diffs = compare_signal_meta(&hier1, &hier2);
    assert!(
        diffs.is_empty(),
        "FST and VCD of the same design should have no metadata diffs: {:?}",
        diffs
    );
}

// -- --no-attrs CLI tests -----------------------------------------------------

fn run_wavecat_cli(file: &str, extra_args: &[&str]) -> std::process::Output {
    let bin = env!("CARGO_BIN_EXE_wavecat");
    std::process::Command::new(bin)
        .args(extra_args)
        .arg(file)
        .output()
        .expect("Failed to run wavecat")
}

fn run_wavediff_cli(file1: &str, file2: &str, extra_args: &[&str]) -> std::process::Output {
    let bin = env!("CARGO_BIN_EXE_wavediff");
    std::process::Command::new(bin)
        .args(extra_args)
        .arg(file1)
        .arg(file2)
        .output()
        .expect("Failed to run wavediff")
}

#[test]
fn test_cli_attr_diff_nonzero_exit() {
    // Different attrs, same values -- should exit 1
    let output = run_wavediff_cli(
        "tests/data/enum_attrs.a.vcd",
        "tests/data/enum_attrs.b.vcd",
        &[],
    );
    assert_eq!(output.status.code(), Some(1), "Attr diffs should cause exit 1");
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("top.state"), "stderr should mention top.state: {}", stderr);
    assert!(stderr.contains("top.data"), "stderr should mention top.data: {}", stderr);
}

#[test]
fn test_cli_no_attrs_ignores_attr_diff() {
    // Different attrs, same values -- --no-attrs should make it exit 0
    let output = run_wavediff_cli(
        "tests/data/enum_attrs.a.vcd",
        "tests/data/enum_attrs.b.vcd",
        &["--no-attrs"],
    );
    assert_eq!(
        output.status.code(),
        Some(0),
        "--no-attrs should ignore attr differences. stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn test_cli_no_attrs_ignores_missing_attrs() {
    // Attrs in one file, none in the other -- --no-attrs should exit 0
    let output = run_wavediff_cli(
        "tests/data/enum_attrs.a.vcd",
        "tests/data/enum_attrs.missing.vcd",
        &["--no-attrs"],
    );
    assert_eq!(
        output.status.code(),
        Some(0),
        "--no-attrs should ignore missing attrs. stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn test_cli_no_attrs_still_detects_value_diffs() {
    // --no-attrs skips metadata but should still catch value differences
    let output = run_wavediff_cli(
        "tests/data/counter.vcd",
        "tests/data/counter.value.diff.vcd",
        &["--no-attrs"],
    );
    assert_eq!(
        output.status.code(),
        Some(1),
        "--no-attrs should still detect value diffs"
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("cyc_plus_one"),
        "--no-attrs stdout should contain value diff: {}",
        stdout
    );
}

// -- Enum conflict detection tests --------------------------------------------

#[test]
fn test_enum_conflict_errors_on_open() {
    let name_options = NameOptions::default();
    let result = wavetools::open_wave_file(
        std::path::Path::new("tests/data/error/enum_conflict.vcd"),
        &name_options,
    );
    assert!(result.is_err(), "Conflicting enum definitions should error");
    let err = result.err().unwrap();
    assert!(
        err.contains("conflicting enum definitions") && err.contains("$unit::state_t"),
        "Error should mention the conflicting enum name: {}",
        err
    );
}

#[test]
fn test_enum_no_conflict_succeeds() {
    let name_options = NameOptions::default();
    let result = wavetools::open_wave_file(
        std::path::Path::new("tests/data/enum_no_conflict.vcd"),
        &name_options,
    );
    assert!(
        result.is_ok(),
        "Non-conflicting duplicate enum definitions should succeed: {:?}",
        result.err()
    );
}

#[test]
fn test_cli_wavecat_enum_conflict_exits_with_error() {
    let output = run_wavecat_cli("tests/data/error/enum_conflict.vcd", &["--names"]);
    assert_eq!(
        output.status.code(),
        Some(1),
        "wavecat should exit 1 on enum conflict"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("conflicting enum definitions"),
        "wavecat stderr should report conflict: {}",
        stderr
    );
}

#[test]
fn test_cli_wavediff_enum_conflict_exits_with_error() {
    let output = run_wavediff_cli(
        "tests/data/error/enum_conflict.vcd",
        "tests/data/enum_no_conflict.vcd",
        &[],
    );
    assert_eq!(
        output.status.code(),
        Some(2),
        "wavediff should exit 2 on enum conflict"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("conflicting enum definitions"),
        "wavediff stderr should report conflict: {}",
        stderr
    );
}

#[test]
fn test_non_qualified_duplicate_enums_no_conflict() {
    // Enum names without "::" are not checked for conflicts -- they are local
    // definitions that can legitimately differ across scopes.
    let name_options = NameOptions::default();
    let result = wavetools::open_wave_file(
        std::path::Path::new("tests/data/enum_attrs.a.vcd"),
        &name_options,
    );
    assert!(
        result.is_ok(),
        "Non-qualified enum names should never trigger conflict detection: {:?}",
        result.err()
    );
}