batless 0.5.0

A non-blocking, LLM-friendly code viewer inspired by bat
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
//! Additional CLI integration tests to improve coverage for main.rs and config_manager.rs
//! Focuses on edge cases, error handling, and command combinations not covered elsewhere

use std::io::Write;
use std::process::Command;
use tempfile::NamedTempFile;

fn batless_command() -> Command {
    Command::new(env!("CARGO_BIN_EXE_batless"))
}

fn run_batless_args(args: &[&str]) -> std::process::Output {
    batless_command()
        .args(args)
        .output()
        .expect("Failed to execute batless")
}

#[test]
fn test_version_json_command() {
    let output = run_batless_args(&["--version-json"]);
    assert!(output.status.success(), "Command should succeed");

    let stdout_str = String::from_utf8(output.stdout).expect("Valid UTF-8 output");
    assert!(stdout_str.contains("\"name\""), "Should contain name field");
    assert!(
        stdout_str.contains("\"version\""),
        "Should contain version field"
    );
    assert!(
        stdout_str.contains("\"git_hash\""),
        "Should contain git_hash field"
    );
    assert!(
        stdout_str.contains("\"build_timestamp\""),
        "Should contain build_timestamp field"
    );
    assert!(
        stdout_str.contains("\"authors\""),
        "Should contain authors field"
    );

    // Verify it's valid JSON
    let parsed: serde_json::Value =
        serde_json::from_str(&stdout_str).expect("Output should be valid JSON");
    assert!(parsed.is_object(), "Should be a JSON object");
}

#[test]
fn test_get_schema_file_info() {
    let output = run_batless_args(&["--get-schema", "file_info"]);
    assert!(output.status.success(), "Command should succeed");

    let stdout_str = String::from_utf8(output.stdout).expect("Valid UTF-8 output");
    assert!(
        stdout_str.contains("\"$schema\""),
        "Should contain schema field"
    );

    // Verify it's valid JSON
    let _parsed: serde_json::Value =
        serde_json::from_str(&stdout_str).expect("Output should be valid JSON schema");
}

#[test]
fn test_get_schema_json_output() {
    let output = run_batless_args(&["--get-schema", "json_output"]);
    assert!(output.status.success(), "Command should succeed");

    let stdout_str = String::from_utf8(output.stdout).expect("Valid UTF-8 output");
    assert!(
        stdout_str.contains("\"$schema\""),
        "Should contain schema field"
    );
}

#[test]
fn test_get_schema_token_count() {
    let output = run_batless_args(&["--get-schema", "token_count"]);
    assert!(output.status.success(), "Command should succeed");

    let stdout_str = String::from_utf8(output.stdout).expect("Valid UTF-8 output");
    assert!(
        stdout_str.contains("\"$schema\""),
        "Should contain schema field"
    );
}

#[test]
fn test_get_schema_processing_stats() {
    let output = run_batless_args(&["--get-schema", "processing_stats"]);
    assert!(output.status.success(), "Command should succeed");

    let stdout_str = String::from_utf8(output.stdout).expect("Valid UTF-8 output");
    assert!(
        stdout_str.contains("\"$schema\""),
        "Should contain schema field"
    );
}

#[test]
fn test_get_schema_invalid() {
    let output = run_batless_args(&["--get-schema", "invalid_schema"]);
    assert!(!output.status.success(), "Command should fail");

    let stderr_str = String::from_utf8(output.stderr).expect("Valid UTF-8 output");
    assert!(
        stderr_str.contains("Unknown schema format"),
        "Should show error message"
    );
    assert!(
        stderr_str.contains("Available schemas:"),
        "Should show available options"
    );
}

#[test]
#[ignore] // Interactive wizard test - requires TTY input, skip in CI
fn test_configuration_wizard_help() {
    use std::process::Stdio;

    // Use spawn with stdin piped to immediately close it,
    // which causes the wizard to exit gracefully instead of hanging
    let mut child = batless_command()
        .arg("run")
        .arg("--")
        .arg("--configure")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("Failed to spawn batless");

    // Drop stdin immediately to send EOF, causing wizard to exit
    drop(child.stdin.take());

    // Wait for the process with a timeout
    let output = child
        .wait_with_output()
        .expect("Failed to wait for process");

    // The wizard should exit (either successfully or with an error about stdin)
    // We just verify it doesn't hang and doesn't panic
    let _status_code = output.status.code().unwrap_or(-1);
}

#[test]
fn test_list_profiles_command() {
    let output = run_batless_args(&["--list-profiles"]);
    assert!(output.status.success(), "Command should succeed");

    let _stdout_str = String::from_utf8(output.stdout).expect("Valid UTF-8 output");
    // Output depends on whether profiles exist, but command should not fail
    // Just verify it doesn't crash - output content varies
}

#[test]
fn test_edit_profile_nonexistent() {
    let output = run_batless_args(&["--edit-profile", "nonexistent_profile.toml"]);
    // This should either succeed (if file is created) or fail gracefully
    let status_code = output.status.code().unwrap_or(-1);
    assert!(
        status_code >= 0,
        "Command should handle nonexistent profile gracefully"
    );
}

#[test]
fn test_cli_with_max_lines_zero() {
    let mut temp_file = NamedTempFile::new().expect("Failed to create temp file");
    writeln!(temp_file, "line1\nline2\nline3").expect("Failed to write to temp file");
    let temp_path = temp_file.path().to_str().expect("Invalid temp path");

    let output = run_batless_args(&["--max-lines", "0", temp_path]);
    assert!(
        !output.status.success(),
        "Command should fail with max-lines=0"
    );

    let stderr_str = String::from_utf8(output.stderr).expect("Valid UTF-8 output");
    assert!(
        stderr_str.contains("validation"),
        "Should show validation error"
    );
}

#[test]
fn test_cli_with_max_bytes_zero() {
    let mut temp_file = NamedTempFile::new().expect("Failed to create temp file");
    writeln!(temp_file, "content").expect("Failed to write to temp file");
    let temp_path = temp_file.path().to_str().expect("Invalid temp path");

    let output = run_batless_args(&["--max-bytes", "0", temp_path]);
    assert!(
        !output.status.success(),
        "Command should fail with max-bytes=0"
    );

    let stderr_str = String::from_utf8(output.stderr).expect("Valid UTF-8 output");
    assert!(
        stderr_str.contains("validation"),
        "Should show validation error"
    );
}

#[test]
fn test_cli_with_invalid_theme() {
    let mut temp_file = NamedTempFile::new().expect("Failed to create temp file");
    writeln!(temp_file, "content").expect("Failed to write to temp file");
    let temp_path = temp_file.path().to_str().expect("Invalid temp path");

    let output = run_batless_args(&["--theme", "nonexistent-theme", temp_path]);
    assert!(
        !output.status.success(),
        "Command should fail with invalid theme"
    );

    let stderr_str = String::from_utf8(output.stderr).expect("Valid UTF-8 output");
    assert!(
        stderr_str.contains("Theme not found") || stderr_str.contains("theme"),
        "Should show theme-related error"
    );
}

#[test]
fn test_cli_with_invalid_language() {
    let mut temp_file = NamedTempFile::new().expect("Failed to create temp file");
    writeln!(temp_file, "content").expect("Failed to write to temp file");
    let temp_path = temp_file.path().to_str().expect("Invalid temp path");

    let output = run_batless_args(&["--language", "NonexistentLang", temp_path]);
    assert!(
        !output.status.success(),
        "Command should fail with invalid language"
    );

    let stderr_str = String::from_utf8(output.stderr).expect("Valid UTF-8 output");
    assert!(
        stderr_str.contains("Language not found") || stderr_str.contains("language"),
        "Should show language-related error"
    );
}

#[test]
fn test_streaming_json_with_checkpoints() {
    let mut temp_file = NamedTempFile::new().expect("Failed to create temp file");
    // Create a larger file for streaming
    for i in 0..100 {
        writeln!(temp_file, "Line number {} with some content", i).expect("Failed to write");
    }
    let temp_path = temp_file.path().to_str().expect("Invalid temp path");

    let output = run_batless_args(&[
        "--mode",
        "json",
        "--streaming-json",
        "--enable-resume",
        "--streaming-chunk-size",
        "10",
        temp_path,
    ]);
    assert!(output.status.success(), "Streaming command should succeed");

    let stdout_str = String::from_utf8(output.stdout).expect("Valid UTF-8 output");
    assert!(!stdout_str.is_empty(), "Should produce streaming output");
}

#[test]
fn test_ai_model_token_counting() {
    let mut temp_file = NamedTempFile::new().expect("Failed to create temp file");
    writeln!(temp_file, "def hello(): print('world')").expect("Failed to write to temp file");
    let temp_path = temp_file.path().to_str().expect("Invalid temp path");

    let output = run_batless_args(&[
        "--ai-model",
        "gpt4",
        "--include-tokens",
        "--mode",
        "json",
        temp_path,
    ]);
    assert!(output.status.success(), "Token counting should succeed");

    let stdout_str = String::from_utf8(output.stdout).expect("Valid UTF-8 output");
    assert!(
        stdout_str.contains("identifiers"),
        "Should include identifier information"
    );
}

#[test]
fn test_fit_context_with_prompt_tokens() {
    let mut temp_file = NamedTempFile::new().expect("Failed to create temp file");
    // Create a very large file to test context fitting
    for i in 0..1000 {
        writeln!(temp_file, "This is line {} with substantial content to test context window fitting and truncation behavior.", i).expect("Failed to write");
    }
    let temp_path = temp_file.path().to_str().expect("Invalid temp path");

    let output = run_batless_args(&[
        "--ai-model",
        "gpt4",
        "--fit-context",
        "--prompt-tokens",
        "1000",
        "--mode",
        "json",
        temp_path,
    ]);
    assert!(output.status.success(), "Context fitting should succeed");

    let stdout_str = String::from_utf8(output.stdout).expect("Valid UTF-8 output");
    // Verify the content was processed and potentially truncated
    assert!(!stdout_str.is_empty(), "Should produce output");
}

#[test]
fn test_fit_context_marks_truncation_in_json() {
    let mut temp_file = NamedTempFile::new().expect("Failed to create temp file");
    writeln!(temp_file, "line one").expect("Failed to write");
    writeln!(temp_file, "line two").expect("Failed to write");
    let temp_path = temp_file.path().to_str().expect("Invalid temp path");

    // Use a prompt token count that exceeds the model context window to force truncation
    let output = run_batless_args(&[
        "--ai-model",
        "gpt4",
        "--fit-context",
        "--prompt-tokens",
        "200000",
        "--mode",
        "json",
        temp_path,
    ]);

    assert!(output.status.success(), "Context fitting should succeed");

    let stdout_str = String::from_utf8(output.stdout).expect("Valid UTF-8 output");
    let json: serde_json::Value =
        serde_json::from_str(&stdout_str).expect("Output should parse as JSON");

    assert_eq!(json["truncated"], true, "Should flag truncation");
    assert_eq!(
        json["truncated_by_context"], true,
        "Should indicate context-fit truncation"
    );
    assert_eq!(
        json["truncated_by_lines"], false,
        "No line limit in this scenario"
    );
    assert_eq!(
        json["truncated_by_bytes"], false,
        "No byte limit in this scenario"
    );
}

#[test]
fn test_custom_profile_nonexistent() {
    let mut temp_file = NamedTempFile::new().expect("Failed to create temp file");
    writeln!(temp_file, "content").expect("Failed to write to temp file");
    let temp_path = temp_file.path().to_str().expect("Invalid temp path");

    let output = run_batless_args(&["--custom-profile", "nonexistent_profile.toml", temp_path]);
    assert!(
        !output.status.success(),
        "Command should fail with nonexistent profile"
    );

    let stderr_str = String::from_utf8(output.stderr).expect("Valid UTF-8 output");
    assert!(
        stderr_str.contains("profile") || stderr_str.contains("file"),
        "Should show profile or file related error"
    );
}

#[test]
fn test_error_message_formatting() {
    // Test that error messages are properly formatted
    let output = run_batless_args(&["nonexistent_file.txt"]);
    assert!(!output.status.success(), "Command should fail");

    let stderr_str = String::from_utf8(output.stderr).expect("Valid UTF-8 output");
    assert!(
        stderr_str.contains("Error"),
        "Should contain 'Error' prefix"
    );
    assert!(
        stderr_str.len() > 10,
        "Should contain meaningful error message"
    );
}

#[test]
fn test_validate_json_flag() {
    let mut temp_file = NamedTempFile::new().expect("Failed to create temp file");
    writeln!(temp_file, "{{\"key\": \"value\"}}").expect("Failed to write to temp file");
    let temp_path = temp_file.path().to_str().expect("Invalid temp path");

    let output = run_batless_args(&["--mode", "json", "--validate-json", temp_path]);
    assert!(output.status.success(), "JSON validation should succeed");
}

#[test]
fn test_completion_generation_all_shells() {
    // Test bash completion
    let output = run_batless_args(&["--generate-completions", "bash"]);
    assert!(
        output.status.success(),
        "Bash completion generation should succeed"
    );
    let stdout = String::from_utf8(output.stdout).expect("Valid UTF-8");
    assert!(!stdout.is_empty(), "Should generate bash completions");

    // Test zsh completion
    let output = run_batless_args(&["--generate-completions", "zsh"]);
    assert!(
        output.status.success(),
        "Zsh completion generation should succeed"
    );
    let stdout = String::from_utf8(output.stdout).expect("Valid UTF-8");
    assert!(!stdout.is_empty(), "Should generate zsh completions");

    // Test fish completion
    let output = run_batless_args(&["--generate-completions", "fish"]);
    assert!(
        output.status.success(),
        "Fish completion generation should succeed"
    );
    let stdout = String::from_utf8(output.stdout).expect("Valid UTF-8");
    assert!(!stdout.is_empty(), "Should generate fish completions");

    // Test PowerShell completion
    let output = run_batless_args(&["--generate-completions", "power-shell"]);
    assert!(
        output.status.success(),
        "PowerShell completion generation should succeed"
    );
    let stdout = String::from_utf8(output.stdout).expect("Valid UTF-8");
    assert!(!stdout.is_empty(), "Should generate PowerShell completions");
}