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