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    /// Show all output from engine, debug upwards.
23    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/// Configuration for test cases.
40#[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    /// Creates a new test configuration builder.
57    pub fn builder() -> TestConfigBuilder {
58        TestConfigBuilder::new()
59    }
60}
61
62#[derive(Debug, Default)]
63/// Builder pattern to create a [`TestConfig`].
64pub 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    /// Creates a new [`TestConfigBuilder`] with default values.
79    pub(crate) fn new() -> Self {
80        Self::default()
81    }
82
83    /// Adds a dynamic subgraph to the test configuration.
84    pub fn with_subgraph(mut self, subgraph: impl Into<MockSubgraph>) -> Self {
85        self.mock_subgraphs.push(subgraph.into());
86        self
87    }
88
89    /// Specifies a custom path to the gateway binary. If not defined, the binary will be searched in the PATH.
90    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    /// Specifies a custom path to the CLI binary. If not defined, the binary will be searched in the PATH.
96    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    /// Specifies a path to a pre-built extension. If not defined, the extension will be built.
102    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    /// Enables stdout output from the gateway and CLI. Useful for debugging errors in the gateway
108    /// and in the extension.
109    pub fn enable_stdout(mut self) -> Self {
110        self.enable_stdout = true;
111        self
112    }
113
114    /// Enables stderr output from the gateway and CLI. Useful for debugging errors in the gateway
115    /// and in the extension.
116    pub fn enable_stderr(mut self) -> Self {
117        self.enable_stderr = true;
118        self
119    }
120
121    /// Enables networking for the extension.
122    pub fn enable_networking(mut self) -> Self {
123        self.enable_networking = true;
124        self
125    }
126
127    /// Enables environment variables for the extension.
128    pub fn enable_environment_variables(mut self) -> Self {
129        self.enable_environment_variables = true;
130        self
131    }
132
133    /// Sets the maximum pool size for the extension.
134    pub fn max_pool_size(mut self, size: usize) -> Self {
135        self.max_pool_size = Some(size);
136        self
137    }
138
139    /// Sets the log level for the gateway process output.
140    pub fn log_level(mut self, level: LogLevel) -> Self {
141        self.log_level = Some(level);
142        self
143    }
144
145    /// Builds the [`TestConfig`] with the given gateway configuration and federated graph schema.
146    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}