helm-schema-ir 0.0.4

Generate an accurate JSON schema for any helm chart
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
use crate::{CapabilityGuard, HelperBranch, HelperBranchBody};
use helm_schema_ast::DefineIndex;
use indoc::indoc;
use test_util::prelude::sim_assert_eq;

/// Test helper: extract literals from a Literals-bodied
/// `HelperBranch`. Panics if the branch is unexpectedly Nested
/// (tests that need to walk nested structure should match the
/// `body` field directly instead of using this helper).
fn literals_of(b: &HelperBranch) -> &[String] {
    match &b.body {
        HelperBranchBody::Literals { values } => values.as_slice(),
        HelperBranchBody::Nested { .. } => panic!("expected Literals-bodied branch; got {b:?}"),
    }
}

fn index_with(src: &str) -> DefineIndex {
    let mut idx = DefineIndex::new();
    idx.add_file_source("<inline:0>", src);
    idx
}

fn evaluate_helper(name: &str, helpers: &DefineIndex) -> HelperBranchBody {
    let analysis_db = crate::analysis_db::IrAnalysisDb::new(helpers);
    let Some(body) = analysis_db.parsed_helper_body(name) else {
        return HelperBranchBody::literals(Vec::new());
    };
    crate::resource_identity::HelperOutputEvaluator::default().evaluate_body(
        body.source,
        body.tree.root_node(),
        &analysis_db,
        0,
    )
}

#[test]
fn single_literal_helper_resolves() {
    let helpers = index_with(indoc! {r#"
        {{- define "x.apiVersion" -}}
        {{- print "apps/v1" -}}
        {{- end -}}
    "#});
    sim_assert_eq!(
        have: evaluate_helper("x.apiVersion", &helpers).all_literals(),
        want: vec!["apps/v1"]
    );
}

#[test]
fn if_else_helper_returns_both_branches() {
    let helpers = index_with(indoc! {r#"
        {{- define "rbac.apiVersion" -}}
        {{- if .Capabilities.APIVersions.Has "rbac.authorization.k8s.io/v1" }}
        {{- print "rbac.authorization.k8s.io/v1" -}}
        {{- else -}}
        {{- print "rbac.authorization.k8s.io/v1beta1" -}}
        {{- end -}}
        {{- end -}}
    "#});
    let outs = evaluate_helper("rbac.apiVersion", &helpers).all_literals();
    assert!(
        outs.contains(&"rbac.authorization.k8s.io/v1".to_string()),
        "must include modern; got {outs:?}"
    );
    assert!(
        outs.contains(&"rbac.authorization.k8s.io/v1beta1".to_string()),
        "must include legacy; got {outs:?}"
    );
}

#[test]
fn helper_with_values_reference_is_silent_about_dynamic_branch() {
    // grafana podDisruptionBudget shape: first branch is
    // Values-driven (unresolvable), other branches are literal.
    // We collect the literal branches and skip the dynamic one.
    let helpers = index_with(indoc! {r#"
        {{- define "grafana.podDisruptionBudget.apiVersion" -}}
        {{- if $.Values.podDisruptionBudget.apiVersion }}
        {{- print $.Values.podDisruptionBudget.apiVersion }}
        {{- else if $.Capabilities.APIVersions.Has "policy/v1/PodDisruptionBudget" }}
        {{- print "policy/v1" }}
        {{- else }}
        {{- print "policy/v1beta1" }}
        {{- end }}
        {{- end }}
    "#});
    let outs = evaluate_helper("grafana.podDisruptionBudget.apiVersion", &helpers).all_literals();
    assert!(
        outs.contains(&"policy/v1".to_string()),
        "must include policy/v1 literal branch; got {outs:?}"
    );
    assert!(
        outs.contains(&"policy/v1beta1".to_string()),
        "must include policy/v1beta1 literal branch; got {outs:?}"
    );
}

#[test]
fn unknown_helper_returns_empty() {
    let helpers = DefineIndex::new();
    sim_assert_eq!(
        have: evaluate_helper("nope", &helpers).all_literals(),
        want: Vec::<String>::new()
    );
}

#[test]
fn nested_helper_recurses_one_level() {
    let helpers = index_with(indoc! {r#"
        {{- define "outer" -}}
        {{- template "inner" . -}}
        {{- end -}}
        {{- define "inner" -}}
        {{- print "apps/v1" -}}
        {{- end -}}
    "#});
    sim_assert_eq!(
        have: evaluate_helper("outer", &helpers).all_literals(),
        want: vec!["apps/v1"]
    );
}

#[test]
fn cyclic_helper_does_not_stack_overflow() {
    let helpers = index_with(indoc! {r#"
        {{- define "a" -}}
        {{- template "b" . -}}
        {{- end -}}
        {{- define "b" -}}
        {{- template "a" . -}}
        {{- end -}}
    "#});
    // Either empty (cycle suppressed) — must NOT panic / overflow.
    let outs = evaluate_helper("a", &helpers).all_literals();
    assert!(
        outs.is_empty(),
        "cyclic helper must return empty, not infinite recursion; got {outs:?}"
    );
}

#[test]
fn typed_output_preserves_guard_and_branch_literals() {
    // The vendored RBAC-shaped helper: stable variant gated by
    // Capabilities.APIVersions.Has, legacy as else. The typed
    // output must split into two branches; one carrying the guard,
    // one unguarded (the else fallback).
    let helpers = index_with(indoc! {r#"
        {{- define "rbac.apiVersion" -}}
        {{- if .Capabilities.APIVersions.Has "rbac.authorization.k8s.io/v1" }}
        {{- print "rbac.authorization.k8s.io/v1" -}}
        {{- else -}}
        {{- print "rbac.authorization.k8s.io/v1beta1" -}}
        {{- end -}}
        {{- end -}}
    "#});
    let out = evaluate_helper("rbac.apiVersion", &helpers);
    let HelperBranchBody::Nested { branches } = out else {
        panic!("expected Nested; got {out:?}");
    };
    sim_assert_eq!(have: branches.len(), want: 2, "expected 2 branches; got {branches:?}");
    // First branch carries the CapabilityHas guard for the v1 API
    // and yields the modern literal.
    sim_assert_eq!(
        have: branches[0].guard,
        want: Some(CapabilityGuard::Has {
            api: "rbac.authorization.k8s.io/v1".to_string(),
        }),
        "branch[0] guard mismatch"
    );
    sim_assert_eq!(
        have: literals_of(&branches[0]),
        want: vec!["rbac.authorization.k8s.io/v1".to_string()]
    );
    // Second branch is the unguarded fallback yielding the legacy
    // literal.
    sim_assert_eq!(have: branches[1].guard, want: None, "branch[1] should be unguarded");
    sim_assert_eq!(
        have: literals_of(&branches[1]),
        want: vec!["rbac.authorization.k8s.io/v1beta1".to_string()]
    );
}

/// Typed branch structure survives through a wrapper helper that only
/// delegates via `{{ include "branched_inner" . }}`.
#[test]
fn typed_output_preserves_branches_through_wrapper_include() {
    let helpers = index_with(indoc! {r#"
        {{- define "outer.apiVersion" -}}
        {{- include "rbac.apiVersion" . -}}
        {{- end -}}
        {{- define "rbac.apiVersion" -}}
        {{- if .Capabilities.APIVersions.Has "rbac.authorization.k8s.io/v1" }}
        {{- print "rbac.authorization.k8s.io/v1" -}}
        {{- else -}}
        {{- print "rbac.authorization.k8s.io/v1beta1" -}}
        {{- end -}}
        {{- end -}}
    "#});
    let out = evaluate_helper("outer.apiVersion", &helpers);
    let HelperBranchBody::Nested { branches } = out else {
        panic!(
            "wrapper helper must preserve branched typed output from delegated callee; got {out:?}"
        );
    };
    sim_assert_eq!(have: branches.len(), want: 2, "expected 2 branches; got {branches:?}");
    sim_assert_eq!(
        have: branches[0].guard,
        want: Some(CapabilityGuard::Has {
            api: "rbac.authorization.k8s.io/v1".to_string(),
        }),
        "branch[0] guard must carry the CapabilityHas decoded from the inner helper"
    );
    sim_assert_eq!(
        have: literals_of(&branches[0]),
        want: vec!["rbac.authorization.k8s.io/v1".to_string()]
    );
    sim_assert_eq!(have: branches[1].guard, want: None);
    sim_assert_eq!(
        have: literals_of(&branches[1]),
        want: vec!["rbac.authorization.k8s.io/v1beta1".to_string()]
    );
}

/// Same shape, but with `template` (the bare-string variant of
/// the delegation keyword) instead of `include`. Both must
/// preserve branches identically.
#[test]
fn typed_output_preserves_branches_through_wrapper_template() {
    let helpers = index_with(indoc! {r#"
        {{- define "outer.apiVersion" -}}
        {{- template "rbac.apiVersion" . -}}
        {{- end -}}
        {{- define "rbac.apiVersion" -}}
        {{- if .Capabilities.APIVersions.Has "rbac.authorization.k8s.io/v1" }}
        {{- print "rbac.authorization.k8s.io/v1" -}}
        {{- else -}}
        {{- print "rbac.authorization.k8s.io/v1beta1" -}}
        {{- end -}}
        {{- end -}}
    "#});
    let out = evaluate_helper("outer.apiVersion", &helpers);
    assert!(
        matches!(out, HelperBranchBody::Nested { .. }),
        "wrapper via template must preserve Nested output; got {out:?}"
    );
}

/// Multi-level delegation chain: outer → middle → branched inner.
/// Branches must propagate through arbitrary wrapper depth (up to
/// the recursion guard).
#[test]
fn typed_output_preserves_branches_through_multi_level_wrapper() {
    let helpers = index_with(indoc! {r#"
        {{- define "outer" -}}
        {{- include "middle" . -}}
        {{- end -}}
        {{- define "middle" -}}
        {{- include "inner" . -}}
        {{- end -}}
        {{- define "inner" -}}
        {{- if .Capabilities.APIVersions.Has "policy/v1" }}
        {{- print "policy/v1" -}}
        {{- else -}}
        {{- print "policy/v1beta1" -}}
        {{- end -}}
        {{- end -}}
    "#});
    let out = evaluate_helper("outer", &helpers);
    let HelperBranchBody::Nested { branches } = out else {
        panic!("multi-level wrapper must preserve branched output; got {out:?}");
    };
    sim_assert_eq!(have: branches.len(), want: 2);
    sim_assert_eq!(
        have: branches[0].guard,
        want: Some(CapabilityGuard::Has {
            api: "policy/v1".to_string()
        })
    );
}

/// Typed branch structure composes through branch bodies, not just at the
/// top level. The shape:
///
/// ```text
/// {{- define "outer" -}}
/// {{- if .Capabilities.APIVersions.Has "A" -}}
/// {{- include "branched_inner" . -}}    {{- /* nested-branched delegation */ -}}
/// {{- else -}}
/// fallback
/// {{- end -}}
/// {{- end -}}
/// ```
///
/// must yield nested output whose first branch carries a `Nested`
/// body holding the inner helper's typed branches, NOT flatten the
/// inner branches to a `Literals` body. This
/// preserves the inner Has-B guard so the chain can recurse
/// through both A and B at evaluation time.
#[test]
fn typed_output_preserves_nested_branches_through_branch_body_delegation() {
    let helpers = index_with(indoc! {r#"
        {{- define "outer" -}}
        {{- if .Capabilities.APIVersions.Has "A" -}}
        {{- include "inner" . -}}
        {{- else -}}
        {{- print "fallback" -}}
        {{- end -}}
        {{- end -}}
        {{- define "inner" -}}
        {{- if .Capabilities.APIVersions.Has "B" -}}
        {{- print "b" -}}
        {{- else -}}
        {{- print "b_legacy" -}}
        {{- end -}}
        {{- end -}}
    "#});
    let out = evaluate_helper("outer", &helpers);
    let HelperBranchBody::Nested { branches } = out else {
        panic!("outer must be Nested; got {out:?}");
    };
    sim_assert_eq!(have: branches.len(), want: 2, "expected 2 outer branches");

    // First branch: Has A guard + Nested body (the inner helper's branches).
    sim_assert_eq!(
        have: branches[0].guard,
        want: Some(CapabilityGuard::Has {
            api: "A".to_string()
        }),
    );
    let HelperBranchBody::Nested { branches: nested } = &branches[0].body else {
        panic!(
            "branch[0].body must be Nested to preserve inner Has-B guard; got {:?}",
            branches[0].body
        );
    };
    sim_assert_eq!(have: nested.len(), want: 2, "inner helper should contribute 2 branches");
    sim_assert_eq!(
        have: nested[0].guard,
        want: Some(CapabilityGuard::Has {
            api: "B".to_string()
        }),
        "nested branch[0] must preserve the inner Has-B guard"
    );
    sim_assert_eq!(have: literals_of(&nested[0]), want: vec!["b".to_string()]);
    sim_assert_eq!(have: nested[1].guard, want: None);
    sim_assert_eq!(have: literals_of(&nested[1]), want: vec!["b_legacy".to_string()]);

    // Second branch: unguarded else + flat literal payload.
    sim_assert_eq!(have: branches[1].guard, want: None);
    sim_assert_eq!(have: literals_of(&branches[1]), want: vec!["fallback".to_string()]);
}

/// The same nested-branch structure is preserved when the nested branch is
/// inline rather than delegated through `include`.
#[test]
fn typed_output_preserves_nested_branches_through_inline_nested_if() {
    let helpers = index_with(indoc! {r#"
        {{- define "outer" -}}
        {{- if .Capabilities.APIVersions.Has "A" -}}
        {{- if .Capabilities.APIVersions.Has "B" -}}
        {{- print "b" -}}
        {{- else -}}
        {{- print "b_legacy" -}}
        {{- end -}}
        {{- else -}}
        {{- print "fallback" -}}
        {{- end -}}
        {{- end -}}
    "#});
    let out = evaluate_helper("outer", &helpers);
    let HelperBranchBody::Nested { branches } = out else {
        panic!("outer must be Nested; got {out:?}");
    };
    sim_assert_eq!(have: branches.len(), want: 2);
    let HelperBranchBody::Nested { branches: nested } = &branches[0].body else {
        panic!(
            "inline nested if must produce Nested body; got {:?}",
            branches[0].body
        );
    };
    sim_assert_eq!(have: nested.len(), want: 2);
    sim_assert_eq!(
        have: nested[0].guard,
        want: Some(CapabilityGuard::Has {
            api: "B".to_string()
        })
    );
}

/// Wrapper helper that mixes a delegation with other content must
/// NOT promote the callee's branches — the wrapper's output isn't
/// equivalent to the callee's output any more (the prefix changes
/// the rendered string). Fall through to the flat path, which
/// already conservatively collects literals as candidates.
#[test]
fn wrapper_with_mixed_content_does_not_promote_branches() {
    let helpers = index_with(indoc! {r#"
        {{- define "outer" -}}
        prefix-{{ include "inner" . }}
        {{- end -}}
        {{- define "inner" -}}
        {{- if .Capabilities.APIVersions.Has "X" }}
        {{- print "X" -}}
        {{- else -}}
        {{- print "Y" -}}
        {{- end -}}
        {{- end -}}
    "#});
    let out = evaluate_helper("outer", &helpers);
    // The mixed-content wrapper is NOT a pure delegation — the
    // typed nested form doesn't fit. Fall back to flat.
    assert!(
        matches!(out, HelperBranchBody::Literals { .. }),
        "mixed-content wrapper must fall through to flat literals; got {out:?}"
    );
}

/// Wrapper indirection must respect the same cycle guard the
/// flat-literal recursion uses — a cyclic helper graph must not
/// stack-overflow the typed-branch extractor.
#[test]
fn wrapper_cycle_falls_through_safely() {
    let helpers = index_with(indoc! {r#"
        {{- define "a" -}}
        {{- include "b" . -}}
        {{- end -}}
        {{- define "b" -}}
        {{- include "a" . -}}
        {{- end -}}
    "#});
    let out = evaluate_helper("a", &helpers);
    // Cycle → no branches discoverable → fall through to
    // Literals (which will also be empty per the existing
    // cycle guard in collect_literals).
    assert!(
        matches!(out, HelperBranchBody::Literals { .. }),
        "cyclic wrapper chain must fall through cleanly; got {out:?}"
    );
}

#[test]
fn typed_output_preserves_elif_chain() {
    // Three-way chain: Values guard (opaque), CapabilityHas guard
    // (decoded), unguarded fallback.
    let helpers = index_with(indoc! {r#"
        {{- define "grafana.pdb.apiVersion" -}}
        {{- if $.Values.podDisruptionBudget.apiVersion }}
        {{- print $.Values.podDisruptionBudget.apiVersion }}
        {{- else if $.Capabilities.APIVersions.Has "policy/v1/PodDisruptionBudget" }}
        {{- print "policy/v1" }}
        {{- else }}
        {{- print "policy/v1beta1" }}
        {{- end }}
        {{- end }}
    "#});
    let out = evaluate_helper("grafana.pdb.apiVersion", &helpers);
    let HelperBranchBody::Nested { branches } = out else {
        panic!("expected Nested; got {out:?}");
    };
    // Values-guarded output is not a literal apiVersion candidate, but
    // later capability branches still keep their structural guards.
    let has_branch = branches.iter().find(|b| {
        matches!(
            &b.guard,
            Some(CapabilityGuard::Has { api }) if api == "policy/v1/PodDisruptionBudget"
        )
    });
    assert!(
        has_branch.is_some(),
        "expected CapabilityHas branch; got {branches:?}"
    );
    sim_assert_eq!(
        have: literals_of(has_branch.unwrap()),
        want: vec!["policy/v1".to_string()]
    );
    // Final unguarded branch carries the legacy fallback.
    let else_branch = branches.iter().find(|b| b.guard.is_none());
    assert!(
        else_branch.is_some(),
        "expected unguarded else branch; got {branches:?}"
    );
    sim_assert_eq!(
        have: literals_of(else_branch.unwrap()),
        want: vec!["policy/v1beta1".to_string()]
    );
}

/// `printf "%s/%s" "apps" "v1"` is exact compositional formatting; the
/// shared expression interpreter should resolve it without the resource
/// helper evaluator carrying a separate printf mini-parser.
#[test]
fn printf_compositional_format_resolves_exactly() {
    let helpers = index_with(indoc! {r#"
        {{- define "x.apiVersion" -}}
        {{- printf "%s/%s" "apps" "v1" -}}
        {{- end -}}
    "#});
    sim_assert_eq!(
        have: evaluate_helper("x.apiVersion", &helpers).all_literals(),
        want: vec!["apps/v1"]
    );
}

/// `printf "%s" "X"` is the one substitution shape we DO model
/// exactly: a single `%s` placeholder + a single string-literal
/// arg evaluates to the arg.
#[test]
fn printf_single_substitution_resolves_to_arg() {
    let helpers = index_with(indoc! {r#"
        {{- define "x.apiVersion" -}}
        {{- printf "%s" "apps/v1" -}}
        {{- end -}}
    "#});
    sim_assert_eq!(
        have: evaluate_helper("x.apiVersion", &helpers).all_literals(),
        want: vec!["apps/v1"]
    );
}

/// `printf "X"` with no substitutions evaluates to the literal.
#[test]
fn printf_no_substitution_resolves_to_format() {
    let helpers = index_with(indoc! {r#"
        {{- define "x.apiVersion" -}}
        {{- printf "apps/v1" -}}
        {{- end -}}
    "#});
    sim_assert_eq!(
        have: evaluate_helper("x.apiVersion", &helpers).all_literals(),
        want: vec!["apps/v1"]
    );
}

/// `printf "%d" .x` uses a non-`%s` directive — refuse to model.
#[test]
fn printf_non_string_directive_emits_no_candidates() {
    let helpers = index_with(indoc! {r#"
        {{- define "x.apiVersion" -}}
        {{- printf "%d" 1 -}}
        {{- end -}}
    "#});
    let outs = evaluate_helper("x.apiVersion", &helpers).all_literals();
    assert!(
        outs.is_empty(),
        "non-%s printf directive must emit no candidates; got {outs:?}"
    );
}

#[test]
fn default_choice_does_not_emit_fallback_as_exact_identity() {
    let helpers = index_with(indoc! {r#"
        {{- define "x.apiVersion" -}}
        {{- default "policy/v1beta1" "policy/v1" -}}
        {{- end -}}
    "#});
    let outs = evaluate_helper("x.apiVersion", &helpers).all_literals();
    assert!(
        outs.is_empty(),
        "resource identity literal evaluation must abstain on expression choices; got {outs:?}"
    );
}

/// `quote "X"` should produce the inner literal "X" (without the
/// added quote wrapping — for apiVersion resolution we want the
/// raw value).
#[test]
fn quote_with_single_string_arg_resolves() {
    let helpers = index_with(indoc! {r#"
        {{- define "x.apiVersion" -}}
        {{- quote "apps/v1" -}}
        {{- end -}}
    "#});
    sim_assert_eq!(
        have: evaluate_helper("x.apiVersion", &helpers).all_literals(),
        want: vec!["apps/v1"]
    );
}

/// `print` with multiple literal args is exact string concatenation.
#[test]
fn print_with_multiple_literal_args_resolves_exactly() {
    let helpers = index_with(indoc! {r#"
        {{- define "x.apiVersion" -}}
        {{- print "apps/" "v1" -}}
        {{- end -}}
    "#});
    sim_assert_eq!(
        have: evaluate_helper("x.apiVersion", &helpers).all_literals(),
        want: vec!["apps/v1"]
    );
}

/// `"X" | quote` pipeline: the seed literal is passed through the
/// identity-shaped `quote` stage; result is the seed.
#[test]
fn pipeline_seed_literal_then_quote_resolves_to_seed() {
    let helpers = index_with(indoc! {r#"
        {{- define "x.apiVersion" -}}
        {{- "apps/v1" | quote -}}
        {{- end -}}
    "#});
    sim_assert_eq!(
        have: evaluate_helper("x.apiVersion", &helpers).all_literals(),
        want: vec!["apps/v1"]
    );
}

#[test]
fn single_literal_helper_is_not_branched() {
    let helpers = index_with(indoc! {r#"
        {{- define "x.apiVersion" -}}
        {{- print "apps/v1" -}}
        {{- end -}}
    "#});
    let out = evaluate_helper("x.apiVersion", &helpers);
    assert!(
        matches!(out, HelperBranchBody::Literals { .. }),
        "single-literal helper must not be Nested; got {out:?}"
    );
}

/// datadog `policy.poddisruptionbudget.apiVersion`: a helper body that
/// writes its literal as a QUOTED YAML scalar (`"policy/v1"`, quotes
/// included) denotes the unquoted scalar once the composed manifest is
/// parsed, so provider lookup must see `policy/v1`.
#[test]
fn quoted_text_literals_decode_as_yaml_scalars() {
    let helpers = index_with(indoc! {r#"
        {{- define "policy.pdb.apiVersion" -}}
        {{- if .Capabilities.APIVersions.Has "policy/v1/PodDisruptionBudget" -}}
        "policy/v1"
        {{- else -}}
        "policy/v1beta1"
        {{- end -}}
        {{- end -}}
    "#});
    let out = evaluate_helper("policy.pdb.apiVersion", &helpers);
    let HelperBranchBody::Nested { branches } = out else {
        panic!("expected Nested; got {out:?}");
    };
    sim_assert_eq!(have: branches.len(), want: 2, "expected 2 branches; got {branches:?}");
    sim_assert_eq!(
        have: literals_of(&branches[0]),
        want: vec!["policy/v1".to_string()]
    );
    sim_assert_eq!(
        have: literals_of(&branches[1]),
        want: vec!["policy/v1beta1".to_string()]
    );
}