mnem-http 0.1.6

HTTP JSON API for mnem - REST surface over the core repo operations.
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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
//! Integration tests for `mnem http`.
//!
//! Uses `tower::ServiceExt::oneshot` to drive the router without
//! binding a TCP port. Every test runs against a fresh temp-dir repo.

use axum::body::Body;
use axum::http::{Request, StatusCode};
use http_body_util::BodyExt as _;
use serde_json::Value;
use tempfile::TempDir;
use tower::ServiceExt;

fn make_app() -> (axum::Router, TempDir) {
    // These tests assert on the caller-supplied `label` being
    // round-tripped (`"Memory"` rather than `Node::DEFAULT_NTYPE`). The
    // production default is to hide labels entirely, gated by
    // `MNEM_BENCH=1`. Tests opt in via the programmatic override so
    // they exercise the benchmark-path schema without touching the
    // process environment (unsafe under Rust 2024).
    let td = TempDir::new().expect("tmp dir");
    let opts = mnem_http::AppOptions {
        allow_labels: Some(true),
        in_memory: false,
        metrics_enabled: false,
    };
    let app = mnem_http::app_with_options(td.path(), opts).expect("build app");
    (app, td)
}

async fn to_json(body: Body) -> Value {
    let bytes = body.collect().await.expect("collect body").to_bytes();
    serde_json::from_slice(&bytes).expect("valid JSON")
}

#[tokio::test]
async fn healthz_returns_ok() {
    let (app, _td) = make_app();
    let resp = app
        .oneshot(
            Request::builder()
                .uri("/v1/healthz")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let j = to_json(resp.into_body()).await;
    assert_eq!(j["ok"], true);
    assert_eq!(j["schema"], "mnem.v1.healthz");
}

#[tokio::test]
async fn stats_returns_op_id_and_schema() {
    let (app, _td) = make_app();
    let resp = app
        .oneshot(
            Request::builder()
                .uri("/v1/stats")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let j = to_json(resp.into_body()).await;
    assert_eq!(j["schema"], "mnem.v1.stats");
    assert!(j["op_id"].as_str().unwrap().starts_with("bafyrei"));
}

#[tokio::test]
async fn post_node_then_get_then_retrieve() {
    let (app, _td) = make_app();

    // POST /v1/nodes
    let body = serde_json::json!({
        "label": "Memory",
        "summary": "Alice lives in Berlin",
        "author": "tests"
    });
    let resp = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/nodes")
                .header("content-type", "application/json")
                .body(Body::from(serde_json::to_vec(&body).unwrap()))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK, "post_node");
    let j = to_json(resp.into_body()).await;
    let id = j["id"].as_str().unwrap().to_string();
    assert_eq!(j["label"], "Memory");

    // GET /v1/nodes/{id}
    let resp = app
        .clone()
        .oneshot(
            Request::builder()
                .uri(format!("/v1/nodes/{id}"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK, "get_node");
    let j = to_json(resp.into_body()).await;
    assert_eq!(j["summary"], "Alice lives in Berlin");
    assert_eq!(j["label"], "Memory");

    // GET /v1/retrieve. No embedder is configured in this test repo,
    // so the only usable filter is label-based (the legacy BM25 text
    // lane was removed in ; text queries without an embedder
    // + sparse provider now return empty, not a stopword-matched
    // fallback). Label-filter retrieval still exercises the full
    // render + token-budget path.
    let resp = app
        .clone()
        .oneshot(
            Request::builder()
                .uri("/v1/retrieve?label=Memory&budget=200")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK, "retrieve");
    let j = to_json(resp.into_body()).await;
    assert_eq!(j["schema"], "mnem.v1.retrieve");
    assert!(j["items"].as_array().unwrap().len() >= 1);
    assert_eq!(j["items"][0]["summary"], "Alice lives in Berlin");
    assert_eq!(j["tokens_budget"], 200);
}

#[tokio::test]
async fn delete_node_round_trip() {
    let (app, _td) = make_app();
    // Create
    let body = serde_json::json!({
        "label": "Memory",
        "summary": "scratch",
        "author": "tests"
    });
    let resp = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/nodes")
                .header("content-type", "application/json")
                .body(Body::from(serde_json::to_vec(&body).unwrap()))
                .unwrap(),
        )
        .await
        .unwrap();
    let id = to_json(resp.into_body()).await["id"]
        .as_str()
        .unwrap()
        .to_string();

    // Delete
    let resp = app
        .clone()
        .oneshot(
            Request::builder()
                .method("DELETE")
                .uri(format!("/v1/nodes/{id}?author=tests"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let j = to_json(resp.into_body()).await;
    assert_eq!(j["existed"], true);

    // Get after delete is 404
    let resp = app
        .oneshot(
            Request::builder()
                .uri(format!("/v1/nodes/{id}"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn bad_uuid_is_400_not_500() {
    let (app, _td) = make_app();
    let resp = app
        .oneshot(
            Request::builder()
                .uri("/v1/nodes/not-a-uuid")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
    let j = to_json(resp.into_body()).await;
    assert_eq!(j["schema"], "mnem.v1.err");
    assert!(j["error"].as_str().unwrap().contains("invalid UUID"));
}

#[tokio::test]
async fn empty_label_falls_back_to_default_ntype() {
    // Previously this asserted a 400 on empty label, but the
    // `MNEM_BENCH`-gated rework silently substitutes
    // `Node::DEFAULT_NTYPE` whenever the caller-supplied label is empty
    // (same as the unset case). Post still succeeds; the stored label
    // is "Node". This test pins that behaviour so a future change to
    // either the gate or the fallback is a deliberate decision.
    let (app, _td) = make_app();
    let body = serde_json::json!({
        "label": "",
        "summary": "x",
        "author": "tests"
    });
    let resp = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/nodes")
                .header("content-type", "application/json")
                .body(Body::from(serde_json::to_vec(&body).unwrap()))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let j = to_json(resp.into_body()).await;
    // Fetch the stored node and confirm the server substituted the
    // default ntype. The POST response echoes the caller's empty input
    // rather than the server-side label, so we have to round-trip.
    let id = j["id"].as_str().unwrap().to_string();
    let resp = app
        .oneshot(
            Request::builder()
                .uri(format!("/v1/nodes/{id}"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let j = to_json(resp.into_body()).await;
    assert_eq!(j["label"], "Node");
}

#[tokio::test]
async fn tombstone_node_round_trip_returns_schema_and_op_id() {
    // Happy path: POST /v1/nodes creates a node, POST
    // /v1/nodes/{id}/tombstone returns 200 with the expected schema
    // envelope. A subsequent GET still resolves the node (logical
    // tombstone, not physical delete); the node just drops out of
    // retrieve (covered in mnem-core integration tests).
    let (app, _td) = make_app();
    let body = serde_json::json!({
        "label": "Memory",
        "summary": "Alice likes jazz",
        "author": "tests"
    });
    let resp = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/nodes")
                .header("content-type", "application/json")
                .body(Body::from(serde_json::to_vec(&body).unwrap()))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let id = to_json(resp.into_body()).await["id"]
        .as_str()
        .unwrap()
        .to_string();

    let body = serde_json::json!({
        "reason": "user asked to forget",
        "author": "tests"
    });
    let resp = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/v1/nodes/{id}/tombstone"))
                .header("content-type", "application/json")
                .body(Body::from(serde_json::to_vec(&body).unwrap()))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK, "tombstone call ok");
    let j = to_json(resp.into_body()).await;
    assert_eq!(j["schema"], "mnem.v1.tombstone");
    assert_eq!(j["node_id"], id);
    assert!(j["op_id"].as_str().is_some());

    // Node itself still resolves: tombstone is logical.
    let resp = app
        .oneshot(
            Request::builder()
                .uri(format!("/v1/nodes/{id}"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
}

#[tokio::test]
async fn tombstone_returns_404_for_missing_and_409_for_already_tombstoned() {
    // Negative paths for POST /v1/nodes/{id}/tombstone: 404 if the
    // node never existed, 409 if it is already tombstoned.
    let (app, _td) = make_app();

    // 404: random well-formed UUID that never existed.
    let fake_id = "00000000-0000-0000-0000-000000000001";
    let resp = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/v1/nodes/{fake_id}/tombstone"))
                .header("content-type", "application/json")
                .body(Body::from(
                    serde_json::to_vec(&serde_json::json!({
                        "reason": "r",
                        "author": "tests"
                    }))
                    .unwrap(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(
        resp.status(),
        StatusCode::NOT_FOUND,
        "missing node must be 404"
    );

    // Create, tombstone, then re-tombstone -> 409.
    let body = serde_json::json!({
        "label": "Memory",
        "summary": "ephemeral",
        "author": "tests"
    });
    let resp = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/nodes")
                .header("content-type", "application/json")
                .body(Body::from(serde_json::to_vec(&body).unwrap()))
                .unwrap(),
        )
        .await
        .unwrap();
    let id = to_json(resp.into_body()).await["id"]
        .as_str()
        .unwrap()
        .to_string();
    let ts_body = serde_json::json!({ "reason": "first", "author": "tests" });
    let resp = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/v1/nodes/{id}/tombstone"))
                .header("content-type", "application/json")
                .body(Body::from(serde_json::to_vec(&ts_body).unwrap()))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK, "first tombstone ok");

    // Second call: 409.
    let resp = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/v1/nodes/{id}/tombstone"))
                .header("content-type", "application/json")
                .body(Body::from(
                    serde_json::to_vec(&serde_json::json!({
                        "reason": "second",
                        "author": "tests"
                    }))
                    .unwrap(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(
        resp.status(),
        StatusCode::CONFLICT,
        "double tombstone must be 409"
    );
}

#[tokio::test]
async fn retrieve_with_no_filters_errors() {
    let (app, _td) = make_app();
    let resp = app
        .oneshot(
            Request::builder()
                .uri("/v1/retrieve")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    // No filters + no rankers -> core returns an error; http surface
    // should bubble as 500 (or a 400 once we classify more tightly).
    assert!(
        resp.status() == StatusCode::BAD_REQUEST
            || resp.status() == StatusCode::INTERNAL_SERVER_ERROR,
        "unexpected status: {}",
        resp.status()
    );
}

// ---------- input clamps (R2-A security hardening) ----------
//
// Every numeric knob the retrieve path exposes has a boundary cap.
// The goal is not to impose product shape, just to prevent an
// accidental or adversarial `u64::MAX` from triggering a downstream
// allocator blow-up. The failure mode is a 400 with a specific
// message that names the offending knob.

#[tokio::test]
async fn retrieve_get_rejects_oversized_limit() {
    let (app, _td) = make_app();
    let resp = app
        .oneshot(
            Request::builder()
                .uri("/v1/retrieve?limit=99999999")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
    let j = to_json(resp.into_body()).await;
    let err = j["error"].as_str().unwrap();
    assert!(
        err.contains("limit=99999999"),
        "error must name the rejected knob + value: {err}"
    );
    assert!(
        err.contains("max of"),
        "error must state the ceiling: {err}"
    );
}

#[tokio::test]
async fn retrieve_post_rejects_oversized_vector_cap() {
    let (app, _td) = make_app();
    let body = serde_json::json!({
        "vector_cap": 9_999_999_u64
    });
    let resp = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/retrieve")
                .header("content-type", "application/json")
                .body(Body::from(serde_json::to_vec(&body).unwrap()))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
    let j = to_json(resp.into_body()).await;
    let err = j["error"].as_str().unwrap();
    assert!(
        err.contains("vector_cap"),
        "error must name the knob: {err}"
    );
}

#[tokio::test]
async fn retrieve_post_rejects_oversized_rerank_top_k() {
    let (app, _td) = make_app();
    let body = serde_json::json!({
        "rerank_top_k": 10_000_u64
    });
    let resp = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/retrieve")
                .header("content-type", "application/json")
                .body(Body::from(serde_json::to_vec(&body).unwrap()))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
    let j = to_json(resp.into_body()).await;
    let err = j["error"].as_str().unwrap();
    assert!(
        err.contains("rerank_top_k"),
        "error must name the knob: {err}"
    );
}

#[tokio::test]
async fn retrieve_post_accepts_at_limit() {
    // Exactly at the cap is allowed; only strictly-greater is rejected.
    // Body sends no retrieval signal so the core rejects with 400 or
    // 500, NOT because of our clamp - that's the property this test
    // defends.
    let (app, _td) = make_app();
    let body = serde_json::json!({
        "limit": 1000,
        "vector_cap": 100_000,
        "rerank_top_k": 500
    });
    let resp = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/retrieve")
                .header("content-type", "application/json")
                .body(Body::from(serde_json::to_vec(&body).unwrap()))
                .unwrap(),
        )
        .await
        .unwrap();
    let status = resp.status();
    if status == StatusCode::BAD_REQUEST {
        // Must NOT be the clamp error - check the message doesn't
        // name any of our three knobs.
        let j = to_json(resp.into_body()).await;
        let err = j["error"].as_str().unwrap_or("");
        assert!(
            !(err.contains("limit=1000 exceeds")
                || err.contains("vector_cap=100000 exceeds")
                || err.contains("rerank_top_k=500 exceeds")),
            "at-cap values must not trip the clamp: {err}"
        );
    }
    // Any non-clamp outcome is fine; we only care about the clamp
    // not firing at the exact ceiling.
}

// ---------- correlation-id (R3-B) ----------

#[tokio::test]
async fn response_carries_minted_correlation_id() {
    // No header on the request -> middleware mints a UUIDv7, echoes
    // it in `X-Request-Id`. Asserts the canonical hex-with-hyphens
    // shape so downstream log-parsing tools can rely on it.
    let (app, _td) = make_app();
    let resp = app
        .oneshot(
            Request::builder()
                .uri("/v1/healthz")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let id = resp
        .headers()
        .get("x-request-id")
        .expect("x-request-id echoed on every response")
        .to_str()
        .expect("ascii header")
        .to_string();
    assert_eq!(
        id.len(),
        36,
        "minted UUIDv7 has 36 chars (32 hex + 4 hyphens), got {id}"
    );
    assert_eq!(id.matches('-').count(), 4, "UUIDv7 has 4 hyphens, got {id}");
}

#[tokio::test]
async fn response_reuses_caller_supplied_correlation_id() {
    // Caller-supplied id in the acceptable window -> echoed back
    // verbatim. Lets upstream gateways thread one id end-to-end.
    let (app, _td) = make_app();
    let caller = "req-test-correlation-0001";
    let resp = app
        .oneshot(
            Request::builder()
                .uri("/v1/healthz")
                .header("x-request-id", caller)
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    assert_eq!(
        resp.headers()
            .get("x-request-id")
            .and_then(|v| v.to_str().ok()),
        Some(caller),
        "caller-supplied correlation id must round-trip"
    );
}

// ============================================================
// POST /v1/ingest (Phase-B5d)
// ============================================================

/// Build a minimal multipart body by hand. Avoids pulling in a
/// multipart-writer crate just for two tests; the bytes below are the
/// literal RFC 7578 shape axum's `Multipart` extractor accepts.
fn multipart_body(
    boundary: &str,
    file_name: &str,
    file_bytes: &[u8],
    fields: &[(&str, &str)],
) -> Vec<u8> {
    let mut out = Vec::new();
    for (k, v) in fields {
        out.extend_from_slice(format!("--{boundary}\r\n").as_bytes());
        out.extend_from_slice(
            format!("Content-Disposition: form-data; name=\"{k}\"\r\n\r\n{v}\r\n").as_bytes(),
        );
    }
    out.extend_from_slice(format!("--{boundary}\r\n").as_bytes());
    out.extend_from_slice(
        format!(
            "Content-Disposition: form-data; name=\"file\"; filename=\"{file_name}\"\r\n\
             Content-Type: application/octet-stream\r\n\r\n"
        )
        .as_bytes(),
    );
    out.extend_from_slice(file_bytes);
    out.extend_from_slice(b"\r\n");
    out.extend_from_slice(format!("--{boundary}--\r\n").as_bytes());
    out
}

#[tokio::test]
async fn ingest_multipart_markdown_commits_subgraph() {
    let (app, _td) = make_app();
    let boundary = "----mnemTestBoundary";
    let body = multipart_body(
        boundary,
        "hello.md",
        b"# Title\n\nAlice Johnson met Bob Lee at Acme Corp on 2026-04-24.\n",
        &[
            ("author", "http-ingest-test"),
            ("message", "ingest roundtrip"),
        ],
    );
    let resp = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/ingest")
                .header(
                    "content-type",
                    format!("multipart/form-data; boundary={boundary}"),
                )
                .body(Body::from(body))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let j = to_json(resp.into_body()).await;
    assert_eq!(j["schema"], "mnem.v1.ingest");
    assert!(j["chunk_count"].as_u64().unwrap_or(0) >= 1);
    assert!(j["node_count"].as_u64().unwrap_or(0) >= 2);
    assert!(j["commit_cid"].is_string());
}

#[tokio::test]
async fn ingest_json_body_text_kind_commits_subgraph() {
    let (app, _td) = make_app();
    let body = serde_json::json!({
        "text": "Alice Johnson joined Acme Corp on 2026-04-24.",
        "kind": "text",
        "author": "json-ingest-test"
    });
    let resp = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/ingest")
                .header("content-type", "application/json")
                .body(Body::from(body.to_string()))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let j = to_json(resp.into_body()).await;
    assert_eq!(j["schema"], "mnem.v1.ingest");
    assert!(j["chunk_count"].as_u64().unwrap_or(0) >= 1);
}

#[tokio::test]
async fn ingest_json_body_missing_author_is_bad_request() {
    let (app, _td) = make_app();
    let body = serde_json::json!({
        "text": "no author on this one",
        "kind": "text"
    });
    let resp = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/ingest")
                .header("content-type", "application/json")
                .body(Body::from(body.to_string()))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn ingest_json_body_max_tokens_clamp_is_bad_request() {
    // `max_tokens` over 8192 must 400 with a clear message; this
    // mirrors the CLI + MCP guardrails documented in B5d.
    let (app, _td) = make_app();
    let body = serde_json::json!({
        "text": "irrelevant",
        "kind": "text",
        "author": "clamp-test",
        "max_tokens": 999_999
    });
    let resp = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/ingest")
                .header("content-type", "application/json")
                .body(Body::from(body.to_string()))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
    let j = to_json(resp.into_body()).await;
    let err = j["error"].as_str().unwrap_or_default();
    assert!(err.contains("8192"), "expected clamp message, got {err:?}");
}