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