brainos-httpadapter 0.5.0

Axum HTTP REST adapter for Brain OS
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
//! HTTP adapter unit + integration tests.

use std::{num::NonZeroUsize, sync::Arc};

use axum::{
    body::Body,
    http::{self, Request},
    routing::{get, post},
    Router,
};
use tokio::sync::Mutex;
use tower::util::ServiceExt;
use uuid::Uuid;

use crate::handlers::*;
use crate::metrics::Metrics;
use crate::state::{AppState, CACHE_CAPACITY};
use crate::types::*;

/// Build a test router with the API key pre-loaded.
async fn make_router() -> (Router, tempfile::TempDir, String) {
    let temp = tempfile::tempdir().unwrap();
    let mut config = brain::BrainConfig::default();
    config.brain.data_dir = temp.path().to_str().unwrap().to_string();
    let api_key = config.access.api_keys.first().unwrap().key.clone();
    let api_keys = config.access.api_keys.clone();
    let processor = signal::SignalProcessor::new(config).await.unwrap();
    let router = crate::create_router(
        Arc::new(processor),
        std::collections::HashMap::new(),
        api_keys,
        true,
    );
    (router, temp, api_key)
}

#[test]
fn test_parse_source_defaults_to_http() {
    assert_eq!(
        signal::SignalSource::parse(None, signal::SignalSource::Http),
        signal::SignalSource::Http
    );
    assert_eq!(
        signal::SignalSource::parse(Some("http"), signal::SignalSource::Http),
        signal::SignalSource::Http
    );
}

#[test]
fn test_parse_source_all_variants() {
    assert_eq!(
        signal::SignalSource::parse(Some("cli"), signal::SignalSource::Http),
        signal::SignalSource::Cli
    );
    assert_eq!(
        signal::SignalSource::parse(Some("ws"), signal::SignalSource::Http),
        signal::SignalSource::WebSocket
    );
    assert_eq!(
        signal::SignalSource::parse(Some("mcp"), signal::SignalSource::Http),
        signal::SignalSource::Mcp
    );
    assert_eq!(
        signal::SignalSource::parse(Some("grpc"), signal::SignalSource::Http),
        signal::SignalSource::Grpc
    );
}

#[test]
fn test_health_response_serializes() {
    let h = HealthResponse {
        status: "ok",
        version: "1.0.0",
    };
    let json = serde_json::to_string(&h).unwrap();
    assert!(json.contains("\"status\":\"ok\""));
    assert!(json.contains("\"version\""));
}

#[test]
fn test_fact_json_serializes() {
    let f = FactJson {
        id: "abc".into(),
        namespace: "personal".into(),
        category: "personal".into(),
        subject: "user".into(),
        predicate: "likes".into(),
        object: "Rust".into(),
        confidence: 0.9,
        distance: Some(0.05),
    };
    let json = serde_json::to_string(&f).unwrap();
    assert!(json.contains("\"subject\":\"user\""));
    assert!(json.contains("\"namespace\":\"personal\""));
    assert!(json.contains("\"distance\":0.05"));
}

/// Per-client rate limit (Issue 51): after the burst is drained, the
/// next `/v1/*` request from the same key gets 429. Anonymous routes
/// (`/health`) remain unthrottled.
#[tokio::test]
async fn test_rate_limit_returns_429_after_burst() {
    let temp = tempfile::tempdir().unwrap();
    let mut config = brain::BrainConfig::default();
    config.brain.data_dir = temp.path().to_str().unwrap().to_string();
    // Tiny bucket so the test is fast and deterministic.
    config.access.rate_limit = brain::ClientRateLimitConfig {
        enabled: true,
        tokens_per_refill: 1,
        refill_interval_ms: 60_000,
        burst_capacity: 2,
    };
    let api_key = config.access.api_keys.first().unwrap().key.clone();
    let api_keys = config.access.api_keys.clone();
    let processor = signal::SignalProcessor::new(config).await.unwrap();
    let registry = std::sync::Arc::new(resilience::RateLimitRegistry::new(
        resilience::RateLimitConfig {
            tokens_per_refill: 1,
            refill_interval: std::time::Duration::from_millis(60_000),
            burst_capacity: 2,
        },
    ));
    let processor = processor.with_client_rate_limits(registry);
    let router = crate::create_router(
        Arc::new(processor),
        std::collections::HashMap::new(),
        api_keys,
        true,
    );

    let make_req = || {
        Request::builder()
            .method(http::Method::GET)
            .uri("/v1/memory/facts")
            .header("authorization", format!("Bearer {api_key}"))
            .body(Body::empty())
            .unwrap()
    };

    // Burst of 2 should succeed (auth passes — 200 with empty list).
    for _ in 0..2 {
        let resp = router.clone().oneshot(make_req()).await.unwrap();
        assert!(
            resp.status() != http::StatusCode::TOO_MANY_REQUESTS,
            "burst should not 429: got {}",
            resp.status()
        );
    }
    // Third should 429.
    let resp = router.clone().oneshot(make_req()).await.unwrap();
    assert_eq!(resp.status(), http::StatusCode::TOO_MANY_REQUESTS);
    let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
        .await
        .unwrap();
    let payload: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
    assert_eq!(payload["error"], "rate_limited");

    // /health is anonymous → still served, never 429.
    let health = router
        .oneshot(
            Request::builder()
                .method(http::Method::GET)
                .uri("/health")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(health.status(), http::StatusCode::OK);
}

/// POST /v1/webhooks/:id on a verifier-less transport must require a
/// Brain API key (Issue 52). Anonymous POST → 401, Bearer → 200.
#[tokio::test]
async fn test_post_webhook_without_verifier_requires_bearer_auth() {
    use channel::transport::{
        inbound::{WebhookInboundConfig, WebhookInboundTransport},
        preset::{
            FieldExtractors, HttpMethod, PresetDefinition, PresetKind, SendSpec, WebhookInboundSpec,
        },
    };

    let temp = tempfile::tempdir().unwrap();
    let mut config = brain::BrainConfig::default();
    config.brain.data_dir = temp.path().to_str().unwrap().to_string();
    let api_key = config.access.api_keys.first().unwrap().key.clone();
    let api_keys = config.access.api_keys.clone();

    let preset = PresetDefinition {
        id: "test-webhook".into(),
        kind: PresetKind::WebhookInbound,
        label: Some("test".into()),
        poll: None,
        send: Some(SendSpec {
            url_template: "http://example.invalid/{reply_to}".into(),
            method: HttpMethod::Post,
            body_template: "{}".into(),
            content_type: "application/json".into(),
            headers: std::collections::HashMap::new(),
        }),
        webhook: Some(WebhookInboundSpec {
            messages_path: "$".into(),
            extract: FieldExtractors {
                id: Some("$.id".into()),
                text: "$.text".into(),
                user_ref: None,
                reply_to: None,
                extra: std::collections::HashMap::new(),
            },
            ack_body: None,
            ack_content_type: "application/json".into(),
            ack_extract: std::collections::HashMap::new(),
            ack_only_when: None,
        }),
        verifier: None, // ← key: no signature verification
    };
    let transport = std::sync::Arc::new(
        WebhookInboundTransport::new(WebhookInboundConfig::new("w", "w", preset)).unwrap(),
    );
    assert!(
        !transport.has_verifier(),
        "test fixture should be verifier-less"
    );

    let processor = signal::SignalProcessor::new(config).await.unwrap();
    let mut webhooks = std::collections::HashMap::new();
    webhooks.insert("ingest".to_string(), transport);

    let router = crate::create_router(Arc::new(processor), webhooks, api_keys, true);

    // Anonymous → 401.
    let resp = router
        .clone()
        .oneshot(
            Request::builder()
                .method(http::Method::POST)
                .uri("/v1/webhooks/ingest")
                .header("content-type", "application/json")
                .body(Body::from(r#"{"id":"m1","text":"hi"}"#))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), http::StatusCode::UNAUTHORIZED);

    // With Bearer → 200.
    let resp = router
        .oneshot(
            Request::builder()
                .method(http::Method::POST)
                .uri("/v1/webhooks/ingest")
                .header("authorization", format!("Bearer {api_key}"))
                .header("content-type", "application/json")
                .body(Body::from(r#"{"id":"m1","text":"hi"}"#))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), http::StatusCode::OK);
}

/// POST /v1/webhooks/:id with an unknown id returns 404 (regression for Issue 43 — used to panic via `Response::builder().unwrap()`).
#[tokio::test]
async fn test_post_webhook_unknown_id_returns_404() {
    let (router, _tmp, _api_key) = make_router().await;

    let request = Request::builder()
        .method(http::Method::POST)
        .uri("/v1/webhooks/does-not-exist")
        .body(Body::from(""))
        .unwrap();

    let response = router.oneshot(request).await.unwrap();
    assert_eq!(response.status(), http::StatusCode::NOT_FOUND);
    let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .unwrap();
    let text = String::from_utf8(bytes.to_vec()).unwrap();
    assert!(
        text.contains("does-not-exist"),
        "expected 404 body to identify the missing transport: {text}"
    );
}

/// GET /openapi.json — no auth required, returns valid OpenAPI spec.
#[tokio::test]
async fn test_openapi_endpoint() {
    let (router, _tmp, _api_key) = make_router().await;

    let request = Request::builder()
        .method(http::Method::GET)
        .uri("/openapi.json")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(request).await.unwrap();
    assert_eq!(response.status(), http::StatusCode::OK);

    let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .unwrap();
    let spec: serde_json::Value = serde_json::from_slice(&bytes).expect("valid JSON");
    assert_eq!(spec["openapi"], "3.0.3");
    assert!(
        spec["paths"]["/v1/signals"].is_object(),
        "missing /v1/signals path"
    );
    assert!(
        spec["components"]["schemas"]["FactJson"].is_object(),
        "missing FactJson schema"
    );
}

/// GET /api — no auth required, returns Swagger UI HTML.
#[tokio::test]
async fn test_swagger_ui_endpoint() {
    let (router, _tmp, _api_key) = make_router().await;

    let request = Request::builder()
        .method(http::Method::GET)
        .uri("/api")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(request).await.unwrap();
    assert_eq!(response.status(), http::StatusCode::OK);

    let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .unwrap();
    let body = std::str::from_utf8(&bytes).unwrap();
    assert!(body.contains("swagger-ui"), "missing Swagger UI element");
    assert!(body.contains("/openapi.json"), "missing spec URL reference");
}

/// GET /ui — no auth required, returns HTML page.
#[tokio::test]
async fn test_ui_endpoint() {
    let (router, _tmp, _api_key) = make_router().await;

    let request = Request::builder()
        .method(http::Method::GET)
        .uri("/ui")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(request).await.unwrap();
    assert_eq!(response.status(), http::StatusCode::OK);

    let ct = response
        .headers()
        .get("content-type")
        .unwrap()
        .to_str()
        .unwrap();
    assert!(ct.contains("text/html"), "expected text/html, got: {ct}");

    let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .unwrap();
    let body = std::str::from_utf8(&bytes).unwrap();
    assert!(body.contains("Brain Memory Explorer"), "missing page title");
    assert!(
        body.contains("/v1/memory/search"),
        "missing API endpoint reference"
    );
}

/// GET /metrics — no auth required, returns Prometheus text.
#[tokio::test]
async fn test_metrics_endpoint() {
    let (router, _tmp, _api_key) = make_router().await;

    let request = Request::builder()
        .method(http::Method::GET)
        .uri("/metrics")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(request).await.unwrap();
    assert_eq!(response.status(), http::StatusCode::OK);

    let ct = response
        .headers()
        .get("content-type")
        .unwrap()
        .to_str()
        .unwrap();
    assert!(ct.contains("text/plain"), "expected text/plain, got: {ct}");

    let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .unwrap();
    let body = std::str::from_utf8(&bytes).unwrap();
    assert!(
        body.contains("brain_signals_total"),
        "missing counter in metrics output"
    );
    assert!(
        body.contains("brain_search_total"),
        "missing search counter"
    );
}

/// GET /health — no auth required, always returns 200.
#[tokio::test]
async fn test_health_endpoint() {
    let (router, _tmp, _api_key) = make_router().await;

    let request = Request::builder()
        .method(http::Method::GET)
        .uri("/health")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(request).await.unwrap();
    assert_eq!(response.status(), http::StatusCode::OK);

    let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .unwrap();
    let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
    assert_eq!(body["status"], "ok");
}

/// POST /v1/signals without auth → 401.
#[tokio::test]
async fn test_post_signal_no_auth_returns_401() {
    let (router, _tmp, _api_key) = make_router().await;

    let payload = serde_json::json!({"content": "Remember Rust is fast"});
    let request = Request::builder()
        .method(http::Method::POST)
        .uri("/v1/signals")
        .header("content-type", "application/json")
        .body(Body::from(serde_json::to_string(&payload).unwrap()))
        .unwrap();

    let response = router.oneshot(request).await.unwrap();
    assert_eq!(response.status(), http::StatusCode::UNAUTHORIZED);
}

/// POST /v1/signals with invalid key → 401.
#[tokio::test]
async fn test_post_signal_invalid_key_returns_401() {
    let (router, _tmp, _api_key) = make_router().await;

    let payload = serde_json::json!({"content": "Remember Rust is fast"});
    let request = Request::builder()
        .method(http::Method::POST)
        .uri("/v1/signals")
        .header("content-type", "application/json")
        .header("authorization", "Bearer wrong-key")
        .body(Body::from(serde_json::to_string(&payload).unwrap()))
        .unwrap();

    let response = router.oneshot(request).await.unwrap();
    assert_eq!(response.status(), http::StatusCode::UNAUTHORIZED);
}

/// POST /v1/signals with valid API key → 200.
#[tokio::test]
async fn test_post_signal_store_fact_with_auth() {
    let (router, _tmp, api_key) = make_router().await;

    let payload = serde_json::json!({"content": "Remember that Rust is fast"});
    let request = Request::builder()
        .method(http::Method::POST)
        .uri("/v1/signals")
        .header("content-type", "application/json")
        .header("authorization", format!("Bearer {api_key}"))
        .body(Body::from(serde_json::to_string(&payload).unwrap()))
        .unwrap();

    let response = router.oneshot(request).await.unwrap();
    assert_eq!(response.status(), http::StatusCode::OK);

    let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .unwrap();
    let resp: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
    assert_eq!(resp["status"], "Ok");
}

/// GET /v1/memory/facts with no auth → 401.
#[tokio::test]
async fn test_get_facts_no_auth_returns_401() {
    let (router, _tmp, _api_key) = make_router().await;

    let request = Request::builder()
        .method(http::Method::GET)
        .uri("/v1/memory/facts")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(request).await.unwrap();
    assert_eq!(response.status(), http::StatusCode::UNAUTHORIZED);
}

/// GET /v1/memory/facts with valid API key → 200.
#[tokio::test]
async fn test_get_facts_endpoint_with_auth() {
    let (router, _tmp, api_key) = make_router().await;

    let request = Request::builder()
        .method(http::Method::GET)
        .uri("/v1/memory/facts")
        .header("authorization", format!("Bearer {api_key}"))
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(request).await.unwrap();
    assert_eq!(response.status(), http::StatusCode::OK);

    let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .unwrap();
    let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
    assert!(body.is_array());
}

/// POST /v1/memory/search with valid read-only key → 200.
#[tokio::test]
async fn test_search_with_read_only_key() {
    let temp = tempfile::tempdir().unwrap();
    let mut config = brain::BrainConfig::default();
    config.brain.data_dir = temp.path().to_str().unwrap().to_string();
    config.access.api_keys.push(brain::ApiKeyConfig {
        key: "read-only-key".to_string(),
        name: "Read Only".to_string(),
        permissions: vec!["read".to_string()],
        agent_id: None,
    });
    let api_keys = config.access.api_keys.clone();
    let processor = signal::SignalProcessor::new(config).await.unwrap();
    let router = crate::create_router(
        Arc::new(processor),
        std::collections::HashMap::new(),
        api_keys,
        true,
    );

    let payload = serde_json::json!({"query": "Rust", "top_k": 5});
    let request = Request::builder()
        .method(http::Method::POST)
        .uri("/v1/memory/search")
        .header("content-type", "application/json")
        .header("authorization", "Bearer read-only-key")
        .body(Body::from(serde_json::to_string(&payload).unwrap()))
        .unwrap();

    let response = router.oneshot(request).await.unwrap();
    assert_eq!(response.status(), http::StatusCode::OK);
}

/// POST /v1/signals with read-only key → 401 (missing write permission).
#[tokio::test]
async fn test_post_signal_read_only_key_returns_401() {
    let temp = tempfile::tempdir().unwrap();
    let mut config = brain::BrainConfig::default();
    config.brain.data_dir = temp.path().to_str().unwrap().to_string();
    config.access.api_keys.push(brain::ApiKeyConfig {
        key: "read-only-key".to_string(),
        name: "Read Only".to_string(),
        permissions: vec!["read".to_string()],
        agent_id: None,
    });
    let api_keys = config.access.api_keys.clone();
    let processor = signal::SignalProcessor::new(config).await.unwrap();
    let router = crate::create_router(
        Arc::new(processor),
        std::collections::HashMap::new(),
        api_keys,
        true,
    );

    let payload = serde_json::json!({"content": "Remember something"});
    let request = Request::builder()
        .method(http::Method::POST)
        .uri("/v1/signals")
        .header("content-type", "application/json")
        .header("authorization", "Bearer read-only-key")
        .body(Body::from(serde_json::to_string(&payload).unwrap()))
        .unwrap();

    let response = router.oneshot(request).await.unwrap();
    assert_eq!(response.status(), http::StatusCode::UNAUTHORIZED);
}

/// Integration test: HTTP POST /v1/signals (store intent) → fact persisted in DB.
#[tokio::test]
async fn test_http_store_signal_fact_persisted_in_db() {
    let temp = tempfile::tempdir().unwrap();
    let mut config = brain::BrainConfig::default();
    config.brain.data_dir = temp.path().to_str().unwrap().to_string();
    let api_key = config.access.api_keys.first().unwrap().key.clone();
    let api_keys = config.access.api_keys.clone();
    let processor = Arc::new(signal::SignalProcessor::new(config).await.unwrap());
    let state = Arc::new(AppState {
        processor,
        webhook_handlers: std::collections::HashMap::new(),
        cache: Mutex::new(lru::LruCache::new(
            NonZeroUsize::new(CACHE_CAPACITY).unwrap(),
        )),
        api_keys,
        metrics: Arc::new(Metrics::default()),
    });

    let payload = serde_json::json!({"content": "Remember that Rust is fast"});
    let post_req = Request::builder()
        .method(http::Method::POST)
        .uri("/v1/signals")
        .header("content-type", "application/json")
        .header("authorization", format!("Bearer {api_key}"))
        .body(Body::from(serde_json::to_string(&payload).unwrap()))
        .unwrap();

    let router = Router::new()
        .route("/v1/signals", post(post_signal_handler))
        .route("/v1/memory/facts", get(get_facts_handler))
        .with_state(state.clone());

    let post_resp = router.clone().oneshot(post_req).await.unwrap();
    assert_eq!(post_resp.status(), http::StatusCode::OK);

    let bytes = axum::body::to_bytes(post_resp.into_body(), usize::MAX)
        .await
        .unwrap();
    let resp_json: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
    assert_eq!(resp_json["status"], "Ok");
    assert!(resp_json["memory_context"].is_object());

    let get_req = Request::builder()
        .method(http::Method::GET)
        .uri("/v1/memory/facts")
        .header("authorization", format!("Bearer {api_key}"))
        .body(Body::empty())
        .unwrap();

    let get_resp = router.oneshot(get_req).await.unwrap();
    assert_eq!(get_resp.status(), http::StatusCode::OK);

    let bytes = axum::body::to_bytes(get_resp.into_body(), usize::MAX)
        .await
        .unwrap();
    let facts: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
    assert!(facts.is_array(), "Expected array of facts");
    assert!(
        !facts.as_array().unwrap().is_empty(),
        "Stored fact should appear in GET /v1/memory/facts"
    );
}

/// Integration test: HTTP POST /v1/memory/search → returns relevant fact.
#[tokio::test]
async fn test_http_memory_search_returns_stored_fact() {
    let temp = tempfile::tempdir().unwrap();
    let mut config = brain::BrainConfig::default();
    config.brain.data_dir = temp.path().to_str().unwrap().to_string();
    let api_key = config.access.api_keys.first().unwrap().key.clone();
    let api_keys = config.access.api_keys.clone();
    let processor = Arc::new(signal::SignalProcessor::new(config).await.unwrap());

    let _ = processor
        .store_fact_direct("personal", "test", "Ferris", "is", "the Rust mascot", None)
        .await
        .unwrap();

    let state = Arc::new(AppState {
        processor,
        webhook_handlers: std::collections::HashMap::new(),
        cache: Mutex::new(lru::LruCache::new(
            NonZeroUsize::new(CACHE_CAPACITY).unwrap(),
        )),
        api_keys,
        metrics: Arc::new(Metrics::default()),
    });

    let router = Router::new()
        .route("/v1/memory/search", post(search_memory_handler))
        .with_state(state);

    let payload = serde_json::json!({"query": "Ferris Rust mascot", "top_k": 5});
    let request = Request::builder()
        .method(http::Method::POST)
        .uri("/v1/memory/search")
        .header("content-type", "application/json")
        .header("authorization", format!("Bearer {api_key}"))
        .body(Body::from(serde_json::to_string(&payload).unwrap()))
        .unwrap();

    let response = router.oneshot(request).await.unwrap();
    assert_eq!(response.status(), http::StatusCode::OK);

    let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .unwrap();
    let results: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
    assert!(results.is_array(), "Expected array of search results");
}

/// Integration test: cached signal can be retrieved by GET /v1/signals/:id.
#[tokio::test]
async fn test_get_cached_signal_with_auth() {
    let temp = tempfile::tempdir().unwrap();
    let mut config = brain::BrainConfig::default();
    config.brain.data_dir = temp.path().to_str().unwrap().to_string();
    let api_key = config.access.api_keys.first().unwrap().key.clone();
    let api_keys = config.access.api_keys.clone();
    let processor = Arc::new(signal::SignalProcessor::new(config).await.unwrap());
    let state = Arc::new(AppState {
        processor,
        webhook_handlers: std::collections::HashMap::new(),
        cache: Mutex::new(lru::LruCache::new(
            NonZeroUsize::new(CACHE_CAPACITY).unwrap(),
        )),
        api_keys,
        metrics: Arc::new(Metrics::default()),
    });

    let id = Uuid::new_v4();
    let fake_resp = signal::SignalResponse::ok(id, "test response");
    state.cache.lock().await.put(id, fake_resp);

    let router = Router::new()
        .route("/v1/signals/:id", get(get_signal_handler))
        .with_state(state);

    let request = Request::builder()
        .method(http::Method::GET)
        .uri(format!("/v1/signals/{id}"))
        .header("authorization", format!("Bearer {api_key}"))
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(request).await.unwrap();
    assert_eq!(response.status(), http::StatusCode::OK);
}