tokf 0.2.33

Config-driven CLI tool that compresses command output before it reaches an LLM context
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
#![allow(clippy::unwrap_used, clippy::expect_used)]

use std::process::Command;

fn tokf() -> Command {
    Command::new(env!("CARGO_BIN_EXE_tokf"))
}

/// Helper: run `tokf rewrite` from a fresh tempdir.
/// Embedded stdlib is always available, so no filters need to be copied.
fn rewrite_with_stdlib(command: &str) -> String {
    let dir = tempfile::TempDir::new().unwrap();

    let output = tokf()
        .args(["rewrite", command])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "tokf rewrite failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    String::from_utf8_lossy(&output.stdout).trim().to_string()
}

// --- Filter-derived rewrites ---

#[test]
fn rewrite_git_status() {
    let result = rewrite_with_stdlib("git status");
    assert_eq!(result, "tokf run git status");
}

#[test]
fn rewrite_git_status_with_args() {
    let result = rewrite_with_stdlib("git status --short");
    assert_eq!(result, "tokf run git status --short");
}

#[test]
fn rewrite_cargo_test() {
    let result = rewrite_with_stdlib("cargo test");
    assert_eq!(result, "tokf run cargo test");
}

#[test]
fn rewrite_cargo_test_with_args() {
    let result = rewrite_with_stdlib("cargo test --lib");
    assert_eq!(result, "tokf run cargo test --lib");
}

#[test]
fn rewrite_git_push() {
    let result = rewrite_with_stdlib("git push");
    assert_eq!(result, "tokf run git push");
}

#[test]
fn rewrite_git_diff() {
    let result = rewrite_with_stdlib("git diff");
    assert_eq!(result, "tokf run git diff");
}

#[test]
fn rewrite_git_log() {
    let result = rewrite_with_stdlib("git log");
    assert_eq!(result, "tokf run git log");
}

#[test]
fn rewrite_git_add() {
    let result = rewrite_with_stdlib("git add");
    assert_eq!(result, "tokf run git add");
}

#[test]
fn rewrite_git_commit() {
    let result = rewrite_with_stdlib("git commit");
    assert_eq!(result, "tokf run git commit");
}

#[test]
fn rewrite_cargo_build() {
    let result = rewrite_with_stdlib("cargo build");
    assert_eq!(result, "tokf run cargo build");
}

#[test]
fn rewrite_cargo_clippy() {
    let result = rewrite_with_stdlib("cargo clippy");
    assert_eq!(result, "tokf run cargo clippy");
}

#[test]
fn rewrite_ls() {
    let result = rewrite_with_stdlib("ls -la");
    assert_eq!(result, "tokf run ls -la");
}

// --- Built-in skip patterns ---

#[test]
fn rewrite_tokf_command_unchanged() {
    let result = rewrite_with_stdlib("tokf run git status");
    assert_eq!(result, "tokf run git status");
}

#[test]
fn rewrite_heredoc_unchanged() {
    let result = rewrite_with_stdlib("cat <<EOF");
    assert_eq!(result, "cat <<EOF");
}

// --- No matching filter ---

#[test]
fn rewrite_unknown_command_passthrough() {
    let result = rewrite_with_stdlib("unknown-cmd foo bar");
    assert_eq!(result, "unknown-cmd foo bar");
}

// --- With user rewrites.toml ---

#[test]
fn rewrite_user_override() {
    let dir = tempfile::TempDir::new().unwrap();

    // Set up stdlib filters
    let filters_dir = dir.path().join(".tokf/filters");
    std::fs::create_dir_all(&filters_dir).unwrap();
    std::fs::write(
        filters_dir.join("git-status.toml"),
        "command = \"git status\"",
    )
    .unwrap();

    // Set up user rewrites.toml with a custom rule
    std::fs::write(
        dir.path().join(".tokf/rewrites.toml"),
        r#"
[[rewrite]]
match = "^git status"
replace = "custom-wrapper {0}"
"#,
    )
    .unwrap();

    let output = tokf()
        .args(["rewrite", "git status"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert_eq!(stdout.trim(), "custom-wrapper git status");
}

#[test]
fn rewrite_user_skip_pattern() {
    let dir = tempfile::TempDir::new().unwrap();

    // Set up stdlib filters
    let filters_dir = dir.path().join(".tokf/filters");
    std::fs::create_dir_all(&filters_dir).unwrap();
    std::fs::write(
        filters_dir.join("git-status.toml"),
        "command = \"git status\"",
    )
    .unwrap();

    // Set up user rewrites.toml with a skip pattern
    std::fs::write(
        dir.path().join(".tokf/rewrites.toml"),
        r#"
[skip]
patterns = ["^git status"]
"#,
    )
    .unwrap();

    let output = tokf()
        .args(["rewrite", "git status"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert_eq!(stdout.trim(), "git status");
}

// --- CommandPattern::Multiple (both patterns rewrite) ---

#[test]
fn rewrite_multiple_patterns_first_variant() {
    let dir = tempfile::TempDir::new().unwrap();
    let filters_dir = dir.path().join(".tokf/filters");
    std::fs::create_dir_all(&filters_dir).unwrap();
    std::fs::write(
        filters_dir.join("test-runner.toml"),
        r#"command = ["pnpm test", "npm test"]"#,
    )
    .unwrap();

    let output = tokf()
        .args(["rewrite", "pnpm test"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());
    assert_eq!(
        String::from_utf8_lossy(&output.stdout).trim(),
        "tokf run pnpm test"
    );
}

#[test]
fn rewrite_multiple_patterns_second_variant() {
    let dir = tempfile::TempDir::new().unwrap();
    let filters_dir = dir.path().join(".tokf/filters");
    std::fs::create_dir_all(&filters_dir).unwrap();
    std::fs::write(
        filters_dir.join("test-runner.toml"),
        r#"command = ["pnpm test", "npm test"]"#,
    )
    .unwrap();

    let output = tokf()
        .args(["rewrite", "npm test"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());
    assert_eq!(
        String::from_utf8_lossy(&output.stdout).trim(),
        "tokf run npm test"
    );
}

#[test]
fn rewrite_multiple_patterns_non_variant_passthrough() {
    let dir = tempfile::TempDir::new().unwrap();
    let filters_dir = dir.path().join(".tokf/filters");
    std::fs::create_dir_all(&filters_dir).unwrap();
    std::fs::write(
        filters_dir.join("test-runner.toml"),
        r#"command = ["pnpm test", "npm test"]"#,
    )
    .unwrap();

    let output = tokf()
        .args(["rewrite", "tokf_test_sentinel_cmd test"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());
    assert_eq!(
        String::from_utf8_lossy(&output.stdout).trim(),
        "tokf_test_sentinel_cmd test"
    );
}

// --- Wildcard pattern rewrites ---

#[test]
fn rewrite_wildcard_pattern_matches_any_subcommand() {
    let dir = tempfile::TempDir::new().unwrap();
    let filters_dir = dir.path().join(".tokf/filters");
    std::fs::create_dir_all(&filters_dir).unwrap();
    std::fs::write(filters_dir.join("npm-run.toml"), r#"command = "npm run *""#).unwrap();

    let output = tokf()
        .args(["rewrite", "npm run build"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());
    assert_eq!(
        String::from_utf8_lossy(&output.stdout).trim(),
        "tokf run npm run build"
    );
}

#[test]
fn rewrite_wildcard_pattern_no_match_without_wildcard_arg() {
    let dir = tempfile::TempDir::new().unwrap();
    let filters_dir = dir.path().join(".tokf/filters");
    std::fs::create_dir_all(&filters_dir).unwrap();
    std::fs::write(filters_dir.join("npm-run.toml"), r#"command = "npm run *""#).unwrap();

    // "npm run" without a subcommand should NOT match the wildcard pattern
    let output = tokf()
        .args(["rewrite", "npm run"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());
    assert_eq!(String::from_utf8_lossy(&output.stdout).trim(), "npm run");
}

// --- Golangci-lint disambiguation ---

#[test]
fn rewrite_golangci_lint_run_matches() {
    let dir = tempfile::TempDir::new().unwrap();
    let filters_dir = dir.path().join(".tokf/filters");
    std::fs::create_dir_all(&filters_dir).unwrap();
    std::fs::write(
        filters_dir.join("golangci-lint.toml"),
        r#"command = "golangci-lint run""#,
    )
    .unwrap();

    let output = tokf()
        .args(["rewrite", "golangci-lint run"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());
    assert_eq!(
        String::from_utf8_lossy(&output.stdout).trim(),
        "tokf run golangci-lint run"
    );
}

#[test]
fn rewrite_golangci_lint_alone_passthrough() {
    let dir = tempfile::TempDir::new().unwrap();
    let filters_dir = dir.path().join(".tokf/filters");
    std::fs::create_dir_all(&filters_dir).unwrap();
    std::fs::write(
        filters_dir.join("golangci-lint.toml"),
        r#"command = "golangci-lint run""#,
    )
    .unwrap();

    // Bare "golangci-lint" should NOT match "golangci-lint run"
    let output = tokf()
        .args(["rewrite", "golangci-lint"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());
    assert_eq!(
        String::from_utf8_lossy(&output.stdout).trim(),
        "golangci-lint"
    );
}

// --- Pipe stripping (simple pipes stripped when filter matches) ---

#[test]
fn rewrite_pipe_grep_stripped() {
    // cargo/test filter matches — pipe to grep is stripped.
    let result = rewrite_with_stdlib("cargo test | grep FAILED");
    assert_eq!(result, "tokf run --baseline-pipe 'grep FAILED' cargo test");
}

#[test]
fn rewrite_pipe_head_stripped() {
    let result = rewrite_with_stdlib("git diff HEAD | head -5");
    assert_eq!(result, "tokf run --baseline-pipe 'head -5' git diff HEAD");
}

#[test]
fn rewrite_pipe_tail_stripped() {
    let result = rewrite_with_stdlib("cargo test | tail -n 5");
    assert_eq!(result, "tokf run --baseline-pipe 'tail -n 5' cargo test");
}

#[test]
fn rewrite_pipe_grep_pattern_stripped() {
    let result = rewrite_with_stdlib("git status | grep modified");
    assert_eq!(
        result,
        "tokf run --baseline-pipe 'grep modified' git status"
    );
}

// --- Pipes that are NOT stripped ---

#[test]
fn rewrite_pipe_tail_follow_passes_through() {
    let result = rewrite_with_stdlib("cargo test | tail -f");
    assert_eq!(result, "cargo test | tail -f");
}

#[test]
fn rewrite_pipe_wc_passes_through() {
    let result = rewrite_with_stdlib("git status | wc -l");
    assert_eq!(result, "git status | wc -l");
}

#[test]
fn rewrite_pipe_no_filter_preserves_pipe() {
    let result = rewrite_with_stdlib("unknown-cmd | tail -5");
    assert_eq!(result, "unknown-cmd | tail -5");
}

#[test]
fn rewrite_multi_pipe_chain_passes_through() {
    let result = rewrite_with_stdlib("git status | grep M | wc -l");
    assert_eq!(result, "git status | grep M | wc -l");
}

#[test]
fn rewrite_multi_pipe_with_tail_passes_through() {
    let result = rewrite_with_stdlib("cargo test | grep x | tail -5");
    assert_eq!(result, "cargo test | grep x | tail -5");
}

// --- Compound + pipe ---

#[test]
fn rewrite_compound_then_tail_stripped() {
    let result = rewrite_with_stdlib("git add . && cargo test | tail -5");
    assert_eq!(
        result,
        "tokf run git add . && tokf run --baseline-pipe 'tail -5' cargo test"
    );
}

#[test]
fn rewrite_logical_or_still_rewritten_integration() {
    let result = rewrite_with_stdlib("cargo test || echo failed");
    assert_eq!(result, "tokf run cargo test || echo failed");
}

#[test]
fn rewrite_logical_or_then_pipe_stripped() {
    // Mixed: || followed by |. The compound splitter handles each segment independently.
    // Second segment "cargo test | grep ok" gets pipe stripped because cargo test has a filter.
    let result = rewrite_with_stdlib("cargo test || cargo test | grep ok");
    assert_eq!(
        result,
        "tokf run cargo test || tokf run --baseline-pipe 'grep ok' cargo test"
    );
}

#[test]
fn rewrite_quoted_pipe_not_treated_as_pipe() {
    // A pipe inside single quotes is not a shell pipe operator — the command should be rewritten.
    // git/log has a stdlib filter and the | is inside quotes so no bare pipe is detected.
    let result = rewrite_with_stdlib("git log --grep='feat|fix'");
    assert_eq!(result, "tokf run git log --grep='feat|fix'");
}

// --- Additional pipe edge cases ---

#[test]
fn rewrite_pipe_head_bytes_passes_through() {
    // head -c (byte mode) is not strippable — different semantic.
    let result = rewrite_with_stdlib("cargo test | head -c 50");
    assert_eq!(result, "cargo test | head -c 50");
}

#[test]
fn rewrite_compound_non_strippable_pipe_passes_through() {
    // First segment rewritten, second has a non-strippable pipe (wc) — preserved.
    let result = rewrite_with_stdlib("git add . && cargo test | wc -l");
    assert_eq!(result, "tokf run git add . && cargo test | wc -l");
}

#[test]
fn rewrite_compound_pipe_no_filter_preserves_pipe() {
    // First segment rewritten, second has a strippable pipe but no filter — preserved.
    let result = rewrite_with_stdlib("git add . && unknown-cmd | tail -5");
    assert_eq!(result, "tokf run git add . && unknown-cmd | tail -5");
}

#[test]
fn rewrite_semicolon_compound() {
    let result = rewrite_with_stdlib("git add .; git status");
    assert_eq!(result, "tokf run git add .; tokf run git status");
}

// --- Quoted grep patterns with baseline-pipe escaping ---

#[test]
fn rewrite_pipe_grep_quoted_pattern_escaped() {
    // Single quotes in grep suffix are escaped in the --baseline-pipe value.
    let result = rewrite_with_stdlib("cargo test | grep -E 'fail|error'");
    assert_eq!(
        result,
        "tokf run --baseline-pipe 'grep -E '\\''fail|error'\\''' cargo test"
    );
}

// --- Variant filter integration tests ---

#[test]
fn rewrite_variant_parent_rewrites_npm_test() {
    // npm/test.toml from stdlib should match "npm test"
    let result = rewrite_with_stdlib("npm test");
    assert_eq!(result, "tokf run npm test");
}

#[test]
fn rewrite_variant_parent_rewrites_pnpm_test() {
    let result = rewrite_with_stdlib("pnpm test");
    assert_eq!(result, "tokf run pnpm test");
}

#[test]
fn rewrite_variant_parent_rewrites_yarn_test() {
    let result = rewrite_with_stdlib("yarn test");
    assert_eq!(result, "tokf run yarn test");
}

#[test]
fn which_shows_variant_info() {
    let dir = tempfile::TempDir::new().unwrap();

    let output = tokf()
        .args(["which", "npm test"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    // Should mention variants since npm/test.toml has them
    assert!(
        stdout.contains("variant") || stdout.contains("npm/test"),
        "expected variant info or filter name, got: {stdout}"
    );
}

#[test]
fn which_variant_file_match() {
    let dir = tempfile::TempDir::new().unwrap();
    // Create a vitest config file so the variant resolves
    std::fs::write(dir.path().join("vitest.config.ts"), "").unwrap();

    let output = tokf()
        .args(["which", "npm test", "--verbose"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    let combined = format!("{stdout}{stderr}");
    // Should show variant resolution to vitest
    assert!(
        combined.contains("vitest") || combined.contains("variant"),
        "expected vitest variant resolution, got stdout={stdout}, stderr={stderr}"
    );
}

// --- Built-in wrapper rewrites ---

#[test]
fn rewrite_make_check() {
    let r = rewrite_with_stdlib("make check");
    assert_eq!(r, "make SHELL=tokf check");
}

#[test]
fn rewrite_make_bare() {
    let r = rewrite_with_stdlib("make");
    assert_eq!(r, "make SHELL=tokf");
}

#[test]
fn rewrite_just_test() {
    let r = rewrite_with_stdlib("just test");
    assert_eq!(r, "just --shell tokf --shell-arg -cu test");
}

#[test]
fn rewrite_just_bare() {
    let r = rewrite_with_stdlib("just");
    assert_eq!(r, "just --shell tokf --shell-arg -cu");
}

#[test]
fn rewrite_cmake_not_wrapper() {
    let r = rewrite_with_stdlib("cmake --build .");
    // cmake should NOT match the make wrapper.
    assert_eq!(r, "cmake --build .");
}

#[test]
fn rewrite_make_compound_with_git() {
    let r = rewrite_with_stdlib("make check && git status");
    assert_eq!(r, "make SHELL=tokf check && tokf run git status");
}

// --- Exit code ---

#[test]
fn rewrite_always_exits_zero() {
    let output = tokf().args(["rewrite", "anything"]).output().unwrap();
    assert!(output.status.success());
    assert_eq!(output.status.code(), Some(0));
}