sqlite_graphrag/commands/
namespace_detect.rs1use crate::errors::AppError;
4use crate::namespace;
5use crate::output;
6use serde::Serialize;
7
8#[derive(clap::Args)]
9#[command(after_long_help = "EXAMPLES:\n \
10 # Resolve namespace using current environment and cwd\n \
11 sqlite-graphrag namespace-detect\n\n \
12 # Override with an explicit namespace flag\n \
13 sqlite-graphrag namespace-detect --namespace my-project\n\n \
14 # Explicit namespace flag\n \
15 sqlite-graphrag namespace-detect --namespace ci-runner")]
16pub struct NamespaceDetectArgs {
17 #[arg(long)]
18 pub namespace: Option<String>,
19 #[arg(long)]
21 pub db: Option<String>,
22 #[arg(long, default_value_t = false)]
24 pub json: bool,
25}
26
27#[derive(Serialize)]
28struct NamespaceDetectResponse {
29 namespace: String,
30 source: namespace::NamespaceSource,
31 cwd: String,
32 elapsed_ms: u64,
34}
35
36pub fn run(args: NamespaceDetectArgs) -> Result<(), AppError> {
37 let inicio = std::time::Instant::now();
38 let _ = args.db;
39 let _ = args.json; let resolution = namespace::detect_namespace(args.namespace.as_deref())?;
41 output::emit_json(&NamespaceDetectResponse {
42 namespace: resolution.namespace,
43 source: resolution.source,
44 cwd: resolution.cwd,
45 elapsed_ms: inicio.elapsed().as_millis() as u64,
46 })?;
47 Ok(())
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53 use crate::namespace::NamespaceSource;
54 use clap::Parser;
55 use serial_test::serial;
56
57 #[test]
58 #[serial]
59 fn namespace_detect_default_returns_global_via_detect() {
60 std::env::remove_var("SQLITE_GRAPHRAG_NAMESPACE");
62 let resolution = namespace::detect_namespace(None).unwrap();
63 assert_eq!(resolution.namespace, "global");
64 assert_eq!(resolution.source, NamespaceSource::Default);
65 }
66
67 #[test]
68 #[serial]
69 fn namespace_detect_explicit_flag_overrides_env() {
70 std::env::set_var("SQLITE_GRAPHRAG_NAMESPACE", "env-namespace");
71 let resolution = namespace::detect_namespace(Some("flag-namespace")).unwrap();
72 assert_eq!(resolution.namespace, "flag-namespace");
73 assert_eq!(resolution.source, NamespaceSource::ExplicitFlag);
74 std::env::remove_var("SQLITE_GRAPHRAG_NAMESPACE");
75 }
76
77 #[test]
78 #[serial]
79 fn namespace_detect_default_when_no_flag() {
80 let resolution = namespace::detect_namespace(None).unwrap();
83 assert!(!resolution.namespace.is_empty());
84 assert!(
85 matches!(
86 resolution.source,
87 NamespaceSource::Default | NamespaceSource::XdgConfig
88 ),
89 "unexpected source {:?}",
90 resolution.source
91 );
92 }
93
94 #[test]
95 fn namespace_detect_response_serializes_all_fields() {
96 let resp = NamespaceDetectResponse {
97 namespace: "meu-projeto".to_string(),
98 source: NamespaceSource::ExplicitFlag,
99 cwd: "/home/usuario/projeto".to_string(),
100 elapsed_ms: 3,
101 };
102 let json = serde_json::to_value(&resp).unwrap();
103 assert_eq!(json["namespace"], "meu-projeto");
104 assert_eq!(json["source"], "explicit_flag");
105 assert!(json["cwd"].is_string());
106 assert_eq!(json["elapsed_ms"], 3);
107 }
108
109 #[test]
110 fn namespace_source_serializes_in_snake_case() {
111 let cases = vec![
112 (NamespaceSource::ExplicitFlag, "explicit_flag"),
113 (NamespaceSource::XdgConfig, "xdg_config"),
114 (NamespaceSource::Default, "default"),
115 ];
116 for (source, expected) in cases {
117 let json = serde_json::to_value(source).unwrap();
118 assert_eq!(
119 json, expected,
120 "NamespaceSource::{source:?} must serialize as \"{expected}\""
121 );
122 }
123 }
124
125 #[test]
126 fn namespace_detect_accepts_db_as_noop() {
127 let cli = crate::cli::Cli::try_parse_from([
128 "sqlite-graphrag",
129 "namespace-detect",
130 "--db",
131 "/tmp/graphrag.sqlite",
132 ])
133 .expect("namespace-detect must accept --db as a no-op");
134
135 match cli.command {
136 Some(crate::cli::Commands::NamespaceDetect(args)) => {
137 assert_eq!(args.db.as_deref(), Some("/tmp/graphrag.sqlite"));
138 }
139 _ => unreachable!("unexpected command parsed"),
140 }
141 }
142}