recall-server 0.4.13

Recall's sync server: SQLite persistence, LLM-assisted merge, and the HTTP API
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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
//! Ported from internal/server/server_test.go — the behaviors here are the
//! frozen ones: auth posture, tombstone semantics, project isolation, the
//! merge fallback, and rate limiting.

use std::sync::Arc;
use std::time::Duration;

use axum::body::{Body, Bytes};
use axum::http::{HeaderMap, Request, StatusCode};
use recall_server::merge::Status;
use recall_server::{now, Config, Server, Store};
use recall_wire::{AdminStats, Health, PushRequest, PushResponse, SyncResponse};
use tempfile::TempDir;
use tower::ServiceExt;

const TEST_TOKEN: &str = "test-token";

struct Harness {
    server: Server,
    store: Arc<Store>,
    dir: TempDir,
}

fn harness(tweak: impl FnOnce(&mut Config)) -> Harness {
    let dir = tempfile::tempdir().unwrap();
    let store = Arc::new(Store::open(dir.path().join("recall.db")).unwrap());
    let mut cfg = Config {
        token: TEST_TOKEN.to_string(),
        git_commit: "testcommit".to_string(),
        rate_limit_window: Duration::from_secs(60),
        rate_limit_max: 1000,
        merge_enabled: false,
        claude_bin: "definitely-not-a-real-binary".to_string(),
        ..Config::default()
    };
    tweak(&mut cfg);
    let server = Server::new(cfg, store.clone());
    Harness { server, store, dir }
}

impl Harness {
    async fn send(&self, req: Request<Body>) -> (StatusCode, HeaderMap, Bytes) {
        let resp = self.server.router().oneshot(req).await.unwrap();
        let status = resp.status();
        let headers = resp.headers().clone();
        let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
            .await
            .unwrap();
        (status, headers, body)
    }

    async fn get(&self, token: Option<&str>, path: &str) -> (StatusCode, HeaderMap, Bytes) {
        let mut req = Request::builder().method("GET").uri(path);
        if let Some(t) = token {
            req = req.header("authorization", format!("Bearer {t}"));
        }
        self.send(req.body(Body::empty()).unwrap()).await
    }

    async fn push(&self, token: &str, body: &PushRequest) -> (StatusCode, Bytes) {
        let req = Request::builder()
            .method("POST")
            .uri("/sync")
            .header("authorization", format!("Bearer {token}"))
            .header("content-type", "application/json")
            .body(Body::from(serde_json::to_vec(body).unwrap()))
            .unwrap();
        let (status, _, bytes) = self.send(req).await;
        (status, bytes)
    }

    async fn ok_push(&self, project_key: &str, file_path: &str, content: &str, source_env: &str) {
        let (status, _) = self
            .push(
                TEST_TOKEN,
                &PushRequest {
                    project_key: project_key.into(),
                    file_path: file_path.into(),
                    content: Some(content.into()),
                    source_env: source_env.into(),
                    deleted: false,
                    base_sha256: None,
                },
            )
            .await;
        assert_eq!(status, StatusCode::OK, "push failed");
    }

    async fn ok_delete(&self, project_key: &str, file_path: &str, source_env: &str) {
        let (status, _) = self
            .push(
                TEST_TOKEN,
                &PushRequest {
                    project_key: project_key.into(),
                    file_path: file_path.into(),
                    source_env: source_env.into(),
                    deleted: true,
                    ..Default::default()
                },
            )
            .await;
        assert_eq!(status, StatusCode::OK, "delete failed");
    }

    async fn pull(&self, project_key: &str) -> (SyncResponse, Bytes) {
        let (status, _, body) = self
            .get(
                Some(TEST_TOKEN),
                &format!("/sync?project_key={project_key}"),
            )
            .await;
        assert_eq!(status, StatusCode::OK);
        (serde_json::from_slice(&body).unwrap(), body)
    }
}

#[tokio::test]
async fn auth_rejects_missing_and_wrong_tokens() {
    let h = harness(|_| {});

    let (status, _, _) = h.get(None, "/sync?project_key=acme/app").await;
    assert_eq!(status, StatusCode::UNAUTHORIZED, "no token");

    let (status, _, _) = h
        .get(Some("wrong-token"), "/sync?project_key=acme/app")
        .await;
    assert_eq!(status, StatusCode::UNAUTHORIZED, "wrong token");

    let (status, _, _) = h.get(Some(TEST_TOKEN), "/sync?project_key=acme/app").await;
    assert_eq!(status, StatusCode::OK, "valid token");
}

/// `/health` has to stay pollable by uptime tooling that holds no secret.
#[tokio::test]
async fn health_needs_no_token() {
    let h = harness(|_| {});
    let (status, _, body) = h.get(None, "/health").await;
    assert_eq!(status, StatusCode::OK);
    let health: Health = serde_json::from_slice(&body).unwrap();
    assert_eq!(health.status, "ok");
    assert_eq!(health.git_commit, "testcommit");
    assert!(!health.started_at.is_empty());
}

#[tokio::test]
async fn push_then_pull_round_trip() {
    let h = harness(|_| {});
    let content = "# Memory\n- a fact\n";
    h.ok_push("acme/app", "MEMORY.md", content, "laptop").await;

    let (out, _) = h.pull("acme/app").await;
    assert_eq!(out.project_key, "acme/app");
    assert_eq!(out.files.len(), 1);
    assert_eq!(out.files[0].content.as_deref(), Some(content));
    assert_eq!(out.files[0].source_env, "laptop");
    assert!(!out.files[0].updated_at.is_empty());
}

#[tokio::test]
async fn rejects_traversal_and_absolute_paths() {
    let h = harness(|_| {});
    for bad in ["../escape.md", "/etc/passwd", "a/../../b.md"] {
        let (status, _) = h
            .push(
                TEST_TOKEN,
                &PushRequest {
                    project_key: "acme/app".into(),
                    file_path: bad.into(),
                    content: Some("x".into()),
                    ..Default::default()
                },
            )
            .await;
        assert_eq!(status, StatusCode::BAD_REQUEST, "file_path {bad:?}");
    }
}

/// A tombstone keeps its content in the database for recovery, but must not
/// hand it back — otherwise a pull would resurrect a deleted file.
#[tokio::test]
async fn tombstone_withholds_content_but_the_database_keeps_it() {
    let h = harness(|_| {});
    let secret = "# Secret\n- do not resurrect\n";
    h.ok_push("acme/app", "gone.md", secret, "laptop").await;
    h.ok_delete("acme/app", "gone.md", "laptop").await;

    let (out, raw) = h.pull("acme/app").await;
    assert_eq!(out.files.len(), 1);
    assert!(out.files[0].deleted, "file not reported as deleted");
    assert_eq!(out.files[0].content, None, "tombstoned content was served");
    assert!(
        !String::from_utf8_lossy(&raw).contains("do not resurrect"),
        "tombstoned content leaked into the response body"
    );

    // Still recoverable at the database level, which is the point of a
    // tombstone rather than a delete.
    let stored = h.store.get("acme/app", "gone.md").unwrap().unwrap();
    assert_eq!(stored.content, secret, "content was not preserved");
    assert!(stored.deleted);
}

#[tokio::test]
async fn projects_are_isolated() {
    let h = harness(|_| {});
    h.ok_push("acme/website", "MEMORY.md", "website", "laptop")
        .await;
    h.ok_push("acme/backend", "MEMORY.md", "backend", "laptop")
        .await;
    h.ok_delete("acme/website", "MEMORY.md", "laptop").await;

    let (out, _) = h.pull("acme/backend").await;
    assert_eq!(out.files.len(), 1);
    assert!(
        !out.files[0].deleted,
        "deleting in one project affected another"
    );
    assert_eq!(out.files[0].content.as_deref(), Some("backend"));

    let (gone, _) = h.pull("acme/website").await;
    assert!(gone.files[0].deleted);
}

/// With merge enabled and the CLI reporting logged in, a broken binary must
/// still leave the push accepted: every merge failure degrades to
/// last-write-wins and still returns 200.
#[tokio::test]
async fn merge_failure_falls_back_to_last_write_wins() {
    let h = harness(|c| c.merge_enabled = true);
    h.server.set_claude_status(Status {
        checked_at: now(),
        available: true,
        logged_in: true,
        error: String::new(),
    });

    h.ok_push("acme/app", "MEMORY.md", "version A", "laptop")
        .await;
    let (status, body) = h
        .push(
            TEST_TOKEN,
            &PushRequest {
                project_key: "acme/app".into(),
                file_path: "MEMORY.md".into(),
                content: Some("version B".into()),
                ..Default::default()
            },
        )
        .await;
    assert_eq!(status, StatusCode::OK, "conflicting push rejected");
    let pr: PushResponse = serde_json::from_slice(&body).unwrap();
    assert!(!pr.merged, "reported a merge without a working claude CLI");

    let (out, _) = h.pull("acme/app").await;
    assert_eq!(
        out.files[0].content.as_deref(),
        Some("version B"),
        "want last-write-wins"
    );

    // The failure is visible on /health rather than swallowed.
    let (_, _, health) = h.get(None, "/health").await;
    let health: Health = serde_json::from_slice(&health).unwrap();
    let err = health.merge.last_merge_error.expect("last_merge_error");
    assert!(!err.message.is_empty() && !err.at.is_empty());
}

/// No merge is even attempted while the CLI is known logged out: every
/// attempt would burn a subprocess and a timeout before failing to the same
/// place.
#[tokio::test]
async fn merge_is_not_attempted_when_the_cli_is_logged_out() {
    let h = harness(|c| c.merge_enabled = true);
    h.ok_push("acme/app", "MEMORY.md", "version A", "laptop")
        .await;
    h.ok_push("acme/app", "MEMORY.md", "version B", "laptop")
        .await;

    let (_, _, body) = h.get(None, "/health").await;
    let health: Health = serde_json::from_slice(&body).unwrap();
    assert!(
        health.merge.last_merge_error.is_none(),
        "a merge was attempted with no usable CLI"
    );
    let (out, _) = h.pull("acme/app").await;
    assert_eq!(out.files[0].content.as_deref(), Some("version B"));
}

/// The whole merge path, with a stand-in CLI: a genuine conflict is
/// reconciled and the merged text is what gets stored.
#[tokio::test]
async fn a_real_conflict_is_merged_end_to_end() {
    let h = harness(|_| {});
    let bin = fake_claude(h.dir.path(), r#"{"is_error":false,"result":"A and B"}"#);
    let h = Harness {
        server: Server::new(
            Config {
                token: TEST_TOKEN.to_string(),
                merge_enabled: true,
                claude_bin: bin,
                merge_timeout: Duration::from_secs(20),
                rate_limit_max: 1000,
                ..Config::default()
            },
            h.store.clone(),
        ),
        ..h
    };
    h.server.set_claude_status(Status {
        checked_at: now(),
        available: true,
        logged_in: true,
        error: String::new(),
    });

    h.ok_push("acme/app", "MEMORY.md", "A", "laptop").await;
    let (status, body) = h
        .push(
            TEST_TOKEN,
            &PushRequest {
                project_key: "acme/app".into(),
                file_path: "MEMORY.md".into(),
                content: Some("B".into()),
                ..Default::default()
            },
        )
        .await;
    assert_eq!(status, StatusCode::OK);
    let pr: PushResponse = serde_json::from_slice(&body).unwrap();
    assert!(pr.merged, "push was not reported as merged");

    let (out, _) = h.pull("acme/app").await;
    assert_eq!(out.files[0].content.as_deref(), Some("A and B"));

    // A re-push of the identical content is not a conflict, so the CLI must
    // not be consulted again.
    let (_, body) = h
        .push(
            TEST_TOKEN,
            &PushRequest {
                project_key: "acme/app".into(),
                file_path: "MEMORY.md".into(),
                content: Some("A and B".into()),
                ..Default::default()
            },
        )
        .await;
    let pr: PushResponse = serde_json::from_slice(&body).unwrap();
    assert!(!pr.merged, "an unchanged re-push must skip the merge");
}

/// The distinction the base exists for. The next edit replaces what it
/// started from — deletions included, which a merge cannot express, since it
/// keeps every distinct fact from both versions. Only an edit made against
/// something other than what is stored is a concurrent one, and only that is
/// merged. A client that sends no base keeps the old behaviour.
#[tokio::test]
async fn only_a_concurrent_edit_is_merged() {
    let h = harness(|_| {});
    let bin = fake_claude(h.dir.path(), r#"{"is_error":false,"result":"MERGED"}"#);
    let h = Harness {
        server: Server::new(
            Config {
                token: TEST_TOKEN.to_string(),
                merge_enabled: true,
                claude_bin: bin,
                merge_timeout: Duration::from_secs(20),
                rate_limit_max: 1000,
                ..Config::default()
            },
            h.store.clone(),
        ),
        ..h
    };
    h.server.set_claude_status(Status {
        checked_at: now(),
        available: true,
        logged_in: true,
        error: String::new(),
    });

    let push = |content: &str, base: Option<String>| {
        let req = PushRequest {
            project_key: "acme/app".into(),
            file_path: "MEMORY.md".into(),
            content: Some(content.into()),
            base_sha256: base,
            ..Default::default()
        };
        let h = &h;
        async move {
            let (status, body) = h.push(TEST_TOKEN, &req).await;
            assert_eq!(status, StatusCode::OK);
            serde_json::from_slice::<PushResponse>(&body)
                .unwrap()
                .merged
        }
    };
    let stored = || async { h.pull("acme/app").await.0.files[0].content.clone().unwrap() };

    let a = "keep
drop this line
";
    assert!(!push(a, None).await, "a new file has nothing to merge with");

    // The next edit, made from what is stored: it replaces it outright, and
    // the deleted line stays deleted.
    let b = "keep
";
    assert!(
        !push(b, Some(recall_wire::content_sha256(a))).await,
        "the next edit was merged — the deleted line would come back"
    );
    assert_eq!(stored().await, b);

    // An edit from a version that is no longer stored: someone else wrote in
    // between, and that is exactly what the merge is for.
    assert!(
        push(
            "keep
other
",
            Some(recall_wire::content_sha256(a))
        )
        .await,
        "a concurrent edit was not merged"
    );
    assert_eq!(stored().await, "MERGED");

    // No base: an older client, treated as it always was.
    assert!(
        push(
            "anything
",
            None
        )
        .await,
        "a push with no base must still merge"
    );

    // Hex is hex; a client that uppercases it is still naming the same base.
    assert!(
        !push(
            "final
",
            Some(recall_wire::content_sha256("MERGED").to_uppercase())
        )
        .await
    );
    assert_eq!(stored().await, "final\n");
}

#[tokio::test]
async fn rate_limit_triggers_and_health_is_exempt() {
    let h = harness(|c| c.rate_limit_max = 3);

    let mut last = StatusCode::OK;
    for _ in 0..5 {
        let (status, _, _) = h.get(Some(TEST_TOKEN), "/sync?project_key=acme/app").await;
        last = status;
    }
    assert_eq!(last, StatusCode::TOO_MANY_REQUESTS);

    // /health is deliberately exempt so uptime polling can't be locked out.
    let (status, _, _) = h.get(None, "/health").await;
    assert_eq!(status, StatusCode::OK, "/health while rate limited");
}

/// Invalid tokens must be counted too, or a flood of them escapes the
/// limiter by never reaching the auth check.
#[tokio::test]
async fn rate_limit_counts_unauthorized_requests() {
    let h = harness(|c| c.rate_limit_max = 3);

    let mut last = StatusCode::OK;
    let mut retry_after = None;
    for _ in 0..5 {
        let (status, headers, _) = h.get(Some("bad-token"), "/sync?project_key=acme/app").await;
        last = status;
        retry_after = headers
            .get("retry-after")
            .map(|v| v.to_str().unwrap().to_string());
    }
    assert_eq!(
        last,
        StatusCode::TOO_MANY_REQUESTS,
        "bad tokens must be rate limited too"
    );
    assert_eq!(retry_after.as_deref(), Some("60"));
}

#[tokio::test]
async fn admin_page_serves_without_leaking_data() {
    let h = harness(|_| {});
    h.ok_push("acme/app", "MEMORY.md", "sensitive note", "laptop")
        .await;

    let (status, headers, body) = h.get(None, "/admin").await;
    assert_eq!(status, StatusCode::OK);
    assert!(
        !String::from_utf8_lossy(&body).contains("sensitive note"),
        "admin page embedded memory content server-side"
    );
    assert!(
        headers.contains_key("content-security-policy"),
        "admin page served without a CSP"
    );
    assert_eq!(headers.get("x-content-type-options").unwrap(), "nosniff");

    let (status, _, _) = h.get(None, "/admin/stats").await;
    assert_eq!(
        status,
        StatusCode::UNAUTHORIZED,
        "/admin/stats without a token"
    );
}

#[tokio::test]
async fn admin_stats_totals() {
    let h = harness(|_| {});
    h.ok_push("acme/app", "MEMORY.md", "a", "laptop").await;
    h.ok_push("acme/app", "gone.md", "b", "cloud").await;
    h.ok_delete("acme/app", "gone.md", "cloud").await;

    let (status, _, body) = h.get(Some(TEST_TOKEN), "/admin/stats").await;
    assert_eq!(status, StatusCode::OK);
    let stats: AdminStats = serde_json::from_slice(&body).unwrap();
    assert_eq!(stats.totals.project_count, 1);
    assert_eq!(stats.totals.file_count, 1);
    assert_eq!(stats.totals.deleted_count, 1);
    assert_eq!(stats.projects.len(), 1);
    assert_eq!(stats.projects[0].sources, vec!["cloud", "laptop"]);
    assert_eq!(stats.git_commit, "testcommit");
}

#[tokio::test]
async fn backup_produces_a_restorable_snapshot() {
    let h = harness(|_| {});
    h.ok_push("acme/app", "MEMORY.md", "backed up", "laptop")
        .await;

    let backups = tempfile::tempdir().unwrap();
    let dest = h.store.backup(backups.path(), 7).unwrap();

    // The snapshot must be a usable database, not just bytes on disk.
    let restored = Store::open(&dest).unwrap();
    let files = restored.list("acme/app").unwrap();
    assert_eq!(files.len(), 1);
    assert_eq!(files[0].content.as_deref(), Some("backed up"));
}

#[tokio::test]
async fn backup_prunes_old_snapshots() {
    let h = harness(|_| {});
    let backups = tempfile::tempdir().unwrap();
    for _ in 0..5 {
        h.store.backup(backups.path(), 2).unwrap();
        // Distinct millisecond stamps: same-millisecond names would collide
        // and VACUUM INTO refuses to overwrite.
        std::thread::sleep(Duration::from_millis(2));
    }
    let kept: Vec<_> = std::fs::read_dir(backups.path())
        .unwrap()
        .filter_map(|e| e.ok())
        .map(|e| e.file_name().to_string_lossy().to_string())
        .filter(|n| n.starts_with("recall-") && n.ends_with(".db"))
        .collect();
    assert_eq!(kept.len(), 2, "kept {kept:?}");
}

#[tokio::test]
async fn unknown_routes_and_methods_are_404_json() {
    let h = harness(|_| {});

    let (status, _, body) = h.get(None, "/nope").await;
    assert_eq!(status, StatusCode::NOT_FOUND);
    assert_eq!(
        serde_json::from_slice::<serde_json::Value>(&body).unwrap()["error"],
        "not found"
    );

    let req = Request::builder()
        .method("PUT")
        .uri("/sync")
        .header("authorization", format!("Bearer {TEST_TOKEN}"))
        .body(Body::empty())
        .unwrap();
    let (status, _, _) = h.send(req).await;
    assert_eq!(status, StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn malformed_pushes_are_400() {
    let h = harness(|_| {});

    let bad = |body: &'static str| {
        Request::builder()
            .method("POST")
            .uri("/sync")
            .header("authorization", format!("Bearer {TEST_TOKEN}"))
            .header("content-type", "application/json")
            .body(Body::from(body))
            .unwrap()
    };
    for body in ["not json", "{}", r#"{"project_key":"acme/app"}"#] {
        let (status, _, _) = h.send(bad(body)).await;
        assert_eq!(status, StatusCode::BAD_REQUEST, "body {body:?}");
    }

    let (status, _, _) = h.get(Some(TEST_TOKEN), "/sync").await;
    assert_eq!(status, StatusCode::BAD_REQUEST, "pull without project_key");
}

/// And leaves the WAL empty behind it, the start leaf it wrote copied into
/// `recall.db`, even with another process holding the file open, as
/// sqlite-web does: closing the server's connection then does not empty
/// the WAL, so only the server's own checkpoint as it stops can.
#[tokio::test]
async fn serve_shuts_down_cleanly() {
    let dir = tempfile::tempdir().unwrap();
    let db = dir.path().join("recall.db");
    let store = Arc::new(Store::open(&db).unwrap());
    let reader = rusqlite::Connection::open(&db).unwrap();
    let _: i64 = reader
        .query_row("SELECT count(*) FROM audit_log", [], |r| r.get(0))
        .unwrap();
    let server = Server::new(
        Config {
            token: TEST_TOKEN.to_string(),
            merge_enabled: false,
            ..Config::default()
        },
        store,
    );

    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let serving = tokio::spawn(async move {
        server
            .serve_with_shutdown(listener, async {
                tokio::time::sleep(Duration::from_millis(100)).await;
            })
            .await
    });

    // Really bound and accepting before the shutdown fires.
    drop(tokio::net::TcpStream::connect(addr).await.unwrap());

    let done = tokio::time::timeout(Duration::from_secs(5), serving)
        .await
        .expect("server did not shut down within 5s")
        .expect("serve task panicked");
    assert!(done.is_ok(), "shutdown returned {done:?}");

    let wal = std::fs::metadata(dir.path().join("recall.db-wal")).map_or(0, |m| m.len());
    assert_eq!(
        wal, 0,
        "the WAL was emptied into recall.db as the server stopped"
    );
    let leaves: i64 = reader
        .query_row("SELECT count(*) FROM audit_log", [], |r| r.get(0))
        .unwrap();
    assert_eq!(leaves, 1, "the start leaf, in the file");
}

/// A stand-in `claude` that drains stdin, ignores its arguments and prints
/// `out`.
fn fake_claude(dir: &std::path::Path, out: &str) -> String {
    use std::io::Write;
    use std::os::unix::fs::PermissionsExt;

    let path = dir.join("claude");
    let mut f = std::fs::File::create(&path).unwrap();
    writeln!(f, "#!/bin/sh").unwrap();
    writeln!(f, "cat > /dev/null").unwrap();
    writeln!(f, "printf '%s' '{out}'").unwrap();
    drop(f);
    std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap();
    path.to_str().unwrap().to_string()
}

// ---------------------------------------------------------------------------
// discovery and the protocol header
// ---------------------------------------------------------------------------

/// A client needs to know what it is talking to before it knows whether it
/// can authenticate, so discovery asks for no token.
#[tokio::test]
async fn discovery_needs_no_token_and_describes_the_server() {
    let h = harness(|_| {});
    let (status, _, body) = h.get(None, recall_wire::DISCOVERY_PATH).await;
    assert_eq!(status, StatusCode::OK);
    let doc: recall_wire::Discovery = serde_json::from_slice(&body).unwrap();
    assert!(doc.speaks(recall_wire::PROTOCOL));
    assert_eq!(doc.protocol.current, recall_wire::PROTOCOL);
    // The deploy's commit is the build's revision, and a build that is not
    // a release says so and reports a -dev version.
    assert_eq!(doc.server.build.revision.as_deref(), Some("testcommit"));
    assert_eq!(doc.server.build.channel, "dev");
    assert!(
        doc.server.version.contains("-dev"),
        "{}",
        doc.server.version
    );
    assert!(recall_wire::discovery::Version::parse(&doc.min_client).is_some());
    // Nothing is removed within a protocol version: bearer stays, where it
    // was, and device signatures are added after it.
    assert_eq!(
        doc.auth.methods,
        vec!["bearer".to_string(), "device-sig-v1".to_string()]
    );
    assert!(doc.can("merge_base") && doc.can("scopes") && doc.can("limits"));
    let devices = doc.devices().expect("the devices capability");
    assert_eq!(devices.enroll_path, recall_wire::devices::ENROLL_PATH);
    assert_eq!(
        (
            devices.code_ttl_seconds,
            devices.poll_interval_seconds,
            devices.signature_window_seconds
        ),
        (900, 5, 60)
    );
}

/// A client that names a protocol this server does not speak is told so,
/// before the token is even looked at: an upgrade is the fix, not a login.
#[tokio::test]
async fn an_unknown_protocol_is_refused_with_what_the_server_speaks() {
    let h = harness(|_| {});
    let req = Request::builder()
        .method("GET")
        .uri("/sync?project_key=acme/app")
        .header("authorization", format!("Bearer {TEST_TOKEN}"))
        .header(recall_wire::PROTOCOL_HEADER, "99")
        .body(Body::empty())
        .unwrap();
    let (status, _, body) = h.send(req).await;
    assert_eq!(status, StatusCode::BAD_REQUEST);
    let text = String::from_utf8_lossy(&body);
    assert!(text.contains("protocol 1") && text.contains("99"), "{text}");
}

/// Every client before the header existed sends none, and the one this
/// build ships sends 1. Both are served.
#[tokio::test]
async fn protocol_1_and_no_header_are_both_served() {
    let h = harness(|_| {});
    for header in [None, Some("1")] {
        let mut req = Request::builder()
            .method("GET")
            .uri("/sync?project_key=acme/app")
            .header("authorization", format!("Bearer {TEST_TOKEN}"));
        if let Some(v) = header {
            req = req.header(recall_wire::PROTOCOL_HEADER, v);
        }
        let (status, _, _) = h.send(req.body(Body::empty()).unwrap()).await;
        assert_eq!(status, StatusCode::OK, "header {header:?}");
    }
}

/// Every push a released client has sent is still accepted, byte for byte.
/// The fixtures are the ones `recall-wire`'s golden tests read; this is the
/// same promise from the side that has to honour it.
#[tokio::test]
async fn every_push_a_released_client_sent_is_still_accepted() {
    let root =
        std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../recall-wire/fixtures/wire");
    let mut sent = 0;
    for version in std::fs::read_dir(&root).unwrap() {
        let version = version.unwrap().path();
        for name in ["push_request.json", "push_request_delete.json"] {
            let path = version.join(name);
            let Ok(body) = std::fs::read(&path) else {
                continue;
            };
            let h = harness(|_| {});
            let req = Request::builder()
                .method("POST")
                .uri("/sync")
                .header("authorization", format!("Bearer {TEST_TOKEN}"))
                .header("content-type", "application/json")
                .body(Body::from(body))
                .unwrap();
            let (status, _, resp) = h.send(req).await;
            assert_eq!(
                status,
                StatusCode::OK,
                "{}: {}",
                path.display(),
                String::from_utf8_lossy(&resp)
            );
            sent += 1;
        }
    }
    assert!(sent >= 4, "found only {sent} request fixtures");
}