Skip to main content

omni_dev/atlassian/
document.rs

1//! JFM document format: YAML frontmatter + markdown body.
2//!
3//! Parses and renders documents in the format:
4//! ```text
5//! ---
6//! type: jira
7//! key: PROJ-123
8//! summary: Issue title
9//! ---
10//!
11//! Markdown body content here.
12//! ```
13
14use std::collections::BTreeMap;
15use std::fmt::Write as _;
16
17use anyhow::{Context, Result};
18use serde::{Deserialize, Serialize};
19
20use crate::atlassian::adf::AdfDocument;
21use crate::atlassian::api::{ContentItem, ContentMetadata};
22use crate::atlassian::client::{JiraCustomField, JiraIssue};
23use crate::atlassian::convert::adf_to_markdown;
24use crate::atlassian::error::AtlassianError;
25
26/// A JFM document consisting of YAML frontmatter and a markdown body.
27#[derive(Debug, Clone)]
28pub struct JfmDocument {
29    /// Parsed frontmatter metadata fields.
30    pub frontmatter: JfmFrontmatter,
31
32    /// Raw markdown body (not parsed — passed through to/from ADF conversion).
33    pub body: String,
34}
35
36/// YAML frontmatter for a JFM document.
37///
38/// Dispatched by the `type` field to the appropriate backend-specific struct.
39#[derive(Debug, Clone, Serialize, Deserialize)]
40#[serde(tag = "type")]
41pub enum JfmFrontmatter {
42    /// JIRA issue frontmatter.
43    #[serde(rename = "jira")]
44    Jira(JiraFrontmatter),
45
46    /// Confluence page frontmatter.
47    #[serde(rename = "confluence")]
48    Confluence(ConfluenceFrontmatter),
49}
50
51/// JIRA-specific frontmatter fields.
52#[derive(Debug, Clone, Serialize, Deserialize)]
53pub struct JiraFrontmatter {
54    /// Atlassian instance base URL.
55    pub instance: String,
56
57    /// JIRA issue key (e.g., "PROJ-123"). Empty when creating a new issue.
58    #[serde(default)]
59    pub key: String,
60
61    /// Project key (e.g., "PROJ"). Used when creating issues without an existing key.
62    #[serde(default, skip_serializing_if = "Option::is_none")]
63    pub project: Option<String>,
64
65    /// Issue summary (title).
66    pub summary: String,
67
68    /// Issue status name.
69    #[serde(skip_serializing_if = "Option::is_none")]
70    pub status: Option<String>,
71
72    /// Issue type name (Bug, Story, Task, etc.).
73    #[serde(skip_serializing_if = "Option::is_none")]
74    pub issue_type: Option<String>,
75
76    /// Assignee display name.
77    #[serde(skip_serializing_if = "Option::is_none")]
78    pub assignee: Option<String>,
79
80    /// Priority name.
81    #[serde(skip_serializing_if = "Option::is_none")]
82    pub priority: Option<String>,
83
84    /// Labels applied to the issue.
85    #[serde(default, skip_serializing_if = "Vec::is_empty")]
86    pub labels: Vec<String>,
87
88    /// Scalar custom field values keyed by human-readable field name.
89    ///
90    /// Populated when the issue was fetched with `--fields` or
91    /// `--all-fields`. Rich-text (ADF) custom fields are rendered into the
92    /// document body as extra sections instead.
93    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
94    pub custom_fields: BTreeMap<String, serde_yaml::Value>,
95}
96
97/// Confluence-specific frontmatter fields.
98#[derive(Debug, Clone, Serialize, Deserialize)]
99pub struct ConfluenceFrontmatter {
100    /// Atlassian instance base URL.
101    pub instance: String,
102
103    /// Confluence page ID. Empty when creating a new page.
104    #[serde(default)]
105    pub page_id: String,
106
107    /// Page title.
108    pub title: String,
109
110    /// Space key (e.g., "ENG").
111    pub space_key: String,
112
113    /// Page status ("current" or "draft").
114    #[serde(skip_serializing_if = "Option::is_none")]
115    pub status: Option<String>,
116
117    /// Page version number.
118    #[serde(skip_serializing_if = "Option::is_none")]
119    pub version: Option<u32>,
120
121    /// Parent page ID.
122    #[serde(skip_serializing_if = "Option::is_none")]
123    pub parent_id: Option<String>,
124}
125
126impl JfmFrontmatter {
127    /// Returns the Atlassian instance URL.
128    pub fn instance(&self) -> &str {
129        match self {
130            Self::Jira(fm) => &fm.instance,
131            Self::Confluence(fm) => &fm.instance,
132        }
133    }
134
135    /// Returns the content identifier (JIRA key or Confluence page ID).
136    pub fn id(&self) -> &str {
137        match self {
138            Self::Jira(fm) => &fm.key,
139            Self::Confluence(fm) => &fm.page_id,
140        }
141    }
142
143    /// Returns the content title (JIRA summary or Confluence page title).
144    pub fn title(&self) -> &str {
145        match self {
146            Self::Jira(fm) => &fm.summary,
147            Self::Confluence(fm) => &fm.title,
148        }
149    }
150
151    /// Returns the document type name.
152    pub fn doc_type(&self) -> &str {
153        match self {
154            Self::Jira(_) => "jira",
155            Self::Confluence(_) => "confluence",
156        }
157    }
158
159    /// Returns the JIRA custom field scalar map, or `None` when the
160    /// frontmatter is not a JIRA variant.
161    pub fn jira_custom_fields(&self) -> Option<&BTreeMap<String, serde_yaml::Value>> {
162        match self {
163            Self::Jira(fm) => Some(&fm.custom_fields),
164            Self::Confluence(_) => None,
165        }
166    }
167}
168
169/// Validates that a string looks like a JIRA issue key (e.g., "PROJ-123").
170pub fn validate_issue_key(key: &str) -> Result<()> {
171    let re =
172        regex::Regex::new(r"^[A-Z][A-Z0-9]+-\d+$").context("Failed to compile issue key regex")?;
173    if !re.is_match(key) {
174        anyhow::bail!("Invalid JIRA issue key: '{key}'. Expected format: PROJ-123");
175    }
176    Ok(())
177}
178
179/// Converts a [`JiraIssue`] into a [`JfmDocument`] with YAML frontmatter.
180///
181/// Custom fields present on the issue are partitioned: rich-text (ADF)
182/// fields become additional JFM sections appended to the body (prefixed
183/// with an HTML-comment tag so the `write` round-trip can identify them),
184/// while scalar fields are serialized into the frontmatter's
185/// `custom_fields` map keyed by human-readable name.
186pub fn issue_to_jfm_document(issue: &JiraIssue, instance_url: &str) -> Result<JfmDocument> {
187    let mut body = if let Some(ref adf_value) = issue.description_adf {
188        let adf_doc: AdfDocument =
189            serde_json::from_value(adf_value.clone()).context("Failed to parse ADF description")?;
190        adf_to_markdown(&adf_doc)?
191    } else {
192        String::new()
193    };
194
195    let mut custom_scalars: BTreeMap<String, serde_yaml::Value> = BTreeMap::new();
196    for field in &issue.custom_fields {
197        render_custom_field(field, &mut body, &mut custom_scalars)?;
198    }
199
200    Ok(JfmDocument {
201        frontmatter: JfmFrontmatter::Jira(JiraFrontmatter {
202            instance: instance_url.to_string(),
203            key: issue.key.clone(),
204            project: None,
205            summary: issue.summary.clone(),
206            status: issue.status.clone(),
207            issue_type: issue.issue_type.clone(),
208            assignee: issue.assignee.clone(),
209            priority: issue.priority.clone(),
210            labels: issue.labels.clone(),
211            custom_fields: custom_scalars,
212        }),
213        body,
214    })
215}
216
217/// Renders a single custom field into either the body (rich text) or the
218/// frontmatter scalar map.
219fn render_custom_field(
220    field: &JiraCustomField,
221    body: &mut String,
222    scalars: &mut BTreeMap<String, serde_yaml::Value>,
223) -> Result<()> {
224    if is_adf_document(&field.value) {
225        let adf_doc: AdfDocument = serde_json::from_value(field.value.clone())
226            .with_context(|| format!("Failed to parse ADF value for {}", field.id))?;
227        let section_md = adf_to_markdown(&adf_doc)?;
228        append_custom_section(body, field, &section_md);
229    } else if let Some(scalar) = extract_custom_field_scalar(&field.value) {
230        scalars.insert(field.name.clone(), scalar);
231    }
232    // Otherwise the field is null or an unrecognized shape — omit it.
233    Ok(())
234}
235
236/// Appends a rich-text custom field to the body as a tagged section.
237fn append_custom_section(body: &mut String, field: &JiraCustomField, section_md: &str) {
238    if !body.is_empty() && !body.ends_with('\n') {
239        body.push('\n');
240    }
241    if !body.is_empty() {
242        body.push('\n');
243    }
244    let _ = write!(
245        body,
246        "---\n<!-- field: {} ({}) -->\n\n{}",
247        field.name, field.id, section_md
248    );
249    if !body.ends_with('\n') {
250        body.push('\n');
251    }
252}
253
254/// A single rich-text custom field section parsed out of a JFM body.
255///
256/// Emitted by [`issue_to_jfm_document`] via [`append_custom_section`] and
257/// recovered by [`JfmDocument::split_custom_sections`] so the write path
258/// can round-trip a field through `markdown_to_adf` for upload.
259#[derive(Debug, Clone, PartialEq, Eq)]
260pub struct CustomFieldSection {
261    /// Human-readable field name captured from the `<!-- field: Name (id) -->` tag.
262    pub name: String,
263
264    /// Stable field ID captured from the tag (e.g., `customfield_19300`).
265    pub id: String,
266
267    /// Markdown body of the section (no trailing newline guarantees).
268    pub body: String,
269}
270
271/// Splits a JFM body into the primary body and any trailing custom-field
272/// sections.
273///
274/// Recognizes the separator emitted by [`append_custom_section`]: a line
275/// containing only `---` followed by a line matching
276/// `<!-- field: <name> (<id>) -->`. Everything before the first such
277/// separator is the primary body; each subsequent separator starts a new
278/// section whose content runs up to the next separator (or end of input).
279pub(crate) fn split_custom_sections(body: &str) -> (String, Vec<CustomFieldSection>) {
280    // (marker_start, content_start, name, id)
281    let mut markers: Vec<(usize, usize, String, String)> = Vec::new();
282    let mut cursor = 0;
283
284    while cursor < body.len() {
285        let Some(marker_start) = find_next_marker(body, cursor) else {
286            break;
287        };
288
289        // Require exactly "---" on its own line, followed by "\n" (or "\r\n").
290        let after_dashes = marker_start + 3;
291        let after_nl = if body[after_dashes..].starts_with("\r\n") {
292            after_dashes + 2
293        } else if body[after_dashes..].starts_with('\n') {
294            after_dashes + 1
295        } else {
296            cursor = after_dashes;
297            continue;
298        };
299
300        if let Some((name, id, content_start)) = parse_field_tag_line(body, after_nl) {
301            markers.push((marker_start, content_start, name, id));
302            cursor = content_start;
303        } else {
304            cursor = after_nl;
305        }
306    }
307
308    if markers.is_empty() {
309        return (body.to_string(), Vec::new());
310    }
311
312    let first_marker = markers[0].0;
313    let main_body = body[..first_marker].trim_end_matches('\n').to_string();
314
315    let mut sections = Vec::with_capacity(markers.len());
316    for i in 0..markers.len() {
317        let (_marker, content_start, name, id) = &markers[i];
318        let content_end = markers.get(i + 1).map_or(body.len(), |next| next.0);
319        let raw = &body[*content_start..content_end];
320        let trimmed = raw.trim_matches('\n').to_string();
321        sections.push(CustomFieldSection {
322            name: name.clone(),
323            id: id.clone(),
324            body: trimmed,
325        });
326    }
327
328    (main_body, sections)
329}
330
331/// Finds the next line-anchored `---` at or after `from`. A marker is
332/// either at the very start of `body` or preceded by a newline.
333fn find_next_marker(body: &str, from: usize) -> Option<usize> {
334    if from == 0 && body.starts_with("---") {
335        return Some(0);
336    }
337    body[from..]
338        .find("\n---")
339        .map(|rel| from + rel + 1)
340        .filter(|p| *p + 3 <= body.len())
341}
342
343/// Parses an HTML-comment field tag at `start` and returns
344/// `(name, id, content_start)` where `content_start` is the byte offset
345/// immediately after the comment line's newline.
346fn parse_field_tag_line(body: &str, start: usize) -> Option<(String, String, usize)> {
347    let rest = body.get(start..)?;
348    let line_end = rest.find('\n').unwrap_or(rest.len());
349    let line = rest[..line_end].trim_end_matches('\r');
350
351    let after_open = line.strip_prefix("<!--")?.trim_start();
352    let after_field = after_open.strip_prefix("field:")?.trim_start();
353    let close_idx = after_field.rfind("-->")?;
354    let inner = after_field[..close_idx].trim_end();
355
356    let paren_open = inner.rfind('(')?;
357    let name = inner[..paren_open].trim().to_string();
358    let rest_part = inner.get(paren_open + 1..)?;
359    let paren_close = rest_part.rfind(')')?;
360    let id = rest_part[..paren_close].trim().to_string();
361
362    if name.is_empty() || id.is_empty() {
363        return None;
364    }
365
366    let next_line_start = (start + line_end + 1).min(body.len());
367    Some((name, id, next_line_start))
368}
369
370/// Returns `true` if `value` has the shape of an Atlassian Document Format
371/// document (`{"type":"doc","version":_,"content":[...]}`).
372fn is_adf_document(value: &serde_json::Value) -> bool {
373    let Some(obj) = value.as_object() else {
374        return false;
375    };
376    obj.get("type").and_then(|t| t.as_str()) == Some("doc")
377        && obj.contains_key("version")
378        && obj.contains_key("content")
379}
380
381/// Converts a custom field's raw JSON value into a scalar YAML
382/// representation suitable for frontmatter serialization.
383///
384/// - Option/select objects (`{self, value, id}`) collapse to their
385///   `value` string.
386/// - User-picker objects collapse to their `displayName`.
387/// - Arrays recurse per element, dropping null/unknown entries.
388/// - Primitives (bool/number/string) pass through unchanged.
389/// - Unknown objects pass through as a structured YAML mapping.
390/// - Null returns `None`.
391fn extract_custom_field_scalar(value: &serde_json::Value) -> Option<serde_yaml::Value> {
392    use serde_json::Value as J;
393    match value {
394        J::Null => None,
395        J::Bool(_) | J::Number(_) | J::String(_) => json_to_yaml(value),
396        J::Array(items) => {
397            let extracted: Vec<_> = items
398                .iter()
399                .filter_map(extract_custom_field_scalar)
400                .collect();
401            if extracted.is_empty() {
402                None
403            } else {
404                Some(serde_yaml::Value::Sequence(extracted))
405            }
406        }
407        J::Object(map) => {
408            if let Some(v) = map.get("value").and_then(|v| v.as_str()) {
409                Some(serde_yaml::Value::String(v.to_string()))
410            } else if let Some(name) = map.get("displayName").and_then(|v| v.as_str()) {
411                Some(serde_yaml::Value::String(name.to_string()))
412            } else {
413                json_to_yaml(value)
414            }
415        }
416    }
417}
418
419fn json_to_yaml(value: &serde_json::Value) -> Option<serde_yaml::Value> {
420    serde_yaml::to_value(value).ok()
421}
422
423/// Converts a [`ContentItem`] into a [`JfmDocument`] with YAML frontmatter.
424///
425/// Dispatches on the [`ContentMetadata`] variant to populate the correct
426/// frontmatter fields for JIRA or Confluence content.
427pub fn content_item_to_document(item: &ContentItem, instance_url: &str) -> Result<JfmDocument> {
428    let body = if let Some(ref adf_value) = item.body_adf {
429        let adf_doc: AdfDocument =
430            serde_json::from_value(adf_value.clone()).context("Failed to parse ADF description")?;
431        adf_to_markdown(&adf_doc)?
432    } else {
433        String::new()
434    };
435
436    let frontmatter = match &item.metadata {
437        ContentMetadata::Jira {
438            status,
439            issue_type,
440            assignee,
441            priority,
442            labels,
443        } => JfmFrontmatter::Jira(JiraFrontmatter {
444            instance: instance_url.to_string(),
445            key: item.id.clone(),
446            project: None,
447            summary: item.title.clone(),
448            status: status.clone(),
449            issue_type: issue_type.clone(),
450            assignee: assignee.clone(),
451            priority: priority.clone(),
452            labels: labels.clone(),
453            custom_fields: BTreeMap::new(),
454        }),
455        ContentMetadata::Confluence {
456            space_key,
457            status,
458            version,
459            parent_id,
460        } => JfmFrontmatter::Confluence(ConfluenceFrontmatter {
461            instance: instance_url.to_string(),
462            page_id: item.id.clone(),
463            title: item.title.clone(),
464            space_key: space_key.clone(),
465            status: status.clone(),
466            version: *version,
467            parent_id: parent_id.clone(),
468        }),
469    };
470
471    Ok(JfmDocument { frontmatter, body })
472}
473
474impl JfmDocument {
475    /// Parses a JFM document from a string.
476    ///
477    /// Expects the format: `---\n<yaml frontmatter>\n---\n<markdown body>`
478    pub fn parse(input: &str) -> Result<Self> {
479        let trimmed = input.trim_start();
480
481        if !trimmed.starts_with("---") {
482            return Err(AtlassianError::InvalidDocument(
483                "Document must start with '---' frontmatter delimiter".to_string(),
484            )
485            .into());
486        }
487
488        // Find the closing '---' delimiter (skip the opening one)
489        let after_opening = &trimmed[3..];
490        let after_opening = after_opening.strip_prefix('\n').unwrap_or(after_opening);
491
492        let closing_pos = after_opening.find("\n---").ok_or_else(|| {
493            AtlassianError::InvalidDocument(
494                "Missing closing '---' frontmatter delimiter".to_string(),
495            )
496        })?;
497
498        let frontmatter_yaml = &after_opening[..closing_pos];
499        let after_closing = &after_opening[closing_pos + 4..]; // skip "\n---"
500
501        // Strip the first newline after the closing delimiter
502        let body = after_closing
503            .strip_prefix('\n')
504            .unwrap_or(after_closing)
505            .to_string();
506
507        let frontmatter: JfmFrontmatter = serde_yaml::from_str(frontmatter_yaml)
508            .context("Failed to parse JFM frontmatter YAML")?;
509
510        Ok(Self { frontmatter, body })
511    }
512
513    /// Renders the document back to a string with YAML frontmatter and markdown body.
514    pub fn render(&self) -> Result<String> {
515        let frontmatter_yaml = serde_yaml::to_string(&self.frontmatter)
516            .context("Failed to serialize JFM frontmatter to YAML")?;
517
518        let mut output = String::new();
519        output.push_str("---\n");
520        output.push_str(&frontmatter_yaml);
521        output.push_str("---\n");
522        if !self.body.is_empty() {
523            output.push('\n');
524            output.push_str(&self.body);
525            // Ensure trailing newline
526            if !self.body.ends_with('\n') {
527                output.push('\n');
528            }
529        }
530
531        Ok(output)
532    }
533
534    /// Splits the body into the primary markdown and any trailing custom-field
535    /// sections emitted by the custom-field-aware read path.
536    ///
537    /// Non-destructive — returns owned strings and leaves `self.body`
538    /// unchanged so `render()` continues to produce a lossless round-trip.
539    pub fn split_custom_sections(&self) -> (String, Vec<CustomFieldSection>) {
540        split_custom_sections(&self.body)
541    }
542}
543
544#[cfg(test)]
545#[allow(clippy::unwrap_used, clippy::expect_used)]
546mod tests {
547    use super::*;
548
549    #[test]
550    fn parse_basic_document() {
551        let input = "---\ntype: jira\ninstance: https://org.atlassian.net\nkey: PROJ-123\nsummary: Fix the bug\n---\n\nThis is the description.\n";
552        let doc = JfmDocument::parse(input).unwrap();
553        assert_eq!(doc.frontmatter.doc_type(), "jira");
554        assert_eq!(doc.frontmatter.id(), "PROJ-123");
555        assert_eq!(doc.frontmatter.title(), "Fix the bug");
556        assert_eq!(doc.body, "\nThis is the description.\n");
557    }
558
559    #[test]
560    fn parse_with_optional_fields() {
561        let input = "---\ntype: jira\ninstance: https://org.atlassian.net\nkey: PROJ-456\nsummary: A story\nstatus: In Progress\nissue_type: Story\nassignee: Alice\npriority: High\nlabels:\n  - backend\n  - auth\n---\n\nDescription here.\n";
562        let doc = JfmDocument::parse(input).unwrap();
563        match &doc.frontmatter {
564            JfmFrontmatter::Jira(fm) => {
565                assert_eq!(fm.status.as_deref(), Some("In Progress"));
566                assert_eq!(fm.issue_type.as_deref(), Some("Story"));
567                assert_eq!(fm.assignee.as_deref(), Some("Alice"));
568                assert_eq!(fm.priority.as_deref(), Some("High"));
569                assert_eq!(fm.labels, vec!["backend", "auth"]);
570            }
571            _ => panic!("Expected Jira frontmatter"),
572        }
573    }
574
575    #[test]
576    fn parse_empty_body() {
577        let input = "---\ntype: jira\ninstance: https://org.atlassian.net\nkey: PROJ-1\nsummary: Empty\n---\n";
578        let doc = JfmDocument::parse(input).unwrap();
579        assert_eq!(doc.body, "");
580    }
581
582    #[test]
583    fn parse_body_with_triple_dashes() {
584        let input = "---\ntype: jira\ninstance: https://org.atlassian.net\nkey: PROJ-1\nsummary: Dashes\n---\n\nContent with --- dashes in it.\n";
585        let doc = JfmDocument::parse(input).unwrap();
586        assert!(doc.body.contains("--- dashes"));
587    }
588
589    #[test]
590    fn parse_missing_opening_delimiter() {
591        let input = "type: jira\nkey: PROJ-1\n";
592        let result = JfmDocument::parse(input);
593        assert!(result.is_err());
594    }
595
596    #[test]
597    fn parse_missing_closing_delimiter() {
598        let input = "---\ntype: jira\nkey: PROJ-1\n";
599        let result = JfmDocument::parse(input);
600        assert!(result.is_err());
601    }
602
603    #[test]
604    fn render_basic_document() {
605        let doc = JfmDocument {
606            frontmatter: JfmFrontmatter::Jira(JiraFrontmatter {
607                instance: "https://org.atlassian.net".to_string(),
608                key: "PROJ-123".to_string(),
609                project: None,
610                summary: "Fix the bug".to_string(),
611                status: None,
612                issue_type: None,
613                assignee: None,
614                priority: None,
615                labels: vec![],
616                custom_fields: BTreeMap::new(),
617            }),
618            body: "Description here.".to_string(),
619        };
620
621        let output = doc.render().unwrap();
622        assert!(output.starts_with("---\n"));
623        assert!(output.contains("key: PROJ-123"));
624        assert!(output.contains("summary: Fix the bug"));
625        assert!(output.contains("---\n\nDescription here.\n"));
626    }
627
628    #[test]
629    fn render_round_trip() {
630        let doc = JfmDocument {
631            frontmatter: JfmFrontmatter::Jira(JiraFrontmatter {
632                instance: "https://org.atlassian.net".to_string(),
633                key: "PROJ-789".to_string(),
634                project: None,
635                summary: "Round trip test".to_string(),
636                status: Some("Open".to_string()),
637                issue_type: Some("Bug".to_string()),
638                assignee: None,
639                priority: None,
640                labels: vec!["test".to_string()],
641                custom_fields: BTreeMap::new(),
642            }),
643            body: "# Heading\n\nSome text.\n".to_string(),
644        };
645
646        let rendered = doc.render().unwrap();
647        let restored = JfmDocument::parse(&rendered).unwrap();
648
649        assert_eq!(doc.frontmatter.id(), restored.frontmatter.id());
650        assert_eq!(doc.frontmatter.title(), restored.frontmatter.title());
651        match &restored.frontmatter {
652            JfmFrontmatter::Jira(fm) => {
653                assert_eq!(fm.status.as_deref(), Some("Open"));
654            }
655            _ => panic!("Expected Jira frontmatter"),
656        }
657        assert!(restored.body.contains("# Heading"));
658        assert!(restored.body.contains("Some text."));
659    }
660
661    // ── validate_issue_key tests ────────��────────────────────────────
662
663    #[test]
664    fn valid_issue_keys() {
665        assert!(validate_issue_key("PROJ-123").is_ok());
666        assert!(validate_issue_key("AB-1").is_ok());
667        assert!(validate_issue_key("A1B-999").is_ok());
668    }
669
670    #[test]
671    fn invalid_issue_keys() {
672        assert!(validate_issue_key("proj-123").is_err());
673        assert!(validate_issue_key("PROJ").is_err());
674        assert!(validate_issue_key("PROJ-").is_err());
675        assert!(validate_issue_key("-123").is_err());
676        assert!(validate_issue_key("").is_err());
677    }
678
679    // ── issue_to_jfm_document tests ─────────��─────────────────────────
680
681    fn sample_issue() -> JiraIssue {
682        JiraIssue {
683            key: "TEST-42".to_string(),
684            summary: "Fix the widget".to_string(),
685            description_adf: Some(serde_json::json!({
686                "version": 1,
687                "type": "doc",
688                "content": [{
689                    "type": "paragraph",
690                    "content": [{"type": "text", "text": "Hello world"}]
691                }]
692            })),
693            status: Some("Open".to_string()),
694            issue_type: Some("Bug".to_string()),
695            assignee: Some("Alice".to_string()),
696            priority: Some("High".to_string()),
697            labels: vec!["backend".to_string()],
698            custom_fields: Vec::new(),
699        }
700    }
701
702    #[test]
703    fn issue_to_jfm_with_description() {
704        let issue = sample_issue();
705        let doc = issue_to_jfm_document(&issue, "https://org.atlassian.net").unwrap();
706        assert_eq!(doc.frontmatter.id(), "TEST-42");
707        assert_eq!(doc.frontmatter.title(), "Fix the widget");
708        match &doc.frontmatter {
709            JfmFrontmatter::Jira(fm) => {
710                assert_eq!(fm.status.as_deref(), Some("Open"));
711                assert_eq!(fm.issue_type.as_deref(), Some("Bug"));
712            }
713            _ => panic!("Expected Jira frontmatter"),
714        }
715        assert!(doc.body.contains("Hello world"));
716    }
717
718    #[test]
719    fn issue_to_jfm_without_description() {
720        let mut issue = sample_issue();
721        issue.description_adf = None;
722        let doc = issue_to_jfm_document(&issue, "https://org.atlassian.net").unwrap();
723        assert_eq!(doc.body, "");
724    }
725
726    #[test]
727    fn issue_to_jfm_minimal_fields() {
728        let issue = JiraIssue {
729            key: "MIN-1".to_string(),
730            summary: "Minimal".to_string(),
731            description_adf: None,
732            status: None,
733            issue_type: None,
734            assignee: None,
735            priority: None,
736            labels: vec![],
737            custom_fields: Vec::new(),
738        };
739        let doc = issue_to_jfm_document(&issue, "https://test.atlassian.net").unwrap();
740        assert_eq!(doc.frontmatter.instance(), "https://test.atlassian.net");
741        match &doc.frontmatter {
742            JfmFrontmatter::Jira(fm) => {
743                assert!(fm.status.is_none());
744                assert!(fm.labels.is_empty());
745            }
746            _ => panic!("Expected Jira frontmatter"),
747        }
748    }
749
750    #[test]
751    fn issue_to_jfm_renders_correctly() {
752        let issue = sample_issue();
753        let doc = issue_to_jfm_document(&issue, "https://org.atlassian.net").unwrap();
754        let rendered = doc.render().unwrap();
755        assert!(rendered.starts_with("---\n"));
756        assert!(rendered.contains("key: TEST-42"));
757        assert!(rendered.contains("Hello world"));
758    }
759
760    #[test]
761    fn render_skips_none_and_empty_fields() {
762        let doc = JfmDocument {
763            frontmatter: JfmFrontmatter::Jira(JiraFrontmatter {
764                instance: "https://org.atlassian.net".to_string(),
765                key: "PROJ-1".to_string(),
766                project: None,
767                summary: "Minimal".to_string(),
768                status: None,
769                issue_type: None,
770                assignee: None,
771                priority: None,
772                labels: vec![],
773                custom_fields: BTreeMap::new(),
774            }),
775            body: String::new(),
776        };
777
778        let output = doc.render().unwrap();
779        assert!(!output.contains("status:"));
780        assert!(!output.contains("issue_type:"));
781        assert!(!output.contains("labels:"));
782    }
783
784    // ── Custom field helpers ────────────────────────────────────────
785
786    // ── append_custom_section ───────────────────────────────────────
787
788    #[test]
789    fn append_custom_section_adds_leading_newline_when_body_unterminated() {
790        let mut body = String::from("No trailing newline");
791        let field = JiraCustomField {
792            id: "customfield_1".to_string(),
793            name: "AC".to_string(),
794            value: serde_json::Value::Null,
795        };
796        append_custom_section(&mut body, &field, "section body");
797        assert!(body.starts_with("No trailing newline\n\n---\n"));
798        assert!(body.ends_with('\n'));
799    }
800
801    #[test]
802    fn append_custom_section_terminates_body_when_section_lacks_newline() {
803        let mut body = String::from("Main body\n");
804        let field = JiraCustomField {
805            id: "customfield_1".to_string(),
806            name: "AC".to_string(),
807            value: serde_json::Value::Null,
808        };
809        append_custom_section(&mut body, &field, "no-trailing-nl");
810        assert!(body.ends_with("no-trailing-nl\n"));
811    }
812
813    #[test]
814    fn append_custom_section_into_empty_body_has_no_leading_blank_line() {
815        let mut body = String::new();
816        let field = JiraCustomField {
817            id: "customfield_1".to_string(),
818            name: "AC".to_string(),
819            value: serde_json::Value::Null,
820        };
821        append_custom_section(&mut body, &field, "s\n");
822        assert!(body.starts_with("---\n<!-- field: AC (customfield_1) -->\n\ns\n"));
823    }
824
825    #[test]
826    fn jira_custom_fields_returns_none_for_confluence_frontmatter() {
827        let fm = JfmFrontmatter::Confluence(ConfluenceFrontmatter {
828            instance: "https://org.atlassian.net".to_string(),
829            page_id: "1".to_string(),
830            title: "t".to_string(),
831            space_key: "X".to_string(),
832            status: None,
833            version: None,
834            parent_id: None,
835        });
836        assert!(fm.jira_custom_fields().is_none());
837    }
838
839    #[test]
840    fn jira_custom_fields_returns_scalars_for_jira_frontmatter() {
841        let mut custom = BTreeMap::new();
842        custom.insert("K".to_string(), serde_yaml::Value::from("V"));
843        let fm = JfmFrontmatter::Jira(JiraFrontmatter {
844            instance: "https://org.atlassian.net".to_string(),
845            key: "X-1".to_string(),
846            project: None,
847            summary: "s".to_string(),
848            status: None,
849            issue_type: None,
850            assignee: None,
851            priority: None,
852            labels: vec![],
853            custom_fields: custom,
854        });
855        let got = fm.jira_custom_fields().unwrap();
856        assert_eq!(got.len(), 1);
857        assert_eq!(got.get("K").unwrap(), &serde_yaml::Value::from("V"));
858    }
859
860    #[test]
861    fn is_adf_document_detects_doc_shape() {
862        let adf = serde_json::json!({
863            "type": "doc",
864            "version": 1,
865            "content": [{"type": "paragraph", "content": []}]
866        });
867        assert!(is_adf_document(&adf));
868    }
869
870    #[test]
871    fn is_adf_document_rejects_scalar_and_other_objects() {
872        assert!(!is_adf_document(&serde_json::json!("string")));
873        assert!(!is_adf_document(&serde_json::json!(42)));
874        assert!(!is_adf_document(&serde_json::json!({"type": "option"})));
875        assert!(!is_adf_document(&serde_json::json!({
876            "type": "doc", "version": 1
877        })));
878    }
879
880    #[test]
881    fn extract_scalar_passes_through_primitives() {
882        assert_eq!(
883            extract_custom_field_scalar(&serde_json::json!(7)),
884            Some(serde_yaml::Value::from(7_i64))
885        );
886        assert_eq!(
887            extract_custom_field_scalar(&serde_json::json!("hello")),
888            Some(serde_yaml::Value::String("hello".to_string()))
889        );
890        assert_eq!(
891            extract_custom_field_scalar(&serde_json::json!(true)),
892            Some(serde_yaml::Value::Bool(true))
893        );
894        assert_eq!(extract_custom_field_scalar(&serde_json::Value::Null), None);
895    }
896
897    #[test]
898    fn extract_scalar_collapses_option_object_to_value_string() {
899        let value = serde_json::json!({
900            "self": "https://example.atlassian.net/rest/api/3/customFieldOption/12345",
901            "value": "Unplanned",
902            "id": "12345"
903        });
904        assert_eq!(
905            extract_custom_field_scalar(&value),
906            Some(serde_yaml::Value::String("Unplanned".to_string()))
907        );
908    }
909
910    #[test]
911    fn extract_scalar_collapses_user_object_to_display_name() {
912        let value = serde_json::json!({
913            "accountId": "abc123",
914            "displayName": "Alice",
915            "emailAddress": "alice@example.com"
916        });
917        assert_eq!(
918            extract_custom_field_scalar(&value),
919            Some(serde_yaml::Value::String("Alice".to_string()))
920        );
921    }
922
923    #[test]
924    fn extract_scalar_recurses_into_arrays_and_drops_nulls() {
925        let value = serde_json::json!([
926            {"value": "A"},
927            null,
928            {"displayName": "Bob"},
929            42
930        ]);
931        let extracted = extract_custom_field_scalar(&value).unwrap();
932        assert_eq!(
933            extracted,
934            serde_yaml::Value::Sequence(vec![
935                serde_yaml::Value::String("A".to_string()),
936                serde_yaml::Value::String("Bob".to_string()),
937                serde_yaml::Value::from(42_i64),
938            ])
939        );
940    }
941
942    #[test]
943    fn extract_scalar_empty_array_returns_none() {
944        let value = serde_json::json!([null, null]);
945        assert_eq!(extract_custom_field_scalar(&value), None);
946    }
947
948    #[test]
949    fn issue_with_scalar_custom_field_goes_to_frontmatter() {
950        let issue = JiraIssue {
951            key: "ACCS-1".to_string(),
952            summary: "S".to_string(),
953            description_adf: None,
954            status: None,
955            issue_type: None,
956            assignee: None,
957            priority: None,
958            labels: vec![],
959            custom_fields: vec![JiraCustomField {
960                id: "customfield_10001".to_string(),
961                name: "Planned / Unplanned Work".to_string(),
962                value: serde_json::json!({"value": "Unplanned", "id": "42"}),
963            }],
964        };
965        let doc = issue_to_jfm_document(&issue, "https://org.atlassian.net").unwrap();
966        let rendered = doc.render().unwrap();
967        assert!(rendered.contains("custom_fields:"));
968        assert!(rendered.contains("Planned / Unplanned Work"));
969        assert!(rendered.contains("Unplanned"));
970        assert!(!rendered.contains("<!-- field:"));
971    }
972
973    #[test]
974    fn issue_with_adf_custom_field_becomes_body_section() {
975        let adf_value = serde_json::json!({
976            "type": "doc",
977            "version": 1,
978            "content": [{
979                "type": "paragraph",
980                "content": [{"type": "text", "text": "Criterion one"}]
981            }]
982        });
983        let issue = JiraIssue {
984            key: "ACCS-1".to_string(),
985            summary: "S".to_string(),
986            description_adf: None,
987            status: None,
988            issue_type: None,
989            assignee: None,
990            priority: None,
991            labels: vec![],
992            custom_fields: vec![JiraCustomField {
993                id: "customfield_19300".to_string(),
994                name: "Acceptance Criteria".to_string(),
995                value: adf_value,
996            }],
997        };
998        let doc = issue_to_jfm_document(&issue, "https://org.atlassian.net").unwrap();
999        let rendered = doc.render().unwrap();
1000        assert!(rendered.contains("<!-- field: Acceptance Criteria (customfield_19300) -->"));
1001        assert!(rendered.contains("Criterion one"));
1002        assert!(!rendered.contains("custom_fields:"));
1003    }
1004
1005    #[test]
1006    fn issue_with_mixed_custom_fields() {
1007        let adf_value = serde_json::json!({
1008            "type": "doc",
1009            "version": 1,
1010            "content": [{"type": "paragraph", "content": [{"type": "text", "text": "AC body"}]}]
1011        });
1012        let issue = JiraIssue {
1013            key: "ACCS-1".to_string(),
1014            summary: "S".to_string(),
1015            description_adf: Some(serde_json::json!({
1016                "type": "doc",
1017                "version": 1,
1018                "content": [{"type": "paragraph", "content": [{"type": "text", "text": "Main"}]}]
1019            })),
1020            status: None,
1021            issue_type: None,
1022            assignee: None,
1023            priority: None,
1024            labels: vec![],
1025            custom_fields: vec![
1026                JiraCustomField {
1027                    id: "customfield_19300".to_string(),
1028                    name: "Acceptance Criteria".to_string(),
1029                    value: adf_value,
1030                },
1031                JiraCustomField {
1032                    id: "customfield_10001".to_string(),
1033                    name: "Sprint Label".to_string(),
1034                    value: serde_json::json!("Q1"),
1035                },
1036            ],
1037        };
1038        let doc = issue_to_jfm_document(&issue, "https://org.atlassian.net").unwrap();
1039        let rendered = doc.render().unwrap();
1040        assert!(rendered.contains("custom_fields:"));
1041        assert!(rendered.contains("Sprint Label: Q1"));
1042        assert!(rendered.contains("Main"));
1043        assert!(rendered.contains("<!-- field: Acceptance Criteria"));
1044        assert!(rendered.contains("AC body"));
1045    }
1046
1047    // ── split_custom_sections ──────────────────────────────────────
1048
1049    #[test]
1050    fn split_custom_sections_no_sections_returns_body_unchanged() {
1051        let (body, sections) = split_custom_sections("Hello world\n\nMore text\n");
1052        assert_eq!(body, "Hello world\n\nMore text\n");
1053        assert!(sections.is_empty());
1054    }
1055
1056    #[test]
1057    fn split_custom_sections_extracts_single_section() {
1058        let input = "Main body\n\n---\n<!-- field: Acceptance Criteria (customfield_19300) -->\n\n- Item 1\n- Item 2\n";
1059        let (body, sections) = split_custom_sections(input);
1060        assert_eq!(body, "Main body");
1061        assert_eq!(sections.len(), 1);
1062        assert_eq!(sections[0].name, "Acceptance Criteria");
1063        assert_eq!(sections[0].id, "customfield_19300");
1064        assert_eq!(sections[0].body, "- Item 1\n- Item 2");
1065    }
1066
1067    #[test]
1068    fn split_custom_sections_extracts_multiple_sections() {
1069        let input = "Main\n\n---\n<!-- field: AC (customfield_1) -->\n\nAC body\n\n---\n<!-- field: Notes (customfield_2) -->\n\nNotes body\n";
1070        let (body, sections) = split_custom_sections(input);
1071        assert_eq!(body, "Main");
1072        assert_eq!(sections.len(), 2);
1073        assert_eq!(sections[0].id, "customfield_1");
1074        assert_eq!(sections[0].body, "AC body");
1075        assert_eq!(sections[1].id, "customfield_2");
1076        assert_eq!(sections[1].body, "Notes body");
1077    }
1078
1079    #[test]
1080    fn split_custom_sections_preserves_triple_dashes_inside_body() {
1081        // A `---` without the follow-up comment tag is just content, not a
1082        // section separator.
1083        let input =
1084            "Before\n\n---\n\nStill body\n\n---\n<!-- field: AC (customfield_1) -->\n\nSection\n";
1085        let (body, sections) = split_custom_sections(input);
1086        assert!(body.contains("Still body"));
1087        assert_eq!(sections.len(), 1);
1088        assert_eq!(sections[0].body, "Section");
1089    }
1090
1091    #[test]
1092    fn split_custom_sections_body_starting_with_marker() {
1093        let input = "---\n<!-- field: AC (customfield_1) -->\n\nSection body\n";
1094        let (body, sections) = split_custom_sections(input);
1095        assert!(body.is_empty());
1096        assert_eq!(sections.len(), 1);
1097        assert_eq!(sections[0].id, "customfield_1");
1098        assert_eq!(sections[0].body, "Section body");
1099    }
1100
1101    #[test]
1102    fn split_custom_sections_rejects_dash_sequence_without_newline() {
1103        // `---foo` on a line is not a valid marker.
1104        let input = "Before\n---foo\nMore\n---\n<!-- field: AC (customfield_1) -->\n\nS\n";
1105        let (body, sections) = split_custom_sections(input);
1106        assert!(body.contains("---foo"));
1107        assert!(body.contains("More"));
1108        assert_eq!(sections.len(), 1);
1109    }
1110
1111    #[test]
1112    fn split_custom_sections_handles_crlf_line_endings() {
1113        let input = "Main\r\n\r\n---\r\n<!-- field: AC (customfield_1) -->\r\n\r\nSection\r\n";
1114        let (_body, sections) = split_custom_sections(input);
1115        assert_eq!(sections.len(), 1);
1116        assert_eq!(sections[0].name, "AC");
1117        assert_eq!(sections[0].id, "customfield_1");
1118    }
1119
1120    #[test]
1121    fn split_custom_sections_rejects_malformed_field_tag() {
1122        // The `---` is present and line-anchored, but the next line is not a
1123        // proper `<!-- field: Name (id) -->` tag, so the section is treated
1124        // as plain body content.
1125        let input = "Before\n\n---\n<!-- not a field tag -->\n\nStill body\n";
1126        let (body, sections) = split_custom_sections(input);
1127        assert!(body.contains("<!-- not a field tag -->"));
1128        assert!(sections.is_empty());
1129    }
1130
1131    #[test]
1132    fn split_custom_sections_roundtrips_through_render() {
1133        let issue = JiraIssue {
1134            key: "TEST-1".to_string(),
1135            summary: "S".to_string(),
1136            description_adf: Some(serde_json::json!({
1137                "type": "doc", "version": 1,
1138                "content": [{"type":"paragraph","content":[{"type":"text","text":"Main"}]}]
1139            })),
1140            status: None,
1141            issue_type: None,
1142            assignee: None,
1143            priority: None,
1144            labels: vec![],
1145            custom_fields: vec![JiraCustomField {
1146                id: "customfield_19300".to_string(),
1147                name: "Acceptance Criteria".to_string(),
1148                value: serde_json::json!({
1149                    "type": "doc", "version": 1,
1150                    "content": [{"type":"paragraph","content":[{"type":"text","text":"AC line"}]}]
1151                }),
1152            }],
1153        };
1154        let doc = issue_to_jfm_document(&issue, "https://org.atlassian.net").unwrap();
1155        let rendered = doc.render().unwrap();
1156        let reparsed = JfmDocument::parse(&rendered).unwrap();
1157        let (body, sections) = reparsed.split_custom_sections();
1158        assert!(body.contains("Main"));
1159        assert_eq!(sections.len(), 1);
1160        assert_eq!(sections[0].id, "customfield_19300");
1161        assert_eq!(sections[0].name, "Acceptance Criteria");
1162        assert!(sections[0].body.contains("AC line"));
1163    }
1164
1165    #[test]
1166    fn issue_with_null_custom_field_is_omitted() {
1167        let issue = JiraIssue {
1168            key: "ACCS-1".to_string(),
1169            summary: "S".to_string(),
1170            description_adf: None,
1171            status: None,
1172            issue_type: None,
1173            assignee: None,
1174            priority: None,
1175            labels: vec![],
1176            custom_fields: vec![JiraCustomField {
1177                id: "customfield_99".to_string(),
1178                name: "Empty Field".to_string(),
1179                value: serde_json::Value::Null,
1180            }],
1181        };
1182        let doc = issue_to_jfm_document(&issue, "https://org.atlassian.net").unwrap();
1183        let rendered = doc.render().unwrap();
1184        assert!(!rendered.contains("custom_fields:"));
1185        assert!(!rendered.contains("Empty Field"));
1186    }
1187
1188    // ── Confluence frontmatter tests ───���─────────────────────────────
1189
1190    #[test]
1191    fn parse_confluence_document() {
1192        let input = "---\ntype: confluence\ninstance: https://org.atlassian.net\npage_id: '12345'\ntitle: Architecture Overview\nspace_key: ENG\nstatus: current\nversion: 7\n---\n\nPage body here.\n";
1193        let doc = JfmDocument::parse(input).unwrap();
1194        assert_eq!(doc.frontmatter.doc_type(), "confluence");
1195        assert_eq!(doc.frontmatter.id(), "12345");
1196        assert_eq!(doc.frontmatter.title(), "Architecture Overview");
1197        match &doc.frontmatter {
1198            JfmFrontmatter::Confluence(fm) => {
1199                assert_eq!(fm.space_key, "ENG");
1200                assert_eq!(fm.status.as_deref(), Some("current"));
1201                assert_eq!(fm.version, Some(7));
1202            }
1203            _ => panic!("Expected Confluence frontmatter"),
1204        }
1205    }
1206
1207    #[test]
1208    fn render_confluence_document() {
1209        let doc = JfmDocument {
1210            frontmatter: JfmFrontmatter::Confluence(ConfluenceFrontmatter {
1211                instance: "https://org.atlassian.net".to_string(),
1212                page_id: "12345".to_string(),
1213                title: "Architecture Overview".to_string(),
1214                space_key: "ENG".to_string(),
1215                status: Some("current".to_string()),
1216                version: Some(7),
1217                parent_id: None,
1218            }),
1219            body: "Page body here.\n".to_string(),
1220        };
1221
1222        let output = doc.render().unwrap();
1223        assert!(output.starts_with("---\n"));
1224        assert!(output.contains("type: confluence"));
1225        assert!(output.contains("page_id:"));
1226        assert!(output.contains("space_key: ENG"));
1227        assert!(output.contains("Page body here."));
1228    }
1229
1230    #[test]
1231    fn confluence_round_trip() {
1232        let doc = JfmDocument {
1233            frontmatter: JfmFrontmatter::Confluence(ConfluenceFrontmatter {
1234                instance: "https://org.atlassian.net".to_string(),
1235                page_id: "99999".to_string(),
1236                title: "Round trip".to_string(),
1237                space_key: "DEV".to_string(),
1238                status: None,
1239                version: Some(3),
1240                parent_id: Some("88888".to_string()),
1241            }),
1242            body: "Content.\n".to_string(),
1243        };
1244
1245        let rendered = doc.render().unwrap();
1246        let restored = JfmDocument::parse(&rendered).unwrap();
1247        assert_eq!(restored.frontmatter.id(), "99999");
1248        assert_eq!(restored.frontmatter.title(), "Round trip");
1249        match &restored.frontmatter {
1250            JfmFrontmatter::Confluence(fm) => {
1251                assert_eq!(fm.space_key, "DEV");
1252                assert_eq!(fm.version, Some(3));
1253                assert_eq!(fm.parent_id.as_deref(), Some("88888"));
1254            }
1255            _ => panic!("Expected Confluence frontmatter"),
1256        }
1257    }
1258
1259    // ── content_item_to_document tests ───────────────────────────────
1260
1261    #[test]
1262    fn content_item_jira_to_document() {
1263        let item = ContentItem {
1264            id: "PROJ-42".to_string(),
1265            title: "A JIRA issue".to_string(),
1266            body_adf: Some(serde_json::json!({
1267                "version": 1,
1268                "type": "doc",
1269                "content": [{
1270                    "type": "paragraph",
1271                    "content": [{"type": "text", "text": "Content"}]
1272                }]
1273            })),
1274            metadata: ContentMetadata::Jira {
1275                status: Some("Open".to_string()),
1276                issue_type: Some("Bug".to_string()),
1277                assignee: None,
1278                priority: None,
1279                labels: vec![],
1280            },
1281        };
1282        let doc = content_item_to_document(&item, "https://org.atlassian.net").unwrap();
1283        assert_eq!(doc.frontmatter.doc_type(), "jira");
1284        assert_eq!(doc.frontmatter.id(), "PROJ-42");
1285        assert!(doc.body.contains("Content"));
1286    }
1287
1288    #[test]
1289    fn content_item_confluence_to_document() {
1290        let item = ContentItem {
1291            id: "12345".to_string(),
1292            title: "A Confluence page".to_string(),
1293            body_adf: None,
1294            metadata: ContentMetadata::Confluence {
1295                space_key: "ENG".to_string(),
1296                status: Some("current".to_string()),
1297                version: Some(5),
1298                parent_id: None,
1299            },
1300        };
1301        let doc = content_item_to_document(&item, "https://org.atlassian.net").unwrap();
1302        assert_eq!(doc.frontmatter.doc_type(), "confluence");
1303        assert_eq!(doc.frontmatter.id(), "12345");
1304        assert_eq!(doc.frontmatter.title(), "A Confluence page");
1305    }
1306}