graphix-stdlib 0.3.1

A dataflow language for UIs and network programming, standard library
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
use crate::{
    run,
    test::{escape_path, init},
};
use anyhow::{bail, Result};
use arcstr::ArcStr;
use graphix_rt::GXEvent;
use netidx::subscriber::Value;
use poolshark::global::GPooled;
use tokio::{fs, sync::mpsc, time::Duration};

/// Macro to create fs::watch tests with common setup/teardown logic.
/// Supports both simple single-action tests and complex multi-event sequences.
macro_rules! watch_test {
    // Simple pattern: single action after establishment, boolean expectation
    // This delegates to the complex pattern with sensible defaults
    (
        name: $test_name:ident,
        interest: $interest:expr,
        setup: |$temp_dir:ident| $setup:block,
        action: |$action_dir:ident| $action:block,
        expect: $expect:expr
    ) => {
        watch_test! {
            name: $test_name,
            interest: $interest,
            timeout_secs: 2,
            setup: |$temp_dir| {
                $setup
                $temp_dir.path()
            },
            state: {
                _event_count: usize = 0,
            },
            on_event: |count, temp_dir, _event_count| {
                *_event_count = count;
                if count == 1 {
                    eprintln!("watch established, performing action");
                    let $action_dir = &temp_dir;
                    $action
                }
            },
            verify: {
                let got_event = _event_count > 1;
                assert_eq!(got_event, $expect,
                    "Expected event: {}, Got event: {}", $expect, got_event)
            }
        }
    };

    // Complex pattern: multi-event sequence with state tracking
    (
        name: $test_name:ident,
        interest: $interest:expr,
        timeout_secs: $timeout:expr,
        setup: |$setup_dir:ident| $setup:block,
        state: { $($state_name:ident: $state_type:ty = $state_init:expr),* $(,)? },
        on_event: |$event_count:ident, $event_dir:ident, $($state_param:ident),*| $on_event:block,
        verify: { $($verify:stmt);* $(;)? }
    ) => {
        #[tokio::test(flavor = "current_thread")]
        async fn $test_name() -> Result<()> {
            let (tx, mut rx) = mpsc::channel::<GPooled<Vec<GXEvent>>>(10);
            let ctx = init(tx).await?;
            let temp_dir = tempfile::tempdir()?;

            // Run setup
            let watch_path = {
                let $setup_dir = &temp_dir;
                $setup
            };

            // Start watching
            let code = format!(
                r#"fs::watch(#interest: {}, "{}")"#,
                $interest, escape_path(watch_path.display())
            );

            let compiled = ctx.rt.compile(ArcStr::from(code)).await?;
            let eid = compiled.exprs[0].id;

            let timeout = tokio::time::sleep(Duration::from_secs($timeout));
            tokio::pin!(timeout);
            let mut event_count = 0;
            $(let mut $state_name: $state_type = $state_init;)*

            loop {
                tokio::select! {
                    _ = &mut timeout => break,
                    Some(mut batch) = rx.recv() => {
                        for event in batch.drain(..) {
                            if let GXEvent::Updated(id, v) = event {
                                if id == eid && matches!(v, Value::String(_)) {
                                    event_count += 1;
                                    eprintln!("Event #{event_count}: {v}");

                                    let $event_count = event_count;
                                    let $event_dir = &temp_dir;
                                    $(let $state_param = &mut $state_param;)*
                                    $on_event
                                }
                            }
                        }
                    }
                }
            }

            $($verify;)*
            Ok(())
        }
    };
}

// Test file creation detection
watch_test! {
    name: test_watch_create_file,
    interest: "[`Established, `Create]",
    setup: |_temp_dir| {},
    action: |temp_dir| {
        let test_file = temp_dir.path().join("test_file.txt");
        fs::write(&test_file, b"hello").await?;
    },
    expect: true
}

// Test file modification detection
watch_test! {
    name: test_watch_modify_file,
    interest: "[`Established, `Modify]",
    setup: |temp_dir| {
        let test_file = temp_dir.path().join("test_file.txt");
        fs::write(&test_file, b"initial").await?;
    },
    action: |temp_dir| {
        let test_file = temp_dir.path().join("test_file.txt");
        fs::write(&test_file, b"modified content").await?;
    },
    expect: true
}

// Test file deletion detection
watch_test! {
    name: test_watch_delete_file,
    interest: "[`Established, `Delete]",
    setup: |temp_dir| {
        let test_file = temp_dir.path().join("test_file.txt");
        fs::write(&test_file, b"to be deleted").await?;
    },
    action: |temp_dir| {
        let test_file = temp_dir.path().join("test_file.txt");
        fs::remove_file(&test_file).await?;
    },
    expect: true
}

// Test interest filtering (should NOT detect events not matching interest)
watch_test! {
    name: test_watch_interest_filtering,
    interest: "[`Established, `Create]",
    setup: |temp_dir| {
        let test_file = temp_dir.path().join("test_file.txt");
        fs::write(&test_file, b"initial").await?;
    },
    action: |temp_dir| {
        let test_file = temp_dir.path().join("test_file.txt");
        fs::write(&test_file, b"modified").await?;
    },
    expect: false
}

// Test watching a non-existent file that gets created
watch_test! {
    name: test_watch_nonexistent_file_created,
    interest: "[`Create, `Established]",
    timeout_secs: 5,
    setup: |temp_dir| {
        let test_file = temp_dir.path().join("nonexistent.txt");
        let _ = std::fs::remove_file(&test_file);
        test_file
    },
    state: {
        file_created: bool = false,
        got_create: bool = false,
    },
    on_event: |count, temp_dir, file_created, got_create| {
        let test_file = temp_dir.path().join("nonexistent.txt");
        if count == 1 && !*file_created {
            eprintln!("Creating file");
            fs::write(&test_file, b"hello").await?;
            *file_created = true;
        } else if count >= 2 {
            *got_create = true;
        }
    },
    verify: {
        assert!(got_create, "Did not receive create event for non-existent file");
    }
}

// Test watching existing file, deleting it, then recreating it
watch_test! {
    name: test_watch_delete_then_recreate,
    interest: "[`Established, `Create, `Delete, `Modify]",
    timeout_secs: 6,
    setup: |temp_dir| {
        let test_file = temp_dir.path().join("delete_recreate.txt");
        fs::write(&test_file, b"initial").await?;
        test_file
    },
    state: {
        got_delete: bool = false,
        got_create: bool = false,
    },
    on_event: |count, temp_dir, got_delete, got_create| {
        let test_file = temp_dir.path().join("delete_recreate.txt");
        if count == 1 {
            eprintln!("Deleting file");
            fs::remove_file(&test_file).await?;
        } else if count == 2 {
            *got_delete = true;
            eprintln!("Recreating file");
            tokio::time::sleep(Duration::from_millis(100)).await;
            fs::write(&test_file, b"recreated").await?;
        } else if count == 3 {
            *got_create = true;
        }
    },
    verify: {
        assert!(got_delete, "Did not receive delete event");
        assert!(got_create, "Did not receive create event after recreation");
    }
}

// Test renaming parent directory
watch_test! {
    name: test_watch_parent_rename,
    interest: "[`Established, `Delete, `Create]",
    timeout_secs: 4,
    setup: |temp_dir| {
        let parent_dir = temp_dir.path().join("parent");
        fs::create_dir(&parent_dir).await?;
        let test_file = parent_dir.join("file.txt");
        fs::write(&test_file, b"content").await?;
        test_file
    },
    state: {
        got_delete: bool = false,
    },
    on_event: |count, temp_dir, got_delete| {
        if count == 1 {
            let parent_dir = temp_dir.path().join("parent");
            let new_parent = temp_dir.path().join("parent_renamed");
            eprintln!("Renaming parent directory");
            fs::rename(&parent_dir, &new_parent).await?;
        } else {
            *got_delete = true;
        }
    },
    verify: {
        assert!(got_delete, "Did not receive delete event after parent rename");
    }
}

// Test multi-level parent creation
watch_test! {
    name: test_watch_multilevel_parent_creation,
    interest: "[`Established, `Create]",
    timeout_secs: 5,
    setup: |temp_dir| {
        let deep_file = temp_dir.path().join("a").join("b").join("c").join("file.txt");
        let _ = fs::remove_dir_all(temp_dir.path().join("a")).await;
        deep_file
    },
    state: {
        got_create: bool = false,
    },
    on_event: |count, temp_dir, got_create| {
        let deep_file = temp_dir.path().join("a").join("b").join("c").join("file.txt");
        if count == 1 {
            eprintln!("Creating /a");
            fs::create_dir(temp_dir.path().join("a")).await?;
            tokio::time::sleep(Duration::from_millis(100)).await;

            eprintln!("Creating /a/b");
            fs::create_dir(temp_dir.path().join("a/b")).await?;
            tokio::time::sleep(Duration::from_millis(100)).await;

            eprintln!("Creating /a/b/c");
            fs::create_dir(temp_dir.path().join("a/b/c")).await?;
            tokio::time::sleep(Duration::from_millis(100)).await;

            eprintln!("Creating file");
            fs::write(&deep_file, b"deep content").await?;
        } else {
            *got_create = true;
        }
    },
    verify: {
        assert!(got_create, "Did not receive create event for deep file");
    }
}

// Test deep parent rename (rename two levels up)
// This isn't supported on windows
#[cfg(unix)]
watch_test! {
    name: test_watch_deep_parent_rename,
    interest: "[`Established, `Delete]",
    timeout_secs: 4,
    setup: |temp_dir| {
        let d = temp_dir.path().join("a").join("b").join("c").join("d");
        fs::create_dir_all(&d).await?;
        let test_file = d.join("file.txt");
        fs::write(&test_file, b"content").await?;
        eprintln!("test file: {}", test_file.display());
        test_file
    },
    state: {
        got_delete: bool = false,
    },
    on_event: |count, temp_dir, got_delete| {
        if count == 1 {
            let a = temp_dir.path().join("a");
            let b = a.join("b");
            let b2 = a.join("b2");
            eprintln!("Renaming /a/b to /a/b2 (two levels up)");
            eprintln!("rename {} to {}", b.display(), b2.display());
            fs::rename(&b, &b2).await?;
        } else {
            *got_delete = true;
        }
    },
    verify: {
        assert!(got_delete, "Did not receive delete event after deep parent rename");
    }
}

// Test race with parent deletion
watch_test! {
    name: test_watch_parent_tree_deletion,
    interest: "[`Established, `Delete]",
    timeout_secs: 4,
    setup: |temp_dir| {
        let b = temp_dir.path().join("a").join("b");
        fs::create_dir_all(&b).await?;
        let test_file = b.join("file.txt");
        fs::write(&test_file, b"content").await?;
        test_file
    },
    state: {
        got_delete: bool = false,
    },
    on_event: |count, temp_dir, got_delete| {
        if count == 1 {
            let a = temp_dir.path().join("a");
            eprintln!("Deleting entire /a directory tree");
            fs::remove_dir_all(&a).await?;
        } else {
            *got_delete = true;
        }
    },
    verify: {
        assert!(got_delete, "Did not receive delete event after parent tree deletion");
    }
}

// Test multiple watches on related paths
#[tokio::test(flavor = "current_thread")]
async fn test_watch_multiple_related_paths() -> Result<()> {
    let (tx, mut rx) = mpsc::channel::<GPooled<Vec<GXEvent>>>(10);
    let ctx = init(tx).await?;
    let temp_dir = tempfile::tempdir()?;

    let file1 = temp_dir.path().join("a").join("b").join("file.txt");
    let file2 = temp_dir.path().join("a").join("b").join("c").join("file.txt");

    // Ensure nothing exists
    let _ = fs::remove_dir_all(temp_dir.path().join("a")).await;

    // Watch both files
    let code1 = format!(
        r#"fs::watch(#interest: [`Established, `Create], "{}")"#,
        escape_path(file1.display())
    );
    let code2 = format!(
        r#"fs::watch(#interest: [`Established, `Create], "{}")"#,
        escape_path(file2.display())
    );

    let compiled1 = ctx.rt.compile(ArcStr::from(code1)).await?;
    let compiled2 = ctx.rt.compile(ArcStr::from(code2)).await?;
    let eid1 = compiled1.exprs[0].id;
    let eid2 = compiled2.exprs[0].id;

    let timeout = tokio::time::sleep(Duration::from_secs(10));
    tokio::pin!(timeout);
    let mut watch1_ready = false;
    let mut watch2_ready = false;
    let mut got_create_file1 = false;
    let mut got_create_file2 = false;

    loop {
        tokio::select! {
            _ = &mut timeout => break,
            Some(mut batch) = rx.recv() => {
                for event in batch.drain(..) {
                    if let GXEvent::Updated(id, v) = event {
                        if matches!(v, Value::String(_)) {
                            eprintln!("Event for {}: {v}", id.inner());

                            if id == eid1 && !watch1_ready {
                                watch1_ready = true;
                                eprintln!("Watch 1 ready");
                            } else if id == eid2 && !watch2_ready {
                                watch2_ready = true;
                                eprintln!("Watch 2 ready");
                            } else if id == eid1 {
                                got_create_file1 = true;
                            } else if id == eid2 {
                                got_create_file2 = true;
                            }

                            if watch1_ready && watch2_ready && !got_create_file1 && !got_create_file2 {
                                // Both watches ready, create only file2
                                eprintln!("Creating deep file only (file2)");
                                fs::create_dir_all(temp_dir.path().join("a/b/c")).await?;
                                fs::write(&file2, b"deep").await?;
                            }
                        }
                    }
                }
            }
        }
    }

    assert!(!got_create_file1, "Should not get create event for file1");
    assert!(got_create_file2, "Should get create event for file2");
    Ok(())
}

// Test established -> pending transition -> established
watch_test! {
    name: test_watch_established_to_pending,
    interest: "[`Delete, `Create, `Established]",
    timeout_secs: 4,
    setup: |temp_dir| {
        let subdir = temp_dir.path().join("subdir");
        fs::create_dir(&subdir).await?;
        let test_file = subdir.join("file.txt");
        fs::write(&test_file, b"content").await?;
        test_file
    },
    state: {
        got_established: bool = false,
        got_delete: bool = false,
        got_create: bool = false,
    },
    on_event: |count, temp_dir, got_established, got_delete, got_create| {
        let subdir = temp_dir.path().join("subdir");
        if count == 1 {
            *got_established = true;
            eprintln!("Deleting parent directory");
            fs::remove_dir_all(&subdir).await?;
            tokio::time::sleep(Duration::from_millis(100)).await;
        } else if count == 2 {
            eprintln!("Parent directory deleted, recreating");
            *got_delete = true;
            fs::create_dir(&subdir).await?;
            fs::write(&subdir.join("file.txt"), b"content").await?
        } else {
            *got_create = true;
        }
    },
    verify: {
        assert!(got_established, "Did not receive Established event");
        assert!(got_delete, "Did not receive Delete event after parent deletion");
        assert!(got_create, "Did not receive Create event after recreation");
    }
}

// Test file -> directory transition
watch_test! {
    name: test_watch_file_to_directory,
    interest: "[`Established, `Delete, `Create]",
    timeout_secs: 4,
    setup: |temp_dir| {
        let path = temp_dir.path().join("transform");
        fs::write(&path, b"file content").await?;
        path
    },
    state: {
        got_delete: bool = false,
        got_create: bool = false,
    },
    on_event: |count, temp_dir, got_delete, got_create| {
        let path = temp_dir.path().join("transform");
        if count == 1 {
            eprintln!("Deleting file and creating directory");
            fs::remove_file(&path).await?;
            tokio::time::sleep(Duration::from_millis(100)).await;
            fs::create_dir(&path).await?;
        } else if count == 2 {
            *got_delete = true;
        } else if count == 3 {
            *got_create = true;
        }
    },
    verify: {
        assert!(got_delete, "Did not receive delete event");
        assert!(got_create, "Did not receive create event for directory");
    }
}

// Test symlink with non-existent target
#[cfg(unix)]
watch_test! {
    name: test_watch_symlink_nonexistent_target,
    interest: "[`Established, `Create, `Modify]",
    timeout_secs: 4,
    setup: |temp_dir| {
        use std::os::unix::fs::symlink;
        let target = temp_dir.path().join("target.txt");
        let link = temp_dir.path().join("link.txt");
        symlink(&target, &link).unwrap();
        link
    },
    state: {
        got_event: bool = false,
    },
    on_event: |count, temp_dir, got_event| {
        let target = temp_dir.path().join("target.txt");
        if count == 1 {
            eprintln!("Creating symlink target");
            fs::write(&target, b"target content").await?;
        } else {
            *got_event = true;
        }
    },
    verify: {
        assert!(got_event, "Did not receive event when symlink target was created");
    }
}

// Test deleting and recreating symlink target (watches resolve through symlinks)
#[cfg(unix)]
watch_test! {
    name: test_watch_symlink_recreate,
    interest: "[`Established, `Delete, `Create]",
    timeout_secs: 4,
    setup: |temp_dir| {
        use std::os::unix::fs::symlink;
        let target = temp_dir.path().join("target.txt");
        let link = temp_dir.path().join("link.txt");
        fs::write(&target, b"content").await?;
        symlink(&target, &link)?;
        link
    },
    state: {
        got_delete: bool = false,
        got_create: bool = false,
    },
    on_event: |count, temp_dir, got_delete, got_create| {
        let target = temp_dir.path().join("target.txt");
        if count == 1 {
            eprintln!("Deleting target");
            fs::remove_file(&target).await?;
            tokio::time::sleep(Duration::from_millis(500)).await;
            eprintln!("Recreating target");
            fs::write(&target, b"content").await?;
        } else if count == 2 {
            *got_delete = true;
        } else if count == 3 {
            *got_create = true;
        }
    },
    verify: {
        assert!(got_delete, "Did not receive delete event for target");
        assert!(got_create, "Did not receive create event for target");
    }
}

// Test SetGlobals - disable polling
run!(
    test_watch_set_globals_disable_polling,
    r#"fs::set_global_watch_parameters(#poll_batch_size: 0, #poll_interval: duration:1.s)"#,
    |v: Result<&Value>| { matches!(v, Ok(Value::Null)) }
);