quillmark-core 0.92.1

Core types and functionality for Quillmark
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
//! End-to-end tests for the `$seed` system-metadata key.
//!
//! `$seed` is the per-card-kind seed-overlay map: parsers accept it, the
//! emitter preserves it, the storage DTO round-trips it, and the plate JSON
//! consumed by backends strips it. Unlike `$ext` the seeding layer interprets
//! it — see `crate::Quill::seed_card` and the `quill::seed` tests for layering.

use serde_json::json;

use crate::document::{Document, MetaKey, PayloadItem};

fn parse(src: &str) -> Document {
    Document::from_markdown(src).expect("source should parse")
}

// ── Parser ─────────────────────────────────────────────────────────────────

#[test]
fn seed_with_mapping_value_is_accepted() {
    let doc = parse(
        "\
~~~card-yaml
$quill: q@1.0
$kind: main
$seed:
  indorsement:
    from: 49 FW/CC
    signature_block:
      - \"JANE A. DOE, Col, USAF\"
      - Commander
title: Hi
~~~
",
    );
    let seed = doc.main().payload().seed().expect("$seed present");
    let ind = seed.get("indorsement").and_then(|v| v.as_object()).unwrap();
    assert_eq!(ind.get("from").and_then(|v| v.as_str()), Some("49 FW/CC"));
}

#[test]
fn seed_with_empty_mapping_is_preserved() {
    let doc = parse(
        "\
~~~card-yaml
$quill: q@1.0
$kind: main
$seed: {}
~~~
",
    );
    let seed = doc.main().payload().seed().expect("$seed present");
    assert!(seed.is_empty());
}

#[test]
fn seed_with_scalar_value_is_rejected() {
    let err = Document::from_markdown(
        "\
~~~card-yaml
$quill: q@1.0
$kind: main
$seed: just-a-string
~~~
",
    )
    .unwrap_err()
    .to_string();
    assert!(
        err.contains("Invalid `$seed`") && err.contains("mapping"),
        "expected $seed-must-be-mapping rejection, got: {err}",
    );
}

#[test]
fn seed_with_sequence_value_is_rejected() {
    let err = Document::from_markdown(
        "\
~~~card-yaml
$quill: q@1.0
$kind: main
$seed:
  - foo
  - bar
~~~
",
    )
    .unwrap_err()
    .to_string();
    assert!(
        err.contains("Invalid `$seed`") && err.contains("mapping"),
        "expected $seed-must-be-mapping rejection, got: {err}",
    );
}

#[test]
fn unknown_dollar_key_message_lists_seed() {
    // The closed-set rejection now advertises `$seed` as accepted.
    let err = Document::from_markdown(
        "\
~~~card-yaml
$quill: q@1.0
$kind: main
$bogus: x
~~~
",
    )
    .unwrap_err()
    .to_string();
    assert!(
        err.contains("$seed"),
        "closed-set message should list $seed: {err}"
    );
}

#[test]
fn seed_on_composable_card_is_rejected() {
    // `$seed` is root-only (like `$quill`): a composable block carrying it is a
    // parse error, not silently-inert data.
    let err = Document::from_markdown(
        "\
~~~card-yaml
$quill: q@1.0
$kind: main
~~~

~~~card-yaml
$kind: indorsement
$seed:
  note:
    from: X
~~~
",
    )
    .unwrap_err()
    .to_string();
    assert!(
        err.contains("must not carry `$seed`"),
        "expected composable-$seed rejection, got: {err}",
    );
}

// ── Emit / round-trip ──────────────────────────────────────────────────────

#[test]
fn seed_round_trips_through_markdown() {
    let src = "\
~~~card-yaml
$quill: q@1.0
$kind: main
$seed:
  indorsement:
    from: 49 FW/CC
title: Body
~~~

Body content.
";
    let doc = parse(src);
    let emitted = doc.to_markdown();
    let reparsed = parse(&emitted);
    assert_eq!(doc, reparsed);
    assert!(
        emitted.contains("$seed:\n  indorsement:\n    from: 49 FW/CC\n"),
        "unexpected emit:\n{emitted}",
    );
}

#[test]
fn empty_seed_emits_as_inline_braces() {
    let src = "\
~~~card-yaml
$quill: q@1.0
$kind: main
$seed: {}
~~~
";
    let doc = parse(src);
    let emitted = doc.to_markdown();
    assert!(
        emitted.contains("$seed: {}\n"),
        "expected `$seed: {{}}` literal in emit, got:\n{emitted}",
    );
    let reparsed = parse(&emitted);
    assert_eq!(doc, reparsed);
}

#[test]
fn comments_inside_seed_round_trip() {
    // Exercises the `$seed` branches of the nested-comment machinery
    // (parse → per-item nested_comments → emit, and the storage-DTO flatten).
    let src = "\
~~~card-yaml
$quill: q@1.0
$kind: main
$seed:
  indorsement:
    # pin the squadron office symbol
    from: 49 FW/CC
~~~
";
    let doc = parse(src);
    let emitted = doc.to_markdown();
    assert!(
        emitted.contains("# pin the squadron office symbol"),
        "nested $seed comment must survive emit:\n{emitted}",
    );
    assert_eq!(doc, parse(&emitted));

    // And it survives the storage DTO round-trip too.
    let json = serde_json::to_string(&doc).unwrap();
    let restored: Document = serde_json::from_str(&json).unwrap();
    assert_eq!(doc, restored);
}

#[test]
fn seed_emit_is_idempotent() {
    let doc = parse(
        "\
~~~card-yaml
$quill: q@1.0
$kind: main
$seed:
  note:
    a: 1
~~~
",
    );
    let once = doc.to_markdown();
    let twice = parse(&once).to_markdown();
    assert_eq!(once, twice);
}

// ── Programmatic construction ──────────────────────────────────────────────

#[test]
fn set_seed_inserts_after_ext_and_before_user_fields() {
    let mut doc = parse(
        "\
~~~card-yaml
$quill: q@1.0
$kind: main
$id: rev-1
$ext:
  a: 1
title: Hi
~~~
",
    );
    let mut seed = serde_json::Map::new();
    seed.insert("indorsement".into(), json!({ "from": "X" }));
    doc.main_mut().payload_mut().set_seed(seed);

    let items = doc.main().payload().items();
    // Canonical order: $quill, $kind, $id, $ext, $seed, then user fields.
    assert!(matches!(items[0], PayloadItem::Quill { .. }));
    assert!(matches!(items[1], PayloadItem::Kind { .. }));
    assert!(matches!(items[2], PayloadItem::Id { .. }));
    assert!(matches!(
        items[3],
        PayloadItem::Meta {
            key: MetaKey::Ext,
            ..
        }
    ));
    assert!(matches!(
        items[4],
        PayloadItem::Meta {
            key: MetaKey::Seed,
            ..
        }
    ));
    assert!(matches!(items[5], PayloadItem::Field { .. }));
}

#[test]
fn seed_overlay_parses_with_body() {
    let doc = parse(
        "\
~~~card-yaml
$quill: q@1.0
$kind: main
$seed:
  indorsement:
    from: 49 FW/CC
    $body: \"Standard endorsement text.\"
~~~
",
    );
    // The overlay is read off the main card's `$seed` map and parsed via
    // `SeedOverlay::from_json` (there is no `Document::seed` convenience).
    let seed = doc.main().seed();
    let overlay = seed
        .and_then(|m| m.get("indorsement"))
        .and_then(crate::SeedOverlay::from_json)
        .expect("overlay present");
    assert_eq!(
        overlay.fields.get("from").and_then(|v| v.as_str()),
        Some("49 FW/CC"),
    );
    assert_eq!(overlay.body.as_deref(), Some("Standard endorsement text."));
    // `$body` is the body override, not a field.
    assert!(!overlay.fields.contains_key("$body"));
    // An undeclared kind yields no overlay.
    assert!(seed.and_then(|m| m.get("missing")).is_none());
}

#[test]
fn seed_namespace_mutators_preserve_siblings() {
    let mut doc = parse(
        "\
~~~card-yaml
$quill: q@1.0
$kind: main
~~~
",
    );
    let card = doc.main_mut();
    card.set_seed_namespace("indorsement", json!({ "from": "A" }))
        .unwrap();
    card.set_seed_namespace("attachment", json!({ "label": "B" }))
        .unwrap();
    assert_eq!(card.seed().map(|m| m.len()), Some(2));

    // Removing one kind leaves the sibling intact.
    let removed = card.remove_seed_namespace("indorsement").unwrap();
    assert_eq!(removed.get("from").and_then(|v| v.as_str()), Some("A"));
    assert_eq!(card.seed().map(|m| m.len()), Some(1));
    assert!(card.seed().unwrap().contains_key("attachment"));

    // Removing the last kind drops `$seed` entirely (not `$seed: {}`).
    card.remove_seed_namespace("attachment");
    assert!(card.seed().is_none());
}

#[test]
fn set_seed_namespace_rejects_invalid_and_reserved_kinds() {
    // `$seed` is keyed by composable card-kind, so the writer must reject
    // names that could never name a composable card (unlike free-form `$ext`).
    let mut doc = parse(
        "\
~~~card-yaml
$quill: q@1.0
$kind: main
~~~
",
    );
    let card = doc.main_mut();

    assert!(matches!(
        card.set_seed_namespace("main", json!({ "from": "A" })),
        Err(crate::document::EditError::ReservedKind)
    ));
    assert!(matches!(
        card.set_seed_namespace("Bad-Kind", json!({ "from": "A" })),
        Err(crate::document::EditError::InvalidKindName(_))
    ));

    // A rejected write leaves the card untouched — no `$seed` map appears.
    assert!(card.seed().is_none());
}

#[test]
fn seed_overlay_drops_reserved_keys_other_than_body() {
    // An overlay only ever carries user fields plus the reserved `$body`;
    // any other `$`-key must be dropped, never smuggled in as a user field.
    let overlay = crate::SeedOverlay::from_json(&json!({
        "from": "49 FW/CC",
        "$body": "Body override.",
        "$kind": "smuggled",
        "$quill": "x@1.0",
    }))
    .expect("overlay is an object");

    assert_eq!(overlay.body.as_deref(), Some("Body override."));
    assert!(overlay.fields.contains_key("from"));
    assert!(!overlay.fields.contains_key("$kind"));
    assert!(!overlay.fields.contains_key("$quill"));
    assert_eq!(
        overlay.fields.len(),
        1,
        "only the user field should survive"
    );
}

// ── Plate JSON ─────────────────────────────────────────────────────────────

#[test]
fn seed_is_stripped_from_plate_json() {
    // Backends must never see `$seed` — it is curation data, not template data.
    let doc = parse(
        "\
~~~card-yaml
$quill: q@1.0
$kind: main
$seed:
  indorsement:
    from: \"Should not reach the backend\"
title: Hi
~~~
",
    );
    let plate = doc.to_plate_json();
    let obj = plate.as_object().expect("plate is an object");
    assert!(
        !obj.contains_key("$seed"),
        "plate must not contain `$seed`: {plate}"
    );
    assert!(
        !obj.contains_key("seed"),
        "plate must not contain `seed`: {plate}"
    );
    assert_eq!(obj.get("title").and_then(|v| v.as_str()), Some("Hi"));
    assert!(obj.contains_key("$quill"));
    assert!(obj.contains_key("$cards"));
}

// ── Storage DTO ────────────────────────────────────────────────────────────

#[test]
fn seed_round_trips_through_serde_json() {
    let doc = parse(
        "\
~~~card-yaml
$quill: q@1.0
$kind: main
$seed:
  indorsement:
    from: 49 FW/CC
    $body: \"Body override.\"
title: Hi
~~~

Body.
",
    );
    let json = serde_json::to_string(&doc).unwrap();
    let restored: Document = serde_json::from_str(&json).unwrap();
    assert_eq!(doc, restored);
    assert_eq!(doc.to_markdown(), restored.to_markdown());

    // The DTO carries `"type": "seed"` under the current 0.92.0 schema tag.
    assert!(
        json.contains("\"type\":\"seed\""),
        "expected seed variant in DTO: {json}"
    );
    assert!(
        json.contains("quillmark/document@0.92.0"),
        "expected 0.92.0 schema tag: {json}",
    );
}