zebflow 0.7.9

Reactive web automation platform — pipelines, templates, and real-time collaboration in a single binary.
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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
//! `n.web.static.generate` — render a TSX page once and persist it to Zebflow FS.
//!
//! This node is the file-producing counterpart to [`super::web_response`]:
//! it uses the same RWE compile/render path, but instead of returning the HTML
//! to the current HTTP request it writes the rendered document into
//! a Zebflow FS object path.
//!
//! The generated HTML is self-contained for the first release:
//! - project `styles/main.css` is inlined when present
//! - Tailwind CSS extracted by the RWE engine is inlined
//! - compiled client scripts are inlined as `<script type="module">`
//!
//! That keeps the artifact self-contained without depending on render-script
//! cache plumbing.

use std::path::Path;

use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

use crate::pipeline::PipelineError;
use crate::pipeline::model::{
    DslFlag, DslFlagKind, LayoutItem, NodeDefinition, NodeFieldDef, NodeFieldType, SelectOptionDef,
};
use crate::pipeline::nodes::basic::web_static_site;
use crate::rwe::{CompiledScript, TemplateSource};

pub const NODE_KIND: &str = "n.web.static.generate";
pub const INPUT_PIN_IN: &str = "in";
pub const OUTPUT_PIN_OUT: &str = "out";

fn default_on_conflict() -> String {
    "overwrite".to_string()
}

/// Typed configuration for `n.web.static.generate`.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Config {
    /// Relative TSX template path under `repo/pipelines`.
    /// Example: `pages/song.tsx`.
    pub template: String,
    /// Inline template markup hydrated by higher layers.
    ///
    /// This is intentionally hidden from normal authors and only exists so
    /// compiled graphs can carry already-loaded markup if desired.
    #[serde(default)]
    pub markup: Option<String>,
    /// Zebflow FS object path.
    ///
    /// Config expressions are resolved before execution, so values like
    /// `artists/{{ $input.artist_slug }}/{{ $input.song_slug }}/lyric.html`
    /// are supported without node-specific syntax.
    pub output_path: String,
    /// Optional static site root under Zebflow FS.
    ///
    /// When set, `output_path` is resolved inside this site root so multiple
    /// generated pages can contribute to one coherent site tree.
    #[serde(default)]
    pub site_root: Option<String>,
    /// Optional absolute deployed site origin used for canonical/meta generation in templates.
    #[serde(default, alias = "base_url")]
    pub deploy_base_url: Option<String>,
    /// Optional deployed URL base path used to derive ctx.route for generated pages.
    #[serde(default, alias = "base_path")]
    pub deploy_base_path: Option<String>,
    /// Optional route injected into the RWE render context as `ctx.route`.
    ///
    /// Defaults to `/fs/{owner}/{project}/{output_path}`.
    #[serde(default)]
    pub route: Option<String>,
    /// `overwrite`, `skip`, or `error` when the file already exists and content differs.
    #[serde(default = "default_on_conflict")]
    pub on_conflict: String,
}

/// Resolve the template file and load its markup from disk when `markup` was not
/// injected beforehand.
pub fn resolve_template_source(
    node_id: &str,
    config: &Config,
    template_root: Option<&Path>,
) -> Result<TemplateSource, PipelineError> {
    let template_rel = normalize_template_rel_path(&config.template)?;
    let markup = if let Some(markup) = config
        .markup
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
    {
        markup.to_string()
    } else {
        let Some(root) = template_root else {
            return Err(PipelineError::new(
                "WEB_STATIC_TEMPLATE_ROOT",
                format!(
                    "node '{node_id}' requires template_root to load '{}'",
                    template_rel
                ),
            ));
        };
        let abs = root.join(&template_rel);
        if !abs.starts_with(root) || !abs.is_file() {
            return Err(PipelineError::new(
                "WEB_STATIC_TEMPLATE_MISSING",
                format!("node '{node_id}' template '{}' not found", template_rel),
            ));
        }
        std::fs::read_to_string(&abs).map_err(|err| {
            PipelineError::new(
                "WEB_STATIC_TEMPLATE_READ",
                format!("failed reading template '{}': {err}", template_rel),
            )
        })?
    };

    let source_path = template_root.map(|root| root.join(&template_rel));
    Ok(TemplateSource {
        id: template_rel.clone(),
        source_path,
        markup,
    })
}

/// Returns an output path relative to the project Zebflow FS root.
pub fn normalize_output_rel_path(output_path: &str) -> Result<String, PipelineError> {
    let mut parts = Vec::new();
    for part in output_path.trim().replace('\\', "/").split('/') {
        let part = part.trim();
        if part.is_empty() || part == "." {
            continue;
        }
        if part == ".." || part.contains('\0') {
            return Err(PipelineError::new(
                "WEB_STATIC_OUTPUT_PATH",
                "output_path must stay inside the project files directory",
            ));
        }
        parts.push(part.to_string());
    }
    if parts.is_empty() {
        return Err(PipelineError::new(
            "WEB_STATIC_OUTPUT_PATH",
            "output_path must not be empty",
        ));
    }
    Ok(parts.join("/"))
}

pub fn effective_site_root_rel_path(config: &Config) -> Result<Option<String>, PipelineError> {
    let Some(raw) = config
        .site_root
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
    else {
        return Ok(None);
    };
    web_static_site::normalize_site_root_rel_path(raw).map(Some)
}

pub fn effective_output_rel_path(config: &Config) -> Result<String, PipelineError> {
    if let Some(site_root_rel) = effective_site_root_rel_path(config)? {
        web_static_site::page_rel_path_from_site_root(&site_root_rel, &config.output_path)
    } else {
        normalize_output_rel_path(&config.output_path)
    }
}

pub fn effective_page_output_path(config: &Config) -> Result<String, PipelineError> {
    web_static_site::normalize_page_output_path(&config.output_path)
}

pub fn effective_deploy_base_url(config: &Config) -> Option<String> {
    web_static_site::normalize_deploy_base_url(config.deploy_base_url.as_deref())
}

pub fn effective_deploy_base_path(config: &Config) -> Result<Option<String>, PipelineError> {
    let Some(raw) = config
        .deploy_base_path
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
    else {
        return Ok(None);
    };
    web_static_site::normalize_deploy_base_path(Some(raw), "/").map(Some)
}

/// Compute the route seen by the template at render time.
pub fn default_route(owner: &str, project: &str, rel_path: &str) -> String {
    format!("/fs/{owner}/{project}/{rel_path}")
}

/// Decorate a rendered RWE HTML document so the persisted file can open directly.
pub fn build_static_html(
    mut html: String,
    hydration_payload: &Value,
    compiled_scripts: &[CompiledScript],
    template_root: Option<&Path>,
) -> String {
    html = ensure_meta_charset(html);

    if let Some(root) = template_root {
        let project_css_path = root.join("styles").join("main.css");
        if let Ok(project_css) = std::fs::read_to_string(&project_css_path)
            && !project_css.trim().is_empty()
        {
            html = inject_before_head_end(
                &html,
                &format!(
                    "<style data-project-theme>{}</style>",
                    escape_style_block(&project_css)
                ),
            );
        }
    }

    if let Some(css) = hydration_payload.get("css").and_then(Value::as_str)
        && !css.trim().is_empty()
    {
        html = inject_before_head_end(
            &html,
            &format!("<style data-rwe-tw>{}</style>", escape_style_block(css)),
        );
    }

    if !compiled_scripts.is_empty() {
        let mut block = String::new();
        for script in compiled_scripts {
            block.push_str("<script type=\"module\" data-zf-static-rwe>");
            block.push_str(&escape_script_block(&script.content));
            block.push_str("</script>");
        }
        html = inject_before_body_end(&html, &block);
    }

    html
}

/// Persist generated HTML with simple conflict handling and atomic replace.
pub fn write_generated_html(
    abs_path: &Path,
    html: &str,
    on_conflict: &str,
) -> Result<&'static str, PipelineError> {
    let bytes = html.as_bytes();
    if let Ok(existing) = std::fs::read(abs_path) {
        if existing == bytes {
            return Ok("unchanged");
        }
        match on_conflict.trim() {
            "overwrite" | "" => {}
            "skip" => return Ok("skipped"),
            "error" => {
                return Err(PipelineError::new(
                    "WEB_STATIC_CONFLICT",
                    format!("destination '{}' already exists", abs_path.display()),
                ));
            }
            other => {
                return Err(PipelineError::new(
                    "WEB_STATIC_CONFLICT_MODE",
                    format!(
                        "unsupported on_conflict value '{other}' — expected overwrite, skip, or error"
                    ),
                ));
            }
        }
    }

    if let Some(parent) = abs_path.parent() {
        std::fs::create_dir_all(parent).map_err(|err| {
            PipelineError::new(
                "WEB_STATIC_MKDIR",
                format!("failed creating '{}': {err}", parent.display()),
            )
        })?;
    }

    let tmp_path = abs_path.with_extension(format!(
        "{}.tmp",
        abs_path
            .extension()
            .and_then(|ext| ext.to_str())
            .unwrap_or("html")
    ));
    std::fs::write(&tmp_path, bytes).map_err(|err| {
        PipelineError::new(
            "WEB_STATIC_WRITE",
            format!("failed writing '{}': {err}", tmp_path.display()),
        )
    })?;
    std::fs::rename(&tmp_path, abs_path).map_err(|err| {
        let _ = std::fs::remove_file(&tmp_path);
        PipelineError::new(
            "WEB_STATIC_RENAME",
            format!("failed finalizing '{}': {err}", abs_path.display()),
        )
    })?;
    Ok("written")
}

/// Kind-level contract for the pipeline editor, DSL, and MCP-facing node help.
pub fn definition() -> NodeDefinition {
    NodeDefinition {
        kind: NODE_KIND.to_string(),
        title: "Web Static Generate".to_string(),
        description: "Render an RWE TSX template once and persist the HTML into project file storage. \
            Use this for static page generation, cached exports, and regeneration pipelines. \
            Generated files are written as Zebflow FS objects and should be treated as static artifacts, not same-origin hosted pages."
            .to_string(),
        input_schema: json!({ "type": "object" }),
        output_schema: json!({
            "type": "object",
            "properties": {
                "generated": {
                    "type": "object",
                    "properties": {
                        "status": { "type": "string" },
                        "path": { "type": "string" },
                        "url": { "type": "string" },
                        "route": { "type": "string" },
                        "deploy_base_url": { "type": ["string", "null"] },
                        "deploy_base_path": { "type": ["string", "null"] },
                        "template": { "type": "string" },
                        "site_root": { "type": ["string", "null"] },
                        "manifest_path": { "type": ["string", "null"] },
                        "asset_group": { "type": "string" },
                        "bytes": { "type": "integer" }
                    }
                }
            }
        }),
        input_pins: vec![INPUT_PIN_IN.to_string()],
        output_pins: vec![OUTPUT_PIN_OUT.to_string()],
        script_available: false,
        script_bridge: None,
        config_schema: json!({
            "type": "object",
            "required": ["template", "output_path"],
            "properties": {
                "template": { "type": "string", "description": "TSX page template relative to repo/pipelines. Must end with .tsx (e.g. pages/lyrics.tsx)." },
                "site_root": { "type": "string", "description": "Optional static site root under Zebflow FS. When set, output_path is resolved inside this shared site tree." },
                "output_path": { "type": "string", "description": "Zebflow FS object path. Supports config expressions." },
                "deploy_base_url": { "type": "string", "description": "Optional absolute deployed site origin used by templates for canonical/meta generation." },
                "deploy_base_path": { "type": "string", "description": "Optional deployed URL base path used to derive ctx.route for generated pages." },
                "route": { "type": "string", "description": "Optional route exposed to the template as ctx.route." },
                "on_conflict": { "type": "string", "enum": ["overwrite", "skip", "error"], "description": "What to do when the destination exists and content differs." }
            }
        }),
        dsl_flags: vec![
            DslFlag {
                flag: "--template".to_string(),
                config_key: "template".to_string(),
                description: "TSX page file relative to repo/pipelines. Must end with .tsx, e.g. pages/lyrics.tsx".to_string(),
                kind: DslFlagKind::Scalar,
                required: true,
            },
            DslFlag {
                flag: "--output-path".to_string(),
                config_key: "output_path".to_string(),
                description: "Zebflow FS object path. Supports {{ expr }} interpolation.".to_string(),
                kind: DslFlagKind::Scalar,
                required: true,
            },
            DslFlag {
                flag: "--site-root".to_string(),
                config_key: "site_root".to_string(),
                description: "Optional static site root under Zebflow FS. When set, output_path is written inside this shared site tree.".to_string(),
                kind: DslFlagKind::Scalar,
                required: false,
            },
            DslFlag {
                flag: "--deploy-base-url".to_string(),
                config_key: "deploy_base_url".to_string(),
                description: "Optional absolute deployed site origin used by templates for canonical/meta generation".to_string(),
                kind: DslFlagKind::Scalar,
                required: false,
            },
            DslFlag {
                flag: "--deploy-base-path".to_string(),
                config_key: "deploy_base_path".to_string(),
                description: "Optional deployed URL base path used to derive ctx.route for generated pages".to_string(),
                kind: DslFlagKind::Scalar,
                required: false,
            },
            DslFlag {
                flag: "--route".to_string(),
                config_key: "route".to_string(),
                description: "Optional ctx.route override seen by the template during generation".to_string(),
                kind: DslFlagKind::Scalar,
                required: false,
            },
            DslFlag {
                flag: "--on-conflict".to_string(),
                config_key: "on_conflict".to_string(),
                description: "overwrite, skip, or error when destination exists (default: overwrite)".to_string(),
                kind: DslFlagKind::Scalar,
                required: false,
            },
        ],
        fields: vec![
            NodeFieldDef {
                name: "template".to_string(),
                label: "Template".to_string(),
                field_type: NodeFieldType::Datalist,
                data_source: Some(crate::pipeline::model::NodeFieldDataSource::TemplatesPages),
                placeholder: Some("pages/lyrics.tsx".to_string()),
                help: Some("TSX page template used to render the generated static file. Must end with .tsx.".to_string()),
                ..Default::default()
            },
            NodeFieldDef {
                name: "site_root".to_string(),
                label: "Site Root".to_string(),
                field_type: NodeFieldType::Text,
                placeholder: Some("static/musiklib".to_string()),
                help: Some("Optional shared static site root under Zebflow FS. Use this when many generated pages belong to one site tree.".to_string()),
                ..Default::default()
            },
            NodeFieldDef {
                name: "output_path".to_string(),
                label: "Output Path".to_string(),
                field_type: NodeFieldType::Text,
                placeholder: Some("artists/{{ $input.artist_slug }}/{{ $input.song_slug }}/lyric.html".to_string()),
                help: Some("Zebflow FS object path. Config expressions are resolved before generation.".to_string()),
                ..Default::default()
            },
            NodeFieldDef {
                name: "deploy_base_url".to_string(),
                label: "Deploy Base URL".to_string(),
                field_type: NodeFieldType::Text,
                placeholder: Some("https://hadaf.id".to_string()),
                help: Some("Optional absolute deployed site origin used by templates for canonical/meta generation.".to_string()),
                ..Default::default()
            },
            NodeFieldDef {
                name: "deploy_base_path".to_string(),
                label: "Deploy Base Path".to_string(),
                field_type: NodeFieldType::Text,
                placeholder: Some("/".to_string()),
                help: Some("Optional deployed URL base path used to derive ctx.route for generated pages.".to_string()),
                ..Default::default()
            },
            NodeFieldDef {
                name: "route".to_string(),
                label: "Render Route".to_string(),
                field_type: NodeFieldType::Text,
                placeholder: Some("/lyrics/{{ $input.artist_slug }}/{{ $input.song_slug }}".to_string()),
                help: Some("Optional ctx.route override. Leave empty to use the generated /fs URL.".to_string()),
                ..Default::default()
            },
            NodeFieldDef {
                name: "on_conflict".to_string(),
                label: "On Conflict".to_string(),
                field_type: NodeFieldType::Select,
                options: vec![
                    SelectOptionDef { value: "overwrite".to_string(), label: "Overwrite".to_string() },
                    SelectOptionDef { value: "skip".to_string(), label: "Skip".to_string() },
                    SelectOptionDef { value: "error".to_string(), label: "Error".to_string() },
                ],
                default_value: Some(json!("overwrite")),
                help: Some("Skip keeps the old file, overwrite replaces it, error stops the pipeline.".to_string()),
                ..Default::default()
            },
        ],
        layout: vec![
            LayoutItem::Field("template".to_string()),
            LayoutItem::Field("on_conflict".to_string()),
            LayoutItem::Field("site_root".to_string()),
            LayoutItem::Field("output_path".to_string()),
            LayoutItem::Row {
                row: vec![
                    LayoutItem::Field("deploy_base_url".to_string()),
                    LayoutItem::Field("deploy_base_path".to_string()),
                ],
            },
            LayoutItem::Field("route".to_string()),
        ],
        ai_tool: Default::default(),
        ..Default::default()
    }
}

fn normalize_template_rel_path(raw: &str) -> Result<String, PipelineError> {
    let trimmed = raw.trim().trim_start_matches('/').replace('\\', "/");
    if trimmed.is_empty() {
        return Err(PipelineError::new(
            "WEB_STATIC_TEMPLATE_PATH",
            "template path must not be empty",
        ));
    }

    let mut parts = Vec::new();
    for part in trimmed.split('/') {
        let part = part.trim();
        if part.is_empty() || part == "." {
            continue;
        }
        if part == ".." || part.contains('\0') {
            return Err(PipelineError::new(
                "WEB_STATIC_TEMPLATE_PATH",
                "template path must stay inside the project template root",
            ));
        }
        parts.push(part.to_string());
    }
    if parts.is_empty() {
        return Err(PipelineError::new(
            "WEB_STATIC_TEMPLATE_PATH",
            "template path must not be empty",
        ));
    }
    let last = parts.last().expect("parts not empty");
    if !last.ends_with(".tsx") {
        return Err(PipelineError::new(
            "WEB_STATIC_TEMPLATE_PATH",
            "template path must end with .tsx",
        ));
    }
    Ok(parts.join("/"))
}

fn ensure_meta_charset(mut html: String) -> String {
    if html.contains("<meta charset") || html.contains("<meta http-equiv=\"Content-Type\"") {
        return html;
    }
    let tag = "<meta charset=\"utf-8\">";
    if let Some(pos) = html.find("<head>") {
        html.insert_str(pos + "<head>".len(), tag);
    } else if let Some(pos) = html.find("</head>") {
        html.insert_str(pos, tag);
    } else {
        html = format!("{tag}{html}");
    }
    html
}

fn inject_before_head_end(html: &str, snippet: &str) -> String {
    if let Some(pos) = html.find("</head>") {
        let mut out = String::with_capacity(html.len() + snippet.len());
        out.push_str(&html[..pos]);
        out.push_str(snippet);
        out.push_str(&html[pos..]);
        out
    } else {
        format!("{snippet}{html}")
    }
}

fn inject_before_body_end(html: &str, snippet: &str) -> String {
    if let Some(pos) = html.find("</body>") {
        let mut out = String::with_capacity(html.len() + snippet.len());
        out.push_str(&html[..pos]);
        out.push_str(snippet);
        out.push_str(&html[pos..]);
        out
    } else {
        format!("{html}{snippet}")
    }
}

fn escape_script_block(content: &str) -> String {
    content.replace("</script>", "<\\/script>")
}

fn escape_style_block(content: &str) -> String {
    content.replace("</style>", "<\\/style>")
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use super::{
        NODE_KIND, build_static_html, default_route, effective_output_rel_path,
        normalize_output_rel_path,
    };
    use serde_json::json;

    use crate::language::DenoSandboxEngine;
    use crate::pipeline::engines::basic::new_template_cache;
    use crate::pipeline::{
        BasicPipelineEngine, PipelineContext, PipelineEngine, PipelineGraph, PipelineNode,
    };
    use crate::platform::adapters::file::build_file_adapter;
    use crate::platform::model::FileAdapterKind;
    use crate::rwe::resolve_engine_or_default;

    #[test]
    fn output_path_stays_scoped() {
        let rel = normalize_output_rel_path("artists/a/song.html").expect("path");
        assert_eq!(rel, "artists/a/song.html");
        assert!(normalize_output_rel_path("../escape.html").is_err());
        let rel = effective_output_rel_path(&super::Config {
            output_path: "artists/a/song.html".to_string(),
            site_root: Some("static/musiklib".to_string()),
            ..Default::default()
        })
        .expect("site root path");
        assert_eq!(rel, "static/musiklib/artists/a/song.html");
    }

    #[test]
    fn template_path_requires_explicit_tsx_extension() {
        assert_eq!(
            super::normalize_template_rel_path("pages/lyrics.tsx").expect("tsx template"),
            "pages/lyrics.tsx"
        );
        let err =
            super::normalize_template_rel_path("pages/lyrics").expect_err("missing extension");
        assert_eq!(err.code, "WEB_STATIC_TEMPLATE_PATH");
        assert!(err.message.contains(".tsx"));
    }

    #[test]
    fn default_route_targets_served_file() {
        assert_eq!(
            default_route("superadmin", "example-project", "collections/a/item.html"),
            "/fs/superadmin/example-project/collections/a/item.html"
        );
    }

    #[test]
    fn build_static_html_inlines_css_and_scripts() {
        let html = build_static_html(
            "<html><head></head><body><main>ok</main></body></html>".to_string(),
            &json!({ "css": ".x{color:red;}" }),
            &[crate::rwe::CompiledScript {
                id: "page".to_string(),
                scope: crate::rwe::CompiledScriptScope::Page,
                content_type: "text/javascript".to_string(),
                content: "console.log('ok')".to_string(),
                content_hash: "abc".to_string(),
                suggested_file_name: "rwe-abc.mjs".to_string(),
            }],
            None,
        );
        assert!(html.contains("data-rwe-tw"));
        assert!(html.contains("data-zf-static-rwe"));
        assert!(html.contains("console.log('ok')"));
    }

    #[tokio::test]
    async fn engine_generates_static_file_and_detects_unchanged_content() {
        let root = std::env::temp_dir().join(format!(
            "zebflow-staticgen-test-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .expect("unix time")
                .as_nanos()
        ));
        let file = build_file_adapter(FileAdapterKind::Filesystem, root.clone());
        let layout = file
            .ensure_project_layout("superadmin", "example-project")
            .expect("layout");
        let template_dir = layout.repo_pipelines_dir.join("pages");
        std::fs::create_dir_all(&template_dir).expect("template dir");
        let asset_dir = layout.repo_pipelines_dir.join("assets").join("icons");
        std::fs::create_dir_all(&asset_dir).expect("asset dir");
        std::fs::write(asset_dir.join("favicon.ico"), b"ico").expect("favicon");
        std::fs::write(
            template_dir.join("lyric.tsx"),
            r#"
export const page = {
  head: {
    title: "Lyric",
    icons: [
      { rel: "icon", href: "/assets/superadmin/example-project/icons/favicon.ico" }
    ]
  }
};

export const app = {};

export default function LyricPage(input) {
  return (
    <Page>
      <main className="min-h-screen bg-white text-slate-900 p-6">
        <img src="/assets/branding/logo.svg" alt="Zebflow" />
        <h1 className="text-3xl font-black">{input.artist_name} - {input.song_title}</h1>
        <p className="mt-4">{input.lyric_line}</p>
      </main>
    </Page>
  );
}
"#,
        )
        .expect("template write");

        let graph = PipelineGraph {
            kind: "zebflow.pipeline".to_string(),
            version: "0.1".to_string(),
            id: "generate-lyric".to_string(),
            description: None,
            metadata: None,
            entry_nodes: vec!["gen".to_string()],
            nodes: vec![PipelineNode {
                id: "gen".to_string(),
                kind: NODE_KIND.to_string(),
                input_pins: vec!["in".to_string()],
                output_pins: vec!["out".to_string()],
                config: json!({
                    "template": "pages/lyric.tsx",
                    "site_root": "static/musiklib",
                    "output_path": "artists/{{ $input.artist_slug }}/{{ $input.song_slug }}/lyric.html"
                }),
            }],
            edges: vec![],
        };
        let ctx = PipelineContext {
            owner: "superadmin".to_string(),
            project: "example-project".to_string(),
            pipeline: graph.id.clone(),
            request_id: "req-1".to_string(),
            route: String::new(),
            input: json!({
                "artist_slug": "iwan-fals",
                "song_slug": "bento",
                "artist_name": "Iwan Fals",
                "song_title": "Bento",
                "lyric_line": "Namaku Bento."
            }),
            trigger: None,
            placeholder: None,
        };
        let engine = BasicPipelineEngine::new(
            Arc::new(DenoSandboxEngine::default()),
            resolve_engine_or_default(None),
            None,
        )
        .with_template_root(Some(layout.repo_pipelines_dir.clone()))
        .with_template_cache(new_template_cache())
        .with_data_root(root.clone());

        let first = engine
            .execute_async(&graph, &ctx)
            .await
            .expect("first generate");
        assert_eq!(first.value["generated"]["status"], "written");
        assert_eq!(
            first.value["generated"]["path"],
            "static/musiklib/artists/iwan-fals/bento/lyric.html"
        );
        assert_eq!(
            first.value["generated"]["url"],
            "/fs/superadmin/example-project/static/musiklib/artists/iwan-fals/bento/lyric.html"
        );
        assert_eq!(first.value["generated"]["site_root"], "static/musiklib");
        assert_eq!(
            first.value["generated"]["manifest_path"],
            "static/musiklib/.zebflow-static-site.json"
        );

        let generated_path = layout
            .files_dir
            .join("static")
            .join("musiklib")
            .join("artists")
            .join("iwan-fals")
            .join("bento")
            .join("lyric.html");
        let generated_html = std::fs::read_to_string(&generated_path).expect("generated html");
        assert!(generated_html.contains("Iwan Fals - Bento"));
        assert!(generated_html.contains("Namaku Bento."));
        assert!(generated_html.contains("data-rwe-tw"));
        assert!(
            generated_html
                .contains("../../../_assets/libraries/zeb/preact/0.1/runtime/preact.bundle.mjs")
        );
        assert!(generated_html.contains("../../../_assets/project/icons/favicon.ico"));
        assert!(generated_html.contains("../../../_assets/branding/logo.svg"));
        assert!(
            layout
                .files_dir
                .join("static")
                .join("musiklib")
                .join("_assets")
                .join("libraries")
                .join("zeb")
                .join("preact")
                .join("0.1")
                .join("runtime")
                .join("preact.bundle.mjs")
                .is_file()
        );
        assert!(
            layout
                .files_dir
                .join("static")
                .join("musiklib")
                .join("_assets")
                .join("project")
                .join("icons")
                .join("favicon.ico")
                .is_file()
        );
        assert!(
            layout
                .files_dir
                .join("static")
                .join("musiklib")
                .join("_assets")
                .join("branding")
                .join("logo.svg")
                .is_file()
        );
        let manifest = std::fs::read_to_string(
            layout
                .files_dir
                .join("static")
                .join("musiklib")
                .join(".zebflow-static-site.json"),
        )
        .expect("manifest");
        assert!(manifest.contains("\"site_root\": \"static/musiklib\""));
        assert!(manifest.contains("\"template\": \"pages/lyric.tsx\""));
        assert!(manifest.contains("\"path\": \"_assets/project/icons/favicon.ico\""));
        assert!(manifest.contains("\"path\": \"_assets/branding/logo.svg\""));

        let second = engine
            .execute_async(&graph, &ctx)
            .await
            .expect("second generate");
        assert_eq!(second.value["generated"]["status"], "unchanged");

        let _ = std::fs::remove_dir_all(&root);
    }

    #[tokio::test]
    async fn regenerating_one_static_page_only_updates_that_page() {
        let root = std::env::temp_dir().join(format!(
            "zebflow-staticgen-single-page-update-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .expect("unix time")
                .as_nanos()
        ));
        let file = build_file_adapter(FileAdapterKind::Filesystem, root.clone());
        let layout = file
            .ensure_project_layout("superadmin", "example-project")
            .expect("layout");
        let template_dir = layout.repo_pipelines_dir.join("pages");
        std::fs::create_dir_all(&template_dir).expect("template dir");
        std::fs::write(
            template_dir.join("lyric.tsx"),
            r#"
export const page = {
  html: {
    lang: "en",
  }
};

export function getPage(input) {
  return {
    head: {
      title: `${input.artist_name} — ${input.song_title} | Musiklib`,
      description: `${input.song_title} lyrics by ${input.artist_name}.`
    }
  };
}

export default function LyricPage(input) {
  return (
    <Page>
      <main className="min-h-screen bg-white text-slate-900 p-6">
        <h1 className="text-3xl font-black">{input.artist_name} - {input.song_title}</h1>
        <p className="mt-4">{input.lyric_line}</p>
      </main>
    </Page>
  );
}
"#,
        )
        .expect("template write");

        let graph = PipelineGraph {
            kind: "zebflow.pipeline".to_string(),
            version: "0.1".to_string(),
            id: "generate-one-lyric".to_string(),
            description: None,
            metadata: None,
            entry_nodes: vec!["gen".to_string()],
            nodes: vec![PipelineNode {
                id: "gen".to_string(),
                kind: NODE_KIND.to_string(),
                input_pins: vec!["in".to_string()],
                output_pins: vec!["out".to_string()],
                config: json!({
                    "template": "pages/lyric.tsx",
                    "site_root": "static/musiklib",
                    "output_path": "{{ $input.letter_slug }}/{{ $input.artist_slug }}/songs/{{ $input.song_slug }}/lyrics/index.html"
                }),
            }],
            edges: vec![],
        };

        let engine = BasicPipelineEngine::new(
            Arc::new(DenoSandboxEngine::default()),
            resolve_engine_or_default(None),
            None,
        )
        .with_template_root(Some(layout.repo_pipelines_dir.clone()))
        .with_template_cache(new_template_cache())
        .with_data_root(root.clone());

        let aurora_ctx = PipelineContext {
            owner: "superadmin".to_string(),
            project: "example-project".to_string(),
            pipeline: graph.id.clone(),
            request_id: "req-aurora-1".to_string(),
            route: String::new(),
            input: json!({
                "letter_slug": "a",
                "artist_slug": "aurora",
                "song_slug": "runaway",
                "artist_name": "Aurora",
                "song_title": "Runaway",
                "lyric_line": "I was listening to the ocean."
            }),
            trigger: None,
            placeholder: None,
        };
        let iwan_ctx = PipelineContext {
            owner: "superadmin".to_string(),
            project: "example-project".to_string(),
            pipeline: graph.id.clone(),
            request_id: "req-iwan-1".to_string(),
            route: String::new(),
            input: json!({
                "letter_slug": "i",
                "artist_slug": "iwan-fals",
                "song_slug": "bento",
                "artist_name": "Iwan Fals",
                "song_title": "Bento",
                "lyric_line": "Namaku Bento."
            }),
            trigger: None,
            placeholder: None,
        };

        engine
            .execute_async(&graph, &aurora_ctx)
            .await
            .expect("generate aurora page");
        engine
            .execute_async(&graph, &iwan_ctx)
            .await
            .expect("generate iwan page");

        let aurora_path = layout
            .files_dir
            .join("static")
            .join("musiklib")
            .join("a")
            .join("aurora")
            .join("songs")
            .join("runaway")
            .join("lyrics")
            .join("index.html");
        let iwan_path = layout
            .files_dir
            .join("static")
            .join("musiklib")
            .join("i")
            .join("iwan-fals")
            .join("songs")
            .join("bento")
            .join("lyrics")
            .join("index.html");

        let aurora_before = std::fs::read_to_string(&aurora_path).expect("aurora before");
        let iwan_before = std::fs::read_to_string(&iwan_path).expect("iwan before");
        assert!(aurora_before.contains("I was listening to the ocean."));
        assert!(iwan_before.contains("Namaku Bento."));

        let aurora_updated_ctx = PipelineContext {
            request_id: "req-aurora-2".to_string(),
            input: json!({
                "letter_slug": "a",
                "artist_slug": "aurora",
                "song_slug": "runaway",
                "artist_name": "Aurora",
                "song_title": "Runaway",
                "lyric_line": "I was listening to the ocean, again."
            }),
            ..aurora_ctx
        };

        let updated = engine
            .execute_async(&graph, &aurora_updated_ctx)
            .await
            .expect("update aurora page");
        assert_eq!(updated.value["generated"]["status"], "written");
        assert_eq!(
            updated.value["generated"]["path"],
            "static/musiklib/a/aurora/songs/runaway/lyrics/index.html"
        );

        let aurora_after = std::fs::read_to_string(&aurora_path).expect("aurora after");
        let iwan_after = std::fs::read_to_string(&iwan_path).expect("iwan after");
        assert!(aurora_after.contains("I was listening to the ocean, again."));
        assert_eq!(iwan_before, iwan_after);

        let _ = std::fs::remove_dir_all(&root);
    }
}