drep-ai 3.0.0

A local commit gate: runs the linters your repo configures, and sends changed code to an LLM for review
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
//! B17, B18, B19: orchestration. `run_to` against fresh `git init` and
//! against a non-repo directory.

use crate::cli::init::{HookKind, InitArgs, run_with};

use super::support::auth_path;

#[tokio::test]
async fn run_to_on_a_non_repo_directory_errors_and_writes_no_toml() {
    let dir = tempfile::tempdir().expect("tempdir");
    // `tempfile::tempdir` lives under /tmp, which is not inside the drep
    // git repo, so `git rev-parse --show-toplevel` fails there.

    let mut out = Vec::new();
    let args = InitArgs {
        path: dir.path().to_path_buf(),
        provider: Some("local".to_owned()),
        model: None,
        endpoint: None,
        hooks: HookKind::None,
        force: false,
        // These tests predate the wizard and drive the flag path.
        no_gitignore: true,
        non_interactive: true,
        interactive: false,
    };
    let result = run_with(&mut out, &args, &auth_path(&dir)).await;
    let err = result.expect_err("non-repo returns Err");
    let msg = format!("{err:#}");
    assert!(msg.contains("not inside a git repository"), "msg: {msg}");

    let toml = dir.path().join("drep.toml");
    assert!(!toml.exists(), "no drep.toml should be written on Err");
}

#[tokio::test]
async fn custom_provider_without_endpoint_or_model_errors() {
    let dir = tempfile::tempdir().expect("tempdir");
    crate::test_support::git_init(dir.path());

    // No endpoint, no model.
    let mut out = Vec::new();
    let result = run_with(
        &mut out,
        &InitArgs {
            path: dir.path().to_path_buf(),
            provider: Some("custom".to_owned()),
            model: None,
            endpoint: None,
            hooks: HookKind::None,
            force: false,
            // These tests predate the wizard and drive the flag path.
            no_gitignore: true,
            non_interactive: true,
            interactive: false,
        },
        &auth_path(&dir),
    )
    .await;
    let err = result.expect_err("custom needs --endpoint");
    let msg = format!("{err:#}");
    assert!(msg.contains("--endpoint"), "msg: {msg}");
    assert!(
        !dir.path().join("drep.toml").exists(),
        "no drep.toml should be written on Err"
    );

    // Endpoint present, no model.
    let mut out = Vec::new();
    let result = run_with(
        &mut out,
        &InitArgs {
            path: dir.path().to_path_buf(),
            provider: Some("custom".to_owned()),
            model: None,
            endpoint: Some("http://x/v1".to_owned()),
            hooks: HookKind::None,
            force: false,
            // These tests predate the wizard and drive the flag path.
            no_gitignore: true,
            non_interactive: true,
            interactive: false,
        },
        &auth_path(&dir),
    )
    .await;
    let err = result.expect_err("custom needs --model");
    let msg = format!("{err:#}");
    assert!(msg.contains("--model"), "msg: {msg}");
}

#[tokio::test]
async fn run_to_with_local_and_both_hooks_writes_toml_and_hooks() {
    let dir = tempfile::tempdir().expect("tempdir");
    crate::test_support::git_init(dir.path());

    let mut out = Vec::new();
    let result = run_with(
        &mut out,
        &InitArgs {
            path: dir.path().to_path_buf(),
            provider: Some("local".to_owned()),
            model: None,
            endpoint: None,
            hooks: HookKind::Both,
            force: false,
            // These tests predate the wizard and drive the flag path.
            no_gitignore: true,
            non_interactive: true,
            interactive: false,
        },
        &auth_path(&dir),
    )
    .await;
    assert!(result.is_ok(), "run_to: {result:?}");

    let toml = dir.path().join("drep.toml");
    assert!(toml.exists(), "drep.toml was written");

    let pre_commit = dir.path().join(".git/hooks/pre-commit");
    let pre_push = dir.path().join(".git/hooks/pre-push");
    assert!(pre_commit.exists(), ".git/hooks/pre-commit was written");
    assert!(pre_push.exists(), ".git/hooks/pre-push was written");

    let rendered = String::from_utf8(out).expect("utf8");
    assert!(
        rendered.contains("qwen3-30b-a3b"),
        "captured output names the preset's default model; rendered:\n{rendered}"
    );
    // local preset has no api_key_env, so the export reminder must be absent.
    assert!(
        !rendered.contains("export "),
        "local preset has no api_key_env; no export reminder expected; rendered:\n{rendered}"
    );

    // B19 second half: a fresh repo with `--provider openrouter` writes the
    // export reminder naming `OPENROUTER_API_KEY`. Spec says "in the same
    // test", so the two halves share one `#[tokio::test]`.
    let dir = tempfile::tempdir().expect("tempdir");
    crate::test_support::git_init(dir.path());

    let mut out = Vec::new();
    let result = run_with(
        &mut out,
        &InitArgs {
            path: dir.path().to_path_buf(),
            provider: Some("openrouter".to_owned()),
            model: None,
            endpoint: None,
            hooks: HookKind::None,
            force: false,
            // These tests predate the wizard and drive the flag path.
            no_gitignore: true,
            non_interactive: true,
            interactive: false,
        },
        &auth_path(&dir),
    )
    .await;
    assert!(result.is_ok(), "run_to: {result:?}");

    let rendered = String::from_utf8(out).expect("utf8");
    assert!(
        rendered.contains("OPENROUTER_API_KEY"),
        "openrouter preset must name its api_key_env; rendered:\n{rendered}"
    );
}

/// An explicit `--model`/`--endpoint` wins over the preset's default.
///
/// Every existing case passed `None` for both, or used `custom` (whose preset
/// values are themselves `None`), so `args.model.or(preset.default_model)`
/// could be inverted to `preset.default_model.or(args.model)` and the suite
/// stayed green - meaning `drep init --provider local --model qwen-x` silently
/// writing `qwen3-30b-a3b` was untestable by omission.
#[tokio::test]
async fn an_explicit_model_and_endpoint_override_the_presets_defaults() {
    let dir = tempfile::tempdir().expect("tempdir");
    crate::test_support::git_init(dir.path());

    let mut out: Vec<u8> = Vec::new();
    run_with(
        &mut out,
        &InitArgs {
            path: dir.path().to_path_buf(),
            provider: Some("local".to_owned()),
            model: Some("my-own-model".to_owned()),
            endpoint: Some("http://elsewhere:9999/v1".to_owned()),
            hooks: HookKind::None,
            force: false,
            // These tests predate the wizard and drive the flag path.
            no_gitignore: true,
            non_interactive: true,
            interactive: false,
        },
        &auth_path(&dir),
    )
    .await
    .expect("run_to");

    let written = std::fs::read_to_string(dir.path().join("drep.toml")).expect("drep.toml");
    assert!(
        written.contains("model = \"my-own-model\""),
        "the flag wins over the preset default; wrote:\n{written}"
    );
    assert!(
        written.contains("endpoint = \"http://elsewhere:9999/v1\""),
        "same for the endpoint; wrote:\n{written}"
    );
    assert!(
        !written.contains("qwen3-30b-a3b") && !written.contains("localhost:1234"),
        "and the preset's own values must not appear; wrote:\n{written}"
    );
}

/// `drep.toml` and the hooks land at the **git toplevel**, not at `--path`.
///
/// Every other test passes the repository root as `--path`, so `args.path ==
/// root` always and `config_file::write(&args.path, ..)` would pass the whole
/// suite - while `drep init --path src/` scattered a config into a
/// subdirectory where nothing would ever look for it.
#[tokio::test]
async fn init_writes_at_the_git_toplevel_not_at_the_path_argument() {
    let dir = tempfile::tempdir().expect("tempdir");
    crate::test_support::git_init(dir.path());
    let nested = dir.path().join("src").join("deep");
    std::fs::create_dir_all(&nested).expect("nested dir");

    let mut out: Vec<u8> = Vec::new();
    run_with(
        &mut out,
        &InitArgs {
            path: nested.clone(),
            provider: Some("local".to_owned()),
            model: None,
            endpoint: None,
            hooks: HookKind::PrePush,
            force: false,
            // These tests predate the wizard and drive the flag path.
            no_gitignore: true,
            non_interactive: true,
            interactive: false,
        },
        &auth_path(&dir),
    )
    .await
    .expect("run_to");

    // `canonicalize` because macOS reports /var as a symlink to /private/var,
    // and the toplevel git prints is the resolved form.
    let root = dir.path().canonicalize().expect("canonical");
    assert!(
        root.join("drep.toml").is_file(),
        "the config belongs at the repository root"
    );
    assert!(
        !nested.join("drep.toml").exists(),
        "and not beside the path the user happened to name"
    );
    assert!(
        root.join(".git").join("hooks").join("pre-push").is_file(),
        "the hook belongs in the repository's hooks dir"
    );
}

/// `--hooks none` writes the config and touches nothing else.
///
/// The documented contract is that it "must not create directories or ask git
/// anything" - an escape hatch with side effects is not one. Deleting the
/// early return passed every test, because nothing asserted on `.git/hooks`
/// after a `None` run.
#[tokio::test]
async fn hooks_none_writes_the_config_and_installs_nothing() {
    let dir = tempfile::tempdir().expect("tempdir");
    crate::test_support::git_init(dir.path());

    let mut out: Vec<u8> = Vec::new();
    run_with(
        &mut out,
        &InitArgs {
            path: dir.path().to_path_buf(),
            provider: Some("local".to_owned()),
            model: None,
            endpoint: None,
            hooks: HookKind::None,
            force: false,
            // These tests predate the wizard and drive the flag path.
            no_gitignore: true,
            non_interactive: true,
            interactive: false,
        },
        &auth_path(&dir),
    )
    .await
    .expect("run_to");

    let root = dir.path().canonicalize().expect("canonical");
    assert!(
        root.join("drep.toml").is_file(),
        "the config is still written"
    );
    for name in ["pre-push", "pre-commit"] {
        assert!(
            !root.join(".git").join("hooks").join(name).exists(),
            "--hooks none must install no {name}"
        );
    }
}

/// A provider needing no key produces no environment section at all.
///
/// Deleting the emptiness guard would print the heading with nothing under it,
/// which reads as "something is missing" for a setup that is complete. Asserted
/// alongside the positive case, because either alone passes with the guard
/// inverted.
#[tokio::test]
async fn the_environment_section_appears_only_when_a_variable_is_needed() {
    async fn report(provider: &str) -> String {
        let dir = tempfile::tempdir().expect("tempdir");
        crate::test_support::git_init(dir.path());
        let mut out = Vec::new();
        run_with(
            &mut out,
            &InitArgs {
                path: dir.path().to_path_buf(),
                provider: Some(provider.to_owned()),
                model: None,
                endpoint: None,
                hooks: HookKind::None,
                force: false,
                no_gitignore: true,
                non_interactive: true,
                interactive: false,
            },
            &auth_path(&dir),
        )
        .await
        .expect("init succeeds");
        String::from_utf8(out).expect("utf8")
    }

    const HEADING: &str = "reads its key from the environment";

    let local = report("local").await;
    assert!(
        !local.contains(HEADING),
        "a local server needs no variable, so there is nothing to head: {local}"
    );

    let cloud = report("openrouter").await;
    assert!(
        cloud.contains(HEADING),
        "and a cloud provider does: {cloud}"
    );
    assert!(cloud.contains("OPENROUTER_API_KEY"), "named: {cloud}");
}

/// A key already held suppresses the environment section entirely.
///
/// The rendered block carries no `api_key` line in that case, so the
/// environment is never consulted and naming a variable would be misleading.
#[tokio::test]
async fn a_stored_key_leaves_the_environment_unmentioned() {
    let dir = tempfile::tempdir().expect("tempdir");
    crate::test_support::git_init(dir.path());

    let mut store = crate::auth::AuthStore::new();
    store
        .set("https://openrouter.ai/api/v1", "sk-held")
        .expect("set");
    store.save(&auth_path(&dir)).expect("save");

    let mut out = Vec::new();
    run_with(
        &mut out,
        &InitArgs {
            path: dir.path().to_path_buf(),
            provider: Some("openrouter".to_owned()),
            model: None,
            endpoint: None,
            hooks: HookKind::None,
            force: false,
            no_gitignore: true,
            non_interactive: true,
            interactive: false,
        },
        &auth_path(&dir),
    )
    .await
    .expect("init succeeds");

    let rendered = String::from_utf8(out).expect("utf8");
    assert!(
        !rendered.contains("OPENROUTER_API_KEY"),
        "the key is held, so the variable is irrelevant: {rendered}"
    );

    let config = std::fs::read_to_string(dir.path().join("drep.toml")).expect("config");
    // By line, not substring: the file's own header comment explains the
    // `api_key = "${VAR}"` escape hatch, so a naive `contains` matches the
    // documentation rather than an assignment.
    assert!(
        !config
            .lines()
            .any(|line| line.trim_start().starts_with("api_key")),
        "no api_key assignment, which would override the stored key: {config}"
    );
}