kelora 1.5.0

A command-line log analysis tool with embedded Rhai scripting
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
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
mod common;
use common::*;

#[test]
fn test_brief_output_mode() {
    let input = r#"{"level": "INFO", "message": "test message", "user": "alice"}
{"level": "ERROR", "message": "error occurred", "user": "bob"}"#;

    let (stdout, _stderr, exit_code) = run_kelora_with_input(&["-f", "json", "--brief"], input);
    assert_eq!(
        exit_code, 0,
        "kelora should exit successfully with brief mode"
    );

    let lines: Vec<&str> = stdout.trim().split('\n').collect();
    assert_eq!(lines.len(), 2, "Should output 2 lines");

    // Brief mode should output only values, space-separated, no keys
    assert_eq!(lines[0], "INFO test message alice");
    assert_eq!(lines[1], "ERROR error occurred bob");

    // Verify no key=value format is used
    assert!(
        !stdout.contains("level="),
        "Brief mode should not contain keys"
    );
    assert!(
        !stdout.contains("message="),
        "Brief mode should not contain keys"
    );
    assert!(
        !stdout.contains("user="),
        "Brief mode should not contain keys"
    );
}

#[test]
fn test_brief_output_mode_short_form() {
    let input = r#"{"level": "INFO", "message": "hello world"}"#;

    let (stdout, _stderr, exit_code) = run_kelora_with_input(&["-f", "json", "-b"], input);
    assert_eq!(
        exit_code, 0,
        "kelora should exit successfully with -b short form"
    );

    // Brief mode should output only values, space-separated
    assert_eq!(stdout.trim(), "INFO hello world");
    assert!(
        !stdout.contains("level="),
        "Brief mode should not contain keys"
    );
}

#[test]
fn test_core_field_filtering() {
    let input = r#"{"ts": "2024-01-01T12:00:00Z", "level": "ERROR", "message": "Test message", "user": "alice", "status": 500}"#;

    let (stdout, _, exit_code) = run_kelora_with_input(&["-f", "json", "--core"], input);
    assert_eq!(exit_code, 0, "kelora should exit successfully with --core");

    // Should only contain core fields
    assert!(stdout.contains("ts="), "Should contain ts field");
    assert!(stdout.contains("level="), "Should contain level field");
    assert!(stdout.contains("message="), "Should contain message field");
    assert!(
        !stdout.contains("user="),
        "Should not contain non-core user field"
    );
    assert!(
        !stdout.contains("status="),
        "Should not contain non-core status field"
    );
}

#[test]
fn test_core_field_filtering_short_flag() {
    let input = r#"{"ts": "2024-01-01T12:00:00Z", "level": "ERROR", "message": "Test message", "user": "alice"}"#;

    let (stdout, _stderr, exit_code) = run_kelora_with_input(&["-f", "json", "-c"], input);
    assert_eq!(
        exit_code, 0,
        "kelora should exit successfully with -c short flag"
    );

    // Should only contain core fields
    assert!(stdout.contains("ts="), "Should contain ts field");
    assert!(stdout.contains("level="), "Should contain level field");
    assert!(stdout.contains("message="), "Should contain message field");
    assert!(
        !stdout.contains("user="),
        "Should not contain non-core user field"
    );
}

#[test]
fn test_core_field_with_alternative_names() {
    let input = r#"{"ts": "2024-01-01T12:00:00Z", "lvl": "WARN", "msg": "Alternative names", "extra_data": "ignored"}"#;

    let (stdout, _stderr, exit_code) = run_kelora_with_input(&["-f", "json", "--core"], input);
    assert_eq!(
        exit_code, 0,
        "kelora should exit successfully with alternative core field names"
    );

    // Should include alternative core field names
    assert!(stdout.contains("ts="), "Should contain ts field");
    assert!(stdout.contains("lvl="), "Should contain lvl field");
    assert!(stdout.contains("msg="), "Should contain msg field");
    assert!(
        !stdout.contains("extra_data="),
        "Should not contain non-core field"
    );
}

#[test]
fn test_core_field_plus_additional_keys() {
    let input = r#"{"ts": "2024-01-01T12:00:00Z", "level": "ERROR", "message": "Test message", "user": "alice", "status": 500}"#;

    let (stdout, _stderr, exit_code) =
        run_kelora_with_input(&["-f", "json", "--core", "--keys", "user,status"], input);
    assert_eq!(
        exit_code, 0,
        "kelora should exit successfully with --core and --keys"
    );

    // Should contain both core fields and specified keys
    assert!(stdout.contains("ts="), "Should contain ts field");
    assert!(stdout.contains("level="), "Should contain level field");
    assert!(stdout.contains("message="), "Should contain message field");
    assert!(
        stdout.contains("user="),
        "Should contain user field from --keys"
    );
    assert!(
        stdout.contains("status="),
        "Should contain status field from --keys"
    );
}

#[test]
fn test_core_field_with_syslog() {
    let input = r#"<34>Jan 1 12:00:00 myhost myapp[1234]: Test syslog message"#;

    let (stdout, _stderr, exit_code) = run_kelora_with_input(&["-f", "syslog", "--core"], input);
    assert_eq!(
        exit_code, 0,
        "kelora should exit successfully with syslog and --core"
    );

    // Should contain syslog core fields
    assert!(
        stdout.contains("severity="),
        "Should contain severity field"
    );
    assert!(stdout.contains("ts="), "Should contain ts field");
    assert!(stdout.contains("msg="), "Should contain msg field");
    // Should not contain non-core syslog fields
    assert!(
        !stdout.contains("facility="),
        "Should not contain facility field"
    );
    assert!(!stdout.contains("host="), "Should not contain host field");
    assert!(!stdout.contains("prog="), "Should not contain prog field");
}

#[test]
fn test_core_field_with_exec_created_fields() {
    let input = r#"{"original_time": "2024-01-01T12:00:00Z", "orig_level": "ERROR", "orig_msg": "Test message", "user": "alice"}"#;

    let (stdout, _stderr, exit_code) = run_kelora_with_input(
        &[
            "-f",
            "json",
            "--exec",
            "e.timestamp = e.original_time; e.level = e.orig_level; e.message = e.orig_msg",
            "--core",
            "--keys",
            "user",
        ],
        input,
    );
    assert_eq!(
        exit_code, 0,
        "kelora should exit successfully with exec-created core fields"
    );

    // Should contain exec-created core fields and specified keys
    assert!(
        stdout.contains("user="),
        "Should contain user field from --keys"
    );
    assert!(
        stdout.contains("timestamp="),
        "Should contain exec-created timestamp field"
    );
    assert!(
        stdout.contains("level="),
        "Should contain exec-created level field"
    );
    assert!(
        stdout.contains("message="),
        "Should contain exec-created message field"
    );
    // Should not contain original fields
    assert!(
        !stdout.contains("original_time="),
        "Should not contain original_time field"
    );
    assert!(
        !stdout.contains("orig_level="),
        "Should not contain orig_level field"
    );
    assert!(
        !stdout.contains("orig_msg="),
        "Should not contain orig_msg field"
    );
}

#[test]
fn test_core_field_with_logfmt() {
    let input =
        r#"time=2024-01-01T12:00:00Z lvl=error msg="Test logfmt message" user=bob status=404"#;

    let (stdout, _stderr, exit_code) = run_kelora_with_input(&["-f", "logfmt", "--core"], input);
    assert_eq!(
        exit_code, 0,
        "kelora should exit successfully with logfmt and --core"
    );

    // Should contain logfmt core fields
    assert!(stdout.contains("time="), "Should contain time field");
    assert!(stdout.contains("lvl="), "Should contain lvl field");
    assert!(stdout.contains("msg="), "Should contain msg field");
    // Should not contain non-core fields
    assert!(!stdout.contains("user="), "Should not contain user field");
    assert!(
        !stdout.contains("status="),
        "Should not contain status field"
    );
}

#[test]
fn test_core_field_multiple_timestamp_variants() {
    let input = r#"{"ts": "2024-01-01T12:00:00Z", "timestamp": "2024-01-01T13:00:00Z", "time": "2024-01-01T14:00:00Z", "level": "INFO", "message": "Multiple timestamps", "other": "data"}"#;

    let (stdout, _stderr, exit_code) = run_kelora_with_input(&["-f", "json", "--core"], input);
    assert_eq!(
        exit_code, 0,
        "kelora should exit successfully with multiple timestamp variants"
    );

    // Should include all timestamp field variants (current behavior: include all matching names)
    assert!(stdout.contains("ts="), "Should contain ts field");
    assert!(stdout.contains("ts="), "Should contain ts field");
    assert!(stdout.contains("time="), "Should contain time field");
    assert!(stdout.contains("level="), "Should contain level field");
    assert!(stdout.contains("message="), "Should contain message field");
    assert!(
        !stdout.contains("other="),
        "Should not contain non-core other field"
    );
}

#[test]
fn test_quiet_level_0_normal_output() {
    // Test normal mode (level 0) - shows everything
    let input = r#"{"level": "info", "message": "test"}"#;

    let (stdout, stderr, exit_code) = run_kelora_with_input(
        &[
            "-f",
            "json",
            "--with-stats",
            "--exec",
            "print(\"Script output\")",
        ],
        input,
    );
    assert_eq!(exit_code, 0);

    // Should show event output
    assert!(stdout.contains("level='info'"));
    assert!(stdout.contains("message='test'"));

    // Should show script output
    assert!(stdout.contains("Script output"));

    // Should show stats
    assert!(stderr.contains("Stats"));
    assert!(stderr.contains("Lines processed"));
}

#[test]
fn test_quiet_level_1_suppress_diagnostics() {
    // Test -q (suppress events) - script output stays, stats still emit when requested
    let input = r#"{"level": "info", "message": "test"}"#;

    let (stdout, stderr, exit_code) = run_kelora_with_input(
        &[
            "-f",
            "json",
            "--with-stats",
            "--exec",
            "print(\"Script output\")",
            "-q",
        ],
        input,
    );
    assert_eq!(exit_code, 0);

    // Events are suppressed, but script output remains
    assert!(!stdout.contains("level='info'"));
    assert!(!stdout.contains("message='test'"));
    assert!(stdout.contains("Script output"));

    // Stats still emit when requested
    assert!(stderr.contains("Stats"));
    assert!(stderr.contains("Lines processed"));
}

#[test]
fn test_quiet_level_2_suppress_events() {
    // Test -q with --no-diagnostics - suppress events and diagnostics but show script output
    let input = r#"{"level": "info", "message": "test"}"#;

    let (stdout, stderr, exit_code) = run_kelora_with_input(
        &[
            "-f",
            "json",
            "--with-stats",
            "--exec",
            "print(\"Script output\")",
            "-q",
            "--no-diagnostics",
        ],
        input,
    );
    assert_eq!(exit_code, 0);

    // Should NOT show event output
    assert!(!stdout.contains("level='info'"));
    assert!(!stdout.contains("message='test'"));

    // Should still show script output (not tied to diagnostics)
    assert!(stdout.contains("Script output"));

    // Stats should still emit when requested even if diagnostics are suppressed
    assert!(stderr.contains("Stats"));
    assert!(stderr.contains("Lines processed"));
}

#[test]
fn test_quiet_level_3_suppress_all() {
    // Test --silent - suppress pipeline output but allow script output unless explicitly disabled
    let input = r#"{"level": "info", "message": "test"}"#;

    let (stdout, stderr, exit_code) = run_kelora_with_input(
        &[
            "-f",
            "json",
            "--with-stats",
            "--exec",
            "print(\"Script output\")",
            "--silent",
        ],
        input,
    );
    assert_eq!(exit_code, 0);

    // Should NOT show event output
    assert!(!stdout.contains("level='info'"));
    assert!(!stdout.contains("message='test'"));

    // Should still show script output
    assert!(stdout.contains("Script output"));

    // Should NOT show stats
    assert!(!stderr.contains("Stats"));
    assert!(!stderr.contains("Lines processed"));

    // Only script output should remain on stdout
    assert_eq!(stderr.trim(), "");
}

#[test]
fn test_normalize_ts_normalizes_primary_timestamp_default_output() {
    let input = r#"{"ts": "2025-01-15 10:00:00", "level": "INFO", "message": "Test"}"#;

    let (stdout, _stderr, exit_code) = run_kelora_with_input(
        &["-f", "json", "--input-tz", "UTC", "--normalize-ts"],
        input,
    );

    assert_eq!(
        exit_code, 0,
        "kelora should exit successfully with --normalize-ts"
    );
    assert!(
        stdout.contains("ts='2025-01-15T10:00:00+00:00'"),
        "Primary timestamp should be normalized in default formatter output. stdout: {}",
        stdout
    );
    assert!(
        !stdout.contains("2025-01-15 10:00:00"),
        "Original timestamp representation should be replaced in default output. stdout: {}",
        stdout
    );
}

#[test]
fn test_normalize_ts_normalizes_primary_timestamp_json_output() {
    let input = r#"{"ts": "2025-01-15 10:00:00", "level": "INFO"}"#;

    let (stdout, _stderr, exit_code) = run_kelora_with_input(
        &[
            "-f",
            "json",
            "--input-tz",
            "UTC",
            "--normalize-ts",
            "-F",
            "json",
        ],
        input,
    );

    assert_eq!(
        exit_code, 0,
        "kelora should exit successfully with --normalize-ts"
    );
    assert!(
        stdout.contains("\"ts\":\"2025-01-15T10:00:00+00:00\""),
        "JSON formatter should receive normalized timestamp. stdout: {}",
        stdout
    );
    assert!(
        !stdout.contains("2025-01-15 10:00:00"),
        "Original timestamp representation should be replaced in JSON output. stdout: {}",
        stdout
    );
}

#[test]
fn test_no_emoji_flag() {
    // Test that --no-emoji suppresses emoji output
    let input = r#"{"level": "error", "message": "test error"}"#;

    let (_stdout_with_emoji, _stderr_with_emoji, _) =
        run_kelora_with_input(&["-f", "json", "--with-stats"], input);
    let (stdout_no_emoji, stderr_no_emoji, _) =
        run_kelora_with_input(&["-f", "json", "--with-stats", "--no-emoji"], input);

    // Stats output might contain emojis by default
    // With --no-emoji, no emojis should be present
    // Check that no emoji characters are in the no-emoji output
    let has_emoji_chars = |s: &str| s.chars().any(|c| c as u32 > 0x1F000);

    // The output with --no-emoji should not contain emoji characters
    assert!(
        !has_emoji_chars(&stdout_no_emoji) && !has_emoji_chars(&stderr_no_emoji),
        "Output with --no-emoji should not contain emoji characters"
    );
}

#[test]
fn test_force_color_flag() {
    // Test that --force-color enables color output even when not in a TTY
    let input = r#"{"level": "error", "message": "test"}"#;

    let (stdout, _stderr, exit_code) =
        run_kelora_with_input(&["-f", "json", "--force-color"], input);

    assert_eq!(exit_code, 0, "kelora should exit successfully");
    // Color codes typically use ANSI escape sequences
    // Check for ANSI escape codes in output (e.g., \x1b[ or \u{1b}[)
    // Note: This might not work if color is disabled in test environment
    // but we can at least ensure the flag doesn't cause errors
    assert!(!stdout.is_empty(), "Should produce output");
}

#[test]
fn test_no_color_flag() {
    // Test that --no-color disables color output
    let input = r#"{"level": "error", "message": "test"}"#;

    let (stdout, _stderr, exit_code) = run_kelora_with_input(&["-f", "json", "--no-color"], input);

    assert_eq!(exit_code, 0, "kelora should exit successfully");
    // Check that output does not contain ANSI color codes
    assert!(
        !stdout.contains("\x1b["),
        "Output with --no-color should not contain ANSI escape codes"
    );
}

#[test]
fn test_wrap_and_no_wrap_flags() {
    // Test --wrap and --no-wrap flags for long output lines
    let long_message = "a".repeat(200);
    let input = format!(r#"{{"level": "info", "message": "{}"}}"#, long_message);

    // Test with default behavior
    let (stdout_default, _stderr, exit_code_default) =
        run_kelora_with_input(&["-f", "json"], &input);
    assert_eq!(exit_code_default, 0);

    // Test with --no-wrap
    let (stdout_no_wrap, _stderr, exit_code_no_wrap) =
        run_kelora_with_input(&["-f", "json", "--no-wrap"], &input);
    assert_eq!(exit_code_no_wrap, 0);

    // Test with --wrap
    let (stdout_wrap, _stderr, exit_code_wrap) =
        run_kelora_with_input(&["-f", "json", "--wrap"], &input);
    assert_eq!(exit_code_wrap, 0);

    // All should produce some output
    assert!(!stdout_default.is_empty());
    assert!(!stdout_no_wrap.is_empty());
    assert!(!stdout_wrap.is_empty());

    // No-wrap output should be on a single line (no newlines within the data)
    let no_wrap_line_count = stdout_no_wrap.trim().lines().count();
    assert_eq!(
        no_wrap_line_count, 1,
        "No-wrap output should be a single line"
    );
}

#[test]
fn test_expand_nested_flag() {
    // Test --expand-nested for nested JSON objects
    let input = r#"{"level": "info", "metadata": {"user": "alice", "request_id": "123"}}"#;

    let (stdout_default, _stderr, exit_code_default) =
        run_kelora_with_input(&["-f", "json"], input);
    assert_eq!(exit_code_default, 0);

    let (stdout_expanded, _stderr, exit_code_expanded) =
        run_kelora_with_input(&["-f", "json", "--expand-nested"], input);
    assert_eq!(exit_code_expanded, 0);

    // Both should produce output
    assert!(!stdout_default.is_empty());
    assert!(!stdout_expanded.is_empty());

    // Expanded output might have different formatting for nested objects
    // We can at least ensure the flag doesn't cause errors and produces output
    assert!(
        stdout_expanded.contains("metadata"),
        "Expanded output should contain metadata field"
    );
}

#[test]
fn test_mark_gaps_flag() {
    // Test --mark-gaps for marking gaps in timestamp sequences
    let input = r#"{"ts": "2024-01-01T10:00:00Z", "message": "first"}
{"ts": "2024-01-01T10:00:01Z", "message": "second"}
{"ts": "2024-01-01T10:05:00Z", "message": "third with gap"}
{"ts": "2024-01-01T10:05:01Z", "message": "fourth"}"#;

    let (stdout_default, _stderr, exit_code_default) =
        run_kelora_with_input(&["-f", "json"], input);
    assert_eq!(exit_code_default, 0);

    // --mark-gaps requires a duration argument
    let (stdout_gaps, stderr_gaps, exit_code_gaps) =
        run_kelora_with_input(&["-f", "json", "--mark-gaps", "30s"], input);
    assert_eq!(
        exit_code_gaps, 0,
        "kelora should exit successfully with --mark-gaps, stderr: {}",
        stderr_gaps
    );

    // Both should produce output
    assert!(!stdout_default.is_empty());
    assert!(!stdout_gaps.is_empty());

    // The mark-gaps output should contain all the messages
    assert!(stdout_gaps.contains("first"));
    assert!(stdout_gaps.contains("second"));
    assert!(stdout_gaps.contains("third with gap"));
    assert!(stdout_gaps.contains("fourth"));
}

#[test]
fn test_conflicting_color_flags() {
    // Test that conflicting color flags are handled
    let input = r#"{"level": "info", "message": "test"}"#;

    // Both --force-color and --no-color - last one should win or error
    let (_stdout, _stderr, exit_code) =
        run_kelora_with_input(&["-f", "json", "--force-color", "--no-color"], input);

    // Should either succeed (last flag wins) or fail with usage error
    assert!(
        exit_code == 0 || exit_code == 2,
        "Should either succeed or fail with usage error"
    );
}

#[test]
fn test_csv_without_keys_suggests_column_order_example() {
    let input = r#"{"ts": "2024-01-01T10:00:00Z", "level": "info", "msg": "hello"}"#;

    let (_stdout, stderr, exit_code) = run_kelora_with_input(&["-f", "json", "-F", "csv"], input);

    assert_ne!(exit_code, 0, "csv without keys should fail");
    assert!(
        stderr.contains("CSV output requires --keys to define column order"),
        "stderr should explain the missing keys requirement: {}",
        stderr
    );
    assert!(
        stderr.contains("--keys ts,level,msg") && stderr.contains("Use -s"),
        "stderr should include an example and field discovery hint: {}",
        stderr
    );
}

#[test]
fn test_keymap_without_single_key_suggests_example() {
    let input = r#"{"level": "info", "msg": "hello"}"#;

    let (_stdout, stderr, exit_code) =
        run_kelora_with_input(&["-f", "json", "-F", "keymap"], input);

    assert_ne!(exit_code, 0, "keymap without a single key should fail");
    assert!(
        stderr.contains("keymap output requires exactly one field via --keys"),
        "stderr should explain the single-field requirement: {}",
        stderr
    );
    assert!(
        stderr.contains("--keys level"),
        "stderr should include a concrete example: {}",
        stderr
    );
}

#[test]
fn test_tailmap_without_single_key_suggests_numeric_example() {
    let input = r#"{"latency_ms": 123, "msg": "hello"}"#;

    let (_stdout, stderr, exit_code) =
        run_kelora_with_input(&["-f", "json", "-F", "tailmap"], input);

    assert_ne!(exit_code, 0, "tailmap without a single key should fail");
    assert!(
        stderr.contains("tailmap output requires exactly one numeric field via --keys"),
        "stderr should explain the single numeric field requirement: {}",
        stderr
    );
    assert!(
        stderr.contains("--keys latency_ms"),
        "stderr should include a numeric-field example: {}",
        stderr
    );
}

#[test]
fn test_drain_without_keys_suggests_single_field_example() {
    let input = r#"{"msg": "hello world"}"#;

    let (_stdout, stderr, exit_code) = run_kelora_with_input(&["-f", "json", "--drain"], input);

    assert_ne!(exit_code, 0, "drain without keys should fail");
    assert!(
        stderr.contains("--drain requires exactly one effective field in --keys"),
        "stderr should explain the drain field requirement: {}",
        stderr
    );
    assert!(
        stderr.contains("--keys msg") && stderr.contains("Use -s"),
        "stderr should include an example and discovery hint: {}",
        stderr
    );
}

#[test]
fn test_drain_parallel_suggests_sequential_rerun() {
    let input = r#"{"msg": "hello world"}"#;

    let (_stdout, stderr, exit_code) = run_kelora_with_input(
        &["-f", "json", "--parallel", "--drain", "--keys", "msg"],
        input,
    );

    assert_ne!(exit_code, 0, "drain with parallel mode should fail");
    assert!(
        stderr.contains("--drain summary is not supported with --parallel"),
        "stderr should explain the mode conflict: {}",
        stderr
    );
    assert!(
        stderr.contains("Rerun without --parallel"),
        "stderr should suggest the sequential rerun: {}",
        stderr
    );
}

#[test]
fn test_wrap_with_different_formatters() {
    // Test wrap behavior with different output formatters
    let long_message = "x".repeat(150);
    let input = format!(r#"{{"message": "{}"}}"#, long_message);

    // Test with JSON formatter
    let (stdout_json, _stderr, exit_code_json) =
        run_kelora_with_input(&["-f", "json", "-F", "json", "--no-wrap"], &input);
    assert_eq!(exit_code_json, 0);
    assert_eq!(stdout_json.trim().lines().count(), 1);

    // Test with default formatter
    let (stdout_default, _stderr, exit_code_default) =
        run_kelora_with_input(&["-f", "json", "--no-wrap"], &input);
    assert_eq!(exit_code_default, 0);
    assert!(!stdout_default.is_empty());
}

#[test]
fn test_multiple_display_flags_combination() {
    // Test combining multiple display flags
    let input = r#"{"level": "error", "message": "test error", "nested": {"key": "value"}}"#;

    let (stdout, _stderr, exit_code) = run_kelora_with_input(
        &[
            "-f",
            "json",
            "--no-emoji",
            "--no-color",
            "--no-wrap",
            "--expand-nested",
        ],
        input,
    );

    assert_eq!(exit_code, 0, "kelora should handle multiple display flags");
    assert!(!stdout.is_empty(), "Should produce output");
}

#[test]
fn test_color_with_filtering() {
    // Test that color flags work with filtering
    let input = r#"{"level": "info", "message": "info msg"}
{"level": "error", "message": "error msg"}"#;

    let (stdout, _stderr, exit_code) = run_kelora_with_input(
        &[
            "-f",
            "json",
            "--filter",
            "e.level == \"error\"",
            "--no-color",
        ],
        input,
    );

    assert_eq!(exit_code, 0);
    assert!(stdout.contains("error msg"));
    assert!(!stdout.contains("info msg"));
    // Should not contain ANSI codes
    assert!(!stdout.contains("\x1b["));
}