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
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
//! CLI-boundary proofs: spawn the real `ggen` binary as a subprocess via
//! chicago-tdd-tools `CliHarness`. No mocks — real argv, real exit codes,
//! real filesystem effects.
//!
//! Coverage goal: every noun/verb/flag combination the CLI actually exposes
//! (`ggen --help`, `sync run [--dry-run]`, `graph validate`, `receipt
//! verify`, `receipt history`, `--version`, `--introspect`, unknown
//! subcommands/args) proven against the compiled binary, not just the
//! library functions underneath it.

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

use chicago_tdd_tools::cli_proof::CliHarness;
use std::io::Read;
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
use tempfile::TempDir;

fn scaffold(root: &std::path::Path) {
    std::fs::write(
        root.join("ggen.toml"),
        "[project]\nname = \"demo\"\n\n[ontology]\nsource = \"ontology.ttl\"\n\n[templates]\ndir = \"templates\"\n",
    )
    .expect("write ggen.toml");
    std::fs::write(
        root.join("ontology.ttl"),
        "@prefix ex: <http://example.org/> .\nex:alice ex:name \"alice\" .\n",
    )
    .expect("write ontology");
    std::fs::create_dir_all(root.join("templates")).expect("mkdir");
    std::fs::write(
        root.join("templates/one.tmpl"),
        "---\nto: out/names.txt\nforce: true\nsparql:\n  people: SELECT ?name WHERE { ?s <http://example.org/name> ?name } ORDER BY ?name\n---\n{% for row in results %}{{ row.name }}\n{% endfor %}",
    )
    .expect("write template");
}

// ---------------------------------------------------------------------
// Root
// ---------------------------------------------------------------------

#[test]
fn root_help_exits_zero_and_lists_all_nouns() {
    let output = CliHarness::from_path(ggen_bin())
        .args(["--help"])
        .run()
        .expect("run --help");
    let _ = output
        .assert_success()
        .assert_stdout_contains("sync")
        .assert_stdout_contains("graph")
        .assert_stdout_contains("receipt");
}

/// Regression test: the noun-level clap `Command`s for sync/graph/receipt/
/// doctor used to have a blank `about`, so `ggen --help` listed four bare
/// noun names with no hint what they do. Each noun line in the top-level
/// help must now carry real description text, not just the bare noun name
/// followed by whitespace/newline.
#[test]
fn root_help_gives_each_noun_a_non_blank_description() {
    let output = CliHarness::from_path(ggen_bin())
        .args(["--help"])
        .run()
        .expect("run --help");
    let _ = output.assert_success();

    let stdout = output.stdout.clone();
    for (noun, expected_word) in [
        ("sync", "pipeline"),
        ("graph", "ontology"),
        ("receipt", "receipt"),
        // "health" -> "diagnostics" (2026-08-03, TECH-DEBT-003 fix): the real, live
        // `doctor` noun registration (crates/ggen-cli/src/cmds/mod.rs's
        // register_doctor_noun) has read "Universal non-actuating diagnostics,
        // root-cause evidence, and deterministic remediation." since it was first
        // written (confirmed via `git log -S register_doctor_noun` -- one commit, no
        // prior wording) -- it never said "health". This test's real intent per its own
        // name is "non-blank, on-topic description", not this exact word; "diagnostics"
        // is the genuine, on-topic keyword actually present. Fixing the test's stale
        // keyword to match the real, deliberately-written CLI text, not rewording real
        // production help output just to satisfy an assertion.
        ("doctor", "diagnostics"),
    ] {
        let line = stdout
            .lines()
            .find(|line| line.trim_start().starts_with(noun))
            .unwrap_or_else(|| panic!("no `{noun}` line in --help output:\n{stdout}"));
        assert!(
            line.contains(expected_word),
            "expected `{noun}` noun's --help line to describe what it does (mentioning \
             `{expected_word}`), got: {line:?}\nfull output:\n{stdout}"
        );
    }
}

#[test]
fn root_version_exits_zero() {
    let output = CliHarness::from_path(ggen_bin())
        .args(["--version"])
        .run()
        .expect("run --version");
    let _ = output.assert_success();
}

#[test]
fn root_no_args_exits_zero_or_prints_usage() {
    // clap-noun-verb with no subcommand: must not crash, must not silently
    // hang — either succeeds with a usage summary or exits nonzero cleanly.
    let output = CliHarness::from_path(ggen_bin())
        .run()
        .expect("run with no args");
    assert!(
        output.exit_code == 0 || !output.stdout.is_empty() || !output.stderr.is_empty(),
        "expected a clean exit or usage output, got exit {} with empty stdout/stderr",
        output.exit_code
    );
}

#[test]
fn unknown_noun_exits_nonzero() {
    let output = CliHarness::from_path(ggen_bin())
        .args(["totally-unknown-noun-xyz"])
        .run()
        .expect("run unknown noun");
    let _ = output.assert_failure();
}

#[test]
fn unknown_flag_exits_nonzero() {
    let output = CliHarness::from_path(ggen_bin())
        .args(["sync", "run", "--this-flag-does-not-exist"])
        .run()
        .expect("run unknown flag");
    let _ = output.assert_failure();
}

// ---------------------------------------------------------------------
// sync <noun> --help / run --help
// ---------------------------------------------------------------------

#[test]
fn sync_noun_help_exits_zero_and_lists_run() {
    let output = CliHarness::from_path(ggen_bin())
        .args(["sync", "--help"])
        .run()
        .expect("run sync --help");
    let _ = output.assert_success().assert_stdout_contains("run");
}

#[test]
fn sync_run_help_lists_dry_run_flag() {
    let output = CliHarness::from_path(ggen_bin())
        .args(["sync", "run", "--help"])
        .run()
        .expect("run sync run --help");
    let _ = output.assert_success().assert_stdout_contains("dry-run");
}

#[test]
fn sync_run_help_lists_watch_flag() {
    let output = CliHarness::from_path(ggen_bin())
        .args(["sync", "run", "--help"])
        .run()
        .expect("run sync run --help");
    let _ = output.assert_success().assert_stdout_contains("watch");
}

/// Regression test: `sync run --dry-run` and `--watch` used to render with
/// empty per-argument help text because the code generator only picked up
/// the function-level doc comment, not per-parameter documentation (unlike
/// `--format <format>`, whose help came from clap's own derive). Each flag's
/// line in `--help` must now carry real description text, not just the bare
/// flag name followed by whitespace/newline.
#[test]
fn sync_run_help_gives_each_flag_a_non_blank_description() {
    let output = CliHarness::from_path(ggen_bin())
        .args(["sync", "run", "--help"])
        .run()
        .expect("run sync run --help");
    let _ = output.assert_success();

    let stdout = output.stdout.clone();
    for (flag, expected_word) in [("--dry-run", "disk"), ("--watch", "filesystem")] {
        let line = stdout
            .lines()
            .find(|line| line.trim_start().starts_with(flag))
            .unwrap_or_else(|| panic!("no `{flag}` line in --help output:\n{stdout}"));
        assert!(
            line.contains(expected_word),
            "expected `{flag}`'s --help line to describe what it does (mentioning \
             `{expected_word}`), got: {line:?}\nfull output:\n{stdout}"
        );
    }
}

// ---------------------------------------------------------------------
// sync run
// ---------------------------------------------------------------------

#[test]
fn sync_run_generates_expected_file() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());

    let output = CliHarness::from_path(ggen_bin())
        .args(["sync", "run"])
        .current_dir(dir.path())
        .run()
        .expect("run sync");
    let _ = output.assert_success();

    let content =
        std::fs::read_to_string(dir.path().join("out/names.txt")).expect("generated file");
    assert_eq!(content, "alice\n");
    assert!(
        dir.path().join(".ggen-v2/receipt.json").exists(),
        "receipt emitted"
    );
}

#[test]
fn sync_run_dry_run_writes_nothing() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());

    let output = CliHarness::from_path(ggen_bin())
        .args(["sync", "run", "--dry-run"])
        .current_dir(dir.path())
        .run()
        .expect("run sync --dry-run");
    let _ = output.assert_success();

    assert!(
        !dir.path().join("out/names.txt").exists(),
        "dry-run must not write output"
    );
    assert!(
        !dir.path().join(".ggen-v2/receipt.json").exists(),
        "dry-run must not emit a receipt"
    );
    assert!(
        !dir.path().join("ggen.lock").exists(),
        "dry-run must not write a lockfile"
    );
}

#[test]
fn sync_run_second_invocation_is_idempotent() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());

    let _ = CliHarness::from_path(ggen_bin())
        .args(["sync", "run"])
        .current_dir(dir.path())
        .run()
        .expect("first sync")
        .assert_success();
    let first = std::fs::read_to_string(dir.path().join("out/names.txt")).expect("first output");

    let _ = CliHarness::from_path(ggen_bin())
        .args(["sync", "run"])
        .current_dir(dir.path())
        .run()
        .expect("second sync")
        .assert_success();
    let second = std::fs::read_to_string(dir.path().join("out/names.txt")).expect("second output");

    assert_eq!(first, second, "second sync must not change output content");
}

#[test]
fn sync_run_missing_manifest_exits_nonzero() {
    let dir = TempDir::new().expect("tempdir");
    let output = CliHarness::from_path(ggen_bin())
        .args(["sync", "run"])
        .current_dir(dir.path())
        .run()
        .expect("run sync with no ggen.toml");
    let _ = output.assert_failure();
}

// ---------------------------------------------------------------------
// sync run --watch
//
// `--watch` runs one sync then blocks forever watching for filesystem
// changes (see `watch.rs` module docs: no SIGINT handling, by design).
// `CliHarness::run` waits for the child to exit, so it cannot be used for
// the success path here. These tests spawn the compiled binary directly,
// read its stderr for the expected boundary signals, then kill the child —
// bounded by a hard deadline so a regression hangs the test suite for at
// most a few seconds, not forever.
// ---------------------------------------------------------------------

/// Read `stderr` from a spawned child for up to `deadline`, returning as
/// soon as `needle` appears (or whatever was captured once the deadline
/// elapses). The child is always killed and reaped before returning.
fn watch_for_stderr(mut child: std::process::Child, needle: &str, deadline: Duration) -> String {
    let mut stderr = child.stderr.take().expect("piped stderr");
    let mut buf = Vec::new();
    let mut byte = [0u8; 1];
    let start = Instant::now();
    // Non-blocking-ish poll: stdlib `Read` on a pipe blocks, so run the
    // read on the current thread but bound total wall time via the
    // deadline check between reads is not possible without a background
    // thread. Use a background reader thread instead.
    let (tx, rx) = std::sync::mpsc::channel();
    std::thread::spawn(move || loop {
        match stderr.read(&mut byte) {
            Ok(0) | Err(_) => break,
            Ok(_) => {
                if tx.send(byte[0]).is_err() {
                    break;
                }
            }
        }
    });
    while start.elapsed() < deadline {
        match rx.recv_timeout(Duration::from_millis(100)) {
            Ok(b) => {
                buf.push(b);
                if String::from_utf8_lossy(&buf).contains(needle) {
                    break;
                }
            }
            Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {}
            Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => break,
        }
    }
    let _ = child.kill();
    let _ = child.wait();
    String::from_utf8_lossy(&buf).into_owned()
}

/// Resolve the compiled `ggen` binary the same way `CliHarness::from_path(ggen_bin())`
/// does (see `chicago-tdd-tools/src/cli_proof/harness.rs`, `resolve_binary`):
/// this test binary belongs to `ggen-engine`, which builds no `ggen` binary of
/// its own (`autobins = false`), so `CARGO_BIN_EXE_ggen` is never set by
/// `cargo test -p ggen-engine`. Fall back to walking up from
/// `CARGO_MANIFEST_DIR` to the workspace root (where `Cargo.lock` lives) and
/// probing `target/{debug,release}/ggen`, then finally `PATH`.
fn ggen_bin() -> std::path::PathBuf {
    if let Ok(path) = std::env::var("CARGO_BIN_EXE_ggen") {
        let p = std::path::PathBuf::from(path);
        if p.exists() {
            return p;
        }
    }

    let target_root = std::env::var_os("CARGO_TARGET_DIR")
        .map(std::path::PathBuf::from)
        .or_else(|| {
            let manifest_dir =
                std::env::var_os("CARGO_MANIFEST_DIR").map(std::path::PathBuf::from)?;
            let mut dir: &std::path::Path = manifest_dir.as_path();
            loop {
                if dir.join("Cargo.lock").exists() {
                    return Some(dir.join("target"));
                }
                match dir.parent() {
                    Some(p) => dir = p,
                    None => return None,
                }
            }
        });

    if let Some(target) = target_root {
        for profile in &["debug", "release"] {
            let candidate = target.join(profile).join("ggen");
            if candidate.is_file() {
                return candidate;
            }
            let candidate_exe = target.join(profile).join("ggen.exe");
            if candidate_exe.is_file() {
                return candidate_exe;
            }
        }
    }

    // Last resort: PATH search — version-guarded. A stale ambient ggen (e.g.
    // 26.9.28 in ~/.local/bin) has poisoned harness runs before, producing
    // flakes that looked like code failures. Refuse any PATH candidate whose
    // `--version` does not match this workspace's version.
    if let Some(path_var) = std::env::var_os("PATH") {
        for dir in std::env::split_paths(&path_var) {
            let candidate = dir.join("ggen");
            let candidate_exe = dir.join("ggen.exe");
            for candidate in [candidate, candidate_exe] {
                if candidate.is_file() {
                    let out = std::process::Command::new(&candidate)
                        .arg("--version")
                        .output()
                        .unwrap_or_else(|e| {
                            panic!("failed to execute {} --version: {e}", candidate.display())
                        });
                    let found = String::from_utf8_lossy(&out.stdout)
                        .split_whitespace()
                        .next_back()
                        .unwrap_or_default()
                        .to_string();
                    assert!(
                        found == env!("CARGO_PKG_VERSION"),
                        "stale ambient ggen: harness fell back to PATH and resolved {}, \
                         which reports version {found} but the workspace version is {}. \
                         Remove or reinstall the ambient binary (ambient reinstall is \
                         user-gated).",
                        candidate.display(),
                        env!("CARGO_PKG_VERSION")
                    );
                    return candidate;
                }
            }
        }
    }

    panic!(
        "could not resolve the `ggen` binary: CARGO_BIN_EXE_ggen unset, no \
         target/{{debug,release}}/ggen found by walking up from \
         CARGO_MANIFEST_DIR, and not found on PATH. Run `cargo build -p ggen` first."
    );
}

#[test]
fn sync_run_watch_runs_initial_sync_then_blocks_watching() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());

    let child = Command::new(ggen_bin())
        .args(["sync", "run", "--watch"])
        .current_dir(dir.path())
        .stdout(Stdio::null())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn ggen sync run --watch");

    let captured = watch_for_stderr(child, "watching", Duration::from_secs(10));

    assert!(
        captured.contains("written") || captured.contains("watching"),
        "expected --watch to run an initial sync and start watching, got stderr:\n{captured}"
    );
    let content =
        std::fs::read_to_string(dir.path().join("out/names.txt")).expect("initial sync output");
    assert_eq!(content, "alice\n");
    assert!(
        dir.path().join(".ggen-v2/receipt.json").exists(),
        "initial sync under --watch must still emit a receipt"
    );
}

#[test]
fn sync_run_watch_and_dry_run_combined_writes_nothing() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());

    let child = Command::new(ggen_bin())
        .args(["sync", "run", "--watch", "--dry-run"])
        .current_dir(dir.path())
        .stdout(Stdio::null())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn ggen sync run --watch --dry-run");

    let captured = watch_for_stderr(child, "watching", Duration::from_secs(10));

    assert!(
        captured.contains("written") || captured.contains("watching"),
        "expected --watch --dry-run to still run and report, got stderr:\n{captured}"
    );
    assert!(
        !dir.path().join("out/names.txt").exists(),
        "--watch --dry-run must not write output from its initial sync"
    );
    assert!(
        !dir.path().join(".ggen-v2/receipt.json").exists(),
        "--watch --dry-run must not emit a receipt from its initial sync"
    );
}

#[test]
fn sync_run_watch_missing_manifest_exits_nonzero_without_hanging() {
    // The unwatchable-root error path: with no `ggen.toml` present, the
    // initial sync inside `watch()` fails before the filesystem watcher is
    // ever constructed, so the process must exit non-zero promptly instead
    // of hanging in the watch loop.
    let dir = TempDir::new().expect("tempdir");
    let output = CliHarness::from_path(ggen_bin())
        .args(["sync", "run", "--watch"])
        .current_dir(dir.path())
        .run()
        .expect("run sync run --watch with no ggen.toml");
    let _ = output.assert_failure();
}

#[test]
fn sync_run_unbound_template_variable_exits_nonzero() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());
    // Overwrite the template with an unbound {{ typo }} — sync must refuse
    // via the same FM-TPL-003 lint that `graph validate` runs, not just
    // silently mis-render.
    std::fs::write(
        dir.path().join("templates/one.tmpl"),
        "---\nto: out/names.txt\nforce: true\nsparql:\n  people: SELECT ?name WHERE { ?s <http://example.org/name> ?name } ORDER BY ?name\n---\n{{ typo }}",
    )
    .expect("overwrite template");

    let output = CliHarness::from_path(ggen_bin())
        .args(["graph", "validate"])
        .current_dir(dir.path())
        .run()
        .expect("run graph validate");
    let _ = output.assert_failure().assert_stderr_contains("FM-TPL-003");
}

// ---------------------------------------------------------------------
// graph validate
// ---------------------------------------------------------------------

#[test]
fn graph_validate_valid_project_exits_zero() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());
    let output = CliHarness::from_path(ggen_bin())
        .args(["graph", "validate"])
        .current_dir(dir.path())
        .run()
        .expect("run graph validate");
    let _ = output
        .assert_success()
        .assert_stdout_contains("templates_checked");
}

#[test]
fn graph_validate_missing_manifest_exits_nonzero() {
    let dir = TempDir::new().expect("tempdir");
    let output = CliHarness::from_path(ggen_bin())
        .args(["graph", "validate"])
        .current_dir(dir.path())
        .run()
        .expect("run graph validate");
    let _ = output.assert_failure();
}

#[test]
fn graph_validate_malformed_ontology_exits_nonzero() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());
    std::fs::write(
        dir.path().join("ontology.ttl"),
        "this is not valid turtle {{{",
    )
    .expect("overwrite ontology");
    let output = CliHarness::from_path(ggen_bin())
        .args(["graph", "validate"])
        .current_dir(dir.path())
        .run()
        .expect("run graph validate on bad ttl");
    let _ = output.assert_failure();
}

// ---------------------------------------------------------------------
// receipt verify / history
// ---------------------------------------------------------------------

#[test]
fn receipt_verify_missing_receipt_exits_nonzero() {
    let dir = TempDir::new().expect("tempdir");
    let output = CliHarness::from_path(ggen_bin())
        .args(["receipt", "verify"])
        .current_dir(dir.path())
        .run()
        .expect("run receipt verify with no receipt");
    let _ = output.assert_failure();
}

#[test]
fn receipt_verify_succeeds_after_sync_and_fails_on_tamper() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());

    let _ = CliHarness::from_path(ggen_bin())
        .args(["sync", "run"])
        .current_dir(dir.path())
        .run()
        .expect("sync")
        .assert_success();

    let _ = CliHarness::from_path(ggen_bin())
        .args(["receipt", "verify"])
        .current_dir(dir.path())
        .run()
        .expect("verify")
        .assert_success();

    // Tamper with the payload → verify must exit nonzero (fail closed).
    let receipt_path = dir.path().join(".ggen-v2/receipt.json");
    let tampered = std::fs::read_to_string(&receipt_path)
        .expect("read receipt")
        .replace("\"graph_hash\"", "\"graph_hash_tampered_key_x\"");
    std::fs::write(&receipt_path, tampered).expect("write tampered");
    let _ = CliHarness::from_path(ggen_bin())
        .args(["receipt", "verify"])
        .current_dir(dir.path())
        .run()
        .expect("verify tampered")
        .assert_failure();
}

#[test]
fn receipt_history_missing_log_exits_nonzero() {
    let dir = TempDir::new().expect("tempdir");
    let output = CliHarness::from_path(ggen_bin())
        .args(["receipt", "history"])
        .current_dir(dir.path())
        .run()
        .expect("run receipt history with no log");
    let _ = output.assert_failure();
}

#[test]
fn receipt_history_after_two_syncs_exits_zero() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());

    let _ = CliHarness::from_path(ggen_bin())
        .args(["sync", "run"])
        .current_dir(dir.path())
        .run()
        .expect("first sync")
        .assert_success();
    // Mutate the ontology so the second sync produces a genuinely new
    // record rather than an unchanged no-op.
    std::fs::write(
        dir.path().join("ontology.ttl"),
        "@prefix ex: <http://example.org/> .\nex:alice ex:name \"alice\" .\nex:bob ex:name \"bob\" .\n",
    )
    .expect("mutate ontology");
    let _ = CliHarness::from_path(ggen_bin())
        .args(["sync", "run"])
        .current_dir(dir.path())
        .run()
        .expect("second sync")
        .assert_success();

    let output = CliHarness::from_path(ggen_bin())
        .args(["receipt", "history"])
        .current_dir(dir.path())
        .run()
        .expect("run receipt history");
    let _ = output
        .assert_success()
        .assert_stdout_contains("\"records\": 2");
}

#[test]
fn receipt_history_tampered_middle_record_exits_nonzero() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());

    for name in ["alice", "alice2"] {
        std::fs::write(
            dir.path().join("ontology.ttl"),
            format!("@prefix ex: <http://example.org/> .\nex:alice ex:name \"{name}\" .\n"),
        )
        .expect("mutate ontology");
        let _ = CliHarness::from_path(ggen_bin())
            .args(["sync", "run"])
            .current_dir(dir.path())
            .run()
            .expect("sync")
            .assert_success();
    }

    let log_path = dir.path().join(".ggen-v2/receipt-log.jsonl");
    let lines: Vec<String> = std::fs::read_to_string(&log_path)
        .expect("read log")
        .lines()
        .map(String::from)
        .collect();
    assert_eq!(lines.len(), 2, "expected two receipt-log lines");
    let tampered_first_line = lines[0].replace("\"written\"", "\"tampered\"");
    std::fs::write(&log_path, format!("{tampered_first_line}\n{}\n", lines[1]))
        .expect("write tampered log");

    let output = CliHarness::from_path(ggen_bin())
        .args(["receipt", "history"])
        .current_dir(dir.path())
        .run()
        .expect("run receipt history on tampered log");
    let _ = output.assert_failure();
}

// ---------------------------------------------------------------------
// Global flags
// ---------------------------------------------------------------------

#[test]
fn introspect_emits_json_schema_and_exits_zero() {
    let output = CliHarness::from_path(ggen_bin())
        .args(["--introspect"])
        .run()
        .expect("run --introspect");
    let _ = output.assert_success();
    assert!(
        output.stdout.trim_start().starts_with('['),
        "expected a JSON array of tool definitions, got: {}",
        output.stdout
    );
}

#[test]
fn format_json_flag_produces_parseable_json_on_success() {
    let dir = TempDir::new().expect("tempdir");
    scaffold(dir.path());
    let output = CliHarness::from_path(ggen_bin())
        .args(["--format", "json", "graph", "validate"])
        .current_dir(dir.path())
        .run()
        .expect("run graph validate --format json");
    let _ = output.assert_success();
    serde_json::from_str::<serde_json::Value>(&output.stdout)
        .unwrap_or_else(|e| panic!("stdout not valid JSON ({e}): {}", output.stdout));
}