brok-cli 0.12.0

Brokspace: local-first knowledge index and event-driven agents for your workspace
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
//! The machine-readable output editors rely on (`--json`), checked against
//! the real binary.

use std::path::Path;
use std::process::Command;

use serde_json::Value;

fn brok(dir: &Path, args: &[&str]) -> std::process::Output {
    Command::new(env!("CARGO_BIN_EXE_brok"))
        .args(args)
        .current_dir(dir)
        // Keep the test away from the user's models and defaults.
        .env("BROK_MODELS_DIR", dir.join(".models"))
        .env("BROK_CONFIG_DIR", dir.join(".config"))
        .env("NO_COLOR", "1")
        .output()
        .expect("brok runs")
}

fn json(output: &std::process::Output) -> Value {
    assert!(output.status.success(), "stderr: {}", String::from_utf8_lossy(&output.stderr));
    serde_json::from_slice(&output.stdout).expect("stdout is one JSON document")
}

#[test]
fn status_json_outside_a_workspace() {
    let dir = tempfile::tempdir().unwrap();
    let status = json(&brok(dir.path(), &["status", "--json"]));
    assert_eq!(status["initialized"], false);
    assert_eq!(status["version"], env!("CARGO_PKG_VERSION"));
}

#[test]
fn status_json_in_a_workspace() {
    let dir = tempfile::tempdir().unwrap();
    std::fs::write(dir.path().join("main.rs"), "fn main() {}\n").unwrap();
    let init = brok(
        dir.path(),
        &["init", "--yes", "--no-agent", "--skip-model", "--no-llm", "--no-integrations", "--no-index"],
    );
    assert!(init.status.success(), "init failed: {}", String::from_utf8_lossy(&init.stderr));

    let status = json(&brok(dir.path(), &["status", "--json"]));
    assert_eq!(status["initialized"], true);
    assert_eq!(status["daemon"]["running"], false);
    assert_eq!(status["embeddings"]["enabled"], false);
    assert_eq!(status["llm"]["provider"], "ollama");
    assert!(status["index"].is_array());
    assert!(status["shards"].is_array());
    assert!(status["root"].as_str().is_some_and(|root| !root.is_empty()));
}

fn workspace_with(dir: &Path, file: &str, body: &str) {
    std::fs::create_dir_all(dir).unwrap();
    std::fs::write(dir.join(file), body).unwrap();
    let init =
        brok(dir, &["init", "--yes", "--no-agent", "--skip-model", "--no-llm", "--no-integrations", "--no-index"]);
    assert!(init.status.success(), "init failed: {}", String::from_utf8_lossy(&init.stderr));
    let index = brok(dir, &["index"]);
    assert!(index.status.success(), "index failed: {}", String::from_utf8_lossy(&index.stderr));
}

#[test]
fn scoped_search_reaches_members_only_when_asked() {
    let root = tempfile::tempdir().unwrap();
    let (web, api) = (root.path().join("web"), root.path().join("api"));
    workspace_with(&web, "ui.rs", "fn render_dashboard() { /* draws the dashboard widgets */ }\n");
    workspace_with(&api, "billing.rs", "fn compute_invoice_total() { /* invoice total with taxes */ }\n");
    let add = brok(&web, &["workspace", "add", "../api", "--json"]);
    assert!(add.status.success(), "add failed: {}", String::from_utf8_lossy(&add.stderr));

    // Default scope: this repository only, unchanged output.
    let own = json(&brok(&web, &["search", "invoice", "total", "--json"]));
    assert!(own["hits"].as_array().unwrap().is_empty());
    assert!(own.get("skipped_members").is_none());

    let all = json(&brok(&web, &["search", "invoice", "total", "--scope", "workspace", "--json"]));
    let hits = all["hits"].as_array().unwrap();
    assert_eq!(hits.len(), 1, "{all}");
    assert_eq!(hits[0]["member"], "api");
    assert_eq!(hits[0]["path"], "billing.rs");

    let text = brok(&web, &["search", "invoice", "total", "--scope", "workspace"]);
    assert!(String::from_utf8_lossy(&text.stdout).contains("api:billing.rs"));
}

#[test]
fn scoped_search_names_members_it_could_not_search() {
    let root = tempfile::tempdir().unwrap();
    let (web, plain) = (root.path().join("web"), root.path().join("plain"));
    workspace_with(&web, "ui.rs", "fn render_dashboard() {}\n");
    std::fs::create_dir_all(&plain).unwrap();
    let add = brok(&web, &["workspace", "add", "../plain", "--json"]);
    assert!(add.status.success(), "add failed: {}", String::from_utf8_lossy(&add.stderr));

    let all = json(&brok(&web, &["search", "dashboard", "--scope", "workspace", "--json"]));
    assert!(all["hits"][0]["member"].is_null());
    assert!(all["skipped_members"][0].as_str().unwrap().starts_with("plain:"));
}

#[cfg(unix)]
#[test]
fn a_closed_pipe_ends_quietly() {
    use std::io::Read;
    use std::process::Stdio;

    let dir = tempfile::tempdir().unwrap();
    // Far more output than a pipe buffers, so the write fails once the reader is gone.
    let line = format!("fn item() {{ /* widget gadget {} */ }}\n", "x".repeat(200));
    workspace_with(dir.path(), "a.rs", &line.repeat(400));
    let mut child = Command::new(env!("CARGO_BIN_EXE_brok"))
        .args(["search", "widget", "gadget", "--full", "-n", "50"])
        .current_dir(dir.path())
        .env("BROK_MODELS_DIR", dir.path().join(".models"))
        .env("BROK_CONFIG_DIR", dir.path().join(".config"))
        .env("NO_COLOR", "1")
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();
    // Read a little, then close the pipe like `| head -1`.
    let mut first = [0u8; 16];
    child.stdout.take().unwrap().read_exact(&mut first).unwrap();
    let mut stderr = String::new();
    child.stderr.take().unwrap().read_to_string(&mut stderr).unwrap();
    let status = child.wait().unwrap();
    assert!(!stderr.contains("panicked"), "stderr: {stderr}");
    assert!(status.success(), "status {status:?}, stderr: {stderr}");
}

#[test]
fn status_lists_members_only_in_a_meta_workspace() {
    let root = tempfile::tempdir().unwrap();
    let (web, api) = (root.path().join("web"), root.path().join("api"));
    workspace_with(&web, "ui.rs", "fn render_dashboard() {}\n");
    workspace_with(&api, "billing.rs", "fn compute_invoice_total() {}\n");

    // Unchanged for a single repository.
    assert!(json(&brok(&web, &["status", "--json"])).get("members").is_none());

    assert!(brok(&web, &["workspace", "add", "../api"]).status.success());
    let status = json(&brok(&web, &["status", "--json"]));
    assert_eq!(status["members"][0]["id"], "api");
    assert_eq!(status["members"][0]["state"], "directory");
    assert_eq!(status["members"][0]["has_brokspace"], true);
}

#[test]
fn mcp_search_reaches_members_when_the_agent_asks() {
    use std::io::Write;
    use std::process::Stdio;

    let root = tempfile::tempdir().unwrap();
    let (web, api) = (root.path().join("web"), root.path().join("api"));
    workspace_with(&web, "ui.rs", "fn render_dashboard() {}\n");
    workspace_with(&api, "billing.rs", "fn compute_invoice_total() { /* invoice total */ }\n");
    assert!(brok(&web, &["workspace", "add", "../api"]).status.success());

    let call = |args: &str| -> String {
        let mut child = Command::new(env!("CARGO_BIN_EXE_brok"))
            .arg("mcp")
            .current_dir(&web)
            .env("BROK_MODELS_DIR", web.join(".models"))
            .env("BROK_CONFIG_DIR", web.join(".config"))
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .spawn()
            .unwrap();
        let request = format!(
            r#"{{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{{"name":"brok_search","arguments":{args}}}}}"#
        );
        child.stdin.take().unwrap().write_all(format!("{request}\n").as_bytes()).unwrap();
        let out = child.wait_with_output().unwrap();
        String::from_utf8_lossy(&out.stdout).into_owned()
    };
    let own = call(r#"{"query":"invoice total"}"#);
    assert!(own.contains("No results"), "{own}");
    let all = call(r#"{"query":"invoice total","scope":"workspace"}"#);
    assert!(all.contains("api:billing.rs"), "{all}");
}

#[test]
fn doctor_reports_the_source_control_provider_and_members() {
    let root = tempfile::tempdir().unwrap();
    let (web, plain) = (root.path().join("web"), root.path().join("plain"));
    workspace_with(&web, "ui.rs", "fn render_dashboard() {}\n");
    std::fs::create_dir_all(&plain).unwrap();

    let doctor = |dir: &Path| json(&brok(dir, &["doctor", "--json"]));
    let names = |checks: &Value, name: &str| -> Vec<String> {
        checks
            .as_array()
            .unwrap()
            .iter()
            .filter(|c| c["name"] == name)
            .map(|c| c["detail"].as_str().unwrap().to_owned())
            .collect()
    };
    let before = doctor(&web);
    assert!(names(&before, "source control")[0].starts_with("git (contract v1"), "{before}");
    assert!(names(&before, "member").is_empty());

    assert!(brok(&web, &["workspace", "add", "../plain"]).status.success());
    let after = doctor(&web);
    assert!(names(&after, "member")[0].contains("no Brokspace of its own"), "{after}");
}

fn git_repo_with(dir: &Path, file: &str, body: &str) {
    std::fs::create_dir_all(dir).unwrap();
    std::fs::write(dir.join(file), body).unwrap();
    let git = |args: &[&str]| {
        let out = Command::new("git")
            .args(args)
            .current_dir(dir)
            .env("GIT_AUTHOR_NAME", "t")
            .env("GIT_AUTHOR_EMAIL", "t@example.com")
            .env("GIT_COMMITTER_NAME", "t")
            .env("GIT_COMMITTER_EMAIL", "t@example.com")
            .output()
            .unwrap();
        assert!(out.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&out.stderr));
    };
    git(&["init", "-q"]);
    git(&["add", "."]);
    git(&["commit", "-q", "-m", "init"]);
}

#[test]
fn a_git_url_member_is_cloned_indexed_searched_and_removed() {
    let root = tempfile::tempdir().unwrap();
    let (web, remote) = (root.path().join("web"), root.path().join("remote-api"));
    workspace_with(&web, "ui.rs", "fn render_dashboard() {}\n");
    git_repo_with(&remote, "billing.rs", "fn compute_invoice_total() { /* invoice total */ }\n");
    let url = format!("file://{}", remote.display());

    let added = json(&brok(&web, &["workspace", "add", &url, "--id", "api", "--json"]));
    assert_eq!(added["added"], true);
    assert!(added["sync"]["error"].is_null(), "{added}");
    assert!(web.join(".brokspace/members/api/src/billing.rs").is_file());
    // The URL is what brok.yml records, never a path to the clone.
    let yml = std::fs::read_to_string(web.join(".brokspace/brok.yml")).unwrap();
    assert!(yml.contains("git:") && yml.contains("remote-api"), "{yml}");

    let all = json(&brok(&web, &["search", "invoice", "total", "--scope", "workspace", "--json"]));
    assert_eq!(all["hits"][0]["member"], "api", "{all}");
    assert_eq!(all["hits"][0]["path"], "billing.rs");
    // Not searched unless asked.
    assert!(json(&brok(&web, &["search", "invoice", "total", "--json"]))["hits"].as_array().unwrap().is_empty());

    // Registering again changes nothing; a sync pulls and indexes again.
    assert_eq!(json(&brok(&web, &["workspace", "add", &url, "--json"]))["added"], false);
    let synced = json(&brok(&web, &["workspace", "sync", "--json"]));
    assert_eq!(synced[0]["id"], "api");
    assert!(synced[0]["error"].is_null(), "{synced}");

    let status = json(&brok(&web, &["status", "--json"]));
    assert_eq!(status["members"][0]["state"], "repository");
    assert!(status["members"][0]["git"].as_str().unwrap().ends_with("remote-api"));

    // Removing deletes what brok made, and nothing else.
    assert!(brok(&web, &["workspace", "remove", "api"]).status.success());
    assert!(!web.join(".brokspace/members/api").exists());
    assert!(remote.join("billing.rs").is_file());
}

#[test]
fn a_git_member_with_credentials_or_a_bad_branch_is_refused_without_writing() {
    let root = tempfile::tempdir().unwrap();
    let web = root.path().join("web");
    workspace_with(&web, "ui.rs", "fn render_dashboard() {}\n");
    let before = std::fs::read_to_string(web.join(".brokspace/brok.yml")).unwrap();

    let out = brok(&web, &["workspace", "add", "https://user:hunter2@example.com/acme/api.git"]);
    assert!(!out.status.success());
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(stderr.contains("credentials") && !stderr.contains("hunter2"), "{stderr}");

    // --no-fetch registers without the network; --branch needs a URL.
    assert!(!brok(&web, &["workspace", "add", "../nowhere", "--branch", "x"]).status.success());
    assert_eq!(std::fs::read_to_string(web.join(".brokspace/brok.yml")).unwrap(), before);

    let lazy = json(&brok(&web, &["workspace", "add", "https://example.com/acme/api.git", "--no-fetch", "--json"]));
    assert_eq!(lazy["added"], true);
    let status = json(&brok(&web, &["workspace", "status", "--json"]));
    assert_eq!(status[0]["state"], "not_cloned");
    let search = json(&brok(&web, &["search", "dashboard", "--scope", "workspace", "--json"]));
    assert!(search["skipped_members"][0].as_str().unwrap().contains("not cloned yet"), "{search}");
}

/// A shard asks the index service for `scope: workspace` and gets the members' hits.
#[cfg(unix)]
#[test]
fn the_index_service_widens_a_search_to_the_members() {
    use std::process::Stdio;
    use std::time::{Duration, Instant};

    use brok_sdk::payload::{SearchRequest, SearchResults, SearchScope};
    use brok_sdk::{BrokClient, ConnectOptions};

    struct Daemon(std::process::Child);
    impl Drop for Daemon {
        fn drop(&mut self) {
            let _ = self.0.kill();
            let _ = self.0.wait();
        }
    }

    let root = tempfile::tempdir().unwrap();
    let (web, api) = (root.path().join("web"), root.path().join("api"));
    workspace_with(&web, "ui.rs", "fn render_dashboard() { /* draws the dashboard widgets */ }\n");
    workspace_with(&api, "billing.rs", "fn compute_invoice_total() { /* invoice total with taxes */ }\n");
    let add = brok(&web, &["workspace", "add", "../api", "--json"]);
    assert!(add.status.success(), "add failed: {}", String::from_utf8_lossy(&add.stderr));

    let _daemon = Daemon(
        Command::new(env!("CARGO_BIN_EXE_brok"))
            .args(["run", "--no-shards"])
            .current_dir(&web)
            .env("BROK_MODELS_DIR", web.join(".models"))
            .env("BROK_CONFIG_DIR", web.join(".config"))
            .env("NO_COLOR", "1")
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()
            .expect("brok run starts"),
    );

    let workspace = brok_core::Workspace::open(&web).unwrap();
    let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
    runtime.block_on(async {
        let deadline = Instant::now() + Duration::from_secs(60);
        let client = loop {
            if let Ok(token) = std::fs::read_to_string(workspace.paths().operator_token()) {
                let options = ConnectOptions::new("test", "0.0.0", token.trim());
                if let Ok((client, _inbox)) = BrokClient::connect(&workspace.endpoint(), options).await {
                    break client;
                }
            }
            assert!(Instant::now() < deadline, "the daemon did not start");
            tokio::time::sleep(Duration::from_millis(200)).await;
        };
        let ask = |scope| {
            let client = client.clone();
            async move {
                // The index service answers once it has loaded; a loaded CI runner can take a while,
                // so a timeout is retried until the deadline instead of failing the test.
                let request = SearchRequest::new("invoice total").scope(scope);
                loop {
                    match client.call::<SearchRequest, SearchResults>(&request, Duration::from_secs(10)).await {
                        Ok(results) => break results,
                        Err(error) => {
                            assert!(Instant::now() < deadline, "the index service did not answer: {error}");
                            tokio::time::sleep(Duration::from_millis(500)).await;
                        }
                    }
                }
            }
        };
        let own = ask(SearchScope::Repository).await;
        assert!(own.hits.is_empty(), "this repository has nothing about invoices");

        let wide = ask(SearchScope::Workspace).await;
        assert_eq!(wide.hits.len(), 1, "{:?}", wide.hits.iter().map(|h| &h.path).collect::<Vec<_>>());
        assert_eq!(wide.hits[0].path, "api:billing.rs");
    });
}

#[test]
fn a_search_right_after_start_is_answered() {
    struct Stop<'a>(&'a Path);
    impl Drop for Stop<'_> {
        fn drop(&mut self) {
            let _ = brok(self.0, &["stop"]);
        }
    }
    let dir = tempfile::tempdir().unwrap();
    workspace_with(dir.path(), "billing.rs", "fn compute_invoice_total() { /* invoice total */ }\n");
    let _stop = Stop(dir.path());
    let started = brok(dir.path(), &["start"]);
    assert!(started.status.success(), "{}", String::from_utf8_lossy(&started.stderr));
    let found = json(&brok(dir.path(), &["search", "--json", "invoice total"]));
    assert_eq!(found["hits"][0]["path"], "billing.rs", "{found}");
}

#[test]
fn asking_without_an_agent_fails_at_once_with_the_next_step() {
    struct Stop<'a>(&'a Path);
    impl Drop for Stop<'_> {
        fn drop(&mut self) {
            let _ = brok(self.0, &["stop"]);
        }
    }
    let dir = tempfile::tempdir().unwrap();
    workspace_with(dir.path(), "billing.rs", "fn compute_invoice_total() {}\n");
    let _stop = Stop(dir.path());
    // An operator watching the bus is not someone who answers.
    assert!(brok(dir.path(), &["start"]).status.success());
    for command in [&["ask", "--json", "what is here?"][..], &["chat"], &["skill", "run", "tdd", "x"]] {
        let started = std::time::Instant::now();
        let output = brok(dir.path(), command);
        let error = String::from_utf8_lossy(&output.stderr);
        assert!(!output.status.success(), "{command:?}");
        assert!(error.contains("brok-agent is not installed"), "{command:?}: {error}");
        assert!(started.elapsed() < std::time::Duration::from_secs(10), "{command:?}");
        assert!(output.stdout.is_empty(), "{command:?}");
    }
}

#[test]
fn init_without_a_terminal_writes_no_project_files_unless_told_to() {
    let outside = |dir: &Path| -> Vec<String> {
        let mut names: Vec<String> = std::fs::read_dir(dir)
            .unwrap()
            .map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
            // `.models` and `.config` are where these tests keep the user's files.
            .filter(|n| ![".brokspace", ".models", ".config", "main.rs", ".vscode"].contains(&n.as_str()))
            .collect();
        names.sort();
        names
    };
    let base = ["init", "--skip-model", "--no-agent", "--no-llm", "--no-index"];
    let dir = tempfile::tempdir().unwrap();
    std::fs::write(dir.path().join("main.rs"), "fn main() {}\n").unwrap();
    std::fs::create_dir(dir.path().join(".vscode")).unwrap();

    let silent = brok(dir.path(), &base);
    assert!(silent.status.success(), "{}", String::from_utf8_lossy(&silent.stderr));
    assert_eq!(outside(dir.path()), Vec::<String>::new());
    assert!(!dir.path().join(".vscode/mcp.json").exists());

    let plan = json(&brok(dir.path(), &["setup", "agents", "--dry-run", "--json"]));
    let planned: Vec<&str> = plan["files"].as_array().unwrap().iter().map(|f| f.as_str().unwrap()).collect();
    assert!(planned.contains(&".vscode/mcp.json") && planned.contains(&".agents/skills/"), "{planned:?}");
    assert!(!dir.path().join(".vscode/mcp.json").exists(), "a plan writes nothing");

    let agreed = brok(dir.path(), &[&base[..], &["--yes"]].concat());
    assert!(agreed.status.success());
    assert!(dir.path().join(".vscode/mcp.json").is_file() && dir.path().join(".agents/skills").is_dir());
}

#[test]
fn the_daemon_takes_over_when_another_writer_finishes() {
    struct Stop<'a>(&'a Path);
    impl Drop for Stop<'_> {
        fn drop(&mut self) {
            let _ = brok(self.0, &["stop"]);
        }
    }
    let dir = tempfile::tempdir().unwrap();
    workspace_with(dir.path(), "billing.rs", "fn compute_invoice_total() {}\n");
    // Another process is writing the index, as `brok index` does.
    let lock = std::fs::OpenOptions::new().write(true).open(dir.path().join(".brokspace/databases/.lock")).unwrap();
    lock.try_lock().unwrap();
    let _stop = Stop(dir.path());
    assert!(brok(dir.path(), &["start"]).status.success());
    std::fs::write(dir.path().join("shipping.rs"), "fn schedule_parcel_pickup() {}\n").unwrap();
    let finds = || {
        let found = json(&brok(dir.path(), &["search", "--json", "schedule parcel pickup"]));
        found["hits"].as_array().unwrap().iter().any(|h| h["path"] == "shipping.rs")
    };
    assert!(!finds(), "read only while the other process writes");
    drop(lock);
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
    while !finds() {
        assert!(std::time::Instant::now() < deadline, "the daemon did not take over");
        std::thread::sleep(std::time::Duration::from_millis(250));
    }
}