terraphim_agent 1.16.34

Terraphim AI Agent CLI - Command-line interface with interactive REPL and ASCII graph visualization
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
use std::process::Command;
use std::str;

use anyhow::Result;
use serial_test::serial;

mod support;
use support::cli_test_env::apply_hermetic_env;

/// Test helper to run TUI commands in offline mode
fn run_offline_command(args: &[&str]) -> Result<(String, String, i32)> {
    let mut cmd = Command::new("cargo");
    cmd.args(["run", "-p", "terraphim_agent", "--"]).args(args);
    apply_hermetic_env(&mut cmd)?;

    let output = cmd.output()?;

    Ok((
        String::from_utf8_lossy(&output.stdout).to_string(),
        String::from_utf8_lossy(&output.stderr).to_string(),
        output.status.code().unwrap_or(-1),
    ))
}

/// Test helper to run TUI commands in server mode
fn run_server_command(args: &[&str]) -> Result<(String, String, i32)> {
    let mut cmd_args = vec!["--server"];
    cmd_args.extend_from_slice(args);

    let mut cmd = Command::new("cargo");
    cmd.args(["run", "-p", "terraphim_agent", "--features", "server", "--"])
        .args(cmd_args);
    apply_hermetic_env(&mut cmd)?;

    let output = cmd.output()?;

    Ok((
        String::from_utf8_lossy(&output.stdout).to_string(),
        String::from_utf8_lossy(&output.stderr).to_string(),
        output.status.code().unwrap_or(-1),
    ))
}

#[tokio::test]
#[serial]
async fn test_offline_help_command() -> Result<()> {
    let (stdout, _stderr, code) = run_offline_command(&["--help"])?;

    assert_eq!(code, 0, "Help command should succeed");
    assert!(
        stdout.contains("server-backed fullscreen TUI"),
        "Should show main help"
    );
    assert!(stdout.contains("--server"), "Should show server flag");
    assert!(
        stdout.contains("--server-url"),
        "Should show server URL flag"
    );
    assert!(stdout.contains("search"), "Should list search command");
    assert!(stdout.contains("roles"), "Should list roles command");
    assert!(stdout.contains("config"), "Should list config command");
    assert!(stdout.contains("graph"), "Should list graph command");
    assert!(stdout.contains("chat"), "Should list chat command");
    assert!(stdout.contains("extract"), "Should list extract command");
    assert!(
        stdout.contains("interactive"),
        "Should list interactive command"
    );

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_non_tty_usage_clarifies_mode_contract() -> Result<()> {
    let (stdout, _stderr, code) = run_offline_command(&[])?;

    assert_eq!(code, 0, "Non-TTY usage should exit cleanly");
    assert!(stdout.contains("Interactive Modes (requires TTY)"));
    assert!(stdout.contains("fullscreen TUI (requires running server)"));
    assert!(stdout.contains("repl"));
    assert!(stdout.contains("offline-capable by default"));

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_offline_config_show() -> Result<()> {
    let (stdout, stderr, code) = run_offline_command(&["config", "show"])?;

    assert_eq!(code, 0, "Config show should succeed, stderr: {}", stderr);

    // Parse JSON output
    let lines: Vec<&str> = stdout.lines().collect();
    let json_start = lines.iter().position(|line| line.starts_with('{'));
    assert!(json_start.is_some(), "Should contain JSON output");

    let json_lines = &lines[json_start.unwrap()..];
    let json_str = json_lines.join("\n");

    let config: serde_json::Value = serde_json::from_str(&json_str).expect("Should be valid JSON");

    assert!(
        config["id"] == "Embedded" || config["id"] == "Server",
        "Should load a valid config id for offline mode"
    );
    assert!(
        config.get("selected_role").is_some(),
        "Should have selected_role"
    );
    assert!(
        config.get("default_role").is_some(),
        "Should have default_role"
    );
    assert!(
        config.get("global_shortcut").is_some(),
        "Should have global_shortcut"
    );

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_offline_roles_list() -> Result<()> {
    let (stdout, stderr, code) = run_offline_command(&["roles", "list"])?;

    assert_eq!(code, 0, "Roles list should succeed, stderr: {}", stderr);

    // The output should be role names, one per line
    // For embedded config, it might be empty initially, which is valid
    println!("Roles output: {}", stdout);

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_offline_search_with_default_role() -> Result<()> {
    let (stdout, stderr, code) = run_offline_command(&["search", "test query"])?;

    // Search may succeed or fail depending on setup, but should not crash
    assert!(
        code == 0 || code == 1,
        "Search should not crash, stderr: {}",
        stderr
    );

    // Should use selected_role from config (no role override)
    if code == 0 {
        println!("Search successful: {}", stdout);
    } else {
        println!("Search failed as expected (no data): {}", stderr);
    }

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_offline_search_with_role_override() -> Result<()> {
    let (stdout, stderr, code) =
        run_offline_command(&["search", "test query", "--role", "Default"])?;

    // Search may succeed or fail depending on setup, but should not crash
    assert!(
        code == 0 || code == 1,
        "Search with role override should not crash, stderr: {}",
        stderr
    );

    if code == 0 {
        println!("Search with role override successful: {}", stdout);
    } else {
        println!("Search with role override failed as expected: {}", stderr);
    }

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_offline_graph_command() -> Result<()> {
    let (stdout, stderr, code) = run_offline_command(&["graph"])?;

    // Graph command should succeed (returns placeholder data)
    assert_eq!(code, 0, "Graph command should succeed, stderr: {}", stderr);

    // Should show concepts (placeholder data)
    let lines: Vec<&str> = stdout
        .lines()
        .filter(|line| !line.contains("INFO") && !line.contains("WARN"))
        .collect();
    println!("Graph output lines: {:?}", lines);

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_offline_graph_with_role() -> Result<()> {
    let (stdout, stderr, code) =
        run_offline_command(&["graph", "--role", "Default", "--top-k", "5"])?;

    // Graph command with role should succeed
    assert_eq!(
        code, 0,
        "Graph command with role should succeed, stderr: {}",
        stderr
    );

    println!("Graph with role output: {}", stdout);

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_offline_chat_command() -> Result<()> {
    let (stdout, stderr, code) = run_offline_command(&["chat", "Hello, how are you?"])?;

    // Chat command may return exit code 1 if no LLM is configured, which is valid
    assert!(
        code == 0 || code == 1,
        "Chat command should not crash, stderr: {}",
        stderr
    );

    // Check for appropriate output - either LLM response or "no LLM configured" message
    let output_lines: Vec<&str> = stdout
        .lines()
        .filter(|line| !line.contains("INFO") && !line.contains("WARN"))
        .collect();
    let response = output_lines.join("\n");

    // Also check stderr for "No LLM configured" since error messages go there
    if code == 0 {
        println!("Chat successful: {}", response);
    } else {
        // Exit code 1 is expected when no LLM is configured
        assert!(
            stderr.contains("No LLM configured") || response.contains("No LLM configured"),
            "Should show no LLM configured message: stdout={}, stderr={}",
            response,
            stderr
        );
        println!("Chat correctly indicated no LLM configured");
    }

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_offline_extract_command() -> Result<()> {
    let text =
        "This is a test paragraph. It contains some text for extraction. Another sentence here.";
    let (stdout, stderr, code) = run_offline_command(&["extract", text])?;

    // Extract might succeed or fail based on thesaurus availability
    assert!(
        code == 0 || code == 1,
        "Extract command should not crash, stderr: {}",
        stderr
    );

    let output_lines: Vec<&str> = stdout
        .lines()
        .filter(|line| !line.contains("INFO") && !line.contains("WARN"))
        .collect();
    let response = output_lines.join("\n");

    if code == 0 {
        println!("Extract successful: {}", response);
    } else {
        println!("Extract failed as expected: {}", response);
    }

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_offline_extract_with_role() -> Result<()> {
    let text = "This is a test paragraph. It contains some text for extraction.";
    let (stdout, stderr, code) =
        run_offline_command(&["extract", text, "--role", "Default", "--exclude-term"])?;

    // Extract with role might succeed or fail
    assert!(
        code == 0 || code == 1,
        "Extract with role should not crash, stderr: {}",
        stderr
    );

    println!("Extract with role output: {}", stdout);

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_offline_config_set_selected_role() -> Result<()> {
    // First show current config
    let (stdout_before, _, _) = run_offline_command(&["config", "show"])?;
    println!("Config before: {}", stdout_before);

    // Set selected role
    let (stdout, stderr, code) =
        run_offline_command(&["config", "set", "selected_role", "Default"])?;

    assert_eq!(code, 0, "Config set should succeed, stderr: {}", stderr);

    let output_lines: Vec<&str> = stdout
        .lines()
        .filter(|line| !line.contains("INFO") && !line.contains("WARN"))
        .collect();
    let response = output_lines.join("\n");
    assert!(
        response.contains("updated selected_role to Default"),
        "Should confirm role update: {}",
        response
    );

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_offline_roles_select() -> Result<()> {
    // This test may fail if no roles exist, which is expected for embedded config
    let (stdout, stderr, code) = run_offline_command(&["roles", "select", "Default"])?;

    if code == 0 {
        let output_lines: Vec<&str> = stdout
            .lines()
            .filter(|line| !line.contains("INFO") && !line.contains("WARN"))
            .collect();
        let response = output_lines.join("\n");
        assert!(
            response.contains("selected:Default"),
            "Should confirm role selection: {}",
            response
        );
    } else {
        // Expected if no roles exist in embedded config
        println!("Role select failed as expected (no roles): {}", stderr);
    }

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_server_mode_connection_failure() -> Result<()> {
    // Test that server mode gracefully handles no server running.
    // The CLI may fall back to offline mode (exit 0) or fail (exit 1).
    let (_stdout, stderr, code) = run_server_command(&["roles", "list"])?;

    let graceful_fallback = code == 0;
    let connection_error =
        stderr.contains("Connection refused") || stderr.contains("connect error");
    assert!(
        graceful_fallback || connection_error,
        "Server mode should either fall back gracefully or report connection error: exit={code}, stderr={stderr}",
    );

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_server_mode_with_custom_url() -> Result<()> {
    // Test server mode with custom URL
    let mut cmd = Command::new("cargo");
    cmd.args(["run", "-p", "terraphim_agent", "--features", "server", "--"])
        .args([
            "--server",
            "--server-url",
            "http://localhost:9999",
            "config",
            "show",
        ]);
    apply_hermetic_env(&mut cmd)?;

    let output = cmd.output()?;
    let stderr = String::from_utf8_lossy(&output.stderr);
    let code = output.status.code().unwrap_or(-1);

    assert_eq!(
        code, 1,
        "Should fail with custom URL when no server running"
    );
    assert!(
        stderr.contains("Connection refused") || stderr.contains("connect error"),
        "Should show connection error with custom URL: {}",
        stderr
    );

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_command_line_argument_validation() -> Result<()> {
    // Test invalid command
    let mut cmd = Command::new("cargo");
    cmd.args(["run", "-p", "terraphim_agent", "--"])
        .args(["invalid-command"]);
    apply_hermetic_env(&mut cmd)?;

    let output = cmd.output()?;
    let code = output.status.code().unwrap_or(-1);

    assert_ne!(code, 0, "Invalid command should fail");

    // Test help for subcommands
    let (stdout, _, code) = run_offline_command(&["search", "--help"])?;
    assert_eq!(code, 0, "Search help should succeed");
    assert!(stdout.contains("query"), "Should show query parameter");
    assert!(stdout.contains("--role"), "Should show role option");
    assert!(stdout.contains("--limit"), "Should show limit option");

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_offline_mode_is_default() -> Result<()> {
    // Test that offline mode is the default (no --server flag)
    let (_stdout, stderr, code) = run_offline_command(&["config", "show"])?;

    assert_eq!(code, 0, "Default mode should be offline and succeed");

    // Should initialize embedded service (log messages will show this)
    assert!(
        stderr.contains("Initializing TUI service")
            || stderr.contains("Failed to load config")
            || stderr.contains("using default embedded")
            || stderr.contains("using embedded defaults"),
        "Should show embedded initialization: {}",
        stderr
    );

    Ok(())
}