arcature 0.1.0

Arcature: an opinionated full-stack Rust web framework. One package, batteries included.
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
//! Tests for the arcature-macros proc-macro crate.

#![cfg(feature = "macros")]

use arcature::jobs::JobRequest;
use serde::{Deserialize, Serialize};

// --- #[derive(Event)] ---

// `pub`, not private: `#[listener]` expands to a `pub` handler taking this
// type, and a private parameter on a public fn is a leak clippy rejects.
#[derive(Debug, Clone, Serialize, Deserialize, arcature::Event)]
pub struct UserRegistered {
    user_id: u64,
    email: String,
}

#[test]
fn derive_event_generates_dxcomponent_name() {
    use arcature::DxComponent;
    assert_eq!(UserRegistered::NAME, "UserRegistered");
}

// --- #[derive(Job)] ---

#[derive(Debug, Clone, Serialize, Deserialize, arcature::Job)]
struct SendVerificationEmail {
    user_id: u64,
}

#[test]
fn derive_job_generates_dxcomponent_name() {
    use arcature::DxComponent;
    assert_eq!(SendVerificationEmail::NAME, "SendVerificationEmail");
}

#[test]
fn derive_job_generates_job_const_with_defaults() {
    // Default: kind = snake_case name, version = 1, attempts = 3.
    assert_eq!(SendVerificationEmail::JOB.kind(), "send_verification_email");
    assert_eq!(SendVerificationEmail::JOB.version(), 1);
    assert_eq!(SendVerificationEmail::JOB.max_attempts(), 3);
}

#[derive(Debug, Clone, Serialize, Deserialize, arcature::Job)]
#[job(kind = "custom_kind", version = 2, attempts = 5)]
struct CleanupSessions {
    user_id: u64,
}

#[test]
fn derive_job_respects_helper_attributes() {
    assert_eq!(CleanupSessions::JOB.kind(), "custom_kind");
    assert_eq!(CleanupSessions::JOB.version(), 2);
    assert_eq!(CleanupSessions::JOB.max_attempts(), 5);
}

#[test]
fn derive_job_job_const_works_with_job_request() {
    let payload = SendVerificationEmail { user_id: 1 };
    let req = JobRequest::new(&SendVerificationEmail::JOB, &payload).unwrap();
    assert_eq!(req.kind(), "send_verification_email");
    assert_eq!(req.version(), 1);
    assert_eq!(req.effective_max_attempts(), 3);
}

// --- #[request] ---

#[derive(Debug, Clone, Deserialize, Serialize)]
#[arcature::request]
struct StoreLinkRequest {
    #[validate(url)]
    pub url: String,
    #[validate(length(min = 1, max = 120))]
    pub title: String,
}

#[test]
fn request_macro_prepends_validate() {
    // If the macro worked, the struct implements Validate.
    use validator::Validate;
    let req = StoreLinkRequest {
        url: "https://example.com".into(),
        title: "Example".into(),
    };
    assert!(req.validate().is_ok());
}

#[test]
fn request_macro_validation_rejects_invalid_url() {
    use validator::Validate;
    let req = StoreLinkRequest {
        url: "not-a-url".into(),
        title: "Example".into(),
    };
    assert!(req.validate().is_err());
}

// --- #[controller] ---

pub struct HomeController;

#[arcature::controller]
impl HomeController {
    pub async fn index() -> String {
        "hello".to_string()
    }

    pub async fn show(id: u64) -> String {
        format!("show {id}")
    }
}

#[test]
fn controller_macro_emits_the_impl_unchanged() {
    // The methods remain genuine functions the caller can invoke.
    let rt = tokio::runtime::Runtime::new().unwrap();
    assert_eq!(rt.block_on(HomeController::index()), "hello");
    assert_eq!(rt.block_on(HomeController::show(7)), "show 7");
}

#[test]
fn controller_macro_emits_controller_metadata() {
    use arcature::ControllerMetadata;

    let methods = <HomeController as ControllerMetadata>::METHODS;
    assert_eq!(methods.len(), 2);
    assert_eq!(methods[0].name, "index");
    assert_eq!(methods[0].params, [] as [&str; 0]);
    assert_eq!(methods[1].name, "show");
    assert_eq!(methods[1].params, ["id"]);
}

#[test]
fn controller_macro_infers_no_page_from_a_non_page_return_type() {
    use arcature::ControllerMetadata;

    for method in <HomeController as ControllerMetadata>::METHODS {
        assert_eq!(method.page, None, "{}", method.name);
    }
}

/// A `#[page]` type: `PAGE_CONTRACT` exists only for these, which is what
/// makes the return-type page derivation a firewall rather than a guess.
#[arcature::page("Dashboard")]
pub struct DashboardPage {
    pub title: String,
}

pub struct DashboardController;

#[arcature::controller]
impl DashboardController {
    /// The golden path: the page edge is read off the return type.
    pub async fn index() -> arcature::Page<DashboardPage> {
        arcature::dx::page(DashboardPage {
            title: "Dashboard".to_string(),
        })
    }

    /// The escape hatch: a handler that renders a page without returning
    /// `Page<T>` declares the identity explicitly.
    #[page("Reports")]
    pub async fn reports() -> String {
        "reports".to_string()
    }
}

#[test]
fn controller_macro_derives_the_page_edge_from_the_return_type() {
    use arcature::ControllerMetadata;

    let methods = <DashboardController as ControllerMetadata>::METHODS;
    assert_eq!(methods[0].name, "index");
    assert_eq!(methods[0].page, Some("Dashboard"));
}

#[test]
fn controller_macro_honours_an_explicit_page_attribute() {
    use arcature::ControllerMetadata;

    let methods = <DashboardController as ControllerMetadata>::METHODS;
    assert_eq!(methods[1].name, "reports");
    assert_eq!(methods[1].page, Some("Reports"));
}

// --- module! over a real #[controller] (Blocker 2 regression guard) ---
//
// `module!` emits `<Ctrl as ControllerMetadata>::METHODS` unconditionally.
// Before `#[controller]` generated that impl, *any* module naming a
// controller failed to compile. This test exists so that never regresses:
// if it compiles, the DSL is usable.

arcature::module! {
    pub Dashboard {
        controllers: [DashboardController],
    }
}

#[test]
fn module_macro_aggregates_controller_metadata() {
    let descriptor = dashboard_module();
    assert_eq!(descriptor.name, "Dashboard");
    assert_eq!(descriptor.controllers, ["DashboardController"]);
    assert_eq!(descriptor.controller_methods.len(), 1);

    let methods = descriptor.controller_methods[0];
    assert_eq!(methods.len(), 2);
    assert_eq!(methods[0].page, Some("Dashboard"));
    assert_eq!(methods[1].page, Some("Reports"));
}

// --- #[listener(Event)] ---

#[arcature::listener(UserRegistered)]
pub async fn send_welcome_email(
    event: UserRegistered,
) -> Result<(), arcature::events::DispatchError> {
    let _ = event;
    Ok(())
}

#[test]
fn listener_macro_emits_binding() {
    // The macro generates a `LISTENER_BINDING` static next to the fn.
    assert_eq!(LISTENER_BINDING.event, "UserRegistered");
    assert_eq!(LISTENER_BINDING.listener, "send_welcome_email");
}

#[test]
fn listener_macro_emits_fn_unchanged() {
    // If the macro worked, the fn is still callable.
    let rt = tokio::runtime::Runtime::new().unwrap();
    let event = UserRegistered {
        user_id: 1,
        email: "a@b.com".into(),
    };
    let result = rt.block_on(send_welcome_email(event));
    assert!(result.is_ok());
}

// --- #[command] ---
//
// The point of these is that the annotated function is genuinely reachable
// through `CommandRegistry::run`, which dispatches by name. A macro that
// only emitted a name would pass none of them.

/// Application state for the command and cache tests. Real applications
/// have a `Db` here; these tests need only something to resolve from.
#[derive(Debug, Clone)]
pub struct TestState {
    greeting: &'static str,
}

/// A resolvable dependency, standing in for a `#[service]`.
#[derive(Debug, Clone)]
pub struct Greeter {
    greeting: &'static str,
}

impl arcature::Resolve<TestState> for Greeter {
    fn resolve(state: &TestState) -> Self {
        Greeter {
            greeting: state.greeting,
        }
    }
}

#[derive(Debug)]
pub struct CommandFailure;

impl std::fmt::Display for CommandFailure {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("the command declined")
    }
}

impl std::error::Error for CommandFailure {}

#[arcature::command("greet:everyone")]
pub async fn greet_everyone(greeter: Greeter) -> Result<(), CommandFailure> {
    assert_eq!(greeter.greeting, "hello");
    Ok(())
}

#[arcature::command("greet:nobody")]
pub async fn greet_nobody() -> Result<(), CommandFailure> {
    Err(CommandFailure)
}

#[test]
fn command_macro_emits_the_binding_descriptor() {
    assert_eq!(GREET_EVERYONE_COMMAND.name, "greet:everyone");
    assert_eq!(GREET_EVERYONE_COMMAND.function, "greet_everyone");
}

#[test]
fn a_command_type_runs_through_the_registry_under_its_declared_name() {
    let registry = arcature::CommandRegistry::<TestState>::new()
        .register_command::<GreetEveryoneCommand>()
        .register_command::<GreetNobodyCommand>();

    assert_eq!(registry.names(), vec!["greet:everyone", "greet:nobody"]);

    let state = TestState { greeting: "hello" };
    let rt = tokio::runtime::Runtime::new().unwrap();
    assert!(rt.block_on(registry.run("greet:everyone", &state)).is_ok());
}

#[test]
fn a_failing_command_surfaces_its_error_through_the_registry() {
    let registry =
        arcature::CommandRegistry::<TestState>::new().register_command::<GreetNobodyCommand>();

    let state = TestState { greeting: "hello" };
    let rt = tokio::runtime::Runtime::new().unwrap();
    let err = rt
        .block_on(registry.run("greet:nobody", &state))
        .unwrap_err();
    assert!(err.to_string().contains("the command declined"), "{err}");
}

// --- #[provider] ---

#[derive(Debug)]
pub struct SearchInitError;

impl std::fmt::Display for SearchInitError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("search client could not start")
    }
}

impl std::error::Error for SearchInitError {}

#[arcature::provider(error = SearchInitError, deps = [Greeter])]
pub struct SearchClient {
    endpoint: String,
}

#[arcature::provider]
pub struct ClockProvider {
    offset: i64,
}

#[test]
fn provider_macro_generates_a_usable_provider_impl() {
    use arcature::{DxComponent, Provider};

    assert_eq!(SearchClient::NAME, "SearchClient");
    assert_eq!(<SearchClient as Provider>::DEPS, ["Greeter"]);

    // The associated error type is the declared one: this only compiles
    // because `Provider` is genuinely implemented, not merely documented.
    fn describe<P: Provider>(error: P::Error) -> String {
        error.to_string()
    }
    assert_eq!(
        describe::<SearchClient>(SearchInitError),
        "search client could not start"
    );

    let client = SearchClient {
        endpoint: "http://localhost".to_string(),
    };
    assert_eq!(client.endpoint, "http://localhost");
}

#[test]
fn a_provider_that_declares_no_failure_is_infallible() {
    use arcature::Provider;

    // `Infallible` has no values, so a match on `P::Error` needs no arms --
    // the type system carries "init cannot fail".
    fn unreachable_error(error: <ClockProvider as Provider>::Error) -> ! {
        match error {}
    }
    let _ = unreachable_error;

    assert!(<ClockProvider as Provider>::DEPS.is_empty());
    let clock = ClockProvider { offset: 0 };
    assert_eq!(clock.offset, 0);
}

// --- #[request_cache] ---

use std::sync::atomic::{AtomicUsize, Ordering};

static PROFILE_LOADS: AtomicUsize = AtomicUsize::new(0);

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Profile {
    user_id: u64,
}

#[arcature::request_cache(name = "load_profile", key = "user_id")]
pub async fn load_profile(
    cache: arcature::dx::RequestCache,
    user_id: u64,
) -> Result<Profile, CommandFailure> {
    PROFILE_LOADS.fetch_add(1, Ordering::SeqCst);
    Ok(Profile { user_id })
}

#[test]
fn request_cache_macro_emits_the_descriptor() {
    assert_eq!(LOAD_PROFILE_REQUEST_CACHE.name, "load_profile");
    assert_eq!(LOAD_PROFILE_REQUEST_CACHE.key_fields, ["user_id"]);
}

#[test]
fn a_memoized_resolver_computes_once_per_request_and_key() {
    let rt = tokio::runtime::Runtime::new().unwrap();
    PROFILE_LOADS.store(0, Ordering::SeqCst);

    rt.block_on(async {
        let request = arcature::dx::RequestCache::new();

        let first = load_profile(request.clone(), 7).await.unwrap();
        let second = load_profile(request.clone(), 7).await.unwrap();
        assert_eq!(first, second);
        assert_eq!(PROFILE_LOADS.load(Ordering::SeqCst), 1);

        // A different key value is a different computation.
        load_profile(request.clone(), 8).await.unwrap();
        assert_eq!(PROFILE_LOADS.load(Ordering::SeqCst), 2);

        // A different request shares nothing.
        let other_request = arcature::dx::RequestCache::new();
        load_profile(other_request, 7).await.unwrap();
        assert_eq!(PROFILE_LOADS.load(Ordering::SeqCst), 3);
    });
}

// --- module! { pages: [...] } ---

arcature::module! {
    pub Reporting {
        controllers: [DashboardController],
        pages: [DashboardPage],
    }
}

#[test]
fn module_macro_records_page_identities_from_their_contract_entries() {
    let descriptor = reporting_module();
    assert_eq!(descriptor.pages, ["Dashboard"]);
    // The identity is the same string the controller edge carries, so the
    // UAG can join route -> controller method -> page without a lookup
    // table.
    assert_eq!(descriptor.controller_methods[0][0].page, Some("Dashboard"));
}