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 EngineTrace,
26 WasiDebug,
28}
29
30impl AsRef<str> for LogLevel {
31 fn as_ref(&self) -> &str {
32 match self {
33 LogLevel::Trace => "trace",
34 LogLevel::Debug => "debug",
35 LogLevel::Info => "info",
36 LogLevel::Warn => "warn",
37 LogLevel::Error => "error",
38 LogLevel::EngineDebug => "engine=debug",
39 LogLevel::EngineTrace => "engine=trace",
40 LogLevel::WasiDebug => "wasi_component_loader=debug",
41 }
42 }
43}
44
45#[derive(Debug)]
47pub struct TestConfig {
48 pub(super) gateway_path: PathBuf,
49 pub(super) cli_path: PathBuf,
50 pub(super) extension_path: Option<PathBuf>,
51 pub(super) gateway_configuration: String,
52 pub(super) enable_stdout: bool,
53 pub(super) enable_stderr: bool,
54 pub(super) mock_subgraphs: Vec<MockSubgraph>,
55 pub(super) enable_networking: bool,
56 pub(super) enable_environment_variables: bool,
57 pub(super) max_pool_size: Option<usize>,
58 pub(super) log_level: LogLevel,
59}
60
61impl TestConfig {
62 pub fn builder() -> TestConfigBuilder {
64 TestConfigBuilder::new()
65 }
66}
67
68#[derive(Debug, Default)]
69pub struct TestConfigBuilder {
71 gateway_path: Option<PathBuf>,
72 cli_path: Option<PathBuf>,
73 extension_path: Option<PathBuf>,
74 mock_subgraphs: Vec<MockSubgraph>,
75 enable_stdout: bool,
76 enable_stderr: bool,
77 enable_networking: bool,
78 enable_environment_variables: bool,
79 max_pool_size: Option<usize>,
80 log_level: Option<LogLevel>,
81}
82
83impl TestConfigBuilder {
84 pub(crate) fn new() -> Self {
86 Self::default()
87 }
88
89 pub fn with_subgraph(mut self, subgraph: impl Into<MockSubgraph>) -> Self {
91 self.mock_subgraphs.push(subgraph.into());
92 self
93 }
94
95 pub fn with_gateway(mut self, gateway_path: impl Into<PathBuf>) -> Self {
97 self.gateway_path = Some(gateway_path.into());
98 self
99 }
100
101 pub fn with_cli(mut self, cli_path: impl Into<PathBuf>) -> Self {
103 self.cli_path = Some(cli_path.into());
104 self
105 }
106
107 pub fn with_extension(mut self, extension_path: impl Into<PathBuf>) -> Self {
109 self.extension_path = Some(extension_path.into().canonicalize().unwrap());
110 self
111 }
112
113 pub fn enable_stdout(mut self) -> Self {
116 self.enable_stdout = true;
117 self
118 }
119
120 pub fn enable_stderr(mut self) -> Self {
123 self.enable_stderr = true;
124 self
125 }
126
127 pub fn enable_networking(mut self) -> Self {
129 self.enable_networking = true;
130 self
131 }
132
133 pub fn enable_environment_variables(mut self) -> Self {
135 self.enable_environment_variables = true;
136 self
137 }
138
139 pub fn max_pool_size(mut self, size: usize) -> Self {
141 self.max_pool_size = Some(size);
142 self
143 }
144
145 pub fn log_level(mut self, level: LogLevel) -> Self {
147 self.log_level = Some(level);
148 self
149 }
150
151 pub fn build(self, gateway_configuration: impl ToString) -> anyhow::Result<TestConfig> {
153 let Self {
154 gateway_path,
155 cli_path,
156 extension_path,
157 enable_stdout,
158 enable_stderr,
159 mock_subgraphs,
160 enable_networking,
161 enable_environment_variables,
162 max_pool_size,
163 log_level,
164 } = self;
165
166 let gateway_path = match gateway_path {
167 Some(path) => path,
168 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.")?,
169 };
170
171 let cli_path = match cli_path {
172 Some(path) => path,
173 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.")?,
174 };
175
176 let gateway_configuration = gateway_configuration.to_string();
177 let log_level = log_level.unwrap_or_default();
178
179 Ok(TestConfig {
180 gateway_path,
181 cli_path,
182 gateway_configuration,
183 extension_path,
184 enable_stdout,
185 enable_stderr,
186 mock_subgraphs,
187 enable_networking,
188 enable_environment_variables,
189 max_pool_size,
190 log_level,
191 })
192 }
193}