git-paw 0.2.0

Parallel AI Worktrees — orchestrate multiple AI coding CLI sessions across git worktrees
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
//! Configuration file discovery and parsing integration tests.
//!
//! Tests config loading from repo-level `.git-paw/config.toml` files using real
//! temporary directories.

use std::fs;

use tempfile::TempDir;

use std::path::Path;

use git_paw::config::{
    PawConfig, add_custom_cli_to, load_config, load_config_from, remove_custom_cli_from,
    repo_config_path,
};

/// Writes content to the repo config path, creating parent directories as needed.
fn write_repo_config(repo_root: &Path, content: &str) {
    let path = repo_config_path(repo_root);
    fs::create_dir_all(path.parent().unwrap()).expect("create config dir");
    fs::write(&path, content).expect("write config");
}

// ---------------------------------------------------------------------------
// No config files
// ---------------------------------------------------------------------------

#[test]
fn load_config_returns_defaults_when_no_files_exist() {
    let tmp = TempDir::new().expect("create temp dir");
    let config = load_config(tmp.path()).expect("load config");

    assert_eq!(config.default_cli, None);
    assert_eq!(config.mouse, None);
    assert!(config.clis.is_empty());
    assert!(config.presets.is_empty());
}

// ---------------------------------------------------------------------------
// Repo config
// ---------------------------------------------------------------------------

#[test]
fn load_config_reads_repo_config() {
    let tmp = TempDir::new().expect("create temp dir");

    write_repo_config(
        tmp.path(),
        r#"
default_cli = "claude"
mouse = false
"#,
    );

    let config = load_config(tmp.path()).expect("load config");

    assert_eq!(config.default_cli, Some("claude".to_string()));
    assert_eq!(config.mouse, Some(false));
}

#[test]
fn repo_config_with_custom_clis() {
    let tmp = TempDir::new().expect("create temp dir");

    write_repo_config(
        tmp.path(),
        r#"
[clis.my-agent]
command = "/usr/local/bin/my-agent"
display_name = "My Agent"

[clis.local-llm]
command = "/usr/bin/ollama-code"
"#,
    );

    let config = load_config(tmp.path()).expect("load config");

    assert_eq!(config.clis.len(), 2);

    let my_agent = config.clis.get("my-agent").expect("my-agent should exist");
    assert_eq!(my_agent.command, "/usr/local/bin/my-agent");
    assert_eq!(my_agent.display_name, Some("My Agent".to_string()));

    let local_llm = config
        .clis
        .get("local-llm")
        .expect("local-llm should exist");
    assert_eq!(local_llm.command, "/usr/bin/ollama-code");
    assert_eq!(local_llm.display_name, None);
}

// ---------------------------------------------------------------------------
// Presets
// ---------------------------------------------------------------------------

#[test]
fn repo_config_with_presets() {
    let tmp = TempDir::new().expect("create temp dir");

    write_repo_config(
        tmp.path(),
        r#"
[presets.backend]
branches = ["feature/api", "fix/db"]
cli = "claude"

[presets.frontend]
branches = ["feature/ui"]
cli = "gemini"
"#,
    );

    let config = load_config(tmp.path()).expect("load config");

    assert_eq!(config.presets.len(), 2);

    let backend = config.get_preset("backend").expect("backend preset");
    assert_eq!(backend.branches, vec!["feature/api", "fix/db"]);
    assert_eq!(backend.cli, "claude");

    let frontend = config.get_preset("frontend").expect("frontend preset");
    assert_eq!(frontend.branches, vec!["feature/ui"]);
    assert_eq!(frontend.cli, "gemini");
}

#[test]
fn get_preset_returns_none_for_unknown() {
    let config = PawConfig::default();
    assert!(config.get_preset("nonexistent").is_none());
}

// ---------------------------------------------------------------------------
// Config merging behavior
// ---------------------------------------------------------------------------

#[test]
fn repo_config_overrides_default_fields() {
    let tmp = TempDir::new().expect("create temp dir");

    write_repo_config(
        tmp.path(),
        r#"
default_cli = "gemini"
mouse = true
"#,
    );

    let config = load_config(tmp.path()).expect("load config");

    // These should come from the repo config
    assert_eq!(config.default_cli, Some("gemini".to_string()));
    assert_eq!(config.mouse, Some(true));
}

// ---------------------------------------------------------------------------
// Config path
// ---------------------------------------------------------------------------

#[test]
fn repo_config_path_is_in_repo_root() {
    let tmp = TempDir::new().expect("create temp dir");
    let path = repo_config_path(tmp.path());
    assert_eq!(path, tmp.path().join(".git-paw").join("config.toml"));
}

// ---------------------------------------------------------------------------
// Malformed config
// ---------------------------------------------------------------------------

#[test]
fn malformed_toml_returns_error() {
    let tmp = TempDir::new().expect("create temp dir");

    write_repo_config(tmp.path(), "this is not valid { toml [[[");

    let result = load_config(tmp.path());
    assert!(result.is_err(), "malformed TOML should produce an error");
}

// ---------------------------------------------------------------------------
// All fields optional
// ---------------------------------------------------------------------------

#[test]
fn empty_config_file_is_valid() {
    let tmp = TempDir::new().expect("create temp dir");

    write_repo_config(tmp.path(), "");

    let config = load_config(tmp.path()).expect("load config");
    assert_eq!(config, PawConfig::default());
}

// ---------------------------------------------------------------------------
// Custom CLI management (add / remove)
// ---------------------------------------------------------------------------

#[test]
fn add_custom_cli_with_absolute_path() {
    let tmp = TempDir::new().expect("create temp dir");
    let config_path = tmp.path().join("config.toml");

    add_custom_cli_to(&config_path, "my-agent", "/usr/local/bin/my-agent", None).expect("add CLI");

    let config = load_config_from(&config_path, tmp.path()).expect("load");
    let cli = config.clis.get("my-agent").expect("CLI should exist");
    assert_eq!(cli.command, "/usr/local/bin/my-agent");
    assert_eq!(cli.display_name, None);
}

#[test]
fn add_custom_cli_with_display_name() {
    let tmp = TempDir::new().expect("create temp dir");
    let config_path = tmp.path().join("config.toml");

    add_custom_cli_to(
        &config_path,
        "my-agent",
        "/usr/local/bin/my-agent",
        Some("My Custom Agent"),
    )
    .expect("add CLI");

    let config = load_config_from(&config_path, tmp.path()).expect("load");
    let cli = config.clis.get("my-agent").expect("CLI should exist");
    assert_eq!(cli.display_name, Some("My Custom Agent".to_string()));
}

#[test]
fn add_multiple_custom_clis_preserves_all() {
    let tmp = TempDir::new().expect("create temp dir");
    let config_path = tmp.path().join("config.toml");

    add_custom_cli_to(&config_path, "agent-a", "/bin/agent-a", Some("Agent A")).expect("add first");
    add_custom_cli_to(&config_path, "agent-b", "/bin/agent-b", Some("Agent B"))
        .expect("add second");
    add_custom_cli_to(&config_path, "agent-c", "/bin/agent-c", Some("Agent C")).expect("add third");
    add_custom_cli_to(&config_path, "agent-d", "/bin/agent-d", None).expect("add fourth");

    let config = load_config_from(&config_path, tmp.path()).expect("load");
    assert_eq!(config.clis.len(), 4);
    assert!(config.clis.contains_key("agent-a"));
    assert!(config.clis.contains_key("agent-b"));
    assert!(config.clis.contains_key("agent-c"));
    assert!(config.clis.contains_key("agent-d"));

    assert_eq!(
        config.clis.get("agent-a").unwrap().display_name,
        Some("Agent A".to_string())
    );
    assert_eq!(config.clis.get("agent-d").unwrap().display_name, None);
}

#[test]
fn add_cli_overwrites_existing_entry() {
    let tmp = TempDir::new().expect("create temp dir");
    let config_path = tmp.path().join("config.toml");

    add_custom_cli_to(&config_path, "my-agent", "/bin/old-path", Some("Old Name"))
        .expect("add first");
    add_custom_cli_to(&config_path, "my-agent", "/bin/new-path", Some("New Name"))
        .expect("add second");

    let config = load_config_from(&config_path, tmp.path()).expect("load");
    assert_eq!(config.clis.len(), 1);
    let cli = config.clis.get("my-agent").unwrap();
    assert_eq!(cli.command, "/bin/new-path");
    assert_eq!(cli.display_name, Some("New Name".to_string()));
}

#[test]
fn add_cli_with_nonexistent_path_command_fails() {
    let tmp = TempDir::new().expect("create temp dir");
    let config_path = tmp.path().join("config.toml");

    // A non-absolute command that isn't on PATH should fail
    let result = add_custom_cli_to(
        &config_path,
        "bad-agent",
        "definitely-not-on-path-xyz",
        None,
    );
    assert!(result.is_err(), "should fail for command not on PATH");
}

#[test]
fn remove_custom_cli() {
    let tmp = TempDir::new().expect("create temp dir");
    let config_path = tmp.path().join("config.toml");

    add_custom_cli_to(&config_path, "agent-a", "/bin/a", None).expect("add a");
    add_custom_cli_to(&config_path, "agent-b", "/bin/b", None).expect("add b");

    remove_custom_cli_from(&config_path, "agent-a").expect("remove a");

    let config = load_config_from(&config_path, tmp.path()).expect("load");
    assert_eq!(config.clis.len(), 1);
    assert!(!config.clis.contains_key("agent-a"));
    assert!(config.clis.contains_key("agent-b"));
}

#[test]
fn remove_nonexistent_cli_returns_error() {
    let tmp = TempDir::new().expect("create temp dir");
    let config_path = tmp.path().join("config.toml");

    let result = remove_custom_cli_from(&config_path, "nonexistent");
    assert!(result.is_err());
}

#[test]
fn remove_all_custom_clis_leaves_empty_config() {
    let tmp = TempDir::new().expect("create temp dir");
    let config_path = tmp.path().join("config.toml");

    add_custom_cli_to(&config_path, "agent-a", "/bin/a", None).expect("add");
    remove_custom_cli_from(&config_path, "agent-a").expect("remove");

    let config = load_config_from(&config_path, tmp.path()).expect("load");
    assert!(config.clis.is_empty());
}

// ---------------------------------------------------------------------------
// Global + repo config merging with custom CLIs
// ---------------------------------------------------------------------------

#[test]
fn repo_custom_clis_merge_with_global_custom_clis() {
    let tmp = TempDir::new().expect("create temp dir");
    let global_path = tmp.path().join("global.toml");
    let repo_root = tmp.path().join("repo");
    fs::create_dir_all(&repo_root).expect("create repo dir");

    // Global config has two CLIs
    fs::write(
        &global_path,
        r#"
[clis.global-agent]
command = "/bin/global-agent"
display_name = "Global Agent"

[clis.shared-agent]
command = "/bin/global-shared"
"#,
    )
    .expect("write global");

    // Repo config has one CLI that overlaps and one new
    write_repo_config(
        &repo_root,
        r#"
[clis.shared-agent]
command = "/bin/repo-shared"
display_name = "Repo Shared"

[clis.repo-agent]
command = "/bin/repo-agent"
"#,
    );

    let config = load_config_from(&global_path, &repo_root).expect("load merged");

    // Should have 3 CLIs total: global-agent, shared-agent (repo wins), repo-agent
    assert_eq!(config.clis.len(), 3);

    // Global-only CLI preserved
    assert_eq!(
        config.clis.get("global-agent").unwrap().command,
        "/bin/global-agent"
    );

    // Repo CLI overrides global on collision
    let shared = config.clis.get("shared-agent").unwrap();
    assert_eq!(shared.command, "/bin/repo-shared");
    assert_eq!(shared.display_name, Some("Repo Shared".to_string()));

    // Repo-only CLI included
    assert!(config.clis.contains_key("repo-agent"));
}

// ---------------------------------------------------------------------------
// Config with many custom CLIs (stress test)
// ---------------------------------------------------------------------------

#[test]
fn config_with_many_custom_clis() {
    let tmp = TempDir::new().expect("create temp dir");

    let mut toml = String::new();
    for i in 0..10 {
        use std::fmt::Write;
        write!(
            toml,
            r#"
[clis.agent-{i}]
command = "/bin/agent-{i}"
display_name = "Agent {i}"
"#
        )
        .unwrap();
    }

    write_repo_config(tmp.path(), &toml);

    let config = load_config(tmp.path()).expect("load config");
    assert_eq!(config.clis.len(), 10);

    for i in 0..10 {
        let name = format!("agent-{i}");
        let cli = config
            .clis
            .get(&name)
            .unwrap_or_else(|| panic!("{name} missing"));
        assert_eq!(cli.command, format!("/bin/agent-{i}"));
        assert_eq!(cli.display_name, Some(format!("Agent {i}")));
    }
}