batpak 0.3.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
#![allow(clippy::disallowed_methods, clippy::unwrap_used)] // tests use thread::spawn for producers
//! Integration tests for SubscriptionOps: filter, take, and combined chains.
//! [SPEC:tests/subscription_ops.rs]
//!
//! PROVES: LAW-004 (Composition Over Construction — ops chain correctly)
//! DEFENDS: FM-009 (Polite Downgrade — map must not silently drop events)
//! INVARIANTS: INV-STATE (subscription: open → recv → closed)

use batpak::prelude::*;
use batpak::store::Notification;
use std::sync::Arc;
use std::thread;

mod common;
use common::small_segment_store as test_store;

#[test]
fn ops_recv_without_filters() {
    let (store, _dir) = test_store();
    let store = Arc::new(store);
    let coord = Coordinate::new("entity:1", "scope:test").expect("valid coord");
    let kind = EventKind::custom(0xF, 1);

    let region = Region::entity("entity:");
    let sub = store.subscribe_lossy(&region);
    let mut ops = sub.ops();

    // Spawn producer thread — recv() is blocking so append must happen concurrently.
    let store_w = Arc::clone(&store);
    let coord_w = coord.clone();
    let producer = thread::spawn(move || {
        let payload = serde_json::json!({"hello": "world"});
        store_w.append(&coord_w, kind, &payload).expect("append");
    });

    let notif = ops.recv();
    assert!(
        notif.is_some(),
        "OPS RECV WITHOUT FILTERS: expected a notification, got None.\n\
         Check: src/store/subscription.rs SubscriptionOps::recv(), writer broadcast."
    );
    let notif = notif.expect("notification should be Some per preceding assert");
    assert_eq!(
        notif.coord.entity(),
        "entity:1",
        "OPS RECV WITHOUT FILTERS: notification entity mismatch."
    );
    assert_eq!(
        notif.kind, kind,
        "OPS RECV WITHOUT FILTERS: notification kind mismatch."
    );

    producer.join().expect("producer thread");
}

#[test]
fn ops_filter_passes_matching() {
    let (store, _dir) = test_store();
    let store = Arc::new(store);
    let coord = Coordinate::new("entity:1", "scope:test").expect("valid coord");
    let target_kind = EventKind::custom(0xF, 1);

    let region = Region::entity("entity:");
    let sub = store.subscribe_lossy(&region);
    let mut ops = sub
        .ops()
        .filter(move |n: &Notification| n.kind == target_kind);

    let store_w = Arc::clone(&store);
    let coord_w = coord.clone();
    let producer = thread::spawn(move || {
        let payload = serde_json::json!({"x": 1});
        // Append an event with the matching kind.
        store_w
            .append(&coord_w, target_kind, &payload)
            .expect("append matching");
    });

    let notif = ops.recv();
    assert!(
        notif.is_some(),
        "OPS FILTER PASSES MATCHING: filter should pass events with matching kind.\n\
         Check: src/store/subscription.rs SubscriptionOps::filter()."
    );
    let notif = notif.expect("notification should be Some per preceding assert");
    assert_eq!(
        notif.kind, target_kind,
        "OPS FILTER PASSES MATCHING: received event has wrong kind."
    );

    producer.join().expect("producer thread");
}

#[test]
fn ops_filter_rejects_non_matching() {
    let (store, _dir) = test_store();
    let store = Arc::new(store);
    let coord = Coordinate::new("entity:1", "scope:test").expect("valid coord");
    let wanted_kind = EventKind::custom(0xF, 1);
    let unwanted_kind = EventKind::custom(0xF, 2);

    let region = Region::entity("entity:");
    let sub = store.subscribe_lossy(&region);
    // Filter only passes wanted_kind. Take 1 so the test terminates.
    let mut ops = sub
        .ops()
        .filter(move |n: &Notification| n.kind == wanted_kind)
        .take(1);

    let store_w = Arc::clone(&store);
    let coord_w = coord.clone();
    let producer = thread::spawn(move || {
        let payload = serde_json::json!({"x": 1});
        // First: append non-matching event — should be rejected by filter.
        store_w
            .append(&coord_w, unwanted_kind, &payload)
            .expect("append unwanted");
        // Second: append matching event — should pass through.
        store_w
            .append(&coord_w, wanted_kind, &payload)
            .expect("append wanted");
    });

    let notif = ops.recv();
    assert!(
        notif.is_some(),
        "OPS FILTER REJECTS: should have received the matching event after the non-matching one."
    );
    let notif = notif.expect("notification should be Some per preceding assert");
    assert_eq!(
        notif.kind, wanted_kind,
        "OPS FILTER REJECTS: the non-matching event should have been filtered out,\n\
         but received kind {:?} instead of {:?}.",
        notif.kind, wanted_kind
    );

    producer.join().expect("producer thread");
}

#[test]
fn ops_take_limits_count() {
    let (store, _dir) = test_store();
    let store = Arc::new(store);
    let coord = Coordinate::new("entity:1", "scope:test").expect("valid coord");
    let kind = EventKind::custom(0xF, 1);

    let region = Region::entity("entity:");
    let sub = store.subscribe_lossy(&region);
    let mut ops = sub.ops().take(2);

    let store_w = Arc::clone(&store);
    let coord_w = coord.clone();
    let producer = thread::spawn(move || {
        let payload = serde_json::json!({"x": 1});
        for _ in 0..5 {
            store_w.append(&coord_w, kind, &payload).expect("append");
        }
    });

    // Should receive exactly 2.
    let first = ops.recv();
    assert!(first.is_some(), "OPS TAKE: first recv should return Some.");
    let second = ops.recv();
    assert!(
        second.is_some(),
        "OPS TAKE: second recv should return Some."
    );
    let third = ops.recv();
    assert!(
        third.is_none(),
        "OPS TAKE: third recv should return None after take(2), but got Some.\n\
         Check: src/store/subscription.rs SubscriptionOps::take() limit enforcement."
    );

    producer.join().expect("producer thread");
}

#[test]
fn ops_filter_and_take_combined() {
    let (store, _dir) = test_store();
    let store = Arc::new(store);
    let coord = Coordinate::new("entity:1", "scope:test").expect("valid coord");
    let wanted_kind = EventKind::custom(0xF, 1);
    let other_kind = EventKind::custom(0xF, 2);

    let region = Region::entity("entity:");
    let sub = store.subscribe_lossy(&region);
    // Filter for wanted_kind only, take 2.
    let mut ops = sub
        .ops()
        .filter(move |n: &Notification| n.kind == wanted_kind)
        .take(2);

    let store_w = Arc::clone(&store);
    let coord_w = coord.clone();
    let producer = thread::spawn(move || {
        let payload = serde_json::json!({"x": 1});
        // Interleave matching and non-matching events.
        store_w
            .append(&coord_w, other_kind, &payload)
            .expect("append other");
        store_w
            .append(&coord_w, wanted_kind, &payload)
            .expect("append wanted 1");
        store_w
            .append(&coord_w, other_kind, &payload)
            .expect("append other");
        store_w
            .append(&coord_w, wanted_kind, &payload)
            .expect("append wanted 2");
        store_w
            .append(&coord_w, wanted_kind, &payload)
            .expect("append wanted 3");
    });

    // Should receive exactly 2 matching events, skipping the non-matching ones.
    let first = ops.recv();
    assert!(
        first.is_some(),
        "OPS COMBINED: first recv should return Some."
    );
    let first = first.expect("first notification should be Some");
    assert_eq!(
        first.kind, wanted_kind,
        "OPS COMBINED: first event should be wanted_kind."
    );

    let second = ops.recv();
    assert!(
        second.is_some(),
        "OPS COMBINED: second recv should return Some."
    );
    assert_eq!(
        second.expect("second notification should be Some").kind,
        wanted_kind,
        "OPS COMBINED: second event should be wanted_kind."
    );

    let third = ops.recv();
    assert!(
        third.is_none(),
        "OPS COMBINED: third recv should return None (take(2) exhausted), but got Some.\n\
         Check: src/store/subscription.rs filter + take interaction."
    );

    producer.join().expect("producer thread");
}

// ===== Wave 3D: Subscription composition depth tests =====
// PROVES: LAW-004 (Composition Over Construction — ops chain correctly)
// DEFENDS: FM-009 (Polite Downgrade — map must not silently drop events)
// INVARIANTS: INV-STATE (subscription state machine: open → recv → closed)

#[test]
fn ops_map_transforms_notification() {
    let (store, _dir) = test_store();
    let store = Arc::new(store);
    let coord = Coordinate::new("entity:1", "scope:test").expect("valid coord");
    let kind = EventKind::custom(0xF, 1);
    let mapped_kind = EventKind::custom(0xF, 5);

    let region = Region::entity("entity:");
    let sub = store.subscribe_lossy(&region);
    // Map: change the kind field of every notification
    let mut ops = sub
        .ops()
        .map(move |n: &Notification| {
            Some(Notification {
                event_id: n.event_id,
                correlation_id: n.correlation_id,
                causation_id: n.causation_id,
                coord: n.coord.clone(),
                kind: mapped_kind,
                sequence: n.sequence,
            })
        })
        .take(1);

    let store_w = Arc::clone(&store);
    let coord_w = coord.clone();
    let producer = thread::spawn(move || {
        store_w
            .append(&coord_w, kind, &serde_json::json!({"x": 1}))
            .expect("append");
    });

    let notif = ops.recv();
    assert!(
        notif.is_some(),
        "OPS MAP: should receive a mapped notification."
    );
    assert_eq!(
        notif
            .expect("notification should be Some per preceding assert")
            .kind,
        mapped_kind,
        "OPS MAP: map should transform the notification kind.\n\
         Investigate: src/store/subscription.rs SubscriptionOps::map().\n\
         Common causes: map_fn not applied, original notification returned instead."
    );

    producer.join().expect("producer thread");
}

#[test]
fn ops_map_returning_none_skips_event() {
    let (store, _dir) = test_store();
    let store = Arc::new(store);
    let coord = Coordinate::new("entity:1", "scope:test").expect("valid coord");
    let skip_kind = EventKind::custom(0xF, 1);
    let pass_kind = EventKind::custom(0xF, 2);

    let region = Region::entity("entity:");
    let sub = store.subscribe_lossy(&region);
    // Map: return None for skip_kind (acts as filter), Some for pass_kind
    let mut ops = sub
        .ops()
        .map(move |n: &Notification| {
            if n.kind == skip_kind {
                None
            } else {
                Some(n.clone())
            }
        })
        .take(1);

    let store_w = Arc::clone(&store);
    let coord_w = coord.clone();
    let producer = thread::spawn(move || {
        // First event: skip_kind — map returns None, should be skipped
        store_w
            .append(&coord_w, skip_kind, &serde_json::json!({"skip": true}))
            .expect("append skip");
        // Second event: pass_kind — map returns Some, should pass through
        store_w
            .append(&coord_w, pass_kind, &serde_json::json!({"pass": true}))
            .expect("append pass");
    });

    let notif = ops.recv();
    assert!(
        notif.is_some(),
        "OPS MAP SKIP: should receive the pass_kind event."
    );
    assert_eq!(
        notif
            .expect("notification should be Some per preceding assert")
            .kind,
        pass_kind,
        "OPS MAP SKIP: map returning None should skip that event.\n\
         Investigate: src/store/subscription.rs SubscriptionOps::recv() map branch.\n\
         Common causes: None from map not triggering continue in recv loop."
    );

    producer.join().expect("producer thread");
}

#[test]
fn ops_multiple_filters_all_must_pass() {
    let (store, _dir) = test_store();
    let store = Arc::new(store);
    let coord = Coordinate::new("entity:multi", "scope:test").expect("valid coord");
    let kind_a = EventKind::custom(0xF, 1);
    let kind_b = EventKind::custom(0xF, 2);
    let kind_c = EventKind::custom(0xF, 3);

    let region = Region::entity("entity:");
    let sub = store.subscribe_lossy(&region);
    // Two independent filters: must be kind_a OR kind_b, AND must have sequence > 0
    // Only events passing BOTH filters are received.
    let mut ops = sub
        .ops()
        .filter(move |n: &Notification| n.kind == kind_a || n.kind == kind_b)
        .filter(move |n: &Notification| n.sequence > 0)
        .take(1);

    let store_w = Arc::clone(&store);
    let coord_w = coord.clone();
    let producer = thread::spawn(move || {
        // Event 1: kind_c — fails first filter
        store_w
            .append(&coord_w, kind_c, &serde_json::json!({"x": 1}))
            .expect("append kind_c");
        // Event 2: kind_a, sequence=1 — passes both filters
        store_w
            .append(&coord_w, kind_a, &serde_json::json!({"x": 2}))
            .expect("append kind_a");
    });

    let notif = ops.recv();
    assert!(
        notif.is_some(),
        "OPS MULTI FILTER: should receive kind_a event."
    );
    let notif = notif.expect("notification should be Some per preceding assert");
    assert_eq!(
        notif.kind, kind_a,
        "OPS MULTI FILTER: only kind_a/kind_b with sequence>0 should pass both filters.\n\
         Investigate: src/store/subscription.rs SubscriptionOps::recv() filter chain.\n\
         Common causes: filters short-circuiting, only first filter applied."
    );

    producer.join().expect("producer thread");
}

#[test]
fn ops_channel_closed_returns_none() {
    let (store, _dir) = test_store();
    let store = Arc::new(store);

    let region = Region::entity("entity:");
    let sub = store.subscribe_lossy(&region);
    let mut ops = sub.ops();

    // Close the store — this shuts down the writer, which closes broadcast channels
    let store_clone = Arc::into_inner(store).expect("Arc should have single owner at test end");
    store_clone.close().expect("close");

    // After channel closes, recv should return None
    let notif = ops.recv();
    assert!(
        notif.is_none(),
        "OPS CHANNEL CLOSED: recv should return None after store is closed.\n\
         Investigate: src/store/subscription.rs Subscription::recv() channel close path.\n\
         Common causes: recv blocking forever instead of returning None on closed channel."
    );
}