diskann-benchmark-core 0.51.0

DiskANN is a fast approximate nearest neighbor search library for high dimensional data
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
/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT license.
 */

use std::path::{Path, PathBuf};

use anyhow::Context;
use serde_yaml::{Mapping, Value};

/// See [`super::RunBook::load`] for documentation of the file format parsed
/// by this function.
pub(super) fn load(
    path: &Path,
    dataset: &str,
    groundtruth: &mut dyn super::FindGroundtruth,
) -> anyhow::Result<super::RunBook> {
    load_mapping(parse_yaml(path)?, path, dataset, groundtruth)
}

fn load_mapping(
    mapping: Mapping,
    path: &Path,
    dataset: &str,
    groundtruth: &mut dyn super::FindGroundtruth,
) -> anyhow::Result<super::RunBook> {
    // Multiple datasets can exist in the same YAML file.
    //
    // Fortunately, all datasets are keyed by their dataset name which allows us to
    // quickly find whether or not it exists.
    let dataset_value = match mapping.get(dataset) {
        Some(value) => value,
        None => return Err(DumpKeys::new(mapping, dataset, path).into()),
    };

    // Try to coerce the dataset value into a `Mapping`.
    let dataset_mapping: &Mapping = match dataset_value.try_as() {
        Ok(mapping) => mapping,
        Err(_) => anyhow::bail!(
            "dataset \"{}\" exists in file \"{}\", but its associated payload is not a YAML map",
            dataset,
            path.display(),
        ),
    };

    let mut raw = parse_stages(dataset_mapping).with_context(|| {
        format!(
            "parsing dataset \"{}\" in file \"{}\"",
            dataset,
            path.display()
        )
    })?;
    raw.stages.sort_by_key(|s| s.index);

    // Translate from raw ranges into higher level steps.
    let context = |index: usize| {
        format!(
            "precessing stage {} of dataset \"{}\" in file \"{}\"",
            index,
            dataset,
            path.display()
        )
    };

    let stages: anyhow::Result<Vec<super::Stage>> = raw
        .stages
        .iter()
        .map(|stage| {
            let stage = match &stage.operation {
                Operation::Search => super::Stage::Search {
                    groundtruth: groundtruth
                        .find_groundtruth(stage.index)
                        .with_context(|| context(stage.index))?,
                },
                Operation::Insert(insert) => super::Stage::Insert {
                    dataset_offsets_and_ids: insert.start..insert.end,
                },
                Operation::Replace(replace) => super::Stage::Replace {
                    dataset_offsets: replace.ids_start..replace.ids_end,
                    ids: replace.tags_start..replace.tags_end,
                },
                Operation::Delete(delete) => super::Stage::Delete {
                    ids: delete.start..delete.end,
                },
            };
            Ok(stage)
        })
        .collect();

    super::RunBook::new(stages?, raw.max_points)
}

fn parse_yaml(path: &Path) -> anyhow::Result<Mapping> {
    let f = std::fs::File::open(path)
        .with_context(|| format!("while opening file \"{}\"", path.display()))?;

    Ok(serde_yaml::from_reader(std::io::BufReader::new(f))?)
}

fn parse_stages(mapping: &Mapping) -> anyhow::Result<Raw> {
    let mut stages = Vec::<Stage>::new();
    let mut max_points = None;
    mapping.iter().try_for_each(|(key, value)| match key {
        Value::String(s) => match s.as_str() {
            "max_pts" => {
                let points: usize = value
                    .try_as()
                    .map_err(|kind| anyhow::anyhow!("failed to parse \"max_pts\" as a {}", kind))?;

                max_points = Some(points);
                Ok(())
            }
            "gt_url" => Ok(()),
            _ => anyhow::bail!("Unrecognized runbook key: \"{}\"", s),
        },
        Value::Number(stage) => match stage.as_i64() {
            Some(stage) => {
                stages.push(
                    handle_stage(stage as usize, value)
                        .with_context(|| format!("processing stage {}", stage))?,
                );
                Ok(())
            }
            None => anyhow::bail!("Stage \"{}\" must be an integer", stage),
        },
        _ => anyhow::bail!("Unrecognized key of type {}", classify(key),),
    })?;

    let max_points = match max_points {
        Some(points) => points,
        None => anyhow::bail!("key \"max_pts\" not found"),
    };

    Ok(Raw { max_points, stages })
}

fn handle_stage(index: usize, value: &Value) -> anyhow::Result<Stage> {
    let mapping: &Mapping = value
        .try_as()
        .map_err(|_| anyhow::anyhow!("YAML type is not a map"))?;

    let kind: &str = mapping.get_as("operation")?;
    let operation = match Kind::try_parse(kind)? {
        Kind::Search => Operation::Search,
        Kind::Insert => Operation::Insert(mapping.try_into()?),
        Kind::Replace => Operation::Replace(mapping.try_into()?),
        Kind::Delete => Operation::Delete(mapping.try_into()?),
    };

    Ok(Stage { index, operation })
}

struct Raw {
    max_points: usize,
    stages: Vec<Stage>,
}

struct Stage {
    index: usize,
    operation: Operation,
}

enum Operation {
    Search,
    Insert(Insert),
    Replace(Replace),
    Delete(Delete),
}

#[derive(Debug)]
struct DumpKeys {
    mapping: Mapping,
    dataset: String,
    file: PathBuf,
}

impl DumpKeys {
    #[inline(never)]
    fn new(mapping: Mapping, dataset: &str, file: &Path) -> Self {
        Self {
            mapping,
            dataset: dataset.to_string(),
            file: file.into(),
        }
    }
}

impl std::fmt::Display for DumpKeys {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "dataset \"{}\" not found in file \"{}\" - possible alternatives: [",
            self.dataset,
            self.file.display(),
        )?;

        let len = self.mapping.len();
        self.mapping.keys().enumerate().try_for_each(|(i, key)| {
            let mut write = |key: &dyn std::fmt::Display| {
                if i + 1 == len {
                    write!(f, "{}", key)
                } else {
                    write!(f, "{}, ", key)
                }
            };

            match key {
                Value::Null => write(&"null"),
                Value::Bool(b) => write(b),
                Value::Number(number) => write(number),
                Value::String(s) => write(s),
                Value::Sequence(_) => write(&"<sequence>"),
                Value::Mapping(_) => write(&"<mapping>"),
                Value::Tagged(_) => write(&"<tagged>"),
            }
        })?;
        write!(f, "]")
    }
}

impl std::error::Error for DumpKeys {}

trait TryAs<'a, T> {
    fn try_as(&'a self) -> Result<T, &'static str>;
}

impl<'a> TryAs<'a, usize> for Value {
    fn try_as(&'a self) -> Result<usize, &'static str> {
        self.as_i64().map(|i| i as usize).ok_or("usize")
    }
}

impl<'a> TryAs<'a, &'a str> for Value {
    fn try_as(&'a self) -> Result<&'a str, &'static str> {
        self.as_str().ok_or("string")
    }
}

impl<'a> TryAs<'a, &'a Mapping> for Value {
    fn try_as(&'a self) -> Result<&'a Mapping, &'static str> {
        self.as_mapping().ok_or("map")
    }
}

trait MappingExt {
    fn get_as<'a, T>(&'a self, index: &str) -> anyhow::Result<T>
    where
        Value: TryAs<'a, T>;
}

impl MappingExt for Mapping {
    fn get_as<'a, T>(&'a self, key: &str) -> anyhow::Result<T>
    where
        Value: TryAs<'a, T>,
    {
        match self.get(key) {
            Some(value) => match value.try_as() {
                Ok(v) => Ok(v),
                Err(expected) => Err(anyhow::anyhow!(
                    "key \"{}\" exists but it is not a {}",
                    key,
                    expected,
                )),
            },
            None => Err(anyhow::anyhow!("key \"{}\" not found", key)),
        }
    }
}

#[derive(Debug, Clone, Copy)]
enum Kind {
    Search,
    Insert,
    Replace,
    Delete,
}

impl Kind {
    fn try_parse(string: &str) -> anyhow::Result<Self> {
        match string {
            "search" => Ok(Kind::Search),
            "insert" => Ok(Kind::Insert),
            "replace" => Ok(Kind::Replace),
            "delete" => Ok(Kind::Delete),
            _ => Err(anyhow::anyhow!("unrecognized operation: {}", string)),
        }
    }
}

#[derive(Debug)]
struct Replace {
    ids_start: usize,
    ids_end: usize,
    tags_start: usize,
    tags_end: usize,
}

impl TryFrom<&Mapping> for Replace {
    type Error = anyhow::Error;
    fn try_from(mapping: &Mapping) -> anyhow::Result<Self> {
        let inner = || -> anyhow::Result<Self> {
            let this = Self {
                ids_start: mapping.get_as("ids_start")?,
                ids_end: mapping.get_as("ids_end")?,
                tags_start: mapping.get_as("tags_start")?,
                tags_end: mapping.get_as("tags_end")?,
            };
            if this.ids_start >= this.ids_end {
                anyhow::bail!(
                    "ids_start ({}) must be less than ids_end ({})",
                    this.ids_start,
                    this.ids_end
                );
            }
            if this.tags_start >= this.tags_end {
                anyhow::bail!(
                    "tags_start ({}) must be less than tags_end ({})",
                    this.tags_start,
                    this.tags_end
                );
            }
            Ok(this)
        };

        inner().context("trying to parse an \"replace\"")
    }
}

#[derive(Debug)]
struct Insert {
    start: usize,
    end: usize,
}

impl TryFrom<&Mapping> for Insert {
    type Error = anyhow::Error;
    fn try_from(mapping: &Mapping) -> anyhow::Result<Self> {
        let inner = || -> anyhow::Result<Self> {
            let this = Self {
                start: mapping.get_as("start")?,
                end: mapping.get_as("end")?,
            };
            if this.start >= this.end {
                anyhow::bail!(
                    "start ({}) must be less than end ({})",
                    this.start,
                    this.end
                );
            }
            Ok(this)
        };

        inner().context("trying to parse an \"insert\"")
    }
}

#[derive(Debug)]
struct Delete {
    start: usize,
    end: usize,
}

impl TryFrom<&Mapping> for Delete {
    type Error = anyhow::Error;
    fn try_from(mapping: &Mapping) -> anyhow::Result<Self> {
        let inner = || -> anyhow::Result<Self> {
            let this = Self {
                start: mapping.get_as("start")?,
                end: mapping.get_as("end")?,
            };
            if this.start >= this.end {
                anyhow::bail!(
                    "start ({}) must be less than end ({})",
                    this.start,
                    this.end
                );
            }
            Ok(this)
        };

        inner().context("trying to parse \"delete\"")
    }
}

fn classify(value: &Value) -> &'static str {
    match value {
        Value::Null => "null",
        Value::Bool(_) => "bool",
        Value::Number(_) => "number",
        Value::String(_) => "string",
        Value::Sequence(_) => "sequence",
        Value::Mapping(_) => "mapping",
        Value::Tagged(_) => "tagged",
    }
}

///////////
// Tests //
///////////

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

    use std::{collections::HashMap, io::Write};

    use tempfile::NamedTempFile;

    use crate::streaming::executors::bigann::Stage;

    /// A test implementation of [`super::super::FindGroundtruth`] that returns
    /// pre-configured paths for each stage index.
    struct MockGroundtruth {
        paths: HashMap<usize, PathBuf>,
    }

    impl MockGroundtruth {
        fn new(stages: impl IntoIterator<Item = (usize, PathBuf)>) -> Self {
            Self {
                paths: stages.into_iter().collect(),
            }
        }
    }

    impl super::super::FindGroundtruth for MockGroundtruth {
        fn find_groundtruth(&mut self, stage: usize) -> anyhow::Result<PathBuf> {
            self.paths
                .get(&stage)
                .cloned()
                .ok_or_else(|| anyhow::anyhow!("no groundtruth configured for stage {}", stage))
        }
    }

    /// Helper to create a temporary YAML file with the given content.
    fn create_yaml_file(content: &str) -> anyhow::Result<NamedTempFile> {
        let mut file = NamedTempFile::new()?;
        file.write_all(content.as_bytes())?;
        file.flush()?;
        Ok(file)
    }

    #[test]
    fn test_load_simple_insert_only_runbook() {
        let yaml = r#"
test_dataset:
  max_pts: 1000
  0:
    operation: insert
    start: 0
    end: 500
"#;

        let file = create_yaml_file(yaml).unwrap();
        let mut groundtruth = MockGroundtruth::new([]);

        let runbook = load(file.path(), "test_dataset", &mut groundtruth).unwrap();

        assert_eq!(runbook.max_points(), 1000);
        assert_eq!(runbook.len(), 1);

        assert_eq!(
            runbook.stages()[0],
            Stage::Insert {
                dataset_offsets_and_ids: 0..500
            }
        );
    }

    #[test]
    fn test_load_runbook_with_search_stage() {
        let yaml = r#"
my_dataset:
  max_pts: 2000
  0:
    operation: insert
    start: 0
    end: 1000
  1:
    operation: search
"#;

        let file = create_yaml_file(yaml).unwrap();
        let mut groundtruth =
            MockGroundtruth::new([(1, PathBuf::from("/path/to/groundtruth.bin"))]);

        let runbook = load(file.path(), "my_dataset", &mut groundtruth).unwrap();

        assert_eq!(runbook.max_points(), 2000);
        assert_eq!(runbook.len(), 2);

        assert_eq!(
            runbook.stages()[1],
            Stage::Search {
                groundtruth: PathBuf::from("/path/to/groundtruth.bin")
            }
        );
    }

    #[test]
    fn test_load_runbook_with_all_operation_types() {
        let yaml = r#"
full_dataset:
  max_pts: 5000
  0:
    operation: insert
    start: 0
    end: 1000
  1:
    operation: search
  2:
    operation: replace
    ids_start: 1000
    ids_end: 1500
    tags_start: 0
    tags_end: 500
  3:
    operation: delete
    start: 500
    end: 1000
"#;

        let file = create_yaml_file(yaml).unwrap();
        let mut groundtruth = MockGroundtruth::new([(1, PathBuf::from("/gt/step1.bin"))]);

        let runbook = load(file.path(), "full_dataset", &mut groundtruth).unwrap();

        assert_eq!(runbook.max_points(), 5000);
        assert_eq!(runbook.len(), 4);

        // Check insert
        assert_eq!(
            runbook.stages()[0],
            Stage::Insert {
                dataset_offsets_and_ids: 0..1000
            }
        );

        // Check search
        assert_eq!(
            runbook.stages()[1],
            Stage::Search {
                groundtruth: PathBuf::from("/gt/step1.bin")
            }
        );

        // Check replace
        assert_eq!(
            runbook.stages()[2],
            Stage::Replace {
                dataset_offsets: 1000..1500,
                ids: 0..500
            }
        );

        // Check delete
        assert_eq!(runbook.stages()[3], Stage::Delete { ids: 500..1000 });
    }

    #[test]
    fn test_load_stages_out_of_order_are_sorted() {
        let yaml = r#"
unordered:
  max_pts: 1000
  2:
    operation: delete
    start: 500
    end: 600
  0:
    operation: insert
    start: 0
    end: 500
  1:
    operation: insert
    start: 500
    end: 1000
"#;

        let file = create_yaml_file(yaml).unwrap();
        let mut groundtruth = MockGroundtruth::new([]);

        let runbook = load(file.path(), "unordered", &mut groundtruth).unwrap();

        assert_eq!(runbook.len(), 3);

        // Stages should be in order 0, 1, 2 regardless of YAML order
        assert_eq!(
            runbook.stages()[0],
            Stage::Insert {
                dataset_offsets_and_ids: 0..500
            }
        );

        assert_eq!(
            runbook.stages()[1],
            Stage::Insert {
                dataset_offsets_and_ids: 500..1000
            }
        );

        assert_eq!(runbook.stages()[2], Stage::Delete { ids: 500..600 });
    }

    #[test]
    fn test_load_multiple_datasets_in_file() {
        let yaml = r#"
dataset_a:
  max_pts: 100
  0:
    operation: insert
    start: 0
    end: 100

dataset_b:
  max_pts: 200
  0:
    operation: insert
    start: 0
    end: 200
"#;

        let file = create_yaml_file(yaml).unwrap();

        // Load dataset_a
        let mut groundtruth_a = MockGroundtruth::new([]);
        let runbook_a = load(file.path(), "dataset_a", &mut groundtruth_a).unwrap();
        assert_eq!(runbook_a.max_points(), 100);

        // Load dataset_b
        let mut groundtruth_b = MockGroundtruth::new([]);
        let runbook_b = load(file.path(), "dataset_b", &mut groundtruth_b).unwrap();
        assert_eq!(runbook_b.max_points(), 200);
    }

    #[test]
    fn test_load_gt_url_is_ignored() {
        let yaml = r#"
with_gt_url:
  max_pts: 100
  gt_url: "https://example.com/groundtruth.bin"
  0:
    operation: insert
    start: 0
    end: 100
"#;

        let file = create_yaml_file(yaml).unwrap();
        let mut groundtruth = MockGroundtruth::new([]);

        // Should succeed - gt_url is parsed but ignored
        let runbook = load(file.path(), "with_gt_url", &mut groundtruth).unwrap();
        assert_eq!(runbook.max_points(), 100);
    }
}

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

    // Exposed by the `ux-tools` feature of `diskann_benchmark_runner`
    use diskann_benchmark_runner::ux as runner_ux;

    //---------------------------//
    // File-Based UX Error Tests //
    //---------------------------//
    //
    // These tests use checked-in YAML files and expected output files to verify error messages.
    // This approach makes it easy to:
    // 1. Add new test cases (just add a new directory with runbook.yaml, dataset.txt, expected.txt)
    // 2. See the full error output for review
    // 3. Regenerate expected output when error messages change
    //
    // ## Directory Structure
    //
    // Each test case is a directory under `tests/bigann-ux` containing:
    // - `runbook.yaml` - The YAML runbook file to parse
    // - `dataset.txt` - Contains the dataset name to load (single line)
    // - `expected.txt` - The expected error message output
    //
    // ## Regenerating Expected Results
    //
    // Run tests with the environment variable:
    // ```
    // DISKANN_BENCHMARK_TEST=overwrite
    // ```
    // to regenerate the `expected.txt` files. Use `git diff` to review changes.

    const TEST_DATA_DIR: &str = "bigann-ux";
    const RUNBOOK_FILE: &str = "runbook.yaml";
    const DATASET_FILE: &str = "dataset.txt";
    const EXPECTED_FILE: &str = "expected.txt";
    const PATH_PLACEHOLDER: &str = "<TEST_DIR>";

    /// Replace the test directory path with a placeholder to make tests portable.
    /// This handles both forward and backslash path separators.
    ///
    /// Additionally:
    /// * All backslashes are replaced with forward slashes.
    /// * Common OS-specific "file not found" error messages are normalized.
    fn fixup_paths_and_os_errors(s: &str, test_dir: &Path) -> String {
        // Try both the native path and with normalized separators
        let native_path = test_dir.display().to_string();
        let forward_slash_path = native_path.replace('\\', "/");

        const NOT_FOUND_WINDOWS: &str = "The system cannot find the file specified.";
        const NOT_FOUND_LINUX: &str = "No such file or directory";

        s.replace(&native_path, PATH_PLACEHOLDER)
            .replace(&forward_slash_path, PATH_PLACEHOLDER)
            .replace('\\', "/") // Normalize any remaining backslashes
            .replace(NOT_FOUND_WINDOWS, NOT_FOUND_LINUX) // Normalize error messages
    }

    /// A groundtruth finder that always fails - used for error path testing.
    struct FailingGroundtruth;

    impl super::super::FindGroundtruth for FailingGroundtruth {
        fn find_groundtruth(&mut self, stage: usize) -> anyhow::Result<PathBuf> {
            Err(anyhow::anyhow!(
                "groundtruth not available for stage {}",
                stage
            ))
        }
    }

    /// Run a single file-based test case.
    fn run_file_test(test_dir: &Path) {
        let runbook_path = test_dir.join(RUNBOOK_FILE);
        let dataset_path = test_dir.join(DATASET_FILE);
        let expected_path = test_dir.join(EXPECTED_FILE);

        // Read the dataset name
        let dataset = std::fs::read_to_string(&dataset_path)
            .unwrap_or_else(|e| panic!("Failed to read {:?}: {}", dataset_path, e));
        let dataset = dataset.trim();

        // Try to load the runbook - we expect an error
        let mut groundtruth = FailingGroundtruth;
        let result = load(&runbook_path, dataset, &mut groundtruth);

        let actual_output = match result {
            Ok(_) => panic!(
                "Test {:?} expected an error but parsing succeeded",
                test_dir.file_name().unwrap()
            ),
            Err(err) => format!("{:?}", err),
        };

        // Replace test directory path with placeholder for portability
        let actual_portable = fixup_paths_and_os_errors(&actual_output, test_dir);
        let actual_normalized = runner_ux::strip_backtrace(runner_ux::normalize(actual_portable));

        if crate::ux::should_overwrite() {
            std::fs::write(&expected_path, &actual_normalized)
                .unwrap_or_else(|e| panic!("Failed to write {:?}: {}", expected_path, e));
            println!("Overwrote {:?}", expected_path);
        } else {
            let expected = std::fs::read_to_string(&expected_path)
                .unwrap_or_else(|e| panic!("Failed to read {:?}: {}", expected_path, e));
            let expected_normalized = runner_ux::normalize(expected);

            if actual_normalized != expected_normalized {
                panic!(
                    "Test {:?} failed.\n\nExpected:\n---\n{}\n---\n\nActual:\n---\n{}\n---\nIf this is expected, run with {} to update the expected output.",
                    test_dir.file_name().unwrap(),
                    expected_normalized,
                    actual_normalized,
                    crate::ux::help(),
                );
            }
        }
    }

    /// Run all file-based tests in the test_data directory.
    fn run_all_file_tests() {
        let test_data_path = crate::ux::test_dir().join(TEST_DATA_DIR);
        if !test_data_path.exists() {
            println!(
                "No test_data directory found at {:?}, skipping file-based tests",
                test_data_path
            );
            return;
        }

        let mut found_tests = false;
        for entry in std::fs::read_dir(&test_data_path)
            .unwrap_or_else(|e| panic!("Failed to read {:?}: {}", test_data_path, e))
        {
            let entry = entry.unwrap();
            if entry.file_type().unwrap().is_dir() {
                found_tests = true;
                println!("Running file-based test: {:?}", entry.file_name());
                run_file_test(&entry.path());
            }
        }

        if !found_tests {
            panic!("No test directories found in {:?}", test_data_path);
        }
    }

    #[test]
    fn file_based_error_tests() {
        run_all_file_tests();
    }
}