govctl 0.8.4

Project governance CLI for RFC, ADR, and Work Item management
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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
//! SSOT to Markdown rendering.
//!
//! Implements [[ADR-0003]] signatures and [[ADR-0011]] inline reference expansion.
//!
//! Rendered markdown files are read-only projections. Each includes:
//! - A "GENERATED" comment warning not to edit
//! - A SHA-256 signature for tampering detection
//! - Inline `[[artifact-id]]` references expanded to markdown links

use crate::config::Config;
use crate::model::{AdrEntry, ClauseKind, ClauseStatus, RfcIndex, WorkItemEntry};
use crate::signature::{
    compute_adr_signature, compute_rfc_signature, compute_work_item_signature,
    format_signature_header,
};
use crate::ui;
use regex::Regex;
use std::fmt::Write as FmtWrite;
use std::io::Write;

/// Generate a markdown link for an artifact reference.
///
/// Supports:
/// - RFC refs: `RFC-0000` → `[RFC-0000](../rfc/RFC-0000.md)`
/// - Clause refs: `RFC-0000:C-NAME` → `[RFC-0000:C-NAME](../rfc/RFC-0000.md#rfc-0000c-name)`
/// - ADR refs: `ADR-0001` → `[ADR-0001](../adr/ADR-0001.md)`
/// - Work Item refs: `WI-2026-01-17-001` → `[WI-2026-01-17-001](../work/WI-2026-01-17-001.md)`
fn ref_link(ref_id: &str) -> String {
    ref_link_with_base(ref_id, "..")
}

/// Generate a markdown link for an artifact reference from the repository root.
///
/// Used for files like CHANGELOG.md that live at the root level.
/// The `docs_output` path comes from config (e.g., "docs").
pub fn ref_link_from_root(ref_id: &str, docs_output: &str) -> String {
    ref_link_with_base(ref_id, docs_output)
}

/// Generate a markdown link with a configurable base path.
///
/// - `base`: Path prefix before `/rfc/`, `/adr/`, `/work/` (e.g., ".." or "docs")
fn ref_link_with_base(ref_id: &str, base: &str) -> String {
    if ref_id.starts_with("RFC-") {
        if ref_id.contains(':') {
            // Clause reference: RFC-0000:C-NAME
            let rfc_id = ref_id.split(':').next().unwrap_or(ref_id);
            // Anchor: lowercase, no special chars (GitHub-style slug)
            let anchor = ref_id.to_lowercase().replace(':', "");
            format!("[{}]({}/rfc/{}.md#{})", ref_id, base, rfc_id, anchor)
        } else {
            // RFC reference
            format!("[{}]({}/rfc/{}.md)", ref_id, base, ref_id)
        }
    } else if ref_id.starts_with("ADR-") {
        format!("[{}]({}/adr/{}.md)", ref_id, base, ref_id)
    } else if ref_id.starts_with("WI-") {
        format!("[{}]({}/work/{}.md)", ref_id, base, ref_id)
    } else {
        // Unknown type, return as-is
        ref_id.to_string()
    }
}

/// Render a list of refs as markdown links.
fn render_refs(refs: &[String]) -> String {
    refs.iter()
        .map(|r| ref_link(r))
        .collect::<Vec<_>>()
        .join(", ")
}

/// Expand inline `[[artifact-id]]` references to markdown links.
///
/// Uses the pattern from source_scan config (per ADR-0011).
/// The pattern must have a capture group for the artifact ID.
pub fn expand_inline_refs(text: &str, pattern: &str) -> String {
    expand_inline_refs_with_linker(text, pattern, ref_link)
}

/// Expand inline `[[artifact-id]]` references to markdown links from repository root.
///
/// Used for files like CHANGELOG.md that live at the root level.
/// The `docs_output` path comes from config (e.g., "docs").
pub fn expand_inline_refs_from_root(text: &str, pattern: &str, docs_output: &str) -> String {
    expand_inline_refs_with_linker(text, pattern, |ref_id| {
        ref_link_from_root(ref_id, docs_output)
    })
}

/// Expand inline references using a custom link generator function.
fn expand_inline_refs_with_linker<F>(text: &str, pattern: &str, linker: F) -> String
where
    F: Fn(&str) -> String,
{
    let Ok(re) = Regex::new(pattern) else {
        // Invalid pattern, return text unchanged
        return text.to_string();
    };

    re.replace_all(text, |caps: &regex::Captures| {
        // Capture group 1 contains the artifact ID
        if let Some(artifact_id) = caps.get(1) {
            linker(artifact_id.as_str())
        } else {
            // No capture group, return match unchanged
            caps.get(0).map_or("", |m| m.as_str()).to_string()
        }
    })
    .to_string()
}

/// Indent continuation lines in multi-line text to preserve markdown list structure.
/// The first line is returned as-is; subsequent lines are prefixed with the indent.
fn indent_continuation(text: &str) -> String {
    let mut lines = text.lines();
    let Some(first) = lines.next() else {
        return String::new();
    };
    let mut result = first.to_string();
    for line in lines {
        result.push('\n');
        result.push_str("  ");
        result.push_str(line);
    }
    result
}

/// Render an RFC to Markdown
///
/// # Errors
/// Returns an error if signature computation fails.
pub fn render_rfc(rfc: &RfcIndex) -> anyhow::Result<String> {
    let mut out = String::new();

    // Compute signature from source content (per ADR-0003)
    let signature = compute_rfc_signature(rfc)?;

    // Signature header (tampering detection per ADR-0003)
    out.push_str(&format_signature_header(&rfc.rfc.rfc_id, &signature));
    let _ = writeln!(out);

    // Title
    let _ = writeln!(out, "# {}: {}", rfc.rfc.rfc_id, rfc.rfc.title);
    let _ = writeln!(out);

    // Version info
    let _ = writeln!(
        out,
        "> **Version:** {} | **Status:** {} | **Phase:** {}",
        rfc.rfc.version,
        rfc.rfc.status.as_ref(),
        rfc.rfc.phase.as_ref()
    );
    let _ = writeln!(out);

    // References (expanded to markdown links)
    if !rfc.rfc.refs.is_empty() {
        let _ = writeln!(out, "**References:** {}", render_refs(&rfc.rfc.refs));
        let _ = writeln!(out);
    }

    // Render sections with clauses
    for (i, section) in rfc.rfc.sections.iter().enumerate() {
        let _ = writeln!(out, "---");
        let _ = writeln!(out);
        let _ = writeln!(out, "## {}. {}", i + 1, section.title);
        let _ = writeln!(out);

        // Find and render clauses for this section
        for clause_path in &section.clauses {
            if let Some(clause) = rfc.clauses.iter().find(|c| {
                c.path
                    .file_name()
                    .and_then(|n| n.to_str())
                    .is_some_and(|n| clause_path.ends_with(n))
            }) {
                render_clause(&mut out, &rfc.rfc.rfc_id, clause);
            }
        }
    }

    // Changelog (Keep a Changelog format)
    if !rfc.rfc.changelog.is_empty() {
        let _ = writeln!(out, "---");
        let _ = writeln!(out);
        let _ = writeln!(out, "## Changelog");
        let _ = writeln!(out);

        for entry in &rfc.rfc.changelog {
            let _ = writeln!(out, "### v{} ({})", entry.version, entry.date);
            let _ = writeln!(out);

            if let Some(ref notes) = entry.notes {
                let _ = writeln!(out, "{notes}");
                let _ = writeln!(out);
            }

            render_changelog_section(&mut out, "Added", &entry.added);
            render_changelog_section(&mut out, "Changed", &entry.changed);
            render_changelog_section(&mut out, "Deprecated", &entry.deprecated);
            render_changelog_section(&mut out, "Removed", &entry.removed);
            render_changelog_section(&mut out, "Fixed", &entry.fixed);
            render_changelog_section(&mut out, "Security", &entry.security);
        }
    }

    Ok(out)
}

/// Render a changelog section (Keep a Changelog format)
fn render_changelog_section(out: &mut String, heading: &str, items: &[String]) {
    if items.is_empty() {
        return;
    }
    let _ = writeln!(out, "#### {heading}");
    let _ = writeln!(out);
    for item in items {
        let _ = writeln!(out, "- {item}");
    }
    let _ = writeln!(out);
}

/// Generate anchor ID for a clause (matches ref_link anchor format).
fn clause_anchor(rfc_id: &str, clause_id: &str) -> String {
    format!("{}:{}", rfc_id, clause_id)
        .to_lowercase()
        .replace(':', "")
}

/// Render a single clause
pub fn render_clause(out: &mut String, rfc_id: &str, clause: &crate::model::ClauseEntry) {
    let spec = &clause.spec;

    // Clause header with ID anchor
    let kind_marker = match spec.kind {
        ClauseKind::Normative => "(Normative)",
        ClauseKind::Informative => "(Informative)",
    };

    // Generate anchor for clause linking (matches ref_link anchor format)
    let anchor = clause_anchor(rfc_id, &spec.clause_id);

    // Format title, wrapped in <del> if deprecated/superseded
    // Using HTML <del> instead of markdown ~~ avoids escaping issues with titles
    let title_part = format!("[{}:{}] {}", rfc_id, spec.clause_id, spec.title);
    let formatted_title = match spec.status {
        ClauseStatus::Active => title_part,
        ClauseStatus::Deprecated | ClauseStatus::Superseded => format!("<del>{}</del>", title_part),
    };

    let _ = writeln!(
        out,
        "### {} {kind_marker} <a id=\"{anchor}\"></a>",
        formatted_title
    );
    let _ = writeln!(out);

    // Clause text
    let _ = writeln!(out, "{}", spec.text);
    let _ = writeln!(out);

    // Superseded by notice
    if let Some(ref by) = spec.superseded_by {
        let _ = writeln!(out, "> **Superseded by:** {by}");
        let _ = writeln!(out);
    }

    // Since version
    if let Some(ref since) = spec.since {
        let _ = writeln!(out, "*Since: v{since}*");
        let _ = writeln!(out);
    }
}

/// Write rendered markdown to file with common formatting.
///
/// Handles dry-run preview, directory creation, and consistent formatting.
/// `preview_lines` controls how many lines to show in dry-run mode.
fn write_rendered_md(
    config: &Config,
    output_path: &std::path::Path,
    content: &str,
    dry_run: bool,
    preview_lines: usize,
) -> anyhow::Result<()> {
    // Trim trailing whitespace, ensure single trailing newline
    let content = format!("{}\n", content.trim_end());
    let display_path = config.display_path(output_path);

    if dry_run {
        ui::dry_run_preview(&display_path);
        for line in content.lines().take(preview_lines) {
            ui::preview_line(line);
        }
        ui::preview_truncated();
    } else {
        // Ensure parent directory exists
        if let Some(parent) = output_path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let mut file = std::fs::File::create(output_path)?;
        file.write_all(content.as_bytes())?;
        ui::rendered(&display_path);
    }

    Ok(())
}

/// Write rendered RFC to file
pub fn write_rfc(config: &Config, rfc: &RfcIndex, dry_run: bool) -> anyhow::Result<()> {
    let output_path = config.rfc_output().join(format!("{}.md", rfc.rfc.rfc_id));

    // Render and expand inline references (per ADR-0011)
    let raw = render_rfc(rfc)?;
    let expanded = expand_inline_refs(&raw, &config.source_scan.pattern);

    write_rendered_md(config, &output_path, &expanded, dry_run, 20)
}

// =============================================================================
// ADR Rendering
// =============================================================================

/// Render an ADR to Markdown
///
/// # Errors
/// Returns an error if signature computation fails.
pub fn render_adr(adr: &AdrEntry) -> anyhow::Result<String> {
    let meta = adr.meta();
    let content = &adr.spec.content;
    let mut out = String::new();

    // Compute signature (per ADR-0003)
    let signature = compute_adr_signature(adr)?;

    // Signature header
    out.push_str(&format_signature_header(&meta.id, &signature));
    let _ = writeln!(out);

    // Title
    let _ = writeln!(out, "# {}: {}", meta.id, meta.title);
    let _ = writeln!(out);

    // Status and date
    let _ = writeln!(
        out,
        "> **Status:** {} | **Date:** {}",
        meta.status.as_ref(),
        meta.date
    );
    if let Some(ref by) = meta.superseded_by {
        let _ = writeln!(out, "> **Superseded by:** {by}");
    }
    let _ = writeln!(out);

    // References (expanded to markdown links)
    if !meta.refs.is_empty() {
        let _ = writeln!(out, "**References:** {}", render_refs(&meta.refs));
        let _ = writeln!(out);
    }

    // Context
    let _ = writeln!(out, "## Context");
    let _ = writeln!(out);
    let _ = writeln!(out, "{}", content.context);
    let _ = writeln!(out);

    // Decision
    let _ = writeln!(out, "## Decision");
    let _ = writeln!(out);
    let _ = writeln!(out, "{}", content.decision);
    let _ = writeln!(out);

    // Consequences
    let _ = writeln!(out, "## Consequences");
    let _ = writeln!(out);
    let _ = writeln!(out, "{}", content.consequences);
    let _ = writeln!(out);

    // Alternatives Considered (extended per ADR-0027)
    if !content.alternatives.is_empty() {
        use crate::model::AlternativeStatus;
        let _ = writeln!(out, "## Alternatives Considered");
        let _ = writeln!(out);
        for alt in &content.alternatives {
            // Render as subheading with status
            let status_suffix = match alt.status {
                AlternativeStatus::Considered => "",
                AlternativeStatus::Accepted => " (accepted)",
                AlternativeStatus::Rejected => " (rejected)",
            };
            let _ = writeln!(out, "### {}{}", alt.text, status_suffix);
            let _ = writeln!(out);

            // Pros
            if !alt.pros.is_empty() {
                let _ = writeln!(out, "- **Pros:** {}", alt.pros.join(", "));
            }

            // Cons
            if !alt.cons.is_empty() {
                let _ = writeln!(out, "- **Cons:** {}", alt.cons.join(", "));
            }

            // Rejection reason
            if let Some(ref reason) = alt.rejection_reason {
                let _ = writeln!(out, "- **Rejected because:** {}", reason);
            }

            let _ = writeln!(out);
        }
    }

    Ok(out)
}

/// Write rendered ADR to file
pub fn write_adr_md(config: &Config, adr: &AdrEntry, dry_run: bool) -> anyhow::Result<()> {
    let meta = adr.meta();
    let output_path = config.adr_output().join(format!("{}.md", meta.id));

    // Render and expand inline references (per ADR-0011)
    let raw = render_adr(adr)?;
    let expanded = expand_inline_refs(&raw, &config.source_scan.pattern);

    write_rendered_md(config, &output_path, &expanded, dry_run, 15)
}

// =============================================================================
// Work Item Rendering
// =============================================================================

/// Render a Work Item to Markdown
///
/// # Errors
/// Returns an error if signature computation fails.
pub fn render_work_item(item: &WorkItemEntry) -> anyhow::Result<String> {
    let meta = item.meta();
    let content = &item.spec.content;
    let mut out = String::new();

    // Compute signature (per ADR-0003)
    let signature = compute_work_item_signature(item)?;

    // Signature header
    out.push_str(&format_signature_header(&meta.id, &signature));
    let _ = writeln!(out);

    // Title
    let _ = writeln!(out, "# {}", meta.title);
    let _ = writeln!(out);

    // Status
    let mut status_line = format!(
        "> **ID:** {} | **Status:** {}",
        meta.id,
        meta.status.as_ref()
    );
    if let Some(ref start) = meta.started {
        status_line.push_str(&format!(" | **Started:** {start}"));
    }
    if let Some(ref done) = meta.completed {
        status_line.push_str(&format!(" | **Completed:** {done}"));
    }
    let _ = writeln!(out, "{status_line}");
    let _ = writeln!(out);

    // References (expanded to markdown links)
    if !meta.refs.is_empty() {
        let _ = writeln!(out, "**References:** {}", render_refs(&meta.refs));
        let _ = writeln!(out);
    }

    // Description
    let _ = writeln!(out, "## Description");
    let _ = writeln!(out);
    let _ = writeln!(out, "{}", content.description);
    let _ = writeln!(out);

    // Journal (per ADR-0026)
    if !content.journal.is_empty() {
        let _ = writeln!(out, "## Journal");
        let _ = writeln!(out);
        for entry in &content.journal {
            // Render heading with date and optional scope
            if let Some(ref scope) = entry.scope {
                let _ = writeln!(out, "### {} · {}", entry.date, scope);
            } else {
                let _ = writeln!(out, "### {}", entry.date);
            }
            let _ = writeln!(out);
            // Render content (multi-line markdown)
            let _ = writeln!(out, "{}", entry.content);
            let _ = writeln!(out);
        }
    }

    // Acceptance Criteria
    if !content.acceptance_criteria.is_empty() {
        use crate::model::ChecklistStatus;
        let _ = writeln!(out, "## Acceptance Criteria");
        let _ = writeln!(out);
        for ac_item in &content.acceptance_criteria {
            // Indent continuation lines to keep them within the list item
            let indented_text = indent_continuation(&ac_item.text);
            let line = match ac_item.status {
                ChecklistStatus::Pending => format!("- [ ] {}", indented_text),
                ChecklistStatus::Done => format!("- [x] {}", indented_text),
                ChecklistStatus::Cancelled => format!("- ~~{}~~", indented_text),
            };
            let _ = writeln!(out, "{line}");
        }
        let _ = writeln!(out);
    }

    // Notes
    if !content.notes.is_empty() {
        let _ = writeln!(out, "## Notes");
        let _ = writeln!(out);
        for note in &content.notes {
            let _ = writeln!(out, "- {}", note);
        }
        let _ = writeln!(out);
    }

    Ok(out)
}

/// Write rendered Work Item to file
pub fn write_work_item_md(
    config: &Config,
    item: &WorkItemEntry,
    dry_run: bool,
) -> anyhow::Result<()> {
    let meta = item.meta();
    let output_path = config.work_output().join(format!("{}.md", meta.id));

    // Render and expand inline references (per ADR-0011)
    let raw = render_work_item(item)?;
    let expanded = expand_inline_refs(&raw, &config.source_scan.pattern);

    write_rendered_md(config, &output_path, &expanded, dry_run, 15)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{
        AdrContent, AdrMeta, AdrSpec, AdrStatus, Alternative, AlternativeStatus, JournalEntry,
        WorkItemContent, WorkItemMeta, WorkItemSpec, WorkItemStatus,
    };

    const DEFAULT_PATTERN: &str = r"\[\[(RFC-\d{4}(?::C-[A-Z][A-Z0-9-]*)?|ADR-\d{4}|WI-\d{4}-\d{2}-\d{2}-(?:[a-f0-9]{4}(?:-\d{3})?|\d{3}))\]\]";

    #[test]
    fn test_expand_inline_refs_rfc() {
        let text = "See [[RFC-0000]] for details.";
        let result = expand_inline_refs(text, DEFAULT_PATTERN);
        assert_eq!(result, "See [RFC-0000](../rfc/RFC-0000.md) for details.");
    }

    #[test]
    fn test_expand_inline_refs_clause() {
        let text = "Per [[RFC-0000:C-WORK-DEF]], work items must...";
        let result = expand_inline_refs(text, DEFAULT_PATTERN);
        assert_eq!(
            result,
            "Per [RFC-0000:C-WORK-DEF](../rfc/RFC-0000.md#rfc-0000c-work-def), work items must..."
        );
    }

    #[test]
    fn test_expand_inline_refs_adr() {
        let text = "This follows [[ADR-0005]] guidelines.";
        let result = expand_inline_refs(text, DEFAULT_PATTERN);
        assert_eq!(
            result,
            "This follows [ADR-0005](../adr/ADR-0005.md) guidelines."
        );
    }

    #[test]
    fn test_expand_inline_refs_multiple() {
        let text = "See [[RFC-0000]] and [[ADR-0001]] for context.";
        let result = expand_inline_refs(text, DEFAULT_PATTERN);
        assert_eq!(
            result,
            "See [RFC-0000](../rfc/RFC-0000.md) and [ADR-0001](../adr/ADR-0001.md) for context."
        );
    }

    #[test]
    fn test_expand_inline_refs_no_match() {
        let text = "No references here.";
        let result = expand_inline_refs(text, DEFAULT_PATTERN);
        assert_eq!(result, "No references here.");
    }

    #[test]
    fn test_expand_inline_refs_invalid_pattern() {
        let text = "[[RFC-0000]] test";
        let result = expand_inline_refs(text, "[invalid(regex");
        // Invalid pattern returns text unchanged
        assert_eq!(result, "[[RFC-0000]] test");
    }

    #[test]
    fn test_ref_link_from_root_rfc() {
        let result = ref_link_from_root("RFC-0000", "docs");
        assert_eq!(result, "[RFC-0000](docs/rfc/RFC-0000.md)");
    }

    #[test]
    fn test_ref_link_from_root_clause() {
        let result = ref_link_from_root("RFC-0000:C-WORK-DEF", "docs");
        assert_eq!(
            result,
            "[RFC-0000:C-WORK-DEF](docs/rfc/RFC-0000.md#rfc-0000c-work-def)"
        );
    }

    #[test]
    fn test_ref_link_from_root_adr() {
        let result = ref_link_from_root("ADR-0005", "docs");
        assert_eq!(result, "[ADR-0005](docs/adr/ADR-0005.md)");
    }

    #[test]
    fn test_ref_link_from_root_custom_path() {
        let result = ref_link_from_root("RFC-0001", "documentation");
        assert_eq!(result, "[RFC-0001](documentation/rfc/RFC-0001.md)");
    }

    #[test]
    fn test_expand_inline_refs_from_root() {
        let text = "Per [[RFC-0002:C-RESOURCE-MODEL]], resources use verb pattern.";
        let result = expand_inline_refs_from_root(text, DEFAULT_PATTERN, "docs");
        assert_eq!(
            result,
            "Per [RFC-0002:C-RESOURCE-MODEL](docs/rfc/RFC-0002.md#rfc-0002c-resource-model), resources use verb pattern."
        );
    }

    #[test]
    fn test_expand_inline_refs_from_root_multiple() {
        let text = "See [[RFC-0000]] and [[ADR-0018]] for details.";
        let result = expand_inline_refs_from_root(text, DEFAULT_PATTERN, "docs");
        assert_eq!(
            result,
            "See [RFC-0000](docs/rfc/RFC-0000.md) and [ADR-0018](docs/adr/ADR-0018.md) for details."
        );
    }

    // Work item inline reference tests (per ADR-0020 ID formats)
    // Constructs strings at runtime to avoid source_scan matching test fixtures

    fn wi_ref(id: &str) -> String {
        format!("[[{}]]", id)
    }

    #[test]
    fn test_expand_inline_refs_work_item_sequential() {
        let id = "WI-9999-01-26-001";
        let text = format!("See {} for task details.", wi_ref(id));
        let result = expand_inline_refs(&text, DEFAULT_PATTERN);
        assert_eq!(
            result,
            format!("See [{}](../work/{}.md) for task details.", id, id)
        );
    }

    #[test]
    fn test_expand_inline_refs_work_item_author_hash() {
        let id = "WI-9999-01-26-a7f3-001";
        let text = format!("See {} for task details.", wi_ref(id));
        let result = expand_inline_refs(&text, DEFAULT_PATTERN);
        assert_eq!(
            result,
            format!("See [{}](../work/{}.md) for task details.", id, id)
        );
    }

    #[test]
    fn test_expand_inline_refs_work_item_random() {
        let id = "WI-9999-01-26-b2c9";
        let text = format!("See {} for task details.", wi_ref(id));
        let result = expand_inline_refs(&text, DEFAULT_PATTERN);
        assert_eq!(
            result,
            format!("See [{}](../work/{}.md) for task details.", id, id)
        );
    }

    #[test]
    fn test_expand_inline_refs_work_item_mixed() {
        let wi_id = "WI-9999-01-26-001";
        let text = format!("Per [[RFC-0000]], see {} and [[ADR-0020]].", wi_ref(wi_id));
        let result = expand_inline_refs(&text, DEFAULT_PATTERN);
        assert_eq!(
            result,
            format!(
                "Per [RFC-0000](../rfc/RFC-0000.md), see [{}](../work/{}.md) and [ADR-0020](../adr/ADR-0020.md).",
                wi_id, wi_id
            )
        );
    }

    // Tests for render_adr with new Alternative fields per [[ADR-0027]]

    #[test]
    fn test_render_adr_alternatives_with_pros_cons() -> Result<(), Box<dyn std::error::Error>> {
        let adr = AdrEntry {
            spec: AdrSpec {
                govctl: AdrMeta {
                    schema: 1,
                    id: "ADR-9999".to_string(),
                    title: "Test ADR".to_string(),
                    status: AdrStatus::Accepted,
                    date: "2026-02-22".to_string(),
                    superseded_by: None,
                    refs: vec![],
                    tags: vec![],
                },
                content: AdrContent {
                    context: "Test context".to_string(),
                    decision: "Test decision".to_string(),
                    consequences: "Test consequences".to_string(),
                    alternatives: vec![Alternative {
                        text: "Option A".to_string(),
                        status: AlternativeStatus::Considered,
                        pros: vec!["Fast".to_string(), "Cheap".to_string()],
                        cons: vec!["Less reliable".to_string()],
                        rejection_reason: None,
                    }],
                },
            },
            path: std::path::PathBuf::new(),
        };

        let result = render_adr(&adr)?;
        assert!(result.contains("### Option A"));
        assert!(result.contains("- **Pros:** Fast, Cheap"));
        assert!(result.contains("- **Cons:** Less reliable"));
        Ok(())
    }

    #[test]
    fn test_render_adr_alternatives_rejected_with_reason() -> Result<(), Box<dyn std::error::Error>>
    {
        let adr = AdrEntry {
            spec: AdrSpec {
                govctl: AdrMeta {
                    schema: 1,
                    id: "ADR-9998".to_string(),
                    title: "Test ADR Rejected".to_string(),
                    status: AdrStatus::Accepted,
                    date: "2026-02-22".to_string(),
                    superseded_by: None,
                    refs: vec![],
                    tags: vec![],
                },
                content: AdrContent {
                    context: "Test context".to_string(),
                    decision: "Test decision".to_string(),
                    consequences: "Test consequences".to_string(),
                    alternatives: vec![Alternative {
                        text: "Option B".to_string(),
                        status: AlternativeStatus::Rejected,
                        pros: vec![],
                        cons: vec!["Too expensive".to_string()],
                        rejection_reason: Some("Budget constraints".to_string()),
                    }],
                },
            },
            path: std::path::PathBuf::new(),
        };

        let result = render_adr(&adr)?;
        assert!(result.contains("### Option B (rejected)"));
        assert!(result.contains("- **Rejected because:** Budget constraints"));
        Ok(())
    }

    // Tests for render_work_item with journal field per [[ADR-0026]]

    #[test]
    fn test_render_work_item_journal() -> Result<(), Box<dyn std::error::Error>> {
        let item = WorkItemEntry {
            spec: WorkItemSpec {
                govctl: WorkItemMeta {
                    schema: 1,
                    id: "WI-2026-02-22-001".to_string(),
                    title: "Test Work Item".to_string(),
                    status: WorkItemStatus::Active,
                    created: Some("2026-02-22".to_string()),
                    started: Some("2026-02-22".to_string()),
                    completed: None,
                    refs: vec![],
                    tags: vec![],
                },
                content: WorkItemContent {
                    description: "Test description".to_string(),
                    journal: vec![JournalEntry {
                        date: "2026-02-22".to_string(),
                        scope: None,
                        content: "Started implementation".to_string(),
                    }],
                    acceptance_criteria: vec![],
                    notes: vec![],
                },
                verification: crate::model::WorkItemVerification::default(),
            },
            path: std::path::PathBuf::new(),
        };

        let result = render_work_item(&item)?;
        assert!(result.contains("## Journal"));
        assert!(result.contains("### 2026-02-22"));
        assert!(result.contains("Started implementation"));
        Ok(())
    }

    #[test]
    fn test_render_work_item_journal_with_scope() -> Result<(), Box<dyn std::error::Error>> {
        let item = WorkItemEntry {
            spec: WorkItemSpec {
                govctl: WorkItemMeta {
                    schema: 1,
                    id: "WI-2026-02-22-002".to_string(),
                    title: "Test Work Item with Scope".to_string(),
                    status: WorkItemStatus::Active,
                    created: Some("2026-02-22".to_string()),
                    started: Some("2026-02-22".to_string()),
                    completed: None,
                    refs: vec![],
                    tags: vec![],
                },
                content: WorkItemContent {
                    description: "Test description".to_string(),
                    journal: vec![
                        JournalEntry {
                            date: "2026-02-22".to_string(),
                            scope: Some("API".to_string()),
                            content: "Created endpoint".to_string(),
                        },
                        JournalEntry {
                            date: "2026-02-23".to_string(),
                            scope: Some("Testing".to_string()),
                            content: "Added unit tests".to_string(),
                        },
                    ],
                    acceptance_criteria: vec![],
                    notes: vec![],
                },
                verification: crate::model::WorkItemVerification::default(),
            },
            path: std::path::PathBuf::new(),
        };

        let result = render_work_item(&item)?;
        assert!(result.contains("### 2026-02-22 · API"));
        assert!(result.contains("Created endpoint"));
        assert!(result.contains("### 2026-02-23 · Testing"));
        assert!(result.contains("Added unit tests"));
        Ok(())
    }
}