xberg-cli 1.3.0

Command-line interface for Xberg document intelligence
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
#[cfg(feature = "ocr-surface")]
use super::super::*;
#[cfg(feature = "ocr-surface")]
use super::default_overrides;
#[cfg(feature = "ocr-surface")]
use xberg::{ExtractionConfig, OcrConfig};

#[cfg(feature = "ocr-surface")]
#[test]
fn test_ocr_default_language_tesseract() {
    let mut config = ExtractionConfig::default();
    let overrides = ExtractionOverrides {
        ocr: Some(true),
        ..default_overrides()
    };
    overrides.apply(&mut config);
    let ocr = config.ocr.unwrap();
    assert_eq!(ocr.backend, "tesseract");
    assert_eq!(ocr.language, vec!["eng".to_string()]);
}

#[cfg(feature = "ocr-surface")]
#[test]
fn test_ocr_default_language_paddleocr() {
    let mut config = ExtractionConfig::default();
    let overrides = ExtractionOverrides {
        ocr: Some(true),
        ocr_backend: Some("paddle-ocr".to_string()),
        ..default_overrides()
    };
    overrides.apply(&mut config);
    let ocr = config.ocr.unwrap();
    assert_eq!(ocr.backend, "paddle-ocr");
    assert_eq!(ocr.language, vec!["en".to_string()]);
}

#[cfg(feature = "ocr-surface")]
#[test]
fn test_ocr_default_language_sceptre() {
    let mut config = ExtractionConfig::default();
    let overrides = ExtractionOverrides {
        ocr: Some(true),
        ocr_backend: Some("sceptre".to_string()),
        ..default_overrides()
    };
    overrides.apply(&mut config);
    let ocr = config.ocr.expect("OCR config should be set");
    assert_eq!(ocr.backend, "sceptre");
    assert_eq!(ocr.language, vec!["eng".to_string()]);
}

/// Regression test for the empirically observed CLI defect: running
/// `xberg extract doc.pdf --ocr-scanned-pages --ocr-backend sceptre` (no
/// `--ocr true`) silently ran tesseract instead of sceptre, because
/// `apply_ocr` only materialised `config.ocr` on `--ocr true`, so
/// `apply_ocr_fields` — which assigns `backend` — never ran, while
/// `--ocr-scanned-pages` set `config.ocr_strategy` unconditionally and
/// triggered OCR to run anyway with the default backend. Before the fix
/// (i.e. reverting `has_ocr_field_flag` back to just `self.ocr ==
/// Some(true)`), `config.ocr` stays `None` here — `ocr.backend` is never
/// even reachable — so this assertion fails without the fix.
#[cfg(feature = "ocr-surface")]
#[test]
fn test_ocr_backend_flag_selects_backend_without_ocr_true_flag() {
    let mut config = ExtractionConfig::default();
    let overrides = ExtractionOverrides {
        ocr_backend: Some("sceptre".to_string()),
        ocr_scanned_pages: true,
        ..default_overrides()
    };

    overrides.apply(&mut config);

    let ocr = config
        .ocr
        .expect("--ocr-backend must materialise an OCR config even without --ocr true");
    assert_eq!(ocr.backend, "sceptre");
    assert_eq!(
        config.ocr_strategy,
        xberg::OcrStrategy::ScannedPages {
            min_confidence: xberg::core::config::DEFAULT_SCANNED_MIN_CONFIDENCE
        }
    );
}

/// Regression test for #656: `--ocr-scanned-pages` alone (no `--extract-pages`, no
/// `--ocr true`, no other `--ocr-*` field flag) previously left `config.pages` as `None`.
/// The PDF backend only tracks per-page byte boundaries when `config.pages` is `Some`
/// (`pdf::native::text::extract_text_from_native_document`'s `page_config` branch), and the
/// mixed OCR route needs those boundaries to splice OCR text back into the native text
/// (`extractors/pdf/mod.rs`). Without them, that route always fell back to "no page
/// boundaries available; using native text" -- an empty result for a scan, i.e. a
/// zero-byte, exit-0 extraction. Before the fix (i.e. removing the
/// `config.pages.get_or_insert_with(Default::default)` call), `config.pages` stays `None`
/// here and this assertion fails.
#[cfg(feature = "ocr-surface")]
#[test]
fn test_ocr_scanned_pages_alone_materialises_page_boundary_tracking() {
    let mut config = ExtractionConfig::default();
    let overrides = ExtractionOverrides {
        ocr_scanned_pages: true,
        ..default_overrides()
    };

    overrides.apply(&mut config);

    let pages = config
        .pages
        .expect("--ocr-scanned-pages must materialise page-boundary tracking on its own");
    assert!(
        !pages.extract_pages,
        "boundary tracking must not silently turn on the `pages` output array"
    );
}

/// Companion guard: materialising `config.pages` for boundary tracking must not clobber an
/// explicit `--extract-pages true` given alongside `--ocr-scanned-pages`.
#[cfg(feature = "ocr-surface")]
#[test]
fn test_ocr_scanned_pages_does_not_override_explicit_extract_pages_flag() {
    let mut config = ExtractionConfig::default();
    let overrides = ExtractionOverrides {
        ocr_scanned_pages: true,
        extract_pages: Some(true),
        ..default_overrides()
    };

    overrides.apply(&mut config);

    let pages = config.pages.expect("pages config should be set");
    assert!(pages.extract_pages, "--extract-pages true must still take effect");
}

#[cfg(feature = "ocr-surface")]
#[test]
fn test_validate_unknown_ocr_backend_rejected() {
    let overrides = ExtractionOverrides {
        ocr_backend: Some("unsupported-ocr".to_string()),
        ..default_overrides()
    };
    let err = overrides.validate().unwrap_err();
    assert!(err.to_string().contains("Invalid OCR backend"));
}

#[cfg(feature = "ocr-surface")]
#[test]
fn test_ocr_language_override_tesseract() {
    let mut config = ExtractionConfig::default();
    let overrides = ExtractionOverrides {
        ocr: Some(true),
        ocr_language: Some("fra".to_string()),
        ..default_overrides()
    };
    overrides.apply(&mut config);
    let ocr = config.ocr.unwrap();
    assert_eq!(ocr.backend, "tesseract");
    assert_eq!(ocr.language, vec!["fra".to_string()]);
}

#[cfg(feature = "ocr-surface")]
#[test]
fn test_ocr_language_override_paddleocr() {
    let mut config = ExtractionConfig::default();
    let overrides = ExtractionOverrides {
        ocr: Some(true),
        ocr_backend: Some("paddle-ocr".to_string()),
        ocr_language: Some("ch".to_string()),
        ..default_overrides()
    };
    overrides.apply(&mut config);
    let ocr = config.ocr.unwrap();
    assert_eq!(ocr.backend, "paddle-ocr");
    assert_eq!(ocr.language, vec!["ch".to_string()]);
}

/// `--ocr-language` alone (no `--ocr true`, no pre-existing `config.ocr`) must
/// still materialise an OCR config carrying the requested language — naming a
/// field is enough to select it, exactly like `--ocr-backend`. Before the fix,
/// this flag was silently discarded whenever `config.ocr` was still `None`.
#[cfg(feature = "ocr-surface")]
#[test]
fn test_ocr_language_without_ocr_flag_no_existing_config() {
    let mut config = ExtractionConfig::default();
    let overrides = ExtractionOverrides {
        ocr_language: Some("deu".to_string()),
        ..default_overrides()
    };
    overrides.apply(&mut config);
    let ocr = config.ocr.expect("--ocr-language alone must materialise an OCR config");
    assert_eq!(ocr.language, vec!["deu".to_string()]);
    assert_eq!(ocr.backend, "tesseract", "backend keeps its compiled-in default");
}

#[cfg(feature = "ocr-surface")]
#[test]
fn test_ocr_language_without_ocr_flag_existing_config() {
    let mut config = ExtractionConfig {
        ocr: Some(OcrConfig {
            enabled: true,
            backend: "tesseract".to_string(),
            language: vec!["eng".to_string()],
            tesseract_config: None,
            output_format: None,
            paddle_ocr_config: None,
            element_config: None,
            quality_thresholds: None,
            pipeline: None,
            auto_rotate: false,
            vlm_config: None,
            vlm_fallback: Default::default(),
            vlm_prompt: None,
            acceleration: None,
            security_limits: None,
            tessdata_bytes: None,
            tessdata_path: None,
            numeric_repair: false,
            backend_options: None,
        }),
        ..Default::default()
    };
    let overrides = ExtractionOverrides {
        ocr_language: Some("deu".to_string()),
        ..default_overrides()
    };
    overrides.apply(&mut config);
    let ocr = config.ocr.unwrap();
    assert_eq!(ocr.backend, "tesseract");
    assert_eq!(ocr.language, vec!["deu".to_string()]);
}

#[cfg(feature = "ocr-surface")]
#[test]
fn test_ocr_language_updates_existing_nested_tesseract_config() {
    let mut config = ExtractionConfig {
        ocr: Some(OcrConfig {
            enabled: true,
            backend: "tesseract".to_string(),
            language: vec!["eng".to_string()],
            tesseract_config: Some(xberg::TesseractConfig {
                language: vec!["eng".to_string()],
                use_cache: false,
                ..Default::default()
            }),
            output_format: None,
            paddle_ocr_config: None,
            element_config: None,
            quality_thresholds: None,
            pipeline: None,
            auto_rotate: false,
            vlm_config: None,
            vlm_fallback: Default::default(),
            vlm_prompt: None,
            acceleration: None,
            security_limits: None,
            tessdata_bytes: None,
            tessdata_path: None,
            numeric_repair: false,
            backend_options: None,
        }),
        ..Default::default()
    };
    let overrides = ExtractionOverrides {
        ocr_language: Some("deu".to_string()),
        ..default_overrides()
    };

    overrides.apply(&mut config);

    let ocr = config.ocr.unwrap();
    assert_eq!(ocr.language, vec!["deu".to_string()]);
    let tesseract = ocr.tesseract_config.unwrap();
    assert_eq!(tesseract.language, vec!["deu".to_string()]);
    assert!(!tesseract.use_cache);
}

/// `--ocr-no-cache` alone (no `--ocr true`, no pre-existing `config.ocr`) must be a
/// no-op: `config.ocr` must stay `None`.
///
/// Regression test for #693. The flag's entire contract is "bypass the Tesseract OCR
/// cache, and nothing else" (see its doc comment), but a prior version of
/// `apply_ocr_no_cache` materialised `tesseract_config` from `TesseractConfig::default()`
/// whenever it was still `None`, purely to have somewhere to write `use_cache: false`.
/// That flipped `tesseract_config` from `None` to `Some(..)`, which
/// `crates/xberg/src/extractors/image.rs::apply_default_tesseract_psm` (and its siblings)
/// treat as "the caller already made an explicit PSM choice" — disarming the
/// `WHOLE_IMAGE_TESSERACT_PSM = 11` default for whole-page image OCR and leaving
/// `TesseractConfig::default()`'s `psm = 3` instead. Measured on a real scan: 217
/// recognised words without `--ocr-no-cache` vs. 194 with it, from that PSM change alone.
///
/// Against the code before this fix (i.e. `apply_ocr_no_cache` using
/// `ocr.tesseract_config.get_or_insert_with(|| TesseractConfig { language, ..Default::default() })`
/// and `has_ocr_field_flag` including `self.ocr_no_cache.is_some()`), this assertion
/// fails: `config.ocr` is `Some(..)` with `tesseract_config` also `Some(TesseractConfig {
/// psm: 3, use_cache: false, .. })` instead of `None`.
#[cfg(feature = "ocr-surface")]
#[test]
fn test_ocr_no_cache_alone_is_a_no_op_without_existing_tesseract_config() {
    let mut config = ExtractionConfig::default();
    let overrides = ExtractionOverrides {
        ocr_no_cache: Some(true),
        ..default_overrides()
    };
    overrides.apply(&mut config);

    assert!(
        config.ocr.is_none(),
        "--ocr-no-cache alone must not materialise config.ocr: doing so (even just to \
             carry use_cache) would disarm the whole-image PSM default in \
             extractors/image.rs::apply_default_tesseract_psm and silently change what \
             Tesseract recognises"
    );
}

/// Companion to the no-op test above for the case where `config.ocr` already exists
/// (e.g. set by `--ocr true`) but `tesseract_config` itself does not. `--ocr-no-cache`
/// must still leave `tesseract_config` as `None` rather than materialising it.
#[cfg(feature = "ocr-surface")]
#[test]
fn test_ocr_no_cache_leaves_tesseract_config_none_when_ocr_config_exists_without_it() {
    let mut config = ExtractionConfig::default();
    let overrides = ExtractionOverrides {
        ocr: Some(true),
        ocr_no_cache: Some(true),
        ..default_overrides()
    };
    overrides.apply(&mut config);

    let ocr = config.ocr.expect("--ocr true must materialise config.ocr");
    assert!(
        ocr.tesseract_config.is_none(),
        "--ocr-no-cache must not materialise tesseract_config on its own, even when \
             config.ocr already exists from --ocr true"
    );
}

/// When `tesseract_config` is already set (e.g. by a loaded config file),
/// `--ocr-no-cache` must flip `use_cache` and change *nothing else*: every other field
/// of `TesseractConfig` that reaches the OCR engine must come out identical.
///
/// Regression test for #693's core claim ("a flag whose entire contract is caching must
/// not move recognition output"), and for the class of bug a `use_cache`-only assertion
/// would miss. Against the code before this fix, this assertion still happens to pass
/// for this particular case (tesseract_config already `Some`, so the old
/// `get_or_insert_with` was a no-op and only `use_cache` changed) — the failure mode
/// this fix targets is exercised by
/// `test_ocr_no_cache_alone_is_a_no_op_without_existing_tesseract_config` above, which
/// covers the case this test cannot: `tesseract_config` starting out `None`.
#[cfg(feature = "ocr-surface")]
#[test]
fn test_ocr_no_cache_changes_only_use_cache_when_tesseract_config_already_set() {
    let non_default_tesseract_config = xberg::TesseractConfig {
        language: vec!["fra".to_string(), "deu".to_string()],
        psm: Some(11),
        output_format: "hocr".to_string(),
        oem: 1,
        min_confidence: 42.5,
        preprocessing: Some(xberg::ImagePreprocessingConfig {
            target_dpi: 600,
            auto_rotate: true,
            deskew: false,
            denoise: true,
            contrast_enhance: true,
            binarization_method: "sauvola".to_string(),
            invert_colors: true,
            normalize_shaded_rows: false,
        }),
        enable_table_detection: false,
        table_min_confidence: 0.75,
        table_column_threshold: 12,
        table_row_threshold_ratio: 0.9,
        use_cache: true,
        classify_use_pre_adapted_templates: false,
        language_model_ngram_on: true,
        tessedit_dont_blkrej_good_wds: false,
        tessedit_dont_rowrej_good_wds: false,
        tessedit_enable_dict_correction: false,
        tessedit_char_whitelist: "0123456789".to_string(),
        tessedit_char_blacklist: "@#".to_string(),
        tessedit_use_primary_params_model: false,
        textord_space_size_is_variable: false,
        thresholding_method: 2,
    };
    let mut config = ExtractionConfig {
        ocr: Some(OcrConfig {
            enabled: true,
            backend: "tesseract".to_string(),
            language: vec!["fra".to_string(), "deu".to_string()],
            tesseract_config: Some(non_default_tesseract_config.clone()),
            output_format: None,
            paddle_ocr_config: None,
            element_config: None,
            quality_thresholds: None,
            pipeline: None,
            auto_rotate: false,
            vlm_config: None,
            vlm_fallback: Default::default(),
            vlm_prompt: None,
            acceleration: None,
            security_limits: None,
            tessdata_bytes: None,
            tessdata_path: None,
            numeric_repair: false,
            backend_options: None,
        }),
        ..Default::default()
    };
    let overrides = ExtractionOverrides {
        ocr_no_cache: Some(true),
        ..default_overrides()
    };
    overrides.apply(&mut config);

    let tesseract = config.ocr.unwrap().tesseract_config.unwrap();
    assert!(!tesseract.use_cache, "--ocr-no-cache true must disable use_cache");
    assert_eq!(tesseract.language, non_default_tesseract_config.language);
    assert_eq!(tesseract.psm, non_default_tesseract_config.psm);
    assert_eq!(tesseract.output_format, non_default_tesseract_config.output_format);
    assert_eq!(tesseract.oem, non_default_tesseract_config.oem);
    assert_eq!(tesseract.min_confidence, non_default_tesseract_config.min_confidence);
    assert_eq!(
        tesseract.preprocessing.as_ref().map(|p| p.target_dpi),
        non_default_tesseract_config
            .preprocessing
            .as_ref()
            .map(|p| p.target_dpi)
    );
    assert_eq!(
        tesseract.preprocessing.as_ref().map(|p| p.auto_rotate),
        non_default_tesseract_config
            .preprocessing
            .as_ref()
            .map(|p| p.auto_rotate)
    );
    assert_eq!(
        tesseract.preprocessing.as_ref().map(|p| p.deskew),
        non_default_tesseract_config.preprocessing.as_ref().map(|p| p.deskew)
    );
    assert_eq!(
        tesseract.preprocessing.as_ref().map(|p| p.denoise),
        non_default_tesseract_config.preprocessing.as_ref().map(|p| p.denoise)
    );
    assert_eq!(
        tesseract.preprocessing.as_ref().map(|p| p.contrast_enhance),
        non_default_tesseract_config
            .preprocessing
            .as_ref()
            .map(|p| p.contrast_enhance)
    );
    assert_eq!(
        tesseract.preprocessing.as_ref().map(|p| p.binarization_method.clone()),
        non_default_tesseract_config
            .preprocessing
            .as_ref()
            .map(|p| p.binarization_method.clone())
    );
    assert_eq!(
        tesseract.preprocessing.as_ref().map(|p| p.invert_colors),
        non_default_tesseract_config
            .preprocessing
            .as_ref()
            .map(|p| p.invert_colors)
    );
    assert_eq!(
        tesseract.enable_table_detection,
        non_default_tesseract_config.enable_table_detection
    );
    assert_eq!(
        tesseract.table_min_confidence,
        non_default_tesseract_config.table_min_confidence
    );
    assert_eq!(
        tesseract.table_column_threshold,
        non_default_tesseract_config.table_column_threshold
    );
    assert_eq!(
        tesseract.table_row_threshold_ratio,
        non_default_tesseract_config.table_row_threshold_ratio
    );
    assert_eq!(
        tesseract.classify_use_pre_adapted_templates,
        non_default_tesseract_config.classify_use_pre_adapted_templates
    );
    assert_eq!(
        tesseract.language_model_ngram_on,
        non_default_tesseract_config.language_model_ngram_on
    );
    assert_eq!(
        tesseract.tessedit_dont_blkrej_good_wds,
        non_default_tesseract_config.tessedit_dont_blkrej_good_wds
    );
    assert_eq!(
        tesseract.tessedit_dont_rowrej_good_wds,
        non_default_tesseract_config.tessedit_dont_rowrej_good_wds
    );
    assert_eq!(
        tesseract.tessedit_enable_dict_correction,
        non_default_tesseract_config.tessedit_enable_dict_correction
    );
    assert_eq!(
        tesseract.tessedit_char_whitelist,
        non_default_tesseract_config.tessedit_char_whitelist
    );
    assert_eq!(
        tesseract.tessedit_char_blacklist,
        non_default_tesseract_config.tessedit_char_blacklist
    );
    assert_eq!(
        tesseract.tessedit_use_primary_params_model,
        non_default_tesseract_config.tessedit_use_primary_params_model
    );
    assert_eq!(
        tesseract.textord_space_size_is_variable,
        non_default_tesseract_config.textord_space_size_is_variable
    );
    assert_eq!(
        tesseract.thresholding_method,
        non_default_tesseract_config.thresholding_method
    );
}

/// `--ocr-no-cache false` (explicitly re-enabling) must flip an already-disabled
/// `tesseract_config.use_cache` back to `true` rather than being a one-way switch.
#[cfg(feature = "ocr-surface")]
#[test]
fn test_ocr_no_cache_false_re_enables_an_already_disabled_cache() {
    let mut config = ExtractionConfig {
        ocr: Some(OcrConfig {
            enabled: true,
            backend: "tesseract".to_string(),
            language: vec!["eng".to_string()],
            tesseract_config: Some(xberg::TesseractConfig {
                language: vec!["eng".to_string()],
                use_cache: false,
                ..Default::default()
            }),
            output_format: None,
            paddle_ocr_config: None,
            element_config: None,
            quality_thresholds: None,
            pipeline: None,
            auto_rotate: false,
            vlm_config: None,
            vlm_fallback: Default::default(),
            vlm_prompt: None,
            acceleration: None,
            security_limits: None,
            tessdata_bytes: None,
            tessdata_path: None,
            numeric_repair: false,
            backend_options: None,
        }),
        ..Default::default()
    };
    let overrides = ExtractionOverrides {
        ocr_no_cache: Some(false),
        ..default_overrides()
    };
    overrides.apply(&mut config);

    let ocr = config.ocr.unwrap();
    assert!(ocr.tesseract_config.unwrap().use_cache);
}

#[cfg(feature = "ocr-surface")]
#[test]
fn test_ocr_language_updates_tesseract_pipeline_stages() {
    let tesseract_config = xberg::TesseractConfig {
        language: vec!["eng".to_string()],
        use_cache: false,
        ..Default::default()
    };
    let mut config = ExtractionConfig {
        ocr: Some(OcrConfig {
            pipeline: Some(xberg::OcrPipelineConfig {
                stages: vec![
                    xberg::OcrPipelineStage {
                        backend: "tesseract".to_string(),
                        priority: 100,
                        language: Some(vec!["eng".to_string()]),
                        tesseract_config: Some(tesseract_config),
                        paddle_ocr_config: None,
                        vlm_config: None,
                        backend_options: None,
                    },
                    xberg::OcrPipelineStage {
                        backend: "paddle-ocr".to_string(),
                        priority: 90,
                        language: Some(vec!["en".to_string()]),
                        tesseract_config: None,
                        paddle_ocr_config: None,
                        vlm_config: None,
                        backend_options: None,
                    },
                ],
                quality_thresholds: Default::default(),
            }),
            ..OcrConfig::default()
        }),
        ..Default::default()
    };
    let overrides = ExtractionOverrides {
        ocr_language: Some("deu".to_string()),
        ..default_overrides()
    };

    overrides.apply(&mut config);

    let stages = &config.ocr.unwrap().pipeline.unwrap().stages;
    assert_eq!(stages[0].language, Some(vec!["deu".to_string()]));
    assert_eq!(
        stages[0].tesseract_config.as_ref().unwrap().language,
        vec!["deu".to_string()]
    );
    assert_eq!(stages[1].backend, "paddle-ocr");
    assert_eq!(stages[1].priority, 90);
    assert_eq!(stages[1].language, Some(vec!["en".to_string()]));
    assert!(stages[1].tesseract_config.is_none());
    assert!(stages[1].paddle_ocr_config.is_none());
    assert!(stages[1].vlm_config.is_none());
    assert!(stages[1].backend_options.is_none());
}

#[cfg(feature = "ocr-surface")]
#[test]
fn test_ocr_disabled_ignores_language() {
    let mut config = ExtractionConfig::default();
    let overrides = ExtractionOverrides {
        ocr: Some(false),
        ocr_language: Some("fra".to_string()),
        ..default_overrides()
    };
    overrides.apply(&mut config);
    assert!(config.ocr.is_none());
}