artifactize-core 0.13.0

The library behind artifactize: Artifacts, evals, reviews, the monitor and review models, and the shared operations every interface calls.
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
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
//! Owner fields for one verdict, as a person enters them. A Human eval's result schemas may
//! use only shapes a form can enter ([`check`] refuses any other when the declaration loads):
//! an object whose properties are strings, integers, numbers, booleans, string choices and
//! constants, lists of strings, and lists of flat objects of those scalars (repeated groups).

use serde_json::{Map, Value, json};

/// Annotations allowed on any schema the form accepts.
const NOTES: [&str; 3] = ["title", "description", "$comment"];
/// Keys of the result object itself, and of each object in a list of groups.
const OBJECT: [&str; 4] = ["type", "properties", "required", "additionalProperties"];
const STRING: [&str; 3] = ["type", "minLength", "maxLength"];
const NUMERIC: [&str; 6] = [
    "type",
    "minimum",
    "maximum",
    "exclusiveMinimum",
    "exclusiveMaximum",
    "multipleOf",
];
const ARRAY: [&str; 4] = ["type", "items", "minItems", "maxItems"];
/// What a Human result schema may hold, as a declaration error says it.
const SHAPES: &str = "a Human result field is a string (with minLength or maxLength), an \
integer or number, a boolean, a string enum, a const, an array of strings, or an array of \
objects whose properties are those scalars";

/// A key as the form edits with it; each interface maps its own key events onto these.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Key {
    Char(char),
    Backspace,
    Delete,
    Left,
    Right,
    Up,
    Down,
    Home,
    End,
    Enter,
    Tab,
    BackTab,
    /// A key the form does not use.
    Other,
}

#[derive(Debug, Clone, PartialEq)]
pub enum Input {
    /// A `const` value: filled in and fixed.
    Fixed(Value),
    /// `None` until chosen.
    Bool(Option<bool>),
    /// String `enum` options and the chosen index.
    Choice(Vec<String>, Option<usize>),
    Text(String),
    Integer(String),
    Number(String),
    /// An array of strings, one entry per item; empty entries are left out of the result.
    List(Vec<String>),
    /// An array of flat objects: one group of fields per item. `fields` is an empty item;
    /// an item with nothing entered is left out of the result.
    Groups {
        fields: Vec<Field>,
        items: Vec<Vec<Field>>,
    },
}

#[derive(Debug, Clone, PartialEq)]
pub struct Field {
    pub name: String,
    pub required: bool,
    /// The type and constraints, then any description.
    pub hint: String,
    pub input: Input,
}

/// One row a terminal form moves through: a field, a field of one group, or a group list's
/// `+ Add`. Constants are filled in and get no row.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Row {
    Field(usize),
    Item {
        field: usize,
        item: usize,
        sub: usize,
    },
    Add(usize),
}

impl Field {
    /// The JSON value to submit, named `label` in errors; `None` leaves an empty optional
    /// field out.
    fn value(&self, label: &str) -> Result<Option<Value>, String> {
        let number = |text: &str, integer: bool| {
            let value: Value = serde_json::from_str(text.trim()).unwrap_or_default();
            let valid = if integer {
                value.is_i64() || value.is_u64()
            } else {
                value.is_number()
            };
            let kind = if integer { "an integer" } else { "a number" };
            valid
                .then_some(value)
                .ok_or_else(|| format!("{label}: enter {kind}."))
        };
        Ok(match &self.input {
            Input::Fixed(value) => Some(value.clone()),
            Input::Bool(value) => value.map(Value::Bool),
            Input::Choice(options, index) => {
                index.map(|index| Value::String(options[index].clone()))
            }
            Input::Text(text) => (self.required || !text.is_empty()).then(|| json!(text)),
            Input::Integer(text) | Input::Number(text) if text.trim().is_empty() => None,
            Input::Integer(text) => Some(number(text, true)?),
            Input::Number(text) => Some(number(text, false)?),
            Input::List(items) => {
                let items: Vec<_> = items
                    .iter()
                    .filter(|item| !item.trim().is_empty())
                    .collect();
                (self.required || !items.is_empty()).then(|| json!(items))
            }
            Input::Groups { items, .. } => {
                let mut values = Vec::new();
                for (at, item) in items.iter().enumerate().filter(|(_, item)| entered(item)) {
                    let mut object = Map::new();
                    for field in item {
                        let label = format!("{label} {} ยท {}", at + 1, field.name);
                        if let Some(value) = field.value(&label)? {
                            object.insert(field.name.clone(), value);
                        }
                    }
                    values.push(Value::Object(object));
                }
                (self.required || !values.is_empty()).then_some(Value::Array(values))
            }
        })
    }

    /// Nothing is entered yet.
    fn empty(&self) -> bool {
        match &self.input {
            Input::Fixed(_) => true,
            Input::Bool(value) => value.is_none(),
            Input::Choice(_, index) => index.is_none(),
            Input::Text(text) | Input::Integer(text) | Input::Number(text) => {
                text.trim().is_empty()
            }
            Input::List(items) => items.iter().all(|item| item.trim().is_empty()),
            Input::Groups { items, .. } => !items.iter().any(|item| entered(item)),
        }
    }

    /// What the form shows for the current value.
    pub fn display(&self) -> String {
        match &self.input {
            Input::Fixed(value) => format!("{value} (fixed)"),
            Input::Bool(None) | Input::Choice(_, None) => "โ€“".into(),
            Input::Bool(Some(value)) => value.to_string(),
            Input::Choice(options, Some(index)) => {
                Value::String(options[*index].clone()).to_string()
            }
            Input::Text(text) | Input::Integer(text) | Input::Number(text) => text.clone(),
            Input::List(items) if items.iter().all(String::is_empty) => "โ€“".into(),
            Input::List(items) => items.join("; "),
            Input::Groups { items, .. } => format!("{} item(s)", items.len()),
        }
    }

    fn text_like(&self) -> bool {
        matches!(
            self.input,
            Input::Text(_) | Input::Integer(_) | Input::Number(_) | Input::List(_)
        )
    }

    /// Append pasted or typed text, bounded like the Human result.
    fn paste(&mut self, text: &str) -> Result<(), String> {
        let fits = |size: usize| size.saturating_add(text.len()) <= crate::human::MAX_RESULT_BYTES;
        let too_big = || {
            format!(
                "Human fields exceed {} bytes.",
                crate::human::MAX_RESULT_BYTES
            )
        };
        match &mut self.input {
            Input::List(items) => {
                if !fits(items.iter().map(String::len).sum()) {
                    return Err(too_big());
                }
                match items.last_mut() {
                    Some(item) => item.push_str(text),
                    None => items.push(text.to_owned()),
                }
            }
            Input::Text(value) | Input::Integer(value) | Input::Number(value) => {
                if !fits(value.len()) {
                    return Err(too_big());
                }
                value.push_str(text);
            }
            _ => {}
        }
        Ok(())
    }

    fn key(&mut self, key: Key) {
        let required = self.required;
        match (&mut self.input, key) {
            (Input::Text(text) | Input::Integer(text) | Input::Number(text), Key::Char(c)) => {
                text.push(c)
            }
            (Input::Text(text) | Input::Integer(text) | Input::Number(text), Key::Backspace) => {
                text.pop();
            }
            // A list types into its last item; Enter starts the next, Backspace on an empty
            // item removes it.
            (Input::List(items), Key::Char(c)) => match items.last_mut() {
                Some(item) => item.push(c),
                None => items.push(c.to_string()),
            },
            (Input::List(items), Key::Enter) => items.push(String::new()),
            (Input::List(items), Key::Backspace) => {
                if items.last().is_some_and(String::is_empty) && items.len() > 1 {
                    items.pop();
                } else if let Some(item) = items.last_mut() {
                    item.pop();
                }
            }
            (Input::Bool(value), Key::Char('t' | 'y')) => *value = Some(true),
            (Input::Bool(value), Key::Char('f' | 'n')) => *value = Some(false),
            (Input::Bool(value), Key::Char(' ') | Key::Left | Key::Right) => {
                *value = match value {
                    None => Some(true),
                    Some(true) => Some(false),
                    Some(false) if required => Some(true),
                    Some(false) => None,
                }
            }
            (Input::Choice(options, index), Key::Char(' ') | Key::Right) => {
                *index = match *index {
                    None => Some(0),
                    Some(i) if i + 1 < options.len() => Some(i + 1),
                    Some(_) if required => Some(0),
                    Some(_) => None,
                }
            }
            (Input::Choice(options, index), Key::Left) => {
                *index = match *index {
                    Some(0) if !required => None,
                    Some(0) | None => Some(options.len() - 1),
                    Some(i) => Some(i - 1),
                }
            }
            _ => {}
        }
    }
}

/// Something is entered in a group.
fn entered(item: &[Field]) -> bool {
    item.iter().any(|field| !field.empty())
}

#[derive(Debug, Clone, PartialEq)]
pub struct Form {
    pub verdict: crate::runtime::Verdict,
    pub fields: Vec<Field>,
    /// The selected row of [`Form::rows`].
    pub selected: usize,
    /// The last local or `human::submit` error.
    pub error: Option<String>,
}

impl Form {
    /// The form of `verdict` for its owner schema, or none. A schema a form cannot enter is
    /// refused, as [`check`] refuses it when the declaration loads.
    pub fn new(verdict: crate::runtime::Verdict, schema: Option<&Value>) -> Result<Self, String> {
        let fields = match schema {
            None => Vec::new(),
            Some(Value::Object(schema)) => fields(schema, "")?,
            Some(_) => return Err("A Human result schema must be a JSON object.".into()),
        };
        Ok(Self {
            verdict,
            fields,
            selected: 0,
            error: None,
        })
    }

    /// The complete result: owner fields plus the chosen verdict.
    pub fn result(&self) -> Result<Value, String> {
        let mut object = Map::new();
        for field in &self.fields {
            if let Some(value) = field.value(&field.name)? {
                object.insert(field.name.clone(), value);
            }
        }
        if object.contains_key("verdict") {
            return Err("Choose the verdict in the review, not inside the owner fields.".into());
        }
        object.insert("verdict".into(), json!(self.verdict));
        Ok(Value::Object(object))
    }

    /// The rows a person moves through, in order.
    pub fn rows(&self) -> Vec<Row> {
        let mut rows = Vec::new();
        for (index, field) in self.fields.iter().enumerate() {
            match &field.input {
                Input::Fixed(_) => {}
                Input::Groups { items, .. } => {
                    for (item, fields) in items.iter().enumerate() {
                        let subs = fields.iter().enumerate();
                        rows.extend(
                            subs.filter(|(_, sub)| !matches!(sub.input, Input::Fixed(_)))
                                .map(|(sub, _)| Row::Item {
                                    field: index,
                                    item,
                                    sub,
                                }),
                        );
                    }
                    rows.push(Row::Add(index));
                }
                _ => rows.push(Row::Field(index)),
            }
        }
        rows
    }

    /// The selected row, if the form has any.
    pub fn row(&self) -> Option<Row> {
        self.rows().get(self.selected).copied()
    }

    /// The field a row edits.
    pub fn field(&self, row: Row) -> Option<&Field> {
        match row {
            Row::Field(index) | Row::Add(index) => self.fields.get(index),
            Row::Item { field, item, sub } => match &self.fields.get(field)?.input {
                Input::Groups { items, .. } => items.get(item)?.get(sub),
                _ => None,
            },
        }
    }

    fn field_mut(&mut self, row: Row) -> Option<&mut Field> {
        match row {
            Row::Field(index) => self.fields.get_mut(index),
            Row::Add(_) => None,
            Row::Item { field, item, sub } => match &mut self.fields.get_mut(field)?.input {
                Input::Groups { items, .. } => items.get_mut(item)?.get_mut(sub),
                _ => None,
            },
        }
    }

    /// Add an empty item to the list of field `index`: an empty text to a list of strings, an
    /// empty group to a list of groups, which the selection moves into.
    pub fn add(&mut self, index: usize) {
        match self.fields.get_mut(index).map(|field| &mut field.input) {
            Some(Input::List(items)) => items.push(String::new()),
            Some(Input::Groups { fields, items }) => {
                items.push(fields.clone());
                let item = items.len() - 1;
                if let Some(at) = self.rows().iter().position(
                    |row| matches!(row, Row::Item { field, item: i, .. } if *field == index && *i == item),
                ) {
                    self.selected = at;
                }
            }
            _ => {}
        }
    }

    /// Remove item `item` of the list of field `index`; one empty item always stays to type in.
    pub fn remove(&mut self, index: usize, item: usize) {
        match self.fields.get_mut(index).map(|field| &mut field.input) {
            Some(Input::List(items)) => {
                if item < items.len() {
                    items.remove(item);
                }
                if items.is_empty() {
                    items.push(String::new());
                }
            }
            Some(Input::Groups { fields, items }) => {
                if item < items.len() {
                    items.remove(item);
                }
                if items.is_empty() {
                    items.push(fields.clone());
                }
            }
            _ => return,
        }
        self.selected = self.selected.min(self.rows().len().saturating_sub(1));
    }

    /// Remove the group the selection is in.
    pub fn remove_selected(&mut self) {
        if let Some(Row::Item { field, item, .. }) = self.row() {
            self.remove(field, item);
        }
    }

    /// Bounded paste follows the existing Human result limit, without splitting UTF-8.
    pub fn paste(&mut self, text: &str) {
        let Some(row) = self.row() else {
            return;
        };
        if let Some(field) = self.field_mut(row)
            && let Err(error) = field.paste(text)
        {
            self.error = Some(error);
        }
    }

    /// A key typed into the form. Enter never submits: on `+ Add` it adds a group, in a list
    /// it starts the next item. Keys with Control or Alt are not the form's.
    pub fn inline_key(&mut self, key: Key, modified: bool) {
        if modified {
            return;
        }
        let row = self.row();
        let text = row
            .and_then(|row| self.field(row))
            .is_some_and(Field::text_like);
        match (key, row) {
            (Key::Char(character), _) if text => self.paste(&character.to_string()),
            (Key::Enter | Key::Char(' '), Some(Row::Add(index))) => self.add(index),
            _ => self.key(key),
        }
    }

    pub fn key(&mut self, key: Key) {
        let count = self.rows().len();
        match key {
            Key::Up | Key::BackTab if count > 0 => {
                self.selected = (self.selected + count - 1) % count
            }
            Key::Down | Key::Tab if count > 0 => self.selected = (self.selected + 1) % count,
            _ => {
                if let Some(row) = self.row()
                    && let Some(field) = self.field_mut(row)
                {
                    field.key(key);
                }
            }
        }
    }
}

/// Whether a Human eval may declare `schema` as a result schema: only shapes a form can
/// enter. The error names the offending key and what is allowed instead.
pub fn check(schema: &Map<String, Value>) -> Result<(), String> {
    fields(schema, "").map(drop)
}

/// The fields of an object schema at `path` (empty for the result itself).
fn fields(schema: &Map<String, Value>, path: &str) -> Result<Vec<Field>, String> {
    let at = |key: &str| {
        if path.is_empty() {
            key.to_owned()
        } else {
            format!("{path}.{key}")
        }
    };
    if let Some(key) = schema
        .keys()
        .find(|key| !OBJECT.contains(&key.as_str()) && !NOTES.contains(&key.as_str()))
    {
        let what = if path.is_empty() {
            "A Human result schema"
        } else {
            "An object in a Human result list"
        };
        return Err(format!(
            "{what} may use only type, properties, required and additionalProperties (with \
             title, description and $comment); `{}` is not allowed.",
            at(key)
        ));
    }
    if let Some(kind) = schema.get("type")
        && kind != "object"
    {
        return Err(format!("`{}` must be \"object\".", at("type")));
    }
    if schema
        .get("additionalProperties")
        .is_some_and(|value| !value.is_boolean())
    {
        return Err(format!(
            "`{}` must be true or false in a Human result schema.",
            at("additionalProperties")
        ));
    }
    let empty = Map::new();
    let properties = match schema.get("properties") {
        None => &empty,
        Some(Value::Object(properties)) => properties,
        Some(_) => return Err(format!("`{}` must be an object.", at("properties"))),
    };
    let required: Vec<&str> = match schema.get("required") {
        None => Vec::new(),
        Some(Value::Array(names)) => names
            .iter()
            .map(Value::as_str)
            .collect::<Option<_>>()
            .ok_or_else(|| format!("`{}` must list property names.", at("required")))?,
        Some(_) => return Err(format!("`{}` must list property names.", at("required"))),
    };
    if let Some(name) = required
        .iter()
        .find(|name| !properties.contains_key(**name))
    {
        return Err(format!(
            "`{}` names `{name}`, which `{}` does not declare, so a reviewer could never enter it.",
            at("required"),
            at("properties")
        ));
    }
    properties
        .iter()
        .map(|(name, property)| {
            let nested = !path.is_empty();
            let path = at(&format!("properties.{name}"));
            // An object below the result is a group, whose fields are scalars.
            let (input, hint) = input(property, &path, nested)?;
            Ok(Field {
                name: name.clone(),
                required: required.contains(&name.as_str()),
                hint,
                input,
            })
        })
        .collect()
}

/// The input of the property at `path`, and its hint. Inside a group (`nested`) only scalars
/// are allowed.
fn input(property: &Value, path: &str, nested: bool) -> Result<(Input, String), String> {
    let refuse = |detail: &str| Err(format!("`{path}`: {detail}; {SHAPES}."));
    let Some(property) = property.as_object() else {
        return refuse("a property schema must be an object");
    };
    let only = |keys: &[&str]| match property
        .keys()
        .find(|key| !keys.contains(&key.as_str()) && !NOTES.contains(&key.as_str()))
    {
        Some(key) => Err(format!(
            "`{path}`: `{key}` is not allowed here; {SHAPES}, and only {} may be used on it.",
            keys.join(", ")
        )),
        None => Ok(()),
    };
    let kind = property.get("type");
    let input = if let Some(value) = property.get("const") {
        only(&["const", "type"])?;
        Input::Fixed(value.clone())
    } else if let Some(options) = property.get("enum") {
        only(&["enum", "type"])?;
        let strings: Option<Vec<String>> = options.as_array().and_then(|options| {
            options
                .iter()
                .map(|option| option.as_str().map(str::to_owned))
                .collect()
        });
        match strings {
            Some(strings) if !strings.is_empty() && kind.is_none_or(|kind| kind == "string") => {
                Input::Choice(strings, None)
            }
            _ => return refuse("an enum lists one or more strings"),
        }
    } else {
        if kind.is_some_and(|kind| !kind.is_string()) {
            return refuse("`type` names a single type");
        }
        match kind.and_then(Value::as_str) {
            Some("boolean") => {
                only(&["type"])?;
                Input::Bool(None)
            }
            Some("string") => {
                only(&STRING)?;
                Input::Text(String::new())
            }
            Some("integer") => {
                only(&NUMERIC)?;
                Input::Integer(String::new())
            }
            Some("number") => {
                only(&NUMERIC)?;
                Input::Number(String::new())
            }
            Some("array") if nested => return refuse("a list cannot hold another list"),
            Some("array") => {
                only(&ARRAY)?;
                array(property, path)?
            }
            Some(other) => return refuse(&format!("type \"{other}\" is not one of them")),
            None => return refuse("a property needs a type, an enum or a const"),
        }
    };
    Ok((input, hint(property)))
}

/// A list of strings, or of groups: `items` is a string schema or a flat object schema.
fn array(property: &Map<String, Value>, path: &str) -> Result<Input, String> {
    let path = format!("{path}.items");
    let items = match property.get("items") {
        Some(Value::Object(items)) => items,
        _ => {
            return Err(format!(
                "`{path}` must be a string schema or an object schema; {SHAPES}."
            ));
        }
    };
    match items.get("type").and_then(Value::as_str) {
        Some("string") => {
            if let Some(key) = items
                .keys()
                .find(|key| !STRING.contains(&key.as_str()) && !NOTES.contains(&key.as_str()))
            {
                return Err(format!(
                    "`{path}`: `{key}` is not allowed here; the items of a list of strings may \
                     use only minLength and maxLength."
                ));
            }
            Ok(Input::List(vec![String::new()]))
        }
        Some("object") => {
            let fields = fields(items, &path)?;
            Ok(Input::Groups {
                items: vec![fields.clone()],
                fields,
            })
        }
        _ => Err(format!(
            "`{path}` must be a string schema or an object schema; {SHAPES}."
        )),
    }
}

/// The type and constraints of a property, then any description.
fn hint(property: &Map<String, Value>) -> String {
    let constraints = property
        .iter()
        .filter(|(key, _)| !["type", "const", "enum", "items"].contains(&key.as_str()))
        .filter(|(key, _)| !NOTES.contains(&key.as_str()))
        .map(|(key, value)| format!("{key} {value}"));
    let kind = property
        .get("type")
        .and_then(Value::as_str)
        .map(str::to_owned);
    let hint = kind
        .into_iter()
        .chain(constraints)
        .collect::<Vec<_>>()
        .join(", ");
    match property.get("description").and_then(Value::as_str) {
        Some(description) if hint.is_empty() => description.to_owned(),
        Some(description) => format!("{hint} โ€” {description}"),
        None => hint,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::runtime::Verdict;

    fn schema(value: Value) -> Map<String, Value> {
        value.as_object().unwrap().clone()
    }

    #[test]
    fn human_schemas_take_scalars_lists_of_strings_and_lists_of_flat_objects() {
        let accepted = json!({
            "type": "object",
            "title": "Review",
            "properties": {
                "approved": {"const": true},
                "comment": {"type": "string", "maxLength": 200, "description": "Why"},
                "count": {"type": "integer", "minimum": 0},
                "score": {"type": "number"},
                "ok": {"type": "boolean"},
                "level": {"enum": ["low", "high"]},
                "changes": {"type": "array", "items": {"type": "string", "minLength": 1}, "minItems": 1},
                "findings": {"type": "array", "maxItems": 5, "items": {
                    "type": "object",
                    "properties": {"line": {"type": "integer"}, "problem": {"type": "string"}},
                    "required": ["problem"],
                    "additionalProperties": false
                }}
            },
            "required": ["approved"],
            "additionalProperties": false
        });
        check(&schema(accepted)).unwrap();
        for (refused, says) in [
            (json!({"oneOf": []}), "`oneOf` is not allowed"),
            (json!({"type": "array"}), "`type` must be \"object\""),
            (
                json!({"properties": {"a": {"type": "object"}}}),
                "`properties.a`: type \"object\"",
            ),
            (
                json!({"properties": {"a": {"type": "string", "pattern": "x"}}}),
                "`pattern` is not allowed",
            ),
            (
                json!({"properties": {"a": {"enum": [1, 2]}}}),
                "an enum lists one or more strings",
            ),
            (
                json!({"properties": {"a": {"type": "array", "items": {"type": "integer"}}}}),
                "`properties.a.items` must be",
            ),
            (
                json!({"properties": {"a": {"type": "array", "items": {"type": "object", "properties": {"b": {"type": "array", "items": {"type": "string"}}}}}}}),
                "`properties.a.items.properties.b`: a list cannot hold another list",
            ),
            (
                json!({"properties": {"a": {"type": "string"}}, "required": ["b"]}),
                "names `b`",
            ),
            (
                json!({"additionalProperties": {"type": "string"}}),
                "true or false",
            ),
        ] {
            let error = check(&schema(refused.clone())).unwrap_err();
            assert!(error.contains(says), "{refused}: {error}");
        }
    }

    fn groups_form() -> Form {
        Form::new(
            Verdict::Red,
            Some(&json!({
                "type": "object",
                "properties": {
                    "kind": {"const": "review"},
                    "findings": {"type": "array", "items": {
                        "type": "object",
                        "properties": {
                            "tag": {"const": "x"},
                            "line": {"type": "integer"},
                            "problem": {"type": "string"}
                        },
                        "required": ["problem"]
                    }}
                },
                "required": ["findings"]
            })),
        )
        .unwrap()
    }

    #[test]
    fn groups_are_rows_to_type_into_with_an_add_row_and_empty_groups_are_left_out() {
        // Properties come in name order: findings, kind; line, problem, tag.
        let mut form = groups_form();
        assert_eq!(
            form.rows(),
            [
                Row::Item {
                    field: 0,
                    item: 0,
                    sub: 0
                },
                Row::Item {
                    field: 0,
                    item: 0,
                    sub: 1
                },
                Row::Add(0),
            ]
        );
        form.inline_key(Key::Char('7'), false);
        form.key(Key::Down);
        form.paste("typo");
        form.key(Key::Down);
        form.inline_key(Key::Enter, false);
        assert_eq!(
            form.row(),
            Some(Row::Item {
                field: 0,
                item: 1,
                sub: 0
            })
        );
        assert_eq!(
            form.result().unwrap(),
            json!({"kind": "review", "verdict": "RED", "findings": [
                {"tag": "x", "line": 7, "problem": "typo"}
            ]})
        );
        form.paste("x");
        assert_eq!(
            form.result().unwrap_err(),
            "findings 2 ยท line: enter an integer."
        );
        form.remove_selected();
        form.remove(0, 0);
        assert_eq!(form.rows().len(), 3, "one empty group always stays");
        assert_eq!(
            form.result().unwrap(),
            json!({"kind": "review", "verdict": "RED", "findings": []})
        );
    }

    #[test]
    fn a_list_of_strings_adds_and_removes_items_and_keeps_one_to_type_in() {
        let mut form = Form::new(
            Verdict::Green,
            Some(&json!({"properties": {"notes": {"type": "array", "items": {"type": "string"}}}})),
        )
        .unwrap();
        form.paste("a");
        form.add(0);
        form.paste("b");
        assert_eq!(
            form.fields[0].input,
            Input::List(vec!["a".into(), "b".into()])
        );
        form.remove(0, 0);
        assert_eq!(
            form.result().unwrap(),
            json!({"verdict": "GREEN", "notes": ["b"]})
        );
        form.remove(0, 0);
        assert_eq!(form.fields[0].input, Input::List(vec![String::new()]));
        assert_eq!(form.result().unwrap(), json!({"verdict": "GREEN"}));
    }

    #[test]
    fn a_schema_a_form_cannot_enter_is_refused_rather_than_shown() {
        let error = Form::new(
            Verdict::Green,
            Some(&json!({"properties": {"a": {"type": "object"}}})),
        )
        .unwrap_err();
        assert!(error.contains("properties.a"), "{error}");
        assert_eq!(
            Form::new(Verdict::Green, None).unwrap().result().unwrap(),
            json!({"verdict": "GREEN"})
        );
    }
}