redactor 0.3.0

Secure PDF redaction library with Type3 font support using MuPDF
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
//! Integration tests for regex pattern redaction feature.
//!
//! These tests verify end-to-end functionality of the regex pattern
//! redaction feature, including various pattern types, edge cases,
//! and error handling.

use anyhow::Result;
use redactor::{RedactionService, RedactionTarget};
use std::sync::Mutex;
use tempfile::TempDir;

mod common;
use common::*;

// Global mutex to serialize MuPDF operations across tests
// MuPDF has thread-safety issues with font loading, so we need to ensure
// only one test uses MuPDF at a time
static MUPDF_LOCK: Mutex<()> = Mutex::new(());

/// Helper macro to wrap MuPDF operations with the global lock
macro_rules! with_mupdf_lock {
    ($body:expr) => {{
        let _guard = MUPDF_LOCK.lock().expect("MuPDF lock poisoned");
        $body
    }};
}

#[test]
fn test_regex_ssn_redaction() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let input_pdf = temp_dir.path().join("input.pdf");
    let output_pdf = temp_dir.path().join("output.pdf");

    // Create PDF with SSN patterns
    TestPdfBuilder::new()
        .with_title("Employee Records")
        .with_content("Employee Information:")
        .with_content("John Doe - SSN: 123-45-6789")
        .with_content("Jane Smith - SSN: 987-65-4321")
        .with_content("Bob Johnson - SSN: 555-12-3456")
        .build(&input_pdf)?;

    // Verify SSNs are in input
    let input_text = extract_text(&input_pdf)?;
    assert!(input_text.contains("123-45-6789"));
    assert!(input_text.contains("987-65-4321"));
    assert!(input_text.contains("555-12-3456"));

    // Redact SSNs using regex pattern (without word boundaries due to PDF text extraction)
    let service = RedactionService::with_secure_strategy();
    let result = with_mupdf_lock!(service.redact(
        &input_pdf,
        &output_pdf,
        &[RedactionTarget::Regex(r"\d{3}-\d{2}-\d{4}".to_string())]
    ))?;

    // Verify redaction occurred
    assert!(result.has_redactions());
    assert_eq!(result.instances_redacted, 3);
    assert!(result.secure);

    // Verify SSNs are removed from output
    let output_text = extract_text(&output_pdf)?;
    assert!(!output_text.contains("123-45-6789"));
    assert!(!output_text.contains("987-65-4321"));
    assert!(!output_text.contains("555-12-3456"));
    assert!(output_text.contains("Employee Information"));

    Ok(())
}

#[test]
fn test_regex_email_redaction() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let input_pdf = temp_dir.path().join("input.pdf");
    let output_pdf = temp_dir.path().join("output.pdf");

    // Create PDF with email addresses
    TestPdfBuilder::new()
        .with_title("Contact Directory")
        .with_content("Primary: john.doe@example.com")
        .with_content("Secondary: jane.smith@company.org")
        .with_content("Support: help@support.net")
        .with_content("Not an email: test@")
        .build(&input_pdf)?;

    // Verify emails are in input
    let input_text = extract_text(&input_pdf)?;
    assert!(input_text.contains("john.doe@example.com"));
    assert!(input_text.contains("jane.smith@company.org"));
    assert!(input_text.contains("help@support.net"));

    // Redact emails using regex pattern (without word boundaries due to PDF text extraction)
    let service = RedactionService::with_secure_strategy();
    let result = with_mupdf_lock!(service.redact(
        &input_pdf,
        &output_pdf,
        &[RedactionTarget::Regex(
            r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}".to_string()
        )]
    ))?;

    // Verify redaction occurred
    assert!(result.has_redactions());
    assert_eq!(result.instances_redacted, 3);

    // Verify emails are removed from output
    let output_text = extract_text(&output_pdf)?;
    assert!(!output_text.contains("john.doe@example.com"));
    assert!(!output_text.contains("jane.smith@company.org"));
    assert!(!output_text.contains("help@support.net"));
    // Labels may or may not be preserved depending on PDF text extraction

    Ok(())
}

#[test]
fn test_regex_currency_redaction() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let input_pdf = temp_dir.path().join("input.pdf");
    let output_pdf = temp_dir.path().join("output.pdf");

    // Create PDF with currency amounts
    TestPdfBuilder::new()
        .with_title("Financial Report")
        .with_content("Revenue: $1,234.56")
        .with_content("Expenses: $987.65")
        .with_content("Balance: $5,432.10")
        .with_content("Total: $7,654.31")
        .build(&input_pdf)?;

    // Verify amounts are in input
    let input_text = extract_text(&input_pdf)?;
    assert!(input_text.contains("$1,234.56"));
    assert!(input_text.contains("$987.65"));

    // Redact currency amounts using regex
    let service = RedactionService::with_secure_strategy();
    let result = with_mupdf_lock!(service.redact(
        &input_pdf,
        &output_pdf,
        &[RedactionTarget::Regex(r"\$[\d,]+\.\d{2}".to_string())]
    ))?;

    // Verify redaction occurred
    assert!(result.has_redactions());
    assert_eq!(result.instances_redacted, 4);

    // Verify amounts are removed from output
    let output_text = extract_text(&output_pdf)?;
    assert!(!output_text.contains("$1,234.56"));
    assert!(!output_text.contains("$987.65"));
    assert!(!output_text.contains("$5,432.10"));
    assert!(output_text.contains("Revenue:"));
    assert!(output_text.contains("Total:"));

    Ok(())
}

#[test]
fn test_regex_date_redaction() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let input_pdf = temp_dir.path().join("input.pdf");
    let output_pdf = temp_dir.path().join("output.pdf");

    // Create PDF with dates
    TestPdfBuilder::new()
        .with_title("Event Schedule")
        .with_content("Meeting: 2026-01-15")
        .with_content("Deadline: 2026-02-28")
        .with_content("Launch: 2026-12-31")
        .with_content("Invalid: 2026-13-40")
        .build(&input_pdf)?;

    // Verify dates are in input
    let input_text = extract_text(&input_pdf)?;
    assert!(input_text.contains("2026-01-15"));
    assert!(input_text.contains("2026-02-28"));

    // Redact dates using regex (YYYY-MM-DD format)
    let service = RedactionService::with_secure_strategy();
    let result = with_mupdf_lock!(service.redact(
        &input_pdf,
        &output_pdf,
        &[RedactionTarget::Regex(r"\d{4}-\d{2}-\d{2}".to_string())]
    ))?;

    // Verify redaction occurred (note: catches invalid date too)
    assert!(result.has_redactions());
    assert!(result.instances_redacted >= 3); // At least the 3 valid dates

    // Verify dates are removed from output
    let output_text = extract_text(&output_pdf)?;
    assert!(!output_text.contains("2026-01-15"));
    assert!(!output_text.contains("2026-02-28"));
    assert!(output_text.contains("Meeting:"));
    assert!(output_text.contains("Deadline:"));

    Ok(())
}

#[test]
fn test_regex_custom_id_pattern() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let input_pdf = temp_dir.path().join("input.pdf");
    let output_pdf = temp_dir.path().join("output.pdf");

    // Create PDF with custom ID format (AA123456)
    TestPdfBuilder::new()
        .with_title("ID Registry")
        .with_content("Employee ID: AB123456")
        .with_content("Badge: XY789012")
        .with_content("Access Code: QW345678")
        .with_content("Invalid: 123456AB")
        .build(&input_pdf)?;

    // Verify IDs are in input
    let input_text = extract_text(&input_pdf)?;
    assert!(input_text.contains("AB123456"));
    assert!(input_text.contains("XY789012"));

    // Redact custom ID format (2 uppercase letters + 6 digits, without word boundaries)
    let service = RedactionService::with_secure_strategy();
    let result = with_mupdf_lock!(service.redact(
        &input_pdf,
        &output_pdf,
        &[RedactionTarget::Regex(r"[A-Z]{2}\d{6}".to_string())]
    ))?;

    // Verify redaction occurred
    assert!(result.has_redactions());
    assert_eq!(result.instances_redacted, 3);

    // Verify IDs are removed from output
    let output_text = extract_text(&output_pdf)?;
    assert!(!output_text.contains("AB123456"));
    assert!(!output_text.contains("XY789012"));
    assert!(!output_text.contains("QW345678"));
    assert!(output_text.contains("Invalid: 123456AB")); // Not matching pattern

    Ok(())
}

#[test]
fn test_regex_multiple_patterns() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let input_pdf = temp_dir.path().join("input.pdf");
    let output_pdf = temp_dir.path().join("output.pdf");

    // Create PDF with mixed sensitive data
    TestPdfBuilder::new()
        .with_title("Mixed Data")
        .with_content("SSN: 123-45-6789")
        .with_content("Email: user@example.com")
        .with_content("Phone: (555) 234-5678")
        .with_content("Amount: $1,000.00")
        .build(&input_pdf)?;

    // Verify all data is in input
    let input_text = extract_text(&input_pdf)?;
    assert!(input_text.contains("123-45-6789"));
    assert!(input_text.contains("user@example.com"));
    assert!(input_text.contains("(555) 234-5678"));
    assert!(input_text.contains("$1,000.00"));

    // Redact using multiple regex patterns (without word boundaries for PDF text)
    let service = RedactionService::with_secure_strategy();
    let result = with_mupdf_lock!(service.redact(
        &input_pdf,
        &output_pdf,
        &[
            RedactionTarget::Regex(r"\d{3}-\d{2}-\d{4}".to_string()), // SSN
            RedactionTarget::Regex(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}".to_string()), // Email
            RedactionTarget::Regex(r"\$[\d,]+\.\d{2}".to_string()), // Currency
        ]
    ))?;

    // Verify redaction occurred
    assert!(result.has_redactions());
    assert!(result.instances_redacted >= 3);

    // Verify sensitive data is removed from output
    let output_text = extract_text(&output_pdf)?;
    assert!(!output_text.contains("123-45-6789"));
    assert!(!output_text.contains("user@example.com"));
    assert!(!output_text.contains("$1,000.00"));
    // Phone might still be there since we didn't include phone pattern

    Ok(())
}

#[test]
fn test_regex_case_insensitive_pattern() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let input_pdf = temp_dir.path().join("input.pdf");
    let output_pdf = temp_dir.path().join("output.pdf");

    // Create PDF with mixed case keywords
    TestPdfBuilder::new()
        .with_title("Confidential Data")
        .with_content("Status: CONFIDENTIAL document")
        .with_content("Note: This is confidential")
        .with_content("Level: Confidential info")
        .build(&input_pdf)?;

    // Verify keywords are in input
    let input_text = extract_text(&input_pdf)?;
    assert!(input_text.to_lowercase().contains("confidential"));

    // Redact "CONFIDENTIAL" in any case using case-insensitive regex (without word boundaries)
    let service = RedactionService::with_secure_strategy();
    let result = with_mupdf_lock!(service.redact(
        &input_pdf,
        &output_pdf,
        &[RedactionTarget::Regex(r"(?i)CONFIDENTIAL".to_string())]
    ))?;

    // Verify redaction occurred
    assert!(result.has_redactions());
    assert!(result.instances_redacted >= 3); // At least 3 instances

    // Verify keywords are removed from output
    let output_text = extract_text(&output_pdf)?;
    assert!(!output_text.to_lowercase().contains("confidential"));

    Ok(())
}

#[test]
fn test_regex_ip_address_redaction() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let input_pdf = temp_dir.path().join("input.pdf");
    let output_pdf = temp_dir.path().join("output.pdf");

    // Create PDF with IP addresses
    TestPdfBuilder::new()
        .with_title("Network Configuration")
        .with_content("Server: 192.168.1.100")
        .with_content("Gateway: 10.0.0.1")
        .with_content("DNS: 8.8.8.8")
        .with_content("Invalid: 999.999.999.999")
        .build(&input_pdf)?;

    // Verify IPs are in input
    let input_text = extract_text(&input_pdf)?;
    assert!(input_text.contains("192.168.1.100"));
    assert!(input_text.contains("10.0.0.1"));

    // Redact IP addresses (simple pattern, without word boundaries)
    let service = RedactionService::with_secure_strategy();
    let result = with_mupdf_lock!(service.redact(
        &input_pdf,
        &output_pdf,
        &[RedactionTarget::Regex(
            r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}".to_string()
        )]
    ))?;

    // Verify redaction occurred
    assert!(result.has_redactions());
    assert_eq!(result.instances_redacted, 4); // Includes invalid IP

    // Verify IPs are removed from output
    let output_text = extract_text(&output_pdf)?;
    assert!(!output_text.contains("192.168.1.100"));
    assert!(!output_text.contains("10.0.0.1"));
    assert!(!output_text.contains("8.8.8.8"));

    Ok(())
}

#[test]
fn test_regex_credit_card_redaction() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let input_pdf = temp_dir.path().join("input.pdf");
    let output_pdf = temp_dir.path().join("output.pdf");

    // Create PDF with credit card numbers
    TestPdfBuilder::new()
        .with_title("Payment Information")
        .with_content("Card 1: 4532-1234-5678-9010")
        .with_content("Card 2: 5425-2334-3010-9876")
        .with_content("Card 3: 3782 822463 10005")
        .build(&input_pdf)?;

    // Verify card numbers are in input
    let input_text = extract_text(&input_pdf)?;
    assert!(input_text.contains("4532-1234-5678-9010"));

    // Redact credit card numbers (groups of 4 digits, without word boundaries)
    let service = RedactionService::with_secure_strategy();
    let result = with_mupdf_lock!(service.redact(
        &input_pdf,
        &output_pdf,
        &[RedactionTarget::Regex(
            r"\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{3,4}".to_string()
        )]
    ))?;

    // Verify redaction occurred
    assert!(result.has_redactions());
    assert!(result.instances_redacted >= 1); // At least one card matched

    // Verify card numbers are removed from output
    let output_text = extract_text(&output_pdf)?;
    assert!(!output_text.contains("4532-1234-5678-9010"));
    assert!(!output_text.contains("5425-2334-3010-9876"));

    Ok(())
}

#[test]
fn test_regex_url_redaction() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let input_pdf = temp_dir.path().join("input.pdf");
    let output_pdf = temp_dir.path().join("output.pdf");

    // Create PDF with URLs
    TestPdfBuilder::new()
        .with_title("Web Resources")
        .with_content("Website: https://example.com/page")
        .with_content("API: http://api.service.com/v1/endpoint")
        .with_content("Docs: https://docs.internal.org")
        .build(&input_pdf)?;

    // Verify URLs are in input
    let input_text = extract_text(&input_pdf)?;
    assert!(input_text.contains("https://example.com/page"));
    assert!(input_text.contains("http://api.service.com"));

    // Redact URLs
    let service = RedactionService::with_secure_strategy();
    let result = with_mupdf_lock!(service.redact(
        &input_pdf,
        &output_pdf,
        &[RedactionTarget::Regex(r"https?://[^\s]+".to_string())]
    ))?;

    // Verify redaction occurred
    assert!(result.has_redactions());
    assert_eq!(result.instances_redacted, 3);

    // Verify URLs are removed from output
    let output_text = extract_text(&output_pdf)?;
    assert!(!output_text.contains("https://example.com"));
    assert!(!output_text.contains("http://api.service.com"));
    // Note: "Website:" and "API:" may be redacted if they're part of the URL in extracted text

    Ok(())
}

#[test]
fn test_regex_invalid_pattern_error() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let input_pdf = temp_dir.path().join("input.pdf");
    let output_pdf = temp_dir.path().join("output.pdf");

    // Create a simple PDF
    TestPdfBuilder::new()
        .with_title("Test Document")
        .with_content("Some content here")
        .build(&input_pdf)?;

    // Try to redact with invalid regex pattern
    let service = RedactionService::with_secure_strategy();
    let result = with_mupdf_lock!(service.redact(
        &input_pdf,
        &output_pdf,
        &[RedactionTarget::Regex(r"[invalid(regex".to_string())] // Invalid regex
    ));

    // Verify error is returned
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(err.to_string().contains("Invalid regex pattern") || err.to_string().contains("regex"));

    Ok(())
}

#[test]
fn test_regex_no_matches() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let input_pdf = temp_dir.path().join("input.pdf");
    let output_pdf = temp_dir.path().join("output.pdf");

    // Create PDF without matching patterns
    TestPdfBuilder::new()
        .with_title("No Sensitive Data")
        .with_content("This document contains no SSNs")
        .with_content("or email addresses")
        .build(&input_pdf)?;

    // Try to redact SSNs (none exist)
    let service = RedactionService::with_secure_strategy();
    let result = with_mupdf_lock!(service.redact(
        &input_pdf,
        &output_pdf,
        &[RedactionTarget::Regex(r"\d{3}-\d{2}-\d{4}".to_string())]
    ))?;

    // Verify no redaction occurred
    assert!(!result.has_redactions());
    assert_eq!(result.instances_redacted, 0);

    // Verify output is created and content preserved
    let output_text = extract_text(&output_pdf)?;
    assert!(output_text.contains("No Sensitive Data"));
    assert!(output_text.contains("no SSNs"));

    Ok(())
}

#[test]
fn test_regex_empty_pattern() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let input_pdf = temp_dir.path().join("input.pdf");
    let output_pdf = temp_dir.path().join("output.pdf");

    // Create a simple PDF
    TestPdfBuilder::new()
        .with_title("Test Document")
        .with_content("Some content")
        .build(&input_pdf)?;

    // Try to redact with empty regex pattern
    let service = RedactionService::with_secure_strategy();
    let _result = with_mupdf_lock!(service.redact(
        &input_pdf,
        &output_pdf,
        &[RedactionTarget::Regex("".to_string())]
    )?);

    // Empty regex is technically valid in Rust regex crate (matches empty strings everywhere)
    // Should complete without error
    // instances_redacted count depends on how many empty strings are found

    Ok(())
}

#[test]
fn test_regex_combined_with_other_targets() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let input_pdf = temp_dir.path().join("input.pdf");
    let output_pdf = temp_dir.path().join("output.pdf");

    // Create PDF with phone and SSN
    TestPdfBuilder::new()
        .with_title("Personal Information")
        .with_phone("(555) 234-5678")
        .with_content("SSN: 123-45-6789")
        .with_content("Email: test@example.com")
        .build(&input_pdf)?;

    // Verify all data is in input
    let input_text = extract_text(&input_pdf)?;
    assert!(input_text.contains("(555) 234-5678"));
    assert!(input_text.contains("123-45-6789"));
    assert!(input_text.contains("test@example.com"));

    // Redact using phone target AND regex for SSN (without word boundaries)
    let service = RedactionService::with_secure_strategy();
    let result = with_mupdf_lock!(service.redact(
        &input_pdf,
        &output_pdf,
        &[
            RedactionTarget::PhoneNumbers,
            RedactionTarget::Regex(r"\d{3}-\d{2}-\d{4}".to_string()),
        ]
    ))?;

    // Verify redaction occurred
    assert!(result.has_redactions());
    assert!(result.instances_redacted >= 1); // At least something was redacted

    // Verify SSN is removed from output
    let output_text = extract_text(&output_pdf)?;
    assert!(!output_text.contains("123-45-6789"));

    Ok(())
}

#[test]
fn test_regex_word_boundary_patterns() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let input_pdf = temp_dir.path().join("input.pdf");
    let output_pdf = temp_dir.path().join("output.pdf");

    // Create PDF with words that might partially match
    TestPdfBuilder::new()
        .with_title("Word Boundaries")
        .with_content("The secret code is: SECRET")
        .with_content("This is not a SECRET_CODE")
        .with_content("But this is SECRET again")
        .build(&input_pdf)?;

    // Verify content is in input
    let input_text = extract_text(&input_pdf)?;
    assert!(input_text.contains("SECRET"));

    // Redact "SECRET" - note: word boundaries may not work as expected in PDF text
    let service = RedactionService::with_secure_strategy();
    let result = with_mupdf_lock!(service.redact(
        &input_pdf,
        &output_pdf,
        &[RedactionTarget::Regex(r"SECRET".to_string())]
    ))?;

    // Verify redaction occurred - will match all occurrences including SECRET_CODE
    assert!(result.has_redactions());
    // Note: May match all instances of SECRET, including within SECRET_CODE

    Ok(())
}

#[test]
fn test_regex_multiline_content() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let input_pdf = temp_dir.path().join("input.pdf");
    let output_pdf = temp_dir.path().join("output.pdf");

    // Create PDF with content across multiple lines
    TestPdfBuilder::new()
        .with_title("Multiline Document")
        .with_content("Line 1: ID-12345")
        .with_content("Line 2: ID-67890")
        .with_content("Line 3: ID-11111")
        .with_content("Line 4: No ID here")
        .with_content("Line 5: ID-99999")
        .build(&input_pdf)?;

    // Verify IDs are in input
    let input_text = extract_text(&input_pdf)?;
    assert!(input_text.contains("ID-12345"));
    assert!(input_text.contains("ID-67890"));

    // Redact all ID patterns
    let service = RedactionService::with_secure_strategy();
    let result = with_mupdf_lock!(service.redact(
        &input_pdf,
        &output_pdf,
        &[RedactionTarget::Regex(r"ID-\d{5}".to_string())]
    ))?;

    // Verify redaction occurred
    assert!(result.has_redactions());
    assert_eq!(result.instances_redacted, 4);

    // Verify IDs are removed from output
    let output_text = extract_text(&output_pdf)?;
    assert!(!output_text.contains("ID-12345"));
    assert!(!output_text.contains("ID-67890"));
    assert!(output_text.contains("No ID here"));

    Ok(())
}

#[test]
fn test_regex_special_characters_in_pattern() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let input_pdf = temp_dir.path().join("input.pdf");
    let output_pdf = temp_dir.path().join("output.pdf");

    // Create PDF with special characters
    TestPdfBuilder::new()
        .with_title("Special Chars")
        .with_content("Reference: [REF-001]")
        .with_content("Code: [REF-002]")
        .with_content("ID: [REF-003]")
        .build(&input_pdf)?;

    // Verify references are in input
    let input_text = extract_text(&input_pdf)?;
    assert!(input_text.contains("[REF-001]"));

    // Redact reference patterns with brackets
    let service = RedactionService::with_secure_strategy();
    let result = with_mupdf_lock!(service.redact(
        &input_pdf,
        &output_pdf,
        &[RedactionTarget::Regex(r"\[REF-\d{3}\]".to_string())]
    ))?;

    // Verify redaction occurred
    assert!(result.has_redactions());
    assert_eq!(result.instances_redacted, 3);

    // Verify references are removed from output
    let output_text = extract_text(&output_pdf)?;
    assert!(!output_text.contains("[REF-001]"));
    assert!(!output_text.contains("[REF-002]"));
    assert!(output_text.contains("Reference:"));

    Ok(())
}