standout-input 8.0.1

Declarative input collection for CLI applications
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
//! Dynamic-default tests: construction rules (static XOR dynamic, non-empty
//! revision), the revision-in-fingerprint contract, blank rendering,
//! identical blank resolution across interactive / file / stdin collection,
//! the computed value running the shared validation pipeline, per-occurrence
//! resolution inside repeatable groups, the earlier-fields-only dependency
//! contract, and the computed default showing in interactive prompts.

use std::sync::{Arc, Mutex};

use serial_test::serial;
use standout_input::env::MockStdin;
use standout_input::questionnaire::{
    AnswerSheetDiagnostic, DynamicDefault, Group, Item, Questionnaire, QuestionnaireError,
    ScalarField, ScalarKind, ValidationDiagnostic,
};
use standout_input::{
    reset_default_prompt_responder, set_default_prompt_responder, PromptContext, PromptResponder,
    PromptResponse, ScriptedResponder,
};

/// The wizard-motivating shape: a value-type question, then a cardinality
/// question whose default is `boolean` when the type is `bool` and `single`
/// otherwise.
fn questionnaire() -> Questionnaire {
    Questionnaire::new(
        "demo.dynamic",
        vec![
            ScalarField::new("input.value_type", "Value type?", ScalarKind::String)
                .one_of(["string", "bool", "path"]),
            ScalarField::new("input.cardinality", "Cardinality?", ScalarKind::String)
                .one_of(["single", "list", "boolean"])
                .with_dynamic_default(DynamicDefault::new("1", |earlier| {
                    match earlier.get_text("input.value_type") {
                        Some("bool") => "boolean".to_string(),
                        _ => "single".to_string(),
                    }
                })),
        ],
    )
    .unwrap()
}

/// Set `answer` as the answer text directly below the question line tagged
/// `id`, replacing a rendered pre-filled default line when one is present.
fn answer(sheet: &str, id: &str, answer: &str) -> String {
    let tag = format!("<id:{id}>");
    let lines: Vec<&str> = sheet.lines().collect();
    let mut out: Vec<String> = Vec::new();
    let mut found = false;
    let mut i = 0;
    while i < lines.len() {
        let line = lines[i];
        out.push(line.to_string());
        i += 1;
        if !found && line.trim_end().ends_with(&tag) {
            found = true;
            if lines.get(i).is_some_and(|next| !next.trim().is_empty()) {
                i += 1;
            }
            out.push(answer.to_string());
        }
    }
    assert!(found, "answer sheet has no question line for {tag}");
    out.join("\n") + "\n"
}

struct ResponderGuard;
impl ResponderGuard {
    fn install(responses: impl IntoIterator<Item = PromptResponse>) -> Self {
        Self::install_with(Arc::new(ScriptedResponder::new(responses)))
    }

    fn install_with(responder: Arc<dyn PromptResponder>) -> Self {
        set_default_prompt_responder(responder);
        Self
    }
}
impl Drop for ResponderGuard {
    fn drop(&mut self) {
        reset_default_prompt_responder();
    }
}

// ============================================================================
// Construction rules
// ============================================================================

#[test]
fn declaring_both_a_static_and_a_dynamic_default_is_an_error() {
    let err = Questionnaire::new(
        "demo",
        vec![ScalarField::new("a", "A?", ScalarKind::String)
            .with_default("static")
            .with_dynamic_default(DynamicDefault::new("1", |_| "dynamic".to_string()))],
    )
    .unwrap_err();
    assert!(matches!(&err, QuestionnaireError::Item { id, .. } if id == "a"));
    assert!(err
        .to_string()
        .contains("declares both a static and a dynamic default"));
}

#[test]
fn an_empty_dynamic_default_revision_is_an_error() {
    let err = Questionnaire::new(
        "demo",
        vec![ScalarField::new("a", "A?", ScalarKind::String)
            .with_dynamic_default(DynamicDefault::new("", |_| "x".to_string()))],
    )
    .unwrap_err();
    assert!(matches!(&err, QuestionnaireError::Item { id, .. } if id == "a"));
    assert!(err
        .to_string()
        .contains("attaches a dynamic default with an empty revision"));
}

// ============================================================================
// Fingerprint: the declared revision is the semantic identity
// ============================================================================

fn with_revision_and_closure(
    revision: &str,
    compute: impl Fn() -> String + Send + Sync + 'static,
) -> Questionnaire {
    Questionnaire::new(
        "demo",
        vec![ScalarField::new("a", "A?", ScalarKind::String)
            .with_dynamic_default(DynamicDefault::new(revision, move |_| compute()))],
    )
    .unwrap()
}

#[test]
fn changing_the_revision_changes_the_fingerprint() {
    let one = with_revision_and_closure("1", || "x".to_string());
    let two = with_revision_and_closure("2", || "x".to_string());
    assert_ne!(one.fingerprint(), two.fingerprint());
}

#[test]
fn the_closure_itself_does_not_affect_the_fingerprint() {
    let one = with_revision_and_closure("1", || "x".to_string());
    let other = with_revision_and_closure("1", || "an entirely different value".to_string());
    assert_eq!(one.fingerprint(), other.fingerprint());
}

#[test]
fn a_dynamic_default_cannot_collide_with_a_static_default() {
    let dynamic = with_revision_and_closure("v", || "v".to_string());
    let static_default = Questionnaire::new(
        "demo",
        vec![ScalarField::new("a", "A?", ScalarKind::String).with_default("v")],
    )
    .unwrap();
    assert_ne!(dynamic.fingerprint(), static_default.fingerprint());
}

#[test]
fn a_revision_bump_invalidates_previously_rendered_sheets() {
    let old = with_revision_and_closure("1", || "x".to_string());
    let new = with_revision_and_closure("2", || "x".to_string());
    let sheet = answer(&old.render_answer_sheet(), "a", "hello");
    let diagnostics = new.parse_answer_sheet(&sheet).unwrap_err();
    assert!(matches!(
        &diagnostics[..],
        [AnswerSheetDiagnostic::Incompatible { message }] if message.contains("fingerprint")
    ));
}

// ============================================================================
// Rendering: the answer region stays empty
// ============================================================================

#[test]
fn dynamic_default_fields_render_with_an_empty_answer_region() {
    let q = questionnaire();
    let sheet = q.render_answer_sheet();
    // The line after the cardinality question is blank (end of document
    // here): a sheet cannot pre-fill a value that depends on other answers.
    let lines: Vec<&str> = sheet.lines().collect();
    let question = lines
        .iter()
        .position(|l| l.trim_end().ends_with("<id:input.cardinality>"))
        .unwrap();
    assert!(lines
        .get(question + 1)
        .is_none_or(|next| next.trim().is_empty()));
}

// ============================================================================
// Blank resolution: identical across interactive, file, and stdin
// ============================================================================

#[test]
fn a_blank_answer_resolves_through_the_computed_default_from_a_sheet() {
    let q = questionnaire();
    // Type answered `bool`, cardinality left blank -> computed `boolean`.
    let sheet = answer(&q.render_answer_sheet(), "input.value_type", "bool");
    let raw = q
        .read_answer_sheet_stdin_with(&MockStdin::piped(sheet.clone()))
        .unwrap();
    let answers = q.decode_answers(&raw).unwrap();
    assert_eq!(answers.get_text("input.cardinality"), Some("boolean"));

    // The same document through the file adapter decodes identically.
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("answers.txt");
    std::fs::write(&path, sheet).unwrap();
    let from_file = q
        .decode_answers(&q.read_answer_sheet_file(&path).unwrap())
        .unwrap();
    assert_eq!(from_file, answers);

    // A non-bool type computes the other default.
    let sheet = answer(&q.render_answer_sheet(), "input.value_type", "string");
    let raw = q
        .read_answer_sheet_stdin_with(&MockStdin::piped(sheet))
        .unwrap();
    let answers = q.decode_answers(&raw).unwrap();
    assert_eq!(answers.get_text("input.cardinality"), Some("single"));
}

#[test]
#[serial(prompt_responder)]
fn a_blank_interactive_entry_resolves_through_the_computed_default() {
    let q = questionnaire();
    let _guard = ResponderGuard::install([
        PromptResponse::text("bool"),
        PromptResponse::Skip, // cardinality: blank -> computed "boolean"
    ]);
    let interactive = q.decode_answers(&q.collect_interactive().unwrap()).unwrap();
    assert_eq!(interactive.get_text("input.cardinality"), Some("boolean"));

    // Equivalence: the same submission as a sheet decodes identically.
    let sheet = answer(&q.render_answer_sheet(), "input.value_type", "bool");
    let batch = q
        .decode_answers(
            &q.read_answer_sheet_stdin_with(&MockStdin::piped(sheet))
                .unwrap(),
        )
        .unwrap();
    assert_eq!(interactive, batch);
}

#[test]
#[serial(prompt_responder)]
fn an_entered_answer_overrides_the_computed_default() {
    let q = questionnaire();
    let _guard =
        ResponderGuard::install([PromptResponse::text("bool"), PromptResponse::text("list")]);
    let answers = q.decode_answers(&q.collect_interactive().unwrap()).unwrap();
    assert_eq!(answers.get_text("input.cardinality"), Some("list"));
}

// ============================================================================
// The computed value runs the shared validation pipeline
// ============================================================================

#[test]
fn a_computed_default_that_violates_a_constraint_is_a_diagnostic() {
    let q = Questionnaire::new(
        "demo",
        vec![ScalarField::new("a", "A?", ScalarKind::String)
            .one_of(["x", "y"])
            .with_dynamic_default(DynamicDefault::new("1", |_| "not-a-choice".to_string()))],
    )
    .unwrap();
    let sheet = q.render_answer_sheet();
    let raw = q
        .read_answer_sheet_stdin_with(&MockStdin::piped(sheet))
        .unwrap();
    let diagnostics = q.decode_answers(&raw).unwrap_err();
    assert!(matches!(
        &diagnostics[..],
        [ValidationDiagnostic::Field { id, message }]
            if id == "a" && message.contains("must be one of")
    ));
}

// ============================================================================
// Scope: per-occurrence resolution inside repeatable groups
// ============================================================================

#[test]
fn dynamic_defaults_resolve_per_occurrence_in_repeatable_groups() {
    let q = Questionnaire::new(
        "demo.repeats",
        vec![Item::from(
            Group::new(
                "inputs",
                "Describe an input.",
                vec![
                    ScalarField::new("inputs.value_type", "Type?", ScalarKind::String),
                    ScalarField::new("inputs.cardinality", "Cardinality?", ScalarKind::String)
                        .with_dynamic_default(DynamicDefault::new("1", |earlier| {
                            match earlier.get_text("inputs.value_type") {
                                Some("bool") => "boolean".to_string(),
                                _ => "single".to_string(),
                            }
                        })),
                ],
            )
            .repeatable(1),
        )],
    )
    .unwrap();

    // Occurrence 0 answers `bool`, occurrence 1 answers `string`; both
    // leave cardinality blank. Each occurrence resolves its own default.
    let sheet = q.render_answer_sheet();
    let block_start = sheet.find("Describe an input.").unwrap();
    let block = sheet[block_start..].to_string();
    let first = sheet.replace("<id:inputs.value_type>\n", "<id:inputs.value_type>\nbool\n");
    let second = block.replace(
        "<id:inputs.value_type>\n",
        "<id:inputs.value_type>\nstring\n",
    );
    let document = format!("{first}\n{second}");
    let raw = q
        .read_answer_sheet_stdin_with(&MockStdin::piped(document))
        .unwrap();
    let answers = q.decode_answers(&raw).unwrap();
    assert_eq!(answers.get_text("inputs[0].cardinality"), Some("boolean"));
    assert_eq!(answers.get_text("inputs[1].cardinality"), Some("single"));
}

// ============================================================================
// Dependency contract: earlier fields only, defined failure behavior
// ============================================================================

#[test]
fn a_dependency_on_a_later_or_unknown_field_reads_as_none() {
    // The closure (illegally) looks at a later-declared field and an
    // unknown one: both read as None, so the fallback default applies —
    // the documented contract-violation behavior, not a panic.
    let q = Questionnaire::new(
        "demo",
        vec![
            ScalarField::new("a", "A?", ScalarKind::String).with_dynamic_default(
                DynamicDefault::new("1", |earlier| {
                    assert!(earlier.get("b").is_none());
                    assert!(earlier.get("no.such.field").is_none());
                    "fallback".to_string()
                }),
            ),
            ScalarField::new("b", "B?", ScalarKind::String).optional(),
        ],
    )
    .unwrap();
    let sheet = q.render_answer_sheet();
    let raw = q
        .read_answer_sheet_stdin_with(&MockStdin::piped(sheet))
        .unwrap();
    let answers = q.decode_answers(&raw).unwrap();
    assert_eq!(answers.get_text("a"), Some("fallback"));
}

// ============================================================================
// Interactive prompts show the computed default
// ============================================================================

/// Records every prompt message and answers from a fixed queue.
struct RecordingResponder {
    messages: Mutex<Vec<String>>,
    responses: Mutex<std::collections::VecDeque<PromptResponse>>,
}

impl PromptResponder for RecordingResponder {
    fn respond(&self, ctx: PromptContext<'_>) -> PromptResponse {
        self.messages.lock().unwrap().push(ctx.message.to_string());
        self.responses
            .lock()
            .unwrap()
            .pop_front()
            .expect("unexpected extra prompt")
    }
}

#[test]
#[serial(prompt_responder)]
fn the_interactive_prompt_displays_the_computed_default() {
    let q = questionnaire();
    let responder = Arc::new(RecordingResponder {
        messages: Mutex::new(Vec::new()),
        responses: Mutex::new(
            [PromptResponse::text("bool"), PromptResponse::Skip]
                .into_iter()
                .collect(),
        ),
    });
    let _guard = ResponderGuard::install_with(responder.clone());
    q.collect_interactive().unwrap();

    let messages = responder.messages.lock().unwrap();
    let cardinality_prompt = messages
        .iter()
        .find(|m| m.starts_with("Cardinality?"))
        .expect("the cardinality question prompted");
    assert!(
        cardinality_prompt.contains("[default: boolean]"),
        "prompt should show the computed default: {cardinality_prompt:?}"
    );
}