Skip to main content

visdom_cli/
cli.rs

1//! The `visdom` command line.
2
3use clap::{Parser, Subcommand};
4use tracevault_cli::agent::Agent;
5use tracevault_cli::cli::{Command, InitArgs, LoginArgs};
6
7#[derive(Parser)]
8#[command(
9    name = "visdom",
10    version,
11    about = "Visdom developer CLI",
12    long_about = "Visdom developer CLI.\n\nWithout a subcommand: checks that this project is wired \
13                  for the coding agent (tracing, bazaar MCP), offers to fix what is not, and \
14                  launches the agent. Arguments after `--` go to the agent."
15)]
16pub struct VisdomCli {
17    /// The agent to check and launch: claude-code or codex. Default: the
18    /// VISDOM_AGENT variable, then `.visdom/config.toml` in the repository,
19    /// then the user config, then claude-code.
20    #[arg(long, value_enum)]
21    pub agent: Option<Agent>,
22    /// Apply every offered fix without asking.
23    #[arg(long)]
24    pub yes: bool,
25    #[command(subcommand)]
26    pub command: Option<VisdomCommand>,
27    /// Arguments passed to the agent unchanged.
28    #[arg(last = true)]
29    pub agent_args: Vec<String>,
30}
31
32#[derive(Subcommand)]
33pub enum VisdomCommand {
34    /// Check this project's Visdom wiring without launching an agent. Exits
35    /// with 1 when a check fails.
36    Doctor {
37        /// Offer the fixes (asking for each; with --yes, apply all).
38        #[arg(long)]
39        fix: bool,
40        /// Print the findings as JSON.
41        #[arg(long)]
42        json: bool,
43        /// With --fix: apply every fix without asking.
44        #[arg(long)]
45        yes: bool,
46        /// The agent to check (see `visdom --help`).
47        #[arg(long, value_enum)]
48        agent: Option<Agent>,
49    },
50    /// Log in to Visdom
51    Login(LoginArgs),
52    /// Log out from Visdom
53    Logout,
54    /// Set up this repository for Visdom: tracing (as `tracevault init`), and
55    /// the bazaar MCP server for the agent.
56    Init {
57        #[command(flatten)]
58        args: InitArgs,
59    },
60    /// Tracing commands (everything `tracevault` does).
61    Trace {
62        #[command(subcommand)]
63        command: Command,
64    },
65}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    fn parse(args: &[&str]) -> VisdomCli {
72        match VisdomCli::try_parse_from(args) {
73            Ok(cli) => cli,
74            Err(e) => panic!("{args:?} must parse: {e}"),
75        }
76    }
77
78    #[test]
79    fn command_definition_is_consistent() {
80        use clap::CommandFactory;
81        VisdomCli::command().debug_assert();
82    }
83
84    #[test]
85    fn parses_trace_status() {
86        let cli = parse(&["visdom", "trace", "status"]);
87        assert!(matches!(
88            cli.command,
89            Some(VisdomCommand::Trace {
90                command: Command::Status { .. }
91            })
92        ));
93    }
94
95    #[test]
96    fn parses_hidden_trace_stream() {
97        let cli = parse(&["visdom", "trace", "stream", "--event", "stop"]);
98        assert!(matches!(
99            cli.command,
100            Some(VisdomCommand::Trace {
101                command: Command::Stream { .. }
102            })
103        ));
104    }
105
106    #[test]
107    fn bare_visdom_with_agent_args() {
108        let cli = parse(&["visdom", "--agent", "codex", "--", "--model", "x"]);
109        assert!(cli.command.is_none());
110        assert_eq!(cli.agent, Some(Agent::Codex));
111        assert_eq!(cli.agent_args, ["--model", "x"]);
112    }
113
114    #[test]
115    fn bare_visdom_without_arguments() {
116        let cli = parse(&["visdom"]);
117        assert!(cli.command.is_none());
118        assert!(cli.agent.is_none() && !cli.yes && cli.agent_args.is_empty());
119    }
120
121    #[test]
122    fn parses_doctor_json() {
123        let cli = parse(&["visdom", "doctor", "--json"]);
124        assert!(matches!(
125            cli.command,
126            Some(VisdomCommand::Doctor {
127                json: true,
128                fix: false,
129                yes: false,
130                agent: None
131            })
132        ));
133    }
134
135    #[test]
136    fn init_takes_tracevault_init_flags() {
137        let cli = parse(&["visdom", "init", "--agent", "codex", "--no-gitignore"]);
138        match cli.command {
139            Some(VisdomCommand::Init { args }) => {
140                assert_eq!(args.agent, Agent::Codex);
141                assert!(args.no_gitignore);
142            }
143            _ => panic!("expected init"),
144        }
145    }
146}