opencrabs 0.3.16

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Install with: cargo install opencrabs
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
//! GitHub Copilot provider integration tests.
//!
//! Tests config resolution, provider indices, factory wiring,
//! extra headers on OpenAIProvider, and onboarding integration.

use crate::brain::Provider;
use crate::brain::provider::custom_openai_compatible::OpenAIProvider;
use crate::config::{Config, ProviderConfig, ProviderConfigs, resolve_provider_from_config};
use crate::tui::onboarding::{OnboardingWizard, PROVIDERS};

// ── Provider array ──────────────────────────────────────────────

#[test]
fn github_models_is_at_index_2() {
    assert_eq!(PROVIDERS[2].name, "GitHub Copilot");
}

#[test]
fn github_models_has_no_static_models() {
    assert!(PROVIDERS[2].models.is_empty());
}

#[test]
fn github_copilot_key_label() {
    assert_eq!(PROVIDERS[2].key_label, "OAuth");
}

#[test]
fn github_copilot_has_help_lines() {
    assert!(!PROVIDERS[2].help_lines.is_empty());
    let help = PROVIDERS[2].help_lines.join(" ");
    assert!(help.contains("Copilot"));
}

#[test]
fn provider_order_after_github_insertion() {
    assert_eq!(PROVIDERS[0].name, "Anthropic Claude");
    assert_eq!(PROVIDERS[1].name, "OpenAI");
    assert_eq!(PROVIDERS[2].name, "GitHub Copilot");
    assert_eq!(PROVIDERS[3].name, "Google Gemini");
    assert_eq!(PROVIDERS[4].name, "OpenRouter");
    assert_eq!(PROVIDERS[5].name, "Minimax");
    assert_eq!(PROVIDERS[6].name, "z.ai GLM");
    assert_eq!(PROVIDERS[7].name, "Claude CLI");
    assert_eq!(PROVIDERS[8].name, "OpenCode CLI");
    assert_eq!(PROVIDERS[9].name, "OpenCode");
    assert_eq!(PROVIDERS[10].name, "Qwen");
    assert_eq!(PROVIDERS[11].name, "Ollama");
    assert_eq!(PROVIDERS[12].name, "Custom OpenAI-Compatible");
}

// ── Config struct ───────────────────────────────────────────────

#[test]
fn provider_configs_has_github_field() {
    let configs = ProviderConfigs::default();
    assert!(configs.github.is_none());
}

#[test]
fn provider_configs_github_round_trip() {
    let configs = ProviderConfigs {
        github: Some(ProviderConfig {
            enabled: true,
            default_model: Some("gpt-4o".to_string()),
            ..Default::default()
        }),
        ..Default::default()
    };
    assert!(configs.github.as_ref().unwrap().enabled);
    assert_eq!(
        configs.github.as_ref().unwrap().default_model.as_deref(),
        Some("gpt-4o")
    );
}

// ── resolve_provider_from_config ────────────────────────────────

#[test]
fn resolve_github_when_enabled() {
    let mut config = Config::default();
    config.providers.github = Some(ProviderConfig {
        enabled: true,
        default_model: Some("gpt-4o".to_string()),
        ..Default::default()
    });
    let (name, model) = resolve_provider_from_config(&config);
    assert_eq!(name, "GitHub Copilot");
    assert_eq!(model, "gpt-4o");
}

#[test]
fn resolve_github_default_model_when_none() {
    let mut config = Config::default();
    config.providers.github = Some(ProviderConfig {
        enabled: true,
        default_model: None,
        ..Default::default()
    });
    let (name, model) = resolve_provider_from_config(&config);
    assert_eq!(name, "GitHub Copilot");
    assert_eq!(model, "gpt-5-mini");
}

#[test]
fn resolve_skips_github_when_disabled() {
    let mut config = Config::default();
    config.providers.github = Some(ProviderConfig {
        enabled: false,
        ..Default::default()
    });
    let (name, _) = resolve_provider_from_config(&config);
    assert_ne!(name, "GitHub Copilot");
}

#[test]
fn resolve_skips_github_when_absent() {
    let config = Config::default();
    assert!(config.providers.github.is_none());
    let (name, _) = resolve_provider_from_config(&config);
    assert_ne!(name, "GitHub Copilot");
}

// ── OpenAIProvider extra_headers ─────────────────────────────────

#[test]
fn extra_headers_builder_sets_headers() {
    use crate::brain::provider::copilot::copilot_extra_headers;
    let headers = copilot_extra_headers();
    let provider = OpenAIProvider::with_base_url(
        "copilot-managed".to_string(),
        "https://api.githubcopilot.com/chat/completions".to_string(),
    )
    .with_extra_headers(headers);
    assert!(!provider.extra_headers.is_empty());
    assert!(
        provider
            .extra_headers
            .iter()
            .any(|(k, _)| k == "copilot-integration-id")
    );
}

#[test]
fn extra_headers_default_empty() {
    let provider = OpenAIProvider::with_base_url(
        "test-key".to_string(),
        "https://api.openai.com/v1/chat/completions".to_string(),
    );
    assert!(provider.extra_headers.is_empty());
}

#[test]
fn with_name_sets_provider_name() {
    let provider = OpenAIProvider::with_base_url(
        "copilot-managed".to_string(),
        "https://api.githubcopilot.com/chat/completions".to_string(),
    )
    .with_name("GitHub Copilot");
    assert_eq!(provider.name(), "GitHub Copilot");
}

// ── Onboarding wizard ──────────────────────────────────────────

#[test]
fn wizard_github_is_not_custom() {
    let mut wizard = OnboardingWizard::new();
    wizard.ps.selected_provider = 2;
    assert!(!wizard.ps.is_custom());
}

#[test]
fn wizard_github_supports_model_fetch() {
    // GitHub Copilot supports live model fetching via /models endpoint
    let mut wizard = OnboardingWizard::new();
    wizard.ps.selected_provider = 2;
    assert!(wizard.ps.supports_model_fetch());
}

#[test]
fn wizard_current_provider_at_index_2_is_github() {
    let mut wizard = OnboardingWizard::new();
    wizard.ps.selected_provider = 2;
    assert_eq!(wizard.ps.current_provider().name, "GitHub Copilot");
}

#[test]
fn wizard_model_filter_works_for_github() {
    let mut wizard = OnboardingWizard::new();
    wizard.ps.selected_provider = 2;
    wizard.ps.models = vec![
        "gpt-4o".to_string(),
        "gpt-4o-mini".to_string(),
        "Llama-3.3-70B-Instruct".to_string(),
    ];
    wizard.ps.model_filter = "gpt".to_string();
    let filtered = wizard.ps.filtered_model_names();
    assert_eq!(filtered.len(), 2);
    assert!(filtered.iter().all(|m| m.contains("gpt")));
}

#[test]
fn wizard_all_model_names_uses_fetched_first() {
    let mut wizard = OnboardingWizard::new();
    wizard.ps.selected_provider = 2;
    wizard.ps.models = vec!["gpt-4o".to_string()];
    let names = wizard.ps.all_model_names();
    assert_eq!(names, vec!["gpt-4o"]);
}

#[test]
fn wizard_all_model_names_falls_back_to_config_models() {
    let mut wizard = OnboardingWizard::new();
    wizard.ps.selected_provider = 2;
    wizard.ps.config_models = vec!["gpt-4o".to_string()];
    let names = wizard.ps.all_model_names();
    assert_eq!(names, vec!["gpt-4o"]);
}

#[test]
fn wizard_selected_model_name_in_bounds() {
    let mut wizard = OnboardingWizard::new();
    wizard.ps.selected_provider = 2;
    wizard.ps.models = vec!["gpt-4o".to_string(), "gpt-4o-mini".to_string()];
    wizard.ps.selected_model = 1;
    assert_eq!(wizard.ps.selected_model_name(), "gpt-4o-mini");
}

#[test]
fn wizard_selected_model_name_out_of_bounds_fallback() {
    let mut wizard = OnboardingWizard::new();
    wizard.ps.selected_provider = 2;
    wizard.ps.models = vec!["gpt-4o".to_string()];
    wizard.ps.selected_model = 99;
    assert_eq!(wizard.ps.selected_model_name(), "gpt-4o");
}

// ── Factory ─────────────────────────────────────────────────────

#[test]
fn github_provider_builder_full_chain() {
    use crate::brain::provider::copilot::copilot_extra_headers;
    let headers = copilot_extra_headers();
    let provider = OpenAIProvider::with_base_url(
        "copilot-managed".to_string(),
        "https://api.githubcopilot.com/chat/completions".to_string(),
    )
    .with_name("GitHub Copilot")
    .with_extra_headers(headers.clone());
    assert_eq!(provider.name(), "GitHub Copilot");
    assert!(!provider.extra_headers.is_empty());
    let keys: Vec<&str> = provider
        .extra_headers
        .iter()
        .map(|(k, _)| k.as_str())
        .collect();
    assert!(keys.contains(&"copilot-integration-id"));
}

#[test]
fn extra_headers_new_constructor_empty() {
    let provider = OpenAIProvider::new("test-key".to_string());
    assert!(provider.extra_headers.is_empty());
}

// ── Config apply section mapping ────────────────────────────────

#[test]
fn github_config_section_is_providers_github() {
    // The all_provider_sections array includes "providers.github"
    let sections = [
        "providers.anthropic",
        "providers.openai",
        "providers.github",
        "providers.gemini",
        "providers.openrouter",
        "providers.minimax",
    ];
    assert_eq!(sections[2], "providers.github");
}

#[test]
fn github_writes_base_url() {
    // GitHub writes base_url to config like OpenRouter and Minimax
    let github_index: usize = 2;
    let writes_base_url = matches!(github_index, 2 | 4 | 5 | 6);
    assert!(writes_base_url);
}

#[test]
fn github_writes_models_array() {
    // GitHub (2), Minimax (5), and Custom (6) write models arrays
    let github_index: usize = 2;
    let writes_models = matches!(github_index, 2 | 5 | 6);
    assert!(writes_models);
}

// ── Config resolution priority ──────────────────────────────────

#[test]
fn resolve_anthropic_takes_priority_over_github() {
    let mut config = Config::default();
    config.providers.anthropic = Some(ProviderConfig {
        enabled: true,
        default_model: Some("claude-sonnet-4-20250514".to_string()),
        ..Default::default()
    });
    config.providers.github = Some(ProviderConfig {
        enabled: true,
        default_model: Some("gpt-4o".to_string()),
        ..Default::default()
    });
    let (name, _) = resolve_provider_from_config(&config);
    assert_eq!(name, "Anthropic");
}

#[test]
fn resolve_github_chosen_when_only_github_enabled() {
    let mut config = Config::default();
    config.providers.anthropic = Some(ProviderConfig {
        enabled: false,
        ..Default::default()
    });
    config.providers.openai = Some(ProviderConfig {
        enabled: false,
        ..Default::default()
    });
    config.providers.github = Some(ProviderConfig {
        enabled: true,
        default_model: Some("Llama-3.3-70B-Instruct".to_string()),
        ..Default::default()
    });
    let (name, model) = resolve_provider_from_config(&config);
    assert_eq!(name, "GitHub Copilot");
    assert_eq!(model, "Llama-3.3-70B-Instruct");
}

// ── GitHub-specific onboarding OAuth flow ───────────────────────

#[test]
fn github_help_mentions_copilot_subscription() {
    let help = PROVIDERS[2].help_lines.join(" ");
    assert!(help.contains("Copilot"));
    assert!(help.contains("subscription"));
}

#[test]
fn wizard_github_static_models_empty() {
    // GitHub has no static models in PROVIDERS[2].models — they come from config
    assert!(PROVIDERS[2].models.is_empty());
}

#[test]
fn github_load_default_models_from_config_example() {
    // Copilot models are fetched live from the API, not from config.toml.example
    // So load_default_models may return empty (no hardcoded list)
    let models = crate::tui::provider_selector::load_default_models("github");
    // Either empty or contains whatever is in config.toml.example
    let _ = models;
}

#[test]
fn github_config_models_written_on_apply() {
    // Verify index 2 is in the models-write match
    let github_index: usize = 2;
    let writes_models = matches!(github_index, 2 | 5 | 6);
    assert!(writes_models);
}

#[test]
fn github_token_persist_path_index_matches() {
    // Verify that index 2 triggers the gh token persist code path in apply_config
    let github_index: usize = 2;
    assert_eq!(github_index, 2);
}

#[test]
fn wizard_github_model_filter_empty_returns_all() {
    let mut wizard = OnboardingWizard::new();
    wizard.ps.selected_provider = 2;
    wizard.ps.models = vec!["gpt-4o".to_string(), "Llama-3.3-70B-Instruct".to_string()];
    wizard.ps.model_filter.clear();
    assert_eq!(wizard.ps.filtered_model_names().len(), 2);
}

#[test]
fn wizard_github_model_filter_no_match() {
    let mut wizard = OnboardingWizard::new();
    wizard.ps.selected_provider = 2;
    wizard.ps.models = vec!["gpt-4o".to_string()];
    wizard.ps.model_filter = "nonexistent".to_string();
    assert!(wizard.ps.filtered_model_names().is_empty());
}

#[test]
fn wizard_github_model_filter_case_insensitive() {
    let mut wizard = OnboardingWizard::new();
    wizard.ps.selected_provider = 2;
    wizard.ps.models = vec!["GPT-4o".to_string(), "Llama-3.3-70B-Instruct".to_string()];
    wizard.ps.model_filter = "gpt".to_string();
    let filtered = wizard.ps.filtered_model_names();
    assert_eq!(filtered.len(), 1);
    assert_eq!(filtered[0], "GPT-4o");
}

#[test]
fn wizard_github_model_filter_by_provider_name() {
    let mut wizard = OnboardingWizard::new();
    wizard.ps.selected_provider = 2;
    wizard.ps.models = vec![
        "gpt-4o".to_string(),
        "gpt-4o-mini".to_string(),
        "Llama-3.3-70B-Instruct".to_string(),
        "Mistral-Large".to_string(),
    ];
    wizard.ps.model_filter = "llama".to_string();
    let filtered = wizard.ps.filtered_model_names();
    assert_eq!(filtered.len(), 1);
    assert_eq!(filtered[0], "Llama-3.3-70B-Instruct");
}