quillmark 0.73.0

Quillmark engine API
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
//! Tests for [`Quill::form`], [`Quill::blank_main`], and [`Quill::blank_card`].

use std::collections::HashMap;

use quillmark_core::quill::FileTreeNode;
use quillmark_core::Document;

use crate::{Quill, Quillmark};

use super::{Form, FormFieldSource};

/// Build a minimal [`Quill`] from inline YAML with no filesystem dependencies.
fn quill_from_yaml(yaml: &str) -> Quill {
    let mut files = HashMap::new();
    files.insert(
        "Quill.yaml".to_string(),
        FileTreeNode::File {
            contents: yaml.as_bytes().to_vec(),
        },
    );
    let root = FileTreeNode::Directory { files };
    Quillmark::new()
        .quill(root)
        .expect("quill_from_yaml: engine.quill failed")
}

#[test]
fn form_all_fields_present() {
    let quill = quill_from_yaml(
        r#"
quill:
  name: form_test
  version: "1.0"
  backend: typst
  description: Form view test

main:
  fields:
    title:
      type: string
    status:
      type: string
      default: draft
"#,
    );

    let md = "---\nQUILL: form_test\ntitle: \"My Title\"\nstatus: \"final\"\n---\n";
    let doc = Document::from_markdown(md).unwrap();

    let form = quill.form(&doc);

    assert!(form.diagnostics.is_empty(), "no diagnostics expected");
    assert!(form.cards.is_empty(), "no cards expected");

    let title_fv = form.main.values.get("title").expect("title field");
    assert_eq!(title_fv.source, FormFieldSource::Document);
    assert_eq!(
        title_fv.value.as_ref().and_then(|v| v.as_str()),
        Some("My Title")
    );

    let status_fv = form.main.values.get("status").expect("status field");
    assert_eq!(status_fv.source, FormFieldSource::Document);
    assert_eq!(
        status_fv.value.as_ref().and_then(|v| v.as_str()),
        Some("final")
    );
    // default still recorded even when document value present
    assert_eq!(
        status_fv.default.as_ref().and_then(|v| v.as_str()),
        Some("draft")
    );
}

#[test]
fn form_missing_field_uses_default() {
    let quill = quill_from_yaml(
        r#"
quill:
  name: form_defaults_test
  version: "1.0"
  backend: typst
  description: Missing fields use defaults

main:
  fields:
    title:
      type: string
      required: true
    status:
      type: string
      default: draft
    notes:
      type: string
"#,
    );

    // `title` and `notes` are absent from the document.
    // `title` is required — that produces a validation diagnostic.
    // `status` is absent but has a default.
    // `notes` is absent and has no default.
    let md = "---\nQUILL: form_defaults_test\n---\n";
    let doc = Document::from_markdown(md).unwrap();

    let form = quill.form(&doc);

    // `title` is required and missing → validation diagnostic
    assert!(
        form.diagnostics.iter().any(|d| d.message.contains("title")),
        "expected validation diagnostic for required 'title'; got: {:?}",
        form.diagnostics
    );

    let status_fv = form.main.values.get("status").expect("status field");
    assert_eq!(status_fv.source, FormFieldSource::Default);
    assert!(
        status_fv.value.is_none(),
        "value should be None when not in document"
    );
    assert_eq!(
        status_fv.default.as_ref().and_then(|v| v.as_str()),
        Some("draft")
    );

    let notes_fv = form.main.values.get("notes").expect("notes field");
    assert_eq!(notes_fv.source, FormFieldSource::Missing);
    assert!(notes_fv.value.is_none());
    assert!(notes_fv.default.is_none());
}

#[test]
fn form_unknown_card_tag_drops_card_and_emits_diagnostic() {
    let quill = quill_from_yaml(
        r#"
quill:
  name: unknown_card_test
  version: "1.0"
  backend: typst
  description: Unknown card tag test

main:
  fields:
    title:
      type: string

card_types:
  known_card:
    fields:
      note:
        type: string
"#,
    );

    let md = "---\nQUILL: unknown_card_test\ntitle: \"T\"\n---\n\n\
              ---\nCARD: known_card\nnote: \"A\"\n---\n\n\
              ---\nCARD: ghost_card\nnote: \"B\"\n---\n";
    let doc = Document::from_markdown(md).unwrap();

    let form = quill.form(&doc);

    // Only the known card appears in cards
    assert_eq!(form.cards.len(), 1, "only known_card should be projected");
    assert_eq!(form.cards[0].schema.name, "known_card");

    // A diagnostic for ghost_card
    let unknown_diag = form
        .diagnostics
        .iter()
        .find(|d| d.code.as_deref() == Some("form::unknown_card_tag"))
        .expect("expected unknown_card_tag diagnostic");
    assert!(
        unknown_diag.message.contains("ghost_card"),
        "diagnostic should name the tag: {:?}",
        unknown_diag.message
    );
}

#[test]
fn form_card_field_sources() {
    let quill = quill_from_yaml(
        r#"
quill:
  name: card_fields_test
  version: "1.0"
  backend: typst
  description: Card field source test

main:
  fields:
    title:
      type: string

card_types:
  indorsement:
    fields:
      signature_block:
        type: string
        required: true
      office:
        type: string
        default: HQ
      extra:
        type: string
"#,
    );

    // signature_block present, office absent (has default), extra absent (no default)
    let md = "---\nQUILL: card_fields_test\ntitle: \"T\"\n---\n\n\
              ---\nCARD: indorsement\nsignature_block: \"Col Smith\"\n---\n";
    let doc = Document::from_markdown(md).unwrap();

    let form = quill.form(&doc);
    assert_eq!(form.cards.len(), 1);
    let card = &form.cards[0];

    let sig = card.values.get("signature_block").expect("signature_block");
    assert_eq!(sig.source, FormFieldSource::Document);
    assert_eq!(
        sig.value.as_ref().and_then(|v| v.as_str()),
        Some("Col Smith")
    );

    let office = card.values.get("office").expect("office");
    assert_eq!(office.source, FormFieldSource::Default);
    assert!(office.value.is_none());
    assert_eq!(office.default.as_ref().and_then(|v| v.as_str()), Some("HQ"));

    let extra = card.values.get("extra").expect("extra");
    assert_eq!(extra.source, FormFieldSource::Missing);
    assert!(extra.value.is_none());
    assert!(extra.default.is_none());
}

#[test]
fn form_validation_diagnostics_appear() {
    let quill = quill_from_yaml(
        r#"
quill:
  name: validation_diag_test
  version: "1.0"
  backend: typst
  description: Validation diagnostics test

main:
  fields:
    count:
      type: integer
      required: true
"#,
    );

    // `count` is a string, not an integer → TypeMismatch validation error
    let md = "---\nQUILL: validation_diag_test\ncount: \"not-a-number\"\n---\n";
    let doc = Document::from_markdown(md).unwrap();

    let form = quill.form(&doc);

    let val_diag = form
        .diagnostics
        .iter()
        .find(|d| d.code.as_deref() == Some("form::validation_error"))
        .expect("expected a validation diagnostic");
    assert!(
        val_diag.message.contains("count"),
        "diagnostic should mention field name; got: {:?}",
        val_diag.message
    );
}

#[test]
fn form_serializes_cleanly() {
    // Smoke test: serde_json round-trip of Form.
    let quill = quill_from_yaml(
        r#"
quill:
  name: serial_test
  version: "1.0"
  backend: typst
  description: Serialization smoke test

main:
  fields:
    title:
      type: string
      default: Untitled
    count:
      type: integer
"#,
    );

    let md = "---\nQUILL: serial_test\ntitle: \"Hello\"\n---\n";
    let doc = Document::from_markdown(md).unwrap();
    let form = quill.form(&doc);

    let json = serde_json::to_string(&form).expect("Form must serialize");
    let back: Form = serde_json::from_str(&json).expect("Form must deserialize");

    // Name fields on CardSchema / FieldSchema are intentionally skipped on the
    // wire (the map key carries them), so round-trip identity does not hold for
    // those. Compare structural content instead.
    assert_eq!(form.main.values, back.main.values);
    assert_eq!(form.cards, back.cards);
    assert_eq!(form.diagnostics, back.diagnostics);
    assert_eq!(
        form.main.schema.fields.keys().collect::<Vec<_>>(),
        back.main.schema.fields.keys().collect::<Vec<_>>()
    );
    assert!(
        json.contains("title"),
        "serialized JSON should contain field name"
    );
}

#[test]
fn form_over_usaf_memo_fixture() {
    // Integration test: load the usaf_memo fixture quill and view the
    // bundled example.  Checks that every required field gets a deterministic
    // FormFieldSource and no projection panics.
    let quill_path = quillmark_fixtures::resource_path("quills/usaf_memo/0.1.0");
    let quill = Quillmark::new()
        .quill_from_path(quill_path)
        .expect("failed to load usaf_memo fixture");

    let example_md = quill.source().example().unwrap_or("");
    // If the example can't parse, skip gracefully (it uses YAML comments that
    // are valid but the field values may not match the schema exactly).
    let doc = match Document::from_markdown(example_md) {
        Ok(d) => d,
        Err(_) => return,
    };

    let form = quill.form(&doc);

    // The form must produce a FormCard for main with at least the required fields.
    assert!(
        !form.main.values.is_empty(),
        "main card view should have fields"
    );

    // Every field value must have a deterministic source.
    for (name, fv) in &form.main.values {
        match fv.source {
            FormFieldSource::Document => {
                assert!(
                    fv.value.is_some(),
                    "Document source must have value for {name}"
                );
            }
            FormFieldSource::Default => {
                assert!(
                    fv.value.is_none(),
                    "Default source must have no value for {name}"
                );
                assert!(
                    fv.default.is_some(),
                    "Default source must have default for {name}"
                );
            }
            FormFieldSource::Missing => {
                assert!(
                    fv.value.is_none(),
                    "Missing source must have no value for {name}"
                );
                assert!(
                    fv.default.is_none(),
                    "Missing source must have no default for {name}"
                );
            }
        }
    }

    // Serialization must not panic.
    let json = serde_json::to_string(&form).expect("form must serialize");
    assert!(!json.is_empty());
}

// ── blank_main / blank_card ─────────────────────────────────────────────────

#[test]
fn blank_main_has_default_or_missing_sources() {
    let quill = quill_from_yaml(
        r#"
quill:
  name: blank_main_test
  version: "1.0"
  backend: typst
  description: Blank main test

main:
  fields:
    title:
      type: string
      default: Untitled
    count:
      type: integer
"#,
    );

    let blank = quill.blank_main();

    let title = blank.values.get("title").expect("title field");
    assert_eq!(title.source, FormFieldSource::Default);
    assert!(title.value.is_none());
    assert_eq!(
        title.default.as_ref().and_then(|v| v.as_str()),
        Some("Untitled")
    );

    let count = blank.values.get("count").expect("count field");
    assert_eq!(count.source, FormFieldSource::Missing);
    assert!(count.value.is_none());
    assert!(count.default.is_none());
}

#[test]
fn blank_card_returns_form_card_for_known_type() {
    let quill = quill_from_yaml(
        r#"
quill:
  name: blank_card_test
  version: "1.0"
  backend: typst
  description: Blank card test

main:
  fields:
    title:
      type: string

card_types:
  indorsement:
    fields:
      office:
        type: string
        default: HQ
      from:
        type: string
"#,
    );

    let blank = quill
        .blank_card("indorsement")
        .expect("known card type should yield a FormCard");

    assert_eq!(blank.schema.name, "indorsement");

    let office = blank.values.get("office").expect("office field");
    assert_eq!(office.source, FormFieldSource::Default);
    assert!(office.value.is_none());
    assert_eq!(office.default.as_ref().and_then(|v| v.as_str()), Some("HQ"));

    let from = blank.values.get("from").expect("from field");
    assert_eq!(from.source, FormFieldSource::Missing);
    assert!(from.value.is_none());
    assert!(from.default.is_none());
}

#[test]
fn blank_card_returns_none_for_unknown_type() {
    let quill = quill_from_yaml(
        r#"
quill:
  name: blank_unknown_test
  version: "1.0"
  backend: typst
  description: Blank unknown card test

main:
  fields:
    title:
      type: string

card_types:
  known:
    fields:
      x:
        type: string
"#,
    );

    assert!(quill.blank_card("does_not_exist").is_none());
}