ggen-engine 26.10.11

SPARQL-in-Tera code generation engine (vendored, renamed from praxis/crates/ggen)
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
//! Chicago-TDD end-to-end tests for the frontmatter fields added under the
//! PROJ-302 extension to `crates/ggen`: `sh_before`/`sh_after`, `backup`,
//! `shape`, `determinism`, `freeze_policy`/`freeze_slots_dir`, `from`.
//! Real filesystem, real sync pipeline — no mocks.

#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

use std::path::Path;

use ggen_engine::receipt_chain_seam::ReceiptRecord;
use ggen_engine::sync::{sync, SyncOptions, RECEIPT_REL_PATH};
use tempfile::TempDir;

const GGEN_TOML: &str = r#"
[project]
name = "demo"

[ontology]
source = "ontology.ttl"

[templates]
dir = "templates"
"#;

const ONTOLOGY: &str = r#"
@prefix ex: <http://example.org/> .
ex:alice ex:name "alice" .
"#;

fn scaffold(root: &Path) {
    std::fs::write(root.join("ggen.toml"), GGEN_TOML).expect("write ggen.toml");
    std::fs::write(root.join("ontology.ttl"), ONTOLOGY).expect("write ontology");
    std::fs::create_dir_all(root.join("templates")).expect("mkdir templates");
}

fn write_template(root: &Path, name: &str, content: &str) {
    std::fs::write(root.join("templates").join(name), content).expect("write template");
}

#[test]
fn sh_before_and_sh_after_run_around_the_write() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());
    write_template(
        dir.path(),
        "hook.tmpl",
        "---\nto: out.txt\nsh_before: \"echo before >> hooks.log\"\nsh_after: \"echo after >> hooks.log\"\n---\nbody\n",
    );

    sync(
        dir.path(),
        SyncOptions {
            consumer_mode: Default::default(),
            dry_run: false,
            ..Default::default()
        },
    )
    .expect("sync");

    let log = std::fs::read_to_string(dir.path().join("hooks.log")).expect("hooks ran");
    assert_eq!(log, "before\nafter\n");
}

#[test]
fn dangerous_sh_command_is_refused() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());
    write_template(
        dir.path(),
        "evil.tmpl",
        "---\nto: out.txt\nsh_before: \"rm -rf /\"\n---\nbody\n",
    );

    let err = sync(
        dir.path(),
        SyncOptions {
            consumer_mode: Default::default(),
            dry_run: false,
            ..Default::default()
        },
    )
    .expect_err("must refuse");
    assert!(err.to_string().contains("FM-SHELL-001"), "{err}");
}

#[test]
fn dry_run_never_executes_shell_hooks() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());
    write_template(
        dir.path(),
        "hook.tmpl",
        "---\nto: out.txt\nsh_before: \"echo ran >> hooks.log\"\n---\nbody\n",
    );

    sync(
        dir.path(),
        SyncOptions {
            consumer_mode: Default::default(),
            dry_run: true,
            ..Default::default()
        },
    )
    .expect("dry run");
    assert!(
        !dir.path().join("hooks.log").exists(),
        "dry run must not run sh_before"
    );
}

#[test]
fn backup_copies_existing_file_before_force_overwrite() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());
    std::fs::write(dir.path().join("out.txt"), "old content\n").expect("seed");
    write_template(
        dir.path(),
        "b.tmpl",
        "---\nto: out.txt\nforce: true\nbackup: true\n---\nnew content\n",
    );

    sync(
        dir.path(),
        SyncOptions {
            consumer_mode: Default::default(),
            dry_run: false,
            ..Default::default()
        },
    )
    .expect("sync");

    assert_eq!(
        std::fs::read_to_string(dir.path().join("out.txt")).expect("new"),
        "new content\n"
    );
    assert_eq!(
        std::fs::read_to_string(dir.path().join("out.txt.bak")).expect("backup"),
        "old content\n"
    );
}

#[test]
fn missing_shape_file_is_refused() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());
    write_template(
        dir.path(),
        "s.tmpl",
        "---\nto: out.txt\nshape:\n  - shapes/missing.ttl\n---\nbody\n",
    );

    let err = sync(
        dir.path(),
        SyncOptions {
            consumer_mode: Default::default(),
            dry_run: false,
            ..Default::default()
        },
    )
    .expect_err("must refuse");
    // FM-TPL-014, not FM-TPL-008: a missing `shape:` file is a distinct
    // failure cause from `from:` path traversal, which keeps FM-TPL-008 (see
    // `check_shape_files_exist`'s doc comment in sync.rs).
    assert!(err.to_string().contains("FM-TPL-014"), "{err}");
}

#[test]
fn existing_shape_file_passes() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());
    std::fs::create_dir_all(dir.path().join("shapes")).expect("mkdir shapes");
    std::fs::write(dir.path().join("shapes/present.ttl"), "# shape\n").expect("write shape");
    write_template(
        dir.path(),
        "s.tmpl",
        "---\nto: out.txt\nshape:\n  - shapes/present.ttl\n---\nbody\n",
    );

    sync(
        dir.path(),
        SyncOptions {
            consumer_mode: Default::default(),
            dry_run: false,
            ..Default::default()
        },
    )
    .expect("sync must succeed");
}

#[test]
fn determinism_true_passes_for_a_pure_template() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());
    write_template(
        dir.path(),
        "d.tmpl",
        "---\nto: out.txt\ndeterminism: true\nsparql:\n  people: SELECT ?name WHERE { ?s <http://example.org/name> ?name }\n---\n{% for row in results %}{{ row.name }}{% endfor %}",
    );

    let report = sync(
        dir.path(),
        SyncOptions {
            consumer_mode: Default::default(),
            dry_run: false,
            ..Default::default()
        },
    )
    .expect("sync must succeed");
    assert_eq!(report.written, vec![std::path::PathBuf::from("out.txt")]);
}

#[test]
fn freeze_always_skips_once_target_exists() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());
    std::fs::write(dir.path().join("out.txt"), "hand-written, never touch\n").expect("seed");
    write_template(
        dir.path(),
        "f.tmpl",
        "---\nto: out.txt\nforce: true\nfreeze_policy: always\n---\ngenerated content\n",
    );

    let report = sync(
        dir.path(),
        SyncOptions {
            consumer_mode: Default::default(),
            dry_run: false,
            ..Default::default()
        },
    )
    .expect("sync");
    assert!(
        report.written.is_empty(),
        "freeze:always must skip, got: {:?}",
        report.written
    );
    assert_eq!(
        std::fs::read_to_string(dir.path().join("out.txt")).expect("unchanged"),
        "hand-written, never touch\n"
    );
}

/// The andon/obligation consequence of freeze:always drift (O-5 /
/// `FreezeAlwaysNoDriftDetection`, second half): the scenario above
/// (`freeze_always_skips_once_target_exists`) already proves the file is
/// never overwritten. This proves the receipt now honestly records that
/// drift was observed -- a `FROZEN-DRIFT:out.txt` obligation, a Quarantined
/// (not Admitted) admission item for `out.txt`, and the derived Andon
/// pulled to at least Yellow by it -- rather than silently reporting
/// Admitted/Green as if nothing were amiss.
#[test]
fn freeze_always_drift_is_quarantined_and_creates_an_obligation_in_the_real_receipt() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());
    std::fs::write(dir.path().join("out.txt"), "hand-written, never touch\n").expect("seed");
    write_template(
        dir.path(),
        "f.tmpl",
        "---\nto: out.txt\nfreeze_policy: always\n---\ngenerated content\n",
    );

    sync(
        dir.path(),
        SyncOptions {
            consumer_mode: Default::default(),
            dry_run: false,
            ..Default::default()
        },
    )
    .expect("sync");

    let raw = std::fs::read_to_string(dir.path().join(RECEIPT_REL_PATH)).expect("read receipt");
    let whole: serde_json::Value = serde_json::from_str(&raw).expect("parse json");
    let record_json = whole.get("record").expect("record field present").clone();
    let record: ReceiptRecord = serde_json::from_value(record_json).expect("parse record");
    let v2 = record.v2.expect("v2 epoch present");

    let admission_items = match &v2.admission {
        ggen_engine::receipt_chain_seam::epoch::AdmissionLedger::Recorded(items) => items,
        ggen_engine::receipt_chain_seam::epoch::AdmissionLedger::LegacyUnrecorded => {
            panic!("expected a recorded v2 admission ledger")
        }
    };
    let out_item = admission_items
        .iter()
        .find(|i| i.evidence_id == "out.txt")
        .expect("out.txt has an admission item");
    assert_eq!(
        out_item.decision,
        ggen_engine::receipt_chain_seam::epoch::AdmissionDecision::Quarantined,
        "drifted frozen file must be Quarantined, not Admitted: {out_item:?}"
    );
    assert_eq!(
        out_item.observed_outcome,
        ggen_engine::receipt_chain_seam::epoch::ObservedOutcome::Fail
    );
    assert_eq!(
        out_item.obligations_created,
        vec!["FROZEN-DRIFT:out.txt".to_string()]
    );
    assert!(out_item.reason.contains("DRIFT:"), "{}", out_item.reason);

    // A single Quarantined item pulls the derived Andon to at least Yellow
    // (never Green) -- proving the observation actually affects standing,
    // not just an unread field.
    assert_ne!(
        v2.andon,
        ggen_engine::receipt_chain_seam::epoch::AndonLevel::Green,
        "a quarantined frozen-drift item must not leave Andon at Green"
    );
}

/// Sabotage/control for the test above: identical (non-drifted) frozen
/// content must NOT be quarantined and must NOT create an obligation --
/// proving the drift detection actually discriminates, not just always
/// flags frozen files.
#[test]
fn freeze_always_no_drift_stays_admitted_with_no_obligation() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());
    std::fs::write(dir.path().join("out.txt"), "generated content\n").expect("seed identical");
    write_template(
        dir.path(),
        "f.tmpl",
        "---\nto: out.txt\nfreeze_policy: always\n---\ngenerated content\n",
    );

    sync(
        dir.path(),
        SyncOptions {
            consumer_mode: Default::default(),
            dry_run: false,
            ..Default::default()
        },
    )
    .expect("sync");

    let raw = std::fs::read_to_string(dir.path().join(RECEIPT_REL_PATH)).expect("read receipt");
    let whole: serde_json::Value = serde_json::from_str(&raw).expect("parse json");
    let record_json = whole.get("record").expect("record field present").clone();
    let record: ReceiptRecord = serde_json::from_value(record_json).expect("parse record");
    let v2 = record.v2.expect("v2 epoch present");
    let admission_items = match &v2.admission {
        ggen_engine::receipt_chain_seam::epoch::AdmissionLedger::Recorded(items) => items,
        ggen_engine::receipt_chain_seam::epoch::AdmissionLedger::LegacyUnrecorded => {
            panic!("expected a recorded v2 admission ledger")
        }
    };
    let out_item = admission_items
        .iter()
        .find(|i| i.evidence_id == "out.txt")
        .expect("out.txt has an admission item");
    assert_eq!(
        out_item.decision,
        ggen_engine::receipt_chain_seam::epoch::AdmissionDecision::Admitted
    );
    assert!(out_item.obligations_created.is_empty());
    assert!(!out_item.reason.contains("DRIFT:"), "{}", out_item.reason);
}

#[test]
fn freeze_checksum_allows_regen_until_manual_edit_then_protects_it() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());
    // Deliberately no `force: true`: this test proves freeze intercepts
    // *before* the normal decision table would otherwise refuse a
    // differing-content overwrite with FM-WRITE-005.
    write_template(
        dir.path(),
        "f.tmpl",
        "---\nto: out.txt\nfreeze_policy: checksum\nfreeze_slots_dir: .ggen-freeze\n---\ngenerated v1\n",
    );

    // First sync: target absent, writes normally and records a checksum.
    let first = sync(
        dir.path(),
        SyncOptions {
            consumer_mode: Default::default(),
            dry_run: false,
            ..Default::default()
        },
    )
    .expect("first sync");
    assert_eq!(first.written, vec![std::path::PathBuf::from("out.txt")]);
    assert!(
        dir.path().join(".ggen-freeze/out.txt.blake3").exists(),
        "checksum recorded"
    );

    // Second sync: on-disk content still matches the recorded checksum (no
    // human edit) and equals the rendered body, so it's a normal
    // Skipped(unchanged) — not a freeze skip.
    let second = sync(
        dir.path(),
        SyncOptions {
            consumer_mode: Default::default(),
            dry_run: false,
            ..Default::default()
        },
    )
    .expect("second sync");
    assert!(second.written.is_empty());
    assert!(
        second.skipped[0].1.contains("unchanged"),
        "{:?}",
        second.skipped
    );

    // Simulate a human hand-editing the generated file.
    std::fs::write(dir.path().join("out.txt"), "hand-edited by a human\n").expect("hand edit");

    let third = sync(
        dir.path(),
        SyncOptions {
            consumer_mode: Default::default(),
            dry_run: false,
            ..Default::default()
        },
    )
    .expect("third sync");
    assert!(
        third.written.is_empty(),
        "checksum mismatch must freeze the file, got: {:?}",
        third.written
    );
    assert_eq!(
        std::fs::read_to_string(dir.path().join("out.txt")).expect("protected"),
        "hand-edited by a human\n",
        "the human edit must survive"
    );
}

#[test]
fn freeze_checksum_without_slots_dir_is_refused() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());
    std::fs::write(dir.path().join("out.txt"), "existing\n").expect("seed");
    write_template(
        dir.path(),
        "f.tmpl",
        "---\nto: out.txt\nforce: true\nfreeze_policy: checksum\n---\ngenerated\n",
    );

    let err = sync(
        dir.path(),
        SyncOptions {
            consumer_mode: Default::default(),
            dry_run: false,
            ..Default::default()
        },
    )
    .expect_err("must refuse");
    assert!(err.to_string().contains("FM-WRITE-006"), "{err}");
}

#[test]
fn from_field_loads_body_from_referenced_sibling_file() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());
    std::fs::write(
        dir.path().join("templates/shared_body.tera"),
        "shared body content\n",
    )
    .expect("write shared body");
    write_template(
        dir.path(),
        "f.tmpl",
        "---\nto: out.txt\nfrom: shared_body.tera\n---\nignored inline body\n",
    );

    sync(
        dir.path(),
        SyncOptions {
            consumer_mode: Default::default(),
            dry_run: false,
            ..Default::default()
        },
    )
    .expect("sync");
    assert_eq!(
        std::fs::read_to_string(dir.path().join("out.txt")).expect("output"),
        "shared body content\n"
    );
}

#[test]
fn from_field_path_traversal_outside_template_dir_is_refused() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());
    // A secret file OUTSIDE the templates/ directory the `from:` escape
    // attempts to read.
    std::fs::write(dir.path().join("secret.txt"), "top secret\n").expect("write secret");
    write_template(
        dir.path(),
        "f.tmpl",
        "---\nto: out.txt\nfrom: ../secret.txt\n---\nignored inline body\n",
    );

    let err = sync(
        dir.path(),
        SyncOptions {
            consumer_mode: Default::default(),
            dry_run: false,
            ..Default::default()
        },
    )
    .expect_err("traversal outside the template's own directory must be refused");
    let msg = err.to_string();
    assert!(
        msg.contains("from:"),
        "error should name the from: field: {msg}"
    );
    assert!(
        !dir.path().join("out.txt").exists(),
        "no output must be written on refusal"
    );
}