drip-cli 0.1.0

Delta Read Interception Proxy — sends only file diffs to your LLM agent
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
//! `drip init` / `drip uninstall` for Codex and Gemini.
//!
//! Mirrors the Claude install/uninstall coverage: each agent has its
//! own config + guidance file, and uninstall must preserve every
//! user-authored byte that wasn't ours.

use crate::common::Drip;
use serde_json::Value;
use std::fs;
use std::path::Path;
use std::process::{Command, Output};

fn drip_cmd(drip: &Drip, project: &Path, home: &Path, args: &[&str]) -> Output {
    Command::new(&drip.bin)
        .args(args)
        .current_dir(project)
        .env("HOME", home)
        .env("DRIP_DATA_DIR", drip.data_dir.path())
        .env("DRIP_SESSION_ID", &drip.session_id)
        // Pin SHELL so completion install in init_claude doesn't
        // create surprise files in HOME during these tests.
        .env_remove("SHELL")
        .output()
        .expect("drip spawn")
}

// ─── Codex ──────────────────────────────────────────────────────────

#[test]
fn codex_uninstall_removes_mcp_and_agents_blocks() {
    let drip = Drip::new();
    let project = tempfile::tempdir().unwrap();
    let home = tempfile::tempdir().unwrap();

    // Pre-existing user content we must NOT clobber: an unrelated
    // section in config.toml and a paragraph in AGENTS.md.
    let codex_dir = home.path().join(".codex");
    fs::create_dir_all(&codex_dir).unwrap();
    fs::write(
        codex_dir.join("config.toml"),
        "[user_section]\nkey = \"value\"\n",
    )
    .unwrap();
    fs::write(
        codex_dir.join("AGENTS.md"),
        "# My personal notes\nbe careful with deletes\n",
    )
    .unwrap();

    // Init.
    let o = drip_cmd(
        &drip,
        project.path(),
        home.path(),
        &["init", "--agent", "codex"],
    );
    assert!(
        o.status.success(),
        "init: {}",
        String::from_utf8_lossy(&o.stderr)
    );
    let cfg = fs::read_to_string(codex_dir.join("config.toml")).unwrap();
    assert!(cfg.contains("[mcp_servers.drip]"));
    let md = fs::read_to_string(codex_dir.join("AGENTS.md")).unwrap();
    assert!(md.contains("drip:agents-instructions"));

    // Uninstall.
    let o = drip_cmd(
        &drip,
        project.path(),
        home.path(),
        &["uninstall", "--agent", "codex"],
    );
    assert!(
        o.status.success(),
        "uninstall: {}",
        String::from_utf8_lossy(&o.stderr)
    );

    // Our blocks gone, user's stuff intact.
    let cfg = fs::read_to_string(codex_dir.join("config.toml")).unwrap();
    assert!(
        !cfg.contains("[mcp_servers.drip]"),
        "DRIP block left behind: {cfg}"
    );
    assert!(
        cfg.contains("[user_section]"),
        "user section dropped: {cfg}"
    );
    assert!(cfg.contains("key = \"value\""));

    let md = fs::read_to_string(codex_dir.join("AGENTS.md")).unwrap();
    assert!(!md.contains("drip:agents-instructions"));
    assert!(md.contains("# My personal notes"), "user notes lost: {md}");
    assert!(md.contains("be careful with deletes"));
}

#[test]
fn codex_init_refreshes_stale_block_with_old_args() {
    // Regression: a config.toml written by a pre-v0.2 binary contained
    // `args = ["mcp"]` (no agent tag). Re-running `drip init` used to
    // skip silently because the section header was already there —
    // leaving Codex spawning DRIP without `--agent codex`, so session
    // rows ended up tagged "shell". `ensure_codex_mcp` must now
    // detect the divergence and rewrite the block.
    let drip = Drip::new();
    let project = tempfile::tempdir().unwrap();
    let home = tempfile::tempdir().unwrap();

    // Stale config — header present, but missing `--agent codex`.
    let codex_dir = home.path().join(".codex");
    fs::create_dir_all(&codex_dir).unwrap();
    fs::write(
        codex_dir.join("config.toml"),
        "[user_section]\nkey = \"value\"\n\n\
         [mcp_servers.drip]\n\
         command = \"/old/path/to/drip\"\n\
         args = [\"mcp\"]\n",
    )
    .unwrap();

    let o = drip_cmd(
        &drip,
        project.path(),
        home.path(),
        &["init", "--agent", "codex"],
    );
    assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr));

    let cfg = fs::read_to_string(codex_dir.join("config.toml")).unwrap();
    assert!(
        cfg.contains("--agent\", \"codex\""),
        "init must refresh stale args, got:\n{cfg}"
    );
    assert!(
        !cfg.contains("\"/old/path/to/drip\""),
        "old command path must be replaced, got:\n{cfg}"
    );
    // User content outside our block must survive.
    assert!(
        cfg.contains("[user_section]"),
        "user section dropped: {cfg}"
    );
    assert!(cfg.contains("key = \"value\""));
}

#[test]
fn codex_uninstall_is_idempotent() {
    let drip = Drip::new();
    let project = tempfile::tempdir().unwrap();
    let home = tempfile::tempdir().unwrap();

    // Run uninstall twice with no prior init — must succeed both times.
    let o1 = drip_cmd(
        &drip,
        project.path(),
        home.path(),
        &["uninstall", "--agent", "codex"],
    );
    assert!(o1.status.success());
    let o2 = drip_cmd(
        &drip,
        project.path(),
        home.path(),
        &["uninstall", "--agent", "codex"],
    );
    assert!(o2.status.success());
}

// ─── Gemini ─────────────────────────────────────────────────────────

#[test]
fn gemini_init_writes_settings_and_gemini_md() {
    let drip = Drip::new();
    let project = tempfile::tempdir().unwrap();
    let home = tempfile::tempdir().unwrap();

    let o = drip_cmd(
        &drip,
        project.path(),
        home.path(),
        &["init", "--global", "--agent", "gemini"],
    );
    assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr));

    let settings = home.path().join(".gemini/settings.json");
    let v: Value = serde_json::from_str(&fs::read_to_string(&settings).unwrap()).unwrap();
    assert!(v["mcpServers"]["drip"]["command"].is_string());
    assert_eq!(v["mcpServers"]["drip"]["args"][0].as_str(), Some("mcp"));

    let md = home.path().join(".gemini/GEMINI.md");
    assert!(md.exists());
    assert!(fs::read_to_string(&md)
        .unwrap()
        .contains("drip:agents-instructions"));
}

#[test]
fn gemini_init_preserves_user_settings() {
    let drip = Drip::new();
    let project = tempfile::tempdir().unwrap();
    let home = tempfile::tempdir().unwrap();

    let gem_dir = home.path().join(".gemini");
    fs::create_dir_all(&gem_dir).unwrap();
    let pre = serde_json::json!({
        "telemetry": false,
        "mcpServers": {
            "another": { "command": "/x/y", "args": [] }
        }
    });
    fs::write(
        gem_dir.join("settings.json"),
        serde_json::to_string_pretty(&pre).unwrap(),
    )
    .unwrap();

    drip_cmd(
        &drip,
        project.path(),
        home.path(),
        &["init", "--global", "--agent", "gemini"],
    );

    let v: Value =
        serde_json::from_str(&fs::read_to_string(gem_dir.join("settings.json")).unwrap()).unwrap();
    assert_eq!(v["telemetry"].as_bool(), Some(false));
    assert!(v["mcpServers"]["drip"]["command"].is_string());
    assert!(v["mcpServers"]["another"]["command"].is_string());
}

#[test]
fn gemini_uninstall_removes_drip_only() {
    let drip = Drip::new();
    let project = tempfile::tempdir().unwrap();
    let home = tempfile::tempdir().unwrap();

    let gem_dir = home.path().join(".gemini");
    fs::create_dir_all(&gem_dir).unwrap();
    fs::write(
        gem_dir.join("settings.json"),
        serde_json::to_string_pretty(&serde_json::json!({
            "mcpServers": { "another": { "command": "/x/y", "args": [] } },
            "telemetry": false
        }))
        .unwrap(),
    )
    .unwrap();

    drip_cmd(
        &drip,
        project.path(),
        home.path(),
        &["init", "--global", "--agent", "gemini"],
    );
    drip_cmd(
        &drip,
        project.path(),
        home.path(),
        &["uninstall", "--global", "--agent", "gemini"],
    );

    let v: Value =
        serde_json::from_str(&fs::read_to_string(gem_dir.join("settings.json")).unwrap()).unwrap();
    assert!(v["mcpServers"].get("drip").is_none());
    assert_eq!(v["mcpServers"]["another"]["command"].as_str(), Some("/x/y"));
    assert_eq!(v["telemetry"].as_bool(), Some(false));
    // GEMINI.md block removed.
    let md = fs::read_to_string(gem_dir.join("GEMINI.md")).unwrap();
    assert!(!md.contains("drip:agents-instructions"));
}

// ─── Agent self-tagging via DRIP_AGENT ───────────────────────────────

#[test]
fn gemini_init_writes_agent_flag_in_args() {
    let drip = Drip::new();
    let project = tempfile::tempdir().unwrap();
    let home = tempfile::tempdir().unwrap();

    drip_cmd(
        &drip,
        project.path(),
        home.path(),
        &["init", "--global", "--agent", "gemini"],
    );
    let v: Value = serde_json::from_str(
        &fs::read_to_string(home.path().join(".gemini/settings.json")).unwrap(),
    )
    .unwrap();
    let strs: Vec<&str> = v["mcpServers"]["drip"]["args"]
        .as_array()
        .unwrap()
        .iter()
        .filter_map(|x| x.as_str())
        .collect();
    assert_eq!(strs, vec!["mcp", "--agent", "gemini"]);
}

#[test]
fn drip_agent_env_persists_to_session_row() {
    // Set DRIP_AGENT=gemini and run a read — the resulting sessions
    // row should carry agent="gemini", and `drip sessions` should
    // render it as "Gemini" rather than the heuristic fallback.
    let drip = Drip::new();
    let dir = tempfile::tempdir().unwrap();
    let f = dir.path().join("hello.txt");
    fs::write(&f, "hi\n").unwrap();

    let o = drip
        .cmd()
        .arg("read")
        .arg(&f)
        .env("DRIP_AGENT", "gemini")
        .output()
        .expect("drip read");
    assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr));

    // Listing must now show "Gemini" in the AGENT column for our row.
    let o = drip.cmd().arg("sessions").output().unwrap();
    assert!(o.status.success());
    let out = String::from_utf8_lossy(&o.stdout);
    let row = out
        .lines()
        .find(|l| l.contains(&drip.session_id))
        .unwrap_or_else(|| panic!("session row missing: {out}"));
    assert!(
        row.contains("Gemini"),
        "expected Gemini agent label in row: {row}"
    );
}

#[test]
fn drip_agent_unknown_value_falls_back_to_heuristic() {
    // Garbage DRIP_AGENT mustn't be persisted — agent_from_env
    // returns None for unrecognised values, so the column stays
    // NULL and the listing uses the strategy/id-shape heuristic.
    let drip = Drip::new();
    let dir = tempfile::tempdir().unwrap();
    let f = dir.path().join("hello.txt");
    fs::write(&f, "hi\n").unwrap();
    drip.cmd()
        .arg("read")
        .arg(&f)
        .env("DRIP_AGENT", "totally-not-an-agent")
        .output()
        .expect("drip read");

    // The session id is `test-<nanos>-<pid>`, env strategy. The
    // heuristic labels env-strategy non-UUID rows as "custom".
    let o = drip.cmd().arg("sessions").output().unwrap();
    let out = String::from_utf8_lossy(&o.stdout);
    let row = out.lines().find(|l| l.contains(&drip.session_id)).unwrap();
    assert!(
        row.contains("custom"),
        "expected fallback `custom` label, got: {row}"
    );
}

#[test]
fn drip_mcp_agent_flag_sets_env_var_for_session() {
    // Spawn `drip mcp --agent codex` and immediately close stdin so
    // it exits cleanly. We can't validate the session row this way
    // (mcp::run never opens a Session unless it gets a request), so
    // we exercise the smaller surface: the flag parses and the
    // command starts. A more thorough test would need an MCP client
    // round-trip, which is overkill here.
    let drip = Drip::new();
    let mut child = std::process::Command::new(&drip.bin)
        .args(["mcp", "--agent", "codex"])
        .env("DRIP_DATA_DIR", drip.data_dir.path())
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .expect("spawn drip mcp");
    drop(child.stdin.take()); // EOF → mcp loop exits
    let o = child.wait_with_output().expect("wait");
    // Either success or any clean exit; what we're really asserting
    // is that --agent codex didn't error out at parse time.
    assert!(
        o.status.code().is_some(),
        "drip mcp --agent crashed: stderr={}",
        String::from_utf8_lossy(&o.stderr)
    );
}

#[test]
fn malformed_mcp_json_fails_loudly_rather_than_clobbering() {
    // If the user's settings.json has a syntax error, init must NOT
    // overwrite it — failing loudly is the only safe move.
    let drip = Drip::new();
    let project = tempfile::tempdir().unwrap();
    let home = tempfile::tempdir().unwrap();

    let gem_dir = home.path().join(".gemini");
    fs::create_dir_all(&gem_dir).unwrap();
    let bad = "{ this is not json,, ";
    fs::write(gem_dir.join("settings.json"), bad).unwrap();

    let o = drip_cmd(
        &drip,
        project.path(),
        home.path(),
        &["init", "--global", "--agent", "gemini"],
    );
    assert!(!o.status.success(), "init should fail on malformed JSON");
    // File is untouched.
    let after = fs::read_to_string(gem_dir.join("settings.json")).unwrap();
    assert_eq!(
        after, bad,
        "init clobbered malformed settings.json: {after:?}"
    );
}