1#[cfg(feature = "async-client")]
13use crate::error::{Error, Result};
14use log::debug;
15use std::path::PathBuf;
16use std::process::Stdio;
17use uuid::Uuid;
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum PermissionMode {
22 AcceptEdits,
23 BypassPermissions,
24 Default,
25 Delegate,
26 DontAsk,
27 Plan,
28}
29
30impl PermissionMode {
31 pub fn as_str(&self) -> &'static str {
33 match self {
34 PermissionMode::AcceptEdits => "acceptEdits",
35 PermissionMode::BypassPermissions => "bypassPermissions",
36 PermissionMode::Default => "default",
37 PermissionMode::Delegate => "delegate",
38 PermissionMode::DontAsk => "dontAsk",
39 PermissionMode::Plan => "plan",
40 }
41 }
42}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46pub enum InputFormat {
47 Text,
48 StreamJson,
49}
50
51impl InputFormat {
52 pub fn as_str(&self) -> &'static str {
54 match self {
55 InputFormat::Text => "text",
56 InputFormat::StreamJson => "stream-json",
57 }
58 }
59}
60
61#[derive(Debug, Clone, Copy, PartialEq, Eq)]
63pub enum OutputFormat {
64 Text,
65 Json,
66 StreamJson,
67}
68
69impl OutputFormat {
70 pub fn as_str(&self) -> &'static str {
72 match self {
73 OutputFormat::Text => "text",
74 OutputFormat::Json => "json",
75 OutputFormat::StreamJson => "stream-json",
76 }
77 }
78}
79
80#[derive(Debug, Clone)]
97pub enum CliFlag {
98 AddDir(Vec<PathBuf>),
100 Agent(String),
102 Agents(String),
104 AllowDangerouslySkipPermissions,
106 AllowedTools(Vec<String>),
108 AppendSystemPrompt(String),
110 Betas(Vec<String>),
112 Chrome,
114 Continue,
116 DangerouslySkipPermissions,
118 Debug(Option<String>),
120 DebugFile(PathBuf),
122 DisableSlashCommands,
124 DisallowedTools(Vec<String>),
126 FallbackModel(String),
128 File(Vec<String>),
130 ForkSession,
132 FromPr(Option<String>),
134 IncludePartialMessages,
136 InputFormat(InputFormat),
138 JsonSchema(String),
140 MaxBudgetUsd(f64),
142 McpConfig(Vec<String>),
144 McpDebug,
146 Model(String),
148 NoChrome,
150 NoSessionPersistence,
152 OutputFormat(OutputFormat),
154 PermissionMode(PermissionMode),
156 PermissionPromptTool(String),
158 PluginDir(Vec<PathBuf>),
160 Print,
162 ReplayUserMessages,
164 Resume(Option<String>),
166 SessionId(String),
168 SettingSources(String),
170 Settings(String),
172 StrictMcpConfig,
174 SystemPrompt(String),
176 Tools(Vec<String>),
178 Verbose,
180}
181
182impl CliFlag {
183 pub fn as_flag(&self) -> &'static str {
185 match self {
186 CliFlag::AddDir(_) => "--add-dir",
187 CliFlag::Agent(_) => "--agent",
188 CliFlag::Agents(_) => "--agents",
189 CliFlag::AllowDangerouslySkipPermissions => "--allow-dangerously-skip-permissions",
190 CliFlag::AllowedTools(_) => "--allowed-tools",
191 CliFlag::AppendSystemPrompt(_) => "--append-system-prompt",
192 CliFlag::Betas(_) => "--betas",
193 CliFlag::Chrome => "--chrome",
194 CliFlag::Continue => "--continue",
195 CliFlag::DangerouslySkipPermissions => "--dangerously-skip-permissions",
196 CliFlag::Debug(_) => "--debug",
197 CliFlag::DebugFile(_) => "--debug-file",
198 CliFlag::DisableSlashCommands => "--disable-slash-commands",
199 CliFlag::DisallowedTools(_) => "--disallowed-tools",
200 CliFlag::FallbackModel(_) => "--fallback-model",
201 CliFlag::File(_) => "--file",
202 CliFlag::ForkSession => "--fork-session",
203 CliFlag::FromPr(_) => "--from-pr",
204 CliFlag::IncludePartialMessages => "--include-partial-messages",
205 CliFlag::InputFormat(_) => "--input-format",
206 CliFlag::JsonSchema(_) => "--json-schema",
207 CliFlag::MaxBudgetUsd(_) => "--max-budget-usd",
208 CliFlag::McpConfig(_) => "--mcp-config",
209 CliFlag::McpDebug => "--mcp-debug",
210 CliFlag::Model(_) => "--model",
211 CliFlag::NoChrome => "--no-chrome",
212 CliFlag::NoSessionPersistence => "--no-session-persistence",
213 CliFlag::OutputFormat(_) => "--output-format",
214 CliFlag::PermissionMode(_) => "--permission-mode",
215 CliFlag::PermissionPromptTool(_) => "--permission-prompt-tool",
216 CliFlag::PluginDir(_) => "--plugin-dir",
217 CliFlag::Print => "--print",
218 CliFlag::ReplayUserMessages => "--replay-user-messages",
219 CliFlag::Resume(_) => "--resume",
220 CliFlag::SessionId(_) => "--session-id",
221 CliFlag::SettingSources(_) => "--setting-sources",
222 CliFlag::Settings(_) => "--settings",
223 CliFlag::StrictMcpConfig => "--strict-mcp-config",
224 CliFlag::SystemPrompt(_) => "--system-prompt",
225 CliFlag::Tools(_) => "--tools",
226 CliFlag::Verbose => "--verbose",
227 }
228 }
229
230 pub fn to_args(&self) -> Vec<String> {
232 let flag = self.as_flag().to_string();
233 match self {
234 CliFlag::AllowDangerouslySkipPermissions
236 | CliFlag::Chrome
237 | CliFlag::Continue
238 | CliFlag::DangerouslySkipPermissions
239 | CliFlag::DisableSlashCommands
240 | CliFlag::ForkSession
241 | CliFlag::IncludePartialMessages
242 | CliFlag::McpDebug
243 | CliFlag::NoChrome
244 | CliFlag::NoSessionPersistence
245 | CliFlag::Print
246 | CliFlag::ReplayUserMessages
247 | CliFlag::StrictMcpConfig
248 | CliFlag::Verbose => vec![flag],
249
250 CliFlag::Debug(filter) => match filter {
252 Some(f) => vec![flag, f.clone()],
253 None => vec![flag],
254 },
255 CliFlag::FromPr(value) | CliFlag::Resume(value) => match value {
256 Some(v) => vec![flag, v.clone()],
257 None => vec![flag],
258 },
259
260 CliFlag::Agent(v)
262 | CliFlag::Agents(v)
263 | CliFlag::AppendSystemPrompt(v)
264 | CliFlag::FallbackModel(v)
265 | CliFlag::JsonSchema(v)
266 | CliFlag::Model(v)
267 | CliFlag::PermissionPromptTool(v)
268 | CliFlag::SessionId(v)
269 | CliFlag::SettingSources(v)
270 | CliFlag::Settings(v)
271 | CliFlag::SystemPrompt(v) => vec![flag, v.clone()],
272
273 CliFlag::InputFormat(f) => vec![flag, f.as_str().to_string()],
275 CliFlag::OutputFormat(f) => vec![flag, f.as_str().to_string()],
276 CliFlag::PermissionMode(m) => vec![flag, m.as_str().to_string()],
277
278 CliFlag::MaxBudgetUsd(amount) => vec![flag, amount.to_string()],
280
281 CliFlag::DebugFile(p) => vec![flag, p.to_string_lossy().to_string()],
283
284 CliFlag::AllowedTools(items)
286 | CliFlag::Betas(items)
287 | CliFlag::DisallowedTools(items)
288 | CliFlag::File(items)
289 | CliFlag::McpConfig(items)
290 | CliFlag::Tools(items) => {
291 let mut args = vec![flag];
292 args.extend(items.clone());
293 args
294 }
295
296 CliFlag::AddDir(paths) | CliFlag::PluginDir(paths) => {
298 let mut args = vec![flag];
299 args.extend(paths.iter().map(|p| p.to_string_lossy().to_string()));
300 args
301 }
302 }
303 }
304
305 pub fn all_flags() -> Vec<(&'static str, &'static str)> {
318 vec![
319 ("AddDir", "--add-dir"),
320 ("Agent", "--agent"),
321 ("Agents", "--agents"),
322 (
323 "AllowDangerouslySkipPermissions",
324 "--allow-dangerously-skip-permissions",
325 ),
326 ("AllowedTools", "--allowed-tools"),
327 ("AppendSystemPrompt", "--append-system-prompt"),
328 ("Betas", "--betas"),
329 ("Chrome", "--chrome"),
330 ("Continue", "--continue"),
331 (
332 "DangerouslySkipPermissions",
333 "--dangerously-skip-permissions",
334 ),
335 ("Debug", "--debug"),
336 ("DebugFile", "--debug-file"),
337 ("DisableSlashCommands", "--disable-slash-commands"),
338 ("DisallowedTools", "--disallowed-tools"),
339 ("FallbackModel", "--fallback-model"),
340 ("File", "--file"),
341 ("ForkSession", "--fork-session"),
342 ("FromPr", "--from-pr"),
343 ("IncludePartialMessages", "--include-partial-messages"),
344 ("InputFormat", "--input-format"),
345 ("JsonSchema", "--json-schema"),
346 ("MaxBudgetUsd", "--max-budget-usd"),
347 ("McpConfig", "--mcp-config"),
348 ("McpDebug", "--mcp-debug"),
349 ("Model", "--model"),
350 ("NoChrome", "--no-chrome"),
351 ("NoSessionPersistence", "--no-session-persistence"),
352 ("OutputFormat", "--output-format"),
353 ("PermissionMode", "--permission-mode"),
354 ("PermissionPromptTool", "--permission-prompt-tool"),
355 ("PluginDir", "--plugin-dir"),
356 ("Print", "--print"),
357 ("ReplayUserMessages", "--replay-user-messages"),
358 ("Resume", "--resume"),
359 ("SessionId", "--session-id"),
360 ("SettingSources", "--setting-sources"),
361 ("Settings", "--settings"),
362 ("StrictMcpConfig", "--strict-mcp-config"),
363 ("SystemPrompt", "--system-prompt"),
364 ("Tools", "--tools"),
365 ("Verbose", "--verbose"),
366 ]
367 }
368}
369
370#[derive(Debug, Clone)]
378pub struct ClaudeCliBuilder {
379 command: PathBuf,
380 prompt: Option<String>,
381 debug: Option<String>,
382 verbose: bool,
383 dangerously_skip_permissions: bool,
384 allowed_tools: Vec<String>,
385 disallowed_tools: Vec<String>,
386 mcp_config: Vec<String>,
387 append_system_prompt: Option<String>,
388 permission_mode: Option<PermissionMode>,
389 continue_conversation: bool,
390 resume: Option<String>,
391 model: Option<String>,
392 fallback_model: Option<String>,
393 settings: Option<String>,
394 add_dir: Vec<PathBuf>,
395 ide: bool,
396 strict_mcp_config: bool,
397 session_id: Option<Uuid>,
398 oauth_token: Option<String>,
399 api_key: Option<String>,
400 permission_prompt_tool: Option<String>,
402}
403
404impl Default for ClaudeCliBuilder {
405 fn default() -> Self {
406 Self::new()
407 }
408}
409
410impl ClaudeCliBuilder {
411 pub fn new() -> Self {
413 Self {
414 command: PathBuf::from("claude"),
415 prompt: None,
416 debug: None,
417 verbose: false,
418 dangerously_skip_permissions: false,
419 allowed_tools: Vec::new(),
420 disallowed_tools: Vec::new(),
421 mcp_config: Vec::new(),
422 append_system_prompt: None,
423 permission_mode: None,
424 continue_conversation: false,
425 resume: None,
426 model: None,
427 fallback_model: None,
428 settings: None,
429 add_dir: Vec::new(),
430 ide: false,
431 strict_mcp_config: false,
432 session_id: None,
433 oauth_token: None,
434 api_key: None,
435 permission_prompt_tool: None,
436 }
437 }
438
439 pub fn command<P: Into<PathBuf>>(mut self, path: P) -> Self {
441 self.command = path.into();
442 self
443 }
444
445 pub fn prompt<S: Into<String>>(mut self, prompt: S) -> Self {
447 self.prompt = Some(prompt.into());
448 self
449 }
450
451 pub fn debug<S: Into<String>>(mut self, filter: Option<S>) -> Self {
453 self.debug = filter.map(|s| s.into());
454 self
455 }
456
457 pub fn verbose(mut self, verbose: bool) -> Self {
459 self.verbose = verbose;
460 self
461 }
462
463 pub fn dangerously_skip_permissions(mut self, skip: bool) -> Self {
465 self.dangerously_skip_permissions = skip;
466 self
467 }
468
469 pub fn allowed_tools<I, S>(mut self, tools: I) -> Self
471 where
472 I: IntoIterator<Item = S>,
473 S: Into<String>,
474 {
475 self.allowed_tools
476 .extend(tools.into_iter().map(|s| s.into()));
477 self
478 }
479
480 pub fn disallowed_tools<I, S>(mut self, tools: I) -> Self
482 where
483 I: IntoIterator<Item = S>,
484 S: Into<String>,
485 {
486 self.disallowed_tools
487 .extend(tools.into_iter().map(|s| s.into()));
488 self
489 }
490
491 pub fn mcp_config<I, S>(mut self, configs: I) -> Self
493 where
494 I: IntoIterator<Item = S>,
495 S: Into<String>,
496 {
497 self.mcp_config
498 .extend(configs.into_iter().map(|s| s.into()));
499 self
500 }
501
502 pub fn append_system_prompt<S: Into<String>>(mut self, prompt: S) -> Self {
504 self.append_system_prompt = Some(prompt.into());
505 self
506 }
507
508 pub fn permission_mode(mut self, mode: PermissionMode) -> Self {
510 self.permission_mode = Some(mode);
511 self
512 }
513
514 pub fn continue_conversation(mut self, continue_conv: bool) -> Self {
516 self.continue_conversation = continue_conv;
517 self
518 }
519
520 pub fn resume<S: Into<String>>(mut self, session_id: Option<S>) -> Self {
522 self.resume = session_id.map(|s| s.into());
523 self
524 }
525
526 pub fn model<S: Into<String>>(mut self, model: S) -> Self {
528 self.model = Some(model.into());
529 self
530 }
531
532 pub fn fallback_model<S: Into<String>>(mut self, model: S) -> Self {
534 self.fallback_model = Some(model.into());
535 self
536 }
537
538 pub fn settings<S: Into<String>>(mut self, settings: S) -> Self {
540 self.settings = Some(settings.into());
541 self
542 }
543
544 pub fn add_directories<I, P>(mut self, dirs: I) -> Self
546 where
547 I: IntoIterator<Item = P>,
548 P: Into<PathBuf>,
549 {
550 self.add_dir.extend(dirs.into_iter().map(|p| p.into()));
551 self
552 }
553
554 pub fn ide(mut self, ide: bool) -> Self {
556 self.ide = ide;
557 self
558 }
559
560 pub fn strict_mcp_config(mut self, strict: bool) -> Self {
562 self.strict_mcp_config = strict;
563 self
564 }
565
566 pub fn session_id(mut self, id: Uuid) -> Self {
568 self.session_id = Some(id);
569 self
570 }
571
572 pub fn oauth_token<S: Into<String>>(mut self, token: S) -> Self {
574 let token_str = token.into();
575 if !token_str.starts_with("sk-ant-oat") {
576 eprintln!("Warning: OAuth token should start with 'sk-ant-oat'");
577 }
578 self.oauth_token = Some(token_str);
579 self
580 }
581
582 pub fn api_key<S: Into<String>>(mut self, key: S) -> Self {
584 let key_str = key.into();
585 if !key_str.starts_with("sk-ant-api") {
586 eprintln!("Warning: API key should start with 'sk-ant-api'");
587 }
588 self.api_key = Some(key_str);
589 self
590 }
591
592 pub fn permission_prompt_tool<S: Into<String>>(mut self, tool: S) -> Self {
607 self.permission_prompt_tool = Some(tool.into());
608 self
609 }
610
611 fn build_args(&self) -> Vec<String> {
613 let mut args = vec![
616 "--print".to_string(),
617 "--verbose".to_string(),
618 "--output-format".to_string(),
619 "stream-json".to_string(),
620 "--input-format".to_string(),
621 "stream-json".to_string(),
622 ];
623
624 if let Some(ref debug) = self.debug {
625 args.push("--debug".to_string());
626 if !debug.is_empty() {
627 args.push(debug.clone());
628 }
629 }
630
631 if self.dangerously_skip_permissions {
632 args.push("--dangerously-skip-permissions".to_string());
633 }
634
635 if !self.allowed_tools.is_empty() {
636 args.push("--allowed-tools".to_string());
637 args.extend(self.allowed_tools.clone());
638 }
639
640 if !self.disallowed_tools.is_empty() {
641 args.push("--disallowed-tools".to_string());
642 args.extend(self.disallowed_tools.clone());
643 }
644
645 if !self.mcp_config.is_empty() {
646 args.push("--mcp-config".to_string());
647 args.extend(self.mcp_config.clone());
648 }
649
650 if let Some(ref prompt) = self.append_system_prompt {
651 args.push("--append-system-prompt".to_string());
652 args.push(prompt.clone());
653 }
654
655 if let Some(ref mode) = self.permission_mode {
656 args.push("--permission-mode".to_string());
657 args.push(mode.as_str().to_string());
658 }
659
660 if self.continue_conversation {
661 args.push("--continue".to_string());
662 }
663
664 if let Some(ref session) = self.resume {
665 args.push("--resume".to_string());
666 args.push(session.clone());
667 }
668
669 if let Some(ref model) = self.model {
670 args.push("--model".to_string());
671 args.push(model.clone());
672 }
673
674 if let Some(ref model) = self.fallback_model {
675 args.push("--fallback-model".to_string());
676 args.push(model.clone());
677 }
678
679 if let Some(ref settings) = self.settings {
680 args.push("--settings".to_string());
681 args.push(settings.clone());
682 }
683
684 if !self.add_dir.is_empty() {
685 args.push("--add-dir".to_string());
686 for dir in &self.add_dir {
687 args.push(dir.to_string_lossy().to_string());
688 }
689 }
690
691 if self.ide {
692 args.push("--ide".to_string());
693 }
694
695 if self.strict_mcp_config {
696 args.push("--strict-mcp-config".to_string());
697 }
698
699 if let Some(ref tool) = self.permission_prompt_tool {
700 args.push("--permission-prompt-tool".to_string());
701 args.push(tool.clone());
702 }
703
704 if self.resume.is_none() && !self.continue_conversation {
708 args.push("--session-id".to_string());
709 let session_uuid = self.session_id.unwrap_or_else(|| {
710 let uuid = Uuid::new_v4();
711 debug!("[CLI] Generated session UUID: {}", uuid);
712 uuid
713 });
714 args.push(session_uuid.to_string());
715 }
716
717 if let Some(ref prompt) = self.prompt {
719 args.push(prompt.clone());
720 }
721
722 args
723 }
724
725 #[cfg(feature = "async-client")]
727 pub async fn spawn(self) -> Result<tokio::process::Child> {
728 let args = self.build_args();
729
730 debug!(
732 "[CLI] Executing command: {} {}",
733 self.command.display(),
734 args.join(" ")
735 );
736
737 let mut cmd = tokio::process::Command::new(&self.command);
738 cmd.args(&args)
739 .stdin(Stdio::piped())
740 .stdout(Stdio::piped())
741 .stderr(Stdio::piped());
742
743 if let Some(ref token) = self.oauth_token {
745 cmd.env("CLAUDE_CODE_OAUTH_TOKEN", token);
746 debug!("[CLI] Setting CLAUDE_CODE_OAUTH_TOKEN environment variable");
747 }
748
749 if let Some(ref key) = self.api_key {
751 cmd.env("ANTHROPIC_API_KEY", key);
752 debug!("[CLI] Setting ANTHROPIC_API_KEY environment variable");
753 }
754
755 let child = cmd.spawn().map_err(Error::Io)?;
756
757 Ok(child)
758 }
759
760 #[cfg(feature = "async-client")]
762 pub fn build_command(self) -> tokio::process::Command {
763 let args = self.build_args();
764 let mut cmd = tokio::process::Command::new(&self.command);
765 cmd.args(&args)
766 .stdin(Stdio::piped())
767 .stdout(Stdio::piped())
768 .stderr(Stdio::piped());
769
770 if let Some(ref token) = self.oauth_token {
772 cmd.env("CLAUDE_CODE_OAUTH_TOKEN", token);
773 }
774
775 if let Some(ref key) = self.api_key {
777 cmd.env("ANTHROPIC_API_KEY", key);
778 }
779
780 cmd
781 }
782
783 pub fn spawn_sync(self) -> std::io::Result<std::process::Child> {
785 let args = self.build_args();
786
787 debug!(
789 "[CLI] Executing sync command: {} {}",
790 self.command.display(),
791 args.join(" ")
792 );
793
794 let mut cmd = std::process::Command::new(&self.command);
795 cmd.args(&args)
796 .stdin(Stdio::piped())
797 .stdout(Stdio::piped())
798 .stderr(Stdio::piped());
799
800 if let Some(ref token) = self.oauth_token {
802 cmd.env("CLAUDE_CODE_OAUTH_TOKEN", token);
803 debug!("[CLI] Setting CLAUDE_CODE_OAUTH_TOKEN environment variable");
804 }
805
806 if let Some(ref key) = self.api_key {
808 cmd.env("ANTHROPIC_API_KEY", key);
809 debug!("[CLI] Setting ANTHROPIC_API_KEY environment variable");
810 }
811
812 cmd.spawn()
813 }
814}
815
816#[cfg(test)]
817mod tests {
818 use super::*;
819
820 #[test]
821 fn test_streaming_flags_always_present() {
822 let builder = ClaudeCliBuilder::new();
823 let args = builder.build_args();
824
825 assert!(args.contains(&"--print".to_string()));
827 assert!(args.contains(&"--verbose".to_string())); assert!(args.contains(&"--output-format".to_string()));
829 assert!(args.contains(&"stream-json".to_string()));
830 assert!(args.contains(&"--input-format".to_string()));
831 }
832
833 #[test]
834 fn test_with_prompt() {
835 let builder = ClaudeCliBuilder::new().prompt("Hello, Claude!");
836 let args = builder.build_args();
837
838 assert_eq!(args.last().unwrap(), "Hello, Claude!");
839 }
840
841 #[test]
842 fn test_with_model() {
843 let builder = ClaudeCliBuilder::new()
844 .model("sonnet")
845 .fallback_model("opus");
846 let args = builder.build_args();
847
848 assert!(args.contains(&"--model".to_string()));
849 assert!(args.contains(&"sonnet".to_string()));
850 assert!(args.contains(&"--fallback-model".to_string()));
851 assert!(args.contains(&"opus".to_string()));
852 }
853
854 #[test]
855 fn test_with_debug() {
856 let builder = ClaudeCliBuilder::new().debug(Some("api"));
857 let args = builder.build_args();
858
859 assert!(args.contains(&"--debug".to_string()));
860 assert!(args.contains(&"api".to_string()));
861 }
862
863 #[test]
864 fn test_with_oauth_token() {
865 let valid_token = "sk-ant-oat-123456789";
866 let builder = ClaudeCliBuilder::new().oauth_token(valid_token);
867
868 let args = builder.clone().build_args();
870 assert!(!args.contains(&valid_token.to_string()));
871
872 assert_eq!(builder.oauth_token, Some(valid_token.to_string()));
874 }
875
876 #[test]
877 fn test_oauth_token_validation() {
878 let invalid_token = "invalid-token-123";
880 let builder = ClaudeCliBuilder::new().oauth_token(invalid_token);
881 assert_eq!(builder.oauth_token, Some(invalid_token.to_string()));
882 }
883
884 #[test]
885 fn test_with_api_key() {
886 let valid_key = "sk-ant-api-987654321";
887 let builder = ClaudeCliBuilder::new().api_key(valid_key);
888
889 let args = builder.clone().build_args();
891 assert!(!args.contains(&valid_key.to_string()));
892
893 assert_eq!(builder.api_key, Some(valid_key.to_string()));
895 }
896
897 #[test]
898 fn test_api_key_validation() {
899 let invalid_key = "invalid-api-key";
901 let builder = ClaudeCliBuilder::new().api_key(invalid_key);
902 assert_eq!(builder.api_key, Some(invalid_key.to_string()));
903 }
904
905 #[test]
906 fn test_both_auth_methods() {
907 let oauth = "sk-ant-oat-123";
908 let api_key = "sk-ant-api-456";
909 let builder = ClaudeCliBuilder::new().oauth_token(oauth).api_key(api_key);
910
911 assert_eq!(builder.oauth_token, Some(oauth.to_string()));
912 assert_eq!(builder.api_key, Some(api_key.to_string()));
913 }
914
915 #[test]
916 fn test_permission_prompt_tool() {
917 let builder = ClaudeCliBuilder::new().permission_prompt_tool("stdio");
918 let args = builder.build_args();
919
920 assert!(args.contains(&"--permission-prompt-tool".to_string()));
921 assert!(args.contains(&"stdio".to_string()));
922 }
923
924 #[test]
925 fn test_permission_prompt_tool_not_present_by_default() {
926 let builder = ClaudeCliBuilder::new();
927 let args = builder.build_args();
928
929 assert!(!args.contains(&"--permission-prompt-tool".to_string()));
930 }
931
932 #[test]
933 fn test_session_id_present_for_new_session() {
934 let builder = ClaudeCliBuilder::new();
935 let args = builder.build_args();
936
937 assert!(
938 args.contains(&"--session-id".to_string()),
939 "New sessions should have --session-id"
940 );
941 }
942
943 #[test]
944 fn test_session_id_not_present_with_resume() {
945 let builder = ClaudeCliBuilder::new().resume(Some("existing-uuid".to_string()));
948 let args = builder.build_args();
949
950 assert!(
951 args.contains(&"--resume".to_string()),
952 "Should have --resume flag"
953 );
954 assert!(
955 !args.contains(&"--session-id".to_string()),
956 "--session-id should NOT be present when resuming"
957 );
958 }
959
960 #[test]
961 fn test_session_id_not_present_with_continue() {
962 let builder = ClaudeCliBuilder::new().continue_conversation(true);
964 let args = builder.build_args();
965
966 assert!(
967 args.contains(&"--continue".to_string()),
968 "Should have --continue flag"
969 );
970 assert!(
971 !args.contains(&"--session-id".to_string()),
972 "--session-id should NOT be present when continuing"
973 );
974 }
975}