stackql-deploy 2.0.9

Infrastructure-as-code framework for declarative cloud resource management using StackQL
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
// lib/templating.rs

//! # Templating Module
//!
//! Handles loading, parsing, and rendering SQL query templates from .iql files.
//! Matches the Python `lib/templating.py` implementation.
//!
//! Queries are loaded and parsed eagerly, but rendered lazily (JIT) when
//! actually needed. This avoids errors from templates that reference variables
//! not yet available in the context (e.g., delete queries referencing exports
//! that haven't been computed yet during a build operation).

use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::process;

use log::{debug, error};
use regex::Regex;

use crate::core::config::prepare_query_context;
use crate::resource::manifest::Resource;
use crate::template::engine::TemplateEngine;

/// Parsed query with its raw template and options.
/// Rendering is deferred until the query is actually needed.
#[derive(Debug, Clone)]
pub struct ParsedQuery {
    pub template: String,
    pub options: QueryOptions,
}

/// Options for a query anchor.
#[derive(Debug, Clone, Default)]
pub struct QueryOptions {
    pub retries: u32,
    pub retry_delay: u32,
    pub postdelete_retries: u32,
    pub postdelete_retry_delay: u32,
    /// Dot-path into the RETURNING * result to check before polling
    /// (e.g. `"ProgressEvent.OperationStatus"`).  Only used on `callback`
    /// anchors.
    pub short_circuit_field: Option<String>,
    /// Value of `short_circuit_field` that means polling can be skipped.
    /// Only used on `callback` anchors.
    pub short_circuit_value: Option<String>,
}

/// Parse an anchor line to extract key, numeric options, and string options.
/// Matches Python's `parse_anchor`, extended for callback string params.
///
/// Returns `(key, uint_options, str_options)`.  Numeric-valued params go into
/// `uint_options`; all other params (e.g. `short_circuit_field`,
/// `short_circuit_value`) go into `str_options`.
fn parse_anchor(anchor: &str) -> (String, HashMap<String, u32>, HashMap<String, String>) {
    let parts: Vec<&str> = anchor.split(',').collect();
    let key = parts[0].trim().to_lowercase();
    let mut uint_options: HashMap<String, u32> = HashMap::new();
    let mut str_options: HashMap<String, String> = HashMap::new();

    for part in &parts[1..] {
        if let Some((option_key, option_value)) = part.split_once('=') {
            let k = option_key.trim().to_string();
            let v = option_value.trim().to_string();
            if let Ok(uint_val) = v.parse::<u32>() {
                uint_options.insert(k, uint_val);
            } else {
                str_options.insert(k, v);
            }
        }
    }

    (key, uint_options, str_options)
}

/// Return type of `load_sql_queries`: (templates, uint_options, str_options).
type SqlQueriesResult = (
    HashMap<String, String>,
    HashMap<String, HashMap<String, u32>>,
    HashMap<String, HashMap<String, String>>,
);

/// Load SQL queries from a .iql file, split by anchors.
/// Matches Python's `load_sql_queries`.
fn load_sql_queries(file_path: &Path) -> SqlQueriesResult {
    let content = match fs::read_to_string(file_path) {
        Ok(c) => c,
        Err(e) => {
            error!("Failed to read query file {:?}: {}", file_path, e);
            process::exit(1);
        }
    };

    let mut queries: HashMap<String, String> = HashMap::new();
    let mut uint_options: HashMap<String, HashMap<String, u32>> = HashMap::new();
    let mut str_options: HashMap<String, HashMap<String, String>> = HashMap::new();
    let mut current_anchor: Option<String> = None;
    let mut query_buffer: Vec<String> = Vec::new();

    for line in content.lines() {
        if line.trim_start().starts_with("/*+") && line.contains("*/") {
            // Store the current query under the last anchor
            if let Some(ref anchor) = current_anchor {
                if !query_buffer.is_empty() {
                    let (anchor_key, anchor_uint_opts, anchor_str_opts) = parse_anchor(anchor);
                    queries.insert(
                        anchor_key.clone(),
                        query_buffer.join("\n").trim().to_string(),
                    );
                    uint_options.insert(anchor_key.clone(), anchor_uint_opts);
                    str_options.insert(anchor_key, anchor_str_opts);
                    query_buffer.clear();
                }
            }
            // Extract new anchor
            let start = line.find("/*+").unwrap() + 3;
            let end = line.find("*/").unwrap();
            current_anchor = Some(line[start..end].trim().to_string());
        } else {
            query_buffer.push(line.to_string());
        }
    }

    // Store the last query
    if let Some(ref anchor) = current_anchor {
        if !query_buffer.is_empty() {
            let (anchor_key, anchor_uint_opts, anchor_str_opts) = parse_anchor(anchor);
            queries.insert(
                anchor_key.clone(),
                query_buffer.join("\n").trim().to_string(),
            );
            uint_options.insert(anchor_key.clone(), anchor_uint_opts);
            str_options.insert(anchor_key, anchor_str_opts);
        }
    }

    (queries, uint_options, str_options)
}

/// Pre-process Jinja2 inline dict expressions that Tera doesn't support.
///
/// Converts patterns like `{{ { "Key": var, ... } | filter }}` into
/// Tera-compatible form by resolving the dict from context variables
/// and injecting the result as a temporary context variable.
///
/// For example:
///   `{{ { "Description": description, "Path": path } | generate_patch_document }}`
/// becomes:
///   `{{ __inline_dict_0 | generate_patch_document }}`
/// with `__inline_dict_0` set to the constructed JSON object in context.
pub fn preprocess_inline_dicts(template: &str, context: &mut HashMap<String, String>) -> String {
    // Match {{ { ... } | filter_name }}
    // This regex captures the dict body and the filter expression
    let re = Regex::new(r"\{\{\s*\{([^}]*(?:\{[^}]*\}[^}]*)*)\}\s*\|\s*(\w+)\s*\}\}").unwrap();

    let mut result = template.to_string();
    let mut counter = 0;

    // We need to iterate carefully since we're modifying the string
    loop {
        let captures = re.captures(&result);
        if captures.is_none() {
            break;
        }
        let caps = captures.unwrap();
        let full_match = caps.get(0).unwrap();
        let dict_body = caps.get(1).unwrap().as_str().trim();
        let filter_name = caps.get(2).unwrap().as_str();

        // Parse the dict body: "Key": var, "Key2": var2
        let mut obj = serde_json::Map::new();
        for entry in split_dict_entries(dict_body) {
            let entry = entry.trim();
            if entry.is_empty() {
                continue;
            }
            if let Some((key_part, val_part)) = entry.split_once(':') {
                let key = key_part
                    .trim()
                    .trim_matches('"')
                    .trim_matches('\'')
                    .to_string();
                let var_name = val_part.trim();

                // Look up the variable in context
                let value = context.get(var_name).cloned().unwrap_or_default();

                // Try to parse as JSON, otherwise use as string
                let json_val = match serde_json::from_str::<serde_json::Value>(&value) {
                    Ok(v) => v,
                    Err(_) => serde_json::Value::String(value),
                };
                obj.insert(key, json_val);
            }
        }

        let var_name = format!("__inline_dict_{}", counter);
        let json_str = serde_json::to_string(&serde_json::Value::Object(obj)).unwrap_or_default();
        context.insert(var_name.clone(), json_str);

        let replacement = format!("{{{{ {} | {} }}}}", var_name, filter_name);
        result = format!(
            "{}{}{}",
            &result[..full_match.start()],
            replacement,
            &result[full_match.end()..]
        );
        counter += 1;
    }

    result
}

/// Split dict entries by commas, but respect nested braces and quoted strings.
fn split_dict_entries(s: &str) -> Vec<String> {
    let mut entries = Vec::new();
    let mut current = String::new();
    let mut brace_depth = 0;
    let mut in_quote = false;
    let mut quote_char = ' ';

    for ch in s.chars() {
        match ch {
            '"' | '\'' if !in_quote => {
                in_quote = true;
                quote_char = ch;
                current.push(ch);
            }
            c if in_quote && c == quote_char => {
                in_quote = false;
                current.push(ch);
            }
            '{' if !in_quote => {
                brace_depth += 1;
                current.push(ch);
            }
            '}' if !in_quote => {
                brace_depth -= 1;
                current.push(ch);
            }
            ',' if !in_quote && brace_depth == 0 => {
                entries.push(current.trim().to_string());
                current.clear();
            }
            _ => {
                current.push(ch);
            }
        }
    }
    if !current.trim().is_empty() {
        entries.push(current.trim().to_string());
    }
    entries
}

/// Pre-process Jinja2-specific syntax into Tera-compatible equivalents.
/// Handles:
/// - `{{ uuid() }}` -> `{{ uuid }}` (function call to variable)
/// - `replace('x', 'y')` -> `replace(from="x", to="y")` (positional to named args)
fn preprocess_jinja2_compat(template: &str) -> String {
    let mut result = template.to_string();

    // Convert {{ uuid() }} to {{ uuid }}
    let uuid_re = Regex::new(r"\{\{\s*uuid\(\)\s*\}\}").unwrap();
    result = uuid_re.replace_all(&result, "{{ uuid }}").to_string();

    // Convert Jinja2 replace('from', 'to') to Tera replace(from="from", to="to")
    // Matches: replace('x', 'y') or replace("x", "y") with any quoting combo
    let replace_re =
        Regex::new(r#"replace\(\s*['"]([^'"]*)['"]\s*,\s*['"]([^'"]*)['"]\s*\)"#).unwrap();
    result = replace_re
        .replace_all(&result, r#"replace(from="$1", to="$2")"#)
        .to_string();

    result
}

/// Render a single query template with the given context.
/// This is the JIT rendering function called when a query is actually needed.
pub fn render_query(
    engine: &TemplateEngine,
    res_name: &str,
    anchor: &str,
    template: &str,
    context: &HashMap<String, String>,
) -> String {
    let temp_context = prepare_query_context(context);

    let expanded = match preprocess_this_prefix(template, res_name) {
        Ok(t) => t,
        Err(e) => {
            crate::core::utils::catch_error_and_exit(&format!("[{}] [{}] {}", res_name, anchor, e));
        }
    };

    let mut ctx = temp_context;
    let compat_query = preprocess_jinja2_compat(&expanded);
    let processed_query = preprocess_inline_dicts(&compat_query, &mut ctx);

    let template_name = format!("{}__{}", res_name, anchor);
    match engine.render_with_filters(&template_name, &processed_query, &ctx) {
        Ok(rendered) => {
            // Check for unresolved template variables in the final rendered output
            let unresolved_re = Regex::new(r"\{\{[^}]+\}\}").unwrap();
            if let Some(m) = unresolved_re.find(&rendered) {
                crate::core::utils::catch_error_and_exit(&format!(
                    "Unresolved template variable in [{}] [{}]: '{}'\n\nRendered query:\n{}\n",
                    res_name,
                    anchor,
                    m.as_str(),
                    rendered
                ));
            }
            debug!(
                "Rendered [{}] [{}] query:\n\n{}\n",
                res_name, anchor, rendered
            );
            rendered
        }
        Err(e) => {
            error!(
                "Error rendering query for [{}] [{}]: {}",
                res_name, anchor, e
            );

            // Extract template variable references for diagnostics
            let re = Regex::new(r"\{\{\s*(\w+)").unwrap();
            let referenced_vars: Vec<&str> = re
                .captures_iter(&processed_query)
                .filter_map(|c| c.get(1).map(|m| m.as_str()))
                .collect();
            let missing: Vec<&&str> = referenced_vars
                .iter()
                .filter(|v| !ctx.contains_key(**v))
                .collect();

            if !missing.is_empty() {
                error!(
                    "Missing variables in context for [{}] [{}]: {:?}",
                    res_name, anchor, missing
                );
                error!(
                    "Hint: ensure these properties are defined in the manifest for resource [{}], \
                     or that the .iql template only references variables provided by the manifest.",
                    res_name
                );
            }

            debug!(
                "[{}] [{}] available context keys: {:?}",
                res_name,
                anchor,
                ctx.keys().collect::<Vec<_>>()
            );

            crate::core::utils::catch_error_and_exit(&format!(
                "Failed to render query for [{}] [{}]",
                res_name, anchor
            ));
        }
    }
}

/// Try to render a query template, returning None if variables are missing.
/// Used for deferred rendering where this.* fields may not yet be available.
pub fn try_render_query(
    engine: &TemplateEngine,
    res_name: &str,
    anchor: &str,
    template: &str,
    context: &HashMap<String, String>,
) -> Option<String> {
    let temp_context = prepare_query_context(context);

    let expanded = match preprocess_this_prefix(template, res_name) {
        Ok(t) => t,
        Err(_) => return None,
    };

    let mut ctx = temp_context;
    let compat_query = preprocess_jinja2_compat(&expanded);
    let processed_query = preprocess_inline_dicts(&compat_query, &mut ctx);

    let template_name = format!("{}__{}", res_name, anchor);
    match engine.render_with_filters(&template_name, &processed_query, &ctx) {
        Ok(rendered) => {
            // Check for unresolved template variables
            let unresolved_re = Regex::new(r"\{\{[^}]+\}\}").unwrap();
            if unresolved_re.is_match(&rendered) {
                debug!(
                    "Unresolved variables in [{}] [{}], deferring render",
                    res_name, anchor
                );
                return None;
            }
            debug!(
                "Rendered [{}] [{}] query:\n\n{}\n",
                res_name, anchor, rendered
            );
            Some(rendered)
        }
        Err(_) => None,
    }
}

/// Get queries for a resource: load from file, parse anchors.
/// Templates are NOT rendered here — rendering is deferred to when
/// each query is actually needed (JIT rendering).
/// Matches Python's `get_queries`.
///
/// Callback anchors (e.g. `callback:create`, `callback:delete`) are stored
/// under the key `"callback:create"`, `"callback:delete"`, etc.  A bare
/// `callback` anchor (no operation qualifier) is stored under `"callback"`.
pub fn get_queries(
    _engine: &TemplateEngine,
    stack_dir: &str,
    resource: &Resource,
    _full_context: &HashMap<String, String>,
) -> HashMap<String, ParsedQuery> {
    let mut result = HashMap::new();

    let template_path = if let Some(ref file) = resource.file {
        Path::new(stack_dir).join("resources").join(file)
    } else {
        Path::new(stack_dir)
            .join("resources")
            .join(format!("{}.iql", resource.name))
    };

    if !template_path.exists() {
        error!("Query file not found: {:?}", template_path);
        process::exit(1);
    }

    let (query_templates, query_uint_options, query_str_options) = load_sql_queries(&template_path);

    for (anchor, template) in &query_templates {
        // Fix backward compatibility for preflight and postdeploy.
        // Callback anchors (callback:create, callback:delete, callback:update,
        // callback) are passed through unchanged.
        let normalized_anchor = match anchor.as_str() {
            "preflight" => "exists".to_string(),
            "postdeploy" => "statecheck".to_string(),
            other => other.to_string(),
        };

        let uint_opts = query_uint_options.get(anchor).cloned().unwrap_or_default();
        let str_opts = query_str_options.get(anchor).cloned().unwrap_or_default();

        result.insert(
            normalized_anchor.clone(),
            ParsedQuery {
                template: template.clone(),
                options: QueryOptions {
                    retries: *uint_opts.get("retries").unwrap_or(&1),
                    retry_delay: *uint_opts.get("retry_delay").unwrap_or(&0),
                    postdelete_retries: *uint_opts.get("postdelete_retries").unwrap_or(&10),
                    postdelete_retry_delay: *uint_opts.get("postdelete_retry_delay").unwrap_or(&5),
                    short_circuit_field: str_opts.get("short_circuit_field").cloned(),
                    short_circuit_value: str_opts.get("short_circuit_value").cloned(),
                },
            },
        );
    }

    debug!(
        "Queries for [{}]: {:?}",
        resource.name,
        result.keys().collect::<Vec<_>>()
    );
    result
}

/// Pre-process `this.` prefix inside Tera template blocks.
///
/// Within every `{{ ... }}` and `{% ... %}` block, replaces `this.` with
/// `{resource_name}.`, allowing resource-scoped variables to be referenced
/// unambiguously inside a resource's own `.iql` file.
///
/// Returns `Err` with a diagnostic if `this.` appears but `resource_name`
/// is empty (i.e. no active resource context, such as a global template).
pub fn preprocess_this_prefix(template: &str, resource_name: &str) -> Result<String, String> {
    if !template.contains("this.") {
        return Ok(template.to_string());
    }

    if resource_name.is_empty() {
        return Err(
            "Template uses 'this.' prefix but no resource context is active; \
             'this.' is only valid inside a resource's .iql file."
                .to_string(),
        );
    }

    let replacement = format!("{}.", resource_name);

    // Replace 'this.' with '{resource_name}.' inside {{ ... }} blocks.
    let var_re = Regex::new(r"(?s)\{\{(.*?)\}\}").unwrap();
    let with_vars = var_re.replace_all(template, |caps: &regex::Captures| {
        let inner = caps[1].replace("this.", &replacement);
        format!("{{{{{}}}}}", inner)
    });

    // Also handle {% ... %} tag blocks (conditionals, loops).
    let tag_re = Regex::new(r"(?s)\{%(.*?)%\}").unwrap();
    let with_tags = tag_re.replace_all(&with_vars, |caps: &regex::Captures| {
        let inner = caps[1].replace("this.", &replacement);
        format!("{{%{}%}}", inner)
    });

    Ok(with_tags.to_string())
}

/// Render an inline SQL template string.
/// Matches Python's `render_inline_template`.
pub fn render_inline_template(
    engine: &TemplateEngine,
    resource_name: &str,
    template_string: &str,
    full_context: &HashMap<String, String>,
) -> String {
    debug!(
        "[{}] inline template:\n\n{}\n",
        resource_name, template_string
    );

    let mut temp_context = prepare_query_context(full_context);

    let expanded = match preprocess_this_prefix(template_string, resource_name) {
        Ok(t) => t,
        Err(e) => {
            error!("[{}] inline template: {}", resource_name, e);
            process::exit(1);
        }
    };

    let compat = preprocess_jinja2_compat(&expanded);
    let processed = preprocess_inline_dicts(&compat, &mut temp_context);
    let template_name = format!("{}__inline", resource_name);

    match engine.render_with_filters(&template_name, &processed, &temp_context) {
        Ok(rendered) => {
            debug!(
                "[{}] rendered inline template:\n\n{}\n",
                resource_name, rendered
            );
            rendered
        }
        Err(e) => {
            error!(
                "Error rendering inline template for [{}]: {}",
                resource_name, e
            );

            let re = Regex::new(r"\{\{\s*(\w+)").unwrap();
            let referenced_vars: Vec<&str> = re
                .captures_iter(&processed)
                .filter_map(|c| c.get(1).map(|m| m.as_str()))
                .collect();
            let missing: Vec<&&str> = referenced_vars
                .iter()
                .filter(|v| !temp_context.contains_key(**v))
                .collect();

            if !missing.is_empty() {
                error!(
                    "Missing variables in context for [{}]: {:?}",
                    resource_name, missing
                );
                error!(
                    "Hint: ensure these properties are defined in the manifest for resource [{}], \
                     or that the inline SQL only references variables provided by the manifest.",
                    resource_name
                );
            }

            debug!(
                "[{}] available context keys: {:?}",
                resource_name,
                temp_context.keys().collect::<Vec<_>>()
            );

            process::exit(1);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::template::engine::TemplateEngine;

    // ── preprocess_this_prefix unit tests ─────────────────────────────────

    #[test]
    fn test_preprocess_this_prefix_basic_rewrite() {
        let result = preprocess_this_prefix("{{ this.fred }}", "resource_name_x").unwrap();
        assert_eq!(result, "{{ resource_name_x.fred }}");
    }

    #[test]
    fn test_preprocess_this_prefix_noop_when_no_this() {
        let template = "{{ fred }}";
        let result = preprocess_this_prefix(template, "resource_name_x").unwrap();
        assert_eq!(
            result, template,
            "template without 'this.' should be unchanged"
        );
    }

    #[test]
    fn test_preprocess_this_prefix_error_when_no_resource_name() {
        let result = preprocess_this_prefix("{{ this.fred }}", "");
        assert!(result.is_err(), "empty resource_name should return Err");
        let msg = result.unwrap_err();
        assert!(
            msg.contains("this.") || msg.contains("resource context"),
            "error message should mention 'this.' or resource context, got: {}",
            msg
        );
    }

    #[test]
    fn test_preprocess_this_prefix_multiple_occurrences() {
        let template = "{{ this.a }} and {{ this.b }}";
        let result = preprocess_this_prefix(template, "my_res").unwrap();
        assert_eq!(result, "{{ my_res.a }} and {{ my_res.b }}");
    }

    #[test]
    fn test_preprocess_this_prefix_deep_path() {
        let template = "{{ this.callback.ProgressEvent.RequestToken }}";
        let result = preprocess_this_prefix(template, "resource_name_x").unwrap();
        assert_eq!(
            result,
            "{{ resource_name_x.callback.ProgressEvent.RequestToken }}"
        );
    }

    #[test]
    fn test_preprocess_this_prefix_in_tag_block() {
        let template = "{% if this.flag %}yes{% endif %}";
        let result = preprocess_this_prefix(template, "res").unwrap();
        assert_eq!(result, "{% if res.flag %}yes{% endif %}");
    }

    #[test]
    fn test_preprocess_this_prefix_with_filter() {
        let template = "{{ this.tags | from_json }}";
        let result = preprocess_this_prefix(template, "my_vpc").unwrap();
        assert_eq!(result, "{{ my_vpc.tags | from_json }}");
    }

    // ── End-to-end rendering tests via TemplateEngine ─────────────────────

    #[test]
    fn test_this_resolves_resource_scoped_over_global() {
        // When both a global 'fred' and a resource-scoped 'resource_name_x.fred'
        // exist, {{ this.fred }} must resolve to the resource-scoped value.
        let engine = TemplateEngine::new();
        let mut context = std::collections::HashMap::new();
        context.insert("fred".to_string(), "global_fred".to_string());
        context.insert(
            "resource_name_x.fred".to_string(),
            "scoped_fred".to_string(),
        );

        let expanded = preprocess_this_prefix("{{ this.fred }}", "resource_name_x").unwrap();
        let result = engine
            .render_with_filters("t", &expanded, &context)
            .unwrap();
        assert_eq!(
            result, "scoped_fred",
            "this.fred should resolve to the resource-scoped value, not the global"
        );
    }

    #[test]
    fn test_this_resolves_when_only_resource_scoped_exists() {
        // No global 'fred' - only the resource-scoped one.
        let engine = TemplateEngine::new();
        let mut context = std::collections::HashMap::new();
        context.insert(
            "resource_name_x.fred".to_string(),
            "scoped_only".to_string(),
        );

        let expanded = preprocess_this_prefix("{{ this.fred }}", "resource_name_x").unwrap();
        let result = engine
            .render_with_filters("t", &expanded, &context)
            .unwrap();
        assert_eq!(result, "scoped_only");
    }

    #[test]
    fn test_this_errors_when_only_global_exists_not_resource_scoped() {
        // this.fred expands to resource_name_x.fred; if only a global 'fred'
        // exists the render should fail rather than silently using the global.
        let engine = TemplateEngine::new();
        let mut context = std::collections::HashMap::new();
        context.insert("fred".to_string(), "global_fred".to_string());
        // No resource_name_x.fred in context

        let expanded = preprocess_this_prefix("{{ this.fred }}", "resource_name_x").unwrap();
        let result = engine.render_with_filters("t", &expanded, &context);
        assert!(
            result.is_err(),
            "this.fred should error when resource_name_x.fred is not in context"
        );
    }

    #[test]
    fn test_this_callback_resolves_same_as_scoped_and_shorthand() {
        // {{ this.callback.ProgressEvent.RequestToken }} inside resource_name_x
        // should resolve identically to:
        //   {{ resource_name_x.callback.ProgressEvent.RequestToken }}  (explicit)
        //   {{ callback.ProgressEvent.RequestToken }}                  (shorthand)
        let engine = TemplateEngine::new();
        let mut context = std::collections::HashMap::new();
        context.insert(
            "resource_name_x.callback.ProgressEvent.RequestToken".to_string(),
            "token-abc".to_string(),
        );
        context.insert(
            "callback.ProgressEvent.RequestToken".to_string(),
            "token-abc".to_string(),
        );

        let expanded = preprocess_this_prefix(
            "{{ this.callback.ProgressEvent.RequestToken }}",
            "resource_name_x",
        )
        .unwrap();

        let via_this = engine
            .render_with_filters("t1", &expanded, &context)
            .unwrap();
        let via_explicit = engine
            .render_with_filters(
                "t2",
                "{{ resource_name_x.callback.ProgressEvent.RequestToken }}",
                &context,
            )
            .unwrap();
        let via_shorthand = engine
            .render_with_filters("t3", "{{ callback.ProgressEvent.RequestToken }}", &context)
            .unwrap();

        assert_eq!(via_this, "token-abc");
        assert_eq!(
            via_this, via_explicit,
            "this.callback should equal resource_name_x.callback"
        );
        assert_eq!(
            via_this, via_shorthand,
            "this.callback should equal shorthand callback"
        );
    }
}