lowfat-runner 0.3.9

Plugin runners (process, future: WASM) for lowfat
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
//! End-to-end tests for shell plugins.
//!
//! Each test creates a real filter.sh script in a temp directory,
//! builds a ProcessFilter, and runs it with realistic FilterInput
//! to verify the full stdin→env→script→stdout pipeline.

use lowfat_core::level::Level;
use lowfat_plugin::plugin::{FilterInput, FilterPlugin, PluginInfo};
use lowfat_runner::process::ProcessFilter;
use std::io::Write;
use std::path::PathBuf;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn temp_plugin(name: &str, script: &str) -> (ProcessFilter, PathBuf) {
    let dir = std::env::temp_dir().join(format!(
        "lowfat-e2e-{name}-{}",
        std::process::id()
    ));
    std::fs::create_dir_all(&dir).unwrap();
    let path = dir.join("filter.sh");
    let mut f = std::fs::File::create(&path).unwrap();
    f.write_all(script.as_bytes()).unwrap();

    let filter = ProcessFilter {
        info: PluginInfo {
            name: format!("{name}-compact"),
            version: "0.1.0".into(),
            commands: vec![name.into()],
            subcommands: vec![],
        },
        entry: path,
        base_dir: dir.clone(),
    };
    (filter, dir)
}

fn make_input(
    raw: &str,
    command: &str,
    subcommand: &str,
    args: Vec<&str>,
    level: Level,
    exit_code: i32,
) -> FilterInput {
    FilterInput {
        raw: raw.to_string(),
        command: command.to_string(),
        subcommand: subcommand.to_string(),
        args: args.into_iter().map(String::from).collect(),
        level,
        head_limit: level.head_limit(40),
        exit_code,
    }
}

// ---------------------------------------------------------------------------
// LOWFAT_ARGS — verify the full arg string reaches the script
// ---------------------------------------------------------------------------

#[test]
fn args_env_contains_full_argument_string() {
    let script = r#"#!/bin/sh
echo "ARGS=$LOWFAT_ARGS"
"#;
    let (filter, _dir) = temp_plugin("argtest", script);
    let input = make_input(
        "ignored",
        "kubectl",
        "get",
        vec!["get", "pods", "-n", "kube-system", "-o", "wide"],
        Level::Full,
        0,
    );
    let result = filter.filter(&input).unwrap();
    assert!(
        result.text.contains("ARGS=get pods -n kube-system -o wide"),
        "expected full args, got: {}",
        result.text
    );
}

#[test]
fn args_env_empty_when_no_args() {
    let script = r#"#!/bin/sh
echo "ARGS=[$LOWFAT_ARGS]"
"#;
    let (filter, _dir) = temp_plugin("noargs", script);
    let input = make_input("ignored", "git", "", vec![], Level::Full, 0);
    let result = filter.filter(&input).unwrap();
    assert!(
        result.text.contains("ARGS=[]"),
        "expected empty args, got: {}",
        result.text
    );
}

// ---------------------------------------------------------------------------
// Realistic kubectl plugin — resource-type-aware filtering via $LOWFAT_ARGS
// ---------------------------------------------------------------------------

const KUBECTL_SCRIPT: &str = r#"#!/bin/sh
RAW=$(cat)
LEVEL="${LOWFAT_LEVEL:-full}"
SUB="${LOWFAT_SUBCOMMAND}"
ARGS="${LOWFAT_ARGS}"

# Extract resource type from args
RESOURCE=""
for arg in $ARGS; do
  case "$arg" in
    "$SUB") continue ;;
    -*) continue ;;
    *) RESOURCE="$arg"; break ;;
  esac
done

# Passthrough structured output
case "$ARGS" in
  *"-o json"*|*"-o yaml"*)
    echo "$RAW"
    exit 0
    ;;
esac

case "$SUB" in
  get)
    case "$RESOURCE" in
      pods|po)
        if [ "$LEVEL" = "ultra" ]; then
          echo "$RAW" | awk 'NR==1 || !/Running/' | head -n 15
        else
          echo "$RAW" | head -n 30
        fi
        ;;
      events|ev)
        if [ "$LEVEL" = "ultra" ]; then
          echo "$RAW" | awk 'NR==1 || /Warning/' | head -n 15
        else
          echo "$RAW" | tail -n 30
        fi
        ;;
      *)
        echo "$RAW" | head -n 30
        ;;
    esac
    ;;
  *)
    echo "$RAW" | head -n 30
    ;;
esac
"#;

const KUBECTL_PODS_OUTPUT: &str = "\
NAME                    READY   STATUS    RESTARTS   AGE
nginx-abc123            1/1     Running   0          5d
redis-def456            1/1     Running   0          3d
crash-ghi789            0/1     CrashLoopBackOff   5   1h
pending-jkl012          0/1     Pending   0          30m
api-mno345              1/1     Running   0          7d";

const KUBECTL_EVENTS_OUTPUT: &str = "\
LAST SEEN   TYPE      REASON    OBJECT              MESSAGE
5m          Normal    Scheduled pod/nginx-abc123    Successfully assigned
4m          Normal    Pulled    pod/nginx-abc123    Container image pulled
3m          Warning   BackOff   pod/crash-ghi789    Back-off restarting
2m          Warning   Failed    pod/crash-ghi789    Error: CrashLoopBackOff
1m          Normal    Created   pod/api-mno345      Created container";

#[test]
fn kubectl_get_pods_ultra_filters_running() {
    let (filter, _dir) = temp_plugin("kubectl", KUBECTL_SCRIPT);
    let input = make_input(
        KUBECTL_PODS_OUTPUT,
        "kubectl",
        "get",
        vec!["get", "pods"],
        Level::Ultra,
        0,
    );
    let result = filter.filter(&input).unwrap();

    // Header should be present
    assert!(result.text.contains("NAME"), "header missing");
    // Non-Running pods should be present
    assert!(result.text.contains("CrashLoopBackOff"), "CrashLoop pod missing");
    assert!(result.text.contains("Pending"), "Pending pod missing");
    // Running pods should be filtered out
    assert!(!result.text.contains("nginx-abc123"), "Running pod should be filtered");
    assert!(!result.text.contains("redis-def456"), "Running pod should be filtered");
}

#[test]
fn kubectl_get_pods_full_keeps_all() {
    let (filter, _dir) = temp_plugin("kubectl-full", KUBECTL_SCRIPT);
    let input = make_input(
        KUBECTL_PODS_OUTPUT,
        "kubectl",
        "get",
        vec!["get", "pods"],
        Level::Full,
        0,
    );
    let result = filter.filter(&input).unwrap();

    // Full level keeps everything
    assert!(result.text.contains("nginx-abc123"), "all pods should be present at full");
    assert!(result.text.contains("CrashLoopBackOff"), "all pods should be present at full");
}

#[test]
fn kubectl_get_events_ultra_warnings_only() {
    let (filter, _dir) = temp_plugin("kubectl-events", KUBECTL_SCRIPT);
    let input = make_input(
        KUBECTL_EVENTS_OUTPUT,
        "kubectl",
        "get",
        vec!["get", "events"],
        Level::Ultra,
        0,
    );
    let result = filter.filter(&input).unwrap();

    // Header + Warning events only
    assert!(result.text.contains("LAST SEEN"), "header missing");
    assert!(result.text.contains("BackOff"), "Warning event missing");
    assert!(result.text.contains("Failed"), "Warning event missing");
    // Normal events should be filtered
    assert!(!result.text.contains("Scheduled"), "Normal event should be filtered");
    assert!(!result.text.contains("Pulled"), "Normal event should be filtered");
}

#[test]
fn kubectl_get_pods_json_passthrough() {
    let json_output = r#"{"items": [{"metadata": {"name": "nginx"}}]}"#;
    let (filter, _dir) = temp_plugin("kubectl-json", KUBECTL_SCRIPT);
    let input = make_input(
        json_output,
        "kubectl",
        "get",
        vec!["get", "pods", "-o", "json"],
        Level::Ultra,
        0,
    );
    let result = filter.filter(&input).unwrap();

    // -o json should passthrough unmodified
    assert!(
        result.text.contains(r#""items""#),
        "JSON should pass through, got: {}",
        result.text
    );
}

#[test]
fn kubectl_get_pods_yaml_passthrough() {
    let yaml_output = "apiVersion: v1\nkind: Pod\nmetadata:\n  name: nginx\n";
    let (filter, _dir) = temp_plugin("kubectl-yaml", KUBECTL_SCRIPT);
    let input = make_input(
        yaml_output,
        "kubectl",
        "get",
        vec!["get", "pods", "-o", "yaml"],
        Level::Ultra,
        0,
    );
    let result = filter.filter(&input).unwrap();
    assert!(
        result.text.contains("apiVersion"),
        "YAML should pass through, got: {}",
        result.text
    );
}

#[test]
fn kubectl_get_pods_with_namespace_flag() {
    // "get pods -n kube-system" — resource is "pods", not "-n"
    let (filter, _dir) = temp_plugin("kubectl-ns", KUBECTL_SCRIPT);
    let input = make_input(
        KUBECTL_PODS_OUTPUT,
        "kubectl",
        "get",
        vec!["get", "pods", "-n", "kube-system"],
        Level::Ultra,
        0,
    );
    let result = filter.filter(&input).unwrap();

    // Should correctly parse "pods" as resource despite -n flag
    assert!(result.text.contains("NAME"), "header missing — resource type not parsed correctly");
    assert!(result.text.contains("CrashLoopBackOff"), "should use pods-specific filter");
    assert!(!result.text.contains("nginx-abc123"), "Running pods should be filtered in ultra");
}

// ---------------------------------------------------------------------------
// Realistic cargo plugin — verifies bundled plugin works end-to-end
// ---------------------------------------------------------------------------

const CARGO_SCRIPT: &str = r#"#!/bin/sh
RAW=$(cat)
LEVEL="${LOWFAT_LEVEL:-full}"
SUB="${LOWFAT_SUBCOMMAND}"

case "$SUB" in
  build|check)
    if [ "$LEVEL" = "ultra" ]; then
      ISSUES=$(echo "$RAW" | grep -E '^(error|warning)\b' | head -n 15)
      if [ -z "$ISSUES" ]; then
        echo "cargo $SUB: ok"
      else
        echo "$ISSUES"
      fi
    else
      LIMIT=$( [ "$LEVEL" = "lite" ] && echo 60 || echo 30 )
      echo "$RAW" | grep -vE '^\s*(Compiling|Downloading|Checking|Blocking|Updating|Locking) ' | head -n "$LIMIT"
    fi
    ;;
  test)
    if [ "$LEVEL" = "ultra" ]; then
      echo "$RAW" | grep -E '^(test result:|failures:|test .+ FAILED|     Running|FAILED)' | head -n 15
    else
      LIMIT=$( [ "$LEVEL" = "lite" ] && echo 60 || echo 30 )
      echo "$RAW" | grep -vE '^\s*(Compiling|Downloading|Checking|Blocking|Updating|Locking) |\.\.\.+ ok$' | head -n "$LIMIT"
    fi
    ;;
  *)
    echo "$RAW" | head -n 30
    ;;
esac
"#;

const CARGO_BUILD_OUTPUT: &str = "\
   Compiling serde v1.0.200
   Compiling tokio v1.37.0
   Compiling myapp v0.1.0 (/home/user/myapp)
warning: unused variable: `x`
  --> src/main.rs:10:9
   |
10 |     let x = 42;
   |         ^ help: if this is intentional, prefix it with an underscore: `_x`
   |
   = note: `#[warn(unused_variables)]` on by default

    Finished `dev` profile [unoptimized + debuginfo] target(s) in 5.32s";

const CARGO_TEST_OUTPUT: &str = "\
   Compiling myapp v0.1.0
     Running unittests src/lib.rs (target/debug/deps/myapp-abc123)

running 5 tests
test tests::basic_add ... ok
test tests::basic_sub ... ok
test tests::edge_case ... ok
test tests::overflow_check ... FAILED
test tests::negative_check ... ok

failures:

---- tests::overflow_check stdout ----
thread 'tests::overflow_check' panicked at 'assertion failed'

failures:
    tests::overflow_check

test result: FAILED. 4 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out";

#[test]
fn cargo_build_ultra_shows_warnings_only() {
    let (filter, _dir) = temp_plugin("cargo-build", CARGO_SCRIPT);
    let input = make_input(
        CARGO_BUILD_OUTPUT,
        "cargo",
        "build",
        vec!["build"],
        Level::Ultra,
        0,
    );
    let result = filter.filter(&input).unwrap();

    assert!(result.text.contains("warning: unused variable"), "warning line missing");
    assert!(!result.text.contains("Compiling serde"), "Compiling noise should be stripped");
    assert!(!result.text.contains("Finished"), "Finished line should be stripped");
}

#[test]
fn cargo_build_ultra_clean_shows_ok() {
    let clean_output = "\
   Compiling myapp v0.1.0
    Finished `dev` profile in 2.0s";
    let (filter, _dir) = temp_plugin("cargo-clean", CARGO_SCRIPT);
    let input = make_input(clean_output, "cargo", "build", vec!["build"], Level::Ultra, 0);
    let result = filter.filter(&input).unwrap();

    assert!(
        result.text.contains("cargo build: ok"),
        "clean build should show ok, got: {}",
        result.text
    );
}

#[test]
fn cargo_build_full_strips_compiling_noise() {
    let (filter, _dir) = temp_plugin("cargo-full", CARGO_SCRIPT);
    let input = make_input(
        CARGO_BUILD_OUTPUT,
        "cargo",
        "build",
        vec!["build"],
        Level::Full,
        0,
    );
    let result = filter.filter(&input).unwrap();

    assert!(!result.text.contains("Compiling serde"), "Compiling noise should be stripped");
    assert!(result.text.contains("warning: unused variable"), "warnings should remain");
    assert!(result.text.contains("Finished"), "Finished line should remain");
}

#[test]
fn cargo_test_ultra_shows_failures_only() {
    let (filter, _dir) = temp_plugin("cargo-test", CARGO_SCRIPT);
    let input = make_input(
        CARGO_TEST_OUTPUT,
        "cargo",
        "test",
        vec!["test"],
        Level::Ultra,
        101,
    );
    let result = filter.filter(&input).unwrap();

    assert!(result.text.contains("test result: FAILED"), "result summary missing");
    assert!(result.text.contains("overflow_check"), "failed test name missing");
    // Individual ok tests should not appear
    assert!(!result.text.contains("basic_add"), "passing tests should be stripped");
}

#[test]
fn cargo_test_full_strips_ok_tests() {
    let (filter, _dir) = temp_plugin("cargo-test-full", CARGO_SCRIPT);
    let input = make_input(
        CARGO_TEST_OUTPUT,
        "cargo",
        "test",
        vec!["test"],
        Level::Full,
        101,
    );
    let result = filter.filter(&input).unwrap();

    // Compiling noise stripped
    assert!(!result.text.contains("Compiling myapp"), "Compiling should be stripped");
    // Failures and result should remain
    assert!(result.text.contains("FAILED"), "FAILED should remain");
    assert!(result.text.contains("test result:"), "result summary should remain");
}

// ---------------------------------------------------------------------------
// Pipeline e2e — shell plugin in a full pipeline with builtins
// ---------------------------------------------------------------------------

#[test]
fn pipeline_with_shell_plugin_and_builtins() {
    use lowfat_core::pipeline::Pipeline;
    use lowfat_runner::runner::execute_pipeline;
    use std::collections::HashMap;

    // A simple plugin that uppercases everything
    let upper_script = "#!/bin/sh\ncat | tr '[:lower:]' '[:upper:]'";
    let (filter, _dir) = temp_plugin("upper", upper_script);

    let pipeline = Pipeline::parse("strip-ansi | upper-compact | head");
    let raw = "\x1b[31mhello world\x1b[0m\nsecond line\nthird line";

    let input = make_input(raw, "test", "", vec![], Level::Full, 0);

    let mut plugin_map: HashMap<String, Box<dyn FilterPlugin>> = HashMap::new();
    plugin_map.insert("upper-compact".to_string(), Box::new(filter));

    let result = execute_pipeline(&pipeline, raw, &input, &plugin_map).unwrap();

    // strip-ansi removes ANSI, then upper-compact uppercases
    assert!(result.contains("HELLO WORLD"), "should be uppercased: {}", result);
    assert!(!result.contains("\x1b["), "ANSI should be stripped");
}

// ---------------------------------------------------------------------------
// Level propagation — verify all three levels reach the script correctly
// ---------------------------------------------------------------------------

#[test]
fn all_levels_propagated() {
    let script = r#"#!/bin/sh
echo "level=$LOWFAT_LEVEL"
"#;
    let (filter, _dir) = temp_plugin("leveltest", script);

    for (level, expected) in [
        (Level::Lite, "level=lite"),
        (Level::Full, "level=full"),
        (Level::Ultra, "level=ultra"),
    ] {
        let input = make_input("ignored", "test", "", vec![], level, 0);
        let result = filter.filter(&input).unwrap();
        assert!(
            result.text.contains(expected),
            "level {:?}: expected '{}', got '{}'",
            level,
            expected,
            result.text.trim()
        );
    }
}

// ---------------------------------------------------------------------------
// Exit code propagation
// ---------------------------------------------------------------------------

#[test]
fn exit_code_reaches_script() {
    let script = r#"#!/bin/sh
RAW=$(cat)
EXIT="$LOWFAT_EXIT_CODE"
if [ "$EXIT" != "0" ]; then
  echo "ERROR (exit $EXIT):"
  echo "$RAW"
else
  echo "$RAW" | head -n 5
fi
"#;
    let (filter, _dir) = temp_plugin("exitcode", script);

    // Success case
    let input = make_input("all good", "test", "", vec![], Level::Full, 0);
    let result = filter.filter(&input).unwrap();
    assert!(!result.text.contains("ERROR"), "should not show error on exit 0");

    // Failure case
    let input = make_input("something broke", "test", "", vec![], Level::Full, 1);
    let result = filter.filter(&input).unwrap();
    assert!(result.text.contains("ERROR (exit 1)"), "should show error on exit 1");
    assert!(result.text.contains("something broke"), "should preserve raw on error");
}