repograph 0.2.0

CLI for registering, grouping, and exposing local git repositories as structured context for AI agents.
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
476
477
478
479
480
//! Acceptance tests for `repograph context`.
//!
//! Mirrors the structure of `tests/status.rs`. Each spec scenario from
//! `openspec/changes/context-command/specs/context-command/spec.md` is
//! represented by at least one acceptance test here.

#![allow(clippy::unwrap_used, clippy::expect_used)]

mod common;

use std::path::Path;

use tempfile::TempDir;

use crate::common::{fixture_git_repo, repograph_cmd};

/// Run `repograph init --no-prompt --agents <list> --scope project` against
/// `config_dir` so subsequent `context` invocations have `[agents]`
/// configured without touching the cliclack flow.
///
/// `--scope project` is required because the `agent-skills` change made
/// `--scope` mandatory under `--no-prompt` when a selected agent has a
/// meaningful scope choice (claude-code, windsurf). We pin to `project` so
/// any per-agent artifacts written during init land in `config_dir.parent()`
/// (the test's tempdir) and get cleaned up automatically.
fn init_agents(config_dir: &Path, agents: &str) {
    let cwd = config_dir
        .parent()
        .expect("config_dir always lives under a tempdir");
    repograph_cmd(config_dir)
        .current_dir(cwd)
        .arg("init")
        .arg("--no-prompt")
        .arg("--agents")
        .arg(agents)
        .arg("--scope")
        .arg("project")
        .assert()
        .success();
}

fn register(config_dir: &Path, repo: &Path, name: &str) {
    repograph_cmd(config_dir)
        .arg("add")
        .arg(repo)
        .arg("--name")
        .arg(name)
        .assert()
        .success();
}

fn create_workspace(config_dir: &Path, name: &str) {
    repograph_cmd(config_dir)
        .arg("workspace")
        .arg("create")
        .arg(name)
        .assert()
        .success();
}

fn add_to_workspace(config_dir: &Path, workspace: &str, repos: &[&str]) {
    let mut cmd = repograph_cmd(config_dir);
    cmd.arg("workspace").arg("add").arg(workspace);
    for r in repos {
        cmd.arg(r);
    }
    cmd.assert().success();
}

fn parse_context_json(stdout: &[u8]) -> serde_json::Value {
    serde_json::from_slice(stdout).expect("stdout is valid JSON")
}

// ─── default scope, JSON happy path ─────────────────────────────────────────

#[test]
fn default_scope_json_includes_every_registered_repo() {
    let tmp = TempDir::new().unwrap();
    let config_dir = tmp.path().join("config");
    let api = fixture_git_repo(tmp.path(), "api");
    std::fs::write(api.join("CLAUDE.md"), "api ctx\n").unwrap();
    let ui = fixture_git_repo(tmp.path(), "ui");
    std::fs::write(ui.join("CLAUDE.md"), "ui ctx\n").unwrap();

    init_agents(&config_dir, "claude-code");
    register(&config_dir, &api, "api");
    register(&config_dir, &ui, "ui");

    let out = repograph_cmd(&config_dir)
        .arg("context")
        .arg("--json")
        .assert()
        .success();
    let v = parse_context_json(&out.get_output().stdout);

    assert_eq!(v["schema_version"], 1);
    assert!(v["generated_at"].is_string());
    assert_eq!(v["agents"][0], "claude-code");
    assert_eq!(v["scope"]["kind"], "all");
    let repos = v["repos"].as_array().unwrap();
    assert_eq!(repos.len(), 2);
    // Sorted ascending.
    assert_eq!(repos[0]["name"], "api");
    assert_eq!(repos[1]["name"], "ui");
    assert_eq!(repos[0]["agent_docs"][0]["files"][0]["path"], "CLAUDE.md");
    assert_eq!(
        repos[0]["agent_docs"][0]["files"][0]["content"],
        "api ctx\n"
    );
    assert_eq!(repos[1]["agent_docs"][0]["files"][0]["content"], "ui ctx\n");
    assert!(v["warnings"].is_array() && v["warnings"].as_array().unwrap().is_empty());
}

// ─── workspace scope ────────────────────────────────────────────────────────

#[test]
fn workspace_scope_filters_to_members_only() {
    let tmp = TempDir::new().unwrap();
    let config_dir = tmp.path().join("config");
    let api = fixture_git_repo(tmp.path(), "api");
    let lib = fixture_git_repo(tmp.path(), "lib");
    let ui = fixture_git_repo(tmp.path(), "ui");
    for p in [&api, &lib, &ui] {
        std::fs::write(p.join("CLAUDE.md"), "x\n").unwrap();
    }

    init_agents(&config_dir, "claude-code");
    register(&config_dir, &api, "api");
    register(&config_dir, &lib, "lib");
    register(&config_dir, &ui, "ui");
    create_workspace(&config_dir, "backend");
    add_to_workspace(&config_dir, "backend", &["api", "lib"]);

    let out = repograph_cmd(&config_dir)
        .arg("context")
        .arg("--workspace")
        .arg("backend")
        .arg("--json")
        .assert()
        .success();
    let v = parse_context_json(&out.get_output().stdout);

    assert_eq!(v["scope"]["kind"], "workspace");
    assert_eq!(v["scope"]["name"], "backend");
    let repos = v["repos"].as_array().unwrap();
    assert_eq!(repos.len(), 2);
    let names: Vec<&str> = repos.iter().map(|r| r["name"].as_str().unwrap()).collect();
    assert_eq!(names, vec!["api", "lib"]);
}

// ─── positional scope ───────────────────────────────────────────────────────

#[test]
fn positional_scope_picks_named_repos_in_user_order_in_scope_echo() {
    let tmp = TempDir::new().unwrap();
    let config_dir = tmp.path().join("config");
    let api = fixture_git_repo(tmp.path(), "api");
    let lib = fixture_git_repo(tmp.path(), "lib");
    let ui = fixture_git_repo(tmp.path(), "ui");
    for p in [&api, &lib, &ui] {
        std::fs::write(p.join("CLAUDE.md"), "x\n").unwrap();
    }

    init_agents(&config_dir, "claude-code");
    register(&config_dir, &api, "api");
    register(&config_dir, &lib, "lib");
    register(&config_dir, &ui, "ui");

    let out = repograph_cmd(&config_dir)
        .arg("context")
        .arg("ui")
        .arg("api")
        .arg("--json")
        .assert()
        .success();
    let v = parse_context_json(&out.get_output().stdout);

    assert_eq!(v["scope"]["kind"], "repos");
    let echoed: Vec<&str> = v["scope"]["repos"]
        .as_array()
        .unwrap()
        .iter()
        .map(|r| r.as_str().unwrap())
        .collect();
    assert_eq!(echoed, vec!["ui", "api"], "scope echoes user order");

    let names: Vec<&str> = v["repos"]
        .as_array()
        .unwrap()
        .iter()
        .map(|r| r["name"].as_str().unwrap())
        .collect();
    // repos[] is always sorted.
    assert_eq!(names, vec!["api", "ui"]);
}

// ─── mutual exclusion ───────────────────────────────────────────────────────

#[test]
fn workspace_and_positional_are_mutually_exclusive() {
    let tmp = TempDir::new().unwrap();
    let config_dir = tmp.path().join("config");
    init_agents(&config_dir, "claude-code");

    repograph_cmd(&config_dir)
        .arg("context")
        .arg("--workspace")
        .arg("any")
        .arg("api")
        .assert()
        .failure()
        .code(2)
        .stderr(predicates::str::contains("cannot be used with"));
}

// ─── unknown name errors ────────────────────────────────────────────────────

#[test]
fn unknown_workspace_exits_3() {
    let tmp = TempDir::new().unwrap();
    let config_dir = tmp.path().join("config");
    init_agents(&config_dir, "claude-code");

    repograph_cmd(&config_dir)
        .arg("context")
        .arg("--workspace")
        .arg("ghost")
        .arg("--json")
        .assert()
        .failure()
        .code(3)
        .stderr(predicates::str::contains("ghost"));
}

#[test]
fn unknown_positional_repo_exits_3_with_no_payload() {
    let tmp = TempDir::new().unwrap();
    let config_dir = tmp.path().join("config");
    let api = fixture_git_repo(tmp.path(), "api");
    init_agents(&config_dir, "claude-code");
    register(&config_dir, &api, "api");

    let out = repograph_cmd(&config_dir)
        .arg("context")
        .arg("api")
        .arg("bogus")
        .arg("--json")
        .assert()
        .failure()
        .code(3)
        .stderr(predicates::str::contains("bogus"));
    assert!(
        out.get_output().stdout.is_empty(),
        "no partial payload on unknown-name error, got: {:?}",
        String::from_utf8_lossy(&out.get_output().stdout)
    );
}

// ─── inline-warnings semantics (no aborts) ──────────────────────────────────

#[test]
fn missing_repo_path_yields_placeholder_entry_exit_zero() {
    let tmp = TempDir::new().unwrap();
    let config_dir = tmp.path().join("config");
    let ghost = fixture_git_repo(tmp.path(), "ghost");
    init_agents(&config_dir, "claude-code");
    register(&config_dir, &ghost, "ghost");
    std::fs::remove_dir_all(&ghost).unwrap();

    let out = repograph_cmd(&config_dir)
        .arg("context")
        .arg("--json")
        .assert()
        .success();
    let v = parse_context_json(&out.get_output().stdout);
    let entry = &v["repos"][0];
    assert_eq!(entry["name"], "ghost");
    assert!(entry["branch"].is_null());
    assert!(entry["agent_docs"].as_array().unwrap().is_empty());
    let warnings = entry["warnings"].as_array().unwrap();
    assert!(!warnings.is_empty(), "missing path produces warning");
}

#[test]
fn non_utf8_file_skipped_with_warning() {
    let tmp = TempDir::new().unwrap();
    let config_dir = tmp.path().join("config");
    let repo = fixture_git_repo(tmp.path(), "binary");
    std::fs::write(repo.join(".cursorrules"), [0xFF, 0xFE]).unwrap();
    init_agents(&config_dir, "cursor");
    register(&config_dir, &repo, "binary");

    let out = repograph_cmd(&config_dir)
        .arg("context")
        .arg("--json")
        .assert()
        .success();
    let v = parse_context_json(&out.get_output().stdout);
    let docs = v["repos"][0]["agent_docs"].as_array().unwrap();
    // Cursor agent block present but with no .cursorrules in files (UTF-8 rejected).
    let cursor_doc = docs.iter().find(|d| d["agent"] == "cursor").unwrap();
    let files = cursor_doc["files"].as_array().unwrap();
    assert!(
        files.iter().all(|f| f["path"] != ".cursorrules"),
        "non-UTF-8 file omitted from files: {files:?}"
    );
    let warnings = v["repos"][0]["warnings"].as_array().unwrap();
    assert!(
        warnings
            .iter()
            .any(|w| w.as_str().unwrap().contains(".cursorrules")
                && w.as_str().unwrap().contains("UTF-8")),
        "warning names file and reason: {warnings:?}"
    );
}

#[test]
fn glob_expansion_lists_files_sorted_alphabetically() {
    let tmp = TempDir::new().unwrap();
    let config_dir = tmp.path().join("config");
    let repo = fixture_git_repo(tmp.path(), "r");
    std::fs::create_dir_all(repo.join(".cursor/rules")).unwrap();
    std::fs::write(repo.join(".cursor/rules/b.md"), "b").unwrap();
    std::fs::write(repo.join(".cursor/rules/a.md"), "a").unwrap();
    init_agents(&config_dir, "cursor");
    register(&config_dir, &repo, "r");

    let out = repograph_cmd(&config_dir)
        .arg("context")
        .arg("--json")
        .assert()
        .success();
    let v = parse_context_json(&out.get_output().stdout);
    let files = v["repos"][0]["agent_docs"][0]["files"].as_array().unwrap();
    let paths: Vec<&str> = files.iter().map(|f| f["path"].as_str().unwrap()).collect();
    assert_eq!(
        paths,
        vec![".cursor/rules/a.md", ".cursor/rules/b.md"],
        "files sorted by relative path"
    );
}

#[test]
fn empty_agents_selection_yields_empty_agent_docs() {
    let tmp = TempDir::new().unwrap();
    let config_dir = tmp.path().join("config");
    let repo = fixture_git_repo(tmp.path(), "r");
    std::fs::write(repo.join("CLAUDE.md"), "x").unwrap();
    register(&config_dir, &repo, "r");
    // Write [agents] section with empty selection directly so we don't have
    // to drive cliclack — mirror how init does it but with no agents.
    let toml = format!(
        "[agents]\nselected = []\n\n[repo.r]\npath = \"{}\"\n",
        repo.display().to_string().replace('\\', "\\\\")
    );
    std::fs::create_dir_all(&config_dir).unwrap();
    std::fs::write(config_dir.join("config.toml"), toml).unwrap();

    let out = repograph_cmd(&config_dir)
        .arg("context")
        .arg("--json")
        .assert()
        .success();
    let v = parse_context_json(&out.get_output().stdout);
    assert!(v["agents"].as_array().unwrap().is_empty());
    assert!(
        v["repos"][0]["agent_docs"].as_array().unwrap().is_empty(),
        "no selected agents yields empty agent_docs"
    );
}

// ─── ensure_agents gating ───────────────────────────────────────────────────

#[test]
fn non_tty_without_agents_exits_2_naming_init() {
    let tmp = TempDir::new().unwrap();
    let config_dir = tmp.path().join("config");
    // Register a repo but DO NOT call init.
    let repo = fixture_git_repo(tmp.path(), "r");
    register(&config_dir, &repo, "r");

    let out = repograph_cmd(&config_dir)
        .arg("context")
        .assert()
        .failure()
        .code(2)
        .stderr(predicates::str::contains("repograph init"));
    assert!(
        out.get_output().stdout.is_empty(),
        "no payload when gating fails, got: {:?}",
        String::from_utf8_lossy(&out.get_output().stdout)
    );
}

// ─── output contract: stdout is clean ───────────────────────────────────────

#[test]
fn stdout_is_pure_json_no_diagnostics_bleed() {
    let tmp = TempDir::new().unwrap();
    let config_dir = tmp.path().join("config");
    let repo = fixture_git_repo(tmp.path(), "r");
    std::fs::write(repo.join("CLAUDE.md"), "ctx").unwrap();
    init_agents(&config_dir, "claude-code");
    register(&config_dir, &repo, "r");

    let out = repograph_cmd(&config_dir)
        .arg("context")
        .arg("--json")
        .assert()
        .success();
    let stdout = String::from_utf8(out.get_output().stdout.clone()).unwrap();
    // Strict: stdout must parse as JSON with no leading or trailing noise.
    let v: serde_json::Value = serde_json::from_str(&stdout).expect("stdout is exactly JSON");
    assert_eq!(v["schema_version"], 1);
}

// ─── non-TTY default is JSON (no flag needed) ───────────────────────────────

#[test]
fn non_tty_without_json_flag_emits_json() {
    // assert_cmd runs with stdout redirected to a pipe (non-TTY), so the
    // mode detector picks Json without --json.
    let tmp = TempDir::new().unwrap();
    let config_dir = tmp.path().join("config");
    let repo = fixture_git_repo(tmp.path(), "r");
    std::fs::write(repo.join("CLAUDE.md"), "ctx").unwrap();
    init_agents(&config_dir, "claude-code");
    register(&config_dir, &repo, "r");

    let out = repograph_cmd(&config_dir).arg("context").assert().success();
    let v: serde_json::Value =
        serde_json::from_slice(&out.get_output().stdout).expect("non-TTY default is JSON");
    assert_eq!(v["schema_version"], 1);
}

// ─── default scope echo ─────────────────────────────────────────────────────

#[test]
fn default_scope_echoes_kind_all_only() {
    let tmp = TempDir::new().unwrap();
    let config_dir = tmp.path().join("config");
    let repo = fixture_git_repo(tmp.path(), "r");
    std::fs::write(repo.join("CLAUDE.md"), "x").unwrap();
    init_agents(&config_dir, "claude-code");
    register(&config_dir, &repo, "r");

    let out = repograph_cmd(&config_dir)
        .arg("context")
        .arg("--json")
        .assert()
        .success();
    let v = parse_context_json(&out.get_output().stdout);
    assert_eq!(v["scope"]["kind"], "all");
    assert!(v["scope"].get("name").is_none(), "no name field for All");
    assert!(v["scope"].get("repos").is_none(), "no repos field for All");
}

// ─── large file body is verbatim, not truncated ─────────────────────────────

#[test]
fn large_claude_md_is_verbatim_in_payload() {
    let tmp = TempDir::new().unwrap();
    let config_dir = tmp.path().join("config");
    let repo = fixture_git_repo(tmp.path(), "r");
    // 50 KB body with predictable content.
    let body: String = "0123456789".repeat(5_120);
    std::fs::write(repo.join("CLAUDE.md"), &body).unwrap();
    init_agents(&config_dir, "claude-code");
    register(&config_dir, &repo, "r");

    let out = repograph_cmd(&config_dir)
        .arg("context")
        .arg("--json")
        .assert()
        .success();
    let v = parse_context_json(&out.get_output().stdout);
    let file = &v["repos"][0]["agent_docs"][0]["files"][0];
    assert_eq!(file["bytes"].as_u64().unwrap(), body.len() as u64);
    assert_eq!(file["content"].as_str().unwrap(), body);
}