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