jlf 0.4.0-dev

CLI for converting JSON logs to human-readable format
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
//! End-to-end tests that drive the built `jlf` binary the way a user would:
//! feed NDJSON on stdin (or via `-i`) and assert on stdout. Output is piped, so
//! `--color=auto` already strips color — no extra flag needed.

use std::io::Write;
use std::process::{Command, Stdio};

const SAMPLE: &str = r#"{"ts":"10:00:01","level":"info","msg":"login","user":"alice","latency_ms":42,"token":"abc123"}
{"ts":"10:00:02","level":"error","msg":"db timeout","user":"bob","latency_ms":510,"token":"xyz"}
{"ts":"10:00:03","level":"warn","msg":"retry","user":"alice","latency_ms":88}
{"ts":"10:00:04","level":"info","msg":"login","user":"carol","latency_ms":33,"token":"q9"}
{"ts":"10:00:05","level":"error","msg":"db timeout","user":"alice","latency_ms":620}
"#;

/// Run `jlf <args>` with `stdin` piped in; return (stdout, exit_code).
fn run(args: &[&str], stdin: &str) -> (String, i32) {
    let mut child = Command::new(env!("CARGO_BIN_EXE_jlf"))
        .args(args)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .spawn()
        .expect("spawn jlf");
    child
        .stdin
        .take()
        .unwrap()
        .write_all(stdin.as_bytes())
        .unwrap();
    let out = child.wait_with_output().unwrap();
    (
        String::from_utf8(out.stdout).unwrap(),
        out.status.code().unwrap_or(-1),
    )
}

fn stdout(args: &[&str]) -> String {
    run(args, SAMPLE).0
}

#[test]
fn template_projects_fields() {
    assert_eq!(
        stdout(&["${ts} ${level} ${user}"]),
        "10:00:01 info alice\n\
         10:00:02 error bob\n\
         10:00:03 warn alice\n\
         10:00:04 info carol\n\
         10:00:05 error alice\n"
    );
}

#[test]
fn fields_flag_is_a_template_shortcut() {
    assert_eq!(stdout(&["-f", "ts,level,user"]), stdout(&["${ts} ${level} ${user}"]));
}

#[test]
fn filter_keeps_matching_records() {
    assert_eq!(
        stdout(&["level=error", "${ts} ${user}"]),
        "10:00:02 bob\n10:00:05 alice\n"
    );
}

#[test]
fn numeric_filter() {
    assert_eq!(
        stdout(&["latency_ms>100", "${user} ${latency_ms}"]),
        "bob 510\nalice 620\n"
    );
}

#[test]
fn or_within_field_and_across_filters() {
    // (error OR warn) AND user=alice
    assert_eq!(stdout(&["level=error,warn", "user=alice", "${ts}"]), "10:00:03\n10:00:05\n");
}

#[test]
fn take_counts_emitted_records_after_filtering() {
    // Two errors exist; --take 1 must stop after the first emitted (filtered) one.
    assert_eq!(stdout(&["level=error", "--take", "1", "${ts}"]), "10:00:02\n");
}

#[test]
fn count_total() {
    assert_eq!(stdout(&["count"]), "5\n");
}

#[test]
fn count_by_field_has_header_and_total() {
    let out = stdout(&["count", "level"]);
    assert!(out.starts_with("     count  value\n"), "got:\n{out}");
    assert!(out.contains("         2  info\n"));
    assert!(out.contains("         2  error\n"));
    assert!(out.contains("         1  warn\n"));
    assert!(out.trim_end().ends_with("         5  total"));
}

#[test]
fn uniq_distinct_count() {
    assert_eq!(stdout(&["uniq", "user"]), "3 distinct (of 5 values)\n");
}

#[test]
fn stats_percentiles() {
    let out = stdout(&["stats", "latency_ms"]);
    assert!(out.contains("count 5\n"), "got:\n{out}");
    assert!(out.contains("min   33.00\n"));
    assert!(out.contains("max   620.00\n"));
    assert!(out.contains("p50   88.00\n"));
}

#[test]
fn top_with_share_and_footer() {
    let out = stdout(&["top", "user"]);
    assert!(out.starts_with("     count   share  value\n"), "got:\n{out}");
    assert!(out.contains("         3   60.0%  alice\n"));
    assert!(out.trim_end().ends_with("top 3 of 3 distinct (5 values)"));
}

#[test]
fn csv_export_with_header() {
    assert_eq!(
        stdout(&["@csv", "ts,level,user"]),
        "ts,level,user\n\
         10:00:01,info,alice\n\
         10:00:02,error,bob\n\
         10:00:03,warn,alice\n\
         10:00:04,info,carol\n\
         10:00:05,error,alice\n"
    );
}

#[test]
fn md_export_with_separator_row() {
    let out = stdout(&["@md", "level,user"]);
    assert!(out.starts_with("| level | user |\n| --- | --- |\n"), "got:\n{out}");
    assert!(out.contains("| info | alice |\n"));
}

#[test]
fn summary_export_to_csv_has_header() {
    let out = stdout(&["count", "level", "@csv"]);
    assert!(out.starts_with("count,value\n"), "got:\n{out}");
}

#[test]
fn redact_masks_named_field() {
    let out = stdout(&["-c", "-r", "token"]);
    assert!(out.contains(r#""token":"***""#), "got:\n{out}");
    assert!(!out.contains("abc123"), "secret leaked:\n{out}");
}

#[test]
fn strict_exits_nonzero_on_invalid_json() {
    let (_out, code) = run(&["-s"], "not json at all\n");
    assert_eq!(code, 1);
}

#[test]
fn non_strict_passes_through_invalid_lines() {
    let (out, code) = run(&[], "not json at all\n");
    assert_eq!(code, 0);
    assert_eq!(out, "not json at all\n");
}

#[test]
fn input_flag_reads_a_file_without_stdin() {
    let mut path = std::env::temp_dir();
    path.push(format!("jlf_test_{}.ndjson", std::process::id()));
    std::fs::write(&path, SAMPLE).unwrap();
    let p = path.to_str().unwrap();

    let out = Command::new(env!("CARGO_BIN_EXE_jlf"))
        .args(["-i", p, "count"])
        .stdin(Stdio::null())
        .output()
        .unwrap();
    std::fs::remove_file(&path).ok();
    assert_eq!(String::from_utf8(out.stdout).unwrap(), "5\n");
}

/// Records used by the conditional tests: `body` is an empty string and
/// `data.count` is 0 — both present but falsey.
const COND: &str = "{\"message\":\"hi\",\"body\":\"\",\"data\":{\"count\":0}}\n";

#[test]
fn if_or_list_is_true_when_any_field_is_truthy() {
    // `body` (empty) and `data.count` (0) are present-but-falsey and must not
    // short-circuit the OR before reaching the truthy `message`.
    let (out, _) = run(&["$if(body|data.count|message => yes)$else(no)"], COND);
    assert_eq!(out, "yes\n");
}

#[test]
fn if_single_falsey_field_is_false() {
    assert_eq!(run(&["$if(body => yes)$else(no)"], COND).0, "no\n");
    assert_eq!(run(&["$if(data.count => yes)$else(no)"], COND).0, "no\n");
}

#[test]
fn has_is_true_for_present_but_falsey_field() {
    // `$has` checks existence, so an empty string still counts.
    assert_eq!(run(&["$has(body => has)$else(missing)"], COND).0, "has\n");
}

#[test]
fn has_falls_through_to_nested_has() {
    // `$has(msg …)` is false (absent); the else nests another `$has` on message.
    let out = run(&["$has(msg => m)$else($has(message => message)$else(none))"], COND).0;
    assert_eq!(out, "message\n");
}

/// `${?field}` collapses one adjacent space when the field is empty/absent, but
/// leaves plain `${field}` and intentional spacing untouched.
mod optional_field {
    use super::run;

    fn render(template: &str, json: &str) -> String {
        run(&[template], &format!("{json}\n")).0
    }

    #[test]
    fn collapses_one_space_in_the_middle_when_absent() {
        assert_eq!(render("${a} ${?b} ${c}", r#"{"a":"A","c":"C"}"#), "A C\n");
    }

    #[test]
    fn keeps_spacing_when_present() {
        assert_eq!(render("${a} ${?b} ${c}", r#"{"a":"A","b":"B","c":"C"}"#), "A B C\n");
    }

    #[test]
    fn trims_trailing_space_at_line_end() {
        assert_eq!(render("${a} ${?b}", r#"{"a":"A"}"#), "A\n");
    }

    #[test]
    fn trims_leading_space_at_line_start() {
        assert_eq!(render("${?a} ${b}", r#"{"b":"B"}"#), "B\n");
    }

    #[test]
    fn collapses_consecutive_absent_optionals() {
        assert_eq!(render("${a} ${?b} ${?c} ${d}", r#"{"a":"A","d":"D"}"#), "A D\n");
    }

    #[test]
    fn empty_string_value_also_collapses() {
        assert_eq!(render("${a} ${?b} ${c}", r#"{"a":"A","b":"","c":"C"}"#), "A C\n");
    }

    #[test]
    fn plain_field_does_not_collapse() {
        // a missing plain ${b} leaves the two surrounding spaces intact
        assert_eq!(render("${a} ${b} ${c}", r#"{"a":"A","c":"C"}"#), "A  C\n");
    }

    #[test]
    fn intentional_indentation_is_preserved() {
        assert_eq!(render("  ${?label}: ${v}", r#"{"label":"L","v":"V"}"#), "  L: V\n");
    }

    #[test]
    fn fallbacks_and_styles_work_on_optionals() {
        assert_eq!(render("${a} ${?x.y|z} ${c}", r#"{"a":"A","z":"Z","c":"C"}"#), "A Z C\n");
        assert_eq!(render("${a} ${?x.y|z} ${c}", r#"{"a":"A","c":"C"}"#), "A C\n");
    }
}

/// `[format.*]` custom output formats and the `--format` flag.
mod custom_format {
    use std::io::Write;
    use std::process::{Command, Stdio};

    fn run_in(dir: &std::path::Path, args: &[&str], stdin: &str) -> String {
        let mut child = Command::new(env!("CARGO_BIN_EXE_jlf"))
            .args(args)
            .current_dir(dir)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .spawn()
            .unwrap();
        child.stdin.take().unwrap().write_all(stdin.as_bytes()).unwrap();
        String::from_utf8(child.wait_with_output().unwrap().stdout).unwrap()
    }

    #[test]
    fn custom_html_format_escapes_values() {
        let dir = std::env::temp_dir().join(format!("jlf_fmt_{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        // `.git` marker so the dir is treated as the workspace root
        std::fs::create_dir_all(dir.join(".git")).unwrap();
        std::fs::write(
            dir.join("jlf.toml"),
            r#"
[recipe.report]
escape = "html"
out = "<table>\n$rows( <tr><td>${level}</td><td>${msg}</td></tr> )*</table>\n"
"#,
        )
        .unwrap();

        let out = run_in(
            &dir,
            &["--format", "report"],
            "{\"level\":\"info\",\"msg\":\"a <b> & c\"}\n",
        );
        std::fs::remove_dir_all(&dir).ok();

        assert!(out.starts_with("<table>\n"), "got:\n{out}");
        assert!(out.contains("<tr><td>info</td><td>a &lt;b&gt; &amp; c</td></tr>\n"), "got:\n{out}");
        assert!(out.trim_end().ends_with("</table>"), "got:\n{out}");
    }

    #[test]
    fn format_csv_is_a_builtin_shorthand() {
        let dir = std::env::temp_dir().join(format!("jlf_fmt_csv_{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let out = run_in(
            &dir,
            &["--format", "csv", "level,msg"],
            "{\"level\":\"info\",\"msg\":\"hi\"}\n",
        );
        std::fs::remove_dir_all(&dir).ok();
        assert_eq!(out, "level,msg\ninfo,hi\n");
    }
}

/// `[preset.*]` saved bundles invoked via `@name` / `-p`.
mod presets {
    use std::io::Write;
    use std::process::{Command, Stdio};

    const CONFIG: &str = r#"
[preset.errors]
where = "level=error,fatal"
template = "${level} ${msg}"

[preset.lat]
where = "latency_ms>=100"
stats = "latency_ms"

[preset.acts]
top = "action"
n = 2
"#;

    const LOGS: &str = concat!(
        "{\"level\":\"info\",\"msg\":\"a\",\"latency_ms\":42,\"action\":\"x\"}\n",
        "{\"level\":\"error\",\"msg\":\"b\",\"latency_ms\":510,\"action\":\"y\"}\n",
        "{\"level\":\"warn\",\"msg\":\"c\",\"latency_ms\":88,\"action\":\"x\"}\n",
        "{\"level\":\"fatal\",\"msg\":\"d\",\"latency_ms\":900,\"action\":\"x\"}\n",
    );

    fn run_preset(args: &[&str]) -> String {
        let dir = std::env::temp_dir().join(format!(
            "jlf_preset_{}_{}",
            std::process::id(),
            args.join("_").replace(['@', '=', '/', ' '], "-")
        ));
        std::fs::create_dir_all(dir.join(".git")).unwrap();
        std::fs::write(dir.join("jlf.toml"), CONFIG).unwrap();
        let mut child = Command::new(env!("CARGO_BIN_EXE_jlf"))
            .args(args)
            .current_dir(&dir)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .spawn()
            .unwrap();
        child.stdin.take().unwrap().write_all(LOGS.as_bytes()).unwrap();
        let out = String::from_utf8(child.wait_with_output().unwrap().stdout).unwrap();
        std::fs::remove_dir_all(&dir).ok();
        out
    }

    #[test]
    fn view_preset_applies_filter_and_template() {
        assert_eq!(run_preset(&["@errors"]), "error b\nfatal d\n");
    }

    #[test]
    fn explicit_filter_on_new_field_is_added() {
        // adds action=x on top of the preset's level filter
        assert_eq!(run_preset(&["@errors", "action=x"]), "fatal d\n");
    }

    #[test]
    fn explicit_filter_overrides_same_field() {
        // level=warn replaces the preset's level=error,fatal
        assert_eq!(run_preset(&["@errors", "level=warn"]), "warn c\n");
    }

    #[test]
    fn explicit_template_overrides_preset() {
        assert_eq!(run_preset(&["@errors", "M:${msg}"]), "M:b\nM:d\n");
    }

    #[test]
    fn summary_preset_runs_stats() {
        // only latency_ms >= 100 (510, 900)
        let out = run_preset(&["-p", "lat"]);
        assert!(out.contains("count 2"), "got:\n{out}");
        assert!(out.contains("max   900.00"), "got:\n{out}");
    }

    #[test]
    fn top_preset_respects_n() {
        let out = run_preset(&["@acts"]);
        assert!(out.starts_with("     count   share  value\n"), "got:\n{out}");
        assert!(out.contains("  x\n"));
        assert!(out.trim_end().ends_with("top 2 of 2 distinct (4 values)"), "got:\n{out}");
    }

    #[test]
    fn unknown_preset_errors() {
        // no stdout on error; just ensure it doesn't render records
        assert_eq!(run_preset(&["@missing"]), "");
    }
}

/// Phase 1 of the recipes redesign: `${@name}` includes variables/recipes, and
/// filters accept `a|b|c` fallback fields.
mod recipes_phase1 {
    use super::run;

    #[test]
    fn at_sign_variable_include_renders() {
        let json = "{\"timestamp\":\"T\",\"level\":\"INFO\",\"message\":\"hi\"}\n";
        assert_eq!(
            run(&["-v", "output=${@message}", "${@output}"], json).0,
            "hi\n"
        );
    }

    #[test]
    fn filter_fallback_fields() {
        let logs = concat!(
            "{\"lvl\":\"error\",\"msg\":\"a\"}\n",
            "{\"level\":\"info\",\"msg\":\"b\"}\n",
            "{\"severity\":\"error\",\"msg\":\"c\"}\n",
        );
        assert_eq!(run(&["lvl|level|severity=error", "${msg}"], logs).0, "a\nc\n");
    }
}

/// Phase 5 render rules: optional rest is empty when nothing's left over, and
/// the `?`-collapse absorbs an adjacent newline.
mod recipes_phase5 {
    use super::run;

    fn render(template: &str, json: &str) -> String {
        run(&[template], &format!("{json}\n")).0
    }

    #[test]
    fn optional_rest_is_empty_when_fully_consumed() {
        // was "X {}" before; the ${?..} now collapses to nothing
        assert_eq!(render("${a} ${?..:json}", r#"{"a":"X"}"#), "X\n");
    }

    #[test]
    fn optional_rest_renders_when_leftover_exists() {
        let out = render("${a} ${?..:json}", r#"{"a":"X","b":"Y"}"#);
        assert!(out.starts_with("X {"), "got: {out:?}");
        assert!(out.contains("\"b\": \"Y\""), "got: {out:?}");
    }

    #[test]
    fn collapse_absorbs_a_newline_separator() {
        // newline before an empty optional is dropped
        assert_eq!(render("${a}\n${?..:json}", r#"{"a":"X"}"#), "X\n");
        // ...but stays when the optional renders
        let out = render("${a}\n${?..:json}", r#"{"a":"X","b":"Y"}"#);
        assert!(out.starts_with("X\n{"), "got: {out:?}");
    }

    #[test]
    fn plain_rest_still_prints_empty_object() {
        // only the `?` form collapses; plain ${..} is unchanged
        assert_eq!(render("${a} ${..:json}", r#"{"a":"X"}"#), "X {}\n");
    }
}

/// Phases 2-4: unified `[recipe.*]` config (variable + preset + format + named
/// field) resolved through `@name`.
mod recipes_config {
    use std::io::Write;
    use std::process::{Command, Stdio};

    fn run_in_cfg(config: &str, args: &[&str], stdin: &str) -> String {
        let dir = std::env::temp_dir().join(format!(
            "jlf_recipe_{}_{}",
            std::process::id(),
            args.join("_").replace(['@', '=', '/', ' ', '|'], "-")
        ));
        std::fs::create_dir_all(dir.join(".git")).unwrap();
        std::fs::write(dir.join("jlf.toml"), config).unwrap();
        let mut child = Command::new(env!("CARGO_BIN_EXE_jlf"))
            .args(args)
            .current_dir(&dir)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .spawn()
            .unwrap();
        child.stdin.take().unwrap().write_all(stdin.as_bytes()).unwrap();
        let out = String::from_utf8(child.wait_with_output().unwrap().stdout).unwrap();
        std::fs::remove_dir_all(&dir).ok();
        out
    }

    const LOGS: &str = concat!(
        "{\"ts\":\"t1\",\"level\":\"info\",\"message\":\"a\",\"x\":1}\n",
        "{\"ts\":\"t2\",\"level\":\"error\",\"message\":\"b <c>\"}\n",
    );

    #[test]
    fn named_field_recipe_inlines_and_filters() {
        let cfg = "[recipe.host]\nout = \"host|hostname:dimmed\"\n";
        // ${@host} is optional-by-default, so an absent host collapses its space
        assert_eq!(run_in_cfg(cfg, &["${@host} ${message}"], LOGS), "a\nb <c>\n");
    }

    /// A `field` recipe (`@name`) resolves in template, filter, and summary
    /// positions — including its `|` fallback chain.
    #[test]
    fn named_field_recipe_works_in_all_positions() {
        let cfg = "[recipe.lat]\nout = \"latency_ms|duration\"\n";
        let logs = concat!(
            "{\"latency_ms\":42,\"message\":\"fast\"}\n",
            "{\"duration\":800,\"message\":\"slow\"}\n",
        );
        // template
        assert_eq!(run_in_cfg(cfg, &["${@lat}ms ${message}"], logs), "42ms fast\n800ms slow\n");
        // filter: @lat>500 keeps the slow one (via the duration fallback)
        assert_eq!(run_in_cfg(cfg, &["@lat>500", "-c"], logs), "slow {\"duration\":800}\n");
        // summary: count breaks down by the resolved value
        let out = run_in_cfg(cfg, &["count", "@lat"], logs);
        assert!(out.contains("42"), "got:\n{out}");
        assert!(out.contains("800"), "got:\n{out}");
    }

    #[test]
    fn preset_recipe_runs_with_filter_and_body() {
        let cfg = "[recipe.errors]\nfilter = \"level=error\"\nout = \"${ts} ${message}\"\n";
        assert_eq!(run_in_cfg(cfg, &["@errors"], LOGS), "t2 b <c>\n");
    }

    #[test]
    fn format_recipe_frames_and_escapes() {
        let cfg = "[recipe.report]\nescape = \"html\"\nout = \"<table>\\n$rows( <tr><td>${message}</td></tr> )*</table>\\n\"\n";
        let out = run_in_cfg(cfg, &["@report"], LOGS);
        assert!(out.starts_with("<table>\n"), "got:\n{out}");
        assert!(out.contains("<tr><td>b &lt;c&gt;</td></tr>\n"), "got:\n{out}");
        assert!(out.trim_end().ends_with("</table>"), "got:\n{out}");
    }

    #[test]
    fn shorthand_recipes_table() {
        let cfg = "[recipes]\nline = \"${level}: ${message}\"\n";
        assert_eq!(run_in_cfg(cfg, &["@line"], LOGS), "info: a\nerror: b <c>\n");
    }

    #[test]
    fn conditional_override_on_compact() {
        let cfg = "[recipe.g]\nout = \"BIG ${message}\"\n[recipe.g.compact]\nout = \"sm ${message}\"\n";
        assert_eq!(run_in_cfg(cfg, &["@g"], LOGS), "BIG a\nBIG b <c>\n");
        assert_eq!(run_in_cfg(cfg, &["@g", "-c"], LOGS), "sm a\nsm b <c>\n");
    }
}

/// Phase 6: optional variable include `${?@name}` collapses when its field is
/// absent (so the recipe-style default works).
mod recipes_optional_include {
    use super::run;

    #[test]
    fn optional_include_renders_when_present() {
        let out = run(
            &["-v", "lvl=${lvl|level:dimmed}", "-v", "o=${?@lvl}${msg}", "${@o}"],
            "{\"level\":\"INFO\",\"msg\":\"hi\"}\n",
        )
        .0;
        assert_eq!(out, "INFOhi\n");
    }

    #[test]
    fn optional_include_collapses_when_absent() {
        let out = run(
            &["-v", "lvl=${lvl|level:dimmed}", "-v", "o=${?@lvl} ${msg}", "${@o}"],
            "{\"msg\":\"hi\"}\n",
        )
        .0;
        assert_eq!(out, "hi\n");
    }
}

/// A positional template that carries `$`-interpolation but no `{` — `$( … )`,
/// `$field`, `$match( … )` — is recognized as the template (not a filter/column).
mod positional_dollar_template {
    use super::run;

    #[test]
    fn dollar_match_arg_is_template() {
        let (out, code) = run(
            &["$match(status $when(>=500 => 5xx) $else(ok))"],
            "{\"status\":200}\n{\"status\":503}\n",
        );
        assert_eq!(code, 0);
        assert_eq!(out, "ok\n5xx\n");
    }

    #[test]
    fn dollar_rep_arg_is_template() {
        let (out, _) = run(&["$( $key=$value )\" \"*"], "{\"a\":\"1\",\"b\":\"2\"}\n");
        assert_eq!(out, "a=1 b=2\n");
    }

    #[test]
    fn table_format_without_columns_errors() {
        // `@csv` with no column list used to print blank rows; now it errors.
        let (out, code) = run(&["@csv"], "{\"a\":1,\"b\":2}\n");
        assert_eq!(code, 2);
        assert_eq!(out, ""); // message goes to stderr (suppressed by the harness)
        // with columns it works
        assert_eq!(run(&["@csv", "a,b"], "{\"a\":1,\"b\":2}\n").0, "a,b\n1,2\n");
    }
}

/// An undefined variable/recipe reference renders empty instead of panicking.
mod undefined_variable {
    use super::run;

    #[test]
    fn undefined_include_does_not_crash() {
        let (out, code) = run(&["-v", "o=${@nope}${a}", "${@o}"], "{\"a\":\"x\"}\n");
        assert_eq!(code, 0);
        assert_eq!(out, "x\n");
    }

    #[test]
    fn undefined_optional_include_does_not_crash() {
        let (_out, code) = run(&["-v", "o=${?@nope} ${a}", "${@o}"], "{\"a\":\"x\"}\n");
        assert_eq!(code, 0);
    }
}
/// Recipe `[recipe.X.compact]` overrides trigger from any source of `compact` —
/// the CLI flag, config, or a preset that sets it (review fix).
mod recipe_override_sources {
    use std::io::Write;
    use std::process::{Command, Stdio};

    const CFG: &str = concat!(
        "[recipe.sep]\nout = \"[normal]\"\n",
        "[recipe.sep.compact]\nout = \"[compact]\"\n",
        "[preset.cmp]\ncompact = true\ntemplate = \"${@sep}\"\n",
    );

    fn run_in(args: &[&str]) -> String {
        let dir = std::env::temp_dir().join(format!(
            "jlf_ovr_{}_{}",
            std::process::id(),
            args.join("_").replace(['@', ' ', '-'], "_")
        ));
        std::fs::create_dir_all(dir.join(".git")).unwrap();
        std::fs::write(dir.join("jlf.toml"), CFG).unwrap();
        let mut child = Command::new(env!("CARGO_BIN_EXE_jlf"))
            .args(args)
            .current_dir(&dir)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .spawn()
            .unwrap();
        child.stdin.take().unwrap().write_all(b"{\"a\":1}\n").unwrap();
        let out = String::from_utf8(child.wait_with_output().unwrap().stdout).unwrap();
        std::fs::remove_dir_all(&dir).ok();
        out
    }

    #[test]
    fn compact_flag_triggers_override() {
        assert_eq!(run_in(&["-c", "${@sep}"]), "[compact]\n");
    }

    #[test]
    fn normal_uses_base() {
        assert_eq!(run_in(&["${@sep}"]), "[normal]\n");
    }

    #[test]
    fn preset_set_compact_triggers_override() {
        // @cmp sets compact=true; the sep recipe's compact override must apply
        assert_eq!(run_in(&["@cmp"]), "[compact]\n");
    }
}

/// Output-format unification: `csv/tsv/md` are predefined table recipes run via
/// `@csv`/`@tsv`/`@md`, and users can define new table formats with repetition
/// templates.
mod table_recipes {
    use std::io::Write;
    use std::process::{Command, Stdio};

    fn run_in(config: &str, args: &[&str], stdin: &str) -> String {
        use std::sync::atomic::{AtomicU32, Ordering};
        static N: AtomicU32 = AtomicU32::new(0);
        let dir = std::env::temp_dir().join(format!(
            "jlf_table_{}_{}",
            std::process::id(),
            N.fetch_add(1, Ordering::Relaxed)
        ));
        std::fs::create_dir_all(dir.join(".git")).unwrap();
        std::fs::write(dir.join("jlf.toml"), config).unwrap();
        let mut child = Command::new(env!("CARGO_BIN_EXE_jlf"))
            .args(args)
            .current_dir(&dir)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .spawn()
            .unwrap();
        child.stdin.take().unwrap().write_all(stdin.as_bytes()).unwrap();
        let out = String::from_utf8(child.wait_with_output().unwrap().stdout).unwrap();
        std::fs::remove_dir_all(&dir).ok();
        out
    }

    #[test]
    fn builtin_csv_unchanged() {
        let out = run_in("", &["@csv", "a,b"], "{\"a\":\"x\",\"b\":\"y\"}\n");
        assert_eq!(out, "a,b\nx,y\n");
    }

    #[test]
    fn builtin_md_unchanged() {
        let out = run_in("", &["@md", "a"], "{\"a\":\"x\"}\n");
        assert_eq!(out, "| a |\n| --- |\n| x |\n");
    }

    #[test]
    fn custom_separator_table_with_csv_quoting() {
        let cfg = concat!(
            "[recipe.psv]\n",
            "out = \"$cols( ${key} )|*\\n$rows( $cols( ${value:csv} )|* )*\"\n",
        );
        let out = run_in(cfg, &["--format", "psv", "-f", "a,b"], "{\"a\":\"x\",\"b\":\"y,z\"}\n");
        assert_eq!(out, "a|b\nx|\"y,z\"\n");
    }

    #[test]
    fn custom_table_run_by_name_with_saved_fields_and_filter() {
        // A custom table frame is one recipe (its `out` is the $cols/$rows
        // template); a report that uses it with saved columns + filter is
        // another that references the format and puts the columns in its `out`.
        let cfg = concat!(
            "[recipe.gridfmt]\n",
            "out = \"| $cols( ${key} )\\\" | \\\"* |\\n| $cols( === )\\\" | \\\"* |\\n$rows( | $cols( ${value:md} )\\\" | \\\"* | )*\"\n",
            "[recipe.grid]\n",
            "format = \"gridfmt\"\nout = \"a,b\"\nfilter = \"keep=1\"\n",
        );
        let logs = concat!(
            "{\"a\":\"1\",\"b\":\"x\",\"keep\":1}\n",
            "{\"a\":\"2\",\"b\":\"y\",\"keep\":0}\n",
        );
        let out = run_in(cfg, &["@grid"], logs);
        assert_eq!(out, "| a | b |\n| === | === |\n| 1 | x |\n");
    }

    #[test]
    fn user_can_override_builtin_csv() {
        // redefine csv to use semicolons
        let cfg = "[recipe.csv]\nout = \"$cols( ${key} );*\\n$rows( $cols( ${value:csv} );* )*\"\n";
        let out = run_in(cfg, &["@csv", "a,b"], "{\"a\":\"x\",\"b\":\"y\"}\n");
        assert_eq!(out, "a;b\nx;y\n");
    }
}

#[test]
fn dim_unmatched_surfaces_matches_first() {
    // The hidden preview flag `--dim-unmatched` keeps non-matching records but
    // emits them (faint) only after every match, so a filter's hits lead.
    let out = run(&["--color=always", "--dim-unmatched", "level=error", "${ts}"], SAMPLE).0;
    let at = |s: &str| out.find(s).unwrap_or_else(|| panic!("missing {s} in:\n{out}"));
    // Both matches (the two errors) precede every non-match.
    for miss in ["10:00:01", "10:00:03", "10:00:04"] {
        assert!(at("10:00:02") < at(miss), "match 02 should precede {miss}");
        assert!(at("10:00:05") < at(miss), "match 05 should precede {miss}");
    }
    // Non-matches are dimmed (faint escape present).
    assert!(out.contains("\u{1b}[2m"), "expected a dim escape for non-matches");
}