conproxy 0.2.1

Cache proxy server for heterogeneous RAG/vector search backends with cascade, scope, and MCP
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
#![allow(
    clippy::arithmetic_side_effects,
    clippy::indexing_slicing,
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic
)]

//! UAT tests for proxy CLI subcommands and search
//!
//! Group A: No proxy needed — tests error paths when proxy is not running.
//! Group B: Proxy-dependent "not running" paths.
//! Group C: Proxy lifecycle (start/stop daemon) — uses `#[serial]`.

mod common;

use common::*;
use serial_test::serial;
use std::fs;

// ─── Group A: No proxy needed ───────────────────────────────────────────────

#[test]
#[serial]
fn test_proxy_status_not_running() {
    let dir = temp_dir();
    write_project_config(dir.path());
    let output = run_conproxy_in(dir.path(), &["status"]);
    // Should succeed even when proxy is not running (exits 0)
    let out = stdout(&output);
    assert!(
        out.contains("Proxy is not running"),
        "expected 'Proxy is not running' message, got: {}",
        out
    );
}

#[test]
#[serial]
fn test_proxy_status_json_not_running() {
    let dir = temp_dir();
    write_project_config(dir.path());
    let output = run_conproxy_in(dir.path(), &["status", "--json"]);
    assert_success(&output);

    let json = parse_json_output(&output);
    assert_eq!(json["running"], false);
}

#[test]
fn test_proxy_install_prints_instructions() {
    let dir = temp_dir();
    write_project_config(dir.path());
    let output = run_conproxy_in(dir.path(), &["install"]);
    assert_success(&output);

    let out = stdout(&output);
    // Linux → "systemd", macOS → "launchd", other → "not supported"
    assert!(
        out.contains("systemd") || out.contains("launchd") || out.contains("not supported"),
        "expected platform-specific install instructions, got: {}",
        out
    );
}

#[test]
fn test_proxy_uninstall_not_installed() {
    let dir = temp_dir();
    write_project_config(dir.path());
    let output = run_conproxy_in(dir.path(), &["uninstall"]);
    assert_success(&output);

    let out = stdout(&output);
    // Linux → "Service not installed." or uninstall instructions
    // macOS → "Service not installed."
    // Other → "not supported"
    assert!(
        out.contains("not installed") || out.contains("not supported") || out.contains("uninstall"),
        "expected service not found message, got: {}",
        out
    );
}

#[test]
fn test_search_no_proxy() {
    let dir = temp_dir();
    write_project_config(dir.path());
    let output = run_conproxy_in(dir.path(), &["search", "test query"]);
    // Search without proxy prints error to stderr
    let err = stderr(&output);
    assert!(
        err.contains("No proxy running") || err.contains("proxy"),
        "expected 'No proxy running' message, got stderr: {}",
        err
    );
}

// ─── Group B: Proxy-dependent "not running" paths ───────────────────────────

#[test]
fn test_proxy_contexts_not_running() {
    let dir = temp_dir();
    write_project_config(dir.path());
    let output = run_conproxy_in(dir.path(), &["contexts"]);
    let out = stdout(&output);
    let err = stderr(&output);
    assert!(
        out.contains("not running")
            || out.contains("Proxy is not running")
            || err.contains("transport")
            || err.contains("not running"),
        "expected 'not running' / 'transport' message, got: stdout={} stderr={}",
        out,
        err
    );
}

#[test]
fn test_proxy_peer_not_running() {
    let dir = temp_dir();
    write_project_config(dir.path());
    let output = run_conproxy_in(dir.path(), &["peer"]);
    let out = stdout(&output);
    assert!(
        out.contains("not running") || out.contains("Proxy is not running"),
        "expected 'not running' message, got: {}",
        out
    );
}

#[test]
fn test_proxy_cdc_not_running() {
    let dir = temp_dir();
    write_project_config(dir.path());
    let output = run_conproxy_in(dir.path(), &["cdc"]);
    let out = stdout(&output);
    assert!(
        out.contains("not running") || out.contains("Proxy is not running"),
        "expected 'not running' message, got: {}",
        out
    );
}

#[test]
fn test_seed_list_empty() {
    // `seed list` against a project config with no scope phrases should
    // report the empty state (no proxy needed).
    let dir = temp_dir();
    write_project_config(dir.path());
    let output = run_conproxy_in(dir.path(), &["seed", "list"]);
    assert_success(&output);
    let out = stdout(&output);
    assert!(
        out.contains("No scope phrases configured"),
        "expected 'No scope phrases configured' in seed list output, got: {}",
        out
    );
}

#[test]
fn test_seed_list_json_empty() {
    // `seed list --json` with no scope phrases should emit `[]`.
    let dir = temp_dir();
    write_project_config(dir.path());
    let output = run_conproxy_in(dir.path(), &["seed", "list", "--json"]);
    assert_success(&output);
    let out = stdout(&output).trim().to_string();
    assert_eq!(
        out, "[]",
        "expected '[]' from seed list --json, got: {}",
        out
    );
}

// ─── Group C: Proxy lifecycle (daemon start/stop) ───────────────────────────

#[test]
#[serial]
fn test_proxy_start_daemon_and_stop() {
    let dir = temp_dir();
    write_project_config(dir.path());
    let _guard = ProxyGuard::new(dir.path());

    let _ports = start_proxy_daemon_ephemeral(dir.path());

    // Verify running via text status
    let output = run_conproxy_in(dir.path(), &["status"]);
    assert_success(&output);
    assert!(
        stdout_contains(&output, "running"),
        "expected proxy to show as running"
    );

    // Stop
    let output = run_conproxy_in(dir.path(), &["stop"]);
    assert_success(&output);

    // Verify stopped
    // Give a moment for process to fully exit
    std::thread::sleep(std::time::Duration::from_millis(500));
    let output = run_conproxy_in(dir.path(), &["status"]);
    assert_success(&output);
    assert!(
        stdout_contains(&output, "not running"),
        "expected proxy to show as not running after stop"
    );
}

#[test]
#[serial]
fn test_proxy_start_daemon_json_status() {
    let dir = temp_dir();
    write_project_config(dir.path());
    let _guard = ProxyGuard::new(dir.path());

    let _ports = start_proxy_daemon_ephemeral(dir.path());

    let output = run_conproxy_in(dir.path(), &["status", "--json"]);
    assert_success(&output);

    let json = parse_json_output(&output);
    assert_eq!(json["running"], true, "expected running: true in JSON");
    assert!(
        json["pid"].is_number() || json["pid"].is_u64(),
        "expected pid in JSON"
    );
}

#[test]
#[serial]
fn test_proxy_start_already_running() {
    let dir = temp_dir();
    write_project_config(dir.path());
    let _guard = ProxyGuard::new(dir.path());

    let _ports = start_proxy_daemon_ephemeral(dir.path());

    // Try to start again
    let output = run_conproxy_in(
        dir.path(),
        &[
            "start",
            "--daemon",
            "--listen",
            &format!("127.0.0.1:{}", _ports.grpc_port),
        ],
    );
    assert_success(&output);
    let out = stdout(&output);
    assert!(
        out.contains("already running"),
        "expected 'already running' message, got: {}",
        out
    );
}

#[test]
#[serial]
#[ignore = "KNOWN BROKEN (2026-07-24): daemon reports started but subsequent CLI commands see 'Proxy is not running'. Likely config-injection or PID-file mismatch with the modern context-rooted config. Run `cargo test -- --ignored` to reproduce locally and diagnose before fixing."]
fn test_proxy_contexts_with_running_proxy() {
    let dir = temp_dir();
    write_project_config(dir.path());
    let _guard = ProxyGuard::new(dir.path());

    let _ports = start_proxy_daemon_ephemeral(dir.path());

    // Text output
    let output = run_conproxy_in(dir.path(), &["contexts"]);
    assert_success(&output);
    let out = stdout(&output);
    assert!(
        out.contains("context") || out.contains("Context") || out.contains("default"),
        "expected context list, got: {}",
        out
    );

    // JSON output
    let output = run_conproxy_in(dir.path(), &["contexts", "--json"]);
    assert_success(&output);
    let json = parse_json_output(&output);
    assert!(
        json["current"].is_string() || json["contexts"].is_array(),
        "expected current/contexts in JSON"
    );
}

#[test]
#[serial]
#[ignore = "KNOWN BROKEN (2026-07-24): daemon reports started but subsequent CLI commands see 'Proxy is not running'. Likely config-injection or PID-file mismatch with the modern context-rooted config. Run `cargo test -- --ignored` to reproduce locally and diagnose before fixing."]
fn test_proxy_peer_with_running_proxy() {
    let dir = temp_dir();
    write_project_config(dir.path());
    let _guard = ProxyGuard::new(dir.path());

    let _ports = start_proxy_daemon_ephemeral(dir.path());

    // Text output — peers disabled by default
    let output = run_conproxy_in(dir.path(), &["peer"]);
    assert_success(&output);
    let out = stdout(&output);
    assert!(
        out.contains("disabled") || out.contains("enabled"),
        "expected peer status, got: {}",
        out
    );

    // JSON output
    let output = run_conproxy_in(dir.path(), &["peer", "--json"]);
    assert_success(&output);
    // Should be valid JSON (even if it shows an error or status)
    let out = stdout(&output);
    assert!(
        out.starts_with('{') || out.starts_with('['),
        "expected JSON output, got: {}",
        out
    );
}

#[test]
#[serial]
#[ignore = "KNOWN BROKEN (2026-07-24): daemon reports started but subsequent CLI commands see 'Proxy is not running'. Likely config-injection or PID-file mismatch with the modern context-rooted config. Run `cargo test -- --ignored` to reproduce locally and diagnose before fixing."]
fn test_proxy_cdc_with_running_proxy() {
    let dir = temp_dir();
    write_project_config(dir.path());
    let _guard = ProxyGuard::new(dir.path());

    let _ports = start_proxy_daemon_ephemeral(dir.path());

    // Text output — CDC disabled by default
    let output = run_conproxy_in(dir.path(), &["cdc"]);
    assert_success(&output);
    let out = stdout(&output);
    assert!(
        out.contains("disabled") || out.contains("enabled"),
        "expected CDC status, got: {}",
        out
    );

    // JSON output
    let output = run_conproxy_in(dir.path(), &["cdc", "--json"]);
    assert_success(&output);
    let out = stdout(&output);
    assert!(out.starts_with('{'), "expected JSON object, got: {}", out);
}

#[test]
#[serial]
fn test_proxy_stop_removes_pid_file() {
    let dir = temp_dir();
    write_project_config(dir.path());
    let _guard = ProxyGuard::new(dir.path());
    let _ports = start_proxy_daemon_ephemeral(dir.path());

    let pid_file = test_pid_file_path(dir.path());
    assert!(
        pid_file.exists(),
        "PID file should exist while proxy is running"
    );

    let _ = run_conproxy_in(dir.path(), &["stop"]);
    // Give process a moment to exit and remove PID file
    std::thread::sleep(std::time::Duration::from_millis(500));
    assert!(!pid_file.exists(), "PID file should be removed after stop");
}

#[test]
#[serial]
fn test_proxy_pid_file_isolation_concurrent() {
    let dir_a = temp_dir();
    let dir_b = temp_dir();
    write_project_config(dir_a.path());
    write_project_config(dir_b.path());
    let _guard_a = ProxyGuard::new(dir_a.path());
    let _guard_b = ProxyGuard::new(dir_b.path());

    let _ports_a = start_proxy_daemon_ephemeral(dir_a.path());
    let _ports_b = start_proxy_daemon_ephemeral(dir_b.path());

    // Each dir sees only its own daemon (different CONPROXY_PID_FILE)
    let out_a = run_conproxy_in(dir_a.path(), &["status", "--json"]);
    let json_a = parse_json_output(&out_a);
    assert_eq!(
        json_a["running"], true,
        "dirA should see its own daemon running"
    );

    let out_b = run_conproxy_in(dir_b.path(), &["status", "--json"]);
    let json_b = parse_json_output(&out_b);
    assert_eq!(
        json_b["running"], true,
        "dirB should see its own daemon running"
    );
}

// ─── Group D: CLI arg parsing (no proxy needed) ──────────────────────────

#[test]
fn test_cli_context_switch() {
    let dir = temp_dir();
    write_project_config(dir.path());
    let output = run_conproxy_in(dir.path(), &["context", "switch", "myctx"]);
    // Fails because no proxy running, but demonstrates arg parsing
    assert_failure(&output);
    let err = stderr(&output);
    assert!(
        err.contains("not running") || err.contains("proxy") || err.contains("connect"),
        "expected proxy error on context switch, got stderr: {}",
        err
    );
}

#[test]
fn test_cli_context_create() {
    let dir = temp_dir();
    write_project_config(dir.path());
    let output = run_conproxy_in(dir.path(), &["context", "create", "myctx"]);
    assert_failure(&output);
    let err = stderr(&output);
    assert!(
        err.contains("not running") || err.contains("proxy") || err.contains("connect"),
        "expected proxy error on context create, got stderr: {}",
        err
    );
}

#[test]
fn test_cli_logs_default() {
    let dir = temp_dir();
    write_project_config(dir.path());
    let output = run_conproxy_in(dir.path(), &["logs"]);
    assert_success(&output);
    let out = stdout(&output);
    assert!(
        out.contains("-- No entries --"),
        "expected '-- No entries --' in log output, got: {}",
        out
    );
}

#[test]
fn test_seed_list_with_phrases() {
    // `seed list` against a config that DOES define scope phrases should
    // render the count + the entries. Replaces the old `seed lookup` test
    // that called a removed subcommand.
    let dir = temp_dir();
    write_project_config(dir.path());
    let toml = r#"[upstreams.dummy]
url = "http://127.0.0.1:1"
type = "elasticsearch"
index = "test"

[contexts.default]
default = true
[[contexts.default.upstreams]]
ref = "dummy"
priority = 0

[contexts.default.scope]
seeds = ["alpha", "beta"]
"#;
    fs::write(dir.path().join(".conproxy/conproxy.toml"), toml).expect("write toml");
    let output = run_conproxy_in(dir.path(), &["seed", "list"]);
    assert_success(&output);
    let out = stdout(&output);
    assert!(
        out.contains("Scope phrases (2 configured)"),
        "expected 'Scope phrases (2 configured)' in seed list output, got: {}",
        out
    );
    assert!(
        out.contains("alpha") && out.contains("beta"),
        "expected both 'alpha' and 'beta' phrases listed, got: {}",
        out
    );
}