grafbase_sdk/test/
config.rs1use anyhow::Context;
2use grafbase_sdk_mock::MockSubgraph;
3use std::path::PathBuf;
4
5const GATEWAY_BINARY_NAME: &str = "grafbase-gateway";
6const CLI_BINARY_NAME: &str = "grafbase";
7
8#[derive(Debug, Clone, Copy, Default)]
10pub enum LogLevel {
11 Trace,
13 Debug,
15 #[default]
17 Info,
18 Warn,
20 Error,
22 EngineDebug,
24 WasiDebug,
26}
27
28impl AsRef<str> for LogLevel {
29 fn as_ref(&self) -> &str {
30 match self {
31 LogLevel::Trace => "trace",
32 LogLevel::Debug => "debug",
33 LogLevel::Info => "info",
34 LogLevel::Warn => "warn",
35 LogLevel::Error => "error",
36 LogLevel::EngineDebug => "engine=debug",
37 LogLevel::WasiDebug => "wasi_component_loader=debug",
38 }
39 }
40}
41
42#[derive(Debug)]
44pub struct TestConfig {
45 pub(super) gateway_path: PathBuf,
46 pub(super) cli_path: PathBuf,
47 pub(super) extension_path: Option<PathBuf>,
48 pub(super) gateway_configuration: String,
49 pub(super) enable_stdout: bool,
50 pub(super) enable_stderr: bool,
51 pub(super) mock_subgraphs: Vec<MockSubgraph>,
52 pub(super) enable_networking: bool,
53 pub(super) enable_environment_variables: bool,
54 pub(super) max_pool_size: Option<usize>,
55 pub(super) log_level: LogLevel,
56}
57
58impl TestConfig {
59 pub fn builder() -> TestConfigBuilder {
61 TestConfigBuilder::new()
62 }
63}
64
65#[derive(Debug, Default)]
66pub struct TestConfigBuilder {
68 gateway_path: Option<PathBuf>,
69 cli_path: Option<PathBuf>,
70 extension_path: Option<PathBuf>,
71 mock_subgraphs: Vec<MockSubgraph>,
72 enable_stdout: bool,
73 enable_stderr: bool,
74 enable_networking: bool,
75 enable_environment_variables: bool,
76 max_pool_size: Option<usize>,
77 log_level: Option<LogLevel>,
78}
79
80impl TestConfigBuilder {
81 pub(crate) fn new() -> Self {
83 Self::default()
84 }
85
86 pub fn with_subgraph(mut self, subgraph: impl Into<MockSubgraph>) -> Self {
88 self.mock_subgraphs.push(subgraph.into());
89 self
90 }
91
92 pub fn with_gateway(mut self, gateway_path: impl Into<PathBuf>) -> Self {
94 self.gateway_path = Some(gateway_path.into());
95 self
96 }
97
98 pub fn with_cli(mut self, cli_path: impl Into<PathBuf>) -> Self {
100 self.cli_path = Some(cli_path.into());
101 self
102 }
103
104 pub fn with_extension(mut self, extension_path: impl Into<PathBuf>) -> Self {
106 self.extension_path = Some(extension_path.into().canonicalize().unwrap());
107 self
108 }
109
110 pub fn enable_stdout(mut self) -> Self {
113 self.enable_stdout = true;
114 self
115 }
116
117 pub fn enable_stderr(mut self) -> Self {
120 self.enable_stderr = true;
121 self
122 }
123
124 pub fn enable_networking(mut self) -> Self {
126 self.enable_networking = true;
127 self
128 }
129
130 pub fn enable_environment_variables(mut self) -> Self {
132 self.enable_environment_variables = true;
133 self
134 }
135
136 pub fn max_pool_size(mut self, size: usize) -> Self {
138 self.max_pool_size = Some(size);
139 self
140 }
141
142 pub fn log_level(mut self, level: LogLevel) -> Self {
144 self.log_level = Some(level);
145 self
146 }
147
148 pub fn build(self, gateway_configuration: impl ToString) -> anyhow::Result<TestConfig> {
150 let Self {
151 gateway_path,
152 cli_path,
153 extension_path,
154 enable_stdout,
155 enable_stderr,
156 mock_subgraphs,
157 enable_networking,
158 enable_environment_variables,
159 max_pool_size,
160 log_level,
161 } = self;
162
163 let gateway_path = match gateway_path {
164 Some(path) => path,
165 None => which::which(GATEWAY_BINARY_NAME).context("Could not fild grafbase-gateway binary in the PATH. Either install it or specify the gateway path in the test configuration.")?,
166 };
167
168 let cli_path = match cli_path {
169 Some(path) => path,
170 None => which::which(CLI_BINARY_NAME).context("Could not fild grafbase binary in the PATH. Either install it or specify the gateway path in the test configuration.")?,
171 };
172
173 let gateway_configuration = gateway_configuration.to_string();
174 let log_level = log_level.unwrap_or_default();
175
176 Ok(TestConfig {
177 gateway_path,
178 cli_path,
179 gateway_configuration,
180 extension_path,
181 enable_stdout,
182 enable_stderr,
183 mock_subgraphs,
184 enable_networking,
185 enable_environment_variables,
186 max_pool_size,
187 log_level,
188 })
189 }
190}