rippy-cli 0.2.0

A shell command safety hook for AI coding tools (Claude Code, Cursor, Gemini CLI) — Rust rewrite of Dippy
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
#![allow(clippy::unwrap_used)]

mod common;
use common::run_rippy;

// ---- Inspect integration tests ----

#[test]
fn inspect_list_with_config() {
    let dir = tempfile::TempDir::new().unwrap();
    let config = dir.path().join("test.toml");
    std::fs::write(
        &config,
        "[[rules]]\naction = \"deny\"\npattern = \"rm -rf *\"\nmessage = \"use trash\"\n",
    )
    .unwrap();

    let output = std::process::Command::new(common::rippy_binary())
        .args(["inspect", "--config"])
        .arg(&config)
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("deny"));
    assert!(stdout.contains("rm -rf *"));
    assert!(stdout.contains("Handlers:"));
    assert!(stdout.contains("Simple safe:"));
}

#[test]
fn inspect_trace_safe_command() {
    let output = std::process::Command::new(common::rippy_binary())
        .args(["inspect", "cat /tmp/file"])
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("ALLOW"));
    assert!(stdout.contains("Allowlist"));
}

#[test]
fn inspect_trace_with_config_rule() {
    let dir = tempfile::TempDir::new().unwrap();
    let config = dir.path().join("test.toml");
    std::fs::write(
        &config,
        "[[rules]]\naction = \"deny\"\npattern = \"echo evil\"\nmessage = \"no evil allowed\"\n",
    )
    .unwrap();

    let output = std::process::Command::new(common::rippy_binary())
        .args(["inspect", "--config"])
        .arg(&config)
        .arg("echo evil")
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("DENY"));
    assert!(stdout.contains("no evil allowed"));
}

#[test]
fn inspect_json_output() {
    let output = std::process::Command::new(common::rippy_binary())
        .args(["inspect", "--json", "cat /tmp/file"])
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert_eq!(parsed["decision"], "allow");
    assert!(parsed["steps"].is_array());
}

#[test]
fn inspect_list_json_output() {
    let dir = tempfile::TempDir::new().unwrap();
    let config = dir.path().join("test.toml");
    std::fs::write(&config, "[[rules]]\naction = \"allow\"\npattern = \"ls\"\n").unwrap();

    let output = std::process::Command::new(common::rippy_binary())
        .args(["inspect", "--json", "--config"])
        .arg(&config)
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert!(parsed["config_sources"].is_array());
    assert!(parsed["handler_count"].is_number());
    assert!(parsed["simple_safe_count"].is_number());
}

// ---- Stats integration tests ----

#[test]
fn stats_json_from_populated_db() {
    let dir = tempfile::TempDir::new().unwrap();
    let db_path = dir.path().join("test.db");

    // Populate the DB directly.
    let conn = rusqlite::Connection::open(&db_path).unwrap();
    conn.execute_batch(
        "PRAGMA journal_mode=WAL;
         CREATE TABLE decisions (
             id INTEGER PRIMARY KEY,
             timestamp TEXT NOT NULL DEFAULT (datetime('now')),
             session_id TEXT, mode TEXT, tool_name TEXT NOT NULL,
             command TEXT, decision TEXT NOT NULL, reason TEXT, payload_json TEXT
         );
         INSERT INTO decisions (tool_name, command, decision, reason) VALUES ('Bash', 'git status', 'allow', 'safe');
         INSERT INTO decisions (tool_name, command, decision, reason) VALUES ('Bash', 'git push', 'ask', 'review');
         INSERT INTO decisions (tool_name, command, decision, reason) VALUES ('Bash', 'rm -rf /', 'deny', 'dangerous');",
    )
    .unwrap();
    drop(conn);

    let output = std::process::Command::new(common::rippy_binary())
        .args(["stats", "--json", "--db"])
        .arg(&db_path)
        .output()
        .unwrap();
    assert!(output.status.success(), "stats failed: {:?}", output.status);
    let stdout = String::from_utf8(output.stdout).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert_eq!(parsed["counts"]["total"], 3);
    assert_eq!(parsed["counts"]["allow"], 1);
    assert_eq!(parsed["counts"]["ask"], 1);
    assert_eq!(parsed["counts"]["deny"], 1);
}

// ---- Stdlib regression tests ----

#[test]
fn stdlib_cargo_test_allowed() {
    let json = r#"{"tool_name":"Bash","tool_input":{"command":"cargo test --release"}}"#;
    let (_, code) = run_rippy(json, "claude", &[]);
    assert_eq!(code, 0);
}

#[test]
fn stdlib_cargo_run_asks() {
    let dir = tempfile::TempDir::new().unwrap();
    let json = r#"{"tool_name":"Bash","tool_input":{"command":"cargo run"}}"#;
    let (_, code) = common::run_rippy_in_dir(json, "claude", dir.path());
    assert_eq!(code, 2);
}

#[test]
fn stdlib_rm_asks() {
    let json = r#"{"tool_name":"Bash","tool_input":{"command":"rm -rf /tmp/test"}}"#;
    let (_, code) = run_rippy(json, "claude", &[]);
    assert_eq!(code, 2);
}

#[test]
fn stdlib_sudo_asks() {
    let json = r#"{"tool_name":"Bash","tool_input":{"command":"sudo apt install foo"}}"#;
    let (_, code) = run_rippy(json, "claude", &[]);
    assert_eq!(code, 2);
}

#[test]
fn init_stdout_prints_stdlib() {
    let output = std::process::Command::new(common::rippy_binary())
        .args(["init", "--stdout"])
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("[[rules]]"));
    assert!(stdout.contains("cargo"));
}

#[test]
fn init_creates_config_file() {
    let dir = tempfile::TempDir::new().unwrap();
    let output = std::process::Command::new(common::rippy_binary())
        .args(["init"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let content = std::fs::read_to_string(dir.path().join(".rippy.toml")).unwrap();
    // Non-interactive init defaults to develop package
    assert!(content.contains("[settings]"));
    assert!(content.contains("package = \"develop\""));
}

#[test]
fn init_with_package_flag() {
    let dir = tempfile::TempDir::new().unwrap();
    let output = std::process::Command::new(common::rippy_binary())
        .args(["init", "--package", "review"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let content = std::fs::read_to_string(dir.path().join(".rippy.toml")).unwrap();
    assert!(content.contains("package = \"review\""));
}

#[test]
fn init_stdout_still_works() {
    let output = std::process::Command::new(common::rippy_binary())
        .args(["init", "--stdout"])
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("[[rules]]"));
    assert!(stdout.contains("cargo"));
}

#[test]
fn init_invalid_package_fails() {
    let dir = tempfile::TempDir::new().unwrap();
    let output = std::process::Command::new(common::rippy_binary())
        .args(["init", "--package", "bogus"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(!output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("unknown package"),
        "expected 'unknown package' in output, got: {stdout}"
    );
}

#[test]
fn init_refuses_existing() {
    let dir = tempfile::TempDir::new().unwrap();
    std::fs::write(dir.path().join(".rippy.toml"), "existing").unwrap();
    let output = std::process::Command::new(common::rippy_binary())
        .args(["init"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(!output.status.success());
}

// ---- Flag discovery tests ----

#[test]
fn discover_finds_curl_flags() {
    let home = tempfile::TempDir::new().unwrap();
    let output = std::process::Command::new(common::rippy_binary())
        .args(["discover", "curl", "--json"])
        .env("HOME", home.path())
        .output()
        .unwrap();
    // curl might not be installed in CI, so just check exit code 0 or graceful error
    if output.status.success() {
        let stdout = String::from_utf8(output.stdout).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&stdout).unwrap();
        let arr = parsed.as_array().unwrap();
        // curl has many flag aliases
        assert!(!arr.is_empty());
    }
}

#[test]
fn discover_without_args_errors() {
    let output = std::process::Command::new(common::rippy_binary())
        .args(["discover"])
        .output()
        .unwrap();
    assert!(!output.status.success());
}

// ---- Session file suggest tests ----

#[test]
fn suggest_from_session_file() {
    let dir = tempfile::TempDir::new().unwrap();
    let session_file = dir.path().join("test-session.jsonl");
    // Write a sample session JSONL with Bash tool calls.
    let jsonl = [
        r#"{"type":"assistant","message":{"content":[{"type":"tool_use","id":"t1","name":"Bash","input":{"command":"git status"}}]}}"#,
        r#"{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"t1","content":"ok"}]}}"#,
        r#"{"type":"assistant","message":{"content":[{"type":"tool_use","id":"t2","name":"Bash","input":{"command":"git status"}}]}}"#,
        r#"{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"t2","content":"ok"}]}}"#,
        r#"{"type":"assistant","message":{"content":[{"type":"tool_use","id":"t3","name":"Bash","input":{"command":"git status"}}]}}"#,
        r#"{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"t3","content":"ok"}]}}"#,
        r#"{"type":"assistant","message":{"content":[{"type":"tool_use","id":"t4","name":"Bash","input":{"command":"rm -rf /"}}]}}"#,
        r#"{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"t4","is_error":true,"content":"denied"}]}}"#,
        r#"{"type":"assistant","message":{"content":[{"type":"tool_use","id":"t5","name":"Bash","input":{"command":"rm -rf /"}}]}}"#,
        r#"{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"t5","is_error":true,"content":"denied"}]}}"#,
        r#"{"type":"assistant","message":{"content":[{"type":"tool_use","id":"t6","name":"Bash","input":{"command":"rm -rf /"}}]}}"#,
        r#"{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"t6","is_error":true,"content":"denied"}]}}"#,
    ];
    std::fs::write(&session_file, jsonl.join("\n")).unwrap();

    let output = std::process::Command::new(common::rippy_binary())
        .args([
            "suggest",
            "--session-file",
            session_file.to_str().unwrap(),
            "--json",
            "--min-count",
            "2",
        ])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    let suggestions: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    let arr = suggestions.as_array().unwrap();
    assert!(arr.len() >= 2);

    // Should have allow and deny suggestions
    let actions: Vec<&str> = arr.iter().filter_map(|s| s["action"].as_str()).collect();
    assert!(actions.contains(&"allow"));
    assert!(actions.contains(&"deny"));
}

// ---- Debug command tests ----

#[test]
fn debug_shows_allow_verdict() {
    let output = std::process::Command::new(common::rippy_binary())
        .args(["debug", "git status"])
        .output()
        .unwrap();
    assert_eq!(
        output.status.code().unwrap_or(-1),
        0,
        "debug always exits 0"
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("ALLOW"), "should show ALLOW, got: {stdout}");
    assert!(stdout.contains("Config sources:"), "should show sources");
    assert!(stdout.contains("Decision trace:"), "should show trace");
}

#[test]
fn debug_shows_deny_with_reason() {
    let dir = tempfile::TempDir::new().unwrap();
    let config_path = dir.path().join("test.toml");
    std::fs::write(
        &config_path,
        "[[rules]]\naction = \"deny\"\npattern = \"rm -rf *\"\nmessage = \"use trash\"\n",
    )
    .unwrap();
    let config_str = config_path.to_str().unwrap();
    let output = std::process::Command::new(common::rippy_binary())
        .args(["debug", "rm -rf /tmp", "--config", config_str])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("DENY"), "should show DENY, got: {stdout}");
    assert!(
        stdout.contains("use trash"),
        "should show reason, got: {stdout}"
    );
}

#[test]
fn debug_json_output_valid() {
    let output = std::process::Command::new(common::rippy_binary())
        .args(["debug", "ls", "--json"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    let v: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert_eq!(v["decision"], "allow");
    assert!(v["sources"].is_array());
    assert!(v["steps"].is_array());
}

#[test]
fn debug_shows_config_source_override() {
    let dir = tempfile::TempDir::new().unwrap();
    let config_path = dir.path().join("custom.toml");
    std::fs::write(&config_path, "").unwrap();
    let config_str = config_path.to_str().unwrap();
    let output = std::process::Command::new(common::rippy_binary())
        .args(["debug", "echo hello", "--config", config_str])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("override"),
        "should show override, got: {stdout}"
    );
}

#[test]
fn debug_unknown_command_shows_ask() {
    let output = std::process::Command::new(common::rippy_binary())
        .args(["debug", "totally_unknown_command_xyz"])
        .output()
        .unwrap();
    assert_eq!(
        output.status.code().unwrap_or(-1),
        0,
        "debug always exits 0"
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("ASK"),
        "unknown cmd should show ASK, got: {stdout}"
    );
}

#[test]
fn debug_shows_resolved_command_for_arithmetic() {
    let output = std::process::Command::new(common::rippy_binary())
        .args(["debug", "echo $((2+2))"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("Resolved: echo 4"),
        "should show resolved arithmetic, got: {stdout}"
    );
    assert!(
        stdout.contains("ALLOW"),
        "resolved arithmetic should allow, got: {stdout}"
    );
}

#[test]
fn debug_json_includes_resolved_field() {
    let output = std::process::Command::new(common::rippy_binary())
        .args(["debug", "echo $'\\x41'", "--json"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    let v: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert_eq!(v["resolved"], "echo A");
    assert_eq!(v["decision"], "allow");
}