vespera_macro 0.3.1

Procedural macros for Vespera - automatic route discovery and OpenAPI generation
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
use std::fs;

use tempfile::TempDir;

use super::*;

fn create_temp_file(dir: &TempDir, filename: &str, content: &str) -> std::path::PathBuf {
    let file_path = dir.path().join(filename);
    if let Some(parent) = file_path.parent() {
        fs::create_dir_all(parent).expect("Failed to create parent directory");
    }
    fs::write(&file_path, content).expect("Failed to write temp file");
    file_path
}

// ========== Tests for finalize_metadata ==========

#[test]
fn test_finalize_metadata_extends_structs_from_storage() {
    let mut metadata = crate::metadata::CollectedMetadata::new();
    let mut storage = HashMap::new();
    storage.insert(
        "User".to_string(),
        StructMetadata::new("User".into(), "struct User { id: i32 }".into()),
    );

    finalize_metadata(&mut metadata, &storage, &[], "vespera!").expect("no duplicates");

    assert_eq!(metadata.structs.len(), 1);
    assert_eq!(metadata.structs[0].name, "User");
}

/// The duplicate-schema-name error must keep its per-macro prefix verbatim now
/// that a single helper builds it for both `vespera!` and `export_app!`.
#[rstest::rstest]
#[case("vespera!", "vespera! macro: Duplicate OpenAPI schema name 'User'")]
#[case(
    "export_app!",
    "export_app! macro: Duplicate OpenAPI schema name 'User'"
)]
fn test_finalize_metadata_duplicate_error_prefix(#[case] label: &str, #[case] expected: &str) {
    let mut metadata = crate::metadata::CollectedMetadata::new();
    metadata.structs.push(StructMetadata::new(
        "User".into(),
        "struct User { id: i32 }".into(),
    ));
    let mut storage = HashMap::new();
    storage.insert(
        "User".to_string(),
        StructMetadata::new("User".into(), "struct User { name: String }".into()),
    );

    let err = finalize_metadata(&mut metadata, &storage, &[], label)
        .expect_err("differing definitions must be rejected")
        .to_string();

    assert!(err.starts_with(expected), "got: {err}");
}

// ========== Tests for process_vespera_macro ==========

#[test]
fn test_process_vespera_macro_folder_not_found() {
    let processed = ProcessedVesperaInput {
        folder_name: "nonexistent_folder_xyz_123".to_string(),
        openapi_file_names: vec![],
        title: None,
        version: None,
        docs_url: None,
        redoc_url: None,
        servers: None,
        security_schemes: None,
        security: None,
        tag_descriptions: None,
        merge: vec![],
    };
    let result = process_vespera_macro(&processed, &HashMap::new(), &[], Span::call_site());
    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(err.contains("route folder") && err.contains("not found"));
}

#[test]
fn test_process_vespera_macro_collect_metadata_error() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");

    // Create an invalid route file (will cause parse error but collect_metadata handles it)
    create_temp_file(&temp_dir, "invalid.rs", "not valid rust code {{{");

    let processed = ProcessedVesperaInput {
        folder_name: temp_dir.path().to_string_lossy().to_string(),
        openapi_file_names: vec![],
        title: Some("Test API".to_string()),
        version: Some("1.0.0".to_string()),
        docs_url: None,
        redoc_url: None,
        servers: None,
        security_schemes: None,
        security: None,
        tag_descriptions: None,
        merge: vec![],
    };

    // This exercises the collect_metadata path (which handles parse errors gracefully)
    let result = process_vespera_macro(&processed, &HashMap::new(), &[], Span::call_site());
    // Result may succeed or fail depending on how collect_metadata handles invalid files
    let _ = result;
}

#[test]
fn test_process_vespera_macro_with_schema_storage() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");

    // Create an empty file (valid but no routes)
    create_temp_file(&temp_dir, "empty.rs", "// empty file\n");

    let schema_storage = HashMap::from([(
        "TestSchema".to_string(),
        StructMetadata::new(
            "TestSchema".to_string(),
            "struct TestSchema { id: i32 }".to_string(),
        ),
    )]);

    let processed = ProcessedVesperaInput {
        folder_name: temp_dir.path().to_string_lossy().to_string(),
        openapi_file_names: vec![],
        title: None,
        version: None,
        docs_url: Some("/docs".to_string()),
        redoc_url: Some("/redoc".to_string()),
        servers: None,
        security_schemes: None,
        security: None,
        tag_descriptions: None,
        merge: vec![],
    };

    // This exercises the schema_storage extend path
    let result = process_vespera_macro(&processed, &schema_storage, &[], Span::call_site());
    // We only care about exercising the code path
    let _ = result;
}

#[test]
#[serial_test::serial]
fn test_process_vespera_macro_with_cron_storage() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");

    // Create src/ subfolder structure to simulate a real project
    let src_dir = temp_dir.path().join("src");
    std::fs::create_dir_all(src_dir.join("routes")).expect("create routes dir");
    std::fs::write(src_dir.join("routes").join("health.rs"), "// empty\n")
        .expect("write health.rs");

    // Set CARGO_MANIFEST_DIR so module path derivation works
    let old_manifest = std::env::var("CARGO_MANIFEST_DIR").ok();
    unsafe {
        std::env::set_var(
            "CARGO_MANIFEST_DIR",
            temp_dir.path().to_string_lossy().as_ref(),
        );
    }

    // Populate CRON_STORAGE with a fake cron entry under the CURRENT crate
    // key (CARGO_MANIFEST_DIR was set to temp_dir above, so this lands in the
    // same bucket `process_vespera_macro` reads back via `current_crate_crons`).
    crate::cron_impl::register_cron(crate::cron_impl::StoredCronInfo {
        fn_name: "test_cron_job".to_string(),
        expression: "0 */5 * * * *".to_string(),
        file_path: Some(
            src_dir
                .join("routes")
                .join("health.rs")
                .display()
                .to_string(),
        ),
    });

    let processed = ProcessedVesperaInput {
        folder_name: src_dir.join("routes").to_string_lossy().to_string(),
        openapi_file_names: vec![],
        title: None,
        version: None,
        docs_url: None,
        redoc_url: None,
        servers: None,
        security_schemes: None,
        security: None,
        tag_descriptions: None,
        merge: vec![],
    };

    // This exercises the CRON_STORAGE → CronMetadata derivation path
    let result = process_vespera_macro(&processed, &HashMap::new(), &[], Span::call_site());
    assert!(
        result.is_ok(),
        "Should succeed with cron storage: {result:?}"
    );

    // Clean up: drop this crate-key's cron bucket (temp_dir key is unique to
    // this test, and CARGO_MANIFEST_DIR is still temp_dir at this point).
    {
        let mut storage = crate::cron_impl::CRON_STORAGE
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        storage.remove(&crate::schema_impl::current_crate_key());
    }

    // Restore CARGO_MANIFEST_DIR
    unsafe {
        if let Some(val) = old_manifest {
            std::env::set_var("CARGO_MANIFEST_DIR", val);
        } else {
            std::env::remove_var("CARGO_MANIFEST_DIR");
        }
    }
}

// ========== Tests for process_export_app ==========

#[test]
fn test_process_export_app_folder_not_found() {
    let name: syn::Ident = syn::parse_quote!(TestApp);
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let result = process_export_app(
        &name,
        "nonexistent_folder_xyz",
        &HashMap::new(),
        &temp_dir.path().to_string_lossy(),
        &[],
        Span::call_site(),
    );
    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(err.contains("route folder") && err.contains("not found"));
}

#[test]
fn test_process_export_app_with_empty_folder() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");

    // Create an empty file
    create_temp_file(&temp_dir, "empty.rs", "// empty\n");

    let name: syn::Ident = syn::parse_quote!(TestApp);
    let folder_path = temp_dir.path().to_string_lossy().to_string();

    // This exercises collect_metadata and other paths
    let result = process_export_app(
        &name,
        &folder_path,
        &HashMap::new(),
        &temp_dir.path().to_string_lossy(),
        &[],
        Span::call_site(),
    );
    // We only care about exercising the code path
    let _ = result;
}

#[test]
fn test_process_export_app_with_schema_storage() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");

    // Create an empty but valid Rust file
    create_temp_file(&temp_dir, "mod.rs", "// module file\n");

    let schema_storage = HashMap::from([(
        "AppSchema".to_string(),
        StructMetadata::new(
            "AppSchema".to_string(),
            "struct AppSchema { name: String }".to_string(),
        ),
    )]);

    let name: syn::Ident = syn::parse_quote!(MyExportedApp);
    let folder_path = temp_dir.path().to_string_lossy().to_string();

    let result = process_export_app(
        &name,
        &folder_path,
        &schema_storage,
        &temp_dir.path().to_string_lossy(),
        &[],
        Span::call_site(),
    );
    // Exercises the schema_storage.extend path
    let _ = result;
}

#[test]
fn test_process_export_app_collect_metadata_error() {
    // Lines 210-212: collect_metadata returns error for invalid Rust syntax
    let temp_dir = TempDir::new().expect("Failed to create temp dir");

    // Create a file with invalid Rust syntax that will cause parse error
    create_temp_file(&temp_dir, "invalid.rs", "fn broken( { syntax error");

    let name: syn::Ident = syn::parse_quote!(TestApp);
    let folder_path = temp_dir.path().to_string_lossy().to_string();

    let result = process_export_app(
        &name,
        &folder_path,
        &HashMap::new(),
        &temp_dir.path().to_string_lossy(),
        &[],
        Span::call_site(),
    );

    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(err.contains("failed to scan route folder"));
}

#[test]
fn test_process_export_app_create_dir_error() {
    // Lines 232-234: create_dir_all failure when path contains a file
    let temp_dir = TempDir::new().expect("Failed to create temp dir");

    // Create an empty valid Rust file
    create_temp_file(&temp_dir, "empty.rs", "// empty file\n");

    // Create target directory but make 'vespera' a file instead of directory
    let target_dir = temp_dir.path().join("target");
    fs::create_dir(&target_dir).expect("Failed to create target dir");
    fs::write(target_dir.join("vespera"), "blocking file").expect("Failed to write file");

    let name: syn::Ident = syn::parse_quote!(TestApp);
    let folder_path = temp_dir.path().to_string_lossy().to_string();

    let result = process_export_app(
        &name,
        &folder_path,
        &HashMap::new(),
        &temp_dir.path().to_string_lossy(),
        &[],
        Span::call_site(),
    );

    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(err.contains("failed to create build cache directory"));
}

#[test]
fn test_process_export_app_write_spec_error() {
    // Lines 239-241: fs::write failure when spec file path is a directory
    let temp_dir = TempDir::new().expect("Failed to create temp dir");

    // Create an empty valid Rust file
    create_temp_file(&temp_dir, "empty.rs", "// empty file\n");

    // Create target/vespera directory and make spec file name a directory
    let vespera_dir = temp_dir.path().join("target").join("vespera");
    fs::create_dir_all(&vespera_dir).expect("Failed to create vespera dir");
    // Create a directory where the spec file should be written
    fs::create_dir(vespera_dir.join("TestApp.openapi.json"))
        .expect("Failed to create blocking dir");

    let name: syn::Ident = syn::parse_quote!(TestApp);
    let folder_path = temp_dir.path().to_string_lossy().to_string();

    let result = process_export_app(
        &name,
        &folder_path,
        &HashMap::new(),
        &temp_dir.path().to_string_lossy(),
        &[],
        Span::call_site(),
    );

    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(err.contains("failed to write OpenAPI spec file"));
}
#[test]
fn test_process_vespera_macro_no_openapi_output() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    create_temp_file(&temp_dir, "empty.rs", "// empty route file\n");

    let processed = ProcessedVesperaInput {
        folder_name: temp_dir.path().to_string_lossy().to_string(),
        openapi_file_names: vec![],
        title: None,
        version: None,
        docs_url: None,
        redoc_url: None,
        servers: None,
        security_schemes: None,
        security: None,
        tag_descriptions: None,
        merge: vec![],
    };

    let result = process_vespera_macro(&processed, &HashMap::new(), &[], Span::call_site());
    assert!(
        result.is_ok(),
        "Should succeed with no openapi output configured"
    );
}

#[test]
#[serial_test::serial]
fn test_process_vespera_macro_with_profiling() {
    let old_profile = std::env::var("VESPERA_PROFILE").ok();
    unsafe { std::env::set_var("VESPERA_PROFILE", "1") };

    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    create_temp_file(&temp_dir, "empty.rs", "// empty\n");

    let processed = ProcessedVesperaInput {
        folder_name: temp_dir.path().to_string_lossy().to_string(),
        openapi_file_names: vec![],
        title: None,
        version: None,
        docs_url: None,
        redoc_url: None,
        servers: None,
        security_schemes: None,
        security: None,
        tag_descriptions: None,
        merge: vec![],
    };

    let result = process_vespera_macro(&processed, &HashMap::new(), &[], Span::call_site());

    // Restore
    unsafe {
        if let Some(val) = old_profile {
            std::env::set_var("VESPERA_PROFILE", val);
        } else {
            std::env::remove_var("VESPERA_PROFILE");
        }
    };

    assert!(result.is_ok());
}

#[test]
#[serial_test::serial]
fn test_process_export_app_with_profiling() {
    let old_profile = std::env::var("VESPERA_PROFILE").ok();
    unsafe { std::env::set_var("VESPERA_PROFILE", "1") };

    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    create_temp_file(&temp_dir, "empty.rs", "// empty\n");

    let name: syn::Ident = syn::parse_quote!(TestProfileApp);
    let folder_path = temp_dir.path().to_string_lossy().to_string();

    let result = process_export_app(
        &name,
        &folder_path,
        &HashMap::new(),
        &temp_dir.path().to_string_lossy(),
        &[],
        Span::call_site(),
    );

    // Restore
    unsafe {
        if let Some(val) = old_profile {
            std::env::set_var("VESPERA_PROFILE", val);
        } else {
            std::env::remove_var("VESPERA_PROFILE");
        }
    };

    // Exercise the code path
    let _ = result;
}

#[test]
#[serial_test::serial]
fn test_process_vespera_macro_cache_hit() {
    // Exercises lines 320-324, 327, 329: the cache_hit branch in process_vespera_macro.
    // First call populates the cache, second call hits it.
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    create_temp_file(
        &temp_dir,
        "users.rs",
        "pub async fn list_users() -> String { \"users\".to_string() }\n",
    );

    let folder_path = temp_dir.path().to_string_lossy().to_string();
    let openapi_path = temp_dir.path().join("openapi.json");

    // Set CARGO_MANIFEST_DIR so cache path resolves to temp_dir/target/vespera/
    let old_manifest = std::env::var("CARGO_MANIFEST_DIR").ok();
    unsafe { std::env::set_var("CARGO_MANIFEST_DIR", temp_dir.path()) };

    let processed = ProcessedVesperaInput {
        folder_name: folder_path.clone(),
        openapi_file_names: vec![openapi_path.to_string_lossy().to_string()],
        title: Some("Test API".to_string()),
        version: Some("1.0.0".to_string()),
        docs_url: Some("/docs".to_string()),
        redoc_url: None,
        servers: None,
        security_schemes: None,
        security: None,
        tag_descriptions: None,
        merge: vec![],
    };

    // First call: cache MISS — scans files, generates spec, writes cache
    let result1 = process_vespera_macro(&processed, &HashMap::new(), &[], Span::call_site());
    assert!(
        result1.is_ok(),
        "First call (cache miss) should succeed: {:?}",
        result1.err()
    );
    assert!(
        openapi_path.exists(),
        "openapi.json should be written on first call"
    );

    // Second call: cache HIT — exercises lines 320-324, 327, 329
    let result2 = process_vespera_macro(&processed, &HashMap::new(), &[], Span::call_site());
    assert!(
        result2.is_ok(),
        "Second call (cache hit) should succeed: {:?}",
        result2.err()
    );

    // Restore CARGO_MANIFEST_DIR
    unsafe {
        if let Some(val) = old_manifest {
            std::env::set_var("CARGO_MANIFEST_DIR", val);
        } else {
            std::env::remove_var("CARGO_MANIFEST_DIR");
        }
    };
}

#[rstest::rstest]
#[case::slashes_and_hyphens("admin-tools/daily-job.rs", "admin_tools::daily_job")]
#[case::module_file("admin-tools/mod.rs", "admin_tools")]
fn cron_module_path_normalizes_rust_module_syntax(#[case] input: &str, #[case] expected: &str) {
    assert_eq!(cron_module_path(input), expected);
}

#[test]
#[serial_test::serial]
fn process_export_app_reuses_fresh_cache_and_sidecar() {
    struct Restore(Option<String>);
    impl Drop for Restore {
        fn drop(&mut self) {
            // SAFETY: this serialized test restores the process environment through RAII.
            unsafe {
                match self.0.take() {
                    Some(value) => std::env::set_var("CARGO_MANIFEST_DIR", value),
                    None => std::env::remove_var("CARGO_MANIFEST_DIR"),
                }
            }
        }
    }

    let temp = TempDir::new().unwrap();
    let routes = temp.path().join("src/routes");
    fs::create_dir_all(&routes).unwrap();
    fs::write(routes.join("mod.rs"), "// empty routes").unwrap();
    let _restore = Restore(std::env::var("CARGO_MANIFEST_DIR").ok());
    // SAFETY: this serialized test restores the process environment through RAII.
    unsafe { std::env::set_var("CARGO_MANIFEST_DIR", temp.path()) };
    let name: syn::Ident = syn::parse_quote!(CachedApp);
    let folder = routes.to_string_lossy().into_owned();

    let first = process_export_app(
        &name,
        &folder,
        &HashMap::new(),
        &temp.path().to_string_lossy(),
        &[],
        Span::call_site(),
    )
    .expect("first expansion populates cache");
    let second = process_export_app(
        &name,
        &folder,
        &HashMap::new(),
        &temp.path().to_string_lossy(),
        &[],
        Span::call_site(),
    )
    .expect("second expansion reuses cache");

    assert_eq!(first.to_string(), second.to_string());
}