oxur-odm 0.3.5

An odd document manager - CLI tool for managing design documentation
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
//! Scan command implementation

use anyhow::Result;
use colored::*;
use design::doc::state_from_directory;
use design::state::StateManager;
use std::path::PathBuf;

/// Scan filesystem and validate/update state
pub fn scan_documents(state_mgr: &mut StateManager, fix: bool, verbose: bool) -> Result<()> {
    println!("\n{}\n", "Scanning documents...".bold());

    let result = state_mgr.scan_for_changes()?;

    // Report changes
    if result.has_changes() {
        if !result.new_files.is_empty() {
            println!("{}", "New Files:".green().bold());
            for num in &result.new_files {
                if let Some(record) = state_mgr.state().get(*num) {
                    println!("  {} {:04} - {}", "+".green(), num, record.metadata.title);
                }
            }
            println!();
        }

        if !result.changed.is_empty() {
            println!("{}", "Modified Files:".yellow().bold());
            for num in &result.changed {
                if let Some(record) = state_mgr.state().get(*num) {
                    println!("  {} {:04} - {}", "~".yellow(), num, record.metadata.title);
                }
            }
            println!();
        }

        if !result.deleted.is_empty() {
            println!("{}", "Deleted Files:".red().bold());
            for num in &result.deleted {
                println!("  {} {:04}", "-".red(), num);
            }
            println!();
        }
    } else {
        println!("{} No changes detected\n", "✓".green().bold());
    }

    // Report errors
    if !result.errors.is_empty() {
        println!("{}", "Errors:".red().bold());
        for error in &result.errors {
            println!("  {} {}", "✗".red(), error);
        }
        println!();
    }

    // Validate consistency
    if verbose {
        validate_consistency(state_mgr, fix)?;
    }

    // Summary
    println!(
        "{} State updated: {} documents tracked\n",
        "✓".green().bold(),
        state_mgr.state().documents.len()
    );

    Ok(())
}

fn validate_consistency(state_mgr: &StateManager, fix: bool) -> Result<()> {
    println!("{}", "Validating Consistency:".bold());

    let mut inconsistencies = 0;
    let mut fixable = Vec::new();

    for record in state_mgr.state().all() {
        let full_path = PathBuf::from(state_mgr.docs_dir()).join(&record.path);

        // Check if file exists
        if !full_path.exists() {
            println!(
                "  {} {:04} - File not found: {}",
                "✗".red(),
                record.metadata.number,
                record.path
            );
            inconsistencies += 1;
            continue;
        }

        // Check state/directory consistency
        if let Some(dir_state) = state_from_directory(&full_path) {
            if record.metadata.state != dir_state {
                println!(
                    "  {} {:04} - State mismatch: YAML='{}' Directory='{}'",
                    "âš ".yellow(),
                    record.metadata.number,
                    record.metadata.state.as_str(),
                    dir_state.as_str()
                );
                inconsistencies += 1;
                fixable.push((record.metadata.number, full_path.clone()));
            }
        }
    }

    if inconsistencies == 0 {
        println!("  {} All documents consistent", "✓".green());
    } else {
        println!("  {} {} inconsistencies found", "âš ".yellow(), inconsistencies);

        if fix && !fixable.is_empty() {
            println!("\n{}", "Fixing inconsistencies...".bold());
            for (num, path) in &fixable {
                println!("  Syncing {:04}: {}", num, path.display());
                // Note: actual fix would call sync_location here
                // For now, just report what would be fixed
            }
        } else if !fixable.is_empty() {
            println!(
                "\n{} Run with {} to fix {} issue(s)",
                "→".cyan(),
                "--fix".cyan(),
                fixable.len()
            );
        }
    }

    println!();
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use design::doc::DocState;
    use std::fs;
    use tempfile::TempDir;

    fn create_test_doc_content(number: u32, title: &str, state: DocState) -> String {
        format!(
            "---\nnumber: {}\ntitle: \"{}\"\nauthor: \"Test Author\"\ncreated: 2024-01-01\nupdated: 2024-01-01\nstate: {}\n---\n\n# {}\n\nTest content",
            number, title, state.as_str(), title
        )
    }

    #[test]
    fn test_scan_no_changes() {
        let temp = TempDir::new().unwrap();

        // Create initial state with one document
        let content = create_test_doc_content(1, "Test Doc", DocState::Draft);
        let doc_path = temp.path().join("0001-test.md");
        fs::write(&doc_path, content).unwrap();

        // Initialize state manager
        let mut state_mgr = StateManager::new(temp.path()).unwrap();
        state_mgr.scan_for_changes().unwrap();

        // Scan again - should find no changes
        let result = scan_documents(&mut state_mgr, false, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_scan_new_file() {
        let temp = TempDir::new().unwrap();

        // Initialize empty state manager
        let mut state_mgr = StateManager::new(temp.path()).unwrap();

        // Create a new document file
        let content = create_test_doc_content(1, "New Doc", DocState::Draft);
        fs::write(temp.path().join("0001-new.md"), content).unwrap();

        // Scan should detect new file
        let result = scan_documents(&mut state_mgr, false, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_scan_with_verbose() {
        let temp = TempDir::new().unwrap();

        // Create document
        let content = create_test_doc_content(1, "Test Doc", DocState::Draft);
        fs::write(temp.path().join("0001-test.md"), content).unwrap();

        let mut state_mgr = StateManager::new(temp.path()).unwrap();

        // Scan with verbose mode
        let result = scan_documents(&mut state_mgr, false, true);
        assert!(result.is_ok());
    }

    #[test]
    fn test_scan_with_fix() {
        let temp = TempDir::new().unwrap();

        // Create document
        let content = create_test_doc_content(1, "Test Doc", DocState::Draft);
        fs::write(temp.path().join("0001-test.md"), content).unwrap();

        let mut state_mgr = StateManager::new(temp.path()).unwrap();

        // Scan with fix mode
        let result = scan_documents(&mut state_mgr, true, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_scan_empty_directory() {
        let temp = TempDir::new().unwrap();
        let mut state_mgr = StateManager::new(temp.path()).unwrap();

        // Scan empty directory
        let result = scan_documents(&mut state_mgr, false, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_scan_multiple_states() {
        let temp = TempDir::new().unwrap();

        // Create documents in different states
        for (num, state) in [(1, DocState::Draft), (2, DocState::Final), (3, DocState::Active)] {
            let content = create_test_doc_content(num, &format!("Doc {}", num), state);
            fs::write(temp.path().join(format!("{:04}-test.md", num)), content).unwrap();
        }

        let mut state_mgr = StateManager::new(temp.path()).unwrap();

        let result = scan_documents(&mut state_mgr, false, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_consistency() {
        let temp = TempDir::new().unwrap();

        // Create document
        let content = create_test_doc_content(1, "Test Doc", DocState::Draft);
        fs::write(temp.path().join("0001-test.md"), content).unwrap();

        let state_mgr = StateManager::new(temp.path()).unwrap();

        // Validate consistency (should pass with no issues)
        let result = validate_consistency(&state_mgr, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_consistency_with_fix() {
        let temp = TempDir::new().unwrap();

        // Create document
        let content = create_test_doc_content(1, "Test Doc", DocState::Draft);
        fs::write(temp.path().join("0001-test.md"), content).unwrap();

        let state_mgr = StateManager::new(temp.path()).unwrap();

        // Validate with fix mode
        let result = validate_consistency(&state_mgr, true);
        assert!(result.is_ok());
    }

    #[test]
    fn test_scan_verbose_and_fix() {
        let temp = TempDir::new().unwrap();

        // Create document
        let content = create_test_doc_content(1, "Test Doc", DocState::Draft);
        fs::write(temp.path().join("0001-test.md"), content).unwrap();

        let mut state_mgr = StateManager::new(temp.path()).unwrap();

        // Scan with both verbose and fix
        let result = scan_documents(&mut state_mgr, true, true);
        assert!(result.is_ok());
    }

    #[test]
    fn test_scan_with_new_files_display() {
        let temp = TempDir::new().unwrap();

        // Create draft directory for documents
        let draft_dir = temp.path().join("01-draft");
        fs::create_dir_all(&draft_dir).unwrap();

        // Initialize empty state manager
        let mut state_mgr = StateManager::new(temp.path()).unwrap();

        // Create multiple new document files in draft directory
        for i in 1..=3 {
            let content = create_test_doc_content(i, &format!("New Doc {}", i), DocState::Draft);
            fs::write(draft_dir.join(format!("{:04}-new.md", i)), content).unwrap();
        }

        // Scan should detect new files and print them
        let result = scan_documents(&mut state_mgr, false, false);
        assert!(result.is_ok());

        // Verify all files are in state now
        assert_eq!(state_mgr.state().documents.len(), 3);
    }

    #[test]
    fn test_scan_with_changed_files_display() {
        let temp = TempDir::new().unwrap();

        // Create draft directory
        let draft_dir = temp.path().join("01-draft");
        fs::create_dir_all(&draft_dir).unwrap();

        // Create initial document
        let content = create_test_doc_content(1, "Original Doc", DocState::Draft);
        let doc_path = draft_dir.join("0001-original.md");
        fs::write(&doc_path, &content).unwrap();

        let mut state_mgr = StateManager::new(temp.path()).unwrap();
        state_mgr.scan_for_changes().unwrap();

        // Modify the file
        let modified_content = content + "\n\nAdditional content added";
        fs::write(&doc_path, modified_content).unwrap();

        // Scan should detect changed file and print it
        let result = scan_documents(&mut state_mgr, false, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_scan_with_deleted_files_display() {
        let temp = TempDir::new().unwrap();

        // Create draft directory
        let draft_dir = temp.path().join("01-draft");
        fs::create_dir_all(&draft_dir).unwrap();

        // Create initial documents
        for i in 1..=3 {
            let content = create_test_doc_content(i, &format!("Doc {}", i), DocState::Draft);
            fs::write(draft_dir.join(format!("{:04}-doc.md", i)), content).unwrap();
        }

        let mut state_mgr = StateManager::new(temp.path()).unwrap();
        state_mgr.scan_for_changes().unwrap();

        // Delete some files
        fs::remove_file(draft_dir.join("0001-doc.md")).unwrap();
        fs::remove_file(draft_dir.join("0002-doc.md")).unwrap();

        // Scan should detect deleted files and print them
        let result = scan_documents(&mut state_mgr, false, false);
        assert!(result.is_ok());

        // Only one document should remain in state
        assert_eq!(state_mgr.state().documents.len(), 1);
    }

    #[test]
    fn test_scan_with_errors_display() {
        let temp = TempDir::new().unwrap();

        // Create draft directory
        let draft_dir = temp.path().join("01-draft");
        fs::create_dir_all(&draft_dir).unwrap();

        // Create a valid document
        let valid_content = create_test_doc_content(1, "Valid Doc", DocState::Draft);
        fs::write(draft_dir.join("0001-valid.md"), valid_content).unwrap();

        // Create an invalid document (missing frontmatter)
        fs::write(draft_dir.join("0002-invalid.md"), "Just plain text without frontmatter")
            .unwrap();

        let mut state_mgr = StateManager::new(temp.path()).unwrap();

        // Scan should detect error and print it
        let result = scan_documents(&mut state_mgr, false, false);
        assert!(result.is_ok());

        // Only valid document should be in state
        assert_eq!(state_mgr.state().documents.len(), 1);
    }

    #[test]
    fn test_scan_with_mixed_changes() {
        let temp = TempDir::new().unwrap();

        // Create draft directory
        let draft_dir = temp.path().join("01-draft");
        fs::create_dir_all(&draft_dir).unwrap();

        // Create initial document
        let content1 = create_test_doc_content(1, "Existing Doc", DocState::Draft);
        let doc1_path = draft_dir.join("0001-existing.md");
        fs::write(&doc1_path, &content1).unwrap();

        let mut state_mgr = StateManager::new(temp.path()).unwrap();
        state_mgr.scan_for_changes().unwrap();

        // Add new file
        let content2 = create_test_doc_content(2, "New Doc", DocState::Draft);
        fs::write(draft_dir.join("0002-new.md"), content2).unwrap();

        // Modify existing file
        let modified_content = content1 + "\n\nModified content";
        fs::write(&doc1_path, modified_content).unwrap();

        // Add invalid file (will cause error)
        fs::write(draft_dir.join("0003-invalid.md"), "Invalid content").unwrap();

        // Scan should show new, changed, and errors sections
        let result = scan_documents(&mut state_mgr, false, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_consistency_missing_file() {
        let temp = TempDir::new().unwrap();

        // Create draft directory
        let draft_dir = temp.path().join("01-draft");
        fs::create_dir_all(&draft_dir).unwrap();

        // Create document and scan it
        let content = create_test_doc_content(1, "Test Doc", DocState::Draft);
        let doc_path = draft_dir.join("0001-test.md");
        fs::write(&doc_path, content).unwrap();

        let mut state_mgr = StateManager::new(temp.path()).unwrap();
        state_mgr.scan_for_changes().unwrap();

        // Delete the file but keep it in state
        fs::remove_file(&doc_path).unwrap();

        // Validate should detect missing file
        let result = validate_consistency(&state_mgr, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_consistency_state_mismatch() {
        let temp = TempDir::new().unwrap();

        // Create state directories
        let draft_dir = temp.path().join("01-draft");
        let final_dir = temp.path().join("06-final");
        fs::create_dir_all(&draft_dir).unwrap();
        fs::create_dir_all(&final_dir).unwrap();

        // Create document in draft directory but with Final state in YAML
        let content = create_test_doc_content(1, "Test Doc", DocState::Final);
        fs::write(draft_dir.join("0001-test.md"), content).unwrap();

        let mut state_mgr = StateManager::new(temp.path()).unwrap();
        state_mgr.scan_for_changes().unwrap();

        // Validate should detect state mismatch
        let result = validate_consistency(&state_mgr, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_consistency_with_fix_mode() {
        let temp = TempDir::new().unwrap();

        // Create state directories
        let draft_dir = temp.path().join("01-draft");
        let final_dir = temp.path().join("06-final");
        fs::create_dir_all(&draft_dir).unwrap();
        fs::create_dir_all(&final_dir).unwrap();

        // Create document with state mismatch
        let content = create_test_doc_content(1, "Test Doc", DocState::Final);
        fs::write(draft_dir.join("0001-test.md"), content).unwrap();

        let mut state_mgr = StateManager::new(temp.path()).unwrap();
        state_mgr.scan_for_changes().unwrap();

        // Validate with fix mode should detect and offer to fix
        let result = validate_consistency(&state_mgr, true);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_consistency_no_fix_suggestion() {
        let temp = TempDir::new().unwrap();

        // Create state directories
        let draft_dir = temp.path().join("01-draft");
        fs::create_dir_all(&draft_dir).unwrap();

        // Create document with state mismatch
        let content = create_test_doc_content(1, "Test Doc", DocState::Final);
        fs::write(draft_dir.join("0001-test.md"), content).unwrap();

        let mut state_mgr = StateManager::new(temp.path()).unwrap();
        state_mgr.scan_for_changes().unwrap();

        // Validate without fix mode should suggest using --fix
        let result = validate_consistency(&state_mgr, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_consistency_all_consistent() {
        let temp = TempDir::new().unwrap();

        // Create state directory
        let draft_dir = temp.path().join("01-draft");
        fs::create_dir_all(&draft_dir).unwrap();

        // Create document with matching state
        let content = create_test_doc_content(1, "Test Doc", DocState::Draft);
        fs::write(draft_dir.join("0001-test.md"), content).unwrap();

        let mut state_mgr = StateManager::new(temp.path()).unwrap();
        state_mgr.scan_for_changes().unwrap();

        // Validate should find no issues
        let result = validate_consistency(&state_mgr, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_consistency_multiple_mismatches() {
        let temp = TempDir::new().unwrap();

        // Create state directories
        let draft_dir = temp.path().join("01-draft");
        let active_dir = temp.path().join("05-active");
        fs::create_dir_all(&draft_dir).unwrap();
        fs::create_dir_all(&active_dir).unwrap();

        // Create multiple documents with state mismatches
        let content1 = create_test_doc_content(1, "Doc 1", DocState::Active);
        fs::write(draft_dir.join("0001-doc1.md"), content1).unwrap();

        let content2 = create_test_doc_content(2, "Doc 2", DocState::Draft);
        fs::write(active_dir.join("0002-doc2.md"), content2).unwrap();

        let mut state_mgr = StateManager::new(temp.path()).unwrap();
        state_mgr.scan_for_changes().unwrap();

        // Validate should detect multiple mismatches
        let result = validate_consistency(&state_mgr, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_consistency_with_missing_and_mismatch() {
        let temp = TempDir::new().unwrap();

        // Create state directories
        let draft_dir = temp.path().join("01-draft");
        fs::create_dir_all(&draft_dir).unwrap();

        // Create documents
        let content1 = create_test_doc_content(1, "Missing Doc", DocState::Draft);
        let doc1_path = draft_dir.join("0001-missing.md");
        fs::write(&doc1_path, content1).unwrap();

        let content2 = create_test_doc_content(2, "Mismatch Doc", DocState::Final);
        fs::write(draft_dir.join("0002-mismatch.md"), content2).unwrap();

        let mut state_mgr = StateManager::new(temp.path()).unwrap();
        state_mgr.scan_for_changes().unwrap();

        // Delete one file
        fs::remove_file(&doc1_path).unwrap();

        // Validate should detect both issues
        let result = validate_consistency(&state_mgr, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_scan_verbose_shows_validation() {
        let temp = TempDir::new().unwrap();

        // Create state directory
        let draft_dir = temp.path().join("01-draft");
        fs::create_dir_all(&draft_dir).unwrap();

        // Create document with state mismatch
        let content = create_test_doc_content(1, "Test Doc", DocState::Final);
        fs::write(draft_dir.join("0001-test.md"), content).unwrap();

        let mut state_mgr = StateManager::new(temp.path()).unwrap();

        // Verbose mode should run validation
        let result = scan_documents(&mut state_mgr, false, true);
        assert!(result.is_ok());
    }

    #[test]
    fn test_scan_quiet_skips_validation() {
        let temp = TempDir::new().unwrap();

        // Create state directory
        let draft_dir = temp.path().join("01-draft");
        fs::create_dir_all(&draft_dir).unwrap();

        // Create document with state mismatch
        let content = create_test_doc_content(1, "Test Doc", DocState::Final);
        fs::write(draft_dir.join("0001-test.md"), content).unwrap();

        let mut state_mgr = StateManager::new(temp.path()).unwrap();

        // Non-verbose mode should skip validation
        let result = scan_documents(&mut state_mgr, false, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_consistency_file_without_directory_state() {
        let temp = TempDir::new().unwrap();

        // Create draft directory first
        let draft_dir = temp.path().join("01-draft");
        fs::create_dir_all(&draft_dir).unwrap();

        // Create document in draft directory (which will have state from directory)
        let content = create_test_doc_content(1, "Test Doc", DocState::Draft);
        fs::write(draft_dir.join("0001-test.md"), content).unwrap();

        let mut state_mgr = StateManager::new(temp.path()).unwrap();
        state_mgr.scan_for_changes().unwrap();

        // Validate should handle file correctly
        let result = validate_consistency(&state_mgr, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_consistency_with_fix_and_no_fixable() {
        let temp = TempDir::new().unwrap();

        // Create draft directory
        let draft_dir = temp.path().join("01-draft");
        fs::create_dir_all(&draft_dir).unwrap();

        // Create document and scan it
        let content = create_test_doc_content(1, "Test Doc", DocState::Draft);
        let doc_path = draft_dir.join("0001-test.md");
        fs::write(&doc_path, content).unwrap();

        let mut state_mgr = StateManager::new(temp.path()).unwrap();
        state_mgr.scan_for_changes().unwrap();

        // Delete the file (creates non-fixable inconsistency)
        fs::remove_file(&doc_path).unwrap();

        // Validate with fix mode but nothing to fix
        let result = validate_consistency(&state_mgr, true);
        assert!(result.is_ok());
    }

    #[test]
    fn test_scan_comprehensive_workflow() {
        let temp = TempDir::new().unwrap();

        // Setup state directories
        let draft_dir = temp.path().join("01-draft");
        let final_dir = temp.path().join("06-final");
        fs::create_dir_all(&draft_dir).unwrap();
        fs::create_dir_all(&final_dir).unwrap();

        // Initial scan - empty
        let mut state_mgr = StateManager::new(temp.path()).unwrap();
        let result = scan_documents(&mut state_mgr, false, false);
        assert!(result.is_ok());

        // Add some files
        let content1 = create_test_doc_content(1, "Doc 1", DocState::Draft);
        fs::write(draft_dir.join("0001-doc1.md"), content1).unwrap();

        // Scan - should find new file
        let result = scan_documents(&mut state_mgr, false, false);
        assert!(result.is_ok());
        assert_eq!(state_mgr.state().documents.len(), 1);

        // Scan again with verbose - no changes
        let result = scan_documents(&mut state_mgr, false, true);
        assert!(result.is_ok());

        // Add file with mismatch and scan with fix and verbose
        let content2 = create_test_doc_content(2, "Doc 2", DocState::Final);
        fs::write(draft_dir.join("0002-doc2.md"), content2).unwrap();
        let result = scan_documents(&mut state_mgr, true, true);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_consistency_empty_state() {
        let temp = TempDir::new().unwrap();

        let state_mgr = StateManager::new(temp.path()).unwrap();

        // Validate empty state should pass
        let result = validate_consistency(&state_mgr, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_scan_all_state_directories() {
        let temp = TempDir::new().unwrap();

        // Create documents in different state directories
        let states = vec![
            (DocState::Draft, "01-draft"),
            (DocState::UnderReview, "02-under-review"),
            (DocState::Revised, "03-revised"),
            (DocState::Accepted, "04-accepted"),
            (DocState::Active, "05-active"),
            (DocState::Final, "06-final"),
            (DocState::Deferred, "07-deferred"),
            (DocState::Rejected, "08-rejected"),
            (DocState::Withdrawn, "09-withdrawn"),
            (DocState::Superseded, "10-superseded"),
        ];

        for (i, (state, dir)) in states.iter().enumerate() {
            let state_dir = temp.path().join(dir);
            fs::create_dir_all(&state_dir).unwrap();

            let num = (i + 1) as u32;
            let content = create_test_doc_content(num, &format!("Doc {}", num), *state);
            fs::write(state_dir.join(format!("{:04}-doc.md", num)), content).unwrap();
        }

        let mut state_mgr = StateManager::new(temp.path()).unwrap();

        // Scan should find all documents
        let result = scan_documents(&mut state_mgr, false, false);
        assert!(result.is_ok());
        assert_eq!(state_mgr.state().documents.len(), 10);

        // Validate with verbose - all should be consistent
        let result = scan_documents(&mut state_mgr, false, true);
        assert!(result.is_ok());
    }
}