larpshell 0.2.3

Ctrl+C then Ctrl+V is simply too much work. Just let an LLM rule your terminal!!
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
use crate::config::{ActiveProvider, AgentMode, Config, ProviderSpecificConfig};
use crate::error::LarpshellError;
use crate::providers::create_provider;
use std::ffi::OsString;
use std::fs;
use std::io::Write;
use std::net::TcpListener;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::sync::Mutex;

static BUILD_ONCE: std::sync::OnceLock<()> = std::sync::OnceLock::new();
use toml::from_str;

mod agent;
mod cli;
mod confirmation;
mod edit;
mod error;
mod explain;
mod interactive;
mod slash;

fn binary() -> PathBuf {
    if let Some(path) = TEST_BINARY_OVERRIDE.lock().unwrap().clone() {
        return PathBuf::from(path);
    }

    if let Some(path) = option_env!("CARGO_BIN_EXE_larpshell") {
        return PathBuf::from(path);
    }

    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("target/debug/larpshell")
}

fn ensure_binary_built() {
    if TEST_BINARY_OVERRIDE.lock().unwrap().is_some()
        || option_env!("CARGO_BIN_EXE_larpshell").is_some()
    {
        return;
    }

    BUILD_ONCE.get_or_init(|| {
        let status = Command::new("cargo")
            .args(["build", "--bin", "larpshell"])
            .current_dir(env!("CARGO_MANIFEST_DIR"))
            .status()
            .expect("failed to build larpshell test binary");
        assert!(status.success(), "cargo build --bin larpshell failed");
    });
}

static TEST_BINARY_OVERRIDE: Mutex<Option<OsString>> = Mutex::new(None);

struct TestBinaryOverrideGuard {
    original: Option<OsString>,
}

impl TestBinaryOverrideGuard {
    fn set(path: &str) -> Self {
        let mut override_path = TEST_BINARY_OVERRIDE.lock().unwrap();
        let original = override_path.replace(OsString::from(path));
        drop(override_path);
        Self { original }
    }
}

impl Drop for TestBinaryOverrideGuard {
    fn drop(&mut self) {
        *TEST_BINARY_OVERRIDE.lock().unwrap() = self.original.take();
    }
}

#[test]
fn binary_prefers_explicit_test_override() {
    let _guard = TestBinaryOverrideGuard::set("/tmp/larpshell-test-bin");
    assert_eq!(binary(), PathBuf::from("/tmp/larpshell-test-bin"));
}

#[test]
fn ensure_binary_built_skips_nested_cargo_when_override_is_present() {
    let _guard = TestBinaryOverrideGuard::set("/tmp/larpshell-test-bin");
    ensure_binary_built();
}

fn run(home: &std::path::Path, args: &[&str]) -> std::process::Output {
    ensure_binary_built();
    Command::new(binary())
        .args(args)
        .env("HOME", home)
        .env("XDG_CONFIG_HOME", home.join("config"))
        .env("NO_COLOR", "1")
        .output()
        .expect("failed to run larpshell")
}

fn temp_home(suffix: &str) -> PathBuf {
    let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("target/tests")
        .join(format!("larpshell_test_{suffix}"));
    // Pre-create the bash completion marker so auto_setup_shell_function sees it as
    // already installed and returns Ok(false) instead of exiting the process early.
    let completion_dir = dir.join(".local/share/bash-completion/completions");
    fs::create_dir_all(&completion_dir).unwrap();
    fs::write(completion_dir.join("larpshell"), "").unwrap();
    dir
}

// ── mock-server helpers ──────────────────────────────────────────────────────

/// Starts a local TCP server that responds to each accepted connection with the
/// next Ollama-format JSON response from `responses`, then closes the socket.
/// Returns the bound port so tests can point the config at it.
fn mock_ollama(responses: &[&str]) -> u16 {
    use std::io::Read;

    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let port = listener.local_addr().unwrap().port();
    let responses: Vec<String> = responses
        .iter()
        .map(std::string::ToString::to_string)
        .collect();

    std::thread::spawn(move || {
        for text in responses {
            let Ok((mut stream, _)) = listener.accept() else {
                break;
            };
            let mut buf = vec![0u8; 8192];
            let read = stream.read(&mut buf).unwrap_or(0);
            let request = String::from_utf8_lossy(&buf[..read]);
            let body = if let Some(raw_json) = text.strip_prefix("CHAT_JSON:") {
                raw_json.to_string()
            } else if request.starts_with("POST /api/chat ") {
                serde_json::json!({
                    "message": {
                        "content": text,
                    }
                })
                .to_string()
            } else {
                serde_json::json!({ "response": text }).to_string()
            };
            let http = format!(
                "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
                body.len(),
                body
            );
            let _ = stream.write_all(http.as_bytes());
        }
    });

    port
}

fn write_ollama_config(home: &std::path::Path, port: u16) {
    let config_dir = home.join("config").join("larpshell");
    fs::create_dir_all(&config_dir).unwrap();
    fs::write(
        config_dir.join("config.toml"),
        format!(
            "provider = \"ollama\"\n\n[providers.ollama]\nbase_url = \"http://127.0.0.1:{port}\"\nmodel = \"test\"\n"
        ),
    )
    .unwrap();
}

/// Like `run_with_stdin`, but sets `LARPSHELL_FORCE_INTERACTIVE=1` so that confirmation
/// prompts and the edit UI actually read from the piped stdin instead of being
/// auto-approved. The piped bytes represent raw key presses (Y/N/Enter/escape
/// sequences for arrow keys, etc.).
fn run_with_stdin_interactive(
    home: &std::path::Path,
    args: &[&str],
    stdin_data: &[u8],
) -> std::process::Output {
    ensure_binary_built();
    let mut child = Command::new(binary())
        .args(args)
        .env("HOME", home)
        .env("XDG_CONFIG_HOME", home.join("config"))
        .env("NO_COLOR", "1")
        .env("LARPSHELL_FORCE_INTERACTIVE", "1")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("failed to spawn larpshell");

    let bytes = stdin_data.to_vec();
    let mut pipe = child.stdin.take().unwrap();
    std::thread::spawn(move || {
        let _ = pipe.write_all(&bytes);
    });

    child
        .wait_with_output()
        .expect("failed to wait for larpshell")
}

/// Like `run`, but feeds `stdin_data` into the binary's stdin before closing it.
fn run_with_stdin(
    home: &std::path::Path,
    args: &[&str],
    stdin_data: &[u8],
) -> std::process::Output {
    ensure_binary_built();
    let mut child = Command::new(binary())
        .args(args)
        .env("HOME", home)
        .env("XDG_CONFIG_HOME", home.join("config"))
        .env("NO_COLOR", "1")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("failed to spawn larpshell");

    let bytes = stdin_data.to_vec();
    let mut pipe = child.stdin.take().unwrap();
    std::thread::spawn(move || {
        let _ = pipe.write_all(&bytes);
        // dropping `pipe` closes stdin → binary sees EOF
    });

    child
        .wait_with_output()
        .expect("failed to wait for larpshell")
}

// ── history subcommand tests ─────────────────────────────────────────────────

#[test]
fn history_on_prints_confirmation() {
    let home = temp_home("history_on");
    let out = run(&home, &["history", "on"]);
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        out.status.success(),
        "history on should exit 0; stderr: {stderr}"
    );
    assert!(
        stderr.contains("history") && (stderr.contains("on") || stderr.contains("enabled")),
        "expected confirmation message; stderr: {stderr}"
    );
    let disabled_flag = home
        .join("config")
        .join("larpshell")
        .join(".history-disabled");
    assert!(
        !disabled_flag.exists(),
        ".history-disabled must not exist after 'history on'"
    );
}

#[test]
fn history_off_creates_disabled_flag_and_prints_confirmation() {
    let home = temp_home("history_off");

    let out = run(&home, &["history", "off"]);
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        out.status.success(),
        "history off should exit 0; stderr: {stderr}"
    );
    assert!(
        stderr.contains("history") && (stderr.contains("off") || stderr.contains("disabled")),
        "expected confirmation message; stderr: {stderr}"
    );
    let disabled_flag = home
        .join("config")
        .join("larpshell")
        .join(".history-disabled");
    assert!(
        disabled_flag.exists(),
        ".history-disabled must exist after 'history off'"
    );
}

#[test]
fn history_enabled_by_default() {
    let home = temp_home("history_default");
    let disabled_flag = home
        .join("config")
        .join("larpshell")
        .join(".history-disabled");
    assert!(
        !disabled_flag.exists(),
        ".history-disabled must not exist in a fresh home"
    );
}

// ── provider config tests ───────────────────────────────────────────────────

#[test]
fn openrouter_config_parsing_and_resolution_succeeds() {
    let config_toml = r#"
provider = "openrouter"

[providers.openrouter]
base_url = "https://openrouter.ai/api/v1"
api_key = "test-openrouter-key"
model = "openrouter/auto"
"#;

    let config: Config = from_str(config_toml).expect("openrouter TOML should parse");
    assert_eq!(config.active_provider, ActiveProvider::OpenRouter);

    let provider_config = config
        .provider_config()
        .expect("openrouter provider config should resolve");

    assert_eq!(provider_config.provider_type, ActiveProvider::OpenRouter);
    match provider_config.config {
        ProviderSpecificConfig::OpenRouter { openrouter } => {
            assert_eq!(openrouter.base_url, "https://openrouter.ai/api/v1");
            assert_eq!(openrouter.api_key.as_deref(), Some("test-openrouter-key"));
            assert_eq!(openrouter.model, "openrouter/auto");
        }
        other => panic!("expected OpenRouter config, got {other:?}"),
    }
}

#[test]
fn openrouter_missing_provider_config_returns_config_error() {
    let config_toml = r#"
provider = "openrouter"
"#;

    let config: Config = from_str(config_toml).expect("openrouter provider enum should parse");
    let error = config
        .provider_config()
        .expect_err("missing openrouter config should return an error");

    match error {
        LarpshellError::ConfigError(message) => {
            assert!(message.contains("openrouter"));
            assert!(message.contains("config not found"));
        }
        other => panic!("expected config error, got {other:?}"),
    }
}

#[test]
fn create_provider_with_openrouter_config_reports_openrouter_name() {
    let config_toml = r#"
provider = "openrouter"

[providers.openrouter]
base_url = "https://openrouter.ai/api/v1"
api_key = "test-openrouter-key"
model = "openrouter/auto"
"#;

    let config: Config = from_str(config_toml).expect("openrouter TOML should parse");
    let provider = create_provider(&config)
        .expect("openrouter provider should be created when support is implemented");

    assert!(
        provider.name().contains("OpenRouter"),
        "expected provider name to identify OpenRouter, got {}",
        provider.name()
    );
}

// ── error formatting tests ──────────────────────────────────────────────────

#[test]
fn connection_failure_prints_pretty_error() {
    let home = temp_home("conn_fail");
    // write config pointing at port unlikely to be open
    write_ollama_config(&home, 1);

    let out = run(&home, &["show disk usage"]);
    assert!(!out.status.success());
    let stderr = String::from_utf8_lossy(&out.stderr);
    eprintln!("stderr for debug: {stderr:?}");
    // should include the styled prefix and the friendly message
    assert!(stderr.contains("error:") && stderr.contains("failed to connect"));
}

// ── agent config tests ─────────────────────────────────────────────────────

#[test]
fn agent_field_defaults_to_off() {
    let config_toml = r#"
provider = "ollama"

[providers.ollama]
base_url = "http://localhost:11434"
model = "test"
"#;
    let config: Config = from_str(config_toml).expect("should parse without agent field");
    assert_eq!(config.agent, AgentMode::Off);
    assert!(config.verbose_tool_output);
}

#[test]
fn agent_field_parses_mode_values() {
    for (value, expected) in [
        ("off", AgentMode::Off),
        ("safe", AgentMode::Safe),
        ("on", AgentMode::On),
    ] {
        let config_toml = format!(
            "provider = \"ollama\"\nagent = \"{value}\"\n\n[providers.ollama]\nbase_url = \"http://localhost:11434\"\nmodel = \"test\"\n"
        );
        let config: Config = from_str(&config_toml).expect("should parse agent mode");
        assert_eq!(config.agent, expected);
    }
}

#[test]
fn agent_field_parses_legacy_bool_values() {
    for (value, expected) in [("false", AgentMode::Off), ("true", AgentMode::On)] {
        let config_toml = format!(
            "provider = \"ollama\"\nagent = {value}\n\n[providers.ollama]\nbase_url = \"http://localhost:11434\"\nmodel = \"test\"\n"
        );
        let config: Config = from_str(&config_toml).expect("should parse legacy bool agent");
        assert_eq!(config.agent, expected);
    }
}

#[test]
fn verbose_tool_output_field_parses_false() {
    let config_toml = r#"
provider = "ollama"
verbose_tool_output = false

[providers.ollama]
base_url = "http://localhost:11434"
model = "test"
"#;
    let config: Config = from_str(config_toml).expect("should parse verbose tool output");
    assert!(!config.verbose_tool_output);
}

#[test]
fn agent_subcommand_on_prints_confirmation() {
    let home = temp_home("agent_on");
    let port = mock_ollama(&[]);
    write_ollama_config(&home, port);
    let out = run(&home, &["agent", "on"]);
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        out.status.success(),
        "agent on should exit 0; stderr: {stderr}"
    );
    assert!(
        stderr.contains("agent mode: on"),
        "expected confirmation message; stderr: {stderr}"
    );
    let config_path = home.join("config").join("larpshell").join("config.toml");
    let contents = fs::read_to_string(config_path).unwrap();
    assert!(
        contents.contains("agent = \"on\""),
        "config should have agent = \"on\""
    );
}

#[test]
fn agent_subcommand_safe_prints_confirmation() {
    let home = temp_home("agent_safe");
    let port = mock_ollama(&[]);
    write_ollama_config(&home, port);
    let out = run(&home, &["agent", "safe"]);
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        out.status.success(),
        "agent safe should exit 0; stderr: {stderr}"
    );
    assert!(
        stderr.contains("agent mode: safe"),
        "expected confirmation message; stderr: {stderr}"
    );
    let config_path = home.join("config").join("larpshell").join("config.toml");
    let contents = fs::read_to_string(config_path).unwrap();
    assert!(
        contents.contains("agent = \"safe\""),
        "config should have agent = \"safe\""
    );
}

#[test]
fn agent_subcommand_off_prints_confirmation() {
    let home = temp_home("agent_off");
    let port = mock_ollama(&[]);
    write_ollama_config(&home, port);
    run(&home, &["agent", "on"]);
    let out = run(&home, &["agent", "off"]);
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        out.status.success(),
        "agent off should exit 0; stderr: {stderr}"
    );
    assert!(
        stderr.contains("agent mode: off"),
        "expected confirmation message; stderr: {stderr}"
    );
    let config_path = home.join("config").join("larpshell").join("config.toml");
    let contents = fs::read_to_string(config_path).unwrap();
    assert!(
        contents.contains("agent = \"off\""),
        "config should have agent = \"off\""
    );
}