ggen-core 26.7.2

Core graph-aware code generation engine
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
#![allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::needless_raw_string_hashes,
    clippy::duration_suboptimal_units,
    clippy::branches_sharing_code,
    clippy::used_underscore_binding,
    clippy::single_char_pattern,
    clippy::ignore_without_reason,
    clippy::cloned_ref_to_slice_refs,
    clippy::doc_overindented_list_items,
    clippy::match_wildcard_for_single_variants,
    clippy::ignored_unit_patterns,
    clippy::needless_collect,
    clippy::unnecessary_map_or,
    clippy::manual_flatten,
    clippy::manual_strip,
    clippy::future_not_send,
    clippy::unnested_or_patterns,
    clippy::no_effect_underscore_binding,
    clippy::literal_string_with_formatting_args
)]
#![allow(
    dead_code,
    unused_imports,
    unused_variables,
    deprecated,
    clippy::all,
    unused_mut
)]

/// Template variable validation tests.
///
/// These tests verify that Tera templates only reference variables that are
/// either provided by SPARQL queries or have default values. This catches
/// template/query mismatches before runtime rendering failures.
use regex::Regex;
use std::collections::HashSet;
use walkdir::WalkDir;

fn workspace_root() -> std::path::PathBuf {
    let mut p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    p.pop(); // crates/ggen-core -> crates
    p.pop(); // crates -> ggen root
    p
}

/// Extract all variable names referenced in a Tera template.
/// Matches {{ var }}, {{ var.filter }}, {{ var.key }}, {{ arr[index] }}, etc.
fn extract_template_variables(content: &str) -> Vec<String> {
    let mut vars = HashSet::new();

    // Match {{ expression }} patterns
    let expr_re = Regex::new(r#"\{\{\s*([\w.\[\]"']+)\s*(?:\|.*?)?\s*\}\}"#).unwrap();
    for cap in expr_re.captures_iter(content) {
        let expr = cap[1].trim();
        // Skip string literals
        if expr.starts_with('"') || expr.starts_with('\'') {
            continue;
        }
        // Extract the root variable name (before any . or [)
        let root = expr.split(['.', '[']).next().unwrap_or(expr);
        if !root.is_empty() && root.chars().all(|c| c.is_alphanumeric() || c == '_') {
            vars.insert(root.to_string());
        }
    }

    // Match {% for var in expr %} patterns
    let for_re = Regex::new(r"\{%\s*for\s+(\w+)\s+in\s+([\w.\[\]]+)\s*%\}").unwrap();
    for cap in for_re.captures_iter(content) {
        let collection = cap[2].trim();
        let root = collection.split(['.', '[']).next().unwrap_or(collection);
        if !root.is_empty() && root.chars().all(|c| c.is_alphanumeric() || c == '_') {
            vars.insert(root.to_string());
        }
    }

    // Match {% if var ... %} patterns
    let if_re = Regex::new(r"\{%\s*if\s+(\w+)").unwrap();
    for cap in if_re.captures_iter(content) {
        vars.insert(cap[1].trim().to_string());
    }

    vars.into_iter().collect()
}

/// Check if a variable reference has a default() filter.
fn has_default_filter(content: &str, var: &str) -> bool {
    // Look for var | default( or var|default(
    let pattern1 = format!("{} | default(", var);
    let pattern2 = format!("{}|default(", var);
    content.contains(&pattern1) || content.contains(&pattern2)
}

/// Extract SELECT variable names from a SPARQL query.
fn extract_sparql_variables(content: &str) -> HashSet<String> {
    let mut vars = HashSet::new();

    // Match SELECT ?var patterns (handles multi-line SELECT clauses)
    let select_re = Regex::new(r"(?s)SELECT\s+(?:DISTINCT\s+)?(.+?)\s*WHERE").unwrap();
    if let Some(cap) = select_re.captures(content) {
        let select_clause = &cap[1];
        for word in select_clause.split_whitespace() {
            if word.starts_with('?') {
                vars.insert(word.trim_start_matches('?').to_string());
            }
        }
    }

    vars
}

// ---------------------------------------------------------------------------
// Test 1: All .tera templates have valid variable references
// ---------------------------------------------------------------------------

/// Variables that are injected by the ggen pipeline (not from SPARQL).
const PIPELINE_INJECTED_VARS: &[&str] = &[
    "sparql_results", // Always injected from SPARQL query results
    "name",           // Template naming convention
];

/// Variables provided by the ggen.toml rule configuration (not from SPARQL).
const RULE_CONFIG_VARS: &[&str] = &[
    "server_name",
    "server_version",
    "server_description",
    "transport_type",
    "agent_name",
    "agent_version",
    "agent_description",
    "agent_url",
    "provider_name",
    "provider_url",
    "package_name",
    "agent_port",
];

#[test]
fn test_templates_reference_valid_variables() {
    let root = workspace_root();
    let templates_dir = root.join("templates");

    if !templates_dir.exists() {
        return;
    }

    let mut errors = Vec::new();
    let mut checked = 0u32;

    for entry in WalkDir::new(&templates_dir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.path().extension().map_or(false, |ext| ext == "tera"))
    {
        let content = match std::fs::read_to_string(entry.path()) {
            Ok(c) => c,
            Err(e) => {
                errors.push(format!("READ ERROR {}: {}", entry.path().display(), e));
                continue;
            }
        };

        let template_vars = extract_template_variables(&content);
        checked += 1;

        for var in &template_vars {
            // Skip if it's a pipeline-injected variable
            if PIPELINE_INJECTED_VARS.contains(&var.as_str()) {
                continue;
            }

            // Skip if it's a rule configuration variable
            if RULE_CONFIG_VARS.contains(&var.as_str()) {
                continue;
            }

            // Skip loop variables (defined by {% for %})
            let for_re = Regex::new(r"\{%\s*for\s+(\w+)").unwrap();
            if for_re.is_match(&content)
                && for_re.captures(&content).map_or(false, |caps| {
                    caps.iter()
                        .skip(1)
                        .any(|c| c.map_or(false, |m| m.as_str() == var))
                })
            {
                continue;
            }

            // Skip if it has a default filter
            if has_default_filter(&content, var) {
                continue;
            }

            // Skip Tera built-in functions/filters
            let tera_builtins = [
                "range", "loop", "now", "self", "super", "true", "false", "null", "none",
            ];
            if tera_builtins.contains(&var.as_str()) {
                continue;
            }

            // Skip if it's an index variable (numeric)
            if var.parse::<usize>().is_ok() {
                continue;
            }

            // At this point, the variable is suspicious — it may not be available at render time
            // Report as warning, not error (some templates use conditional logic)
            eprintln!(
                "[WARN] {} references potentially undefined variable: {}",
                entry.path().display(),
                var
            );
        }
    }

    assert!(checked > 0, "Should find .tera files in templates/");
    if !errors.is_empty() {
        panic!("Template validation errors:\n{}", errors.join("\n"));
    }
}

// ---------------------------------------------------------------------------
// Test 2: SPARQL queries used by ggen.toml rules return expected columns
// ---------------------------------------------------------------------------

/// Map of query file -> expected output columns (from ggen.toml rules).
const QUERY_EXPECTED_COLUMNS: &[(&str, &[&str])] = &[
    (
        "crates/ggen-core/queries/mcp/extract-mcp-full.rq",
        &[
            "server_name",
            "server_version",
            "server_description",
            "transport_type",
            "tool_name",
            "tool_description",
        ],
    ),
    (
        "crates/ggen-core/queries/a2a/extract-a2a-full.rq",
        &[
            "agent_name",
            "agent_version",
            "agent_description",
            "agent_url",
            "provider_name",
            "skill_name",
            "skill_description",
            "skill_tags",
            "streaming",
            "timeout_ms",
            "retry_policy",
        ],
    ),
];

#[test]
fn test_sparql_queries_return_expected_columns() {
    let root = workspace_root();
    let mut errors = Vec::new();

    for (rq_rel, expected_cols) in QUERY_EXPECTED_COLUMNS {
        let path = root.join(rq_rel);
        if !path.exists() {
            continue;
        }

        let content = match std::fs::read_to_string(&path) {
            Ok(c) => c,
            Err(e) => {
                errors.push(format!("READ ERROR {}: {}", path.display(), e));
                continue;
            }
        };

        let actual_cols = extract_sparql_variables(&content);
        let expected_set: HashSet<&str> = expected_cols.iter().copied().collect();

        for col in &expected_set {
            if !actual_cols.contains(*col) {
                errors.push(format!("Query {} missing expected column: {}", rq_rel, col));
            }
        }
    }

    if !errors.is_empty() {
        panic!(
            "SPARQL query column validation errors:\n{}",
            errors.join("\n")
        );
    }
}

// ---------------------------------------------------------------------------
// Test 3: Template variables match SPARQL query columns
// ---------------------------------------------------------------------------

/// Map of template -> SPARQL query it depends on (from ggen.toml rules).
const TEMPLATE_QUERY_PAIRS: &[(&str, &str, &[&str])] = &[
    // (template, query, sparql_result row variables used by template)
    (
        "templates/mcp-rust.tera",
        "crates/ggen-core/queries/mcp/extract-mcp-full.rq",
        &["tool_name", "tool_description"],
    ),
    (
        "templates/mcp-elixir.tera",
        "crates/ggen-core/queries/mcp/extract-mcp-full.rq",
        &["tool_name", "tool_description"],
    ),
    (
        "templates/mcp-go.tera",
        "crates/ggen-core/queries/mcp/extract-mcp-full.rq",
        &["tool_name", "tool_description"],
    ),
    (
        "templates/mcp-typescript.tera",
        "crates/ggen-core/queries/mcp/extract-mcp-full.rq",
        &["tool_name", "tool_description"],
    ),
    (
        "templates/mcp-java.tera",
        "crates/ggen-core/queries/mcp/extract-mcp-full.rq",
        &["tool_name", "tool_description"],
    ),
    (
        "templates/a2a-rust.tera",
        "crates/ggen-core/queries/a2a/extract-a2a-full.rq",
        &[
            "skill_name",
            "skill_description",
            "skill_tags",
            "streaming",
            "timeout_ms",
        ],
    ),
    (
        "templates/a2a-go.tera",
        "crates/ggen-core/queries/a2a/extract-a2a-full.rq",
        &[
            "skill_name",
            "skill_description",
            "skill_tags",
            "streaming",
            "timeout_ms",
        ],
    ),
    (
        "templates/a2a-typescript.tera",
        "crates/ggen-core/queries/a2a/extract-a2a-full.rq",
        &[
            "skill_name",
            "skill_description",
            "skill_tags",
            "streaming",
            "timeout_ms",
        ],
    ),
    (
        "templates/a2a-java.tera",
        "crates/ggen-core/queries/a2a/extract-a2a-full.rq",
        &[
            "skill_name",
            "skill_description",
            "skill_tags",
            "streaming",
            "timeout_ms",
        ],
    ),
    (
        "templates/a2a-elixir.tera",
        "crates/ggen-core/queries/a2a/extract-a2a-full.rq",
        &[
            "skill_name",
            "skill_description",
            "skill_tags",
            "streaming",
            "timeout_ms",
        ],
    ),
];

#[test]
fn test_template_variables_match_sparql_columns() {
    let root = workspace_root();
    let mut errors = Vec::new();

    for (tmpl_rel, rq_rel, expected_row_vars) in TEMPLATE_QUERY_PAIRS {
        let tmpl_path = root.join(tmpl_rel);
        let rq_path = root.join(rq_rel);

        if !tmpl_path.exists() || !rq_path.exists() {
            continue;
        }

        let tmpl_content = match std::fs::read_to_string(&tmpl_path) {
            Ok(c) => c,
            Err(_) => continue,
        };

        let rq_content = match std::fs::read_to_string(&rq_path) {
            Ok(c) => c,
            Err(_) => continue,
        };

        let query_cols = extract_sparql_variables(&rq_content);

        for var in *expected_row_vars {
            if !query_cols.contains(*var) {
                errors.push(format!(
                    "Template {} uses row.{} but query {} doesn't return ?{}",
                    tmpl_rel, var, rq_rel, var
                ));
            }

            // Also check the template references the variable
            let row_var = format!("row.{}", var);
            if !tmpl_content.contains(&row_var) {
                eprintln!(
                    "[INFO] Template {} doesn't reference row.{} (may use it conditionally)",
                    tmpl_rel, var
                );
            }
        }
    }

    if !errors.is_empty() {
        panic!(
            "Template/query variable mismatch errors:\n{}",
            errors.join("\n")
        );
    }
}

// ---------------------------------------------------------------------------
// Test 4: No template uses both `tools` and `sparql_results` (migration completeness)
// ---------------------------------------------------------------------------

#[test]
fn test_no_template_uses_both_tools_and_sparql_results() {
    let root = workspace_root();
    let templates_dir = root.join("templates");

    if !templates_dir.exists() {
        return;
    }

    let mut conflicts = Vec::new();

    for entry in WalkDir::new(&templates_dir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.path().extension().map_or(false, |ext| ext == "tera"))
    {
        let content = match std::fs::read_to_string(entry.path()) {
            Ok(c) => c,
            Err(_) => continue,
        };

        let uses_tools = content.contains("for tool in tools")
            || content.contains("tools | length")
            || content.contains("for skill in skills")
            || content.contains("skills | length");

        let uses_sparql = content.contains("for row in sparql_results")
            || content.contains("sparql_results | length");

        if uses_tools && uses_sparql {
            conflicts.push(format!(
                "{} uses both `tools`/`skills` and `sparql_results`",
                entry.path().display()
            ));
        }
    }

    if !conflicts.is_empty() {
        panic!(
            "Templates with conflicting variable usage:\n{}",
            conflicts.join("\n")
        );
    }
}

// ---------------------------------------------------------------------------
// Test 5: All original templates migrated to sparql_results pattern
// ---------------------------------------------------------------------------

/// Templates that were originally using `tools`/`skills` arrays.
const ORIGINAL_TEMPLATES: &[&str] = &[
    "templates/mcp-rust.tera",
    "templates/mcp-elixir.tera",
    "templates/mcp-go.tera",
    "templates/mcp-typescript.tera",
    "templates/mcp-java.tera",
    "templates/a2a-rust.tera",
    "templates/a2a-go.tera",
    "templates/a2a-typescript.tera",
    "templates/a2a-java.tera",
    "templates/a2a-elixir.tera",
];

#[test]
fn test_all_original_templates_use_sparql_results() {
    let root = workspace_root();
    let mut not_migrated = Vec::new();

    for tmpl_rel in ORIGINAL_TEMPLATES {
        let path = root.join(tmpl_rel);
        if !path.exists() {
            continue;
        }

        let content = match std::fs::read_to_string(&path) {
            Ok(c) => c,
            Err(_) => continue,
        };

        let uses_sparql = content.contains("for row in sparql_results")
            || content.contains("sparql_results | length");

        if !uses_sparql {
            not_migrated.push(tmpl_rel.to_string());
        }
    }

    // Report but don't fail — templates are being migrated by background agents
    for tmpl in &not_migrated {
        eprintln!(
            "[WARN] Template not yet migrated to sparql_results: {}",
            tmpl
        );
    }

    // This assertion will flip to panic once all templates are migrated
    if not_migrated.len() > 3 {
        panic!(
            "Too many templates not yet migrated to sparql_results ({}):\n{}",
            not_migrated.len(),
            not_migrated.join("\n")
        );
    }
}