claude-code 0.1.2

A Rust library for executing Claude Code CLI
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/// Conditional tracing macros — compile to nothing when the `tracing` feature is disabled.
macro_rules! trace_debug {
    ($($arg:tt)*) => {
        #[cfg(feature = "tracing")]
        tracing::debug!($($arg)*);
    };
}
macro_rules! trace_error {
    ($($arg:tt)*) => {
        #[cfg(feature = "tracing")]
        tracing::error!($($arg)*);
    };
}
macro_rules! trace_info {
    ($($arg:tt)*) => {
        #[cfg(feature = "tracing")]
        tracing::info!($($arg)*);
    };
}

#[cfg(test)]
use mockall::automock;

use std::process::Output;

use tokio::process::Command as TokioCommand;

use crate::config::ClaudeConfig;
use crate::conversation::Conversation;
use crate::error::ClaudeError;
use crate::types::{ClaudeResponse, strip_ansi};

#[cfg(feature = "stream")]
use crate::stream::{StreamEvent, parse_stream};
#[cfg(feature = "stream")]
use std::pin::Pin;
#[cfg(feature = "stream")]
use tokio::io::BufReader;
#[cfg(feature = "stream")]
use tokio_stream::Stream;

/// Trait abstracting CLI execution. Mockable in tests.
#[allow(async_fn_in_trait)]
#[cfg_attr(test, automock)]
pub trait CommandRunner: Send + Sync {
    /// Runs the `claude` command with the given arguments.
    async fn run(&self, args: &[String]) -> std::io::Result<Output>;
}

/// Runs `claude` via `tokio::process::Command`.
#[derive(Debug, Clone)]
pub struct DefaultRunner {
    cli_path: String,
}

impl DefaultRunner {
    /// Creates a runner with a custom CLI binary path.
    #[must_use]
    pub fn new(cli_path: impl Into<String>) -> Self {
        Self {
            cli_path: cli_path.into(),
        }
    }
}

impl Default for DefaultRunner {
    fn default() -> Self {
        Self {
            cli_path: "claude".into(),
        }
    }
}

impl CommandRunner for DefaultRunner {
    async fn run(&self, args: &[String]) -> std::io::Result<Output> {
        TokioCommand::new(&self.cli_path).args(args).output().await
    }
}

/// RAII guard that kills the child process on drop.
///
/// tokio's `Child` does NOT kill the process on drop — it detaches.
/// This guard ensures the CLI subprocess is killed when the stream
/// is dropped (e.g., client disconnection).
#[cfg(feature = "stream")]
struct ChildGuard(Option<tokio::process::Child>);

#[cfg(feature = "stream")]
impl Drop for ChildGuard {
    fn drop(&mut self) {
        if let Some(ref mut child) = self.0 {
            let _ = child.start_kill();
        }
    }
}

/// Claude Code CLI client.
#[derive(Debug, Clone)]
pub struct ClaudeClient<R: CommandRunner = DefaultRunner> {
    config: ClaudeConfig,
    runner: R,
}

impl ClaudeClient {
    /// Creates a new client with the default [`DefaultRunner`].
    #[must_use]
    pub fn new(config: ClaudeConfig) -> Self {
        let runner = DefaultRunner::new(config.cli_path_or_default());
        Self { config, runner }
    }
}

#[cfg(feature = "stream")]
#[cfg_attr(docsrs, doc(cfg(feature = "stream")))]
impl ClaudeClient {
    /// Sends a prompt and returns a stream of events.
    ///
    /// Spawns the CLI with `--output-format stream-json` and streams events
    /// in real-time. The stream ends with a [`StreamEvent::Result`] on success.
    ///
    /// For real-time token-level streaming, enable
    /// [`crate::ClaudeConfigBuilder::include_partial_messages`]. This produces
    /// [`StreamEvent::Text`] / [`StreamEvent::Thinking`] delta chunks.
    /// Without it, only complete [`StreamEvent::AssistantText`] /
    /// [`StreamEvent::AssistantThinking`] messages are emitted.
    ///
    /// Use [`crate::ClaudeConfigBuilder::stream_idle_timeout`] to set an idle timeout.
    /// If no event arrives within the specified duration, the stream yields
    /// [`ClaudeError::Timeout`] and terminates.
    pub async fn ask_stream(
        &self,
        prompt: &str,
    ) -> Result<Pin<Box<dyn Stream<Item = Result<StreamEvent, ClaudeError>> + Send>>, ClaudeError>
    {
        let args = self.config.to_stream_args(prompt);

        trace_debug!(args = ?args, "spawning claude CLI stream");

        let mut child = TokioCommand::new(self.config.cli_path_or_default())
            .args(&args)
            .stdin(std::process::Stdio::null())
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .spawn()
            .map_err(|e| {
                if e.kind() == std::io::ErrorKind::NotFound {
                    ClaudeError::CliNotFound
                } else {
                    ClaudeError::Io(e)
                }
            })?;

        let stdout = child.stdout.take().expect("stdout must be piped");
        let reader = BufReader::new(stdout);
        let event_stream = parse_stream(reader);
        let mut guard = ChildGuard(Some(child));
        let idle_timeout = self.config.stream_idle_timeout;

        Ok(Box::pin(async_stream::stream! {
            tokio::pin!(event_stream);

            loop {
                let next = tokio_stream::StreamExt::next(&mut event_stream);
                let maybe_event = if let Some(timeout_dur) = idle_timeout {
                    match tokio::time::timeout(timeout_dur, next).await {
                        Ok(Some(event)) => Some(event),
                        Ok(None) => None,
                        Err(_) => {
                            trace_error!("stream idle timeout");
                            yield Err(ClaudeError::Timeout);
                            return;
                        }
                    }
                } else {
                    next.await
                };

                match maybe_event {
                    Some(event) => yield Ok(event),
                    None => break,
                }
            }

            // Take child out of guard to wait for exit status.
            // If stream is dropped before reaching here, guard's Drop kills the process.
            if let Some(mut child) = guard.0.take() {
                let status = child.wait().await;
                match status {
                    Ok(s) if !s.success() => {
                        let code = s.code().unwrap_or(-1);
                        let mut stderr_buf = Vec::new();
                        if let Some(mut stderr) = child.stderr.take() {
                            let _ = tokio::io::AsyncReadExt::read_to_end(&mut stderr, &mut stderr_buf).await;
                        }
                        let stderr_str = String::from_utf8_lossy(&stderr_buf).into_owned();
                        yield Err(ClaudeError::NonZeroExit { code, stderr: stderr_str });
                    }
                    Err(e) => {
                        yield Err(ClaudeError::Io(e));
                    }
                    Ok(_) => {}
                }
            }
        }))
    }
}

impl<R: CommandRunner> ClaudeClient<R> {
    /// Creates a new client with a custom [`CommandRunner`] for testing.
    #[must_use]
    pub fn with_runner(config: ClaudeConfig, runner: R) -> Self {
        Self { config, runner }
    }

    /// Sends a prompt and deserializes the result into `T`.
    ///
    /// Requires `json_schema` to be set on the config beforehand.
    /// Use [`generate_schema`](crate::generate_schema) to auto-generate it
    /// (requires the `structured` feature).
    pub async fn ask_structured<T: serde::de::DeserializeOwned>(
        &self,
        prompt: &str,
    ) -> Result<T, ClaudeError> {
        let response = self.ask(prompt).await?;
        response.parse_result()
    }

    /// Sends a prompt and returns the response.
    pub async fn ask(&self, prompt: &str) -> Result<ClaudeResponse, ClaudeError> {
        let args = self.config.to_args(prompt);

        trace_debug!(args = ?args, "executing claude CLI");

        let io_result: std::io::Result<Output> = if let Some(timeout) = self.config.timeout {
            tokio::time::timeout(timeout, self.runner.run(&args))
                .await
                .map_err(|_| {
                    let err = ClaudeError::Timeout;
                    trace_error!(error = %err, "claude CLI failed");
                    err
                })?
        } else {
            self.runner.run(&args).await
        };

        let output = io_result.map_err(|e| {
            let err = if e.kind() == std::io::ErrorKind::NotFound {
                ClaudeError::CliNotFound
            } else {
                ClaudeError::Io(e)
            };
            trace_error!(error = %err, "claude CLI failed");
            err
        })?;

        if !output.status.success() {
            let code = output.status.code().unwrap_or(-1);
            let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
            let err = ClaudeError::NonZeroExit { code, stderr };
            trace_error!(error = %err, "claude CLI failed");
            return Err(err);
        }

        let stdout = String::from_utf8_lossy(&output.stdout);
        let json_str = strip_ansi(&stdout);
        let response: ClaudeResponse = serde_json::from_str(json_str).map_err(|e| {
            let err = ClaudeError::ParseError(e);
            trace_error!(error = %err, "claude CLI failed");
            err
        })?;

        trace_info!("claude CLI returned successfully");
        Ok(response)
    }
}

impl<R: CommandRunner + Clone> ClaudeClient<R> {
    /// Creates a new [`Conversation`] for multi-turn interaction.
    ///
    /// The conversation manages `session_id` automatically, injecting
    /// `--resume` from the second turn onwards.
    ///
    /// Callers must set [`crate::ClaudeConfigBuilder::no_session_persistence`]`(false)`
    /// for multi-turn to work.
    #[must_use]
    pub fn conversation(&self) -> Conversation<R> {
        Conversation::with_runner(self.config.clone(), self.runner.clone())
    }

    /// Creates a [`Conversation`] that resumes an existing session.
    ///
    /// The first `ask()` / `ask_stream()` call will include `--resume`
    /// with the given session ID.
    #[must_use]
    pub fn conversation_resume(&self, session_id: impl Into<String>) -> Conversation<R> {
        Conversation::with_runner_resume(self.config.clone(), self.runner.clone(), session_id)
    }
}

/// Checks that the `claude` CLI is available and returns its version string.
///
/// Runs `claude --version` and returns the trimmed stdout on success.
/// To check a binary at a custom path, use [`check_cli_with_path`].
///
/// # Errors
///
/// - [`ClaudeError::CliNotFound`] if `claude` is not in PATH.
/// - [`ClaudeError::NonZeroExit`] if the command fails.
/// - [`ClaudeError::Io`] for other I/O errors.
pub async fn check_cli() -> Result<String, ClaudeError> {
    check_cli_with_path("claude").await
}

/// Checks that the CLI at the given path is available and returns its version string.
///
/// Runs `<cli_path> --version` and returns the trimmed stdout on success.
///
/// # Errors
///
/// - [`ClaudeError::CliNotFound`] if the binary is not found.
/// - [`ClaudeError::NonZeroExit`] if the command fails.
/// - [`ClaudeError::Io`] for other I/O errors.
pub async fn check_cli_with_path(cli_path: &str) -> Result<String, ClaudeError> {
    let output = TokioCommand::new(cli_path)
        .arg("--version")
        .output()
        .await
        .map_err(|e| {
            if e.kind() == std::io::ErrorKind::NotFound {
                ClaudeError::CliNotFound
            } else {
                ClaudeError::Io(e)
            }
        })?;

    if !output.status.success() {
        let code = output.status.code().unwrap_or(-1);
        let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
        return Err(ClaudeError::NonZeroExit { code, stderr });
    }

    let version = String::from_utf8_lossy(&output.stdout).trim().to_string();
    Ok(version)
}

/// Parses a version string like `"2.1.92"` or `"claude-code 2.1.92"` into `(major, minor, patch)`.
///
/// Returns `None` if no valid semver triple is found.
fn parse_version(version: &str) -> Option<(u64, u64, u64)> {
    // Take the last whitespace-delimited token to handle prefixes like "claude-code 2.1.92"
    let ver = version.split_whitespace().next_back()?;
    let mut parts = ver.splitn(3, '.');
    let major = parts.next()?.parse().ok()?;
    let minor = parts.next()?.parse().ok()?;
    let patch = parts.next()?.parse().ok()?;
    Some((major, minor, patch))
}

/// Result of comparing the installed CLI version against [`TESTED_CLI_VERSION`](crate::TESTED_CLI_VERSION).
///
/// Each variant carries the raw version string returned by `claude --version`.
/// The library does not judge any status as an error — callers decide how to
/// handle each case (e.g. log a warning, reject, or ignore).
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum CliVersionStatus {
    /// Installed version exactly matches `TESTED_CLI_VERSION`.
    Exact(String),
    /// Installed version is newer than `TESTED_CLI_VERSION`.
    Newer(String),
    /// Installed version is older than `TESTED_CLI_VERSION`.
    Older(String),
    /// Installed version string could not be parsed.
    Unknown(String),
}

/// Compares an `installed` version string against a `tested` version string.
fn compare_version(installed: &str, tested: &str) -> CliVersionStatus {
    let tested_tuple = parse_version(tested).unwrap_or((0, 0, 0));
    match parse_version(installed) {
        None => CliVersionStatus::Unknown(installed.to_string()),
        Some(v) if v == tested_tuple => CliVersionStatus::Exact(installed.to_string()),
        Some(v) if v > tested_tuple => CliVersionStatus::Newer(installed.to_string()),
        Some(_) => CliVersionStatus::Older(installed.to_string()),
    }
}

/// Checks the installed `claude` CLI version against [`TESTED_CLI_VERSION`](crate::TESTED_CLI_VERSION).
///
/// Runs `claude --version` and returns a [`CliVersionStatus`] indicating
/// whether the installed version is exact, newer, older, or unparseable.
///
/// # Errors
///
/// - [`ClaudeError::CliNotFound`] if `claude` is not in PATH.
/// - [`ClaudeError::NonZeroExit`] if the command fails.
/// - [`ClaudeError::Io`] for other I/O errors.
pub async fn check_cli_version() -> Result<CliVersionStatus, ClaudeError> {
    check_cli_version_with_path("claude").await
}

/// Checks the CLI at the given path against [`TESTED_CLI_VERSION`](crate::TESTED_CLI_VERSION).
///
/// Returns a [`CliVersionStatus`] indicating the comparison result.
///
/// # Errors
///
/// - [`ClaudeError::CliNotFound`] if the binary is not found.
/// - [`ClaudeError::NonZeroExit`] if the command fails.
/// - [`ClaudeError::Io`] for other I/O errors.
pub async fn check_cli_version_with_path(cli_path: &str) -> Result<CliVersionStatus, ClaudeError> {
    let version = check_cli_with_path(cli_path).await?;
    Ok(compare_version(&version, crate::TESTED_CLI_VERSION))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::os::unix::process::ExitStatusExt;
    use std::process::ExitStatus;

    fn success_output() -> Output {
        Output {
            status: ExitStatus::from_raw(0),
            stdout: include_bytes!("../tests/fixtures/success.json").to_vec(),
            stderr: Vec::new(),
        }
    }

    fn non_zero_output() -> Output {
        Output {
            status: ExitStatus::from_raw(256), // exit code 1
            stdout: Vec::new(),
            stderr: b"something went wrong".to_vec(),
        }
    }

    #[tokio::test]
    async fn ask_success() {
        let mut mock = MockCommandRunner::new();
        mock.expect_run().returning(|_| Ok(success_output()));

        let client = ClaudeClient::with_runner(ClaudeConfig::default(), mock);
        let resp = client.ask("hello").await.unwrap();
        assert_eq!(resp.result, "Hello!");
        assert!(!resp.is_error);
    }

    #[tokio::test]
    async fn ask_cli_not_found() {
        let mut mock = MockCommandRunner::new();
        mock.expect_run().returning(|_| {
            Err(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                "not found",
            ))
        });

        let client = ClaudeClient::with_runner(ClaudeConfig::default(), mock);
        let err = client.ask("hello").await.unwrap_err();
        assert!(matches!(err, ClaudeError::CliNotFound));
    }

    #[tokio::test]
    async fn ask_non_zero_exit() {
        let mut mock = MockCommandRunner::new();
        mock.expect_run().returning(|_| Ok(non_zero_output()));

        let client = ClaudeClient::with_runner(ClaudeConfig::default(), mock);
        let err = client.ask("hello").await.unwrap_err();
        assert!(matches!(err, ClaudeError::NonZeroExit { code: 1, .. }));
    }

    #[tokio::test]
    async fn ask_parse_error() {
        let mut mock = MockCommandRunner::new();
        mock.expect_run().returning(|_| {
            Ok(Output {
                status: ExitStatus::from_raw(0),
                stdout: b"not json".to_vec(),
                stderr: Vec::new(),
            })
        });

        let client = ClaudeClient::with_runner(ClaudeConfig::default(), mock);
        let err = client.ask("hello").await.unwrap_err();
        assert!(matches!(err, ClaudeError::ParseError(_)));
    }

    /// Custom CommandRunner that always sleeps (for timeout tests).
    struct SlowRunner;

    impl CommandRunner for SlowRunner {
        async fn run(&self, _args: &[String]) -> std::io::Result<Output> {
            tokio::time::sleep(std::time::Duration::from_secs(10)).await;
            Ok(Output {
                status: std::os::unix::process::ExitStatusExt::from_raw(0),
                stdout: Vec::new(),
                stderr: Vec::new(),
            })
        }
    }

    #[tokio::test(start_paused = true)]
    async fn ask_timeout() {
        let config = ClaudeConfig::builder()
            .timeout(std::time::Duration::from_millis(10))
            .build();
        let client = ClaudeClient::with_runner(config, SlowRunner);
        let err = client.ask("hello").await.unwrap_err();
        assert!(matches!(err, ClaudeError::Timeout));
    }

    #[tokio::test]
    async fn ask_io_error() {
        let mut mock = MockCommandRunner::new();
        mock.expect_run().returning(|_| {
            Err(std::io::Error::new(
                std::io::ErrorKind::PermissionDenied,
                "denied",
            ))
        });

        let client = ClaudeClient::with_runner(ClaudeConfig::default(), mock);
        let err = client.ask("hello").await.unwrap_err();
        assert!(matches!(err, ClaudeError::Io(_)));
    }

    #[tokio::test]
    async fn ask_with_ansi_escape() {
        let json = include_str!("../tests/fixtures/success.json");
        let stdout = format!("\x1b[?1004l{json}\x1b[?1004l");

        let mut mock = MockCommandRunner::new();
        mock.expect_run().returning(move |_| {
            Ok(Output {
                status: ExitStatus::from_raw(0),
                stdout: stdout.clone().into_bytes(),
                stderr: Vec::new(),
            })
        });

        let client = ClaudeClient::with_runner(ClaudeConfig::default(), mock);
        let resp = client.ask("hello").await.unwrap();
        assert_eq!(resp.result, "Hello!");
    }

    #[tokio::test]
    async fn ask_passes_correct_args() {
        let mut mock = MockCommandRunner::new();
        mock.expect_run()
            .withf(|args| {
                args.contains(&"--print".to_string())
                    && args.contains(&"--model".to_string())
                    && args.contains(&"haiku".to_string())
                    && args.last() == Some(&"test prompt".to_string())
            })
            .returning(|_| Ok(success_output()));

        let config = ClaudeConfig::builder().model("haiku").build();
        let client = ClaudeClient::with_runner(config, mock);
        client.ask("test prompt").await.unwrap();
    }

    #[derive(Debug, serde::Deserialize, PartialEq)]
    struct TestAnswer {
        value: i32,
    }

    fn structured_success_output() -> Output {
        Output {
            status: ExitStatus::from_raw(0),
            stdout: include_bytes!("../tests/fixtures/structured_success.json").to_vec(),
            stderr: Vec::new(),
        }
    }

    #[tokio::test]
    async fn ask_structured_success() {
        let mut mock = MockCommandRunner::new();
        mock.expect_run()
            .returning(|_| Ok(structured_success_output()));

        let client = ClaudeClient::with_runner(ClaudeConfig::default(), mock);
        let answer: TestAnswer = client.ask_structured("What is 6*7?").await.unwrap();
        assert_eq!(answer, TestAnswer { value: 42 });
    }

    #[tokio::test]
    async fn ask_structured_deserialization_error() {
        let mut mock = MockCommandRunner::new();
        mock.expect_run().returning(|_| Ok(success_output()));

        let client = ClaudeClient::with_runner(ClaudeConfig::default(), mock);
        let err = client
            .ask_structured::<TestAnswer>("hello")
            .await
            .unwrap_err();
        assert!(matches!(err, ClaudeError::StructuredOutputError { .. }));
    }

    #[tokio::test]
    async fn ask_structured_cli_error() {
        let mut mock = MockCommandRunner::new();
        mock.expect_run().returning(|_| Ok(non_zero_output()));

        let client = ClaudeClient::with_runner(ClaudeConfig::default(), mock);
        let err = client
            .ask_structured::<TestAnswer>("hello")
            .await
            .unwrap_err();
        assert!(matches!(err, ClaudeError::NonZeroExit { code: 1, .. }));
    }

    /// Verifies that shell metacharacters in `cli_path` are not interpreted.
    ///
    /// `Command::new()` uses `execvp` directly (no shell), so a path like
    /// `"claude; echo pwned"` is treated as a literal filename lookup and
    /// fails with `NotFound` — not as a shell command.
    #[tokio::test]
    async fn cli_path_with_shell_metacharacters_is_not_interpreted() {
        let malicious = "claude; echo pwned";
        let err = check_cli_with_path(malicious).await.unwrap_err();
        assert!(matches!(err, ClaudeError::CliNotFound));
    }

    #[tokio::test]
    async fn cli_path_with_command_substitution_is_not_interpreted() {
        let malicious = "$(echo claude)";
        let err = check_cli_with_path(malicious).await.unwrap_err();
        assert!(matches!(err, ClaudeError::CliNotFound));
    }

    #[test]
    fn parse_version_semver() {
        assert_eq!(parse_version("2.1.92"), Some((2, 1, 92)));
    }

    #[test]
    fn parse_version_with_prefix() {
        assert_eq!(parse_version("claude-code 2.1.92"), Some((2, 1, 92)));
    }

    #[test]
    fn parse_version_invalid() {
        assert_eq!(parse_version("not-a-version"), None);
    }

    #[test]
    fn parse_version_empty() {
        assert_eq!(parse_version(""), None);
    }

    #[test]
    fn parse_version_two_components() {
        assert_eq!(parse_version("2.1"), None);
    }

    #[test]
    fn parse_version_four_components() {
        // splitn(3, '.') yields ["2", "1", "92.1"] — "92.1" fails u64 parse
        assert_eq!(parse_version("2.1.92.1"), None);
    }

    #[test]
    fn compare_version_exact() {
        let status = compare_version("2.1.92", "2.1.92");
        assert!(matches!(status, CliVersionStatus::Exact(_)));
    }

    #[test]
    fn compare_version_newer() {
        let status = compare_version("2.2.0", "2.1.92");
        assert!(matches!(status, CliVersionStatus::Newer(_)));
    }

    #[test]
    fn compare_version_older() {
        let status = compare_version("2.0.0", "2.1.92");
        assert!(matches!(status, CliVersionStatus::Older(_)));
    }

    #[test]
    fn compare_version_major_newer() {
        let status = compare_version("3.0.0", "2.1.92");
        assert!(matches!(status, CliVersionStatus::Newer(_)));
    }

    #[test]
    fn compare_version_major_older() {
        let status = compare_version("1.9.99", "2.1.92");
        assert!(matches!(status, CliVersionStatus::Older(_)));
    }

    #[test]
    fn compare_version_unparseable() {
        let status = compare_version("garbage", "2.1.92");
        assert!(matches!(status, CliVersionStatus::Unknown(_)));
    }

    #[test]
    fn compare_version_with_prefix() {
        let status = compare_version("claude-code 2.1.92", "2.1.92");
        assert!(matches!(status, CliVersionStatus::Exact(_)));
    }

    #[test]
    fn cli_version_status_preserves_version_string() {
        let status = compare_version("2.2.0", "2.1.92");
        match status {
            CliVersionStatus::Newer(v) => assert_eq!(v, "2.2.0"),
            other => panic!("expected Newer, got {other:?}"),
        }
    }
}