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
#![allow(clippy::unwrap_used, clippy::expect_used)]

use std::net::TcpListener;
use std::process::Command;

use tempfile::TempDir;

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

#[test]
fn telemetry_status_shows_disabled_by_default() {
    let tmp = TempDir::new().unwrap();
    let output = tokf()
        .current_dir(tmp.path())
        .env("TOKF_HOME", tmp.path())
        .args(["telemetry", "status"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        stdout.contains("telemetry: disabled"),
        "expected 'telemetry: disabled' in output:\n{stdout}"
    );
}

#[test]
fn telemetry_status_shows_enabled_when_env_set() {
    let tmp = TempDir::new().unwrap();
    let output = tokf()
        .current_dir(tmp.path())
        .env("TOKF_HOME", tmp.path())
        .env("TOKF_TELEMETRY_ENABLED", "true")
        .args(["telemetry", "status"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        stdout.contains("telemetry: enabled"),
        "expected 'telemetry: enabled' in output:\n{stdout}"
    );
}

#[test]
fn telemetry_status_shows_expected_fields() {
    let tmp = TempDir::new().unwrap();
    let output = tokf()
        .current_dir(tmp.path())
        .env("TOKF_HOME", tmp.path())
        .args(["telemetry", "status"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(output.status.success());
    assert!(
        stdout.contains("endpoint:"),
        "missing endpoint field:\n{stdout}"
    );
    assert!(
        stdout.contains("protocol:"),
        "missing protocol field:\n{stdout}"
    );
    assert!(
        stdout.contains("service:"),
        "missing service field:\n{stdout}"
    );
    assert!(
        stdout.contains("pipeline:"),
        "missing pipeline field:\n{stdout}"
    );
}

#[test]
fn telemetry_status_shows_custom_endpoint() {
    let tmp = TempDir::new().unwrap();
    let output = tokf()
        .current_dir(tmp.path())
        .env("TOKF_HOME", tmp.path())
        .env(
            "OTEL_EXPORTER_OTLP_ENDPOINT",
            "http://otel.example.com:4318",
        )
        .args(["telemetry", "status"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(output.status.success());
    assert!(
        stdout.contains("http://otel.example.com:4318"),
        "expected custom endpoint in output:\n{stdout}"
    );
}

#[test]
fn telemetry_status_verbose_shows_config_and_features() {
    let tmp = TempDir::new().unwrap();
    let output = tokf()
        .current_dir(tmp.path())
        .env("TOKF_HOME", tmp.path())
        .args(["--verbose", "telemetry", "status"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(output.status.success());
    assert!(
        stdout.contains("config:"),
        "expected config path in verbose output:\n{stdout}"
    );
    assert!(
        stdout.contains("features:"),
        "expected features in verbose output:\n{stdout}"
    );
    assert!(
        stdout.contains("headers:"),
        "expected headers in verbose output:\n{stdout}"
    );
}

#[test]
fn telemetry_status_check_http_unreachable_exits_1() {
    let tmp = TempDir::new().unwrap();
    let output = tokf()
        .current_dir(tmp.path())
        .env("TOKF_HOME", tmp.path())
        // Port 1 on loopback is always closed — deterministic failure.
        .env("OTEL_EXPORTER_OTLP_ENDPOINT", "http://127.0.0.1:1")
        .args(["telemetry", "status", "--check"])
        .output()
        .unwrap();
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        !output.status.success(),
        "expected exit code 1 for unreachable endpoint, stderr:\n{stderr}"
    );
    assert!(
        stderr.contains("FAILED"),
        "expected FAILED in stderr:\n{stderr}"
    );
    assert!(
        stderr.contains("[tokf] error:"),
        "expected error detail in stderr:\n{stderr}"
    );
}

#[test]
fn telemetry_status_check_http_reachable_exits_0() {
    // Bind a TCP listener and serve a minimal HTTP response so that
    // reqwest's POST completes successfully.
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let port = listener.local_addr().unwrap().port();
    let handle = std::thread::spawn(move || {
        if let Ok((mut stream, _)) = listener.accept() {
            use std::io::{Read, Write};
            let mut buf = [0u8; 512];
            let _ = stream.read(&mut buf);
            let _ = stream.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
        }
    });

    let tmp = TempDir::new().unwrap();
    let output = tokf()
        .current_dir(tmp.path())
        .env("TOKF_HOME", tmp.path())
        .env(
            "OTEL_EXPORTER_OTLP_ENDPOINT",
            format!("http://127.0.0.1:{port}"),
        )
        .args(["telemetry", "status", "--check"])
        .output()
        .unwrap();
    let stderr = String::from_utf8_lossy(&output.stderr);
    let _ = handle.join();
    assert!(
        output.status.success(),
        "expected exit code 0 for reachable endpoint, stderr:\n{stderr}"
    );
    assert!(stderr.contains("OK"), "expected OK in stderr:\n{stderr}");
    assert!(
        stderr.contains("ms)"),
        "expected latency in stderr:\n{stderr}"
    );
}

#[test]
fn telemetry_status_check_grpc_unreachable_exits_1() {
    let tmp = TempDir::new().unwrap();
    let output = tokf()
        .current_dir(tmp.path())
        .env("TOKF_HOME", tmp.path())
        .env("OTEL_EXPORTER_OTLP_PROTOCOL", "grpc")
        // Port 1 on loopback is always closed — deterministic failure.
        .env("OTEL_EXPORTER_OTLP_ENDPOINT", "http://127.0.0.1:1")
        .args(["telemetry", "status", "--check"])
        .output()
        .unwrap();
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        !output.status.success(),
        "expected exit code 1 for unreachable gRPC endpoint, stderr:\n{stderr}"
    );
    assert!(
        stderr.contains("FAILED"),
        "expected FAILED in stderr:\n{stderr}"
    );
}

#[test]
fn telemetry_status_check_grpc_reachable_exits_0() {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let port = listener.local_addr().unwrap().port();

    let tmp = TempDir::new().unwrap();
    let output = tokf()
        .current_dir(tmp.path())
        .env("TOKF_HOME", tmp.path())
        .env("OTEL_EXPORTER_OTLP_PROTOCOL", "grpc")
        .env(
            "OTEL_EXPORTER_OTLP_ENDPOINT",
            format!("http://127.0.0.1:{port}"),
        )
        .args(["telemetry", "status", "--check"])
        .output()
        .unwrap();
    let stderr = String::from_utf8_lossy(&output.stderr);
    drop(listener);
    assert!(
        output.status.success(),
        "expected exit code 0 for reachable gRPC endpoint, stderr:\n{stderr}"
    );
    assert!(stderr.contains("OK"), "expected OK in stderr:\n{stderr}");
}

#[test]
fn telemetry_status_pipeline_from_env() {
    let tmp = TempDir::new().unwrap();
    let output = tokf()
        .current_dir(tmp.path())
        .env("TOKF_HOME", tmp.path())
        .env("TOKF_OTEL_PIPELINE", "ci-main")
        .args(["telemetry", "status"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(output.status.success());
    assert!(
        stdout.contains("ci-main"),
        "expected pipeline value in output:\n{stdout}"
    );
}

#[test]
fn telemetry_status_reads_config_file() {
    let tmp = TempDir::new().unwrap();
    std::fs::write(
        tmp.path().join("config.toml"),
        "[telemetry]\nenabled = true\nendpoint = \"http://custom:4318\"\nservice_name = \"my-svc\"\n",
    )
    .unwrap();
    let output = tokf()
        .current_dir(tmp.path())
        .env("TOKF_HOME", tmp.path())
        // Clear env vars that would override
        .env_remove("TOKF_TELEMETRY_ENABLED")
        .env_remove("OTEL_EXPORTER_OTLP_ENDPOINT")
        .args(["telemetry", "status"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(output.status.success());
    assert!(
        stdout.contains("telemetry: enabled"),
        "expected enabled from config file:\n{stdout}"
    );
    assert!(
        stdout.contains("http://custom:4318"),
        "expected custom endpoint from config file:\n{stdout}"
    );
    assert!(
        stdout.contains("my-svc"),
        "expected custom service name:\n{stdout}"
    );
}

#[test]
fn telemetry_status_verbose_redacts_headers() {
    let tmp = TempDir::new().unwrap();
    let output = tokf()
        .current_dir(tmp.path())
        .env("TOKF_HOME", tmp.path())
        .env(
            "OTEL_EXPORTER_OTLP_HEADERS",
            "x-api-key=supersecret,x-team=eng",
        )
        .args(["--verbose", "telemetry", "status"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(output.status.success());
    // Header keys should be visible but values redacted
    assert!(
        stdout.contains("<redacted>"),
        "expected <redacted> in verbose output:\n{stdout}"
    );
    // The actual secret value must NOT appear
    assert!(
        !stdout.contains("supersecret"),
        "secret value should be redacted, got:\n{stdout}"
    );
}

#[test]
fn telemetry_status_verbose_shows_no_headers_when_empty() {
    let tmp = TempDir::new().unwrap();
    let output = tokf()
        .current_dir(tmp.path())
        .env("TOKF_HOME", tmp.path())
        .env_remove("OTEL_EXPORTER_OTLP_HEADERS")
        .args(["--verbose", "telemetry", "status"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(output.status.success());
    assert!(
        stdout.contains("headers:   (none)"),
        "expected '(none)' for empty headers:\n{stdout}"
    );
}

#[test]
fn telemetry_status_shows_grpc_protocol() {
    let tmp = TempDir::new().unwrap();
    let output = tokf()
        .current_dir(tmp.path())
        .env("TOKF_HOME", tmp.path())
        .env("OTEL_EXPORTER_OTLP_PROTOCOL", "grpc")
        .args(["telemetry", "status"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(output.status.success());
    assert!(
        stdout.contains("protocol:  grpc"),
        "expected 'protocol:  grpc' in output:\n{stdout}"
    );
    // gRPC default endpoint should be :4317
    assert!(
        stdout.contains(":4317"),
        "expected gRPC default port 4317:\n{stdout}"
    );
}

#[test]
fn telemetry_status_verbose_config_not_found() {
    let tmp = TempDir::new().unwrap();
    // No config.toml created — should show "not found"
    let output = tokf()
        .current_dir(tmp.path())
        .env("TOKF_HOME", tmp.path())
        .args(["--verbose", "telemetry", "status"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(output.status.success());
    assert!(
        stdout.contains("(not found)"),
        "expected '(not found)' for missing config:\n{stdout}"
    );
}

#[test]
fn telemetry_status_verbose_config_exists() {
    let tmp = TempDir::new().unwrap();
    std::fs::write(tmp.path().join("config.toml"), "[telemetry]\n").unwrap();
    let output = tokf()
        .current_dir(tmp.path())
        .env("TOKF_HOME", tmp.path())
        .args(["--verbose", "telemetry", "status"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(output.status.success());
    // When config exists, path should be shown without "(not found)"
    assert!(
        stdout.contains("config:"),
        "expected config line:\n{stdout}"
    );
    assert!(
        stdout.contains("config.toml"),
        "expected config.toml path:\n{stdout}"
    );
    // Should NOT say "(not found)" when file exists
    let config_line = stdout
        .lines()
        .find(|l| l.starts_with("config:"))
        .unwrap_or("");
    assert!(
        !config_line.contains("(not found)"),
        "config exists but line says not found:\n{stdout}"
    );
}

#[test]
fn telemetry_status_check_stderr_shows_endpoint() {
    let tmp = TempDir::new().unwrap();
    let output = tokf()
        .current_dir(tmp.path())
        .env("TOKF_HOME", tmp.path())
        .env("OTEL_EXPORTER_OTLP_ENDPOINT", "http://127.0.0.1:1")
        .args(["telemetry", "status", "--check"])
        .output()
        .unwrap();
    let stderr = String::from_utf8_lossy(&output.stderr);
    // The "checking" line should include the endpoint
    assert!(
        stderr.contains("checking OTLP endpoint"),
        "expected 'checking OTLP endpoint' in stderr:\n{stderr}"
    );
    assert!(
        stderr.contains("127.0.0.1:1"),
        "expected endpoint address in stderr:\n{stderr}"
    );
}