1use clap::{Parser, Subcommand};
4use tracevault_cli::agent::Agent;
5use tracevault_cli::cli::{Command, InitArgs, LoginArgs};
6
7use crate::mode::TracingMode;
8
9#[derive(Parser)]
10#[command(
11 name = "visdom",
12 version,
13 about = "Visdom developer CLI",
14 long_about = "Visdom developer CLI.\n\nWithout a subcommand: checks that this project is wired \
15 for the coding agent (tracing, bazaar MCP), offers to fix what is not, and \
16 launches the agent — with hooks tracing, or through the AI Gateway (--mode). \
17 Arguments after `--` go to the agent."
18)]
19pub struct VisdomCli {
20 #[arg(long, value_enum)]
24 pub agent: Option<Agent>,
25 #[arg(long, value_enum)]
30 pub mode: Option<TracingMode>,
31 #[arg(long)]
33 pub yes: bool,
34 #[arg(long)]
38 pub no_instructions: bool,
39 #[command(subcommand)]
40 pub command: Option<VisdomCommand>,
41 #[arg(last = true)]
43 pub agent_args: Vec<String>,
44}
45
46#[derive(Subcommand)]
47pub enum VisdomCommand {
48 Doctor {
51 #[arg(long)]
53 fix: bool,
54 #[arg(long)]
56 json: bool,
57 #[arg(long)]
59 yes: bool,
60 #[arg(long, value_enum)]
62 agent: Option<Agent>,
63 #[arg(long, value_enum)]
65 mode: Option<TracingMode>,
66 },
67 #[command(hide = true)]
70 GatewayKey {
71 #[arg(long)]
73 console: String,
74 #[arg(long)]
76 project: String,
77 },
78 Env {
82 #[arg(long, value_enum)]
84 agent: Option<Agent>,
85 },
86 Instructions {
90 #[arg(long, value_enum)]
92 agent: Option<Agent>,
93 #[arg(long, value_enum)]
95 mode: Option<TracingMode>,
96 },
97 Login(LoginArgs),
99 Logout,
101 Init {
104 #[command(flatten)]
105 args: InitArgs,
106 },
107 Trace {
109 #[command(subcommand)]
110 command: Command,
111 },
112}
113
114#[cfg(test)]
115mod tests {
116 use super::*;
117
118 fn parse(args: &[&str]) -> VisdomCli {
119 match VisdomCli::try_parse_from(args) {
120 Ok(cli) => cli,
121 Err(e) => panic!("{args:?} must parse: {e}"),
122 }
123 }
124
125 #[test]
126 fn the_mode_is_an_option_of_the_launch_and_of_doctor() {
127 let cli = parse(&["visdom", "--mode", "gateway", "--", "--verbose"]);
128 assert_eq!(cli.mode, Some(TracingMode::Gateway));
129 assert_eq!(cli.agent_args, ["--verbose"]);
130 let cli = parse(&["visdom", "doctor", "--mode", "hooks"]);
131 assert!(matches!(
132 cli.command,
133 Some(VisdomCommand::Doctor {
134 mode: Some(TracingMode::Hooks),
135 ..
136 })
137 ));
138 assert!(VisdomCli::try_parse_from(["visdom", "--mode", "proxy"]).is_err());
139 }
140
141 #[test]
142 fn the_key_helper_takes_a_console_and_a_project() {
143 let cli = parse(&[
144 "visdom",
145 "gateway-key",
146 "--console",
147 "https://gw.example.com",
148 "--project",
149 "p-1",
150 ]);
151 match cli.command {
152 Some(VisdomCommand::GatewayKey { console, project }) => {
153 assert_eq!(
154 (console.as_str(), project.as_str()),
155 ("https://gw.example.com", "p-1")
156 );
157 }
158 _ => panic!("expected gateway-key"),
159 }
160 }
161
162 #[test]
163 fn command_definition_is_consistent() {
164 use clap::CommandFactory;
165 VisdomCli::command().debug_assert();
166 }
167
168 #[test]
169 fn parses_trace_status() {
170 let cli = parse(&["visdom", "trace", "status"]);
171 assert!(matches!(
172 cli.command,
173 Some(VisdomCommand::Trace {
174 command: Command::Status { .. }
175 })
176 ));
177 }
178
179 #[test]
180 fn parses_hidden_trace_stream() {
181 let cli = parse(&["visdom", "trace", "stream", "--event", "stop"]);
182 assert!(matches!(
183 cli.command,
184 Some(VisdomCommand::Trace {
185 command: Command::Stream { .. }
186 })
187 ));
188 }
189
190 #[test]
191 fn bare_visdom_with_agent_args() {
192 let cli = parse(&["visdom", "--agent", "codex", "--", "--model", "x"]);
193 assert!(cli.command.is_none());
194 assert_eq!(cli.agent, Some(Agent::Codex));
195 assert_eq!(cli.agent_args, ["--model", "x"]);
196 }
197
198 #[test]
199 fn bare_visdom_without_arguments() {
200 let cli = parse(&["visdom"]);
201 assert!(cli.command.is_none());
202 assert!(cli.agent.is_none() && !cli.yes && cli.agent_args.is_empty());
203 }
204
205 #[test]
206 fn parses_doctor_json() {
207 let cli = parse(&["visdom", "doctor", "--json"]);
208 assert!(matches!(
209 cli.command,
210 Some(VisdomCommand::Doctor {
211 json: true,
212 fix: false,
213 yes: false,
214 agent: None,
215 mode: None
216 })
217 ));
218 }
219
220 #[test]
221 fn init_takes_tracevault_init_flags() {
222 let cli = parse(&["visdom", "init", "--agent", "codex", "--no-gitignore"]);
223 match cli.command {
224 Some(VisdomCommand::Init { args }) => {
225 assert_eq!(args.agent, Agent::Codex);
226 assert!(args.no_gitignore);
227 }
228 _ => panic!("expected init"),
229 }
230 }
231}