perl-corpus 0.15.2

Test corpus management and generators for Perl parsers
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
use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::fs;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ExpectationMode {
    ParseClean,
    RecoverWithoutPanic,
    ExpectedError,
    TokenOnly,
    SpanOnly,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SidecarConcept {
    pub id: String,
    pub tier: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SidecarExpect {
    pub panic: bool,
    pub timeout: bool,
    pub mode: ExpectationMode,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SidecarMetrics {
    pub max_error_nodes: Option<u32>,
    pub must_emit_node_kinds: Option<Vec<String>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SidecarSnapshots {
    pub tokens: bool,
    pub ast: bool,
    pub spans: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct FixtureExpectationSidecar {
    pub concept: SidecarConcept,
    pub expect: SidecarExpect,
    pub metrics: Option<SidecarMetrics>,
    pub snapshots: Option<SidecarSnapshots>,
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SidecarValidation {
    pub errors: Vec<String>,
    pub warnings: Vec<String>,
}

impl SidecarValidation {
    pub fn is_ok(&self) -> bool {
        self.errors.is_empty()
    }
}

#[derive(Debug, Clone)]
pub struct ConceptRegistry {
    concept_ids: HashSet<String>,
}

impl ConceptRegistry {
    pub fn load(path: &Path) -> Result<Self> {
        let raw = fs::read_to_string(path)
            .with_context(|| format!("reading concept registry {}", path.display()))?;
        let value = toml::from_str::<toml::Value>(&raw)
            .with_context(|| format!("parsing concept registry TOML {}", path.display()))?;

        let mut concept_ids = HashSet::new();
        collect_concept_ids(&value, &mut concept_ids);

        Ok(Self { concept_ids })
    }

    pub fn contains(&self, concept_id: &str) -> bool {
        self.concept_ids.contains(concept_id)
    }
}

pub fn parse_sidecar(path: &Path) -> Result<FixtureExpectationSidecar> {
    let raw =
        fs::read_to_string(path).with_context(|| format!("reading sidecar {}", path.display()))?;
    toml::from_str::<toml::Value>(&raw)
        .with_context(|| format!("parsing sidecar TOML {}", path.display()))?;
    let parsed: FixtureExpectationSidecar = toml::from_str(&raw)
        .with_context(|| format!("deserializing sidecar {}", path.display()))?;
    Ok(parsed)
}

pub fn expected_fixture_path(sidecar_path: &Path) -> Result<PathBuf> {
    let file_name = sidecar_path
        .file_name()
        .and_then(|name| name.to_str())
        .ok_or_else(|| anyhow::anyhow!("invalid sidecar path: {}", sidecar_path.display()))?;

    if !file_name.ends_with(".meta.toml") {
        bail!("sidecar filename must end with .meta.toml: {}", sidecar_path.display());
    }

    let fixture_stem = file_name.trim_end_matches(".meta.toml");
    if fixture_stem.is_empty() {
        bail!("fixture stem must not be empty: {}", sidecar_path.display());
    }

    let fixture_name = fixture_stem.to_string() + ".pl";
    let parent = sidecar_path.parent().unwrap_or_else(|| Path::new("."));
    Ok(parent.join(fixture_name))
}

pub fn validate_sidecar(
    sidecar_path: &Path,
    sidecar: &FixtureExpectationSidecar,
    concept_registry: Option<&ConceptRegistry>,
) -> SidecarValidation {
    let mut validation = SidecarValidation::default();

    match expected_fixture_path(sidecar_path) {
        Ok(fixture_path) => {
            if !fixture_path.exists() {
                validation
                    .errors
                    .push(format!("fixture file does not exist: {}", fixture_path.display()));
            }
        }
        Err(error) => validation.errors.push(error.to_string()),
    }

    if sidecar.concept.id.trim().is_empty() {
        validation.errors.push("concept.id must not be empty".to_string());
    } else if let Some(registry) = concept_registry {
        if !registry.contains(&sidecar.concept.id) {
            validation.errors.push(format!(
                "concept.id '{}' is not present in the loaded concept registry",
                sidecar.concept.id
            ));
        }
    } else {
        validation.warnings.push(format!(
            "concept registry unavailable; concept resolution pending for '{}'",
            sidecar.concept.id
        ));
    }

    validation
}

pub fn load_and_validate_sidecar(
    sidecar_path: &Path,
    concept_registry: Option<&ConceptRegistry>,
) -> Result<SidecarValidation> {
    let sidecar = parse_sidecar(sidecar_path)?;
    Ok(validate_sidecar(sidecar_path, &sidecar, concept_registry))
}

pub fn discover_sidecars(root: &Path) -> Result<Vec<PathBuf>> {
    let mut sidecars = Vec::new();
    if !root.exists() {
        return Ok(sidecars);
    }

    let mut stack = vec![root.to_path_buf()];
    while let Some(dir) = stack.pop() {
        let entries =
            fs::read_dir(&dir).with_context(|| format!("reading directory {}", dir.display()))?;
        for entry in entries {
            let entry = entry.with_context(|| format!("reading entry in {}", dir.display()))?;
            let path = entry.path();
            let file_type = entry
                .file_type()
                .with_context(|| format!("getting file type for {}", path.display()))?;

            if file_type.is_dir() {
                stack.push(path);
                continue;
            }

            if file_type.is_file() && path.to_string_lossy().ends_with(".meta.toml") {
                sidecars.push(path);
            }
        }
    }

    sidecars.sort();
    Ok(sidecars)
}

fn collect_concept_ids(value: &toml::Value, concept_ids: &mut HashSet<String>) {
    match value {
        toml::Value::Table(table) => {
            for (key, item) in table {
                if key == "id"
                    && let toml::Value::String(id) = item
                    && !id.trim().is_empty()
                {
                    concept_ids.insert(id.clone());
                }
                collect_concept_ids(item, concept_ids);
            }
        }
        toml::Value::Array(items) => {
            for item in items {
                collect_concept_ids(item, concept_ids);
            }
        }
        _ => {}
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::path::Path;

    // helpers

    fn minimal_sidecar_toml(id: &str, mode: &str) -> String {
        format!(
            r#"
[concept]
id = "{id}"
tier = "pr"

[expect]
panic = false
timeout = false
mode = "{mode}"
"#
        )
    }

    fn write_temp_file(dir: &std::path::Path, name: &str, contents: &str) -> Result<PathBuf> {
        let path = dir.join(name);
        fs::write(&path, contents)?;
        Ok(path)
    }

    // ExpectationMode deserialization

    #[test]
    fn expectation_mode_rejects_unknown_value() {
        let raw = r#"
[concept]
id = "parser.recovery.missing"
tier = "pr"

[expect]
panic = false
timeout = false
mode = "mystery"
"#;

        let parsed = toml::from_str::<FixtureExpectationSidecar>(raw);
        assert!(parsed.is_err());
    }

    #[test]
    fn expectation_mode_parse_clean_roundtrips() -> Result<()> {
        let raw = minimal_sidecar_toml("some.concept", "parse_clean");
        let sidecar: FixtureExpectationSidecar = toml::from_str(&raw)?;
        assert_eq!(sidecar.expect.mode, ExpectationMode::ParseClean);
        Ok(())
    }

    #[test]
    fn expectation_mode_recover_without_panic_roundtrips() -> Result<()> {
        let raw = minimal_sidecar_toml("some.concept", "recover_without_panic");
        let sidecar: FixtureExpectationSidecar = toml::from_str(&raw)?;
        assert_eq!(sidecar.expect.mode, ExpectationMode::RecoverWithoutPanic);
        Ok(())
    }

    #[test]
    fn expectation_mode_expected_error_roundtrips() -> Result<()> {
        let raw = minimal_sidecar_toml("some.concept", "expected_error");
        let sidecar: FixtureExpectationSidecar = toml::from_str(&raw)?;
        assert_eq!(sidecar.expect.mode, ExpectationMode::ExpectedError);
        Ok(())
    }

    #[test]
    fn expectation_mode_token_only_roundtrips() -> Result<()> {
        let raw = minimal_sidecar_toml("some.concept", "token_only");
        let sidecar: FixtureExpectationSidecar = toml::from_str(&raw)?;
        assert_eq!(sidecar.expect.mode, ExpectationMode::TokenOnly);
        Ok(())
    }

    #[test]
    fn expectation_mode_span_only_roundtrips() -> Result<()> {
        let raw = minimal_sidecar_toml("some.concept", "span_only");
        let sidecar: FixtureExpectationSidecar = toml::from_str(&raw)?;
        assert_eq!(sidecar.expect.mode, ExpectationMode::SpanOnly);
        Ok(())
    }

    // SidecarValidation::is_ok

    #[test]
    fn sidecar_validation_is_ok_when_no_errors() {
        let v = SidecarValidation::default();
        assert!(v.is_ok());
    }

    #[test]
    fn sidecar_validation_is_not_ok_when_errors_present() {
        let mut v = SidecarValidation::default();
        v.errors.push("something went wrong".to_string());
        assert!(!v.is_ok());
    }

    #[test]
    fn sidecar_validation_ok_with_warnings_but_no_errors() {
        let mut v = SidecarValidation::default();
        v.warnings.push("just a warning".to_string());
        assert!(v.is_ok(), "warnings alone do not make validation fail");
    }

    // expected_fixture_path

    #[test]
    fn expected_fixture_path_rejects_empty_fixture_stem() {
        let result = expected_fixture_path(Path::new(".meta.toml"));
        assert!(result.is_err(), "empty fixture stem should be rejected");
        let error = result.err().map(|err| err.to_string()).unwrap_or_default();
        assert!(error.contains("fixture stem must not be empty"));
    }

    #[test]
    fn expected_fixture_path_resolves_valid_sidecar_name() {
        let result = expected_fixture_path(Path::new("quote_like/delimiter.meta.toml"));
        assert!(result.is_ok(), "valid sidecar name should resolve to fixture path");
        let path = result.ok().unwrap_or_default();
        assert_eq!(path, Path::new("quote_like/delimiter.pl"));
    }

    #[test]
    fn expected_fixture_path_rejects_wrong_extension() {
        let result = expected_fixture_path(Path::new("foo/bar.toml"));
        assert!(result.is_err(), "wrong extension should be rejected");
        let msg = result.err().map(|e| e.to_string()).unwrap_or_default();
        assert!(msg.contains("must end with .meta.toml"));
    }

    #[test]
    fn expected_fixture_path_root_level_sidecar() -> Result<()> {
        let result = expected_fixture_path(Path::new("basic.meta.toml"))?;
        assert_eq!(result, Path::new("basic.pl"));
        Ok(())
    }

    #[test]
    fn expected_fixture_path_deep_nested() -> Result<()> {
        let result = expected_fixture_path(Path::new("a/b/c/foo.meta.toml"))?;
        assert_eq!(result, Path::new("a/b/c/foo.pl"));
        Ok(())
    }

    // parse_sidecar

    #[test]
    fn parse_sidecar_reads_valid_file() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let sidecar_path = write_temp_file(
            dir.path(),
            "foo.meta.toml",
            &minimal_sidecar_toml("my.concept", "parse_clean"),
        )?;
        let sidecar = parse_sidecar(&sidecar_path)?;
        assert_eq!(sidecar.concept.id, "my.concept");
        assert_eq!(sidecar.concept.tier, "pr");
        assert!(!sidecar.expect.panic);
        assert!(!sidecar.expect.timeout);
        assert_eq!(sidecar.expect.mode, ExpectationMode::ParseClean);
        Ok(())
    }

    #[test]
    fn parse_sidecar_fails_on_missing_file() {
        let result = parse_sidecar(Path::new("/nonexistent/path/test.meta.toml"));
        assert!(result.is_err());
        let msg = result.err().map(|e| e.to_string()).unwrap_or_default();
        assert!(msg.contains("reading sidecar"));
    }

    #[test]
    fn parse_sidecar_fails_on_invalid_toml() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let path = write_temp_file(dir.path(), "bad.meta.toml", "not valid toml }{{{")?;
        let result = parse_sidecar(&path);
        assert!(result.is_err());
        Ok(())
    }

    #[test]
    fn parse_sidecar_fails_on_wrong_schema() -> Result<()> {
        let dir = tempfile::tempdir()?;
        // Valid TOML but missing required fields
        let path = write_temp_file(dir.path(), "schema.meta.toml", "[concept]\nid = \"x\"\n")?;
        let result = parse_sidecar(&path);
        assert!(result.is_err());
        Ok(())
    }

    #[test]
    fn parse_sidecar_handles_optional_metrics_and_snapshots() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let full_toml = r#"
[concept]
id = "full.case"
tier = "tier1"

[expect]
panic = false
timeout = true
mode = "parse_clean"

[metrics]
max_error_nodes = 5
must_emit_node_kinds = ["foo", "bar"]

[snapshots]
tokens = true
ast = false
spans = true
"#;
        let path = write_temp_file(dir.path(), "full.meta.toml", full_toml)?;
        let sidecar = parse_sidecar(&path)?;
        assert!(sidecar.expect.timeout);
        let metrics = sidecar.metrics.as_ref().ok_or_else(|| anyhow::anyhow!("missing metrics"))?;
        assert_eq!(metrics.max_error_nodes, Some(5));
        let kinds = metrics
            .must_emit_node_kinds
            .as_ref()
            .ok_or_else(|| anyhow::anyhow!("missing node kinds"))?;
        assert_eq!(kinds, &["foo", "bar"]);
        let snaps =
            sidecar.snapshots.as_ref().ok_or_else(|| anyhow::anyhow!("missing snapshots"))?;
        assert!(snaps.tokens);
        assert!(!snaps.ast);
        assert!(snaps.spans);
        Ok(())
    }

    // validate_sidecar

    fn make_sidecar(id: &str) -> FixtureExpectationSidecar {
        FixtureExpectationSidecar {
            concept: SidecarConcept { id: id.to_string(), tier: "pr".to_string() },
            expect: SidecarExpect {
                panic: false,
                timeout: false,
                mode: ExpectationMode::ParseClean,
            },
            metrics: None,
            snapshots: None,
        }
    }

    #[test]
    fn validate_sidecar_empty_concept_id_produces_error() -> Result<()> {
        let dir = tempfile::tempdir()?;
        // create the .pl fixture so the path check passes
        write_temp_file(dir.path(), "empty.pl", "# perl")?;
        let sidecar_path = dir.path().join("empty.meta.toml");
        let sidecar = make_sidecar("   "); // whitespace-only id
        let v = validate_sidecar(&sidecar_path, &sidecar, None);
        assert!(!v.is_ok());
        assert!(v.errors.iter().any(|e| e.contains("concept.id must not be empty")));
        Ok(())
    }

    #[test]
    fn validate_sidecar_missing_fixture_produces_error() -> Result<()> {
        let dir = tempfile::tempdir()?;
        // do NOT create the .pl file
        let sidecar_path = dir.path().join("missing.meta.toml");
        let sidecar = make_sidecar("some.id");
        let v = validate_sidecar(&sidecar_path, &sidecar, None);
        assert!(!v.is_ok());
        assert!(v.errors.iter().any(|e| e.contains("fixture file does not exist")));
        Ok(())
    }

    #[test]
    fn validate_sidecar_no_registry_emits_warning() -> Result<()> {
        let dir = tempfile::tempdir()?;
        write_temp_file(dir.path(), "warn.pl", "# perl")?;
        let sidecar_path = dir.path().join("warn.meta.toml");
        let sidecar = make_sidecar("concept.pending");
        let v = validate_sidecar(&sidecar_path, &sidecar, None);
        assert!(v.is_ok(), "should have no errors");
        assert!(v.warnings.iter().any(|w| w.contains("concept registry unavailable")));
        assert!(v.warnings.iter().any(|w| w.contains("concept.pending")));
        Ok(())
    }

    #[test]
    fn validate_sidecar_known_concept_is_clean() -> Result<()> {
        let dir = tempfile::tempdir()?;
        write_temp_file(dir.path(), "ok.pl", "# perl")?;
        let sidecar_path = dir.path().join("ok.meta.toml");
        let sidecar = make_sidecar("known.concept");

        // Build a registry with the concept present
        let registry_toml = r#"
[[concepts]]
id = "known.concept"
"#;
        let reg_path = write_temp_file(dir.path(), "concepts.toml", registry_toml)?;
        let registry = ConceptRegistry::load(&reg_path)?;

        let v = validate_sidecar(&sidecar_path, &sidecar, Some(&registry));
        assert!(v.is_ok(), "known concept with fixture should be clean: {:?}", v.errors);
        assert!(v.warnings.is_empty());
        Ok(())
    }

    #[test]
    fn validate_sidecar_unknown_concept_in_registry_produces_error() -> Result<()> {
        let dir = tempfile::tempdir()?;
        write_temp_file(dir.path(), "unknown.pl", "# perl")?;
        let sidecar_path = dir.path().join("unknown.meta.toml");
        let sidecar = make_sidecar("not.in.registry");

        let registry_toml = r#"
[[concepts]]
id = "known.concept"
"#;
        let reg_path = write_temp_file(dir.path(), "concepts.toml", registry_toml)?;
        let registry = ConceptRegistry::load(&reg_path)?;

        let v = validate_sidecar(&sidecar_path, &sidecar, Some(&registry));
        assert!(!v.is_ok());
        assert!(v.errors.iter().any(|e| e.contains("not.in.registry")));
        assert!(v.errors.iter().any(|e| e.contains("not present in the loaded concept registry")));
        Ok(())
    }

    #[test]
    fn validate_sidecar_invalid_sidecar_path_extension_produces_error() -> Result<()> {
        let dir = tempfile::tempdir()?;
        // path has wrong extension, so expected_fixture_path will fail
        let sidecar_path = dir.path().join("foo.toml"); // not .meta.toml
        let sidecar = make_sidecar("some.id");
        let v = validate_sidecar(&sidecar_path, &sidecar, None);
        assert!(!v.is_ok());
        assert!(v.errors.iter().any(|e| e.contains("must end with .meta.toml")));
        Ok(())
    }

    // load_and_validate_sidecar

    #[test]
    fn load_and_validate_sidecar_succeeds_for_valid_file() -> Result<()> {
        let dir = tempfile::tempdir()?;
        write_temp_file(dir.path(), "good.pl", "# perl")?;
        let sidecar_path = write_temp_file(
            dir.path(),
            "good.meta.toml",
            &minimal_sidecar_toml("good.concept", "parse_clean"),
        )?;
        let v = load_and_validate_sidecar(&sidecar_path, None)?;
        // Fixture exists, concept id is not empty, and no registry yields a warning.
        assert!(v.is_ok());
        Ok(())
    }

    #[test]
    fn load_and_validate_sidecar_fails_on_missing_file() {
        let result = load_and_validate_sidecar(Path::new("/nonexistent/ghost.meta.toml"), None);
        assert!(result.is_err());
    }

    // discover_sidecars

    #[test]
    fn discover_sidecars_returns_empty_for_nonexistent_root() -> Result<()> {
        let sidecars = discover_sidecars(Path::new("/this/does/not/exist"))?;
        assert!(sidecars.is_empty());
        Ok(())
    }

    #[test]
    fn discover_sidecars_finds_meta_toml_files() -> Result<()> {
        let dir = tempfile::tempdir()?;
        write_temp_file(dir.path(), "a.meta.toml", "# meta")?;
        write_temp_file(dir.path(), "b.meta.toml", "# meta")?;
        write_temp_file(dir.path(), "not_a_sidecar.txt", "ignored")?;
        write_temp_file(dir.path(), "also_ignored.toml", "ignored")?;

        let sidecars = discover_sidecars(dir.path())?;
        assert_eq!(sidecars.len(), 2);
        assert!(sidecars.iter().any(|p| p.ends_with("a.meta.toml")));
        assert!(sidecars.iter().any(|p| p.ends_with("b.meta.toml")));
        Ok(())
    }

    #[test]
    fn discover_sidecars_recurses_into_subdirectories() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let subdir = dir.path().join("sub");
        fs::create_dir(&subdir)?;
        write_temp_file(dir.path(), "root.meta.toml", "# meta")?;
        write_temp_file(&subdir, "nested.meta.toml", "# meta")?;

        let sidecars = discover_sidecars(dir.path())?;
        assert_eq!(sidecars.len(), 2);
        Ok(())
    }

    #[test]
    fn discover_sidecars_returns_sorted_paths() -> Result<()> {
        let dir = tempfile::tempdir()?;
        write_temp_file(dir.path(), "zzz.meta.toml", "# meta")?;
        write_temp_file(dir.path(), "aaa.meta.toml", "# meta")?;
        write_temp_file(dir.path(), "mmm.meta.toml", "# meta")?;

        let sidecars = discover_sidecars(dir.path())?;
        assert_eq!(sidecars.len(), 3);
        // Verify sorted
        let names: Vec<_> =
            sidecars.iter().filter_map(|p| p.file_name().and_then(|n| n.to_str())).collect();
        let mut sorted = names.clone();
        sorted.sort();
        assert_eq!(names, sorted, "sidecars should be returned in sorted order");
        Ok(())
    }

    #[test]
    fn discover_sidecars_ignores_non_meta_toml_files() -> Result<()> {
        let dir = tempfile::tempdir()?;
        write_temp_file(dir.path(), "only_a.meta.toml", "# meta")?;
        write_temp_file(dir.path(), "ignore.json", "{}")?;
        write_temp_file(dir.path(), "ignore.toml", "[t]")?;
        write_temp_file(dir.path(), "ignore.pl", "1;")?;

        let sidecars = discover_sidecars(dir.path())?;
        assert_eq!(sidecars.len(), 1);
        Ok(())
    }

    // ConceptRegistry

    #[test]
    fn concept_registry_load_fails_on_missing_file() {
        let result = ConceptRegistry::load(Path::new("/no/such/file.toml"));
        assert!(result.is_err());
        let msg = result.err().map(|e| e.to_string()).unwrap_or_default();
        assert!(msg.contains("reading concept registry"));
    }

    #[test]
    fn concept_registry_load_fails_on_invalid_toml() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let path = write_temp_file(dir.path(), "bad.toml", "{{not valid")?;
        let result = ConceptRegistry::load(&path);
        assert!(result.is_err());
        Ok(())
    }

    #[test]
    fn concept_registry_contains_returns_true_for_known_id() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let toml = r#"
[[concepts]]
id = "parser.basic"

[[concepts]]
id = "regex.quantifier"
"#;
        let path = write_temp_file(dir.path(), "concepts.toml", toml)?;
        let registry = ConceptRegistry::load(&path)?;
        assert!(registry.contains("parser.basic"));
        assert!(registry.contains("regex.quantifier"));
        Ok(())
    }

    #[test]
    fn concept_registry_contains_returns_false_for_unknown_id() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let toml = r#"
[[concepts]]
id = "parser.basic"
"#;
        let path = write_temp_file(dir.path(), "concepts.toml", toml)?;
        let registry = ConceptRegistry::load(&path)?;
        assert!(!registry.contains("not.there"));
        Ok(())
    }

    #[test]
    fn concept_registry_ignores_empty_ids() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let toml = r#"
[[concepts]]
id = ""

[[concepts]]
id = "   "

[[concepts]]
id = "real.concept"
"#;
        let path = write_temp_file(dir.path(), "concepts.toml", toml)?;
        let registry = ConceptRegistry::load(&path)?;
        assert!(!registry.contains(""));
        assert!(!registry.contains("   "));
        assert!(registry.contains("real.concept"));
        Ok(())
    }

    #[test]
    fn concept_registry_handles_nested_toml_tables() -> Result<()> {
        let dir = tempfile::tempdir()?;
        // Nested table structure should be traversed by collect_concept_ids.
        let toml = r#"
[outer]
id = "outer.concept"

[outer.inner]
id = "inner.concept"
"#;
        let path = write_temp_file(dir.path(), "nested.toml", toml)?;
        let registry = ConceptRegistry::load(&path)?;
        assert!(registry.contains("outer.concept"));
        assert!(registry.contains("inner.concept"));
        Ok(())
    }

    #[test]
    fn concept_registry_handles_array_of_tables() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let toml = r#"
concepts = [
    { id = "array.one" },
    { id = "array.two" },
]
"#;
        let path = write_temp_file(dir.path(), "array.toml", toml)?;
        let registry = ConceptRegistry::load(&path)?;
        assert!(registry.contains("array.one"));
        assert!(registry.contains("array.two"));
        Ok(())
    }

    // SidecarExpect/SidecarConcept equality

    #[test]
    fn sidecar_types_support_equality() {
        let c1 = SidecarConcept { id: "x".to_string(), tier: "t".to_string() };
        let c2 = SidecarConcept { id: "x".to_string(), tier: "t".to_string() };
        let c3 = SidecarConcept { id: "y".to_string(), tier: "t".to_string() };
        assert_eq!(c1, c2);
        assert_ne!(c1, c3);

        let e1 = SidecarExpect { panic: true, timeout: false, mode: ExpectationMode::ParseClean };
        let e2 = SidecarExpect { panic: true, timeout: false, mode: ExpectationMode::ParseClean };
        let e3 = SidecarExpect { panic: false, timeout: false, mode: ExpectationMode::ParseClean };
        assert_eq!(e1, e2);
        assert_ne!(e1, e3);
    }

    #[test]
    fn sidecar_metrics_supports_equality() {
        let m1 = SidecarMetrics {
            max_error_nodes: Some(3),
            must_emit_node_kinds: Some(vec!["a".to_string()]),
        };
        let m2 = SidecarMetrics {
            max_error_nodes: Some(3),
            must_emit_node_kinds: Some(vec!["a".to_string()]),
        };
        assert_eq!(m1, m2);
    }

    #[test]
    fn sidecar_snapshots_supports_equality() {
        let s1 = SidecarSnapshots { tokens: true, ast: false, spans: true };
        let s2 = SidecarSnapshots { tokens: true, ast: false, spans: true };
        let s3 = SidecarSnapshots { tokens: false, ast: false, spans: true };
        assert_eq!(s1, s2);
        assert_ne!(s1, s3);
    }
}