rskim 2.3.0

The most intelligent context optimization engine for coding agents. Code-aware AST parsing, command rewriting, output compression.
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
//! CLI subcommand disambiguation tests.
//!
//! Validates the pre-parse router correctly distinguishes file operations
//! from subcommands, maintaining 100% backward compatibility.

use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::TempDir;

const FIND_FIXTURE: &str = include_str!("fixtures/cmd/file/find_small.txt");
const GH_FIXTURE: &str = include_str!("fixtures/cmd/infra/gh_pr_list.json");
const LOG_FIXTURE: &str = include_str!("fixtures/cmd/log/plaintext_mixed.txt");

fn skim_cmd() -> Command {
    Command::cargo_bin("skim").unwrap()
}

// ============================================================================
// File operation routing (backward compatibility)
// ============================================================================

#[test]
fn test_file_with_extension_routes_to_file_operation() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("test.ts");
    fs::write(&file, "function add(a: number): number { return a; }").unwrap();

    Command::cargo_bin("skim")
        .unwrap()
        .arg(&file)
        .assert()
        .success()
        .stdout(predicate::str::contains("function add"));
}

#[test]
fn test_file_named_init_py_routes_to_file_operation() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("init.py");
    fs::write(&file, "def hello(): pass").unwrap();

    // "init" is a known subcommand, but "init.py" contains a dot
    Command::cargo_bin("skim")
        .unwrap()
        .arg(&file)
        .assert()
        .success()
        .stdout(predicate::str::contains("def hello"));
}

#[test]
fn test_path_with_separator_routes_to_file_operation() {
    let dir = TempDir::new().unwrap();
    let subdir = dir.path().join("src");
    fs::create_dir(&subdir).unwrap();
    let file = subdir.join("test.rs");
    fs::write(&file, "fn main() {}").unwrap();

    Command::cargo_bin("skim")
        .unwrap()
        .arg(&file)
        .arg("--mode=signatures")
        .assert()
        .success();
}

#[test]
fn test_stdin_dash_routes_to_file_operation() {
    Command::cargo_bin("skim")
        .unwrap()
        .arg("-")
        .arg("-l")
        .arg("rust")
        .write_stdin("fn main() {}")
        .assert()
        .success()
        .stdout(predicate::str::contains("fn main"));
}

#[test]
fn test_glob_pattern_routes_to_file_operation() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("foo.ts");
    fs::write(&file, "const x = 1;").unwrap();

    // Glob chars → FileOperation (use relative pattern with current_dir)
    Command::cargo_bin("skim")
        .unwrap()
        .current_dir(dir.path())
        .arg("*.ts")
        .assert()
        .success();
}

#[test]
fn test_dot_routes_to_file_operation() {
    // "." is a directory — contains a dot → FileOperation
    Command::cargo_bin("skim")
        .unwrap()
        .arg(".")
        .assert()
        .success();
}

#[test]
fn test_no_positional_routes_to_file_operation() {
    // Flags only, no positional → FileOperation → clap handles --clear-cache
    Command::cargo_bin("skim")
        .unwrap()
        .arg("--clear-cache")
        .assert()
        .success();
}

#[test]
fn test_double_dash_before_subcommand_name_routes_to_file_operation() {
    // `skim -- test` should NOT route to subcommand
    // `--` means everything after is positional, so "test" is a file arg.
    // This will fail because no file named "test" exists, but the important
    // thing is it does NOT route to the subcommand stub.
    let output = Command::cargo_bin("skim")
        .unwrap()
        .arg("--")
        .arg("test")
        .assert()
        .failure();

    // Should get a file error, not "not yet implemented"
    output.stderr(predicate::str::contains("not yet implemented").not());
}

// ============================================================================
// Subcommand routing
// ============================================================================

#[test]
fn test_known_subcommand_init_is_implemented() {
    // "init" is a known, implemented subcommand — help should work
    Command::cargo_bin("skim")
        .unwrap()
        .arg("init")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("skim init"));
}

#[test]
fn test_subcommand_init_with_unknown_flag_fails() {
    // "init" with an unknown flag should fail gracefully
    Command::cargo_bin("skim")
        .unwrap()
        .arg("init")
        .arg("--nonexistent-flag")
        .assert()
        .failure()
        .stderr(predicate::str::contains("unknown flag"));
}

#[test]
fn test_subcommand_help_exits_zero() {
    Command::cargo_bin("skim")
        .unwrap()
        .arg("init")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("skim init"))
        .stdout(predicate::str::contains("Install skim"));
}

#[test]
fn test_subcommand_short_help_exits_zero() {
    Command::cargo_bin("skim")
        .unwrap()
        .arg("build")
        .arg("-h")
        .assert()
        .success()
        .stdout(predicate::str::contains("skim build"));
}

// All known subcommands are now implemented — no stubs remaining.
// Previously tested init as a stub; now it's fully implemented (#44).

// ============================================================================
// File-named-as-subcommand precedence
// ============================================================================

#[test]
fn test_full_path_to_file_named_as_subcommand_uses_separator_heuristic() {
    let dir = TempDir::new().unwrap();
    // Create a file called "init" (no extension) in the temp dir
    let file = dir.path().join("init");
    fs::write(&file, "fn setup() {}").unwrap();

    // Full path contains "/" → routes via path-separator heuristic (never reaches path.exists())
    Command::cargo_bin("skim")
        .unwrap()
        .arg(&file)
        .arg("-l")
        .arg("rust")
        .assert()
        .success()
        .stdout(predicate::str::contains("fn setup"));
}

#[test]
fn test_bare_file_named_as_subcommand_routes_to_subcommand() {
    let dir = TempDir::new().unwrap();
    // Create a file called "init" (a known subcommand name) in the temp dir
    let file = dir.path().join("init");
    fs::write(&file, "fn setup() {}").unwrap();

    // After the router fix, bare "init" ALWAYS routes to the subcommand
    // regardless of whether a file with that name exists on disk.
    // To read such a file, users must use ./init or the full path.
    Command::cargo_bin("skim")
        .unwrap()
        .current_dir(dir.path())
        .arg("init")
        .arg("--help")
        .assert()
        .success()
        // Should show the subcommand help, not the file contents
        .stdout(predicate::str::contains("skim init"))
        .stdout(predicate::str::contains("fn setup").not());
}

#[test]
fn test_full_path_to_dir_named_as_subcommand_uses_separator_heuristic() {
    let dir = TempDir::new().unwrap();
    // Create a directory called "build" with a source file inside
    let build_dir = dir.path().join("build");
    fs::create_dir(&build_dir).unwrap();
    let file = build_dir.join("main.rs");
    fs::write(&file, "fn main() {}").unwrap();

    // Full path contains "/" → routes via path-separator heuristic (never reaches path.exists())
    Command::cargo_bin("skim")
        .unwrap()
        .arg(&build_dir)
        .assert()
        .success();
}

#[test]
fn test_bare_dir_named_as_subcommand_routes_to_subcommand() {
    let dir = TempDir::new().unwrap();
    // Create a directory called "build" (a known subcommand name) with a source file inside
    let build_dir = dir.path().join("build");
    fs::create_dir(&build_dir).unwrap();
    let file = build_dir.join("main.rs");
    fs::write(&file, "fn main() {}").unwrap();

    // After the router fix, bare "build" ALWAYS routes to the subcommand
    // regardless of whether a directory with that name exists on disk.
    // To process such a directory, users must use ./build or the full path.
    Command::cargo_bin("skim")
        .unwrap()
        .current_dir(dir.path())
        .arg("build")
        .arg("--help")
        .assert()
        .success()
        // Should show the subcommand help, not process the directory
        .stdout(predicate::str::contains("skim build"));
}

// ============================================================================
// Flag-with-value parsing (ensure flags don't consume subcommand names)
// ============================================================================

#[test]
fn test_mode_flag_consumes_next_token() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("test.ts");
    fs::write(&file, "function f(): void { return; }").unwrap();

    // `--mode signatures` — "signatures" is consumed by --mode, not treated
    // as a positional.
    Command::cargo_bin("skim")
        .unwrap()
        .arg("--mode")
        .arg("signatures")
        .arg(&file)
        .assert()
        .success();
}

#[test]
fn test_mode_equals_syntax_is_single_token() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("test.ts");
    fs::write(&file, "function f(): void { return; }").unwrap();

    // `--mode=signatures` is one token — the router sees no positional
    // before the file path.
    Command::cargo_bin("skim")
        .unwrap()
        .arg("--mode=signatures")
        .arg(&file)
        .assert()
        .success();
}

// ============================================================================
// Help text includes subcommands
// ============================================================================

#[test]
fn test_help_lists_subcommands() {
    Command::cargo_bin("skim")
        .unwrap()
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("SUBCOMMANDS"))
        .stdout(predicate::str::contains("init"))
        .stdout(predicate::str::contains("test"))
        .stdout(predicate::str::contains("build"))
        .stdout(predicate::str::contains("completions"));
}

// ============================================================================
// Unknown words fall through to FileOperation
// ============================================================================

#[test]
fn test_unknown_word_routes_to_file_operation() {
    // "foobar" is not a known subcommand — routes to FileOperation.
    // Clap/file-processing will produce an error since the file doesn't exist.
    Command::cargo_bin("skim")
        .unwrap()
        .arg("foobar")
        .assert()
        .failure()
        .stderr(predicate::str::contains("not yet implemented").not());
}

// ============================================================================
// Subcommand help
// ============================================================================

#[test]
fn test_subcommand_file_help() {
    skim_cmd()
        .args(["file", "--help"])
        .assert()
        .success()
        .stdout(predicate::str::contains("find"));
}

#[test]
fn test_subcommand_infra_help() {
    skim_cmd()
        .args(["infra", "--help"])
        .assert()
        .success()
        .stdout(predicate::str::contains("gh"));
}

#[test]
fn test_subcommand_log_help() {
    skim_cmd()
        .args(["log", "--help"])
        .assert()
        .success()
        .stdout(predicate::str::contains("dedup"));
}

// ============================================================================
// Error/edge paths
// ============================================================================

#[test]
fn test_subcommand_file_unknown_tool() {
    skim_cmd()
        .args(["file", "unknown-tool-xyz"])
        .assert()
        .failure();
}

#[test]
fn test_subcommand_log_empty_stdin() {
    skim_cmd().arg("log").write_stdin("").assert().success();
}

#[test]
fn test_log_conflicting_flags() {
    skim_cmd()
        .args(["log", "--debug-only", "--keep-debug"])
        .write_stdin(LOG_FIXTURE)
        .assert()
        .success();
}

#[test]
fn test_file_find_empty_stdin() {
    skim_cmd()
        .args(["file", "find"])
        .write_stdin("")
        .assert()
        .success();
}

// ============================================================================
// JSON output
// ============================================================================

#[test]
fn test_subcommand_file_json() {
    let output = skim_cmd()
        .args(["file", "find", "--json"])
        .write_stdin(FIND_FIXTURE)
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let json: serde_json::Value = serde_json::from_str(stdout.trim()).unwrap();
    assert!(json.get("tool").is_some(), "JSON should have 'tool' field");
}

#[test]
fn test_subcommand_infra_json() {
    let output = skim_cmd()
        .args(["infra", "gh", "--json"])
        .write_stdin(GH_FIXTURE)
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let json: serde_json::Value = serde_json::from_str(stdout.trim()).unwrap();
    assert!(json.get("tool").is_some(), "JSON should have 'tool' field");
}

#[test]
fn test_subcommand_log_json() {
    let output = skim_cmd()
        .args(["log", "--json"])
        .write_stdin(LOG_FIXTURE)
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let json: serde_json::Value = serde_json::from_str(stdout.trim()).unwrap();
    // total_lines is nested under the "result" key
    let has_total_lines = json
        .get("result")
        .and_then(|r| r.get("total_lines"))
        .is_some();
    assert!(
        has_total_lines,
        "JSON should have 'result.total_lines' field"
    );
}

// ============================================================================
// --show-stats token output
// ============================================================================

#[test]
fn test_subcommand_file_show_stats() {
    let output = skim_cmd()
        .args(["file", "find", "--show-stats"])
        .write_stdin(FIND_FIXTURE)
        .output()
        .unwrap();
    assert!(output.status.success());
    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(
        stderr.to_lowercase().contains("token"),
        "stderr should contain token stats, got: {stderr}"
    );
}

#[test]
fn test_subcommand_infra_show_stats() {
    let output = skim_cmd()
        .args(["infra", "gh", "--show-stats"])
        .write_stdin(GH_FIXTURE)
        .output()
        .unwrap();
    assert!(output.status.success());
    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(
        stderr.to_lowercase().contains("token"),
        "stderr should contain token stats, got: {stderr}"
    );
}

#[test]
fn test_subcommand_log_show_stats() {
    let output = skim_cmd()
        .args(["log", "--show-stats"])
        .write_stdin(LOG_FIXTURE)
        .output()
        .unwrap();
    assert!(output.status.success());
    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(
        stderr.to_lowercase().contains("token"),
        "stderr should contain token stats, got: {stderr}"
    );
}