repograph 0.3.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
//! Acceptance tests for the per-agent artifact install layer.
//!
//! Drives the `repograph init` binary end-to-end and asserts the right files
//! land at the right paths for each agent in the v1 matrix, that re-runs are
//! idempotent, that `--force` rewrites the file fresh, that pre-existing user
//! content is preserved by default, and that the stdout contract holds.
//!
//! The settings-panel `Update agent selection` flow shares `run_install` with
//! the non-interactive path, so its behavior is exercised end-to-end here via
//! chained `--no-prompt` invocations. The cliclack UX itself is covered by
//! the manual validation script (see `openspec/changes/agent-skills/tasks.md`
//! task 14.5).

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

mod common;

use std::path::PathBuf;

use assert_cmd::Command;
use predicates::prelude::*;
use tempfile::TempDir;

use crate::common::repograph_cmd;

/// One tempdir housing `home/`, `proj/`, and `config/` so a single test can
/// drive the binary with a fully isolated environment: `HOME` for user-scope
/// paths, `current_dir` for project-scope paths, and `REPOGRAPH_CONFIG_DIR`
/// for the config file. Holds the `TempDir` so it survives until the test
/// drops the fixture.
struct InitFixture {
    _tmp: TempDir,
    home: PathBuf,
    proj: PathBuf,
    config: PathBuf,
}

impl InitFixture {
    fn new() -> Self {
        let tmp = TempDir::new().unwrap();
        let home = tmp.path().join("home");
        let proj = tmp.path().join("proj");
        let config = tmp.path().join("config");
        std::fs::create_dir_all(&home).unwrap();
        std::fs::create_dir_all(&proj).unwrap();
        Self {
            _tmp: tmp,
            home,
            proj,
            config,
        }
    }

    /// Build a `repograph` command with `HOME`, `current_dir`, and
    /// `REPOGRAPH_CONFIG_DIR` wired so every install path resolves under the
    /// fixture. `USERPROFILE` is also set for Windows-host parity (the binary
    /// uses `dirs::home_dir()` which prefers `USERPROFILE` on Windows).
    fn cmd(&self) -> Command {
        let mut cmd = repograph_cmd(&self.config);
        cmd.env("HOME", &self.home)
            .env("USERPROFILE", &self.home)
            .current_dir(&self.proj);
        cmd
    }
}

/// Read a UTF-8 file, panicking on failure (acceptable in tests).
fn read(path: &PathBuf) -> String {
    std::fs::read_to_string(path).unwrap()
}

// ---- per-scope, per-agent install paths --------------------------------

#[test]
fn claude_code_user_scope_writes_skill_md_under_home() {
    let f = InitFixture::new();
    f.cmd()
        .args(["init", "--no-prompt", "--agents", "claude-code"])
        .args(["--scope", "user"])
        .assert()
        .success();

    let target = f.home.join(".claude/skills/repograph/SKILL.md");
    assert!(target.exists(), "expected {target:?} to exist");
    let body = read(&target);
    assert!(
        body.starts_with("---\nname: repograph\n"),
        "missing YAML frontmatter, got:\n{body}",
    );
    assert!(
        body.contains("description: >-"),
        "missing folded-scalar description in frontmatter, got:\n{body}",
    );
    assert!(
        body.contains("ALWAYS prefer this over manual"),
        "description should steer the agent away from manual find/git, got:\n{body}",
    );
    assert!(
        body.contains("<!-- repograph:begin -->"),
        "missing begin delimiter, got:\n{body}",
    );
    assert!(
        body.contains("<!-- repograph:end -->"),
        "missing end delimiter, got:\n{body}",
    );
    assert!(
        body.contains("repograph context"),
        "managed body should mention `repograph context`, got:\n{body}",
    );
}

#[test]
fn claude_code_project_scope_writes_skill_md_under_cwd() {
    let f = InitFixture::new();
    f.cmd()
        .args(["init", "--no-prompt", "--agents", "claude-code"])
        .args(["--scope", "project"])
        .assert()
        .success();

    let target = f.proj.join(".claude/skills/repograph/SKILL.md");
    assert!(target.exists(), "expected {target:?} to exist");
    // The user-scope counterpart MUST NOT have been created.
    assert!(
        !f.home.join(".claude/skills/repograph/SKILL.md").exists(),
        "user-scope path should be untouched under --scope project",
    );
}

#[test]
fn agents_md_no_scope_required_writes_to_cwd() {
    let f = InitFixture::new();
    f.cmd()
        .args(["init", "--no-prompt", "--agents", "agents-md"])
        .assert()
        .success();

    let target = f.proj.join("AGENTS.md");
    assert!(target.exists(), "expected {target:?} to exist");
    let body = read(&target);
    assert!(
        body.starts_with("<!-- repograph:begin -->"),
        "agents-md begins with the delimiter (no frontmatter), got:\n{body}",
    );
    assert!(body.contains("# repograph"), "managed heading present");
}

#[test]
fn project_only_agent_falls_through_under_user_scope() {
    let f = InitFixture::new();
    let output = f
        .cmd()
        .args(["init", "--no-prompt", "--agents", "agents-md"])
        .args(["--scope", "user"])
        .output()
        .unwrap();
    assert!(output.status.success(), "init should succeed");

    // File still lands at the project path because agents-md is project-only.
    let target = f.proj.join("AGENTS.md");
    assert!(target.exists(), "expected fall-through to project path");
    // The stderr install summary names the resolved path so an operator can
    // see the fall-through happened.
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("AGENTS.md"),
        "stderr should mention the resolved path; got:\n{stderr}",
    );
    // No artifact at the user-scope location.
    assert!(
        !f.home.join("AGENTS.md").exists(),
        "user-scope AGENTS.md must not exist",
    );
}

#[test]
fn copilot_selection_succeeds_with_no_artifact() {
    let f = InitFixture::new();
    let output = f
        .cmd()
        .args(["init", "--no-prompt", "--agents", "copilot"])
        .output()
        .unwrap();
    assert!(output.status.success(), "copilot is a valid selection");

    // Config gets [agents] selected = ["copilot"], no artifact files.
    let cfg = read(&f.config.join("config.toml"));
    assert!(cfg.contains("copilot"), "config records copilot:\n{cfg}");

    // No artifact files anywhere under home or proj for v1.
    for p in [
        f.home.join(".claude/skills/repograph/SKILL.md"),
        f.proj.join(".claude/skills/repograph/SKILL.md"),
        f.proj.join("AGENTS.md"),
        f.proj.join("CONVENTIONS.md"),
        f.proj.join(".cursor/rules/repograph.mdc"),
        f.proj.join(".windsurfrules"),
        f.home.join(".codeium/windsurf/memories/repograph.md"),
    ] {
        assert!(
            !p.exists(),
            "no artifact should exist for copilot, found {p:?}"
        );
    }
}

#[test]
fn scope_bearing_agent_without_scope_under_no_prompt_exits_2() {
    let f = InitFixture::new();
    let output = f
        .cmd()
        .args(["init", "--no-prompt", "--agents", "claude-code"])
        .output()
        .unwrap();

    assert_eq!(
        output.status.code(),
        Some(2),
        "missing --scope for scope-bearing agent must exit 2"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("--scope"),
        "stderr names --scope:\n{stderr}"
    );
    assert!(
        stderr.contains("claude-code"),
        "stderr names the offending agent:\n{stderr}",
    );
    // No config written, no artifact written.
    assert!(
        !f.config.join("config.toml").exists(),
        "no config write on usage error",
    );
    assert!(
        !f.home.join(".claude/skills/repograph/SKILL.md").exists(),
        "no artifact write on usage error",
    );
}

// ---- idempotency & user-content preservation ---------------------------

#[test]
fn re_run_is_idempotent_byte_for_byte() {
    let f = InitFixture::new();
    f.cmd()
        .args(["init", "--no-prompt", "--agents", "agents-md"])
        .assert()
        .success();
    let target = f.proj.join("AGENTS.md");
    let first = read(&target);

    let output = f
        .cmd()
        .args(["init", "--no-prompt", "--agents", "agents-md"])
        .output()
        .unwrap();
    assert!(output.status.success(), "second init succeeds");

    let second = read(&target);
    assert_eq!(first, second, "file must be byte-stable across re-runs");

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("artifact unchanged"),
        "stderr should announce Unchanged on re-run; got:\n{stderr}",
    );
}

#[test]
fn pre_existing_user_content_is_preserved() {
    let f = InitFixture::new();
    let target = f.proj.join("AGENTS.md");
    std::fs::write(&target, "# My project\n\nCustom prose.\n").unwrap();

    f.cmd()
        .args(["init", "--no-prompt", "--agents", "agents-md"])
        .assert()
        .success();

    let body = read(&target);
    assert!(
        body.starts_with("# My project\n\nCustom prose.\n"),
        "user prose preserved at top, got:\n{body}",
    );
    assert!(
        body.contains("<!-- repograph:begin -->"),
        "managed block appended, got:\n{body}",
    );
    assert!(
        body.contains("<!-- repograph:end -->"),
        "managed block closed, got:\n{body}",
    );
}

#[test]
fn force_overwrites_pre_existing_user_content() {
    let f = InitFixture::new();
    let target = f.proj.join("AGENTS.md");
    std::fs::write(&target, "# My project\n\nCustom prose.\n").unwrap();

    f.cmd()
        .args(["init", "--no-prompt", "--agents", "agents-md", "--force"])
        .assert()
        .success();

    let body = read(&target);
    assert!(
        body.starts_with("<!-- repograph:begin -->"),
        "force replaced the file with the bare delimited block, got:\n{body}",
    );
    assert!(
        !body.contains("Custom prose."),
        "user content removed under --force, got:\n{body}",
    );
}

// ---- multi-agent & stdout contract --------------------------------------

#[test]
fn multi_agent_install_lands_each_at_its_matrix_path() {
    let f = InitFixture::new();
    f.cmd()
        .args([
            "init",
            "--no-prompt",
            "--agents",
            "claude-code,agents-md,cursor",
        ])
        .args(["--scope", "user"])
        .assert()
        .success();

    // claude-code under user-scope.
    assert!(
        f.home.join(".claude/skills/repograph/SKILL.md").exists(),
        "claude-code user-scope path missing",
    );
    // agents-md and cursor fall through to project-scope.
    assert!(f.proj.join("AGENTS.md").exists(), "agents-md path missing");
    assert!(
        f.proj.join(".cursor/rules/repograph.mdc").exists(),
        "cursor path missing",
    );
}

#[test]
fn stdout_is_empty_for_artifact_install() {
    let f = InitFixture::new();
    let output = f
        .cmd()
        .args(["init", "--no-prompt", "--agents", "claude-code"])
        .args(["--scope", "user"])
        .output()
        .unwrap();
    assert!(output.status.success(), "init succeeds");
    assert!(
        output.stdout.is_empty(),
        "stdout must be empty for init (data contract); got: {}",
        String::from_utf8_lossy(&output.stdout),
    );
    // Per-result lines land on stderr (info!).
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("artifact written"),
        "stderr should announce the Written outcome; got:\n{stderr}",
    );
}

#[test]
fn invalid_scope_value_exits_2_with_empty_stdout() {
    let f = InitFixture::new();
    f.cmd()
        .args(["init", "--no-prompt", "--agents", "claude-code"])
        .args(["--scope", "bogus"])
        .assert()
        .failure()
        .code(2)
        .stdout(predicate::str::is_empty())
        .stderr(predicate::str::contains("user").and(predicate::str::contains("project")));
}

#[test]
fn windsurf_user_scope_writes_under_codeium_dir() {
    let f = InitFixture::new();
    f.cmd()
        .args(["init", "--no-prompt", "--agents", "windsurf"])
        .args(["--scope", "user"])
        .assert()
        .success();

    let target = f.home.join(".codeium/windsurf/memories/repograph.md");
    assert!(target.exists(), "windsurf user-scope path missing");
    let body = read(&target);
    assert!(body.contains("<!-- repograph:begin -->"));
}

#[test]
fn windsurf_project_scope_writes_windsurfrules() {
    let f = InitFixture::new();
    f.cmd()
        .args(["init", "--no-prompt", "--agents", "windsurf"])
        .args(["--scope", "project"])
        .assert()
        .success();

    let target = f.proj.join(".windsurfrules");
    assert!(target.exists(), "windsurf project-scope path missing");
}

#[test]
fn switching_selection_writes_new_artifact_but_leaves_old_one() {
    // The settings-panel `Update agent selection` arm and `run_non_interactive`
    // both flow through the same `run_install` helper, so chained
    // `--no-prompt` invocations are a faithful end-to-end stand-in. The
    // cliclack UX itself is covered by the manual validation script.
    let f = InitFixture::new();
    f.cmd()
        .args(["init", "--no-prompt", "--agents", "claude-code"])
        .args(["--scope", "project"])
        .assert()
        .success();
    let old = f.proj.join(".claude/skills/repograph/SKILL.md");
    assert!(old.exists(), "claude-code artifact written on first init");

    f.cmd()
        .args(["init", "--no-prompt", "--agents", "agents-md"])
        .assert()
        .success();

    let new = f.proj.join("AGENTS.md");
    assert!(new.exists(), "agents-md artifact written on second init");
    // Documented behavior: previous artifacts are NOT removed when the
    // selection changes; only the currently-selected agents get a write.
    assert!(
        old.exists(),
        "previous claude-code artifact must be left in place",
    );
}