metis-docs-core 1.2.0

Core library for Flight Levels documentation management system
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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
use super::content::DocumentContent;
use super::helpers::FrontmatterParser;
use super::metadata::DocumentMetadata;
use super::traits::{Document, DocumentTemplate, DocumentValidationError};
use super::types::{DocumentId, DocumentType, Phase, Tag};
use chrono::Utc;
use gray_matter;
use std::path::Path;
use tera::{Context, Tera};

/// A Task document represents a concrete, actionable piece of work
#[derive(Debug)]
pub struct Task {
    core: super::traits::DocumentCore,
}

impl Task {
    /// Create a new Task document with content rendered from template
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        title: String,
        parent_id: Option<DocumentId>,     // Usually an Initiative
        parent_title: Option<String>,      // Title of parent for template rendering
        strategy_id: Option<DocumentId>,   // The strategy this task belongs to
        initiative_id: Option<DocumentId>, // The initiative this task belongs to
        blocked_by: Vec<DocumentId>,
        tags: Vec<Tag>,
        archived: bool,
        short_code: String,
    ) -> Result<Self, DocumentValidationError> {
        // Use embedded default template
        let template_content = include_str!("content.md");
        Self::new_with_template(
            title,
            parent_id,
            parent_title,
            strategy_id,
            initiative_id,
            blocked_by,
            tags,
            archived,
            short_code,
            template_content,
        )
    }

    /// Create a new Task document with a custom template
    #[allow(clippy::too_many_arguments)]
    pub fn new_with_template(
        title: String,
        parent_id: Option<DocumentId>,
        parent_title: Option<String>,
        strategy_id: Option<DocumentId>,
        initiative_id: Option<DocumentId>,
        blocked_by: Vec<DocumentId>,
        tags: Vec<Tag>,
        archived: bool,
        short_code: String,
        template_content: &str,
    ) -> Result<Self, DocumentValidationError> {
        // Create fresh metadata
        let metadata = DocumentMetadata::new(short_code);

        // Render the content template
        let mut tera = Tera::default();
        tera.add_raw_template("task_content", template_content)
            .map_err(|e| {
                DocumentValidationError::InvalidContent(format!("Template error: {}", e))
            })?;

        let mut context = Context::new();
        context.insert("title", &title);
        context.insert(
            "parent_title",
            &parent_title.unwrap_or_else(|| "Parent Initiative".to_string()),
        );

        let rendered_content = tera.render("task_content", &context).map_err(|e| {
            DocumentValidationError::InvalidContent(format!("Template render error: {}", e))
        })?;

        let content = DocumentContent::new(&rendered_content);

        Ok(Self {
            core: super::traits::DocumentCore {
                title,
                metadata,
                content,
                parent_id,
                blocked_by,
                tags,
                archived,
                strategy_id,
                initiative_id,
            },
        })
    }

    /// Create a Task document from existing data (used when loading from file)
    #[allow(clippy::too_many_arguments)]
    pub fn from_parts(
        title: String,
        metadata: DocumentMetadata,
        content: DocumentContent,
        parent_id: Option<DocumentId>,
        strategy_id: Option<DocumentId>,
        initiative_id: Option<DocumentId>,
        blocked_by: Vec<DocumentId>,
        tags: Vec<Tag>,
        archived: bool,
    ) -> Self {
        Self {
            core: super::traits::DocumentCore {
                title,
                metadata,
                content,
                parent_id,
                blocked_by,
                tags,
                archived,
                strategy_id,
                initiative_id,
            },
        }
    }

    /// Create a Task document by reading and parsing a file
    pub async fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, DocumentValidationError> {
        let raw_content = std::fs::read_to_string(path.as_ref()).map_err(|e| {
            DocumentValidationError::InvalidContent(format!("Failed to read file: {}", e))
        })?;

        Self::from_content(&raw_content)
    }

    /// Create a Task document from raw file content string
    pub fn from_content(raw_content: &str) -> Result<Self, DocumentValidationError> {
        // Parse frontmatter and content
        let parsed = gray_matter::Matter::<gray_matter::engine::YAML>::new().parse(raw_content);

        // Extract frontmatter data
        let frontmatter = parsed.data.ok_or_else(|| {
            DocumentValidationError::MissingRequiredField("frontmatter".to_string())
        })?;

        // Parse frontmatter into structured data
        let fm_map = match frontmatter {
            gray_matter::Pod::Hash(map) => map,
            _ => {
                return Err(DocumentValidationError::InvalidContent(
                    "Frontmatter must be a hash/map".to_string(),
                ))
            }
        };

        // Extract required fields
        let title = FrontmatterParser::extract_string(&fm_map, "title")?;
        let archived = FrontmatterParser::extract_bool(&fm_map, "archived").unwrap_or(false);

        // Parse timestamps
        let created_at = FrontmatterParser::extract_datetime(&fm_map, "created_at")?;
        let updated_at = FrontmatterParser::extract_datetime(&fm_map, "updated_at")?;
        let exit_criteria_met =
            FrontmatterParser::extract_bool(&fm_map, "exit_criteria_met").unwrap_or(false);

        // Parse tags
        let tags = FrontmatterParser::extract_tags(&fm_map)?;

        // Verify this is actually a task document
        let level = FrontmatterParser::extract_string(&fm_map, "level")?;
        if level != "task" {
            return Err(DocumentValidationError::InvalidContent(format!(
                "Expected level 'task', found '{}'",
                level
            )));
        }

        // Extract task-specific fields
        let parent_id = FrontmatterParser::extract_string(&fm_map, "parent")
            .ok()
            .map(DocumentId::from);
        let blocked_by = FrontmatterParser::extract_string_array(&fm_map, "blocked_by")
            .unwrap_or_default()
            .into_iter()
            .map(DocumentId::from)
            .collect();

        // Create metadata and content
        let short_code = FrontmatterParser::extract_string(&fm_map, "short_code")?;
        let metadata = DocumentMetadata::from_frontmatter(
            created_at,
            updated_at,
            exit_criteria_met,
            short_code,
        );
        let content = DocumentContent::from_markdown(&parsed.content);

        // Extract lineage from frontmatter
        let strategy_id = FrontmatterParser::extract_optional_string(&fm_map, "strategy_id")
            .map(DocumentId::from);
        let initiative_id = FrontmatterParser::extract_optional_string(&fm_map, "initiative_id")
            .map(DocumentId::from);

        Ok(Self::from_parts(
            title,
            metadata,
            content,
            parent_id,
            strategy_id,
            initiative_id,
            blocked_by,
            tags,
            archived,
        ))
    }

    /// Get the next phase in the Task sequence
    fn next_phase_in_sequence(current: Phase) -> Option<Phase> {
        use Phase::*;
        match current {
            Backlog => None, // Backlog doesn't auto-transition - must be explicitly assigned
            Todo => Some(Active),
            Active => Some(Completed),
            Completed => None, // Final phase
            Blocked => None,   // Blocked doesn't auto-transition
            _ => None,         // Invalid phase for Task
        }
    }

    /// Update the phase tag in the document's tags
    fn update_phase_tag(&mut self, new_phase: Phase) {
        // Remove any existing phase tags
        self.core.tags.retain(|tag| !matches!(tag, Tag::Phase(_)));
        // Add the new phase tag
        self.core.tags.push(Tag::Phase(new_phase));
        // Update timestamp
        self.core.metadata.updated_at = Utc::now();
    }

    /// Write the Task document to a file
    pub async fn to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), DocumentValidationError> {
        let content = self.to_content()?;
        std::fs::write(path.as_ref(), content).map_err(|e| {
            DocumentValidationError::InvalidContent(format!("Failed to write file: {}", e))
        })
    }

    /// Convert the Task document to its markdown string representation using templates
    pub fn to_content(&self) -> Result<String, DocumentValidationError> {
        let mut tera = Tera::default();

        // Add the frontmatter template to Tera
        tera.add_raw_template("frontmatter", self.frontmatter_template())
            .map_err(|e| {
                DocumentValidationError::InvalidContent(format!("Template error: {}", e))
            })?;

        // Create context with all document data
        let mut context = Context::new();
        context.insert("slug", &self.id().to_string());
        context.insert("title", self.title());
        context.insert("short_code", &self.metadata().short_code);
        context.insert("created_at", &self.metadata().created_at.to_rfc3339());
        context.insert("updated_at", &self.metadata().updated_at.to_rfc3339());
        context.insert("archived", &self.archived().to_string());
        context.insert(
            "exit_criteria_met",
            &self.metadata().exit_criteria_met.to_string(),
        );
        context.insert(
            "parent_id",
            &self
                .parent_id()
                .map(|id| id.to_string())
                .unwrap_or_default(),
        );
        let blocked_by_list: Vec<String> =
            self.blocked_by().iter().map(|id| id.to_string()).collect();
        context.insert("blocked_by", &blocked_by_list);

        // Convert tags to strings
        let tag_strings: Vec<String> = self.tags().iter().map(|tag| tag.to_str()).collect();
        context.insert("tags", &tag_strings);

        // Add lineage fields
        context.insert(
            "strategy_id",
            &self
                .core
                .strategy_id
                .as_ref()
                .map(|id| id.to_string())
                .unwrap_or_else(|| "NULL".to_string()),
        );
        context.insert(
            "initiative_id",
            &self
                .core
                .initiative_id
                .as_ref()
                .map(|id| id.to_string())
                .unwrap_or_else(|| "NULL".to_string()),
        );

        // Render frontmatter
        let frontmatter = tera.render("frontmatter", &context).map_err(|e| {
            DocumentValidationError::InvalidContent(format!("Frontmatter render error: {}", e))
        })?;

        // Use the actual content body
        let content_body = &self.content().body;

        // Use actual acceptance criteria if present, otherwise empty string
        let acceptance_criteria = if let Some(ac) = &self.content().acceptance_criteria {
            format!("\n\n## Acceptance Criteria\n\n{}", ac)
        } else {
            String::new()
        };

        // Combine everything
        Ok(format!(
            "---\n{}\n---\n\n{}{}",
            frontmatter.trim_end(),
            content_body,
            acceptance_criteria
        ))
    }
}

impl Document for Task {
    // id() uses default implementation from trait

    fn document_type(&self) -> DocumentType {
        DocumentType::Task
    }

    fn title(&self) -> &str {
        &self.core.title
    }

    fn metadata(&self) -> &DocumentMetadata {
        &self.core.metadata
    }

    fn content(&self) -> &DocumentContent {
        &self.core.content
    }

    fn core(&self) -> &super::traits::DocumentCore {
        &self.core
    }

    fn can_transition_to(&self, phase: Phase) -> bool {
        if let Ok(current_phase) = self.phase() {
            // Delegate to DocumentType - the single source of truth
            DocumentType::Task.can_transition(current_phase, phase)
        } else {
            false // Can't transition if we can't determine current phase
        }
    }

    fn parent_id(&self) -> Option<&DocumentId> {
        self.core.parent_id.as_ref()
    }

    fn blocked_by(&self) -> &[DocumentId] {
        &self.core.blocked_by
    }

    fn validate(&self) -> Result<(), DocumentValidationError> {
        // Task-specific validation rules
        if self.title().trim().is_empty() {
            return Err(DocumentValidationError::InvalidTitle(
                "Task title cannot be empty".to_string(),
            ));
        }

        // Tasks should have a parent (Initiative) unless they are in Backlog phase
        if self.parent_id().is_none() {
            // Allow no parent only if task is in Backlog phase
            if let Ok(phase) = self.phase() {
                if phase != Phase::Backlog {
                    return Err(DocumentValidationError::MissingRequiredField(
                        "Tasks should have a parent Initiative unless in Backlog phase".to_string(),
                    ));
                }
            } else {
                return Err(DocumentValidationError::MissingRequiredField(
                    "Tasks should have a parent Initiative".to_string(),
                ));
            }
        }

        // If blocked, must have blocking documents listed
        if let Ok(Phase::Blocked) = self.phase() {
            if self.blocked_by().is_empty() {
                return Err(DocumentValidationError::InvalidContent(
                    "Blocked tasks must specify what they are blocked by".to_string(),
                ));
            }
        }

        Ok(())
    }

    fn exit_criteria_met(&self) -> bool {
        // Check if all acceptance criteria checkboxes are checked
        // This would typically parse the content for checkbox completion
        // For now, return false as a placeholder
        false
    }

    fn template(&self) -> DocumentTemplate {
        DocumentTemplate {
            frontmatter: self.frontmatter_template(),
            content: self.content_template(),
            acceptance_criteria: self.acceptance_criteria_template(),
            file_extension: "md",
        }
    }

    fn frontmatter_template(&self) -> &'static str {
        include_str!("frontmatter.yaml")
    }

    fn content_template(&self) -> &'static str {
        include_str!("content.md")
    }

    fn acceptance_criteria_template(&self) -> &'static str {
        include_str!("acceptance_criteria.md")
    }

    fn transition_phase(
        &mut self,
        target_phase: Option<Phase>,
    ) -> Result<Phase, DocumentValidationError> {
        let current_phase = self.phase()?;

        let new_phase = match target_phase {
            Some(phase) => {
                // Validate the transition is allowed
                if !self.can_transition_to(phase) {
                    return Err(DocumentValidationError::InvalidPhaseTransition {
                        from: current_phase,
                        to: phase,
                    });
                }
                phase
            }
            None => {
                // Auto-transition to next phase in sequence
                match Self::next_phase_in_sequence(current_phase) {
                    Some(next) => next,
                    None => return Ok(current_phase), // Already at final phase or blocked
                }
            }
        };

        self.update_phase_tag(new_phase);
        Ok(new_phase)
    }

    fn core_mut(&mut self) -> &mut super::traits::DocumentCore {
        &mut self.core
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::domain::documents::traits::DocumentValidationError;
    use tempfile::tempdir;

    #[tokio::test]
    async fn test_task_from_content() {
        let content = r##"---
id: test-task
level: task
title: "Test Task"
created_at: 2025-01-01T00:00:00Z
updated_at: 2025-01-01T00:00:00Z
archived: false
parent: initiative-001
blocked_by: []
short_code: TEST-T-9001

tags:
  - "#task"
  - "#phase/todo"

exit_criteria_met: false
---

# Test Task

## Description

This is a test task for our system.

## Implementation Notes

Details on how to implement this.

## Acceptance Criteria

- [ ] Implementation is complete
- [ ] Tests pass
"##;

        let task = Task::from_content(content).unwrap();

        assert_eq!(task.title(), "Test Task");
        assert_eq!(task.document_type(), DocumentType::Task);
        assert!(!task.archived());
        assert_eq!(task.tags().len(), 2);
        assert_eq!(task.phase().unwrap(), Phase::Todo);
        assert!(task.content().has_acceptance_criteria());

        // Round-trip test: write to file and read back
        let temp_dir = tempdir().unwrap();
        let file_path = temp_dir.path().join("test-task.md");

        task.to_file(&file_path).await.unwrap();
        let loaded_task = Task::from_file(&file_path).await.unwrap();

        assert_eq!(loaded_task.title(), task.title());
        assert_eq!(loaded_task.phase().unwrap(), task.phase().unwrap());
        assert_eq!(loaded_task.content().body, task.content().body);
        assert_eq!(loaded_task.archived(), task.archived());
        assert_eq!(loaded_task.tags().len(), task.tags().len());
    }

    #[test]
    fn test_task_invalid_level() {
        let content = r##"---
id: test-doc
level: strategy
title: "Test Strategy"
created_at: 2025-01-01T00:00:00Z
updated_at: 2025-01-01T00:00:00Z
archived: false
tags:
  - "#strategy"
  - "#phase/shaping"
exit_criteria_met: false
---

# Test Strategy
"##;

        let result = Task::from_content(content);
        assert!(result.is_err());
        match result.unwrap_err() {
            DocumentValidationError::InvalidContent(msg) => {
                assert!(msg.contains("Expected level 'task'"));
            }
            _ => panic!("Expected InvalidContent error"),
        }
    }

    #[test]
    fn test_task_validation() {
        let task = Task::new(
            "Test Task".to_string(),
            Some(DocumentId::from("parent-initiative")), // parent_id
            Some("Parent Initiative".to_string()),       // parent_title
            Some(DocumentId::from("parent-strategy")),   // strategy_id
            Some(DocumentId::from("parent-initiative")), // initiative_id
            vec![],                                      // blocked_by
            vec![Tag::Label("task".to_string()), Tag::Phase(Phase::Todo)],
            false,
            "TEST-T-0401".to_string(),
        )
        .expect("Failed to create task");

        assert!(task.validate().is_ok());

        // Test validation failure - no parent
        let task_no_parent = Task::new(
            "Test Task".to_string(),
            None,   // No parent
            None,   // No parent title
            None,   // No strategy
            None,   // No initiative
            vec![], // blocked_by
            vec![Tag::Phase(Phase::Todo)],
            false,
            "TEST-T-0401".to_string(),
        )
        .expect("Failed to create task");

        assert!(task_no_parent.validate().is_err());
    }

    #[test]
    fn test_task_blocked_validation() {
        // Task marked as blocked but no blocking documents
        let blocked_task = Task::new(
            "Blocked Task".to_string(),
            Some(DocumentId::from("parent-initiative")), // parent_id
            Some("Parent Initiative".to_string()),       // parent_title
            Some(DocumentId::from("parent-strategy")),   // strategy_id
            Some(DocumentId::from("parent-initiative")), // initiative_id
            vec![],                                      // No blocking documents
            vec![Tag::Phase(Phase::Blocked)],
            false,
            "TEST-T-0401".to_string(),
        )
        .expect("Failed to create task");

        assert!(blocked_task.validate().is_err());

        // Task marked as blocked with blocking documents
        let properly_blocked_task = Task::new(
            "Blocked Task".to_string(),
            Some(DocumentId::from("parent-initiative")), // parent_id
            Some("Parent Initiative".to_string()),       // parent_title
            Some(DocumentId::from("parent-strategy")),   // strategy_id
            Some(DocumentId::from("parent-initiative")), // initiative_id
            vec![DocumentId::from("blocking-task")],
            vec![Tag::Phase(Phase::Blocked)],
            false,
            "TEST-T-0401".to_string(),
        )
        .expect("Failed to create task");

        assert!(properly_blocked_task.validate().is_ok());
    }

    #[test]
    fn test_task_phase_transitions() {
        let task = Task::new(
            "Test Task".to_string(),
            Some(DocumentId::from("parent-initiative")), // parent_id
            Some("Parent Initiative".to_string()),       // parent_title
            Some(DocumentId::from("parent-strategy")),   // strategy_id
            Some(DocumentId::from("parent-initiative")), // initiative_id
            vec![],
            vec![Tag::Phase(Phase::Todo)],
            false,
            "TEST-T-0401".to_string(),
        )
        .expect("Failed to create task");

        assert!(task.can_transition_to(Phase::Active));
        assert!(task.can_transition_to(Phase::Blocked));
        assert!(!task.can_transition_to(Phase::Completed));
        assert!(!task.can_transition_to(Phase::Design));
    }

    #[test]
    fn test_task_active_phase_transitions() {
        let active_task = Task::new(
            "Active Task".to_string(),
            Some(DocumentId::from("parent-initiative")), // parent_id
            Some("Parent Initiative".to_string()),       // parent_title
            Some(DocumentId::from("parent-strategy")),   // strategy_id
            Some(DocumentId::from("parent-initiative")), // initiative_id
            vec![],
            vec![Tag::Phase(Phase::Active)],
            false,
            "TEST-T-0401".to_string(),
        )
        .expect("Failed to create task");

        assert!(active_task.can_transition_to(Phase::Completed));
        assert!(active_task.can_transition_to(Phase::Blocked));
        assert!(!active_task.can_transition_to(Phase::Todo));
    }

    #[test]
    fn test_task_blocked_phase_transitions() {
        let blocked_task = Task::new(
            "Blocked Task".to_string(),
            Some(DocumentId::from("parent-initiative")), // parent_id
            Some("Parent Initiative".to_string()),       // parent_title
            Some(DocumentId::from("parent-strategy")),   // strategy_id
            Some(DocumentId::from("parent-initiative")), // initiative_id
            vec![DocumentId::from("blocking-task")],
            vec![Tag::Phase(Phase::Blocked)],
            false,
            "TEST-T-0401".to_string(),
        )
        .expect("Failed to create task");

        assert!(blocked_task.can_transition_to(Phase::Active));
        assert!(blocked_task.can_transition_to(Phase::Todo));
        assert!(!blocked_task.can_transition_to(Phase::Completed));
    }

    #[test]
    fn test_task_transition_phase_auto() {
        let mut task = Task::new(
            "Test Task".to_string(),
            Some(DocumentId::from("parent-initiative")), // parent_id
            Some("Parent Initiative".to_string()),       // parent_title
            Some(DocumentId::from("parent-strategy")),   // strategy_id
            Some(DocumentId::from("parent-initiative")), // initiative_id
            vec![],
            vec![Tag::Phase(Phase::Todo)],
            false,
            "TEST-T-0401".to_string(),
        )
        .expect("Failed to create task");

        // Auto-transition from Todo should go to Active
        let new_phase = task.transition_phase(None).unwrap();
        assert_eq!(new_phase, Phase::Active);
        assert_eq!(task.phase().unwrap(), Phase::Active);

        // Auto-transition from Active should go to Completed
        let new_phase = task.transition_phase(None).unwrap();
        assert_eq!(new_phase, Phase::Completed);
        assert_eq!(task.phase().unwrap(), Phase::Completed);

        // Auto-transition from Completed should stay at Completed (final phase)
        let new_phase = task.transition_phase(None).unwrap();
        assert_eq!(new_phase, Phase::Completed);
        assert_eq!(task.phase().unwrap(), Phase::Completed);
    }

    #[test]
    fn test_task_transition_phase_blocking() {
        let mut task = Task::new(
            "Test Task".to_string(),
            Some(DocumentId::from("parent-initiative")), // parent_id
            Some("Parent Initiative".to_string()),       // parent_title
            Some(DocumentId::from("parent-strategy")),   // strategy_id
            Some(DocumentId::from("parent-initiative")), // initiative_id
            vec![DocumentId::from("blocking-task")],
            vec![Tag::Phase(Phase::Todo)],
            false,
            "TEST-T-0401".to_string(),
        )
        .expect("Failed to create task");

        // Explicit transition from Todo to Blocked
        let new_phase = task.transition_phase(Some(Phase::Blocked)).unwrap();
        assert_eq!(new_phase, Phase::Blocked);
        assert_eq!(task.phase().unwrap(), Phase::Blocked);

        // Transition from Blocked back to Active (unblocking)
        let new_phase = task.transition_phase(Some(Phase::Active)).unwrap();
        assert_eq!(new_phase, Phase::Active);
        assert_eq!(task.phase().unwrap(), Phase::Active);

        // Blocked doesn't auto-transition
        task.core.tags.retain(|tag| !matches!(tag, Tag::Phase(_)));
        task.core.tags.push(Tag::Phase(Phase::Blocked));
        let new_phase = task.transition_phase(None).unwrap();
        assert_eq!(new_phase, Phase::Blocked); // Should stay blocked
    }

    #[test]
    fn test_task_transition_phase_invalid() {
        let mut task = Task::new(
            "Test Task".to_string(),
            Some(DocumentId::from("parent-initiative")), // parent_id
            Some("Parent Initiative".to_string()),       // parent_title
            Some(DocumentId::from("parent-strategy")),   // strategy_id
            Some(DocumentId::from("parent-initiative")), // initiative_id
            vec![],
            vec![Tag::Phase(Phase::Todo)],
            false,
            "TEST-T-0401".to_string(),
        )
        .expect("Failed to create task");

        // Invalid transition from Todo to Completed (must go through Active)
        let result = task.transition_phase(Some(Phase::Completed));
        assert!(result.is_err());
        match result.unwrap_err() {
            DocumentValidationError::InvalidPhaseTransition { from, to } => {
                assert_eq!(from, Phase::Todo);
                assert_eq!(to, Phase::Completed);
            }
            _ => panic!("Expected InvalidPhaseTransition error"),
        }

        // Should still be in Todo phase
        assert_eq!(task.phase().unwrap(), Phase::Todo);
    }

    #[test]
    fn test_task_update_section() {
        // First create a task with the template
        let mut task = Task::new(
            "Test Task".to_string(),
            Some(DocumentId::from("parent-initiative")), // parent_id
            Some("Parent Initiative".to_string()),       // parent_title
            Some(DocumentId::from("parent-strategy")),   // strategy_id
            Some(DocumentId::from("parent-initiative")), // initiative_id
            vec![],
            vec![Tag::Phase(Phase::Todo)],
            false,
            "TEST-T-0401".to_string(),
        )
        .expect("Failed to create task");

        // Then update its content to have specific test content
        task.core_mut().content = DocumentContent::new(
            "## Description\n\nOriginal description\n\n## Implementation Notes\n\nOriginal notes",
        );

        // Replace existing section
        task.update_section("Updated task description", "Description", false)
            .unwrap();
        let content = task.content().body.clone();
        assert!(content.contains("## Description\n\nUpdated task description"));
        assert!(!content.contains("Original description"));

        // Append to existing section
        task.update_section(
            "Additional implementation details",
            "Implementation Notes",
            true,
        )
        .unwrap();
        let content = task.content().body.clone();
        assert!(content.contains("Original notes"));
        assert!(content.contains("Additional implementation details"));

        // Add new section
        task.update_section("Test approach details", "Testing Strategy", false)
            .unwrap();
        let content = task.content().body.clone();
        assert!(content.contains("## Testing Strategy\n\nTest approach details"));
    }
}