grafbase_sdk/test/
config.rs

1use 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/// Log level for the test process output. Default value is `LogLevel::Error`.
9#[derive(Debug, Clone, Copy, Default)]
10pub enum LogLevel {
11    /// Show all output from traces upwards.
12    Trace,
13    /// Show all output from debug upwards.
14    Debug,
15    /// Show all output from info upwards.
16    #[default]
17    Info,
18    /// Show all output from warn upwards.
19    Warn,
20    /// Show only error messages.
21    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/// Configuration for test cases.
37#[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    /// Creates a new test configuration builder.
54    pub fn builder() -> TestConfigBuilder {
55        TestConfigBuilder::new()
56    }
57}
58
59#[derive(Debug, Default)]
60/// Builder pattern to create a [`TestConfig`].
61pub 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    /// Creates a new [`TestConfigBuilder`] with default values.
76    pub(crate) fn new() -> Self {
77        Self::default()
78    }
79
80    /// Adds a dynamic subgraph to the test configuration.
81    pub fn with_subgraph(mut self, subgraph: impl Into<MockSubgraph>) -> Self {
82        self.mock_subgraphs.push(subgraph.into());
83        self
84    }
85
86    /// Specifies a custom path to the gateway binary. If not defined, the binary will be searched in the PATH.
87    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    /// Specifies a custom path to the CLI binary. If not defined, the binary will be searched in the PATH.
93    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    /// Specifies a path to a pre-built extension. If not defined, the extension will be built.
99    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    /// Enables stdout output from the gateway and CLI. Useful for debugging errors in the gateway
105    /// and in the extension.
106    pub fn enable_stdout(mut self) -> Self {
107        self.enable_stdout = true;
108        self
109    }
110
111    /// Enables stderr output from the gateway and CLI. Useful for debugging errors in the gateway
112    /// and in the extension.
113    pub fn enable_stderr(mut self) -> Self {
114        self.enable_stderr = true;
115        self
116    }
117
118    /// Enables networking for the extension.
119    pub fn enable_networking(mut self) -> Self {
120        self.enable_networking = true;
121        self
122    }
123
124    /// Enables environment variables for the extension.
125    pub fn enable_environment_variables(mut self) -> Self {
126        self.enable_environment_variables = true;
127        self
128    }
129
130    /// Sets the maximum pool size for the extension.
131    pub fn max_pool_size(mut self, size: usize) -> Self {
132        self.max_pool_size = Some(size);
133        self
134    }
135
136    /// Sets the log level for the gateway process output.
137    pub fn log_level(mut self, level: LogLevel) -> Self {
138        self.log_level = Some(level);
139        self
140    }
141
142    /// Builds the [`TestConfig`] with the given gateway configuration and federated graph schema.
143    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}