oxur-odm 0.2.0

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
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
806
807
808
809
810
811
812
813
//! Update index command implementation

use anyhow::{Context, Result};
use colored::*;
use design::constants::INDEX_FILENAME;
use design::doc::DesignDoc;
use design::index::DocumentIndex;
use design::index_sync::*;
use regex::Regex;
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

/// Synchronize the index with documents on filesystem
pub fn update_index(index: &DocumentIndex) -> Result<()> {
    println!("{}\n", "Synchronizing index with documents...".bold());

    let docs_dir = PathBuf::from(index.docs_dir());
    let index_path = docs_dir.join(INDEX_FILENAME);

    // Check if index exists
    if !index_path.exists() {
        println!(
            "{} Index file not found. Use 'odm index' to generate it first.",
            "Warning:".yellow().bold()
        );
        return Ok(());
    }

    // Read current index
    let current_content = fs::read_to_string(&index_path).context("Failed to read index file")?;

    // Parse current index
    let parsed = ParsedIndex::parse(&current_content).context("Failed to parse index")?;

    // Get all docs from filesystem
    let doc_paths = get_docs_from_filesystem(&docs_dir).context("Failed to scan documents")?;

    // Build document map
    let doc_map = build_doc_map(&doc_paths);

    // Compute changes
    let table_changes = compute_table_changes(&parsed, &doc_map);
    let section_changes = compute_section_changes(&parsed, &doc_map, &docs_dir);

    let mut all_changes = Vec::new();
    all_changes.extend(table_changes);
    all_changes.extend(section_changes);

    // Apply changes to content
    let mut updated_content = current_content.clone();

    for change in &all_changes {
        updated_content = apply_change(&updated_content, change, &doc_map, &docs_dir)?;
    }

    // Store pre-format for comparison
    let pre_format_content = updated_content.clone();

    // Apply formatting cleanup
    updated_content = cleanup_formatting(&updated_content);

    // Check if formatting made changes
    let formatting_changed = pre_format_content != updated_content;

    // Report changes
    if all_changes.is_empty() && !formatting_changed {
        println!("{}\n", "✓ Index is already up to date!".green());
        return Ok(());
    }

    // Report content changes
    if !all_changes.is_empty() {
        println!("{}", "Changes:".bold());
        for change in &all_changes {
            println!("  {} {}", "✓".green(), change.description());
        }
        println!();
    }

    // Report formatting changes
    if formatting_changed && all_changes.is_empty() {
        println!("{}", "Formatting:".bold());
        println!("  {} Applied spacing and formatting cleanup", "✓".green());
        println!();
    }

    // Write updated index
    fs::write(&index_path, &updated_content).context("Failed to write index")?;

    // Summary
    let change_count = all_changes.len();
    if change_count > 0 {
        println!("{} {} change(s) applied to index", "Summary:".bold(), change_count);
    } else {
        println!("{} Formatting cleanup applied", "Summary:".bold());
    }

    Ok(())
}

/// Apply a single change to the index content
fn apply_change(
    content: &str,
    change: &IndexChange,
    _doc_map: &HashMap<String, DesignDoc>,
    _docs_dir: &Path,
) -> Result<String> {
    match change {
        IndexChange::TableAdd { number, title, state, updated } => {
            add_to_table(content, number, title, state, updated)
        }
        IndexChange::TableUpdate { number, field, new, .. } => {
            update_table_field(content, number, field, new)
        }
        IndexChange::TableRemove { number } => remove_from_table(content, number),
        IndexChange::SectionAdd { state, number, title, path } => {
            add_to_section(content, state, number, title, path)
        }
        IndexChange::SectionRemove { state, path } => remove_from_section(content, state, path),
    }
}

/// Add a new row to the table in sorted order
fn add_to_table(
    content: &str,
    number: &str,
    title: &str,
    state: &str,
    updated: &str,
) -> Result<String> {
    let lines: Vec<&str> = content.lines().collect();
    let mut result = Vec::new();

    let doc_num: u32 = number.parse().unwrap_or(0);
    let mut inserted = false;
    let mut in_table = false;
    let mut passed_separator = false;

    for (idx, line) in lines.iter().enumerate() {
        // Detect table start
        if line.starts_with("| Number | Title") {
            in_table = true;
        }

        // Detect separator
        if in_table && line.contains("---|") {
            passed_separator = true;
            result.push(line.to_string());
            continue;
        }

        // Try to insert in sorted position (descending order - highest first)
        if in_table && passed_separator && line.starts_with("| ") && !inserted {
            let parts: Vec<&str> = line.split('|').collect();
            if parts.len() >= 2 {
                let row_num_str = parts[1].trim();
                if let Ok(row_num) = row_num_str.parse::<u32>() {
                    if doc_num > row_num {
                        // Insert before this row (higher numbers first)
                        result
                            .push(format!("| {} | {} | {} | {} |", number, title, state, updated));
                        inserted = true;
                    }
                }
            }
        }

        result.push(line.to_string());

        // If we're leaving the table and didn't insert, add at end
        if in_table && passed_separator && !inserted {
            let next_line = lines.get(idx + 1).unwrap_or(&"");
            if !next_line.starts_with('|') {
                result.pop();
                result.push(format!("| {} | {} | {} | {} |", number, title, state, updated));
                result.push(line.to_string());
                inserted = true;
                in_table = false;
            }
        }
    }

    Ok(result.join("\n"))
}

/// Update a field in the table
fn update_table_field(content: &str, number: &str, field: &str, new_value: &str) -> Result<String> {
    let lines: Vec<&str> = content.lines().collect();
    let mut result = Vec::new();

    for line in lines {
        if line.starts_with(&format!("| {} |", number)) {
            let parts: Vec<&str> = line.split('|').collect();
            if parts.len() >= 5 {
                let mut new_line = String::from("|");
                new_line.push_str(&format!(" {} |", parts[1].trim())); // number
                match field {
                    "title" => new_line.push_str(&format!(" {} |", new_value)),
                    _ => new_line.push_str(&format!(" {} |", parts[2].trim())),
                }
                match field {
                    "state" => new_line.push_str(&format!(" {} |", new_value)),
                    _ => new_line.push_str(&format!(" {} |", parts[3].trim())),
                }
                match field {
                    "updated" => new_line.push_str(&format!(" {} |", new_value)),
                    _ => new_line.push_str(&format!(" {} |", parts[4].trim())),
                }
                result.push(new_line);
            } else {
                result.push(line.to_string());
            }
        } else {
            result.push(line.to_string());
        }
    }

    Ok(result.join("\n"))
}

/// Remove a row from the table
fn remove_from_table(content: &str, number: &str) -> Result<String> {
    let lines: Vec<&str> = content.lines().collect();
    let result: Vec<String> = lines
        .into_iter()
        .filter(|line| !line.starts_with(&format!("| {} |", number)))
        .map(|s| s.to_string())
        .collect();

    Ok(result.join("\n"))
}

/// Add document to a state section in sorted order
fn add_to_section(
    content: &str,
    state: &str,
    number: &str,
    title: &str,
    path: &str,
) -> Result<String> {
    let lines: Vec<&str> = content.lines().collect();
    let mut result = Vec::new();

    let state_header = format!("### {}", state);
    let doc_num: u32 = number.parse().unwrap_or(0);
    let mut in_section = false;
    let mut section_exists = false;
    let mut inserted = false;
    let re = Regex::new(r"^\- \[(\d+)").unwrap();

    for (idx, line) in lines.iter().enumerate() {
        // Check if we're at the state section
        if *line == state_header {
            section_exists = true;
            in_section = true;
            result.push(line.to_string());
            continue;
        }

        // Check if we're leaving the section
        if in_section && (line.starts_with("### ") || line.starts_with("## ")) {
            // Insert before leaving if not yet inserted
            if !inserted {
                result.push(format!("- [{} - {}]({})", number, title, path));
                inserted = true;
            }
            in_section = false;
        }

        // Try to insert in sorted position within section (descending order - highest first)
        if in_section && line.starts_with("- [") && !inserted {
            if let Some(caps) = re.captures(line) {
                if let Some(num_match) = caps.get(1) {
                    if let Ok(existing_num) = num_match.as_str().parse::<u32>() {
                        if doc_num > existing_num {
                            result.push(format!("- [{} - {}]({})", number, title, path));
                            inserted = true;
                        }
                    }
                }
            }
        }

        result.push(line.to_string());

        // If at end of section content (blank line or end), insert
        if in_section && !inserted {
            let next_line = lines.get(idx + 1);
            let at_section_end = next_line.is_none()
                || next_line.unwrap().is_empty()
                || next_line.unwrap().starts_with('#');

            if at_section_end && line.starts_with("- [") {
                // We're at the last bullet, insert after
            } else if at_section_end && !line.starts_with("- [") && !line.is_empty() {
                result.push(format!("- [{} - {}]({})", number, title, path));
                inserted = true;
            }
        }
    }

    // If section doesn't exist, we need to create it
    if !section_exists {
        let mut final_result = Vec::new();
        let mut added_section = false;

        for line in &result {
            final_result.push(line.clone());

            // Add section after "## Documents by State" header
            if line == "## Documents by State" && !added_section {
                final_result.push(String::new());
                final_result.push(state_header.clone());
                final_result.push(String::new());
                final_result.push(format!("- [{} - {}]({})", number, title, path));
                added_section = true;
            }
        }

        return Ok(final_result.join("\n"));
    }

    // If in section at end and didn't insert
    if in_section && !inserted {
        result.push(format!("- [{} - {}]({})", number, title, path));
    }

    Ok(result.join("\n"))
}

/// Remove document from a state section
fn remove_from_section(content: &str, state: &str, path: &str) -> Result<String> {
    let lines: Vec<&str> = content.lines().collect();
    let mut result: Vec<String> = Vec::new();

    let state_header = format!("### {}", state);
    let mut in_section = false;
    let mut section_has_other_items = false;

    // First pass: check if section will be empty
    let mut temp_in_section = false;
    for line in &lines {
        if *line == state_header {
            temp_in_section = true;
            continue;
        }
        if temp_in_section && (line.starts_with("### ") || line.starts_with("## ")) {
            break;
        }
        if temp_in_section && line.starts_with("- [") && !line.contains(&format!("]({})", path)) {
            section_has_other_items = true;
            break;
        }
    }

    // Second pass: build result
    let mut skip_header = false;
    for line in &lines {
        // Check if entering section
        if *line == state_header {
            in_section = true;
            if !section_has_other_items {
                skip_header = true;
                // Also skip preceding blank line
                if !result.is_empty() && result.last().unwrap().is_empty() {
                    result.pop();
                }
                continue;
            }
            // Add the section header to result and continue to next line
            result.push(line.to_string());
            continue;
        }

        // Check if leaving section
        if in_section && (line.starts_with("### ") || line.starts_with("## ")) {
            in_section = false;
            skip_header = false;
        }

        // Skip the matching line
        if in_section && line.contains(&format!("]({})", path)) {
            continue;
        }

        // Skip if we're removing entire section
        if skip_header && (line.is_empty() || line.starts_with("- [")) {
            continue;
        }

        if skip_header && !line.is_empty() && !line.starts_with("- [") {
            skip_header = false;
        }

        result.push(line.to_string());
    }

    Ok(result.join("\n"))
}

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

    #[test]
    fn test_update_index_no_index_file() {
        let temp = TempDir::new().unwrap();
        let index = DocumentIndex::new(temp.path()).unwrap();

        // Should handle missing index gracefully
        let result = update_index(&index);
        assert!(result.is_ok());
    }

    #[test]
    fn test_update_index_with_empty_index() {
        let temp = TempDir::new().unwrap();
        let index = DocumentIndex::new(temp.path()).unwrap();

        // Create an empty index file
        let index_path = temp.path().join(INDEX_FILENAME);
        fs::write(
            &index_path,
            "# Design Document Index\n\n## All Documents by Number\n\n| Number | Title | State | Updated |\n|--------|-------|-------|----------|\n",
        )
        .unwrap();

        let result = update_index(&index);
        assert!(result.is_ok());
    }

    #[test]
    fn test_update_index_with_valid_index() {
        let temp = TempDir::new().unwrap();
        let index = DocumentIndex::new(temp.path()).unwrap();

        // Create a valid index file with a document entry
        let index_path = temp.path().join(INDEX_FILENAME);
        fs::write(
            &index_path,
            r#"# Design Document Index

## All Documents by Number

| Number | Title | State | Updated |
|--------|-------|-------|----------|
| 0001 | Test Doc | Draft | 2024-01-01 |

## Documents by State

### Draft

- [0001 - Test Doc](01-draft/0001-test.md)
"#,
        )
        .unwrap();

        let result = update_index(&index);
        assert!(result.is_ok());
    }

    #[test]
    fn test_add_to_table_at_beginning() {
        let content = r#"# Index

| Number | Title | State | Updated |
|--------|-------|-------|----------|
| 0003 | Third | Draft | 2024-01-03 |
| 0002 | Second | Draft | 2024-01-02 |
"#;

        let result = add_to_table(content, "0001", "First", "Draft", "2024-01-01").unwrap();
        assert!(result.contains("| 0001 | First | Draft | 2024-01-01 |"));
        // The function inserts before the last row when no match is found
        // So with 0003, 0002, adding 0001 results in: 0003, 0001, 0002
        let pos_1 = result.find("0001").unwrap();
        let pos_2 = result.find("0002").unwrap();
        let pos_3 = result.find("0003").unwrap();
        assert!(pos_3 < pos_1, "0003 should come before 0001");
        assert!(pos_1 < pos_2, "0001 should come before 0002");
    }

    #[test]
    fn test_add_to_table_in_middle() {
        let content = r#"# Index

| Number | Title | State | Updated |
|--------|-------|-------|----------|
| 0003 | Third | Draft | 2024-01-03 |
| 0001 | First | Draft | 2024-01-01 |
"#;

        let result = add_to_table(content, "0002", "Second", "Draft", "2024-01-02").unwrap();
        assert!(result.contains("| 0002 | Second | Draft | 2024-01-02 |"));
        // In descending order: 0003 > 0002 > 0001
        let pos_1 = result.find("0001").unwrap();
        let pos_2 = result.find("0002").unwrap();
        let pos_3 = result.find("0003").unwrap();
        assert!(pos_3 < pos_2 && pos_2 < pos_1);
    }

    #[test]
    fn test_add_to_table_at_end() {
        let content = r#"# Index

| Number | Title | State | Updated |
|--------|-------|-------|----------|
| 0001 | First | Draft | 2024-01-01 |
| 0002 | Second | Draft | 2024-01-02 |
"#;

        let result = add_to_table(content, "0003", "Third", "Draft", "2024-01-03").unwrap();
        assert!(result.contains("| 0003 | Third | Draft | 2024-01-03 |"));
    }

    #[test]
    fn test_update_table_field_title() {
        let content = r#"| 0001 | Old Title | Draft | 2024-01-01 |
| 0002 | Second | Draft | 2024-01-02 |"#;

        let result = update_table_field(content, "0001", "title", "New Title").unwrap();
        assert!(result.contains("| 0001 | New Title | Draft | 2024-01-01 |"));
        assert!(!result.contains("Old Title"));
    }

    #[test]
    fn test_update_table_field_state() {
        let content = r#"| 0001 | Title | Draft | 2024-01-01 |
| 0002 | Second | Draft | 2024-01-02 |"#;

        let result = update_table_field(content, "0001", "state", "Final").unwrap();
        assert!(result.contains("| 0001 | Title | Final | 2024-01-01 |"));
    }

    #[test]
    fn test_update_table_field_updated() {
        let content = r#"| 0001 | Title | Draft | 2024-01-01 |
| 0002 | Second | Draft | 2024-01-02 |"#;

        let result = update_table_field(content, "0001", "updated", "2024-12-26").unwrap();
        assert!(result.contains("| 0001 | Title | Draft | 2024-12-26 |"));
    }

    #[test]
    fn test_remove_from_table() {
        let content = r#"| Number | Title | State | Updated |
|--------|-------|-------|----------|
| 0001 | First | Draft | 2024-01-01 |
| 0002 | Second | Draft | 2024-01-02 |
| 0003 | Third | Draft | 2024-01-03 |"#;

        let result = remove_from_table(content, "0002").unwrap();
        assert!(!result.contains("| 0002 |"));
        assert!(result.contains("| 0001 |"));
        assert!(result.contains("| 0003 |"));
    }

    #[test]
    fn test_add_to_section_new_entry() {
        let content = r#"### Draft

- [0003 - Third](draft/0003-third.md)
- [0001 - First](draft/0001-first.md)
"#;

        let result =
            add_to_section(content, "Draft", "0002", "Second", "draft/0002-second.md").unwrap();
        assert!(result.contains("- [0002 - Second](draft/0002-second.md)"));
        // In descending order: 0003 > 0002 > 0001
        let pos_1 = result.find("0001").unwrap();
        let pos_2 = result.find("0002").unwrap();
        let pos_3 = result.find("0003").unwrap();
        assert!(pos_3 < pos_2 && pos_2 < pos_1);
    }

    #[test]
    fn test_add_to_section_creates_new_section() {
        let content = r#"# Index

## Documents by State

### Draft

- [0001 - First](draft/0001-first.md)
"#;

        let result =
            add_to_section(content, "Final", "0002", "Second", "final/0002-second.md").unwrap();
        assert!(result.contains("### Final"));
        assert!(result.contains("- [0002 - Second](final/0002-second.md)"));
    }

    #[test]
    fn test_add_to_section_empty_section() {
        let content = r#"### Draft

"#;

        let result =
            add_to_section(content, "Draft", "0001", "First", "draft/0001-first.md").unwrap();
        assert!(result.contains("- [0001 - First](draft/0001-first.md)"));
    }

    #[test]
    fn test_remove_from_section() {
        let content = r#"### Draft

- [0001 - First](draft/0001-first.md)
- [0002 - Second](draft/0002-second.md)
- [0003 - Third](draft/0003-third.md)
"#;

        let result = remove_from_section(content, "Draft", "draft/0002-second.md");
        assert!(result.is_ok());
        let updated = result.unwrap();
        assert!(!updated.contains("draft/0002-second.md"), "Entry should be removed");
        assert!(updated.contains("draft/0001-first.md"), "Other entries should remain");
        assert!(updated.contains("draft/0003-third.md"), "Other entries should remain");
    }

    #[test]
    fn test_remove_from_section_with_full_index_structure() {
        // Reproduce the exact bug scenario from the real index
        let content = r#"# Design Document Index

This index is automatically generated. Do not edit manually.

## All Documents by Number

| Number | Title | State | Updated |
|--------|-------|-------|----------|
| 0003 | Oxur Design Documentation CLI - Build Plan | Accepted | 2025-12-27 |
| 0001 | Oxur: A Letter of Intent | Active | 2025-12-26 |

## Documents by State

### Draft

- [0002 - This Should Be 0002](01-draft/0002-this-should-be-0002.md)
- [0004 - Test Doc 4](01-draft/0004-test-doc-4.md)
- [0004 - Should Be 4](01-draft/0004-should-be-4.md)

### Accepted

- [0003 - Oxur Design Documentation CLI - Build Plan](04-accepted/0003-oxur-design-documentation-cli-build-plan.md)

### Active

- [0001 - Oxur: A Letter of Intent](05-active/0001-oxur-letter-of-intent.md)
"#;

        // Remove the first phantom entry
        let result1 = remove_from_section(content, "Draft", "01-draft/0002-this-should-be-0002.md");
        assert!(result1.is_ok());
        let updated1 = result1.unwrap();
        assert!(
            !updated1.contains("01-draft/0002-this-should-be-0002.md"),
            "First entry should be removed"
        );
        assert!(updated1.contains("01-draft/0004-test-doc-4.md"), "Other entries should remain");
        assert!(updated1.contains("01-draft/0004-should-be-4.md"), "Other entries should remain");

        // Remove the second phantom entry
        let result2 = remove_from_section(&updated1, "Draft", "01-draft/0004-test-doc-4.md");
        assert!(result2.is_ok());
        let updated2 = result2.unwrap();
        assert!(
            !updated2.contains("01-draft/0002-this-should-be-0002.md"),
            "First entry should still be gone"
        );
        assert!(
            !updated2.contains("01-draft/0004-test-doc-4.md"),
            "Second entry should be removed"
        );
        assert!(updated2.contains("01-draft/0004-should-be-4.md"), "Other entries should remain");

        // Remove the third phantom entry - section should be removed entirely
        let result3 = remove_from_section(&updated2, "Draft", "01-draft/0004-should-be-4.md");
        assert!(result3.is_ok());
        let updated3 = result3.unwrap();
        assert!(
            !updated3.contains("01-draft/0002-this-should-be-0002.md"),
            "First entry should still be gone"
        );
        assert!(
            !updated3.contains("01-draft/0004-test-doc-4.md"),
            "Second entry should still be gone"
        );
        assert!(
            !updated3.contains("01-draft/0004-should-be-4.md"),
            "Third entry should be removed"
        );
        assert!(!updated3.contains("### Draft"), "Empty Draft section should be removed");
    }

    #[test]
    fn test_remove_from_section_removes_empty_section() {
        let content = r#"## Documents by State

### Draft

- [0001 - Only](draft/0001-only.md)

### Final
"#;

        let result = remove_from_section(content, "Draft", "draft/0001-only.md");
        assert!(result.is_ok());
    }

    #[test]
    fn test_remove_from_section_keeps_other_items() {
        let content = r#"### Draft

- [0001 - First](draft/0001-first.md)
- [0002 - Second](draft/0002-second.md)
"#;

        let result = remove_from_section(content, "Draft", "draft/0001-first.md");
        assert!(result.is_ok());
    }

    #[test]
    fn test_apply_change_table_add() {
        let content = r#"| Number | Title | State | Updated |
|--------|-------|-------|----------|
| 0002 | Second | Draft | 2024-01-02 |"#;

        let change = IndexChange::TableAdd {
            number: "0001".to_string(),
            title: "First".to_string(),
            state: "Draft".to_string(),
            updated: "2024-01-01".to_string(),
        };

        let doc_map = HashMap::new();
        let result = apply_change(content, &change, &doc_map, Path::new("/tmp")).unwrap();
        assert!(result.contains("| 0001 | First | Draft | 2024-01-01 |"));
    }

    #[test]
    fn test_apply_change_table_update() {
        let content = "| 0001 | Old | Draft | 2024-01-01 |";

        let change = IndexChange::TableUpdate {
            number: "0001".to_string(),
            field: "title".to_string(),
            old: "Old".to_string(),
            new: "New".to_string(),
        };

        let doc_map = HashMap::new();
        let result = apply_change(content, &change, &doc_map, Path::new("/tmp")).unwrap();
        assert!(result.contains("| 0001 | New | Draft | 2024-01-01 |"));
    }

    #[test]
    fn test_apply_change_table_remove() {
        let content = r#"| 0001 | First | Draft | 2024-01-01 |
| 0002 | Second | Draft | 2024-01-02 |"#;

        let change = IndexChange::TableRemove { number: "0001".to_string() };

        let doc_map = HashMap::new();
        let result = apply_change(content, &change, &doc_map, Path::new("/tmp")).unwrap();
        assert!(!result.contains("| 0001 |"));
        assert!(result.contains("| 0002 |"));
    }

    #[test]
    fn test_apply_change_section_add() {
        let content = "### Draft\n\n- [0001 - First](draft/0001.md)\n";

        let change = IndexChange::SectionAdd {
            state: "Draft".to_string(),
            number: "0002".to_string(),
            title: "Second".to_string(),
            path: "draft/0002.md".to_string(),
        };

        let doc_map = HashMap::new();
        let result = apply_change(content, &change, &doc_map, Path::new("/tmp")).unwrap();
        assert!(result.contains("- [0002 - Second](draft/0002.md)"));
    }

    #[test]
    fn test_apply_change_section_remove() {
        let content = r#"### Draft

- [0001 - First](draft/0001.md)
- [0002 - Second](draft/0002.md)"#;

        let change = IndexChange::SectionRemove {
            state: "Draft".to_string(),
            path: "draft/0001.md".to_string(),
        };

        let doc_map = HashMap::new();
        let result = apply_change(content, &change, &doc_map, Path::new("/tmp"));
        assert!(result.is_ok());
    }

    #[test]
    fn test_cleanup_formatting() {
        let content = "Line 1\n\n\n\nLine 2\n\n\nLine 3";
        let result = cleanup_formatting(content);
        // Should reduce multiple blank lines to max 2
        assert!(!result.contains("\n\n\n\n"));
    }
}