ares-server 0.7.5

A.R.E.S - Agentic Retrieval Enhanced Server: A production-grade agentic chatbot server with multi-provider LLM support, tool calling, RAG, and MCP integration
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
//! CLI Integration Tests for A.R.E.S
//!
//! Tests the command-line interface functionality including the init command,
//! config command, and agent commands.

use std::fs;
use std::process::Command;
use tempfile::TempDir;

/// Helper to get the path to the built binary
fn get_binary_path() -> String {
    // In tests, we use cargo run
    "cargo".to_string()
}

/// Helper to run ares-server with arguments
fn run_ares(args: &[&str], working_dir: Option<&str>) -> std::process::Output {
    let mut cmd = Command::new(get_binary_path());
    cmd.arg("run").arg("--quiet").arg("--").args(args);

    if let Some(dir) = working_dir {
        cmd.current_dir(dir);
    }

    cmd.output().expect("Failed to execute command")
}

// =============================================================================
// Help and Version Tests
// =============================================================================

#[test]
fn test_help_command() {
    let output = run_ares(&["--help"], None);

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Check key sections are present
    assert!(stdout.contains("A.R.E.S"));
    assert!(stdout.contains("USAGE") || stdout.contains("Usage"));
    assert!(stdout.contains("init"));
    assert!(stdout.contains("config"));
    assert!(stdout.contains("agent"));
}

#[test]
fn test_version_command() {
    let output = run_ares(&["--version"], None);

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("ares-server"));
}

#[test]
fn test_init_help() {
    let output = run_ares(&["init", "--help"], None);

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Check init-specific options
    assert!(stdout.contains("--force"));
    assert!(stdout.contains("--minimal"));
    assert!(stdout.contains("--provider"));
    assert!(stdout.contains("--host"));
    assert!(stdout.contains("--port"));
    assert!(stdout.contains("--no-examples"));
}

// =============================================================================
// Init Command Tests
// =============================================================================

#[test]
fn test_init_creates_ares_toml() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let temp_path = temp_dir.path().to_str().unwrap();

    let output = run_ares(&["init", temp_path], None);

    assert!(output.status.success(), "Init command failed: {:?}", output);

    // Check ares.toml was created
    let config_path = temp_dir.path().join("ares.toml");
    assert!(config_path.exists(), "ares.toml was not created");

    // Verify content
    let content = fs::read_to_string(&config_path).expect("Failed to read ares.toml");
    assert!(content.contains("[server]"));
    assert!(content.contains("[auth]"));
    assert!(content.contains("[database]"));
    assert!(content.contains("[providers.ollama-local]"));
}

#[test]
fn test_init_creates_directories() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let temp_path = temp_dir.path().to_str().unwrap();

    let output = run_ares(&["init", temp_path], None);
    assert!(output.status.success());

    // Check directories were created
    assert!(temp_dir.path().join("data").is_dir());
    assert!(temp_dir.path().join("config").is_dir());
    assert!(temp_dir.path().join("config/agents").is_dir());
    assert!(temp_dir.path().join("config/models").is_dir());
    assert!(temp_dir.path().join("config/tools").is_dir());
    assert!(temp_dir.path().join("config/workflows").is_dir());
    assert!(temp_dir.path().join("config/mcps").is_dir());
}

#[test]
fn test_init_creates_env_example() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let temp_path = temp_dir.path().to_str().unwrap();

    let output = run_ares(&["init", temp_path], None);
    assert!(output.status.success());

    let env_path = temp_dir.path().join(".env.example");
    assert!(env_path.exists(), ".env.example was not created");

    let content = fs::read_to_string(&env_path).expect("Failed to read .env.example");
    assert!(content.contains("JWT_SECRET"));
    assert!(content.contains("API_KEY"));
}

#[test]
fn test_init_creates_gitignore() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let temp_path = temp_dir.path().to_str().unwrap();

    let output = run_ares(&["init", temp_path], None);
    assert!(output.status.success());

    let gitignore_path = temp_dir.path().join(".gitignore");
    assert!(gitignore_path.exists(), ".gitignore was not created");

    let content = fs::read_to_string(&gitignore_path).expect("Failed to read .gitignore");
    assert!(content.contains("/data/"));
    assert!(content.contains(".env"));
}

#[test]
fn test_init_creates_toon_files() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let temp_path = temp_dir.path().to_str().unwrap();

    let output = run_ares(&["init", temp_path], None);
    assert!(output.status.success());

    // Check model TOON files
    assert!(temp_dir.path().join("config/models/fast.toon").exists());
    assert!(temp_dir.path().join("config/models/balanced.toon").exists());
    assert!(temp_dir.path().join("config/models/powerful.toon").exists());

    // Check agent TOON files
    assert!(temp_dir.path().join("config/agents/router.toon").exists());
    assert!(temp_dir
        .path()
        .join("config/agents/orchestrator.toon")
        .exists());

    // Check tool TOON files
    assert!(temp_dir
        .path()
        .join("config/tools/calculator.toon")
        .exists());
    assert!(temp_dir
        .path()
        .join("config/tools/web_search.toon")
        .exists());

    // Check workflow TOON files
    assert!(temp_dir
        .path()
        .join("config/workflows/default.toon")
        .exists());
    assert!(temp_dir
        .path()
        .join("config/workflows/research.toon")
        .exists());
}

#[test]
fn test_init_with_openai_provider() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let temp_path = temp_dir.path().to_str().unwrap();

    let output = run_ares(&["init", temp_path, "--provider", "openai"], None);
    assert!(output.status.success());

    let content =
        fs::read_to_string(temp_dir.path().join("ares.toml")).expect("Failed to read ares.toml");
    assert!(content.contains("[providers.openai]"));
    assert!(content.contains("type = \"openai\""));
    assert!(content.contains("OPENAI_API_KEY"));
}

#[test]
fn test_init_with_both_providers() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let temp_path = temp_dir.path().to_str().unwrap();

    let output = run_ares(&["init", temp_path, "--provider", "both"], None);
    assert!(output.status.success());

    let content =
        fs::read_to_string(temp_dir.path().join("ares.toml")).expect("Failed to read ares.toml");
    assert!(content.contains("[providers.ollama-local]"));
    assert!(content.contains("[providers.openai]"));
}

#[test]
fn test_init_with_custom_host_port() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let temp_path = temp_dir.path().to_str().unwrap();

    let output = run_ares(
        &["init", temp_path, "--host", "0.0.0.0", "--port", "8080"],
        None,
    );
    assert!(output.status.success());

    let content =
        fs::read_to_string(temp_dir.path().join("ares.toml")).expect("Failed to read ares.toml");
    assert!(content.contains("host = \"0.0.0.0\""));
    assert!(content.contains("port = 8080"));
}

#[test]
fn test_init_no_examples_flag() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let temp_path = temp_dir.path().to_str().unwrap();

    let output = run_ares(&["init", temp_path, "--no-examples"], None);
    assert!(output.status.success());

    // ares.toml should still exist
    assert!(temp_dir.path().join("ares.toml").exists());

    // But TOON files should not exist
    assert!(!temp_dir.path().join("config/models/fast.toon").exists());
    assert!(!temp_dir.path().join("config/agents/router.toon").exists());
}

#[test]
fn test_init_fails_without_force_when_exists() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let temp_path = temp_dir.path().to_str().unwrap();

    // First init
    let output1 = run_ares(&["init", temp_path], None);
    assert!(output1.status.success());

    // Second init without --force should fail/warn
    let output2 = run_ares(&["init", temp_path], None);

    // Check that it recognized the existing config
    let stderr = String::from_utf8_lossy(&output2.stderr);
    let stdout = String::from_utf8_lossy(&output2.stdout);
    let combined = format!("{}{}", stdout, stderr);

    assert!(
        combined.contains("already exists") || !output2.status.success(),
        "Should warn about existing config or fail"
    );
}

#[test]
fn test_init_with_force_overwrites() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let temp_path = temp_dir.path().to_str().unwrap();

    // First init with default port
    let output1 = run_ares(&["init", temp_path], None);
    assert!(output1.status.success());

    // Second init with --force and different port
    let output2 = run_ares(&["init", temp_path, "--force", "--port", "9999"], None);
    assert!(output2.status.success());

    // Verify new port is in config
    let content =
        fs::read_to_string(temp_dir.path().join("ares.toml")).expect("Failed to read ares.toml");
    assert!(content.contains("port = 9999"));
}

// =============================================================================
// Config Command Tests
// =============================================================================

#[test]
fn test_config_command_with_valid_config() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let temp_path = temp_dir.path().to_str().unwrap();

    // Initialize first
    let init_output = run_ares(&["init", temp_path], None);
    assert!(init_output.status.success());

    // Run config command
    let config_path = temp_dir.path().join("ares.toml");
    let output = run_ares(&["config", "--config", config_path.to_str().unwrap()], None);

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Check output contains expected sections
    assert!(stdout.contains("Configuration") || stdout.contains("config"));
    assert!(stdout.contains("127.0.0.1") || stdout.contains("Server"));
}

#[test]
fn test_config_command_missing_file() {
    let output = run_ares(&["config", "--config", "/nonexistent/path/ares.toml"], None);

    // Should fail gracefully
    assert!(
        !output.status.success() || {
            let stderr = String::from_utf8_lossy(&output.stderr);
            let stdout = String::from_utf8_lossy(&output.stdout);
            stderr.contains("not found") || stdout.contains("not found")
        }
    );
}

// =============================================================================
// Agent Command Tests
// =============================================================================

#[test]
fn test_agent_list_command() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let temp_path = temp_dir.path().to_str().unwrap();

    // Initialize first
    let init_output = run_ares(&["init", temp_path], None);
    assert!(init_output.status.success());

    // Run agent list command
    let config_path = temp_dir.path().join("ares.toml");
    let output = run_ares(
        &["agent", "list", "--config", config_path.to_str().unwrap()],
        None,
    );

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Check agents are listed
    assert!(stdout.contains("router") || stdout.contains("Router"));
    assert!(stdout.contains("orchestrator") || stdout.contains("Orchestrator"));
}

#[test]
fn test_agent_show_command() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let temp_path = temp_dir.path().to_str().unwrap();

    // Initialize first
    let init_output = run_ares(&["init", temp_path], None);
    assert!(init_output.status.success());

    // Run agent show command
    let config_path = temp_dir.path().join("ares.toml");
    let output = run_ares(
        &[
            "agent",
            "show",
            "orchestrator",
            "--config",
            config_path.to_str().unwrap(),
        ],
        None,
    );

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Check agent details are shown
    assert!(stdout.contains("orchestrator") || stdout.contains("Orchestrator"));
    assert!(stdout.contains("powerful") || stdout.contains("Model"));
}

#[test]
fn test_agent_show_nonexistent() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let temp_path = temp_dir.path().to_str().unwrap();

    // Initialize first
    let init_output = run_ares(&["init", temp_path], None);
    assert!(init_output.status.success());

    // Run agent show command for nonexistent agent
    let config_path = temp_dir.path().join("ares.toml");
    let output = run_ares(
        &[
            "agent",
            "show",
            "nonexistent_agent",
            "--config",
            config_path.to_str().unwrap(),
        ],
        None,
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    let combined = format!("{}{}", stdout, stderr);

    assert!(combined.contains("not found") || combined.contains("Not found"));
}

// =============================================================================
// No-Color Flag Tests
// =============================================================================

#[test]
fn test_no_color_flag() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let temp_path = temp_dir.path().to_str().unwrap();

    let output = run_ares(&["--no-color", "init", temp_path], None);
    assert!(output.status.success());

    // The output should not contain ANSI escape codes
    let stdout = String::from_utf8_lossy(&output.stdout);
    // ANSI escape codes start with \x1b[
    assert!(
        !stdout.contains("\x1b["),
        "Output should not contain ANSI escape codes when --no-color is used"
    );
}

// =============================================================================
// Output Module Unit Tests
// =============================================================================

#[cfg(test)]
mod output_tests {
    use ares::cli::output::Output;

    #[test]
    fn test_output_new() {
        let output = Output::new();
        assert!(output.colored);
    }

    #[test]
    fn test_output_no_color() {
        let output = Output::no_color();
        assert!(!output.colored);
    }

    #[test]
    fn test_output_default() {
        let output = Output::default();
        assert!(output.colored);
    }
}

// =============================================================================
// Init Module Unit Tests
// =============================================================================

#[cfg(test)]
mod init_tests {
    use ares::cli::init::{InitConfig, InitResult};
    use std::path::PathBuf;

    #[test]
    fn test_init_config_creation() {
        let config = InitConfig {
            path: PathBuf::from("/tmp/test"),
            force: false,
            minimal: false,
            no_examples: false,
            provider: "ollama".to_string(),
            host: "127.0.0.1".to_string(),
            port: 3000,
        };

        assert_eq!(config.path, PathBuf::from("/tmp/test"));
        assert!(!config.force);
        assert!(!config.minimal);
        assert_eq!(config.provider, "ollama");
        assert_eq!(config.port, 3000);
    }

    #[test]
    fn test_init_result_variants() {
        // Just verify the enum variants exist
        let _success = InitResult::Success;
        let _exists = InitResult::AlreadyExists;
        let _error = InitResult::Error("test error".to_string());
    }
}