ggen-domain 5.1.3

Domain logic layer for ggen - pure business logic without CLI dependencies
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
//! Domain logic for template rendering with RDF integration
//!
//! This module provides the v2 API for rendering templates with RDF/SPARQL support,
//! maintaining backward compatibility with v1 template rendering.

use ggen_core::{Graph, Template};
use ggen_utils::error::Result;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use tera::Context;

/// Options for rendering templates with RDF support
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RenderWithRdfOptions {
    /// Path to the template file
    pub template_path: PathBuf,

    /// RDF/TTL files to load for context
    pub rdf_files: Vec<PathBuf>,

    /// Output path for the rendered template
    pub output_path: PathBuf,

    /// Additional variables for template context
    pub variables: BTreeMap<String, String>,

    /// Force overwrite existing output
    pub force_overwrite: bool,

    /// Use preprocessor for template processing
    pub use_preprocessor: bool,
}

impl RenderWithRdfOptions {
    pub fn new(template_path: PathBuf, output_path: PathBuf) -> Self {
        Self {
            template_path,
            output_path,
            rdf_files: Vec::new(),
            variables: BTreeMap::new(),
            force_overwrite: false,
            use_preprocessor: false,
        }
    }

    pub fn with_rdf_file(mut self, rdf_file: PathBuf) -> Self {
        self.rdf_files.push(rdf_file);
        self
    }

    pub fn with_rdf_files(mut self, rdf_files: Vec<PathBuf>) -> Self {
        self.rdf_files.extend(rdf_files);
        self
    }

    pub fn with_var(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.variables.insert(key.into(), value.into());
        self
    }

    pub fn with_vars(mut self, vars: BTreeMap<String, String>) -> Self {
        self.variables.extend(vars);
        self
    }

    pub fn force(mut self) -> Self {
        self.force_overwrite = true;
        self
    }

    pub fn with_preprocessor(mut self, enabled: bool) -> Self {
        self.use_preprocessor = enabled;
        self
    }
}

/// Result of template rendering with RDF
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RenderWithRdfResult {
    /// Path to the generated output file (or directory if multi-file)
    pub output_path: PathBuf,

    /// Number of bytes written (total if multi-file)
    pub bytes_written: usize,

    /// Template path used
    pub template_path: PathBuf,

    /// Number of RDF files loaded
    pub rdf_files_loaded: usize,

    /// Number of SPARQL queries executed
    pub sparql_queries_executed: usize,

    /// Variables used in rendering
    pub variables_used: usize,

    /// Number of files created (for multi-file generation)
    pub files_created: usize,
}

/// Render a template with RDF data integration
///
/// This is the v2 API that integrates RDF/SPARQL with template rendering.
/// It maintains backward compatibility - if no RDF files are provided,
/// it behaves like the v1 API.
pub fn render_with_rdf(options: &RenderWithRdfOptions) -> Result<RenderWithRdfResult> {
    // Validate template exists
    if !options.template_path.exists() {
        return Err(ggen_utils::error::Error::new(&format!(
            "Template not found: {}",
            options.template_path.display()
        )));
    }

    // Check if output exists and we're not forcing
    // For multi-file output, we check the base directory, not the specific file
    // (File marker detection happens after rendering, so we can't check individual files yet)
    if options.output_path.exists() && !options.force_overwrite {
        return Err(ggen_utils::error::Error::new(&format!(
            "Output file already exists: {}. Use force_overwrite to overwrite.",
            options.output_path.display()
        )));
    }

    // Prepare output directory
    if let Some(parent) = options.output_path.parent() {
        std::fs::create_dir_all(parent).map_err(|e| {
            ggen_utils::error::Error::new(&format!("Failed to create output directory: {}", e))
        })?;
    }

    // Read template content
    let template_content = std::fs::read_to_string(&options.template_path)
        .map_err(|e| ggen_utils::error::Error::new(&format!("Failed to read template: {}", e)))?;

    // Parse template with or without preprocessor
    let output_dir = options
        .output_path
        .parent()
        .unwrap_or_else(|| Path::new("."));

    let mut template = if options.use_preprocessor {
        Template::parse_with_preprocessor(&template_content, &options.template_path, output_dir)
            .map_err(|e| {
                ggen_utils::error::Error::new(&format!("Failed to parse template: {}", e))
            })?
    } else {
        Template::parse(&template_content).map_err(|e| {
            ggen_utils::error::Error::new(&format!("Failed to parse template: {}", e))
        })?
    };

    // Create Tera instance with filters
    let mut tera = ggen_core::tera_env::build_tera_minimal().map_err(|e| {
        ggen_utils::error::Error::new(&format!("Failed to create Tera instance: {}", e))
    })?;

    // Build context from variables
    let mut context = Context::new();
    for (key, value) in &options.variables {
        context.insert(key, value);
    }

    // Create RDF graph
    let mut graph = Graph::new().map_err(|e| {
        ggen_utils::error::Error::new(&format!("Failed to create RDF graph: {}", e))
    })?;

    // Render frontmatter first - this populates template.front from raw_frontmatter
    template
        .render_frontmatter(&mut tera, &context)
        .map_err(|e| {
            ggen_utils::error::Error::new(&format!("Failed to render frontmatter: {}", e))
        })?;

    // Count SPARQL queries from frontmatter AFTER render_frontmatter populates it
    let sparql_count = template.front.sparql.len();

    // Determine base output directory for resolving frontmatter `to` paths
    let base_output_dir = if options.output_path.is_dir() {
        options.output_path.clone()
    } else {
        options
            .output_path
            .parent()
            .map(|p| p.to_path_buf())
            .unwrap_or_else(|| {
                std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."))
            })
    };

    // Resolve final output path from frontmatter `to` if present
    // This honors the template's declared output path (e.g., `to: "ggen.toml"`)
    let resolved_output_path = if let Some(to_path) = &template.front.to {
        // Render the `to` path through Tera to resolve any template variables
        let rendered_to = tera.render_str(to_path, &context).map_err(|e| {
            ggen_utils::error::Error::new(&format!(
                "Failed to render frontmatter 'to' path '{}': {}",
                to_path, e
            ))
        })?;

        // Join with base output directory
        let joined_path = base_output_dir.join(&rendered_to);

        // Security: Prevent path traversal attacks by ensuring the resolved path
        // stays within base_output_dir. This prevents paths like "../../../etc/passwd"
        // from escaping the output directory.
        // We normalize the path and check that all components stay within base_output_dir
        let normalized = joined_path.components().collect::<Vec<_>>();
        let base_components = base_output_dir.components().collect::<Vec<_>>();

        // Check that normalized path starts with base components
        if normalized.len() < base_components.len() {
            return Err(ggen_utils::error::Error::new(&format!(
                "Output path '{}' would escape output root '{}'",
                rendered_to,
                base_output_dir.display()
            )));
        }

        for (i, component) in base_components.iter().enumerate() {
            if normalized.get(i) != Some(component) {
                return Err(ggen_utils::error::Error::new(&format!(
                    "Output path '{}' would escape output root '{}'",
                    rendered_to,
                    base_output_dir.display()
                )));
            }
        }

        joined_path
    } else {
        // No frontmatter `to` - use the provided output_path
        options.output_path.clone()
    };

    // Re-check overwrite guard for resolved path (if different from original)
    if resolved_output_path != options.output_path
        && resolved_output_path.exists()
        && !options.force_overwrite
    {
        return Err(ggen_utils::error::Error::new(&format!(
            "Output file already exists: {}. Use force_overwrite to overwrite.",
            resolved_output_path.display()
        )));
    }

    // Render using v2 RDF integration
    // If RDF files provided via CLI, use those (take precedence)
    // Otherwise, use frontmatter rdf: field (filesystem-routed)
    let rendered_content = if !options.rdf_files.is_empty() {
        // Use CLI-provided RDF files (explicit)
        template
            .render_with_rdf(
                options.rdf_files.clone(),
                &mut graph,
                &mut tera,
                &context,
                &options.template_path,
            )
            .map_err(|e| {
                ggen_utils::error::Error::new(&format!("Template rendering failed: {}", e))
            })?
    } else if !template.front.rdf.is_empty() {
        // Use frontmatter rdf: field (filesystem-routed)
        // Process graph will load RDF files from frontmatter
        template
            .process_graph(&mut graph, &mut tera, &context, &options.template_path)
            .map_err(|e| {
                ggen_utils::error::Error::new(&format!("Failed to process graph: {}", e))
            })?;

        // Render body with SPARQL results available
        template.render(&mut tera, &context).map_err(|e| {
            ggen_utils::error::Error::new(&format!("Template rendering failed: {}", e))
        })?
    } else {
        // No RDF files - backward compatible v1 API
        template
            .process_graph(&mut graph, &mut tera, &context, &options.template_path)
            .map_err(|e| {
                ggen_utils::error::Error::new(&format!("Failed to process graph: {}", e))
            })?;

        template.render(&mut tera, &context).map_err(|e| {
            ggen_utils::error::Error::new(&format!("Template rendering failed: {}", e))
        })?
    };

    // Check for file markers and split if present
    let (files_created, total_bytes, output_path) = if rendered_content.contains("{# FILE:") {
        // Multi-file generation mode
        // Use base_output_dir for file markers
        // File markers contain paths relative to project root (e.g., "crates/clnrm-v2-generated/...")
        let files = split_file_markers(&rendered_content, &base_output_dir)?;

        for (file_path, content) in &files {
            if let Some(parent) = file_path.parent() {
                std::fs::create_dir_all(parent).map_err(|e| {
                    ggen_utils::error::Error::new(&format!(
                        "Failed to create output directory for {}: {}",
                        file_path.display(),
                        e
                    ))
                })?;
            }

            std::fs::write(file_path, content).map_err(|e| {
                ggen_utils::error::Error::new(&format!(
                    "Failed to write file {}: {}",
                    file_path.display(),
                    e
                ))
            })?;
        }

        let total_bytes: usize = files.iter().map(|(_, content)| content.len()).sum();
        (files.len(), total_bytes, base_output_dir.to_path_buf())
    } else {
        // Single file output - use resolved path (honors frontmatter `to`)
        // Ensure parent directory exists
        if let Some(parent) = resolved_output_path.parent() {
            std::fs::create_dir_all(parent).map_err(|e| {
                ggen_utils::error::Error::new(&format!(
                    "Failed to create output directory for {}: {}",
                    resolved_output_path.display(),
                    e
                ))
            })?;
        }

        std::fs::write(&resolved_output_path, &rendered_content).map_err(|e| {
            ggen_utils::error::Error::new(&format!(
                "Failed to write output to {}: {}",
                resolved_output_path.display(),
                e
            ))
        })?;

        (1, rendered_content.len(), resolved_output_path)
    };

    // Count RDF files loaded (from CLI args or frontmatter)
    let rdf_files_loaded = if !options.rdf_files.is_empty() {
        // RDF files provided via CLI/API
        options.rdf_files.len()
    } else if !template.front.rdf.is_empty() {
        // RDF files loaded from frontmatter (filesystem-routed)
        template.front.rdf.len()
    } else {
        // No RDF files
        0
    };

    Ok(RenderWithRdfResult {
        output_path,
        bytes_written: total_bytes,
        template_path: options.template_path.clone(),
        rdf_files_loaded,
        sparql_queries_executed: sparql_count,
        variables_used: options.variables.len(),
        files_created,
    })
}

/// Generate a template from RDF metadata
///
/// This function takes RDF/TTL files describing template metadata
/// and generates a template file from them.
pub fn generate_from_rdf(
    rdf_files: Vec<PathBuf>, output_template_path: PathBuf,
) -> Result<PathBuf> {
    // Create RDF graph
    let graph = Graph::new().map_err(|e| {
        ggen_utils::error::Error::new(&format!("Failed to create RDF graph: {}", e))
    })?;

    // Load RDF files
    for rdf_file in &rdf_files {
        let ttl_content = std::fs::read_to_string(rdf_file).map_err(|e| {
            ggen_utils::error::Error::new(&format!(
                "Failed to read RDF file '{}': {}",
                rdf_file.display(),
                e
            ))
        })?;

        graph
            .insert_turtle(&ttl_content)
            .map_err(|e| ggen_utils::error::Error::new(&format!("Failed to parse RDF: {}", e)))?;
    }

    // Query for template metadata
    let query = r#"
        PREFIX ggen: <http://ggen.dev/ontology#>
        SELECT ?template ?name ?description ?to
        WHERE {
            ?template a ggen:Template ;
                ggen:templateName ?name .
            OPTIONAL { ?template ggen:templateDescription ?description }
            OPTIONAL { ?template ggen:outputPath ?to }
        }
        LIMIT 1
    "#;

    let results = graph
        .query_cached(query)
        .map_err(|e| ggen_utils::error::Error::new(&format!("SPARQL query failed: {}", e)))?;

    // Extract template metadata
    let (name, description, to) = match results {
        ggen_core::graph::CachedResult::Solutions(rows) if !rows.is_empty() => {
            let row = &rows[0];
            let name = row
                .get("name")
                .map(|s| s.trim_matches('"').to_string())
                .unwrap_or_else(|| "Generated Template".to_string());
            let description = row
                .get("description")
                .map(|s| s.trim_matches('"').to_string());
            let to = row.get("to").map(|s| s.trim_matches('"').to_string());
            (name, description, to)
        }
        _ => {
            return Err(ggen_utils::error::Error::new(
                "No template metadata found in RDF files",
            ))
        }
    };

    // Generate template content
    let mut template_content = String::from("---\n");

    if let Some(to_path) = to {
        template_content.push_str(&format!("to: \"{}\"\n", to_path));
    }

    template_content.push_str("---\n");

    if let Some(desc) = description {
        template_content.push_str(&format!("# {}\n", desc));
    }

    template_content.push_str(&format!("# Template: {}\n", name));
    template_content.push_str("# Generated from RDF metadata\n\n");
    template_content.push_str("Hello from {{ name }}!\n");

    // Write template file
    std::fs::write(&output_template_path, template_content)
        .map_err(|e| ggen_utils::error::Error::new(&format!("Failed to write template: {}", e)))?;

    Ok(output_template_path)
}

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

    #[test]
    fn test_render_with_rdf_options_builder() {
        let options =
            RenderWithRdfOptions::new(PathBuf::from("template.tmpl"), PathBuf::from("output.txt"))
                .with_rdf_file(PathBuf::from("data.ttl"))
                .with_var("name", "test")
                .force()
                .with_preprocessor(true);

        assert_eq!(options.rdf_files.len(), 1);
        assert_eq!(options.variables.len(), 1);
        assert!(options.force_overwrite);
        assert!(options.use_preprocessor);
    }

    #[test]
    fn test_render_with_rdf_backward_compatible() {
        let temp_dir = TempDir::new().unwrap();
        let template_path = temp_dir.path().join("template.tmpl");
        let output_path = temp_dir.path().join("output.txt");

        // Create a simple v1-style template (no RDF)
        fs::write(
            &template_path,
            r#"---
to: "output.txt"
---
Hello, {{ name }}!"#,
        )
        .unwrap();

        let options =
            RenderWithRdfOptions::new(template_path, output_path.clone()).with_var("name", "World");

        let result = render_with_rdf(&options).unwrap();

        assert_eq!(result.output_path, output_path);
        assert_eq!(result.rdf_files_loaded, 0);
        assert!(result.bytes_written > 0);

        let content = fs::read_to_string(&output_path).unwrap();
        assert!(content.contains("Hello, World!"));
    }

    #[test]
    fn test_render_with_inline_rdf() {
        let temp_dir = TempDir::new().unwrap();
        let template_path = temp_dir.path().join("template.tmpl");
        let output_path = temp_dir.path().join("output.txt");

        // Create template with inline RDF
        fs::write(
            &template_path,
            r#"---
to: "output.txt"
prefixes: { ex: "http://example.org/" }
rdf_inline:
  - "@prefix ex: <http://example.org/> . ex:Alice a ex:Person ."
sparql:
  people: "SELECT ?person WHERE { ?person a ex:Person }"
---
Found {{ sparql_results.people | length }} person(s)"#,
        )
        .unwrap();

        let options = RenderWithRdfOptions::new(template_path, output_path.clone());

        let result = render_with_rdf(&options).unwrap();

        // Verify output was written
        assert!(result.bytes_written > 0);

        // Verify the SPARQL query was executed by checking output content
        let content = fs::read_to_string(&output_path).unwrap();
        assert!(
            content.contains("Found 1 person(s)"),
            "Expected output to contain 'Found 1 person(s)' but got: {}",
            content
        );
    }

    #[test]
    fn test_render_template_not_found() {
        let options = RenderWithRdfOptions::new(
            PathBuf::from("/nonexistent/template.tmpl"),
            PathBuf::from("output.txt"),
        );

        let result = render_with_rdf(&options);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Template not found"));
    }

    #[test]
    fn test_render_prevents_overwrite_without_force() {
        let temp_dir = TempDir::new().unwrap();
        let template_path = temp_dir.path().join("template.tmpl");
        let output_path = temp_dir.path().join("output.txt");

        fs::write(&template_path, "---\nto: \"output.txt\"\n---\nContent").unwrap();
        fs::write(&output_path, "Existing").unwrap();

        let options = RenderWithRdfOptions::new(template_path, output_path);

        let result = render_with_rdf(&options);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("already exists"));
    }

    #[test]
    fn test_render_with_preprocessor() {
        let temp_dir = TempDir::new().unwrap();
        let template_path = temp_dir.path().join("template.tmpl");
        let output_path = temp_dir.path().join("output.txt");

        fs::write(
            &template_path,
            r#"---
to: "output.txt"
---
Hello {{ name }}!"#,
        )
        .unwrap();

        let options = RenderWithRdfOptions::new(template_path, output_path.clone())
            .with_var("name", "Preprocessor")
            .with_preprocessor(true);

        let result = render_with_rdf(&options);
        assert!(result.is_ok());
    }

    #[test]
    fn test_generate_from_rdf() {
        let temp_dir = TempDir::new().unwrap();
        let rdf_file = temp_dir.path().join("metadata.ttl");
        let output_template = temp_dir.path().join("generated.tmpl");

        // Create RDF metadata
        let rdf_content = r#"
@prefix ggen: <http://ggen.dev/ontology#> .

<http://example.org/template1> a ggen:Template ;
    ggen:templateName "My Template" ;
    ggen:templateDescription "A generated template" ;
    ggen:outputPath "output.txt" .
"#;
        fs::write(&rdf_file, rdf_content).unwrap();

        let result = generate_from_rdf(vec![rdf_file], output_template.clone());
        assert!(result.is_ok());
        assert!(output_template.exists());

        let content = fs::read_to_string(&output_template).unwrap();
        assert!(content.contains("to: \"output.txt\""));
        assert!(content.contains("# A generated template"));
        assert!(content.contains("# Template: My Template"));
    }

    #[test]
    fn test_split_file_markers() {
        let content = r#"Some content before
{# FILE: file1.txt #}
Content for file 1
Line 2

{# FILE: subdir/file2.txt #}
Content for file 2
"#;
        let base_dir = std::path::Path::new("/tmp/test");
        let files = split_file_markers(content, base_dir).unwrap();

        assert_eq!(files.len(), 2);
        assert_eq!(files[0].0, base_dir.join("file1.txt"));
        assert_eq!(files[0].1.trim(), "Content for file 1\nLine 2");
        assert_eq!(files[1].0, base_dir.join("subdir/file2.txt"));
        assert_eq!(files[1].1.trim(), "Content for file 2");
    }

    #[test]
    fn test_render_honors_frontmatter_to_for_ggen_toml() {
        // Regression test: Verify that frontmatter `to: "ggen.toml"` is honored
        // even when a placeholder output path is provided (CLI-style usage)
        let temp_dir = TempDir::new().unwrap();
        let template_path = temp_dir.path().join("ggen.toml.tmpl");
        let placeholder_output = temp_dir.path().join("placeholder");

        // Create template with frontmatter `to: "ggen.toml"` and template variables
        fs::write(
            &template_path,
            r#"---
to: "ggen.toml"
---
[project]
name = "{{ project_name }}"
version = "{{ project_version }}"
description = "{{ project_description }}"
"#,
        )
        .unwrap();

        // Use placeholder output path (simulating CLI usage)
        let options = RenderWithRdfOptions::new(template_path, placeholder_output.clone())
            .with_var("project_name", "test-project")
            .with_var("project_version", "1.0.0")
            .with_var("project_description", "A test project");

        let result = render_with_rdf(&options).unwrap();

        // Verify file was written to resolved location (not placeholder)
        let resolved_path = temp_dir.path().join("ggen.toml");
        assert!(
            resolved_path.exists(),
            "File should be written to resolved path: {}",
            resolved_path.display()
        );
        assert_eq!(
            result.output_path, resolved_path,
            "Result should report resolved path"
        );

        // Verify placeholder was NOT created
        assert!(
            !placeholder_output.exists(),
            "Placeholder path should not exist: {}",
            placeholder_output.display()
        );

        // Verify content was rendered correctly with variables
        let content = fs::read_to_string(&resolved_path).unwrap();
        assert!(
            content.contains("name = \"test-project\""),
            "Content should contain rendered project_name"
        );
        assert!(
            content.contains("version = \"1.0.0\""),
            "Content should contain rendered project_version"
        );
        assert!(
            content.contains("description = \"A test project\""),
            "Content should contain rendered project_description"
        );
        assert!(
            content.contains("[project]"),
            "Content should contain project section"
        );

        // Verify bytes written
        assert!(result.bytes_written > 0, "Should report bytes written");
        assert_eq!(result.files_created, 1, "Should report 1 file created");
    }
}

/// Split rendered content by file markers
///
/// Parses `{# FILE: path/to/file.ext #}` markers and returns a vector of
/// (file_path, content) tuples. Content before the first marker is discarded.
fn split_file_markers(content: &str, base_dir: &Path) -> Result<Vec<(PathBuf, String)>> {
    let file_marker_re = Regex::new(r"\{#\s*FILE:\s*([^\s#]+)\s*#\}").map_err(|e| {
        ggen_utils::error::Error::new(&format!("Failed to compile file marker regex: {}", e))
    })?;

    let mut files = Vec::new();
    let mut current_file: Option<PathBuf> = None;
    let mut current_content = String::new();

    for line in content.lines() {
        if let Some(captures) = file_marker_re.captures(line) {
            // Write previous file if exists
            if let Some(path) = current_file.take() {
                files.push((path, current_content.trim_end().to_string()));
            }

            // Start new file
            let file_path = captures.get(1).unwrap().as_str();
            let full_path = base_dir.join(file_path);
            current_file = Some(full_path);
            current_content.clear();
        } else if current_file.is_some() {
            // Accumulate content for current file
            current_content.push_str(line);
            current_content.push('\n');
        }
        // Content before first marker is ignored
    }

    // Write last file if exists
    if let Some(path) = current_file {
        files.push((path, current_content.trim_end().to_string()));
    }

    Ok(files)
}