1use clap::{Parser, Subcommand};
4use tracevault_cli::agent::Agent;
5use tracevault_cli::cli::{Command, InitArgs, LoginArgs};
6
7use crate::billing::Billing;
8use crate::mode::TracingMode;
9
10#[derive(Parser)]
11#[command(
12 name = "visdom",
13 version,
14 about = "Visdom developer CLI",
15 long_about = "Visdom developer CLI.\n\nWithout a subcommand: checks that this project is wired \
16 for the coding agent (tracing, bazaar MCP), offers to fix what is not, and \
17 launches the agent — with hooks tracing, or through the AI Gateway (--mode). \
18 Arguments after `--` go to the agent."
19)]
20pub struct VisdomCli {
21 #[arg(long, value_enum)]
25 pub agent: Option<Agent>,
26 #[arg(long, value_enum)]
31 pub mode: Option<TracingMode>,
32 #[arg(long, value_enum)]
38 pub billing: Option<Billing>,
39 #[arg(long)]
41 pub yes: bool,
42 #[arg(long)]
46 pub no_instructions: bool,
47 #[command(subcommand)]
48 pub command: Option<VisdomCommand>,
49 #[arg(last = true)]
51 pub agent_args: Vec<String>,
52}
53
54#[derive(Subcommand)]
55pub enum VisdomCommand {
56 Doctor {
59 #[arg(long)]
61 fix: bool,
62 #[arg(long)]
64 json: bool,
65 #[arg(long)]
67 yes: bool,
68 #[arg(long, value_enum)]
70 agent: Option<Agent>,
71 #[arg(long, value_enum)]
73 mode: Option<TracingMode>,
74 #[arg(long, value_enum)]
76 billing: Option<Billing>,
77 },
78 #[command(hide = true)]
81 GatewayKey {
82 #[arg(long)]
84 console: String,
85 #[arg(long)]
87 project: String,
88 #[arg(long, value_enum, default_value_t = Billing::Project)]
91 billing: Billing,
92 },
93 Env {
97 #[arg(long, value_enum)]
99 agent: Option<Agent>,
100 },
101 Instructions {
105 #[arg(long, value_enum)]
107 agent: Option<Agent>,
108 #[arg(long, value_enum)]
110 mode: Option<TracingMode>,
111 },
112 Login(LoginArgs),
114 Logout,
116 Init {
119 #[command(flatten)]
120 args: InitArgs,
121 },
122 Trace {
124 #[command(subcommand)]
125 command: Command,
126 },
127}
128
129#[cfg(test)]
130mod tests {
131 use super::*;
132
133 fn parse(args: &[&str]) -> VisdomCli {
134 match VisdomCli::try_parse_from(args) {
135 Ok(cli) => cli,
136 Err(e) => panic!("{args:?} must parse: {e}"),
137 }
138 }
139
140 #[test]
141 fn the_mode_is_an_option_of_the_launch_and_of_doctor() {
142 let cli = parse(&["visdom", "--mode", "gateway", "--", "--verbose"]);
143 assert_eq!(cli.mode, Some(TracingMode::Gateway));
144 assert_eq!(cli.agent_args, ["--verbose"]);
145 let cli = parse(&["visdom", "doctor", "--mode", "hooks"]);
146 assert!(matches!(
147 cli.command,
148 Some(VisdomCommand::Doctor {
149 mode: Some(TracingMode::Hooks),
150 ..
151 })
152 ));
153 assert!(VisdomCli::try_parse_from(["visdom", "--mode", "proxy"]).is_err());
154 }
155
156 #[test]
157 fn who_pays_is_an_option_of_the_launch_init_and_doctor() {
158 let cli = parse(&[
159 "visdom",
160 "--mode",
161 "gateway",
162 "--billing",
163 "subscription",
164 "init",
165 ]);
166 assert_eq!(cli.billing, Some(Billing::Subscription));
167 assert!(matches!(cli.command, Some(VisdomCommand::Init { .. })));
168 let cli = parse(&["visdom", "doctor", "--billing", "project"]);
169 assert!(matches!(
170 cli.command,
171 Some(VisdomCommand::Doctor {
172 billing: Some(Billing::Project),
173 ..
174 })
175 ));
176 assert!(VisdomCli::try_parse_from(["visdom", "--billing", "free"]).is_err());
177 }
178
179 #[test]
180 fn the_key_helper_takes_a_console_and_a_project() {
181 let cli = parse(&[
182 "visdom",
183 "gateway-key",
184 "--console",
185 "https://gw.example.com",
186 "--project",
187 "p-1",
188 ]);
189 match cli.command {
190 Some(VisdomCommand::GatewayKey {
191 console,
192 project,
193 billing,
194 }) => {
195 assert_eq!(
196 (console.as_str(), project.as_str(), billing),
197 ("https://gw.example.com", "p-1", Billing::Project)
198 );
199 }
200 _ => panic!("expected gateway-key"),
201 }
202 let cli = parse(&[
203 "visdom",
204 "gateway-key",
205 "--console",
206 "https://gw.example.com",
207 "--project",
208 "p-1",
209 "--billing",
210 "subscription",
211 ]);
212 assert!(matches!(
213 cli.command,
214 Some(VisdomCommand::GatewayKey {
215 billing: Billing::Subscription,
216 ..
217 })
218 ));
219 }
220
221 #[test]
222 fn command_definition_is_consistent() {
223 use clap::CommandFactory;
224 VisdomCli::command().debug_assert();
225 }
226
227 #[test]
228 fn parses_trace_status() {
229 let cli = parse(&["visdom", "trace", "status"]);
230 assert!(matches!(
231 cli.command,
232 Some(VisdomCommand::Trace {
233 command: Command::Status { .. }
234 })
235 ));
236 }
237
238 #[test]
239 fn parses_hidden_trace_stream() {
240 let cli = parse(&["visdom", "trace", "stream", "--event", "stop"]);
241 assert!(matches!(
242 cli.command,
243 Some(VisdomCommand::Trace {
244 command: Command::Stream { .. }
245 })
246 ));
247 }
248
249 #[test]
250 fn bare_visdom_with_agent_args() {
251 let cli = parse(&["visdom", "--agent", "codex", "--", "--model", "x"]);
252 assert!(cli.command.is_none());
253 assert_eq!(cli.agent, Some(Agent::Codex));
254 assert_eq!(cli.agent_args, ["--model", "x"]);
255 }
256
257 #[test]
258 fn bare_visdom_without_arguments() {
259 let cli = parse(&["visdom"]);
260 assert!(cli.command.is_none());
261 assert!(cli.agent.is_none() && !cli.yes && cli.agent_args.is_empty());
262 }
263
264 #[test]
265 fn parses_doctor_json() {
266 let cli = parse(&["visdom", "doctor", "--json"]);
267 assert!(matches!(
268 cli.command,
269 Some(VisdomCommand::Doctor {
270 json: true,
271 fix: false,
272 yes: false,
273 agent: None,
274 mode: None,
275 billing: None
276 })
277 ));
278 }
279
280 #[test]
281 fn init_takes_tracevault_init_flags() {
282 let cli = parse(&["visdom", "init", "--agent", "codex", "--no-gitignore"]);
283 match cli.command {
284 Some(VisdomCommand::Init { args }) => {
285 assert_eq!(args.agent, Agent::Codex);
286 assert!(args.no_gitignore);
287 }
288 _ => panic!("expected init"),
289 }
290 }
291}