fallow-cli 2.47.0

CLI for fallow, Rust-native codebase intelligence for TypeScript and JavaScript
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
//! End-to-end integration tests for `fallow health --production-coverage`.
//!
//! Exercises the full CLI → sidecar pipeline with a signed stub sidecar:
//! discovery via `FALLOW_COV_BIN`, Ed25519 signature verification, Request
//! marshalling over stdin, Response parsing, protocol-version check, and the
//! 3 / 4 / 5 / 6 exit-code matrix. Pairs with the source-level
//! network-prohibition assertion in
//! `crates/cli/src/health/coverage.rs::tests::production_coverage_module_has_no_network_code`
//! to cover the Phase 2 step 4 roadmap gate:
//!
//! > integration test asserting zero network calls during analysis.
//!
//! Gated behind the `test-sidecar-key` cargo feature: the feature swaps both
//! the sidecar binary-signing pubkey and the license JWT pubkey for
//! deterministic test keypairs, and activates the `stub_sidecar` bin target.
//! A `compile_error!` in `coverage.rs` blocks the feature from release builds.
//!
//! Run: `cargo test -p fallow-cli --features test-sidecar-key production_coverage`.

#[path = "common/mod.rs"]
mod common;

#[cfg(feature = "test-sidecar-key")]
#[path = "common/sign.rs"]
mod sign;

#[cfg(feature = "test-sidecar-key")]
mod gated {
    use std::fs;
    use std::path::{Path, PathBuf};
    use std::process::Command;

    use tempfile::TempDir;

    use super::common::{fallow_bin, fixture_path, parse_json};
    use super::sign;

    struct Harness {
        _tmp: TempDir,
        home: PathBuf,
        coverage_file: PathBuf,
        stub_bin: PathBuf,
    }

    impl Harness {
        fn new() -> Self {
            let tmp = tempfile::tempdir().expect("create temp dir");
            let root = tmp.path();

            let home = root.join("home");
            fs::create_dir_all(&home).expect("create fake home");

            // A minimal V8-shaped coverage file so the CLI accepts the input
            // and the shape classification picks V8 (not Istanbul). The stub
            // does not read the content, so an empty result array suffices.
            let coverage_file = root.join("coverage-final-v8.json");
            fs::write(&coverage_file, br#"{"result":[]}"#).expect("write coverage input");

            let stub_bin = copy_and_sign_stub(root);

            Self {
                _tmp: tmp,
                home,
                coverage_file,
                stub_bin,
            }
        }

        fn fallow(&self) -> Command {
            let mut cmd = Command::new(fallow_bin());
            cmd.env("NO_COLOR", "1");
            cmd.env("RUST_LOG", "");
            // Remove any inherited license material so the developer's real
            // license cannot leak into tests. Each test case that needs a
            // license sets `FALLOW_LICENSE` explicitly.
            cmd.env_remove("FALLOW_LICENSE");
            cmd.env_remove("FALLOW_LICENSE_PATH");
            // Same for the alternative sidecar override.
            cmd.env_remove("FALLOW_COV_BINARY_PATH");
            // And for unrelated fallow env vars that could leak in from the
            // developer's shell and perturb analysis (FALLOW_COVERAGE feeds
            // CRAP scoring; FALLOW_BIN overrides the binary MCP looks up).
            cmd.env_remove("FALLOW_COVERAGE");
            cmd.env_remove("FALLOW_BIN");
            cmd.env_remove("FALLOW_FORMAT");
            cmd.env_remove("FALLOW_QUIET");
            // Point HOME at a fresh directory so discovery of the default
            // license path (`~/.fallow/license.jwt`) cannot pick up a real
            // license from the developer's machine.
            cmd.env("HOME", &self.home);
            cmd.env("USERPROFILE", &self.home);
            // Explicit override takes precedence over the auto-discovery
            // ladder, so the stub is the one and only sidecar the CLI can
            // see during each test case.
            cmd.env("FALLOW_COV_BIN", &self.stub_bin);
            cmd
        }

        fn health_args(&self) -> Vec<String> {
            self.health_args_with_format("json")
        }

        fn health_args_with_format(&self, format: &str) -> Vec<String> {
            let fixture = fixture_path("coverage-gaps");
            vec![
                "health".to_owned(),
                "--root".to_owned(),
                fixture.to_string_lossy().into_owned(),
                "--production-coverage".to_owned(),
                self.coverage_file.to_string_lossy().into_owned(),
                "--format".to_owned(),
                format.to_owned(),
                "--quiet".to_owned(),
            ]
        }
    }

    fn copy_and_sign_stub(root: &Path) -> PathBuf {
        let source = PathBuf::from(env!("CARGO_BIN_EXE_stub_sidecar"));
        let target_name = if cfg!(windows) {
            "fallow-cov.exe"
        } else {
            "fallow-cov"
        };
        let target = root.join(target_name);
        fs::copy(&source, &target).expect("copy stub sidecar");

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mut perms = fs::metadata(&target).expect("stat stub").permissions();
            perms.set_mode(0o755);
            fs::set_permissions(&target, perms).expect("chmod stub");
        }

        sign::sign_sidecar_binary(&target);
        target
    }

    fn run_with(mut cmd: Command) -> (String, String, i32) {
        let output = cmd.output().expect("run fallow binary");
        (
            String::from_utf8_lossy(&output.stdout).into_owned(),
            String::from_utf8_lossy(&output.stderr).into_owned(),
            output.status.code().unwrap_or(-1),
        )
    }

    #[test]
    fn license_missing_exits_3() {
        let harness = Harness::new();
        let mut cmd = harness.fallow();
        // No FALLOW_LICENSE, no file at ~/.fallow/license.jwt under the
        // sandboxed HOME — the license layer should short-circuit before
        // the sidecar is ever spawned.
        for arg in harness.health_args() {
            cmd.arg(arg);
        }
        let (stdout, stderr, code) = run_with(cmd);
        assert_eq!(
            code, 3,
            "missing license must exit 3; stdout={stdout}, stderr={stderr}"
        );
    }

    #[test]
    fn license_expired_hard_fail_exits_3() {
        let harness = Harness::new();
        let mut cmd = harness.fallow();
        cmd.env(
            "FALLOW_LICENSE",
            sign::mint_expired_production_coverage_jwt(),
        );
        for arg in harness.health_args() {
            cmd.arg(arg);
        }
        let (stdout, stderr, code) = run_with(cmd);
        assert_eq!(
            code, 3,
            "expired hard-fail license must exit 3; stdout={stdout}, stderr={stderr}"
        );
    }

    #[test]
    fn happy_path_exits_0_and_renders_production_coverage() {
        let harness = Harness::new();
        let mut cmd = harness.fallow();
        cmd.env("FALLOW_LICENSE", sign::mint_production_coverage_jwt());
        cmd.env("FALLOW_STUB_MODE", "ok");
        for arg in harness.health_args() {
            cmd.arg(arg);
        }
        let (stdout, stderr, code) = run_with(cmd);
        assert_eq!(
            code,
            0,
            "happy path must exit 0; stderr={stderr}; stdout head={}",
            &stdout.chars().take(400).collect::<String>()
        );
        let json: serde_json::Value = serde_json::from_str(&stdout).unwrap_or_else(|err| {
            panic!(
                "expected JSON output; err={err}; stdout head={}",
                &stdout.chars().take(400).collect::<String>()
            )
        });
        assert!(
            json.get("production_coverage").is_some(),
            "production_coverage key missing from JSON output; keys={:?}",
            json.as_object().map(|o| o.keys().collect::<Vec<_>>())
        );
    }

    #[test]
    fn sidecar_missing_exits_4() {
        let harness = Harness::new();
        let mut cmd = harness.fallow();
        cmd.env("FALLOW_LICENSE", sign::mint_production_coverage_jwt());
        // Deliberately point at a path that does not exist so discovery
        // hits the explicit-beats-implicit bailout in discover_sidecar.
        cmd.env("FALLOW_COV_BIN", harness.home.join("does-not-exist"));
        for arg in harness.health_args() {
            cmd.arg(arg);
        }
        let (stdout, stderr, code) = run_with(cmd);
        assert_eq!(
            code, 4,
            "missing sidecar must exit 4; stdout={stdout}; stderr={stderr}"
        );
        let combined = format!("{stdout}{stderr}");
        assert!(
            combined.contains("FALLOW_COV_BIN"),
            "error message should mention FALLOW_COV_BIN; got:\n{combined}"
        );
    }

    #[test]
    fn sidecar_exits_4_marshalled_to_cli_exit_4() {
        exit_code_case("exit-4", 4);
    }

    #[test]
    fn sidecar_exits_5_marshalled_to_cli_exit_5() {
        exit_code_case("exit-5", 5);
    }

    #[test]
    fn sidecar_exits_6_marshalled_to_cli_exit_6() {
        exit_code_case("exit-6", 6);
    }

    #[test]
    fn sidecar_protocol_mismatch_exits_4() {
        let harness = Harness::new();
        let mut cmd = harness.fallow();
        cmd.env("FALLOW_LICENSE", sign::mint_production_coverage_jwt());
        cmd.env("FALLOW_STUB_MODE", "protocol-mismatch");
        for arg in harness.health_args() {
            cmd.arg(arg);
        }
        let (stdout, stderr, code) = run_with(cmd);
        assert_eq!(
            code, 4,
            "protocol-version mismatch must exit 4; stdout={stdout}; stderr={stderr}"
        );
    }

    #[test]
    fn malformed_sidecar_stdout_exits_4() {
        let harness = Harness::new();
        let mut cmd = harness.fallow();
        cmd.env("FALLOW_LICENSE", sign::mint_production_coverage_jwt());
        cmd.env("FALLOW_STUB_MODE", "malformed-stdout");
        for arg in harness.health_args() {
            cmd.arg(arg);
        }
        let (stdout, stderr, code) = run_with(cmd);
        assert_eq!(
            code, 4,
            "non-JSON sidecar stdout must exit 4; stdout={stdout}; stderr={stderr}"
        );
    }

    #[test]
    fn bad_sidecar_signature_exits_4() {
        let harness = Harness::new();
        // Corrupt the .sig file with zeros; Ed25519 rejects this.
        let mut sig_os = harness.stub_bin.as_os_str().to_os_string();
        sig_os.push(".sig");
        let sig_path = PathBuf::from(sig_os);
        fs::write(&sig_path, [0u8; 64]).expect("overwrite signature");

        let mut cmd = harness.fallow();
        cmd.env("FALLOW_LICENSE", sign::mint_production_coverage_jwt());
        cmd.env("FALLOW_STUB_MODE", "ok");
        for arg in harness.health_args() {
            cmd.arg(arg);
        }
        let (stdout, stderr, code) = run_with(cmd);
        assert_eq!(
            code, 4,
            "bad signature must exit 4; stdout={stdout}; stderr={stderr}"
        );
    }

    /// Happy path + JSON inspection sanity check. Re-uses the harness but
    /// goes a little further than the headline test: the license watermark
    /// field should be absent for a fresh (non-expired) JWT.
    #[test]
    fn happy_path_does_not_set_watermark() {
        let harness = Harness::new();
        let mut cmd = harness.fallow();
        cmd.env("FALLOW_LICENSE", sign::mint_production_coverage_jwt());
        cmd.env("FALLOW_STUB_MODE", "ok");
        for arg in harness.health_args() {
            cmd.arg(arg);
        }
        let (stdout, _stderr, code) = run_with(cmd);
        assert_eq!(code, 0);
        let json = parse_json(&super::common::CommandOutput {
            stdout,
            stderr: String::new(),
            code,
        });
        let coverage = json
            .get("production_coverage")
            .expect("production_coverage present");
        let watermark = coverage.get("watermark");
        assert!(
            watermark.is_none_or(serde_json::Value::is_null),
            "fresh license must not emit a watermark; got {watermark:?}"
        );
    }

    /// ADR 009 step 6b: a short-window capture must show both the warning
    /// banner and the quantified trial CTA in human output. The stub returns
    /// a 12-minute capture with `lazy_parse_warning: true`.
    #[test]
    fn capture_quality_short_renders_warning_and_upgrade_prompt_in_human_output() {
        let harness = Harness::new();
        let mut cmd = harness.fallow();
        cmd.env("FALLOW_LICENSE", sign::mint_production_coverage_jwt());
        cmd.env("FALLOW_STUB_MODE", "capture-quality-short");
        for arg in harness.health_args_with_format("human") {
            cmd.arg(arg);
        }
        let (stdout, stderr, code) = run_with(cmd);
        let combined = format!("{stdout}{stderr}");
        assert_eq!(
            code, 0,
            "short-capture happy path must exit 0; combined={combined}"
        );
        assert!(
            combined.contains("note: short capture (12 min from 1 instance)"),
            "short-capture warning banner missing; combined={combined}"
        );
        assert!(
            combined.contains("lazy-parsed scripts may not appear"),
            "lazy-parse guidance missing; combined={combined}"
        );
        assert!(
            combined.contains("captured 12 min from 1 instance."),
            "quantified upgrade prompt header missing; combined={combined}"
        );
        assert!(
            combined.contains("continuous monitoring over 30 days"),
            "upgrade prompt body missing; combined={combined}"
        );
        assert!(
            combined.contains("fallow license activate --trial"),
            "trial CTA command missing; combined={combined}"
        );
    }

    /// ADR 009 step 6b: a long-window capture must be quiet. No warning, no CTA.
    #[test]
    fn capture_quality_long_shows_neither_warning_nor_upgrade_prompt() {
        let harness = Harness::new();
        let mut cmd = harness.fallow();
        cmd.env("FALLOW_LICENSE", sign::mint_production_coverage_jwt());
        cmd.env("FALLOW_STUB_MODE", "capture-quality-long");
        for arg in harness.health_args_with_format("human") {
            cmd.arg(arg);
        }
        let (stdout, stderr, code) = run_with(cmd);
        let combined = format!("{stdout}{stderr}");
        assert_eq!(
            code, 0,
            "long-capture happy path must exit 0; combined={combined}"
        );
        assert!(
            !combined.contains("short capture"),
            "long capture must not emit the short-capture warning; combined={combined}"
        );
        assert!(
            !combined.contains("start a trial"),
            "long capture must not emit the trial CTA; combined={combined}"
        );
    }

    /// The trial CTA is a human-format sales touchpoint. It must never land in
    /// machine-readable formats (JSON, SARIF, etc.); those feed agent pipelines
    /// and scripted consumers that would choke on free text.
    #[test]
    fn capture_quality_short_does_not_emit_upgrade_prompt_in_json() {
        let harness = Harness::new();
        let mut cmd = harness.fallow();
        cmd.env("FALLOW_LICENSE", sign::mint_production_coverage_jwt());
        cmd.env("FALLOW_STUB_MODE", "capture-quality-short");
        for arg in harness.health_args_with_format("json") {
            cmd.arg(arg);
        }
        let (stdout, stderr, code) = run_with(cmd);
        assert_eq!(
            code, 0,
            "json short-capture run must exit 0; stderr={stderr}"
        );
        assert!(
            !stdout.contains("start a trial"),
            "JSON format must never include the trial CTA free text; stdout={stdout}"
        );
        let json: serde_json::Value =
            serde_json::from_str(&stdout).expect("json output must parse");
        let quality = json
            .pointer("/production_coverage/summary/capture_quality")
            .expect("capture_quality absent from json summary");
        assert_eq!(
            quality.get("lazy_parse_warning"),
            Some(&serde_json::json!(true))
        );
        assert_eq!(quality.get("window_seconds"), Some(&serde_json::json!(720)));
        assert_eq!(
            quality.get("instances_observed"),
            Some(&serde_json::json!(1))
        );
    }

    fn exit_code_case(mode: &str, expected: i32) {
        let harness = Harness::new();
        let mut cmd = harness.fallow();
        cmd.env("FALLOW_LICENSE", sign::mint_production_coverage_jwt());
        cmd.env("FALLOW_STUB_MODE", mode);
        for arg in harness.health_args() {
            cmd.arg(arg);
        }
        let (stdout, stderr, code) = run_with(cmd);
        assert_eq!(
            code, expected,
            "sidecar mode {mode} must map to CLI exit {expected}; stdout={stdout}; stderr={stderr}"
        );
    }
}