greentic-flow 0.4.65

Generic YGTC flow schema/loader/IR for self-describing component nodes.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
use anyhow::{Context, Result, anyhow};
use serde_json::Value;
use std::collections::HashMap;
use std::io::{self, Read, Write};

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QuestionKind {
    String,
    Bool,
    Choice,
    Int,
    Float,
}

#[derive(Debug, Clone)]
pub struct Question {
    pub id: String,
    pub prompt: String,
    pub kind: QuestionKind,
    pub required: bool,
    pub default: Option<Value>,
    pub choices: Vec<Value>,
    pub show_if: Option<Value>,
    pub writes_to: Option<String>,
}

pub type Answers = HashMap<String, Value>;

#[derive(Debug, Clone)]
pub struct MissingRequired {
    pub missing: Vec<String>,
    pub template: String,
}

impl std::fmt::Display for MissingRequired {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "missing required answers: {}. Provide via --answers/--answers-file. Example:\n{}",
            self.missing.join(", "),
            self.template
        )
    }
}

impl std::error::Error for MissingRequired {}

pub fn merge_answers(cli_answers: Option<Answers>, file_answers: Option<Answers>) -> Answers {
    let mut merged = Answers::new();
    if let Some(cli) = cli_answers {
        merged.extend(cli);
    }
    if let Some(file) = file_answers {
        merged.extend(file);
    }
    merged
}

pub fn validate_required(questions: &[Question], answers: &Answers) -> Result<()> {
    let missing = missing_required(questions, answers);
    if missing.is_empty() {
        return Ok(());
    }
    let template = serde_json::to_string_pretty(&template_for_questions(questions, answers))
        .unwrap_or_else(|_| "{}".to_string());
    Err(MissingRequired { missing, template }.into())
}

pub fn run_interactive(questions: &[Question]) -> Result<Answers> {
    run_interactive_with_seed(questions, Answers::new())
}

pub fn run_interactive_with_seed(questions: &[Question], seed: Answers) -> Result<Answers> {
    let stdin = io::stdin();
    let stdout = io::stdout();
    run_interactive_with_io(questions, seed, stdin.lock(), stdout.lock())
}

pub fn run_interactive_with_io<R: Read, W: Write>(
    questions: &[Question],
    mut answers: Answers,
    mut reader: R,
    mut writer: W,
) -> Result<Answers> {
    let mut input = String::new();
    for question in questions {
        if !question_visible(question, &answers) {
            continue;
        }
        if answers.contains_key(&question.id) {
            continue;
        }
        let effective_default = question.default.clone();
        loop {
            input.clear();
            write_prompt(&mut writer, question, effective_default.as_ref())?;
            writer.flush().ok();
            let read_any = read_line(&mut reader, &mut input)?;
            let raw = input.trim();
            if raw.is_empty() {
                if let Some(default) = effective_default.clone() {
                    answers.insert(question.id.clone(), default);
                    break;
                }
                if !read_any {
                    return Err(anyhow!(
                        "stdin closed while waiting for answer for '{}'",
                        question.id
                    ));
                }
                if question.required {
                    continue;
                }
                break;
            }
            match parse_answer(raw, question) {
                Ok(value) => {
                    answers.insert(question.id.clone(), value);
                    break;
                }
                Err(_) => {
                    continue;
                }
            }
        }
    }
    Ok(answers)
}

pub fn extract_questions_from_flow(flow: &Value) -> Result<Vec<Question>> {
    let Some(nodes) = flow.get("nodes").and_then(Value::as_object) else {
        return Ok(Vec::new());
    };
    let mut questions = Vec::new();
    for node in nodes.values() {
        let Some(qnode) = node.get("questions") else {
            continue;
        };
        let fields = qnode
            .get("fields")
            .and_then(Value::as_array)
            .ok_or_else(|| anyhow!("questions node missing fields array"))?;
        for field in fields {
            let id = field
                .get("id")
                .and_then(Value::as_str)
                .ok_or_else(|| anyhow!("questions field missing id"))?;
            let prompt = field
                .get("prompt")
                .and_then(Value::as_str)
                .unwrap_or(id)
                .to_string();
            let default = field.get("default").cloned();
            let required = field
                .get("required")
                .and_then(Value::as_bool)
                .unwrap_or(default.is_none());
            let kind = match field.get("type").and_then(Value::as_str) {
                Some("bool") | Some("boolean") => QuestionKind::Bool,
                Some("int") | Some("integer") => QuestionKind::Int,
                Some("float") | Some("number") => QuestionKind::Float,
                Some("choice") | Some("enum") => QuestionKind::Choice,
                _ => QuestionKind::String,
            };
            let choices = field
                .get("options")
                .and_then(Value::as_array)
                .map(|opts| opts.to_vec())
                .unwrap_or_default();
            let show_if = field.get("show_if").cloned();
            questions.push(Question {
                id: id.to_string(),
                prompt,
                kind,
                required,
                default,
                choices,
                show_if,
                writes_to: field
                    .get("writes_to")
                    .and_then(Value::as_str)
                    .map(|s| s.to_string()),
            });
        }
    }
    Ok(questions)
}
fn write_prompt<W: Write>(
    writer: &mut W,
    question: &Question,
    default_override: Option<&Value>,
) -> Result<()> {
    write!(writer, "Question ({}): {}", question.id, question.prompt).context("write prompt")?;
    if let Some(default) = default_override.or(question.default.as_ref()) {
        write!(writer, " [default: {}]", display_value(default)).ok();
    }
    writeln!(writer).ok();
    if question.kind == QuestionKind::Choice && !question.choices.is_empty() {
        for (idx, choice) in question.choices.iter().enumerate() {
            writeln!(writer, "  {}) {}", idx + 1, display_value(choice)).ok();
        }
    }
    Ok(())
}

fn read_line<R: Read>(reader: &mut R, buf: &mut String) -> Result<bool> {
    let mut bytes = Vec::new();
    let mut cursor = 0usize;
    let mut byte = [0u8; 1];
    let mut read_any = false;
    while reader.read(&mut byte).context("read input")? == 1 {
        read_any = true;
        match byte[0] {
            b'\n' => break,
            b'\r' => continue,
            // Backspace (Ctrl+H) and DEL.
            0x08 | 0x7f => {
                if cursor > 0 {
                    cursor -= 1;
                    bytes.remove(cursor);
                }
            }
            // Ctrl+A / Ctrl+E.
            0x01 => cursor = 0,
            0x05 => cursor = bytes.len(),
            // ANSI escape sequence (arrow keys/home/end/delete).
            0x1b => consume_escape_sequence(reader, &mut bytes, &mut cursor)?,
            b if b.is_ascii_control() => {}
            b => {
                bytes.insert(cursor, b);
                cursor += 1;
            }
        }
    }
    *buf = String::from_utf8(bytes).context("parse input as UTF-8")?;
    Ok(read_any)
}

fn consume_escape_sequence<R: Read>(
    reader: &mut R,
    bytes: &mut Vec<u8>,
    cursor: &mut usize,
) -> Result<()> {
    let mut next = [0u8; 1];
    if reader.read(&mut next).context("read escape sequence")? != 1 {
        return Ok(());
    }
    if next[0] != b'[' {
        return Ok(());
    }

    let mut seq = Vec::new();
    loop {
        let mut b = [0u8; 1];
        if reader.read(&mut b).context("read escape sequence")? != 1 {
            return Ok(());
        }
        seq.push(b[0]);
        if b[0].is_ascii_alphabetic() || b[0] == b'~' {
            break;
        }
        if seq.len() > 8 {
            return Ok(());
        }
    }

    match seq.as_slice() {
        [b'D'] => {
            if *cursor > 0 {
                *cursor -= 1;
            }
        }
        [b'C'] => {
            if *cursor < bytes.len() {
                *cursor += 1;
            }
        }
        [b'H'] | [b'1', b'~'] | [b'7', b'~'] => *cursor = 0,
        [b'F'] | [b'4', b'~'] | [b'8', b'~'] => *cursor = bytes.len(),
        [b'3', b'~'] => {
            if *cursor < bytes.len() {
                bytes.remove(*cursor);
            }
        }
        _ => {}
    }
    Ok(())
}

fn parse_answer(raw: &str, question: &Question) -> Result<Value> {
    match question.kind {
        QuestionKind::String => Ok(Value::String(raw.to_string())),
        QuestionKind::Bool => parse_bool(raw).map(Value::Bool),
        QuestionKind::Int => {
            let parsed = raw.parse::<i64>().map_err(|_| anyhow!("invalid integer"))?;
            Ok(Value::Number(parsed.into()))
        }
        QuestionKind::Float => {
            let parsed = raw.parse::<f64>().map_err(|_| anyhow!("invalid number"))?;
            let number =
                serde_json::Number::from_f64(parsed).ok_or_else(|| anyhow!("invalid number"))?;
            Ok(Value::Number(number))
        }
        QuestionKind::Choice => parse_choice(raw, question),
    }
}

fn parse_bool(raw: &str) -> Result<bool> {
    let lowered = raw.trim().to_lowercase();
    let compact: String = lowered.chars().filter(|c| !c.is_whitespace()).collect();
    match compact.as_str() {
        "yes=true" => Ok(true),
        "no=false" => Ok(false),
        "y" | "yes" | "true" | "1" => Ok(true),
        "n" | "no" | "false" | "0" => Ok(false),
        _ => Err(anyhow!("invalid boolean")),
    }
}

fn parse_choice(raw: &str, question: &Question) -> Result<Value> {
    if let Ok(idx) = raw.parse::<usize>()
        && idx >= 1
        && idx <= question.choices.len()
    {
        return Ok(question.choices[idx - 1].clone());
    }
    for choice in &question.choices {
        if display_value(choice) == raw {
            return Ok(choice.clone());
        }
    }
    Err(anyhow!("invalid choice"))
}

fn display_value(value: &Value) -> String {
    match value {
        Value::String(s) => s.clone(),
        other => other.to_string(),
    }
}

pub fn apply_writes_to(
    mut base: Value,
    questions: &[Question],
    answers: &Answers,
) -> Result<Value> {
    for question in questions {
        let Some(path) = question.writes_to.as_deref() else {
            continue;
        };
        let Some(answer) = answers.get(&question.id) else {
            continue;
        };
        let tokens = parse_path_tokens(path)?;
        set_value_at_path(&mut base, &tokens, answer.clone());
    }
    Ok(base)
}

pub fn extract_answers_from_payload(questions: &[Question], payload: &Value) -> Answers {
    let mut answers = Answers::new();
    for question in questions {
        let Some(path) = question.writes_to.as_deref() else {
            continue;
        };
        if let Ok(tokens) = parse_path_tokens(path)
            && let Some(value) = get_value_at_path(payload, &tokens)
        {
            answers.insert(question.id.clone(), value);
        }
    }
    answers
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum PathToken {
    Key(String),
    Index(usize),
}

fn parse_path_tokens(path: &str) -> Result<Vec<PathToken>> {
    let mut tokens = Vec::new();
    let mut buf = String::new();
    let mut chars = path.chars().peekable();
    while let Some(ch) = chars.next() {
        match ch {
            '.' => {
                if !buf.is_empty() {
                    tokens.push(PathToken::Key(std::mem::take(&mut buf)));
                }
            }
            '[' => {
                if !buf.is_empty() {
                    tokens.push(PathToken::Key(std::mem::take(&mut buf)));
                }
                let mut idx_buf = String::new();
                for c in chars.by_ref() {
                    if c == ']' {
                        break;
                    }
                    idx_buf.push(c);
                }
                let idx = idx_buf
                    .parse::<usize>()
                    .map_err(|_| anyhow!("invalid index in writes_to path"))?;
                tokens.push(PathToken::Index(idx));
            }
            _ => buf.push(ch),
        }
    }
    if !buf.is_empty() {
        tokens.push(PathToken::Key(buf));
    }
    if tokens.is_empty() {
        Err(anyhow!("writes_to path is empty"))
    } else {
        Ok(tokens)
    }
}

fn ensure_array_len(arr: &mut Vec<Value>, index: usize) {
    if arr.len() <= index {
        arr.resize(index + 1, Value::Null);
    }
}

fn set_value_at_path(target: &mut Value, tokens: &[PathToken], value: Value) {
    let mut current = target;
    for (i, token) in tokens.iter().enumerate() {
        let last = i == tokens.len() - 1;
        match token {
            PathToken::Key(key) => {
                if !current.is_object() {
                    *current = Value::Object(serde_json::Map::new());
                }
                let obj = current.as_object_mut().unwrap();
                if last {
                    obj.insert(key.clone(), value);
                    return;
                }
                current = obj.entry(key.clone()).or_insert(Value::Null);
            }
            PathToken::Index(index) => {
                if !current.is_array() {
                    *current = Value::Array(Vec::new());
                }
                let arr = current.as_array_mut().unwrap();
                ensure_array_len(arr, *index);
                if last {
                    arr[*index] = value;
                    return;
                }
                current = &mut arr[*index];
            }
        }
    }
}

fn get_value_at_path(target: &Value, tokens: &[PathToken]) -> Option<Value> {
    let mut current = target;
    for token in tokens {
        match token {
            PathToken::Key(key) => {
                current = current.as_object()?.get(key)?;
            }
            PathToken::Index(index) => {
                current = current.as_array()?.get(*index)?;
            }
        }
    }
    Some(current.clone())
}

fn missing_required(questions: &[Question], answers: &Answers) -> Vec<String> {
    questions
        .iter()
        .filter(|q| q.required && question_visible(q, answers) && !answers.contains_key(&q.id))
        .map(|q| q.id.clone())
        .collect::<Vec<_>>()
}

fn template_for_questions(questions: &[Question], answers: &Answers) -> Value {
    let mut obj = serde_json::Map::new();
    for question in questions {
        if !question_visible(question, answers) {
            continue;
        }
        let value = if let Some(default) = question.default.clone() {
            default
        } else {
            match question.kind {
                QuestionKind::Bool => Value::Bool(false),
                QuestionKind::Int => Value::Number(0.into()),
                QuestionKind::Float => Value::Number(
                    serde_json::Number::from_f64(0.0)
                        .unwrap_or_else(|| serde_json::Number::from(0)),
                ),
                QuestionKind::Choice => question
                    .choices
                    .first()
                    .cloned()
                    .unwrap_or_else(|| Value::String(String::new())),
                QuestionKind::String => Value::String(String::new()),
            }
        };
        obj.insert(question.id.clone(), value);
    }
    Value::Object(obj)
}

fn question_visible(question: &Question, answers: &Answers) -> bool {
    let Some(show_if) = &question.show_if else {
        return true;
    };
    match show_if {
        Value::Bool(value) => *value,
        Value::Object(map) => {
            let Some(id) = map.get("id").and_then(Value::as_str) else {
                return true;
            };
            let Some(expected) = map.get("equals") else {
                return true;
            };
            let Some(actual) = answers.get(id) else {
                return false;
            };
            actual == expected
        }
        _ => true,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use std::io::Cursor;

    #[test]
    fn interactive_accepts_default_on_empty() {
        let question = Question {
            id: "name".to_string(),
            prompt: "Name?".to_string(),
            kind: QuestionKind::String,
            required: true,
            default: Some(Value::String("Ada".to_string())),
            choices: Vec::new(),
            show_if: None,
            writes_to: None,
        };
        let input = Cursor::new("\n");
        let output = Vec::new();
        let answers = run_interactive_with_io(&[question], Answers::new(), input, output).unwrap();
        assert_eq!(answers.get("name"), Some(&Value::String("Ada".to_string())));
    }

    #[test]
    fn choice_accepts_index_or_value() {
        let question = Question {
            id: "color".to_string(),
            prompt: "Color?".to_string(),
            kind: QuestionKind::Choice,
            required: true,
            default: None,
            choices: vec![
                Value::String("red".to_string()),
                Value::String("blue".to_string()),
            ],
            show_if: None,
            writes_to: None,
        };
        let input = Cursor::new("2\n");
        let output = Vec::new();
        let answers = run_interactive_with_io(
            std::slice::from_ref(&question),
            Answers::new(),
            input,
            output,
        )
        .unwrap();
        assert_eq!(
            answers.get("color"),
            Some(&Value::String("blue".to_string()))
        );

        let input = Cursor::new("red\n");
        let output = Vec::new();
        let answers = run_interactive_with_io(&[question], Answers::new(), input, output).unwrap();
        assert_eq!(
            answers.get("color"),
            Some(&Value::String("red".to_string()))
        );
    }

    #[test]
    fn missing_required_reports_all_fields() {
        let questions = vec![
            Question {
                id: "a".to_string(),
                prompt: "A?".to_string(),
                kind: QuestionKind::String,
                required: true,
                default: None,
                choices: Vec::new(),
                show_if: None,
                writes_to: None,
            },
            Question {
                id: "b".to_string(),
                prompt: "B?".to_string(),
                kind: QuestionKind::String,
                required: true,
                default: None,
                choices: Vec::new(),
                show_if: None,
                writes_to: None,
            },
        ];
        let err = validate_required(&questions, &Answers::new()).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("a"));
        assert!(msg.contains("b"));
        assert!(msg.contains("--answers"));
        assert!(msg.contains('{'));
    }

    #[test]
    fn interactive_parses_int_and_bool() {
        let questions = vec![
            Question {
                id: "count".to_string(),
                prompt: "Count?".to_string(),
                kind: QuestionKind::Int,
                required: true,
                default: None,
                choices: Vec::new(),
                show_if: None,
                writes_to: None,
            },
            Question {
                id: "flag".to_string(),
                prompt: "Flag?".to_string(),
                kind: QuestionKind::Bool,
                required: true,
                default: None,
                choices: Vec::new(),
                show_if: None,
                writes_to: None,
            },
        ];
        let input = Cursor::new("42\ny\n");
        let output = Vec::new();
        let answers = run_interactive_with_io(&questions, Answers::new(), input, output).unwrap();
        assert_eq!(answers.get("count"), Some(&Value::Number(42.into())));
        assert_eq!(answers.get("flag"), Some(&Value::Bool(true)));
    }

    #[test]
    fn interactive_accepts_yes_no_equals_true_false() {
        let questions = vec![
            Question {
                id: "enabled".to_string(),
                prompt: "Enabled?".to_string(),
                kind: QuestionKind::Bool,
                required: true,
                default: None,
                choices: Vec::new(),
                show_if: None,
                writes_to: None,
            },
            Question {
                id: "disabled".to_string(),
                prompt: "Disabled?".to_string(),
                kind: QuestionKind::Bool,
                required: true,
                default: None,
                choices: Vec::new(),
                show_if: None,
                writes_to: None,
            },
        ];
        let input = Cursor::new("YeS = TrUe\nNo = False\n");
        let output = Vec::new();
        let answers = run_interactive_with_io(&questions, Answers::new(), input, output).unwrap();
        assert_eq!(answers.get("enabled"), Some(&Value::Bool(true)));
        assert_eq!(answers.get("disabled"), Some(&Value::Bool(false)));
    }

    #[test]
    fn interactive_respects_show_if_equals() {
        let questions = vec![
            Question {
                id: "mode".to_string(),
                prompt: "Mode?".to_string(),
                kind: QuestionKind::String,
                required: true,
                default: Some(Value::String("asset".to_string())),
                choices: Vec::new(),
                show_if: None,
                writes_to: None,
            },
            Question {
                id: "asset_path".to_string(),
                prompt: "Asset?".to_string(),
                kind: QuestionKind::String,
                required: true,
                default: None,
                choices: Vec::new(),
                show_if: Some(json!({ "id": "mode", "equals": "asset" })),
                writes_to: None,
            },
        ];
        let input = Cursor::new("\npath.json\n");
        let output = Vec::new();
        let answers = run_interactive_with_io(&questions, Answers::new(), input, output).unwrap();
        assert_eq!(
            answers.get("mode"),
            Some(&Value::String("asset".to_string()))
        );
        assert_eq!(
            answers.get("asset_path"),
            Some(&Value::String("path.json".to_string()))
        );

        let input = Cursor::new("inline\n");
        let output = Vec::new();
        let answers = run_interactive_with_io(&questions, Answers::new(), input, output).unwrap();
        assert_eq!(
            answers.get("mode"),
            Some(&Value::String("inline".to_string()))
        );
        assert!(!answers.contains_key("asset_path"));
    }

    #[test]
    fn validate_required_skips_hidden_questions() {
        let questions = vec![Question {
            id: "hidden".to_string(),
            prompt: "Hidden?".to_string(),
            kind: QuestionKind::String,
            required: true,
            default: None,
            choices: Vec::new(),
            show_if: Some(Value::Bool(false)),
            writes_to: None,
        }];
        validate_required(&questions, &Answers::new()).unwrap();
    }

    #[test]
    fn writes_to_creates_nested_objects() {
        let questions = vec![Question {
            id: "asset_path".to_string(),
            prompt: "Asset?".to_string(),
            kind: QuestionKind::String,
            required: true,
            default: None,
            choices: Vec::new(),
            show_if: None,
            writes_to: Some("card_spec.asset_path".to_string()),
        }];
        let mut answers = Answers::new();
        answers.insert(
            "asset_path".to_string(),
            Value::String("path.json".to_string()),
        );
        let output = apply_writes_to(Value::Object(Default::default()), &questions, &answers)
            .expect("apply");
        let card_spec = output.get("card_spec").and_then(Value::as_object).unwrap();
        assert_eq!(
            card_spec.get("asset_path").and_then(Value::as_str),
            Some("path.json")
        );
    }

    #[test]
    fn writes_to_supports_array_indexes() {
        let questions = vec![Question {
            id: "action_id".to_string(),
            prompt: "Action?".to_string(),
            kind: QuestionKind::String,
            required: true,
            default: None,
            choices: Vec::new(),
            show_if: None,
            writes_to: Some("actions[0].id".to_string()),
        }];
        let mut answers = Answers::new();
        answers.insert(
            "action_id".to_string(),
            Value::String("action-1".to_string()),
        );
        let output = apply_writes_to(Value::Object(Default::default()), &questions, &answers)
            .expect("apply");
        let actions = output.get("actions").and_then(Value::as_array).unwrap();
        let first = actions[0].as_object().unwrap();
        assert_eq!(first.get("id").and_then(Value::as_str), Some("action-1"));
    }

    #[test]
    fn read_line_supports_backspace_and_arrow_edits() {
        let mut input = Cursor::new(b"abc\x1b[D\x1b[D\x7fX\n".to_vec());
        let mut buf = String::new();
        let read_any = read_line(&mut input, &mut buf).expect("read line");
        assert!(read_any);
        assert_eq!(buf, "Xbc");
    }

    #[test]
    fn read_line_supports_home_end_and_delete() {
        let mut input = Cursor::new(b"abcd\x1b[D\x1b[D\x1b[D\x1b[3~X\x1b[H*\x1b[F!\n".to_vec());
        let mut buf = String::new();
        let read_any = read_line(&mut input, &mut buf).expect("read line");
        assert!(read_any);
        assert_eq!(buf, "*aXcd!");
    }
}