greentic-setup 0.4.28

End-to-end bundle setup engine for the Greentic platform — pack discovery, QA-driven configuration, secrets persistence, and bundle lifecycle management
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
//! QA-aware setup wizard that unifies WASM-based `qa-spec` and legacy
//! `setup.yaml` into a single FormSpec-driven flow.
//!
//! Provides both interactive CLI prompts and Adaptive Card rendering
//! for collecting provider configuration answers.

use std::path::Path;

use anyhow::{Result, anyhow};
use qa_spec::spec::form::ProgressPolicy;
use qa_spec::{FormSpec, VisibilityMode, build_render_payload, render_card, resolve_visibility};
use serde_json::{Map as JsonMap, Value};

use crate::setup_input::SetupInputAnswers;
use crate::setup_to_formspec;

// Re-exports for backward compatibility (these are the public API)
pub use crate::qa::prompts::{
    answer_satisfies_question, ask_form_spec_question, has_required_questions, matches_pattern,
    parse_typed_value, prompt_form_spec_answers, prompt_form_spec_answers_with_existing,
};
pub use crate::qa::shared_questions::{
    ProviderFormSpec, SHARED_QUESTION_IDS, SharedQuestionsResult, build_provider_form_specs,
    collect_shared_questions, merge_shared_with_provider_answers, prompt_shared_questions,
};

// Internal imports for use within this module (aliased to avoid conflicts with re-exports)
use crate::qa::prompts::{
    ask_form_spec_question as prompt_question, has_required_questions as check_required,
    matches_pattern as pattern_match, prompt_form_spec_answers as do_prompt_answers,
    prompt_form_spec_answers_with_existing as do_prompt_with_existing,
};
use crate::qa::shared_questions::merge_shared_with_provider_answers as merge_answers;

/// Run the QA setup wizard for a provider pack.
///
/// Builds a `FormSpec` from `setup.yaml` (or uses a pre-built one from a
/// component `qa-spec` invocation), then collects and validates answers.
///
/// Returns `(answers, form_spec)` where `form_spec` is `Some` if one was found.
pub fn run_qa_setup(
    pack_path: &Path,
    provider_id: &str,
    setup_input: Option<&SetupInputAnswers>,
    interactive: bool,
    qa_form_spec: Option<FormSpec>,
    advanced: bool,
) -> Result<(Value, Option<FormSpec>)> {
    let form_spec =
        qa_form_spec.or_else(|| setup_to_formspec::pack_to_form_spec(pack_path, provider_id));

    let answers = if let Some(input) = setup_input {
        if let Some(value) = input.answers_for_provider(provider_id) {
            let mut answers = crate::setup_input::ensure_object(value.clone())?;
            if let Some(ref spec) = form_spec {
                // Check for missing required fields and prompt if needed
                let missing = find_missing_required_fields(spec, &answers);
                if !missing.is_empty() {
                    let display = setup_to_formspec::strip_domain_prefix(provider_id);
                    println!("\n⚠️  Missing required fields for {display}. Please provide values:");
                    answers = prompt_for_missing_fields(spec, &answers, &missing)?;
                }
                validate_answers_against_form_spec(spec, &answers)?;
            }
            answers
        } else if check_required(form_spec.as_ref()) {
            return Err(anyhow!("setup input missing answers for {provider_id}"));
        } else {
            Value::Object(JsonMap::new())
        }
    } else if let Some(ref spec) = form_spec {
        if spec.questions.is_empty() {
            Value::Object(JsonMap::new())
        } else if interactive {
            do_prompt_answers(spec, provider_id, advanced)?
        } else {
            return Err(anyhow!(
                "setup answers required for {provider_id} but run is non-interactive"
            ));
        }
    } else {
        Value::Object(JsonMap::new())
    };

    Ok((answers, form_spec))
}

/// Render a QA setup step as an Adaptive Card v1.3.
///
/// Returns `(card_json, next_question_id)` where `next_question_id` is `None`
/// when all visible questions have been answered.
pub fn render_qa_card(form_spec: &FormSpec, answers: &Value) -> (Value, Option<String>) {
    let mut spec = form_spec.clone();
    spec.progress_policy = Some(
        spec.progress_policy
            .map(|mut p| {
                p.skip_answered = true;
                p
            })
            .unwrap_or(ProgressPolicy {
                skip_answered: true,
                ..ProgressPolicy::default()
            }),
    );

    let ctx = serde_json::json!({});
    let payload = build_render_payload(&spec, &ctx, answers);
    let next_id = payload.next_question_id.clone();
    let mut card = render_card(&payload);

    // Ensure Action.Submit has an `id` field for the REPL's @click.
    if let Some(actions) = card.get_mut("actions").and_then(Value::as_array_mut) {
        for action in actions.iter_mut() {
            if action.get("id").is_none() {
                action["id"] = Value::String("submit".into());
            }
        }
    }

    (card, next_id)
}

/// Validate answers against a FormSpec, checking required fields and constraints.
///
/// Questions with `visible_if` expressions that evaluate to `false` are skipped.
pub fn validate_answers_against_form_spec(spec: &FormSpec, answers: &Value) -> Result<()> {
    let map = answers
        .as_object()
        .ok_or_else(|| anyhow!("setup answers must be an object"))?;

    let visibility = resolve_visibility(spec, answers, VisibilityMode::Visible);

    for question in &spec.questions {
        let visible = visibility.get(&question.id).copied().unwrap_or(true);
        if !visible {
            continue;
        }

        if question.required {
            match map.get(&question.id) {
                Some(value) if !value.is_null() => {}
                _ => {
                    return Err(anyhow!(
                        "missing required setup answer for '{}'{}",
                        question.id,
                        question
                            .description
                            .as_ref()
                            .map(|d| format!(" ({d})"))
                            .unwrap_or_default()
                    ));
                }
            }
        }

        if let Some(value) = map.get(&question.id)
            && let Some(s) = value.as_str()
            && let Some(ref constraint) = question.constraint
            && let Some(ref pattern) = constraint.pattern
            && !pattern_match(s, pattern)
        {
            return Err(anyhow!(
                "answer for '{}' does not match pattern: {}",
                question.id,
                pattern
            ));
        }
    }

    Ok(())
}

/// Compute the visibility map for a FormSpec given the current answers.
///
/// Returns a map of `question_id → visible`. Questions without `visible_if`
/// default to visible.
pub fn compute_visibility(spec: &FormSpec, answers: &Value) -> qa_spec::VisibilityMap {
    resolve_visibility(spec, answers, VisibilityMode::Visible)
}

/// Run QA setup for a provider with pre-filled shared answers.
///
/// This is a convenience wrapper around `run_qa_setup` that merges shared
/// answers with provider-specific answers from `setup_input`.
///
/// When using `--answers` file (non-interactive mode), if any required fields
/// are missing or empty, the user will be prompted to fill them in.
pub fn run_qa_setup_with_shared(
    pack_path: &Path,
    provider_id: &str,
    setup_input: Option<&SetupInputAnswers>,
    interactive: bool,
    qa_form_spec: Option<FormSpec>,
    advanced: bool,
    shared_answers: &Value,
) -> Result<(Value, Option<FormSpec>)> {
    let form_spec =
        qa_form_spec.or_else(|| setup_to_formspec::pack_to_form_spec(pack_path, provider_id));

    // Merge shared answers with provider-specific answers from setup_input
    let merged_initial = merge_answers(
        shared_answers,
        setup_input.and_then(|i| i.answers_for_provider(provider_id)),
    );

    let answers = if let Some(ref spec) = form_spec {
        if spec.questions.is_empty() {
            Value::Object(JsonMap::new())
        } else if interactive {
            // Prompt with merged initial answers (shared + provider-specific)
            do_prompt_with_existing(spec, provider_id, advanced, &merged_initial)?
        } else {
            // Non-interactive: check for missing required fields
            let mut answers = crate::setup_input::ensure_object(merged_initial)?;
            let missing = find_missing_required_fields(spec, &answers);

            if !missing.is_empty() {
                // Prompt for missing required fields
                let display = setup_to_formspec::strip_domain_prefix(provider_id);
                println!("\n⚠️  Missing required fields for {display}. Please provide values:");
                answers = prompt_for_missing_fields(spec, &answers, &missing)?;
            }

            validate_answers_against_form_spec(spec, &answers)?;
            answers
        }
    } else {
        Value::Object(JsonMap::new())
    };

    Ok((answers, form_spec))
}

/// Find required fields that are missing or have empty values.
///
/// Returns a list of question IDs that are required, visible, and either:
/// - Missing from answers
/// - Have null value
/// - Have empty string value
fn find_missing_required_fields(spec: &FormSpec, answers: &Value) -> Vec<String> {
    let map = answers.as_object();
    let visibility = resolve_visibility(spec, answers, VisibilityMode::Visible);

    spec.questions
        .iter()
        .filter(|q| {
            // Must be required
            if !q.required {
                return false;
            }
            // Must be visible
            let visible = visibility.get(&q.id).copied().unwrap_or(true);
            if !visible {
                return false;
            }
            // Check if missing or empty
            match map.and_then(|m| m.get(&q.id)) {
                None => true,                                   // Missing
                Some(Value::Null) => true,                      // Null
                Some(Value::String(s)) if s.is_empty() => true, // Empty string
                _ => false,                                     // Has value
            }
        })
        .map(|q| q.id.clone())
        .collect()
}

/// Prompt for specific missing required fields.
///
/// Only prompts for the questions whose IDs are in `missing_ids`.
fn prompt_for_missing_fields(
    spec: &FormSpec,
    existing_answers: &Value,
    missing_ids: &[String],
) -> Result<Value> {
    let mut answers = existing_answers.as_object().cloned().unwrap_or_default();

    for question in &spec.questions {
        if !missing_ids.contains(&question.id) {
            continue;
        }

        // Re-evaluate visibility with answers collected so far
        if question.visible_if.is_some() {
            let current = Value::Object(answers.clone());
            let vis = resolve_visibility(spec, &current, VisibilityMode::Visible);
            if !vis.get(&question.id).copied().unwrap_or(true) {
                continue;
            }
        }

        if let Some(value) = prompt_question(question)? {
            answers.insert(question.id.clone(), value);
        }
    }

    Ok(Value::Object(answers))
}

#[cfg(test)]
mod tests {
    use super::*;
    use qa_spec::{QuestionSpec, QuestionType};
    use serde_json::json;

    fn test_form_spec() -> FormSpec {
        FormSpec {
            id: "test-setup".into(),
            title: "Test Setup".into(),
            version: "1.0.0".into(),
            description: None,
            presentation: None,
            progress_policy: None,
            secrets_policy: None,
            store: vec![],
            validations: vec![],
            includes: vec![],
            questions: vec![
                QuestionSpec {
                    id: "api_url".into(),
                    kind: QuestionType::String,
                    title: "API URL".into(),
                    title_i18n: None,
                    description: None,
                    description_i18n: None,
                    required: true,
                    choices: None,
                    default_value: None,
                    secret: false,
                    visible_if: None,
                    constraint: Some(qa_spec::spec::Constraint {
                        pattern: Some(r"^https?://\S+".into()),
                        min: None,
                        max: None,
                        min_len: None,
                        max_len: None,
                    }),
                    list: None,
                    computed: None,
                    policy: Default::default(),
                    computed_overridable: false,
                },
                QuestionSpec {
                    id: "token".into(),
                    kind: QuestionType::String,
                    title: "Token".into(),
                    title_i18n: None,
                    description: None,
                    description_i18n: None,
                    required: true,
                    choices: None,
                    default_value: None,
                    secret: true,
                    visible_if: None,
                    constraint: None,
                    list: None,
                    computed: None,
                    policy: Default::default(),
                    computed_overridable: false,
                },
                QuestionSpec {
                    id: "optional".into(),
                    kind: QuestionType::String,
                    title: "Optional Field".into(),
                    title_i18n: None,
                    description: None,
                    description_i18n: None,
                    required: false,
                    choices: None,
                    default_value: Some("default_val".into()),
                    secret: false,
                    visible_if: None,
                    constraint: None,
                    list: None,
                    computed: None,
                    policy: Default::default(),
                    computed_overridable: false,
                },
            ],
        }
    }

    #[test]
    fn validates_required_answers() {
        let spec = test_form_spec();
        let answers = json!({"api_url": "https://example.com", "token": "abc"});
        assert!(validate_answers_against_form_spec(&spec, &answers).is_ok());
    }

    #[test]
    fn rejects_missing_required() {
        let spec = test_form_spec();
        let answers = json!({"api_url": "https://example.com"});
        let err = validate_answers_against_form_spec(&spec, &answers).unwrap_err();
        assert!(err.to_string().contains("token"));
    }

    #[test]
    fn rejects_invalid_url_pattern() {
        let spec = test_form_spec();
        let answers = json!({"api_url": "not-a-url", "token": "abc"});
        let err = validate_answers_against_form_spec(&spec, &answers).unwrap_err();
        assert!(err.to_string().contains("pattern"));
    }

    #[test]
    fn skips_invisible_required_in_validation() {
        use qa_spec::Expr;

        let spec = FormSpec {
            id: "vis-test".into(),
            title: "Visibility Test".into(),
            version: "1.0.0".into(),
            description: None,
            presentation: None,
            progress_policy: None,
            secrets_policy: None,
            store: vec![],
            validations: vec![],
            includes: vec![],
            questions: vec![
                QuestionSpec {
                    id: "trigger".into(),
                    kind: QuestionType::Boolean,
                    title: "Enable feature".into(),
                    title_i18n: None,
                    description: None,
                    description_i18n: None,
                    required: true,
                    choices: None,
                    default_value: None,
                    secret: false,
                    visible_if: None,
                    constraint: None,
                    list: None,
                    computed: None,
                    policy: Default::default(),
                    computed_overridable: false,
                },
                QuestionSpec {
                    id: "dependent".into(),
                    kind: QuestionType::String,
                    title: "Dependent field".into(),
                    title_i18n: None,
                    description: None,
                    description_i18n: None,
                    required: true,
                    choices: None,
                    default_value: None,
                    secret: false,
                    visible_if: Some(Expr::Answer {
                        path: "trigger".to_string(),
                    }),
                    constraint: None,
                    list: None,
                    computed: None,
                    policy: Default::default(),
                    computed_overridable: false,
                },
            ],
        };

        // trigger=false → dependent is invisible → should pass without "dependent"
        let answers = json!({"trigger": false});
        assert!(validate_answers_against_form_spec(&spec, &answers).is_ok());

        // trigger=true → dependent is visible → should fail without "dependent"
        let answers = json!({"trigger": true});
        let err = validate_answers_against_form_spec(&spec, &answers);
        assert!(err.is_err());
        assert!(err.unwrap_err().to_string().contains("dependent"));
    }

    #[test]
    fn compute_visibility_returns_map() {
        use qa_spec::Expr;

        let spec = FormSpec {
            id: "vis-test".into(),
            title: "Test".into(),
            version: "1.0.0".into(),
            description: None,
            presentation: None,
            progress_policy: None,
            secrets_policy: None,
            store: vec![],
            validations: vec![],
            includes: vec![],
            questions: vec![QuestionSpec {
                id: "conditional".into(),
                kind: QuestionType::String,
                title: "Cond".into(),
                title_i18n: None,
                description: None,
                description_i18n: None,
                required: false,
                choices: None,
                default_value: None,
                secret: false,
                visible_if: Some(Expr::Answer {
                    path: "flag".to_string(),
                }),
                constraint: None,
                list: None,
                computed: None,
                policy: Default::default(),
                computed_overridable: false,
            }],
        };

        let vis = compute_visibility(&spec, &json!({"flag": true}));
        assert_eq!(vis.get("conditional"), Some(&true));

        let vis = compute_visibility(&spec, &json!({"flag": false}));
        assert_eq!(vis.get("conditional"), Some(&false));
    }

    #[test]
    fn normal_mode_skips_optional_questions() {
        let spec = test_form_spec();
        let advanced = false;
        let visible: Vec<&str> = spec
            .questions
            .iter()
            .filter(|q| !q.id.is_empty() && (advanced || q.required))
            .map(|q| q.id.as_str())
            .collect();
        assert_eq!(visible, vec!["api_url", "token"]);
        assert!(!visible.contains(&"optional"));
    }

    #[test]
    fn advanced_mode_shows_all_questions() {
        let spec = test_form_spec();
        let advanced = true;
        let visible: Vec<&str> = spec
            .questions
            .iter()
            .filter(|q| !q.id.is_empty() && (advanced || q.required))
            .map(|q| q.id.as_str())
            .collect();
        assert_eq!(visible, vec!["api_url", "token", "optional"]);
    }

    // ── Missing Required Fields Tests ──────────────────────────────────────────

    #[test]
    fn find_missing_required_fields_detects_missing() {
        let spec = test_form_spec();
        let answers = json!({"api_url": "https://example.com"});

        let missing = find_missing_required_fields(&spec, &answers);

        assert_eq!(missing.len(), 1);
        assert!(missing.contains(&"token".to_string()));
    }

    #[test]
    fn find_missing_required_fields_detects_empty_string() {
        let spec = test_form_spec();
        let answers = json!({"api_url": "https://example.com", "token": ""});

        let missing = find_missing_required_fields(&spec, &answers);

        assert_eq!(missing.len(), 1);
        assert!(missing.contains(&"token".to_string()));
    }

    #[test]
    fn find_missing_required_fields_detects_null() {
        let spec = test_form_spec();
        let answers = json!({"api_url": "https://example.com", "token": null});

        let missing = find_missing_required_fields(&spec, &answers);

        assert_eq!(missing.len(), 1);
        assert!(missing.contains(&"token".to_string()));
    }

    #[test]
    fn find_missing_required_fields_returns_empty_when_all_filled() {
        let spec = test_form_spec();
        let answers = json!({"api_url": "https://example.com", "token": "abc123"});

        let missing = find_missing_required_fields(&spec, &answers);

        assert!(missing.is_empty());
    }

    #[test]
    fn find_missing_required_fields_ignores_optional() {
        let spec = test_form_spec();
        let answers = json!({"api_url": "https://example.com", "token": "abc"});

        let missing = find_missing_required_fields(&spec, &answers);

        assert!(missing.is_empty());
        assert!(!missing.contains(&"optional".to_string()));
    }

    #[test]
    fn find_missing_required_fields_respects_visibility() {
        use qa_spec::Expr;

        let spec = FormSpec {
            id: "vis-test".into(),
            title: "Visibility Test".into(),
            version: "1.0.0".into(),
            description: None,
            presentation: None,
            progress_policy: None,
            secrets_policy: None,
            store: vec![],
            validations: vec![],
            includes: vec![],
            questions: vec![
                QuestionSpec {
                    id: "trigger".into(),
                    kind: QuestionType::Boolean,
                    title: "Enable feature".into(),
                    title_i18n: None,
                    description: None,
                    description_i18n: None,
                    required: true,
                    choices: None,
                    default_value: None,
                    secret: false,
                    visible_if: None,
                    constraint: None,
                    list: None,
                    computed: None,
                    policy: Default::default(),
                    computed_overridable: false,
                },
                QuestionSpec {
                    id: "dependent".into(),
                    kind: QuestionType::String,
                    title: "Dependent field".into(),
                    title_i18n: None,
                    description: None,
                    description_i18n: None,
                    required: true,
                    choices: None,
                    default_value: None,
                    secret: false,
                    visible_if: Some(Expr::Answer {
                        path: "trigger".to_string(),
                    }),
                    constraint: None,
                    list: None,
                    computed: None,
                    policy: Default::default(),
                    computed_overridable: false,
                },
            ],
        };

        // trigger=false → dependent is invisible → should NOT be in missing list
        let answers = json!({"trigger": false});
        let missing = find_missing_required_fields(&spec, &answers);
        assert!(missing.is_empty());

        // trigger=true → dependent is visible → should BE in missing list
        let answers = json!({"trigger": true});
        let missing = find_missing_required_fields(&spec, &answers);
        assert_eq!(missing.len(), 1);
        assert!(missing.contains(&"dependent".to_string()));
    }
}