byre 0.6.0

A set of libs for quickly bootstrapping a project
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! Integration tests for the byre crate.
//!
//! These tests exercise the public APIs to ensure nothing breaks when changes are made.

use std::path::PathBuf;

use doku::Document;
use serde::Deserialize;

/// Test settings structure similar to what a real application would use.
#[derive(Document, Deserialize)]
pub struct TestSettings {
    /// Application-specific settings
    pub application: ApplicationSettings,

    /// Telemetry settings from byre
    pub telemetry: byre::telemetry::TelemetrySettings,
}

#[derive(Document, Deserialize)]
pub struct ApplicationSettings {
    /// Port to listen on
    #[doku(example = "8080")]
    pub listen_port: u16,

    /// Hostname to listen on
    #[doku(example = "localhost")]
    pub listen_host: String,

    /// Database directory path
    #[doku(example = "/var/db/test")]
    pub db_path: PathBuf,
}

// ============================================================================
// ServiceInfo Tests
// ============================================================================

#[test]
fn test_service_info_macro() {
    let info = byre::service_info!();

    assert_eq!(info.name, "byre");
    assert_eq!(info.name_in_metrics, "byre");
    assert!(!info.version.is_empty());
}

#[test]
fn test_service_info_fields() {
    let info = byre::ServiceInfo {
        name: "test-service",
        name_in_metrics: "test_service".to_string(),
        version: "1.0.0",
        author: "Test Author",
        description: "A test service",
    };

    assert_eq!(info.name, "test-service");
    assert_eq!(info.name_in_metrics, "test_service");
    assert_eq!(info.version, "1.0.0");
    assert_eq!(info.author, "Test Author");
    assert_eq!(info.description, "A test service");
}

#[test]
fn test_service_info_default() {
    let info = byre::ServiceInfo::default();

    assert_eq!(info.name, "");
    assert_eq!(info.name_in_metrics, "");
    assert_eq!(info.version, "");
}

#[test]
fn test_service_info_clone() {
    let info = byre::service_info!();
    let cloned = info.clone();

    assert_eq!(info.name, cloned.name);
    assert_eq!(info.version, cloned.version);
}

// ============================================================================
// Config Generation Tests
// ============================================================================

#[test]
fn test_config_generation() {
    let temp_dir = std::env::temp_dir();
    let config_path = temp_dir.join("test_byre_config.toml");

    // Clean up any existing file
    let _ = std::fs::remove_file(&config_path);

    // Generate the config file
    let result = byre::config::create_config_file::<TestSettings>(&config_path);
    assert!(result.is_ok(), "Failed to create config file: {:?}", result);

    // Verify the file exists
    assert!(config_path.exists(), "Config file was not created");

    // Read and verify the content
    let content = std::fs::read_to_string(&config_path).expect("Failed to read config file");

    // Check for expected sections and fields
    assert!(
        content.contains("[application]"),
        "Missing [application] section"
    );
    assert!(content.contains("listen_port"), "Missing listen_port field");
    assert!(content.contains("listen_host"), "Missing listen_host field");
    assert!(content.contains("db_path"), "Missing db_path field");
    assert!(content.contains("[telemetry"), "Missing telemetry section");

    // Check for doku examples
    assert!(content.contains("8080"), "Missing example value 8080");
    assert!(
        content.contains("localhost"),
        "Missing example value localhost"
    );

    // Clean up
    let _ = std::fs::remove_file(&config_path);
}

#[test]
fn test_config_generation_with_comments() {
    let temp_dir = std::env::temp_dir();
    let config_path = temp_dir.join("test_byre_config_comments.toml");

    let _ = std::fs::remove_file(&config_path);

    byre::config::create_config_file::<TestSettings>(&config_path)
        .expect("Failed to create config file");

    let content = std::fs::read_to_string(&config_path).expect("Failed to read config file");

    // Doc comments should be included as TOML comments
    assert!(
        content.contains("# Port to listen on") || content.contains("Port to listen on"),
        "Missing doc comment for listen_port"
    );

    let _ = std::fs::remove_file(&config_path);
}

// ============================================================================
// Config Loading Tests
// ============================================================================

#[test]
fn test_config_loading() {
    let temp_dir = std::env::temp_dir();
    let config_path = temp_dir.join("test_byre_load_config.toml");

    // Create a test config file
    let config_content = r#"
[application]
listen_port = 9090
listen_host = "127.0.0.1"
db_path = "/tmp/test_db"

[telemetry.trace]

[telemetry.log]
console_level = "info"
otel_level = "warn"

[telemetry.metric]
"#;

    std::fs::write(&config_path, config_content).expect("Failed to write test config");

    // Load the config
    let config: byre::config::Config<TestSettings> =
        byre::config::Config::new(Some(&config_path), None::<&str>).expect("Failed to load config");

    assert_eq!(config.config.application.listen_port, 9090);
    assert_eq!(config.config.application.listen_host, "127.0.0.1");
    assert_eq!(
        config.config.application.db_path,
        PathBuf::from("/tmp/test_db")
    );
    assert_eq!(config.config.telemetry.log.console_level, "info");
    assert_eq!(config.config.telemetry.log.otel_level, "warn");

    let _ = std::fs::remove_file(&config_path);
}

#[test]
fn test_config_loading_with_env_override() {
    let temp_dir = std::env::temp_dir();
    let config_path = temp_dir.join("test_byre_env_override.toml");

    let config_content = r#"
[application]
listen_port = 8080
listen_host = "localhost"
db_path = "/var/db"

[telemetry.trace]

[telemetry.log]
console_level = "debug"
otel_level = "info"

[telemetry.metric]
"#;

    std::fs::write(&config_path, config_content).expect("Failed to write test config");

    // Set environment variable to override
    std::env::set_var("TESTAPP_APPLICATION__LISTEN_PORT", "3000");
    std::env::set_var("TESTAPP_APPLICATION__LISTEN_HOST", "0.0.0.0");

    let config: byre::config::Config<TestSettings> =
        byre::config::Config::new(Some(&config_path), Some("TESTAPP_"))
            .expect("Failed to load config");

    // These should be overridden by env vars
    assert_eq!(config.config.application.listen_port, 3000);
    assert_eq!(config.config.application.listen_host, "0.0.0.0");

    // This should remain from the file
    assert_eq!(config.config.application.db_path, PathBuf::from("/var/db"));

    // Clean up
    std::env::remove_var("TESTAPP_APPLICATION__LISTEN_PORT");
    std::env::remove_var("TESTAPP_APPLICATION__LISTEN_HOST");
    let _ = std::fs::remove_file(&config_path);
}

#[test]
fn test_config_loading_invalid_file() {
    let result: Result<byre::config::Config<TestSettings>, _> =
        byre::config::Config::new(Some("/nonexistent/path/config.toml"), None::<&str>);

    assert!(result.is_err(), "Should fail with nonexistent file");
}

#[test]
fn test_config_loading_invalid_toml() {
    let temp_dir = std::env::temp_dir();
    let config_path = temp_dir.join("test_byre_invalid.toml");

    // Write invalid TOML
    std::fs::write(&config_path, "this is not valid toml {{{{").expect("Failed to write file");

    let result: Result<byre::config::Config<TestSettings>, _> =
        byre::config::Config::new(Some(&config_path), None::<&str>);

    assert!(result.is_err(), "Should fail with invalid TOML");

    let _ = std::fs::remove_file(&config_path);
}

// ============================================================================
// TelemetrySettings Tests
// ============================================================================

#[test]
fn test_telemetry_settings_default() {
    let settings = byre::telemetry::TelemetrySettings::default();

    assert!(settings.trace.endpoint.is_none());
    assert!(settings.log.endpoint.is_none());
    assert!(settings.metric.endpoint.is_none());
    assert!(settings.log.console_level.is_empty());
    assert!(settings.log.otel_level.is_empty());
}

#[test]
fn test_telemetry_settings_serialization() {
    let settings = byre::telemetry::TelemetrySettings {
        trace: byre::telemetry::TraceSettings {
            endpoint: Some("http://localhost:4317".to_string()),
        },
        log: byre::telemetry::LogSettings {
            console_level: "debug".to_string(),
            otel_level: "warn".to_string(),
            endpoint: Some("http://localhost:4317".to_string()),
        },
        metric: byre::telemetry::MetricSettings {
            endpoint: Some("http://localhost:4318/v1/metrics".to_string()),
        },
    };

    // Test that it can be serialized
    let serialized = toml::to_string(&settings);
    assert!(serialized.is_ok(), "Failed to serialize TelemetrySettings");

    let toml_str = serialized.unwrap();
    assert!(toml_str.contains("endpoint"));
    assert!(toml_str.contains("console_level"));
}

#[test]
fn test_telemetry_settings_deserialization() {
    let toml_content = r#"
[trace]
endpoint = "http://trace:4317"

[log]
console_level = "info,mycrate=debug"
otel_level = "warn"
endpoint = "http://log:4317"

[metric]
endpoint = "http://metric:4318/v1/metrics"
"#;

    let settings: byre::telemetry::TelemetrySettings =
        toml::from_str(toml_content).expect("Failed to deserialize");

    assert_eq!(
        settings.trace.endpoint,
        Some("http://trace:4317".to_string())
    );
    assert_eq!(settings.log.console_level, "info,mycrate=debug");
    assert_eq!(settings.log.otel_level, "warn");
    assert_eq!(settings.log.endpoint, Some("http://log:4317".to_string()));
    assert_eq!(
        settings.metric.endpoint,
        Some("http://metric:4318/v1/metrics".to_string())
    );
}

#[test]
fn test_telemetry_settings_partial_config() {
    // Test with optional endpoints omitted
    let toml_content = r#"
[trace]

[log]
console_level = "info"
otel_level = "warn"

[metric]
"#;

    let settings: byre::telemetry::TelemetrySettings =
        toml::from_str(toml_content).expect("Failed to deserialize");

    assert!(settings.trace.endpoint.is_none());
    assert!(settings.log.endpoint.is_none());
    assert!(settings.metric.endpoint.is_none());
    assert_eq!(settings.log.console_level, "info");
}

// ============================================================================
// Telemetry Initialization Tests
// ============================================================================

#[test]
fn test_telemetry_init_with_disabled_endpoints() {
    let service_info = byre::service_info!();

    let settings = byre::telemetry::TelemetrySettings {
        trace: byre::telemetry::TraceSettings { endpoint: None },
        log: byre::telemetry::LogSettings {
            console_level: "off".to_string(),
            otel_level: "off".to_string(),
            endpoint: None,
        },
        metric: byre::telemetry::MetricSettings { endpoint: None },
    };

    // This should succeed when all endpoints are disabled
    // Note: We can only run this once per process due to global tracing subscriber
    // So we just verify the settings are valid
    assert!(settings.trace.endpoint.is_none());
    assert!(settings.log.endpoint.is_none());
    assert!(settings.metric.endpoint.is_none());

    // Verify service info is usable
    assert!(!service_info.name.is_empty());
}

// ============================================================================
// Error Type Tests
// ============================================================================

#[test]
fn test_error_display() {
    // Test that error types implement Display properly
    let config_error = byre::Error::ConfigFileWrite {
        path: PathBuf::from("/test/path"),
        source: std::io::Error::new(std::io::ErrorKind::PermissionDenied, "test error"),
    };

    let error_string = format!("{}", config_error);
    assert!(error_string.contains("/test/path"));
}

// ============================================================================
// CLI Error Tests
// ============================================================================

#[test]
fn test_cli_error_display() {
    // Test that cli::Error types implement Display properly
    let error = byre::cli::Error::ArgParse {
        message: "missing required argument".to_string(),
    };
    let error_string = format!("{}", error);
    assert!(error_string.contains("missing required argument"));
}

// ============================================================================
// Document Trait Tests
// ============================================================================

#[test]
fn test_telemetry_settings_document() {
    // Verify TelemetrySettings implements doku::Document
    let toml = doku::to_toml::<byre::telemetry::TelemetrySettings>();

    assert!(toml.contains("[trace]"));
    assert!(toml.contains("[log]"));
    assert!(toml.contains("[metric]"));
    assert!(toml.contains("endpoint"));
    assert!(toml.contains("console_level"));
    assert!(toml.contains("otel_level"));
}

#[test]
fn test_nested_settings_document() {
    // Verify our TestSettings with nested TelemetrySettings generates proper TOML
    let toml = doku::to_toml::<TestSettings>();

    assert!(toml.contains("[application]"));
    assert!(toml.contains("[telemetry.trace]"));
    assert!(toml.contains("[telemetry.log]"));
    assert!(toml.contains("[telemetry.metric]"));
}