opencrabs 0.3.19

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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//! Provider factory regression tests.
//!
//! These tests verify that all 11 built-in providers are correctly wired
//! across the factory functions. They serve as a regression suite before
//! refactoring the factory into a registry pattern.

use crate::brain::provider::factory::{
    active_provider_vision, create_provider, create_provider_by_name,
};
use crate::config::{Config, ProviderConfig, ProviderConfigs};
use std::collections::BTreeMap;

// ── Helpers ─────────────────────────────────────────────────────

fn config_with_provider(name: &str) -> Config {
    let cfg = ProviderConfig {
        enabled: true,
        api_key: Some("test-key".to_string()),
        base_url: Some("http://localhost:1234/v1".to_string()),
        default_model: Some("test-model".to_string()),
        models: vec![],
        vision_model: None,
        ..Default::default()
    };

    let mut providers = ProviderConfigs::default();
    match name {
        "claude_cli" => providers.claude_cli = Some(cfg),
        "opencode_cli" => providers.opencode_cli = Some(cfg),
        "codex_cli" => providers.codex_cli = Some(cfg),
        "qwen" => providers.qwen = Some(cfg),
        "anthropic" => providers.anthropic = Some(cfg),
        "openai" => providers.openai = Some(cfg),
        "github" => providers.github = Some(cfg),
        "gemini" => providers.gemini = Some(cfg),
        "openrouter" => providers.openrouter = Some(cfg),
        "minimax" => providers.minimax = Some(cfg),
        "zhipu" => providers.zhipu = Some(cfg),
        "ollama" => providers.ollama = Some(cfg),
        _ => {}
    }
    Config {
        providers,
        ..Default::default()
    }
}

// ── create_provider_by_name: session ID resolution ──────────────

#[tokio::test]
async fn by_name_anthropic() {
    let config = config_with_provider("anthropic");
    let result = create_provider_by_name(&config, "anthropic").await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap().name(), "anthropic");
}

#[tokio::test]
async fn by_name_openai() {
    let config = config_with_provider("openai");
    let result = create_provider_by_name(&config, "openai").await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap().name(), "openai");
}

#[tokio::test]
async fn by_name_github() {
    let config = config_with_provider("github");
    let result = create_provider_by_name(&config, "github").await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn by_name_gemini() {
    let config = config_with_provider("gemini");
    let result = create_provider_by_name(&config, "gemini").await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn by_name_openrouter() {
    let config = config_with_provider("openrouter");
    let result = create_provider_by_name(&config, "openrouter").await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn by_name_minimax() {
    let config = config_with_provider("minimax");
    let result = create_provider_by_name(&config, "minimax").await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn by_name_zhipu() {
    let config = config_with_provider("zhipu");
    let result = create_provider_by_name(&config, "zhipu").await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn by_name_qwen() {
    let config = config_with_provider("qwen");
    let result = create_provider_by_name(&config, "qwen").await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap().name(), "qwen");
}

#[tokio::test]
async fn by_name_ollama() {
    let config = config_with_provider("ollama");
    let result = create_provider_by_name(&config, "ollama").await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap().name(), "ollama");
}

// ── create_provider_by_name: alias resolution ───────────────────

#[tokio::test]
async fn by_name_claude_cli_hyphen() {
    // Claude CLI name resolution should work regardless of binary presence.
    // If binary exists → Ok with name "claude-cli". If not → Err about binary.
    // Must NOT return "unknown provider".
    let config = Config::default();
    let result = create_provider_by_name(&config, "claude-cli").await;
    match &result {
        Ok(p) => assert_eq!(p.name(), "claude-cli"),
        Err(e) => {
            let err = e.to_string();
            assert!(
                err.contains("binary") || err.contains("configured") || err.contains("not found"),
                "Expected claude-cli resolution error, got: {}",
                err
            );
        }
    }
}

#[tokio::test]
async fn by_name_claude_cli_underscore() {
    let config = Config::default();
    let result = create_provider_by_name(&config, "claude_cli").await;
    match &result {
        Ok(p) => assert_eq!(p.name(), "claude-cli"),
        Err(e) => {
            let err = e.to_string();
            assert!(
                err.contains("binary") || err.contains("configured") || err.contains("not found"),
                "Expected claude_cli resolution error, got: {}",
                err
            );
        }
    }
}

#[tokio::test]
async fn by_name_opencode_cli_hyphen() {
    let config = Config::default();
    let result = create_provider_by_name(&config, "opencode-cli").await;
    match &result {
        Ok(p) => assert_eq!(p.name(), "opencode"),
        Err(e) => {
            let err = e.to_string();
            assert!(
                err.contains("binary") || err.contains("configured") || err.contains("not found"),
                "Expected opencode-cli resolution error, got: {}",
                err
            );
        }
    }
}

#[tokio::test]
async fn by_name_opencode_cli_underscore() {
    let config = Config::default();
    let result = create_provider_by_name(&config, "opencode_cli").await;
    match &result {
        Ok(p) => assert_eq!(p.name(), "opencode"),
        Err(e) => {
            let err = e.to_string();
            assert!(
                err.contains("binary") || err.contains("configured") || err.contains("not found"),
                "Expected opencode_cli resolution error, got: {}",
                err
            );
        }
    }
}

#[tokio::test]
async fn by_name_codex_cli_hyphen() {
    let config = Config::default();
    let result = create_provider_by_name(&config, "codex-cli").await;
    match &result {
        Ok(p) => assert_eq!(p.name(), "codex-cli"),
        Err(e) => {
            let err = e.to_string();
            assert!(
                err.contains("binary") || err.contains("configured") || err.contains("not found"),
                "Expected codex-cli resolution error, got: {}",
                err
            );
        }
    }
}

#[tokio::test]
async fn by_name_codex_cli_underscore() {
    let config = Config::default();
    let result = create_provider_by_name(&config, "codex_cli").await;
    match &result {
        Ok(p) => assert_eq!(p.name(), "codex-cli"),
        Err(e) => {
            let err = e.to_string();
            assert!(
                err.contains("binary") || err.contains("configured") || err.contains("not found"),
                "Expected codex_cli resolution error, got: {}",
                err
            );
        }
    }
}

#[tokio::test]
async fn by_name_custom_prefix() {
    let mut custom_map = BTreeMap::new();
    custom_map.insert(
        "mylocal".to_string(),
        ProviderConfig {
            enabled: true,
            base_url: Some("http://localhost:1234/v1".to_string()),
            ..Default::default()
        },
    );
    let config = Config {
        providers: ProviderConfigs {
            custom: Some(custom_map),
            ..Default::default()
        },
        ..Default::default()
    };
    let result = create_provider_by_name(&config, "custom:mylocal").await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap().name(), "mylocal");
}

// ── create_provider: priority order ─────────────────────────────

#[tokio::test]
async fn priority_anthropic_over_openai() {
    let config = Config {
        providers: ProviderConfigs {
            anthropic: Some(ProviderConfig {
                enabled: true,
                api_key: Some("anthropic-key".to_string()),
                ..Default::default()
            }),
            openai: Some(ProviderConfig {
                enabled: true,
                api_key: Some("openai-key".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        },
        ..Default::default()
    };
    let result = create_provider(&config).await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap().name(), "anthropic");
}

#[tokio::test]
async fn priority_openai_over_gemini() {
    let config = Config {
        providers: ProviderConfigs {
            openai: Some(ProviderConfig {
                enabled: true,
                api_key: Some("openai-key".to_string()),
                ..Default::default()
            }),
            gemini: Some(ProviderConfig {
                enabled: true,
                api_key: Some("gemini-key".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        },
        ..Default::default()
    };
    let result = create_provider(&config).await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap().name(), "openai");
}

#[tokio::test]
async fn priority_gemini_over_openrouter() {
    let config = Config {
        providers: ProviderConfigs {
            gemini: Some(ProviderConfig {
                enabled: true,
                api_key: Some("gemini-key".to_string()),
                ..Default::default()
            }),
            openrouter: Some(ProviderConfig {
                enabled: true,
                api_key: Some("or-key".to_string()),
                base_url: Some("https://openrouter.ai/api/v1/chat/completions".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        },
        ..Default::default()
    };
    let result = create_provider(&config).await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap().name(), "gemini");
}

#[tokio::test]
async fn priority_minimax_over_zhipu() {
    let config = Config {
        providers: ProviderConfigs {
            minimax: Some(ProviderConfig {
                enabled: true,
                api_key: Some("minimax-key".to_string()),
                base_url: Some("https://api.minimax.io/v1".to_string()),
                ..Default::default()
            }),
            zhipu: Some(ProviderConfig {
                enabled: true,
                api_key: Some("zhipu-key".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        },
        ..Default::default()
    };
    let result = create_provider(&config).await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap().name(), "minimax");
}

#[tokio::test]
async fn disabled_provider_skipped() {
    // Anthropic disabled, OpenAI enabled — should pick OpenAI
    let config = Config {
        providers: ProviderConfigs {
            anthropic: Some(ProviderConfig {
                enabled: false,
                api_key: Some("anthropic-key".to_string()),
                ..Default::default()
            }),
            openai: Some(ProviderConfig {
                enabled: true,
                api_key: Some("openai-key".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        },
        ..Default::default()
    };
    let result = create_provider(&config).await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap().name(), "openai");
}

#[tokio::test]
async fn no_provider_returns_placeholder() {
    let config = Config::default();
    let result = create_provider(&config).await;
    assert!(result.is_ok());
    // PlaceholderProvider::name() returns "none"
    assert_eq!(result.unwrap().name(), "none");
}

// ── TUI / Factory consistency ──────────────────────────────────

#[test]
fn tui_providers_match_factory_session_ids() {
    // Every static PROVIDER id in the TUI must have a matching session_id
    // in the factory's REGISTRATIONS. The last entry (Custom, id="") is excluded.
    // This catches cases where a provider is added to one layer but not the other.
    use crate::tui::onboarding::PROVIDERS;
    let session_ids = crate::brain::provider::factory::provider_session_ids();

    for p in PROVIDERS.iter() {
        if p.id.is_empty() {
            continue; // Custom sentinel — dynamic name, skip
        }
        assert!(
            session_ids.contains(&p.id),
            "TUI provider id '{}' not found in factory session_ids",
            p.id
        );
    }
}

// ── active_provider_vision ──────────────────────────────────────

#[test]
fn vision_anthropic() {
    let config = Config {
        providers: ProviderConfigs {
            anthropic: Some(ProviderConfig {
                enabled: true,
                api_key: Some("key".to_string()),
                vision_model: Some("claude-3-opus".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        },
        ..Default::default()
    };
    let result = active_provider_vision(&config);
    assert!(result.is_some());
    let (key, url, model) = result.unwrap();
    assert_eq!(key, "key");
    assert_eq!(model, "claude-3-opus");
    assert!(url.contains("chat/completions"));
}

#[test]
fn vision_openai() {
    let config = Config {
        providers: ProviderConfigs {
            openai: Some(ProviderConfig {
                enabled: true,
                api_key: Some("key".to_string()),
                vision_model: Some("gpt-4o".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        },
        ..Default::default()
    };
    let result = active_provider_vision(&config);
    assert!(result.is_some());
    let (_, _, model) = result.unwrap();
    assert_eq!(model, "gpt-4o");
}

#[test]
fn vision_openrouter() {
    let config = Config {
        providers: ProviderConfigs {
            openrouter: Some(ProviderConfig {
                enabled: true,
                api_key: Some("key".to_string()),
                base_url: Some("https://openrouter.ai/api/v1/chat/completions".to_string()),
                vision_model: Some("anthropic/claude-3.5-sonnet".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        },
        ..Default::default()
    };
    let result = active_provider_vision(&config);
    assert!(result.is_some());
    let (_, _, model) = result.unwrap();
    assert_eq!(model, "anthropic/claude-3.5-sonnet");
}

#[test]
fn vision_minimax() {
    let config = Config {
        providers: ProviderConfigs {
            minimax: Some(ProviderConfig {
                enabled: true,
                api_key: Some("key".to_string()),
                base_url: Some("https://api.minimax.io/v1".to_string()),
                vision_model: Some("MiniMax-Text-01".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        },
        ..Default::default()
    };
    let result = active_provider_vision(&config);
    assert!(result.is_some());
    let (_, _, model) = result.unwrap();
    assert_eq!(model, "MiniMax-Text-01");
}

#[test]
fn vision_none_when_no_vision_model() {
    let config = Config {
        providers: ProviderConfigs {
            anthropic: Some(ProviderConfig {
                enabled: true,
                api_key: Some("key".to_string()),
                vision_model: None,
                ..Default::default()
            }),
            ..Default::default()
        },
        ..Default::default()
    };
    let result = active_provider_vision(&config);
    assert!(result.is_none());
}

#[test]
fn vision_none_when_no_api_key() {
    let config = Config {
        providers: ProviderConfigs {
            openai: Some(ProviderConfig {
                enabled: true,
                api_key: None,
                vision_model: Some("gpt-4o".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        },
        ..Default::default()
    };
    let result = active_provider_vision(&config);
    assert!(result.is_none());
}

#[test]
fn vision_custom_provider() {
    let mut custom_map = BTreeMap::new();
    custom_map.insert(
        "myprovider".to_string(),
        ProviderConfig {
            enabled: true,
            api_key: Some("custom-key".to_string()),
            base_url: Some("http://localhost:8080/v1".to_string()),
            vision_model: Some("custom-vision-model".to_string()),
            ..Default::default()
        },
    );
    let config = Config {
        providers: ProviderConfigs {
            custom: Some(custom_map),
            ..Default::default()
        },
        ..Default::default()
    };
    // active_provider_vision uses active_provider_and_model which picks
    // the first enabled custom provider. The session_id for custom is
    // "custom:<name>".
    // Note: active_provider_vision checks the active provider from config,
    // not a specific name. Custom providers are picked via active_custom().
    // This test verifies the custom: prefix routing works.
    let result = active_provider_vision(&config);
    assert!(result.is_some());
    let (key, _, model) = result.unwrap();
    assert_eq!(key, "custom-key");
    assert_eq!(model, "custom-vision-model");
}