batpak 0.7.0

Event sourcing with causal graphs and policy gates. Sync API, zero async.
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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
//! Direct tests of ProjectionCache trait methods on the built-in backends.
//! Plus: integration tests with real Store operations around cache population,
//! stale reads, and refresh behavior.
//!
//! Integration tests: `cargo test --test projection_cache`
//!
//! PROVES: LAW-001 (No Fake Success — cached projections must be correct)
//! DEFENDS: FM-009 (Polite Downgrade — MaybeStale stale-window semantics stay honest)
//! INVARIANTS: INV-TYPE (cache round-trip fidelity), INV-TEMP (freshness semantics)

use batpak::store::projection::{CacheMeta, NoCache, ProjectionCache};
use batpak::store::StoreError;
use std::sync::atomic::{AtomicI64, AtomicUsize, Ordering};
use std::sync::Arc;

fn test_meta() -> CacheMeta {
    CacheMeta {
        watermark: 42,
        cached_at_us: 1_000_000,
        cached_at_mono_ns: None,
        process_boot_ns: None,
    }
}

struct GetErrorCache;

impl ProjectionCache for GetErrorCache {
    fn capabilities(&self) -> batpak::store::projection::CacheCapabilities {
        batpak::store::projection::CacheCapabilities::none()
    }

    fn get(&self, _key: &[u8]) -> Result<Option<(Vec<u8>, CacheMeta)>, StoreError> {
        Err(StoreError::CacheFailed(
            "simulated cache get failure".into(),
        ))
    }

    fn put(&self, _key: &[u8], _value: &[u8], _meta: CacheMeta) -> Result<(), StoreError> {
        Ok(())
    }

    fn delete_prefix(&self, _prefix: &[u8]) -> Result<u64, StoreError> {
        Ok(0)
    }

    fn sync(&self) -> Result<(), StoreError> {
        Ok(())
    }
}

#[derive(Default)]
struct CacheProbeCounters {
    gets: AtomicUsize,
    puts: AtomicUsize,
    prefetches: AtomicUsize,
}

struct CountingCache {
    counters: Arc<CacheProbeCounters>,
}

impl ProjectionCache for CountingCache {
    fn capabilities(&self) -> batpak::store::projection::CacheCapabilities {
        batpak::store::projection::CacheCapabilities {
            is_noop: false,
            supports_prefetch: true,
        }
    }

    fn get(&self, _key: &[u8]) -> Result<Option<(Vec<u8>, CacheMeta)>, StoreError> {
        self.counters.gets.fetch_add(1, Ordering::SeqCst);
        Ok(None)
    }

    fn put(&self, _key: &[u8], _value: &[u8], _meta: CacheMeta) -> Result<(), StoreError> {
        self.counters.puts.fetch_add(1, Ordering::SeqCst);
        Ok(())
    }

    fn delete_prefix(&self, _prefix: &[u8]) -> Result<u64, StoreError> {
        Ok(0)
    }

    fn sync(&self) -> Result<(), StoreError> {
        Ok(())
    }

    fn prefetch(&self, _key: &[u8], _predicted_meta: CacheMeta) -> Result<(), StoreError> {
        self.counters.prefetches.fetch_add(1, Ordering::SeqCst);
        Ok(())
    }
}

fn legacy_cache_bytes(value: &[u8], watermark: u64, cached_at_us: i64) -> Vec<u8> {
    let mut bytes = value.to_vec();
    bytes.extend_from_slice(&watermark.to_le_bytes());
    bytes.extend_from_slice(&cached_at_us.to_le_bytes());
    bytes
}

// ================================================================
// NoCache — the default. Every read replays from segments.
// ================================================================

#[test]
fn nocache_get_always_returns_none() {
    let cache = NoCache;
    let result = cache.get(b"any_key").expect("get should not error");
    assert!(
        result.is_none(),
        "NoCache::get should always return None. Investigate: src/store/projection/mod.rs NoCache."
    );
}

#[test]
fn nocache_put_is_noop() {
    let cache = NoCache;
    cache
        .put(b"key", b"value", test_meta())
        .expect("put should not error");
    // Verify: still returns None after put
    let result = cache.get(b"key").expect("get");
    assert!(result.is_none(), "NoCache should not store anything.");
}

#[test]
fn nocache_delete_prefix_returns_zero() {
    let cache = NoCache;
    let count = cache.delete_prefix(b"prefix").expect("delete_prefix");
    assert_eq!(count, 0, "NoCache::delete_prefix should return 0.");
}

#[test]
fn nocache_sync_is_noop() {
    let cache = NoCache;
    cache.sync().expect("NoCache::sync should not error.");
}

// ================================================================
// NativeCache — built-in file-backed cache.
// ================================================================

mod native_tests {
    use super::*;
    use batpak::store::projection::NativeCache;
    use tempfile::TempDir;

    fn native_cache() -> (NativeCache, TempDir) {
        let dir = TempDir::new().expect("temp dir");
        let path = dir.path().join("cache");
        let cache = NativeCache::open(&path).expect("open native cache");
        (cache, dir)
    }

    #[test]
    fn native_get_put_round_trip() {
        let (cache, _dir) = native_cache();
        let meta = test_meta();

        // Put creates shard dir and file, then get retrieves
        cache.put(b"key1", b"hello", meta.clone()).expect("put");
        let (value, returned_meta) = cache.get(b"key1").expect("get").expect("should be Some");
        assert_eq!(
            value, b"hello",
            "NativeCache round-trip failed. Investigate: src/store/projection/mod.rs NativeCache."
        );
        assert_eq!(
            returned_meta.watermark, 42,
            "NATIVE ROUND-TRIP META WATERMARK: watermark should be preserved across put/get.\n\
             Investigate: src/store/projection/mod.rs NativeCache::put and NativeCache::get.\n\
             Common causes: CacheMeta serialization losing watermark field."
        );
        assert_eq!(
            returned_meta.cached_at_us, 1_000_000,
            "NATIVE ROUND-TRIP META CACHED_AT: cached_at_us should be preserved across put/get.\n\
             Investigate: src/store/projection/mod.rs NativeCache::put and NativeCache::get.\n\
             Common causes: CacheMeta serialization losing cached_at_us field."
        );

        // Non-existent key returns None
        assert!(
            cache.get(b"nonexistent").expect("get").is_none(),
            "NATIVE ROUND-TRIP: get for a key that was never inserted should return None.\n\
             Investigate: src/store/projection/mod.rs NativeCache::get."
        );
    }

    #[test]
    fn native_delete_prefix() {
        let (cache, _dir) = native_cache();
        let meta = test_meta();
        cache.put(b"user:1", b"alice", meta.clone()).expect("put");
        cache.put(b"user:2", b"bob", meta.clone()).expect("put");
        cache.put(b"order:1", b"widget", meta.clone()).expect("put");

        let deleted = cache.delete_prefix(b"user:").expect("delete_prefix");
        assert_eq!(deleted, 2, "Should delete 2 keys with prefix 'user:'.");

        // user keys gone
        assert!(
            cache.get(b"user:1").expect("get").is_none(),
            "NATIVE DELETE PREFIX: key 'user:1' should be gone after delete_prefix('user:').\n\
             Investigate: src/store/projection/mod.rs NativeCache::delete_prefix."
        );
        assert!(
            cache.get(b"user:2").expect("get").is_none(),
            "NATIVE DELETE PREFIX: key 'user:2' should be gone after delete_prefix('user:')."
        );
        // order key remains
        assert!(
            cache.get(b"order:1").expect("get").is_some(),
            "NATIVE DELETE PREFIX: key 'order:1' should survive delete_prefix('user:')."
        );
    }

    #[test]
    fn native_delete_prefix_is_idempotent() {
        let (cache, _dir) = native_cache();
        let meta = test_meta();

        cache.put(b"user:1", b"alice", meta.clone()).expect("put");
        cache.put(b"user:2", b"bob", meta).expect("put");

        let first = cache.delete_prefix(b"user:").expect("delete prefix");
        let second = cache.delete_prefix(b"user:").expect("delete prefix again");
        cache.sync().expect("sync");

        assert_eq!(
            first, 2,
            "NATIVE DELETE PREFIX IDEMPOTENCE: first delete should remove both matching entries."
        );
        assert_eq!(
            second, 0,
            "NATIVE DELETE PREFIX IDEMPOTENCE: repeating the delete must be a clean no-op."
        );
    }

    #[test]
    fn native_sync_is_safe() {
        let (cache, _dir) = native_cache();
        cache.sync().expect("NativeCache::sync should not error.");
    }

    #[test]
    fn native_reopen_preserves_cache() {
        let dir = TempDir::new().expect("temp dir");
        let cache_path = dir.path().join("cache");
        let meta = test_meta();

        // Write with first instance
        {
            let cache = NativeCache::open(&cache_path).expect("open");
            cache
                .put(b"persistent_key", b"durable_value", meta)
                .expect("put");
        }

        // Reopen and verify
        {
            let cache = NativeCache::open(&cache_path).expect("reopen");
            let (value, returned_meta) = cache
                .get(b"persistent_key")
                .expect("get")
                .expect("should be Some after reopen");
            assert_eq!(
                value, b"durable_value",
                "NativeCache must survive process restart."
            );
            assert_eq!(returned_meta.watermark, 42);
        }
    }

    #[test]
    fn native_corruption_falls_back_to_cache_miss() {
        let dir = TempDir::new().expect("temp dir");
        let cache_path = dir.path().join("cache");
        let meta = test_meta();

        let cache = NativeCache::open(&cache_path).expect("open");
        cache.put(b"corrupt_me", b"valid_data", meta).expect("put");

        // Corrupt the file by writing garbage
        let hex_key: String = b"corrupt_me".iter().map(|b| format!("{b:02x}")).collect();
        let shard = &hex_key[..2];
        let corrupt_path = cache_path.join(shard).join(format!("{hex_key}.bin"));
        std::fs::write(&corrupt_path, b"garbage").expect("write garbage");

        // Get should return None (cache miss), not error
        let result = cache.get(b"corrupt_me").expect("get should not error");
        assert!(
            result.is_none(),
            "NATIVE CORRUPTION: corrupt cache file should degrade to cache miss, not error.\n\
             Investigate: src/store/projection/mod.rs NativeCache::get decode error path."
        );

        // Corrupt file should be deleted (self-healing)
        assert!(
            !corrupt_path.exists(),
            "NATIVE SELF-HEAL: corrupt cache file should be deleted after failed decode."
        );
    }

    #[test]
    fn native_delete_prefix_with_0xff_keys() {
        let (cache, _dir) = native_cache();
        let meta = test_meta();

        cache
            .put(&[0xFF, 0x01], b"val1", meta.clone())
            .expect("put");
        cache
            .put(&[0xFF, 0x02], b"val2", meta.clone())
            .expect("put");
        cache
            .put(&[0xFF, 0xFF], b"val3", meta.clone())
            .expect("put");
        cache
            .put(&[0xFE, 0x01], b"other", meta.clone())
            .expect("put");

        let deleted = cache.delete_prefix(&[0xFF]).expect("delete_prefix");
        assert_eq!(
            deleted, 3,
            "DELETE PREFIX 0xFF: should delete all 3 keys starting with 0xFF."
        );

        assert!(
            cache.get(&[0xFE, 0x01]).expect("get").is_some(),
            "DELETE PREFIX 0xFF: key [0xFE, 0x01] should survive prefix delete of [0xFF]."
        );
    }

    #[test]
    fn native_delete_prefix_empty_prefix_deletes_all() {
        let (cache, _dir) = native_cache();
        let meta = test_meta();

        cache.put(b"a", b"1", meta.clone()).expect("put");
        cache.put(b"b", b"2", meta.clone()).expect("put");
        cache.put(b"z", b"3", meta.clone()).expect("put");

        let deleted = cache.delete_prefix(b"").expect("delete_prefix");
        assert_eq!(
            deleted, 3,
            "DELETE PREFIX EMPTY: empty prefix should match all keys."
        );
    }

    // -- Integration: Store + NativeCache --

    use batpak::prelude::*;
    use batpak::store::SyncConfig;

    #[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)]
    struct Counter {
        count: u32,
    }
    impl EventSourced for Counter {
        type Input = batpak::prelude::JsonValueInput;

        fn from_events(events: &[batpak::prelude::Event<serde_json::Value>]) -> Option<Self> {
            Some(Counter {
                count: u32::try_from(events.len()).expect("test uses < 2^32 events"),
            })
        }
        fn apply_event(&mut self, _event: &batpak::prelude::Event<serde_json::Value>) {
            self.count += 1;
        }
        fn relevant_event_kinds() -> &'static [EventKind] {
            &[]
        }
    }

    #[test]
    fn native_projection_round_trip() {
        let dir = TempDir::new().expect("temp dir");
        let cache_path = dir.path().join("cache");

        let config = StoreConfig {
            data_dir: dir.path().join("data"),
            segment_max_bytes: 4096,
            sync: SyncConfig {
                every_n_events: 1,
                ..SyncConfig::default()
            },
            ..StoreConfig::new("")
        };
        let store = Store::open_with_native_cache(config, &cache_path)
            .expect("open store with native cache");

        let coord = Coordinate::new("entity:native1", "scope:test").expect("coord");
        let kind = EventKind::custom(0xF, 1);
        store
            .append(&coord, kind, &serde_json::json!({"x": 1}))
            .expect("append 1");
        store
            .append(&coord, kind, &serde_json::json!({"x": 2}))
            .expect("append 2");

        // First project: cache miss, replays from segments
        let result: Option<Counter> = store
            .project("entity:native1", &Freshness::Consistent)
            .expect("project");
        assert_eq!(
            result,
            Some(Counter { count: 2 }),
            "NATIVE PROJECTION ROUND-TRIP: first project should replay 2 events."
        );

        // Second project: should hit cache (same watermark)
        let result2: Option<Counter> = store
            .project("entity:native1", &Freshness::Consistent)
            .expect("project 2");
        assert_eq!(result2, Some(Counter { count: 2 }));

        // Append more → cache should be stale → re-replay
        store
            .append(&coord, kind, &serde_json::json!({"x": 3}))
            .expect("append 3");
        let result3: Option<Counter> = store
            .project("entity:native1", &Freshness::Consistent)
            .expect("project 3");
        assert_eq!(
            result3,
            Some(Counter { count: 3 }),
            "NATIVE CACHE INVALIDATION: after appending more events, project should re-replay."
        );

        store.close().expect("close");
    }

    #[test]
    fn native_delete_prefix_then_project_repopulates_cache() {
        let dir = TempDir::new().expect("temp dir");
        let cache_path = dir.path().join("cache");
        let config = StoreConfig {
            data_dir: dir.path().join("data"),
            segment_max_bytes: 4096,
            sync: SyncConfig {
                every_n_events: 1,
                ..SyncConfig::default()
            },
            ..StoreConfig::new("")
        };
        let coord = Coordinate::new("entity:native-miss", "scope:test").expect("coord");
        let kind = EventKind::custom(0xF, 1);

        {
            let store =
                Store::open_with_native_cache(config.clone(), &cache_path).expect("open store");
            store
                .append(&coord, kind, &serde_json::json!({"x": 1}))
                .expect("append 1");
            store
                .append(&coord, kind, &serde_json::json!({"x": 2}))
                .expect("append 2");
            let _: Option<Counter> = store
                .project("entity:native-miss", &Freshness::Consistent)
                .expect("warm cache");
            store.close().expect("close");
        }

        {
            let cache = NativeCache::open(&cache_path).expect("reopen cache");
            let deleted = cache
                .delete_prefix(b"entity:native-miss")
                .expect("delete prefix");
            assert!(
                deleted >= 1,
                "NATIVE CACHE MISS PROOF: delete_prefix should remove at least one warmed cache key, got {deleted}."
            );
        }

        {
            let store = Store::open_with_native_cache(config, &cache_path).expect("reopen store");
            let result: Option<Counter> = store
                .project("entity:native-miss", &Freshness::Consistent)
                .expect("project after delete");
            assert_eq!(result, Some(Counter { count: 2 }));
            store.close().expect("close");
        }

        let cache = NativeCache::open(&cache_path).expect("final reopen cache");
        let repopulated = cache
            .delete_prefix(b"entity:native-miss")
            .expect("check repopulated");
        assert!(
            repopulated >= 1,
            "NATIVE CACHE MISS PROOF: projecting after delete_prefix must repopulate the cache key."
        );
    }
}

// ================================================================
// Freshness::MaybeStale + cache metadata edge cases
// PROVES: LAW-001 (No Fake Success — stale cache must not serve wrong data)
// DEFENDS: FM-009 (Polite Downgrade — MaybeStale stale-window semantics stay honest)
// ================================================================

#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)]
struct MaybeStaleCounter {
    count: u32,
}
impl batpak::prelude::EventSourced for MaybeStaleCounter {
    type Input = batpak::prelude::JsonValueInput;

    fn from_events(events: &[batpak::prelude::Event<serde_json::Value>]) -> Option<Self> {
        Some(MaybeStaleCounter {
            count: u32::try_from(events.len()).expect("test uses < 2^32 events"),
        })
    }
    fn apply_event(&mut self, _event: &batpak::prelude::Event<serde_json::Value>) {
        self.count += 1;
    }
    fn relevant_event_kinds() -> &'static [batpak::prelude::EventKind] {
        &[]
    }
}

const MAYBE_STALE_GENERATION_KIND: batpak::prelude::EventKind =
    batpak::prelude::EventKind::custom(0xF, 0x51);

#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)]
struct MaybeStaleGenerationCounter {
    count: u32,
}

impl batpak::prelude::EventSourced for MaybeStaleGenerationCounter {
    type Input = batpak::prelude::JsonValueInput;

    fn from_events(events: &[batpak::prelude::Event<serde_json::Value>]) -> Option<Self> {
        Some(Self {
            count: u32::try_from(events.len()).expect("test uses < 2^32 events"),
        })
    }

    fn apply_event(&mut self, _event: &batpak::prelude::Event<serde_json::Value>) {
        self.count += 1;
    }

    fn relevant_event_kinds() -> &'static [batpak::prelude::EventKind] {
        &[MAYBE_STALE_GENERATION_KIND]
    }
}

fn find_only_native_cache_entry(cache_path: &std::path::Path) -> std::path::PathBuf {
    let mut stack = vec![cache_path.to_path_buf()];
    let mut entries = Vec::new();

    while let Some(dir) = stack.pop() {
        for entry in std::fs::read_dir(&dir).expect("read_dir") {
            let entry = entry.expect("dir entry");
            let path = entry.path();
            if path.is_dir() {
                stack.push(path);
            } else if path.extension().and_then(std::ffi::OsStr::to_str) == Some("bin") {
                entries.push(path);
            }
        }
    }

    assert_eq!(
        entries.len(),
        1,
        "PROJECTION CACHE TEST SETUP: expected exactly one native cache entry, found {}",
        entries.len()
    );
    entries.pop().expect("single cache entry")
}

#[test]
fn freshness_maybe_stale_serves_stale_cache_within_window() {
    use batpak::prelude::*;
    use batpak::store::{Freshness, NativeCache, Store, StoreConfig, SyncConfig};
    use tempfile::TempDir;

    let dir = TempDir::new().expect("temp dir");
    let cache_path = dir.path().join("cache");
    let cache = NativeCache::open(&cache_path).expect("open native cache");

    let config = StoreConfig {
        data_dir: dir.path().join("data"),
        segment_max_bytes: 4096,
        sync: SyncConfig {
            every_n_events: 1,
            ..SyncConfig::default()
        },
        ..StoreConfig::new("")
    };
    let store = Store::open_with_cache(config, Box::new(cache)).expect("open store");

    let coord = Coordinate::new("entity:besteff1", "scope:test").expect("coord");
    let kind = EventKind::custom(0xF, 1);
    store
        .append(&coord, kind, &serde_json::json!({"x": 1}))
        .expect("append 1");
    store
        .append(&coord, kind, &serde_json::json!({"x": 2}))
        .expect("append 2");

    // Project with Consistent to populate cache
    let result: Option<MaybeStaleCounter> = store
        .project("entity:besteff1", &Freshness::Consistent)
        .expect("project consistent");
    assert_eq!(result, Some(MaybeStaleCounter { count: 2 }));

    // Append a third event — cache is now stale
    store
        .append(&coord, kind, &serde_json::json!({"x": 3}))
        .expect("append 3");

    // MaybeStale with large window should serve the stale cached value
    let result_best: Option<MaybeStaleCounter> = store
        .project(
            "entity:besteff1",
            &Freshness::MaybeStale {
                max_stale_ms: 60_000,
            },
        )
        .expect("project maybe stale");
    assert_eq!(
        result_best,
        Some(MaybeStaleCounter { count: 2 }),
        "FRESHNESS BEST EFFORT: with large stale window, should serve cached value (count=2) \
         even though a 3rd event was appended.\n\
         Investigate: src/store/mod.rs project() MaybeStale branch."
    );

    // MaybeStale with zero window should force re-replay
    let result_strict: Option<MaybeStaleCounter> = store
        .project(
            "entity:besteff1",
            &Freshness::MaybeStale { max_stale_ms: 0 },
        )
        .expect("project maybe stale strict");
    assert_eq!(
        result_strict,
        Some(MaybeStaleCounter { count: 3 }),
        "FRESHNESS BEST EFFORT ZERO: with max_stale_ms=0, cache should always be considered \
         stale, forcing a full replay (count=3)."
    );

    store.close().expect("close");
}

#[test]
fn freshness_maybe_stale_replays_when_stale_cache_bytes_are_corrupt() {
    use batpak::prelude::*;
    use batpak::store::{Freshness, Store, StoreConfig, SyncConfig};
    use tempfile::TempDir;

    let dir = TempDir::new().expect("temp dir");
    let cache_path = dir.path().join("cache");

    let config = StoreConfig {
        data_dir: dir.path().join("data"),
        segment_max_bytes: 4096,
        sync: SyncConfig {
            every_n_events: 1,
            ..SyncConfig::default()
        },
        ..StoreConfig::new("")
    };
    let coord = Coordinate::new("entity:maybe-stale-corrupt", "scope:test").expect("coord");
    let kind = EventKind::custom(0xF, 1);

    {
        let store = Store::open_with_native_cache(config.clone(), &cache_path).expect("open store");
        store
            .append(&coord, kind, &serde_json::json!({"x": 1}))
            .expect("append 1");
        store
            .append(&coord, kind, &serde_json::json!({"x": 2}))
            .expect("append 2");

        let seeded: Option<MaybeStaleCounter> = store
            .project("entity:maybe-stale-corrupt", &Freshness::Consistent)
            .expect("seed cache");
        assert_eq!(seeded, Some(MaybeStaleCounter { count: 2 }));

        // Advance the entity so the warmed cache row is stale by watermark,
        // but keep the row young enough that MaybeStale would otherwise try
        // to serve it from the external cache.
        store
            .append(&coord, kind, &serde_json::json!({"x": 3}))
            .expect("append 3");
        store.close().expect("close seeded store");
    }

    let cache_entry = find_only_native_cache_entry(&cache_path);
    let mut corrupted = std::fs::read(&cache_entry).expect("read cache entry");
    let last = corrupted
        .len()
        .checked_sub(1)
        .expect("non-empty cache entry");
    corrupted[last] ^= 0x5A;
    std::fs::write(&cache_entry, corrupted).expect("corrupt cache entry");

    let store = Store::open_with_native_cache(config, &cache_path).expect("reopen store");
    let result: Option<MaybeStaleCounter> = store
        .project(
            "entity:maybe-stale-corrupt",
            &Freshness::MaybeStale {
                max_stale_ms: 60_000,
            },
        )
        .expect("project maybe stale after corruption");
    assert_eq!(
        result,
        Some(MaybeStaleCounter { count: 3 }),
        "MAYBE STALE CORRUPTION HONESTY: a stale-but-young corrupt cache row must fall back to replay and return the current folded state.\n\
         It must not serve garbage and must not preserve the stale count=2 row just because the age window still says 'fresh enough'."
    );
    store.close().expect("close");
}

#[test]
fn freshness_maybe_stale_replays_when_fresh_cache_bytes_are_corrupt() {
    use batpak::prelude::*;
    use batpak::store::{Freshness, Store, StoreConfig, SyncConfig};
    use tempfile::TempDir;

    let dir = TempDir::new().expect("temp dir");
    let cache_path = dir.path().join("cache");

    let config = StoreConfig {
        data_dir: dir.path().join("data"),
        segment_max_bytes: 4096,
        sync: SyncConfig {
            every_n_events: 1,
            ..SyncConfig::default()
        },
        ..StoreConfig::new("")
    };
    let coord = Coordinate::new("entity:maybe-stale-fresh-corrupt", "scope:test").expect("coord");
    let kind = EventKind::custom(0xF, 1);

    {
        let store = Store::open_with_native_cache(config.clone(), &cache_path).expect("open store");
        store
            .append(&coord, kind, &serde_json::json!({"x": 1}))
            .expect("append 1");
        store
            .append(&coord, kind, &serde_json::json!({"x": 2}))
            .expect("append 2");

        let seeded: Option<MaybeStaleCounter> = store
            .project("entity:maybe-stale-fresh-corrupt", &Freshness::Consistent)
            .expect("seed cache");
        assert_eq!(seeded, Some(MaybeStaleCounter { count: 2 }));
        store.close().expect("close seeded store");
    }

    let cache_entry = find_only_native_cache_entry(&cache_path);
    let mut corrupted = std::fs::read(&cache_entry).expect("read cache entry");
    let first = corrupted.first_mut().expect("non-empty cache entry");
    *first ^= 0xA5;
    std::fs::write(&cache_entry, corrupted).expect("corrupt cache entry");

    let store = Store::open_with_native_cache(config, &cache_path).expect("reopen store");
    let result: Option<MaybeStaleCounter> = store
        .project(
            "entity:maybe-stale-fresh-corrupt",
            &Freshness::MaybeStale {
                max_stale_ms: 60_000,
            },
        )
        .expect("project maybe stale after fresh corruption");
    assert_eq!(
        result,
        Some(MaybeStaleCounter { count: 2 }),
        "MAYBE STALE FRESH CORRUPTION HONESTY: a fresh-but-corrupt cache row must fall back to replay and return the current folded state.\n\
         It must not fail open just because the age window still says 'fresh enough'."
    );
    store.close().expect("close");
}

#[test]
fn project_if_changed_never_pairs_maybe_stale_cache_with_new_generation() {
    use batpak::prelude::*;
    use batpak::store::{Freshness, NativeCache, Store, StoreConfig, SyncConfig};
    use tempfile::TempDir;

    let dir = TempDir::new().expect("temp dir");
    let cache_path = dir.path().join("cache");
    let cache = NativeCache::open(&cache_path).expect("open native cache");

    let config = StoreConfig {
        data_dir: dir.path().join("data"),
        segment_max_bytes: 4096,
        sync: SyncConfig {
            every_n_events: 1,
            ..SyncConfig::default()
        },
        ..StoreConfig::new("")
    };
    let store = Store::open_with_cache(config, Box::new(cache)).expect("open store");

    let coord = Coordinate::new("entity:generation-honesty", "scope:test").expect("coord");
    store
        .append(
            &coord,
            MAYBE_STALE_GENERATION_KIND,
            &serde_json::json!({"x": 1}),
        )
        .expect("append 1");
    store
        .append(
            &coord,
            MAYBE_STALE_GENERATION_KIND,
            &serde_json::json!({"x": 2}),
        )
        .expect("append 2");

    let seeded: Option<MaybeStaleGenerationCounter> = store
        .project("entity:generation-honesty", &Freshness::Consistent)
        .expect("seed cache");
    assert_eq!(seeded, Some(MaybeStaleGenerationCounter { count: 2 }));

    let baseline_generation = store
        .entity_generation("entity:generation-honesty")
        .expect("baseline generation");

    store
        .append(
            &coord,
            MAYBE_STALE_GENERATION_KIND,
            &serde_json::json!({"x": 3}),
        )
        .expect("append 3");

    let changed = store
        .project_if_changed::<MaybeStaleGenerationCounter>(
            "entity:generation-honesty",
            baseline_generation,
            &Freshness::MaybeStale {
                max_stale_ms: 60_000,
            },
        )
        .expect("project_if_changed")
        .expect("changed projection");

    assert!(
        changed.0 > baseline_generation,
        "generation should advance after the third relevant append"
    );
    assert_eq!(
        changed.1,
        Some(MaybeStaleGenerationCounter { count: 3 }),
        "PROPERTY: project_if_changed must not return stale cache bytes together with a newer generation token.\n\
         Investigate: src/store/projection/flow.rs project_if_changed() MaybeStale path."
    );

    store.close().expect("close");
}

#[test]
fn empty_projection_surface_skips_cache_for_no_replay_plan() {
    use batpak::store::{Freshness, Store, StoreConfig, SyncConfig};
    use tempfile::TempDir;

    let dir = TempDir::new().expect("temp dir");
    let counters = Arc::new(CacheProbeCounters::default());
    let cache = CountingCache {
        counters: Arc::clone(&counters),
    };
    let config = StoreConfig {
        data_dir: dir.path().join("data"),
        segment_max_bytes: 4096,
        sync: SyncConfig {
            every_n_events: 1,
            ..SyncConfig::default()
        },
        ..StoreConfig::new("")
    };
    let store = Store::open_with_cache(config, Box::new(cache)).expect("open store");

    let consistent: Option<MaybeStaleCounter> = store
        .project("entity:no-events", &Freshness::Consistent)
        .expect("project empty consistent");
    let maybe_stale: Option<MaybeStaleCounter> = store
        .project(
            "entity:no-events",
            &Freshness::MaybeStale {
                max_stale_ms: 60_000,
            },
        )
        .expect("project empty maybe stale");
    let unchanged = store
        .project_if_changed::<MaybeStaleCounter>("entity:no-events", 0, &Freshness::Consistent)
        .expect("project_if_changed empty");

    assert_eq!(
        consistent, None,
        "EMPTY PROJECTION SURFACE: project() on an entity with no replay plan should return None."
    );
    assert_eq!(
        maybe_stale, None,
        "EMPTY PROJECTION SURFACE: MaybeStale must not invent a cache-backed value for an empty entity."
    );
    assert_eq!(
        unchanged, None,
        "EMPTY PROJECTION SURFACE: project_if_changed() should report no change for a never-seen entity."
    );
    assert_eq!(
        counters.gets.load(Ordering::SeqCst),
        0,
        "EMPTY PROJECTION SURFACE: no replay plan should skip external cache get entirely."
    );
    assert_eq!(
        counters.prefetches.load(Ordering::SeqCst),
        0,
        "EMPTY PROJECTION SURFACE: no replay plan should skip cache prefetch entirely."
    );
    assert_eq!(
        counters.puts.load(Ordering::SeqCst),
        0,
        "EMPTY PROJECTION SURFACE: no replay plan should not populate cache."
    );

    store.close().expect("close");
}

#[test]
fn consistent_replays_when_reopened_native_cache_row_is_stale() {
    use batpak::prelude::*;
    use batpak::store::{Freshness, Store, StoreConfig, SyncConfig};
    use tempfile::TempDir;

    let dir = TempDir::new().expect("temp dir");
    let cache_path = dir.path().join("cache");
    let config = StoreConfig {
        data_dir: dir.path().join("data"),
        segment_max_bytes: 4096,
        sync: SyncConfig {
            every_n_events: 1,
            ..SyncConfig::default()
        },
        ..StoreConfig::new("")
    };
    let coord = Coordinate::new("entity:consistent-stale-cache", "scope:test").expect("coord");
    let kind = EventKind::custom(0xF, 1);

    {
        let store = Store::open_with_native_cache(config.clone(), &cache_path).expect("open store");
        store
            .append(&coord, kind, &serde_json::json!({"x": 1}))
            .expect("append 1");
        store
            .append(&coord, kind, &serde_json::json!({"x": 2}))
            .expect("append 2");
        let seeded: Option<MaybeStaleCounter> = store
            .project("entity:consistent-stale-cache", &Freshness::Consistent)
            .expect("seed cache");
        assert_eq!(seeded, Some(MaybeStaleCounter { count: 2 }));
        store.close().expect("close seeded store");
    }

    {
        let store = Store::open_with_native_cache(config, &cache_path).expect("reopen store");
        store
            .append(&coord, kind, &serde_json::json!({"x": 3}))
            .expect("append 3");
        let result: Option<MaybeStaleCounter> = store
            .project("entity:consistent-stale-cache", &Freshness::Consistent)
            .expect("project after stale cache");
        assert_eq!(
            result,
            Some(MaybeStaleCounter { count: 3 }),
            "CONSISTENT STALE CACHE HONESTY: after reopen, a populated external cache row with an older watermark must be bypassed and replayed."
        );
        store.close().expect("close");
    }
}

#[test]
fn maybe_stale_replays_when_cache_row_has_valid_metadata_but_empty_payload() {
    use batpak::prelude::*;
    use batpak::store::{Freshness, Store, StoreConfig, SyncConfig};
    use tempfile::TempDir;

    let dir = TempDir::new().expect("temp dir");
    let cache_path = dir.path().join("cache");
    let config = StoreConfig {
        data_dir: dir.path().join("data"),
        segment_max_bytes: 4096,
        sync: SyncConfig {
            every_n_events: 1,
            ..SyncConfig::default()
        },
        ..StoreConfig::new("")
    };
    let coord = Coordinate::new("entity:metadata-only-stale", "scope:test").expect("coord");
    let kind = EventKind::custom(0xF, 1);

    {
        let store = Store::open_with_native_cache(config.clone(), &cache_path).expect("open store");
        store
            .append(&coord, kind, &serde_json::json!({"x": 1}))
            .expect("append 1");
        store
            .append(&coord, kind, &serde_json::json!({"x": 2}))
            .expect("append 2");
        let seeded: Option<MaybeStaleCounter> = store
            .project("entity:metadata-only-stale", &Freshness::Consistent)
            .expect("seed cache");
        assert_eq!(seeded, Some(MaybeStaleCounter { count: 2 }));
        store
            .append(&coord, kind, &serde_json::json!({"x": 3}))
            .expect("append 3");
        store.close().expect("close seeded store");
    }

    let cache_entry = find_only_native_cache_entry(&cache_path);
    std::fs::write(&cache_entry, legacy_cache_bytes(&[], 2, 1_000_000))
        .expect("write metadata-only legacy cache entry");

    let store = Store::open_with_native_cache(config, &cache_path).expect("reopen store");
    let result: Option<MaybeStaleCounter> = store
        .project(
            "entity:metadata-only-stale",
            &Freshness::MaybeStale {
                max_stale_ms: 60_000,
            },
        )
        .expect("project maybe stale after metadata-only cache row");
    assert_eq!(
        result,
        Some(MaybeStaleCounter { count: 3 }),
        "METADATA-ONLY CACHE HONESTY: a cache file with valid metadata but undecodable payload must replay rather than serve a stale MaybeStale success."
    );
    store.close().expect("close");
}

#[test]
fn consistent_replays_when_cache_row_has_valid_metadata_but_truncated_payload() {
    use batpak::prelude::*;
    use batpak::store::{Freshness, Store, StoreConfig, SyncConfig};
    use tempfile::TempDir;

    let dir = TempDir::new().expect("temp dir");
    let cache_path = dir.path().join("cache");
    let config = StoreConfig {
        data_dir: dir.path().join("data"),
        segment_max_bytes: 4096,
        sync: SyncConfig {
            every_n_events: 1,
            ..SyncConfig::default()
        },
        ..StoreConfig::new("")
    };
    let coord = Coordinate::new("entity:metadata-valid-truncated", "scope:test").expect("coord");
    let kind = EventKind::custom(0xF, 1);

    {
        let store = Store::open_with_native_cache(config.clone(), &cache_path).expect("open store");
        store
            .append(&coord, kind, &serde_json::json!({"x": 1}))
            .expect("append 1");
        store
            .append(&coord, kind, &serde_json::json!({"x": 2}))
            .expect("append 2");
        let seeded: Option<MaybeStaleCounter> = store
            .project("entity:metadata-valid-truncated", &Freshness::Consistent)
            .expect("seed cache");
        assert_eq!(seeded, Some(MaybeStaleCounter { count: 2 }));
        store.close().expect("close seeded store");
    }

    let cache_entry = find_only_native_cache_entry(&cache_path);
    std::fs::write(&cache_entry, legacy_cache_bytes(b"\x92", 2, 1_000_000))
        .expect("write truncated-payload legacy cache entry");

    let store = Store::open_with_native_cache(config, &cache_path).expect("reopen store");
    let result: Option<MaybeStaleCounter> = store
        .project("entity:metadata-valid-truncated", &Freshness::Consistent)
        .expect("project after truncated cache payload");
    assert_eq!(
        result,
        Some(MaybeStaleCounter { count: 2 }),
        "TRUNCATED PAYLOAD CACHE HONESTY: valid cache metadata with an undecodable payload must fall back to replay under Consistent freshness."
    );
    store.close().expect("close");
}

#[test]
fn projection_replays_when_cache_get_errors() {
    use batpak::prelude::*;
    use batpak::store::{Freshness, Store, StoreConfig, SyncConfig};
    use tempfile::TempDir;

    let dir = TempDir::new().expect("temp dir");
    let config = StoreConfig {
        data_dir: dir.path().join("data"),
        segment_max_bytes: 4096,
        sync: SyncConfig {
            every_n_events: 1,
            ..SyncConfig::default()
        },
        ..StoreConfig::new("")
    };
    let store = Store::open_with_cache(config, Box::new(GetErrorCache)).expect("open store");
    let coord = Coordinate::new("entity:cache-get-error", "scope:test").expect("coord");
    let kind = EventKind::custom(0xF, 1);
    store
        .append(&coord, kind, &serde_json::json!({"x": 1}))
        .expect("append 1");
    store
        .append(&coord, kind, &serde_json::json!({"x": 2}))
        .expect("append 2");

    let result: Option<MaybeStaleCounter> = store
        .project("entity:cache-get-error", &Freshness::Consistent)
        .expect("project after cache get error");
    assert_eq!(
        result,
        Some(MaybeStaleCounter { count: 2 }),
        "CACHE GET ERROR HONESTY: a cache backend get failure must fall back to replay and return the honest folded state."
    );

    store.close().expect("close");
}

#[test]
fn freshness_maybe_stale_replays_at_exact_age_boundary() {
    use batpak::prelude::*;
    use batpak::store::{Freshness, NativeCache, Store, StoreConfig, SyncConfig};
    use tempfile::TempDir;

    let dir = TempDir::new().expect("temp dir");
    let cache_path = dir.path().join("cache");
    let cache = NativeCache::open(&cache_path).expect("open native cache");
    let now_us = Arc::new(AtomicI64::new(1_000_000));
    let clock: Arc<dyn Fn() -> i64 + Send + Sync> = {
        let now_us = Arc::clone(&now_us);
        Arc::new(move || now_us.load(Ordering::SeqCst))
    };

    let config = StoreConfig {
        data_dir: dir.path().join("data"),
        segment_max_bytes: 4096,
        sync: SyncConfig {
            every_n_events: 1,
            ..SyncConfig::default()
        },
        ..StoreConfig::new("")
    }
    .with_clock(Some(clock));
    let store = Store::open_with_cache(config, Box::new(cache)).expect("open store");

    let coord = Coordinate::new("entity:maybe-stale-boundary", "scope:test").expect("coord");
    let kind = EventKind::custom(0xF, 1);
    store
        .append(&coord, kind, &serde_json::json!({"x": 1}))
        .expect("append 1");
    store
        .append(&coord, kind, &serde_json::json!({"x": 2}))
        .expect("append 2");

    let seeded: Option<MaybeStaleCounter> = store
        .project("entity:maybe-stale-boundary", &Freshness::Consistent)
        .expect("seed cache");
    assert_eq!(seeded, Some(MaybeStaleCounter { count: 2 }));

    store
        .append(&coord, kind, &serde_json::json!({"x": 3}))
        .expect("append 3");

    now_us.store(1_005_000, Ordering::SeqCst);
    let result: Option<MaybeStaleCounter> = store
        .project(
            "entity:maybe-stale-boundary",
            &Freshness::MaybeStale { max_stale_ms: 5 },
        )
        .expect("project maybe stale at boundary");
    assert_eq!(
        result,
        Some(MaybeStaleCounter { count: 3 }),
        "MAYBE STALE BOUNDARY HONESTY: when cache age equals max_stale_ms exactly, the strict '<' boundary must force replay rather than serve the stale row."
    );

    store.close().expect("close");
}

#[test]
fn nocache_ignores_put_and_always_returns_none() {
    let cache = NoCache;
    cache.put(b"short", b"x", test_meta()).expect("put");
    let result = cache.get(b"short").expect("get");
    assert!(
        result.is_none(),
        "CACHE METADATA: NoCache should return None regardless of what was put."
    );
}

#[test]
fn nocache_prefetch_is_noop() {
    let cache = NoCache;
    let meta = test_meta();
    let caps = cache.capabilities();
    assert!(
        !caps.supports_prefetch,
        "NoCache must explicitly report that it does not support prefetch hints."
    );
    assert!(
        caps.is_noop,
        "NoCache must report itself as a no-op cache backend."
    );
    cache
        .prefetch(b"any_key", meta)
        .expect("NoCache::prefetch should not error — it's a no-op by default.");
}