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    /// LOL
25    WasiDebug,
26}
27
28impl AsRef<str> for LogLevel {
29    fn as_ref(&self) -> &str {
30        match self {
31            LogLevel::Trace => "trace",
32            LogLevel::Debug => "debug",
33            LogLevel::Info => "info",
34            LogLevel::Warn => "warn",
35            LogLevel::Error => "error",
36            LogLevel::EngineDebug => "engine=debug",
37            LogLevel::WasiDebug => "wasi_component_loader=debug",
38        }
39    }
40}
41
42/// Configuration for test cases.
43#[derive(Debug)]
44pub struct TestConfig {
45    pub(super) gateway_path: PathBuf,
46    pub(super) cli_path: PathBuf,
47    pub(super) extension_path: Option<PathBuf>,
48    pub(super) gateway_configuration: String,
49    pub(super) enable_stdout: bool,
50    pub(super) enable_stderr: bool,
51    pub(super) mock_subgraphs: Vec<MockSubgraph>,
52    pub(super) enable_networking: bool,
53    pub(super) enable_environment_variables: bool,
54    pub(super) max_pool_size: Option<usize>,
55    pub(super) log_level: LogLevel,
56}
57
58impl TestConfig {
59    /// Creates a new test configuration builder.
60    pub fn builder() -> TestConfigBuilder {
61        TestConfigBuilder::new()
62    }
63}
64
65#[derive(Debug, Default)]
66/// Builder pattern to create a [`TestConfig`].
67pub struct TestConfigBuilder {
68    gateway_path: Option<PathBuf>,
69    cli_path: Option<PathBuf>,
70    extension_path: Option<PathBuf>,
71    mock_subgraphs: Vec<MockSubgraph>,
72    enable_stdout: bool,
73    enable_stderr: bool,
74    enable_networking: bool,
75    enable_environment_variables: bool,
76    max_pool_size: Option<usize>,
77    log_level: Option<LogLevel>,
78}
79
80impl TestConfigBuilder {
81    /// Creates a new [`TestConfigBuilder`] with default values.
82    pub(crate) fn new() -> Self {
83        Self::default()
84    }
85
86    /// Adds a dynamic subgraph to the test configuration.
87    pub fn with_subgraph(mut self, subgraph: impl Into<MockSubgraph>) -> Self {
88        self.mock_subgraphs.push(subgraph.into());
89        self
90    }
91
92    /// Specifies a custom path to the gateway binary. If not defined, the binary will be searched in the PATH.
93    pub fn with_gateway(mut self, gateway_path: impl Into<PathBuf>) -> Self {
94        self.gateway_path = Some(gateway_path.into());
95        self
96    }
97
98    /// Specifies a custom path to the CLI binary. If not defined, the binary will be searched in the PATH.
99    pub fn with_cli(mut self, cli_path: impl Into<PathBuf>) -> Self {
100        self.cli_path = Some(cli_path.into());
101        self
102    }
103
104    /// Specifies a path to a pre-built extension. If not defined, the extension will be built.
105    pub fn with_extension(mut self, extension_path: impl Into<PathBuf>) -> Self {
106        self.extension_path = Some(extension_path.into().canonicalize().unwrap());
107        self
108    }
109
110    /// Enables stdout output from the gateway and CLI. Useful for debugging errors in the gateway
111    /// and in the extension.
112    pub fn enable_stdout(mut self) -> Self {
113        self.enable_stdout = true;
114        self
115    }
116
117    /// Enables stderr output from the gateway and CLI. Useful for debugging errors in the gateway
118    /// and in the extension.
119    pub fn enable_stderr(mut self) -> Self {
120        self.enable_stderr = true;
121        self
122    }
123
124    /// Enables networking for the extension.
125    pub fn enable_networking(mut self) -> Self {
126        self.enable_networking = true;
127        self
128    }
129
130    /// Enables environment variables for the extension.
131    pub fn enable_environment_variables(mut self) -> Self {
132        self.enable_environment_variables = true;
133        self
134    }
135
136    /// Sets the maximum pool size for the extension.
137    pub fn max_pool_size(mut self, size: usize) -> Self {
138        self.max_pool_size = Some(size);
139        self
140    }
141
142    /// Sets the log level for the gateway process output.
143    pub fn log_level(mut self, level: LogLevel) -> Self {
144        self.log_level = Some(level);
145        self
146    }
147
148    /// Builds the [`TestConfig`] with the given gateway configuration and federated graph schema.
149    pub fn build(self, gateway_configuration: impl ToString) -> anyhow::Result<TestConfig> {
150        let Self {
151            gateway_path,
152            cli_path,
153            extension_path,
154            enable_stdout,
155            enable_stderr,
156            mock_subgraphs,
157            enable_networking,
158            enable_environment_variables,
159            max_pool_size,
160            log_level,
161        } = self;
162
163        let gateway_path = match gateway_path {
164            Some(path) => path,
165            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.")?,
166        };
167
168        let cli_path = match cli_path {
169            Some(path) => path,
170            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.")?,
171        };
172
173        let gateway_configuration = gateway_configuration.to_string();
174        let log_level = log_level.unwrap_or_default();
175
176        Ok(TestConfig {
177            gateway_path,
178            cli_path,
179            gateway_configuration,
180            extension_path,
181            enable_stdout,
182            enable_stderr,
183            mock_subgraphs,
184            enable_networking,
185            enable_environment_variables,
186            max_pool_size,
187            log_level,
188        })
189    }
190}