git-iris 2.1.0

AI-powered Git workflow assistant for smart commits, code reviews, changelogs, and release notes
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
419
420
421
422
423
424
425
426
427
use git_iris::common::CommonParams;
use git_iris::config::Config;
use git_iris::providers::ProviderConfig;
use std::collections::HashMap;
use std::env;
use std::fs;
use std::path::Path;
use std::process::Command;
use std::sync::{Mutex, MutexGuard, OnceLock};

// Use our centralized test infrastructure
#[path = "test_utils.rs"]
mod test_utils;
use test_utils::{MockDataBuilder, setup_git_repo};

fn cwd_lock() -> MutexGuard<'static, ()> {
    static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
    LOCK.get_or_init(|| Mutex::new(())).lock().expect("lock")
}

// Helper to verify git repo status
fn is_git_repo(dir: &Path) -> bool {
    let status = Command::new("git")
        .args(["rev-parse", "--is-inside-work-tree"])
        .current_dir(dir)
        .output()
        .expect("Failed to execute git command");

    status.status.success()
}

#[test]
fn test_project_config_security() {
    let _guard = cwd_lock();
    // Set up a git repository using our centralized infrastructure
    let (temp_dir, _git_repo) = setup_git_repo();

    // Save current directory so we can restore it later
    let original_dir = env::current_dir().expect("Failed to get current directory");

    // Change to the test repository directory for the test
    env::set_current_dir(temp_dir.path()).expect("Failed to change to test directory");

    // Verify we're in a git repo
    assert!(
        is_git_repo(Path::new(".")),
        "Current directory is not a git repository"
    );

    // 1. Test API key security in project config
    // Create a config with API keys using our MockDataBuilder
    let mut config = MockDataBuilder::config();

    // Add API keys to multiple providers
    for provider_name in &["openai", "anthropic", "google"] {
        let provider_config = ProviderConfig {
            api_key: format!("secret_{provider_name}_api_key"),
            model: format!("{provider_name}_model"),
            ..Default::default()
        };

        config
            .providers
            .insert((*provider_name).to_string(), provider_config);
    }

    // Save as project config
    config
        .save_as_project_config()
        .expect("Failed to save project config");

    // Verify the file exists
    let config_path = Path::new(".irisconfig");
    assert!(config_path.exists(), "Project config file not created");

    // Read the file content
    let content = fs::read_to_string(config_path).expect("Failed to read project config file");

    // Verify no API keys are in the file
    for provider_name in &["openai", "anthropic", "google"] {
        let api_key = format!("secret_{provider_name}_api_key");
        assert!(
            !content.contains(&api_key),
            "API key was found in project config file"
        );
    }

    // 2. Test merging project config with personal config
    // Create configs using our MockDataBuilder
    let mut personal_config =
        MockDataBuilder::test_config_with_api_key("openai", "personal_api_key");
    personal_config
        .providers
        .get_mut("openai")
        .expect("OpenAI provider should exist")
        .model = "gpt-5.4".to_string();

    let mut project_config = MockDataBuilder::config();
    let project_provider_config = ProviderConfig {
        api_key: String::new(), // Empty API key
        model: "gpt-5.4".to_string(),
        ..Default::default()
    };
    project_config
        .providers
        .insert("openai".to_string(), project_provider_config);

    // Merge configs
    personal_config.merge_with_project_config(project_config);

    // Verify API key from personal config is preserved
    let provider_config = personal_config
        .providers
        .get("openai")
        .expect("OpenAI provider config not found");
    assert_eq!(
        provider_config.api_key, "personal_api_key",
        "Personal API key was lost during merge"
    );

    // Verify model from project config is used
    assert_eq!(
        provider_config.model, "gpt-5.4",
        "Project model setting was not applied"
    );

    // 3. Test CLI command integration
    // Set up common parameters similar to CLI arguments
    let common = CommonParams {
        provider: Some("openai".to_string()),
        model: None,
        instructions: Some("Test instructions".to_string()),
        preset: Some("default".to_string()),
        gitmoji: Some(true),
        gitmoji_flag: false,
        no_gitmoji: false,
        critic_flag: false,
        no_critic: false,
        critic: None,
        repository_url: None,
    };

    // Create a config using our MockDataBuilder and apply common parameters
    let mut config = MockDataBuilder::config();
    common
        .apply_to_config(&mut config)
        .expect("Failed to apply common params");

    // Set an API key
    let provider_config = config
        .providers
        .get_mut("openai")
        .expect("OpenAI provider config not found");
    provider_config.api_key = "cli_integration_api_key".to_string();

    // Save as project config
    config
        .save_as_project_config()
        .expect("Failed to save project config with CLI params");

    // Read the file content
    let content = fs::read_to_string(config_path).expect("Failed to read project config file");

    // Verify the API key is not in the file
    assert!(
        !content.contains("cli_integration_api_key"),
        "API key from CLI integration was found in project config file"
    );

    // Clean up - restore original directory
    env::set_current_dir(original_dir).expect("Failed to restore original directory");
}

#[test]
fn test_project_config_only_serializes_changed_values() {
    // Test Config serialization directly without file system operations
    // This avoids current directory race conditions in parallel tests
    let config = Config {
        default_provider: String::new(),
        providers: HashMap::new(),
        use_gitmoji: false, // Explicitly changed from default
        instructions: String::new(),
        instruction_preset: "conventional".to_string(), // Explicitly changed from default
        theme: String::new(),
        subagent_timeout_secs: 120,
        subagent_max_turns: 20,
        critic_enabled: true,
        temp_instructions: None,
        temp_preset: None,
        is_project_config: true,
        gitmoji_override: None,
    };

    let content = toml::to_string_pretty(&config).expect("Failed to serialize config");

    // Should contain the explicitly set values
    assert!(
        content.contains("use_gitmoji = false"),
        "use_gitmoji should be serialized when false. Got:\n{content}"
    );
    assert!(
        content.contains(r#"instruction_preset = "conventional""#),
        "instruction_preset should be serialized when non-default. Got:\n{content}"
    );

    // Should NOT contain default/empty values
    assert!(
        !content.contains("default_provider"),
        "default_provider should NOT be serialized when empty. Got:\n{content}"
    );
    assert!(
        !content.contains("theme"),
        "theme should NOT be serialized when empty. Got:\n{content}"
    );
    assert!(
        !content.contains("subagent_timeout"),
        "subagent_timeout should NOT be serialized when default (120). Got:\n{content}"
    );
    assert!(
        !content.contains("subagent_max_turns"),
        "subagent_max_turns should NOT be serialized when default (20). Got:\n{content}"
    );
    assert!(
        !content.contains("instructions ="),
        "empty instructions should NOT be serialized. Got:\n{content}"
    );
    assert!(
        !content.contains("[providers]"),
        "empty providers table should NOT be serialized. Got:\n{content}"
    );
}

#[test]
fn test_project_config_with_provider_only_serializes_set_fields() {
    // Test Config serialization directly without file system operations
    // This avoids current directory race conditions in parallel tests
    let mut providers = HashMap::new();
    providers.insert(
        "anthropic".to_string(),
        ProviderConfig {
            api_key: String::new(),
            model: "claude-opus-4-6".to_string(),
            fast_model: None,
            token_limit: None,
            additional_params: HashMap::new(),
        },
    );

    let config = Config {
        default_provider: "anthropic".to_string(),
        providers,
        use_gitmoji: true, // default, should NOT serialize
        instructions: String::new(),
        instruction_preset: "default".to_string(), // default, should NOT serialize
        theme: String::new(),
        subagent_timeout_secs: 120,
        subagent_max_turns: 20,
        critic_enabled: true,
        temp_instructions: None,
        temp_preset: None,
        is_project_config: true,
        gitmoji_override: None,
    };

    let content = toml::to_string_pretty(&config).expect("Failed to serialize config");

    // Should contain provider and model
    assert!(
        content.contains(r#"default_provider = "anthropic""#),
        "default_provider should be serialized when set. Got:\n{content}"
    );
    assert!(
        content.contains("[providers.anthropic]"),
        "provider section should exist. Got:\n{content}"
    );
    assert!(
        content.contains(r#"model = "claude-opus-4-6""#),
        "model should be serialized. Got:\n{content}"
    );

    // Should NOT contain defaults
    assert!(
        !content.contains("use_gitmoji"),
        "use_gitmoji=true (default) should NOT be serialized. Got:\n{content}"
    );
    assert!(
        !content.contains("instruction_preset"),
        "instruction_preset=default should NOT be serialized. Got:\n{content}"
    );
    assert!(
        !content.contains("theme"),
        "empty theme should NOT be serialized. Got:\n{content}"
    );
    assert!(
        !content.contains("api_key"),
        "empty api_key should NOT be serialized. Got:\n{content}"
    );
    assert!(
        !content.contains("fast_model"),
        "None fast_model should NOT be serialized. Got:\n{content}"
    );
    assert!(
        !content.contains("token_limit"),
        "None token_limit should NOT be serialized. Got:\n{content}"
    );
}

#[test]
fn test_subagent_max_turns_serializes_when_changed() {
    let config = Config {
        subagent_max_turns: 35,
        ..Config::default()
    };

    let content = toml::to_string_pretty(&config).expect("Failed to serialize config");

    assert!(
        content.contains("subagent_max_turns = 35"),
        "subagent_max_turns should serialize when non-default. Got:\n{content}"
    );
}

#[test]
fn test_critic_enabled_serializes_when_disabled() {
    let config = Config {
        critic_enabled: false,
        ..Config::default()
    };

    let content = toml::to_string_pretty(&config).expect("Failed to serialize config");

    assert!(
        content.contains("critic_enabled = false"),
        "critic_enabled should serialize when disabled. Got:\n{content}"
    );
}

#[test]
fn test_common_params_can_disable_critic() {
    let common = CommonParams {
        no_critic: true,
        ..CommonParams::default()
    };
    let mut config = Config::default();

    common
        .apply_to_config(&mut config)
        .expect("Failed to apply common params");

    assert!(!config.critic_enabled);
}

#[test]
fn test_provider_config_skip_serialization() {
    // Test that ProviderConfig properly skips empty fields
    let config = ProviderConfig {
        api_key: String::new(),
        model: String::new(),
        fast_model: None,
        token_limit: None,
        additional_params: HashMap::new(),
    };

    let serialized = toml::to_string(&config).expect("Failed to serialize");

    // All fields are empty/None, should result in empty or minimal output
    assert!(
        !serialized.contains("api_key"),
        "empty api_key should not serialize"
    );
    assert!(
        !serialized.contains("model"),
        "empty model should not serialize"
    );
    assert!(
        !serialized.contains("fast_model"),
        "None fast_model should not serialize"
    );
    assert!(
        !serialized.contains("token_limit"),
        "None token_limit should not serialize"
    );
    assert!(
        !serialized.contains("additional_params"),
        "empty additional_params should not serialize"
    );
}

#[test]
fn test_provider_config_serializes_set_values() {
    let mut params = HashMap::new();
    params.insert("temperature".to_string(), "0.7".to_string());

    let config = ProviderConfig {
        api_key: String::new(), // Still empty, should skip
        model: "gpt-5.4".to_string(),
        fast_model: Some("gpt-5.4-mini".to_string()),
        token_limit: Some(4096),
        additional_params: params,
    };

    let serialized = toml::to_string(&config).expect("Failed to serialize");

    // Set values should appear
    assert!(
        serialized.contains(r#"model = "gpt-5.4""#),
        "model should serialize"
    );
    assert!(
        serialized.contains(r#"fast_model = "gpt-5.4-mini""#),
        "fast_model should serialize"
    );
    assert!(
        serialized.contains("token_limit = 4096"),
        "token_limit should serialize"
    );
    assert!(
        serialized.contains("temperature"),
        "additional_params should serialize"
    );

    // Empty api_key should NOT appear
    assert!(
        !serialized.contains("api_key"),
        "empty api_key should not serialize"
    );
}