graphrefly-operators 0.0.7

Built-in operator node types for GraphReFly (map, filter, scan, switchMap, valve, gate, retry, …)
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
//! Integration tests for multi-dep combinator operators (Slice C-2, D020).
//!
//! Covers: `combine` (combineLatest), `with_latest_from`, `merge`.

mod common;

use common::{OpRuntime, RecordedEvent, TestValue};
use graphrefly_operators::combine::{combine, merge, with_latest_from};

// =====================================================================
// combine (combineLatest)
// =====================================================================

#[test]
fn combine_emits_tuple_on_any_dep_fire() {
    let rt = OpRuntime::new();
    let a = rt.state_int(Some(1));
    let b = rt.state_int(Some(2));

    let c = combine(rt.core(), &rt.op_binding, &[a, b], rt.make_packer()).unwrap();
    let rec = rt.subscribe_recorder(c.node);

    // Both deps have initial values → first-run gate satisfied at subscribe →
    // push-on-subscribe delivers combined tuple.
    let data = rec.data_values();
    assert_eq!(data.len(), 1);
    assert_eq!(
        data[0],
        TestValue::Tuple(vec![TestValue::Int(1), TestValue::Int(2)])
    );

    // Emit on dep a → new tuple.
    rec.clear();
    rt.emit_int(a, 10);
    let data = rec.data_values();
    assert_eq!(data.len(), 1);
    assert_eq!(
        data[0],
        TestValue::Tuple(vec![TestValue::Int(10), TestValue::Int(2)])
    );

    // Emit on dep b → new tuple with latest a.
    rec.clear();
    rt.emit_int(b, 20);
    let data = rec.data_values();
    assert_eq!(data.len(), 1);
    assert_eq!(
        data[0],
        TestValue::Tuple(vec![TestValue::Int(10), TestValue::Int(20)])
    );
}

#[test]
fn combine_first_run_gate_holds_until_all_deps_fire() {
    let rt = OpRuntime::new();
    // Create sources without initial values (sentinel).
    let a = rt.state_int(None);
    let b = rt.state_int(None);

    let c = combine(rt.core(), &rt.op_binding, &[a, b], rt.make_packer()).unwrap();
    let rec = rt.subscribe_recorder(c.node);

    // No data yet — gate holds.
    assert!(rec.data_values().is_empty());

    // Emit only a — gate still holds (b is sentinel).
    rt.emit_int(a, 1);
    assert!(rec.data_values().is_empty());

    // Emit b — gate releases, tuple emitted.
    rt.emit_int(b, 2);
    let data = rec.data_values();
    assert_eq!(data.len(), 1);
    assert_eq!(
        data[0],
        TestValue::Tuple(vec![TestValue::Int(1), TestValue::Int(2)])
    );
}

#[test]
fn combine_3_deps() {
    let rt = OpRuntime::new();
    let a = rt.state_int(Some(1));
    let b = rt.state_int(Some(2));
    let c = rt.state_int(Some(3));

    let combined = combine(rt.core(), &rt.op_binding, &[a, b, c], rt.make_packer()).unwrap();
    let rec = rt.subscribe_recorder(combined.node);

    let data = rec.data_values();
    assert_eq!(data.len(), 1);
    assert_eq!(
        data[0],
        TestValue::Tuple(vec![
            TestValue::Int(1),
            TestValue::Int(2),
            TestValue::Int(3)
        ])
    );

    rec.clear();
    rt.emit_int(b, 20);
    let data = rec.data_values();
    assert_eq!(
        data[0],
        TestValue::Tuple(vec![
            TestValue::Int(1),
            TestValue::Int(20),
            TestValue::Int(3)
        ])
    );
}

#[test]
fn combine_complete_when_all_deps_complete() {
    let rt = OpRuntime::new();
    let a = rt.state_int(Some(1));
    let b = rt.state_int(Some(2));

    let c = combine(rt.core(), &rt.op_binding, &[a, b], rt.make_packer()).unwrap();
    let rec = rt.subscribe_recorder(c.node);
    rec.clear();

    // Complete a — combine doesn't complete yet.
    rt.core().complete(a);
    assert!(!rec.events().contains(&RecordedEvent::Complete));

    // Complete b — now combine completes (R1.3.4.b).
    rt.core().complete(b);
    assert!(rec.events().contains(&RecordedEvent::Complete));
}

// =====================================================================
// with_latest_from
// =====================================================================

#[test]
fn with_latest_from_emits_only_on_primary() {
    let rt = OpRuntime::new();
    let primary = rt.state_int(Some(1));
    let secondary = rt.state_int(Some(2));

    let wlf = with_latest_from(
        rt.core(),
        &rt.op_binding,
        primary,
        secondary,
        rt.make_packer(),
    );
    let rec = rt.subscribe_recorder(wlf.node);

    // Push-on-subscribe: both have values, gate satisfied → pair emitted.
    let data = rec.data_values();
    assert_eq!(data.len(), 1);
    assert_eq!(
        data[0],
        TestValue::Tuple(vec![TestValue::Int(1), TestValue::Int(2)])
    );

    // Emit on secondary only → RESOLVED, no new data.
    rec.clear();
    rt.emit_int(secondary, 20);
    assert!(rec.data_values().is_empty());
    assert!(rec.events().contains(&RecordedEvent::Resolved));

    // Emit on primary → pair with latest secondary.
    rec.clear();
    rt.emit_int(primary, 10);
    let data = rec.data_values();
    assert_eq!(data.len(), 1);
    assert_eq!(
        data[0],
        TestValue::Tuple(vec![TestValue::Int(10), TestValue::Int(20)])
    );
}

#[test]
fn with_latest_from_gate_holds_until_both_deliver() {
    let rt = OpRuntime::new();
    let primary = rt.state_int(None);
    let secondary = rt.state_int(None);

    let wlf = with_latest_from(
        rt.core(),
        &rt.op_binding,
        primary,
        secondary,
        rt.make_packer(),
    );
    let rec = rt.subscribe_recorder(wlf.node);

    // No data — gate holds.
    assert!(rec.data_values().is_empty());

    // Emit primary only — gate holds (secondary sentinel).
    rt.emit_int(primary, 1);
    assert!(rec.data_values().is_empty());

    // Emit secondary — gate releases (both deps now have values).
    // First-fire special case: gate release always emits regardless of
    // which dep triggered the wave, since the gate guarantees both have
    // real values.
    rt.emit_int(secondary, 2);
    let data = rec.data_values();
    assert_eq!(data.len(), 1);
    assert_eq!(
        data[0],
        TestValue::Tuple(vec![TestValue::Int(1), TestValue::Int(2)])
    );

    // Subsequent secondary-only fire → no emission (fire-on-primary-only).
    rec.clear();
    rt.emit_int(secondary, 20);
    assert!(rec.data_values().is_empty());

    // Primary fires → pair with latest secondary.
    rt.emit_int(primary, 10);
    let data = rec.data_values();
    assert_eq!(data.len(), 1);
    assert_eq!(
        data[0],
        TestValue::Tuple(vec![TestValue::Int(10), TestValue::Int(20)])
    );
}

#[test]
fn with_latest_from_secondary_update_samples_latest() {
    let rt = OpRuntime::new();
    let primary = rt.state_int(Some(1));
    let secondary = rt.state_int(Some(100));

    let wlf = with_latest_from(
        rt.core(),
        &rt.op_binding,
        primary,
        secondary,
        rt.make_packer(),
    );
    let rec = rt.subscribe_recorder(wlf.node);
    rec.clear();

    // Update secondary multiple times.
    rt.emit_int(secondary, 200);
    rt.emit_int(secondary, 300);

    // Now fire primary — should sample latest secondary (300).
    rt.emit_int(primary, 5);
    let data = rec.data_values();
    assert_eq!(data.len(), 1);
    assert_eq!(
        data[0],
        TestValue::Tuple(vec![TestValue::Int(5), TestValue::Int(300)])
    );
}

#[test]
fn with_latest_from_complete_when_all_deps_complete() {
    let rt = OpRuntime::new();
    let primary = rt.state_int(Some(1));
    let secondary = rt.state_int(Some(2));

    let wlf = with_latest_from(
        rt.core(),
        &rt.op_binding,
        primary,
        secondary,
        rt.make_packer(),
    );
    let rec = rt.subscribe_recorder(wlf.node);
    rec.clear();

    rt.core().complete(primary);
    assert!(!rec.events().contains(&RecordedEvent::Complete));

    rt.core().complete(secondary);
    assert!(rec.events().contains(&RecordedEvent::Complete));
}

// =====================================================================
// merge
// =====================================================================

#[test]
fn merge_forwards_all_dep_data_verbatim() {
    let rt = OpRuntime::new();
    let a = rt.state_int(Some(1));
    let b = rt.state_int(Some(2));

    let m = merge(rt.core(), &[a, b]).unwrap();
    let rec = rt.subscribe_recorder(m.node);

    // Push-on-subscribe: merge is partial:true, so fires on first dep.
    // Both deps deliver sequentially on subscribe (N separate waves per
    // R2.7.5). Merge forwards each.
    let data = rec.data_values();
    assert_eq!(data.len(), 2);
    assert_eq!(data[0], TestValue::Int(1));
    assert_eq!(data[1], TestValue::Int(2));

    // Emit on a → forwarded.
    rec.clear();
    rt.emit_int(a, 10);
    let data = rec.data_values();
    assert_eq!(data.len(), 1);
    assert_eq!(data[0], TestValue::Int(10));

    // Emit on b → forwarded.
    rec.clear();
    rt.emit_int(b, 20);
    let data = rec.data_values();
    assert_eq!(data.len(), 1);
    assert_eq!(data[0], TestValue::Int(20));
}

#[test]
fn merge_zero_ffi_no_binding_call() {
    // Merge doesn't call any FFI method (project/predicate/fold/pack) —
    // it's pure handle forwarding. We verify by checking that the binding
    // doesn't panic (since the default impls panic for unregistered ops).
    let rt = OpRuntime::new();
    let a = rt.state_int(Some(1));
    let b = rt.state_int(Some(2));

    let m = merge(rt.core(), &[a, b]).unwrap();
    let _rec = rt.subscribe_recorder(m.node);

    // If any FFI method were called, InnerBinding would unreachable! on
    // invoke_fn. Reaching here proves zero FFI on the fire path.
    rt.emit_int(a, 100);
    rt.emit_int(b, 200);
}

#[test]
fn merge_complete_when_all_deps_complete() {
    let rt = OpRuntime::new();
    let a = rt.state_int(Some(1));
    let b = rt.state_int(Some(2));
    let c = rt.state_int(Some(3));

    let m = merge(rt.core(), &[a, b, c]).unwrap();
    let rec = rt.subscribe_recorder(m.node);
    rec.clear();

    rt.core().complete(a);
    assert!(!rec.events().contains(&RecordedEvent::Complete));

    rt.core().complete(b);
    assert!(!rec.events().contains(&RecordedEvent::Complete));

    rt.core().complete(c);
    assert!(rec.events().contains(&RecordedEvent::Complete));
}

#[test]
fn merge_error_cascades_when_all_deps_terminal() {
    // Rust merge uses Core's standard dep-terminal cascade (R1.3.4.b):
    // error propagates when ALL deps are terminal, ERROR dominates.
    // This differs from TS merge (first-error-terminates via producer
    // pattern). Documented in porting-deferred.md.
    let rt = OpRuntime::new();
    let a = rt.state_int(Some(1));
    let b = rt.state_int(Some(2));

    let m = merge(rt.core(), &[a, b]).unwrap();
    let rec = rt.subscribe_recorder(m.node);
    rec.clear();

    let err_h = rt.intern(TestValue::Str("boom".into()));
    rt.core().error(a, err_h);
    // Only dep a is terminal — merge doesn't cascade yet.
    assert!(!rec
        .events()
        .iter()
        .any(|e| matches!(e, RecordedEvent::Error(_))));

    // Complete dep b → all deps terminal, ERROR dominates → merge errors.
    rt.core().complete(b);
    assert!(rec
        .events()
        .iter()
        .any(|e| matches!(e, RecordedEvent::Error(_))));
}

#[test]
fn merge_partial_mode_fires_on_first_dep() {
    // Merge uses partial:true — fires as soon as any dep delivers.
    let rt = OpRuntime::new();
    let a = rt.state_int(None); // sentinel
    let b = rt.state_int(None); // sentinel

    let m = merge(rt.core(), &[a, b]).unwrap();
    let rec = rt.subscribe_recorder(m.node);

    // No data yet — both sentinel.
    assert!(rec.data_values().is_empty());

    // Emit on b only → merge fires immediately (partial:true).
    rt.emit_int(b, 42);
    let data = rec.data_values();
    assert_eq!(data.len(), 1);
    assert_eq!(data[0], TestValue::Int(42));
}

#[test]
fn merge_many_sources() {
    let rt = OpRuntime::new();
    let sources: Vec<_> = (0..5).map(|i| rt.state_int(Some(i))).collect();

    let m = merge(rt.core(), &sources).unwrap();
    let rec = rt.subscribe_recorder(m.node);

    // Push-on-subscribe delivers each source's initial value.
    let data = rec.data_values();
    assert_eq!(data.len(), 5);
    for (i, datum) in data.iter().enumerate() {
        assert_eq!(*datum, TestValue::Int(i as i64));
    }
}