rskim 2.3.1

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
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
//! CLI integration tests for directory processing
//!
//! Tests recursive directory processing with auto-detection

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

#[test]
fn test_directory_single_language() {
    let temp_dir = TempDir::new().unwrap();

    // Create multiple TypeScript files
    fs::write(
        temp_dir.path().join("file1.ts"),
        "function test1() { return 1; }",
    )
    .unwrap();
    fs::write(
        temp_dir.path().join("file2.ts"),
        "function test2() { return 2; }",
    )
    .unwrap();
    fs::write(
        temp_dir.path().join("file3.ts"),
        "function test3() { return 3; }",
    )
    .unwrap();

    Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("function test1"))
        .stdout(predicate::str::contains("function test2"))
        .stdout(predicate::str::contains("function test3"));
}

#[test]
fn test_directory_mixed_languages() {
    let temp_dir = TempDir::new().unwrap();

    // Create files with different languages
    fs::write(temp_dir.path().join("test.ts"), "function tsFunc() {}").unwrap();
    fs::write(temp_dir.path().join("test.py"), "def py_func(): pass").unwrap();
    fs::write(temp_dir.path().join("test.rs"), "fn rust_func() {}").unwrap();

    let output = Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path())
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let stdout = String::from_utf8(output).unwrap();

    // Verify all languages are processed
    assert!(stdout.contains("tsFunc"));
    assert!(stdout.contains("py_func"));
    assert!(stdout.contains("rust_func"));
}

#[test]
fn test_directory_recursive() {
    let temp_dir = TempDir::new().unwrap();

    // Create nested directory structure
    fs::create_dir_all(temp_dir.path().join("src/utils")).unwrap();
    fs::write(temp_dir.path().join("root.ts"), "function root() {}").unwrap();
    fs::write(temp_dir.path().join("src/main.ts"), "function main() {}").unwrap();
    fs::write(
        temp_dir.path().join("src/utils/helper.ts"),
        "function helper() {}",
    )
    .unwrap();

    let output = Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path())
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let stdout = String::from_utf8(output).unwrap();

    // Verify all nested files are processed
    assert!(stdout.contains("root"));
    assert!(stdout.contains("main"));
    assert!(stdout.contains("helper"));
}

#[test]
fn test_directory_with_headers() {
    let temp_dir = TempDir::new().unwrap();

    fs::write(temp_dir.path().join("a.ts"), "function a() {}").unwrap();
    fs::write(temp_dir.path().join("b.ts"), "function b() {}").unwrap();

    Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("// === "))
        .stdout(predicate::str::contains("a.ts"))
        .stdout(predicate::str::contains("b.ts"));
}

#[test]
fn test_directory_no_header_flag() {
    let temp_dir = TempDir::new().unwrap();

    fs::write(temp_dir.path().join("a.ts"), "function a() {}").unwrap();
    fs::write(temp_dir.path().join("b.ts"), "function b() {}").unwrap();

    Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path())
        .arg("--no-header")
        .assert()
        .success()
        .stdout(predicate::str::contains("// === ").not());
}

#[test]
fn test_directory_empty() {
    let temp_dir = TempDir::new().unwrap();

    // Empty directory - no supported files
    Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("No files found"));
}

#[test]
fn test_directory_only_unsupported_files() {
    let temp_dir = TempDir::new().unwrap();

    // Create files with unsupported extensions
    fs::write(temp_dir.path().join("file.txt"), "some text").unwrap();
    fs::write(temp_dir.path().join("file.md.bak"), "backup").unwrap();

    Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("No files found"));
}

#[test]
fn test_directory_with_modes() {
    let temp_dir = TempDir::new().unwrap();

    fs::write(
        temp_dir.path().join("test.ts"),
        "function test() { console.log('impl'); }",
    )
    .unwrap();

    // Test structure mode (default)
    Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path())
        .arg("--mode=structure")
        .assert()
        .success()
        .stdout(predicate::str::contains("function test"))
        .stdout(predicate::str::contains("/* ... */"));

    // Test signatures mode
    Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path())
        .arg("--mode=signatures")
        .assert()
        .success()
        .stdout(predicate::str::contains("function test"));
}

#[test]
fn test_directory_with_jobs_flag() {
    let temp_dir = TempDir::new().unwrap();

    fs::write(temp_dir.path().join("a.ts"), "function a() {}").unwrap();
    fs::write(temp_dir.path().join("b.ts"), "function b() {}").unwrap();
    fs::write(temp_dir.path().join("c.ts"), "function c() {}").unwrap();

    Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path())
        .arg("--jobs")
        .arg("2")
        .assert()
        .success()
        .stdout(predicate::str::contains("function a"))
        .stdout(predicate::str::contains("function b"))
        .stdout(predicate::str::contains("function c"));
}

#[test]
fn test_directory_skips_symlinks() {
    let temp_dir = TempDir::new().unwrap();

    fs::write(temp_dir.path().join("real.ts"), "function real() {}").unwrap();

    // Create a symlink (skip on Windows if not supported)
    #[cfg(unix)]
    {
        use std::os::unix::fs::symlink;
        let _ = symlink(
            temp_dir.path().join("real.ts"),
            temp_dir.path().join("link.ts"),
        );

        // The ignore crate silently skips symlinks (follow_links=false).
        // Verify the real file is processed but the symlink is not duplicated.
        let output = Command::cargo_bin("skim")
            .unwrap()
            .arg(temp_dir.path())
            .arg("--no-header")
            .assert()
            .success()
            .get_output()
            .stdout
            .clone();

        let stdout = String::from_utf8(output).unwrap();
        // The real file should be processed exactly once
        assert!(
            stdout.contains("function real"),
            "real file should be in output"
        );
        // Count occurrences of "function real" to ensure symlink was not followed
        let count = stdout.matches("function real").count();
        assert_eq!(
            count, 1,
            "expected exactly 1 occurrence (symlink should be skipped), got: {count}"
        );
    }
}

#[test]
fn test_directory_with_subdirectory() {
    let temp_dir = TempDir::new().unwrap();

    // Create subdirectory
    fs::create_dir_all(temp_dir.path().join("subdir")).unwrap();
    fs::write(temp_dir.path().join("subdir/file.ts"), "function sub() {}").unwrap();

    Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path().join("subdir"))
        .assert()
        .success()
        .stdout(predicate::str::contains("function sub"));
}

#[test]
fn test_directory_language_override_ignored() {
    let temp_dir = TempDir::new().unwrap();

    // Create mixed language files
    fs::write(temp_dir.path().join("test.ts"), "function ts() {}").unwrap();
    fs::write(temp_dir.path().join("test.py"), "def py(): pass").unwrap();

    // Even with --language flag, each file should be auto-detected
    let output = Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path())
        .arg("--language=typescript")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let stdout = String::from_utf8(output).unwrap();

    // Both should be processed correctly by their own language
    assert!(stdout.contains("ts"));
    assert!(stdout.contains("py"));
}

#[test]
fn test_directory_current_directory() {
    let temp_dir = TempDir::new().unwrap();

    fs::write(temp_dir.path().join("test.ts"), "function test() {}").unwrap();

    // Using "." should work
    Command::cargo_bin("skim")
        .unwrap()
        .arg(".")
        .current_dir(temp_dir.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("function test"));
}

// ========================================================================
// Gitignore support tests
// ========================================================================

/// Helper: create a minimal .git directory so the ignore crate recognises
/// the directory as a git repository and applies .gitignore rules.
///
/// NOTE: Duplicated in `cli_glob.rs`. Keep both in sync.
fn init_fake_git_repo(dir: &std::path::Path) {
    fs::create_dir_all(dir.join(".git")).unwrap();
}

#[test]
fn test_directory_respects_gitignore() {
    let temp_dir = TempDir::new().unwrap();
    init_fake_git_repo(temp_dir.path());

    // Create .gitignore that ignores the "ignored_dir/" directory
    fs::write(temp_dir.path().join(".gitignore"), "ignored_dir/\n").unwrap();

    // Create a visible file and an ignored file
    fs::write(temp_dir.path().join("visible.ts"), "function visible() {}").unwrap();
    fs::create_dir_all(temp_dir.path().join("ignored_dir")).unwrap();
    fs::write(
        temp_dir.path().join("ignored_dir/secret.ts"),
        "function secret() {}",
    )
    .unwrap();

    let output = Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path())
        .arg("--no-header")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let stdout = String::from_utf8(output).unwrap();
    assert!(
        stdout.contains("function visible"),
        "visible file should be in output"
    );
    assert!(
        !stdout.contains("function secret"),
        "gitignored file should NOT be in output"
    );
}

#[test]
fn test_directory_no_ignore_includes_gitignored() {
    let temp_dir = TempDir::new().unwrap();
    init_fake_git_repo(temp_dir.path());

    fs::write(temp_dir.path().join(".gitignore"), "ignored_dir/\n").unwrap();

    fs::write(temp_dir.path().join("visible.ts"), "function visible() {}").unwrap();
    fs::create_dir_all(temp_dir.path().join("ignored_dir")).unwrap();
    fs::write(
        temp_dir.path().join("ignored_dir/secret.ts"),
        "function secret() {}",
    )
    .unwrap();

    let output = Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path())
        .arg("--no-header")
        .arg("--no-ignore")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let stdout = String::from_utf8(output).unwrap();
    assert!(
        stdout.contains("function visible"),
        "visible file should be in output"
    );
    assert!(
        stdout.contains("function secret"),
        "with --no-ignore, gitignored file SHOULD be in output"
    );
}

#[test]
fn test_directory_skips_hidden_files_by_default() {
    let temp_dir = TempDir::new().unwrap();

    fs::write(temp_dir.path().join("visible.ts"), "function visible() {}").unwrap();
    fs::write(temp_dir.path().join(".hidden.ts"), "function hidden() {}").unwrap();

    let output = Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path())
        .arg("--no-header")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let stdout = String::from_utf8(output).unwrap();
    assert!(
        stdout.contains("function visible"),
        "visible file should be in output"
    );
    assert!(
        !stdout.contains("function hidden"),
        "hidden file should NOT be in output by default"
    );
}

#[test]
fn test_directory_no_ignore_shows_hidden_files() {
    let temp_dir = TempDir::new().unwrap();

    fs::write(temp_dir.path().join("visible.ts"), "function visible() {}").unwrap();
    fs::write(temp_dir.path().join(".hidden.ts"), "function hidden() {}").unwrap();

    let output = Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path())
        .arg("--no-header")
        .arg("--no-ignore")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let stdout = String::from_utf8(output).unwrap();
    assert!(
        stdout.contains("function visible"),
        "visible file should be in output"
    );
    assert!(
        stdout.contains("function hidden"),
        "with --no-ignore, hidden file SHOULD be in output"
    );
}

#[test]
fn test_directory_gitignore_without_git_repo() {
    let temp_dir = TempDir::new().unwrap();
    // Deliberately NO .git/ directory — .gitignore should still be respected
    // because we configure require_git(false)

    fs::write(temp_dir.path().join(".gitignore"), "ignored.ts\n").unwrap();

    fs::write(temp_dir.path().join("visible.ts"), "function visible() {}").unwrap();
    fs::write(temp_dir.path().join("ignored.ts"), "function ignored() {}").unwrap();

    let output = Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path())
        .arg("--no-header")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let stdout = String::from_utf8(output).unwrap();
    assert!(
        stdout.contains("function visible"),
        "visible file should be in output"
    );
    assert!(
        !stdout.contains("function ignored"),
        ".gitignore should be respected even without .git/ directory"
    );
}

#[test]
fn test_directory_skips_hidden_directories() {
    let temp_dir = TempDir::new().unwrap();

    fs::write(temp_dir.path().join("visible.ts"), "function visible() {}").unwrap();
    fs::create_dir_all(temp_dir.path().join(".hidden_dir")).unwrap();
    fs::write(
        temp_dir.path().join(".hidden_dir/file.ts"),
        "function in_hidden_dir() {}",
    )
    .unwrap();

    let output = Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path())
        .arg("--no-header")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let stdout = String::from_utf8(output).unwrap();
    assert!(
        stdout.contains("function visible"),
        "visible file should be in output"
    );
    assert!(
        !stdout.contains("function in_hidden_dir"),
        "files in hidden directories should NOT be in output"
    );
}

#[test]
fn test_directory_nested_gitignore() {
    let temp_dir = TempDir::new().unwrap();
    init_fake_git_repo(temp_dir.path());

    // Root .gitignore ignores the "build/" directory
    fs::write(temp_dir.path().join(".gitignore"), "build/\n").unwrap();

    // Subdirectory .gitignore ignores *.generated.ts files
    fs::create_dir_all(temp_dir.path().join("src")).unwrap();
    fs::write(temp_dir.path().join("src/.gitignore"), "*.generated.ts\n").unwrap();

    // Create files that should be visible
    fs::write(temp_dir.path().join("src/app.ts"), "function app() {}").unwrap();

    // Create files that should be ignored by root .gitignore
    fs::create_dir_all(temp_dir.path().join("build")).unwrap();
    fs::write(
        temp_dir.path().join("build/bundle.ts"),
        "function bundle() {}",
    )
    .unwrap();

    // Create files that should be ignored by nested .gitignore
    fs::write(
        temp_dir.path().join("src/schema.generated.ts"),
        "function generated() {}",
    )
    .unwrap();

    let output = Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path())
        .arg("--no-header")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let stdout = String::from_utf8(output).unwrap();
    assert!(
        stdout.contains("function app"),
        "non-ignored file should be in output"
    );
    assert!(
        !stdout.contains("function bundle"),
        "file in build/ should be excluded by root .gitignore"
    );
    assert!(
        !stdout.contains("function generated"),
        "*.generated.ts should be excluded by nested src/.gitignore"
    );
}

#[test]
fn test_directory_no_ignore_hint_in_error() {
    let temp_dir = TempDir::new().unwrap();
    init_fake_git_repo(temp_dir.path());

    // Gitignore ignores all .ts files — so the directory has no processable files
    fs::write(temp_dir.path().join(".gitignore"), "*.ts\n").unwrap();
    fs::write(temp_dir.path().join("only.ts"), "function only() {}").unwrap();

    Command::cargo_bin("skim")
        .unwrap()
        .arg(temp_dir.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("No files found"))
        .stderr(predicate::str::contains("--no-ignore"));
}