hypen-engine 0.4.84

A Rust implementation of the Hypen engine
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
//! Tests for src/reactive/graph.rs - Dependency tracking
//!
//! Tests the dependency graph that tracks which nodes depend on which state paths

use hypen_engine::ir::NodeId;
use hypen_engine::reactive::{Binding, DependencyGraph};
use slotmap::SlotMap;
use std::sync::Mutex;

// Helper to create unique test NodeIds
lazy_static::lazy_static! {
    static ref NODE_POOL: Mutex<SlotMap<NodeId, ()>> = Mutex::new(SlotMap::with_key());
}

fn node(_n: u32) -> NodeId {
    // Create unique NodeIds using a shared SlotMap
    let mut pool = NODE_POOL.lock().unwrap();
    pool.insert(())
}

// ============================================================================
// Dependency Tracking: Add, Get, Remove (6 tests)
// ============================================================================

#[test]
fn test_add_dependency_basic() {
    // GIVEN: Empty dependency graph
    let mut graph = DependencyGraph::new();
    let node_id = node(1);
    let binding = Binding::state(vec!["user".to_string(), "name".to_string()]);

    // WHEN: Add dependency
    graph.add_dependency(node_id, &binding,
        None,
);

    // THEN: Dependency recorded
    let nodes = graph.get_dependent_nodes("user.name");
    assert!(nodes.is_some());
    assert_eq!(nodes.unwrap().len(), 1);
    assert!(nodes.unwrap().contains(&node_id));
}

#[test]
fn test_add_multiple_nodes_same_path() {
    // GIVEN: Dependency graph
    let mut graph = DependencyGraph::new();
    let node1 = node(1);
    let node2 = node(2);
    let node3 = node(3);
    let binding = Binding::state(vec!["count".to_string()]);

    // WHEN: Multiple nodes depend on same path
    graph.add_dependency(node1, &binding,
        None,
);
    graph.add_dependency(node2, &binding,
        None,
);
    graph.add_dependency(node3, &binding,
        None,
);

    // THEN: All nodes tracked
    let nodes = graph.get_dependent_nodes("count");
    assert!(nodes.is_some());
    assert_eq!(nodes.unwrap().len(), 3);
}

#[test]
fn test_add_single_node_multiple_paths() {
    // GIVEN: Dependency graph
    let mut graph = DependencyGraph::new();
    let node_id = node(1);

    // WHEN: Single node depends on multiple paths
    graph.add_dependency(node_id, &Binding::state(vec!["firstName".to_string()]),
        None,
);
    graph.add_dependency(node_id, &Binding::state(vec!["lastName".to_string()]),
        None,
);
    graph.add_dependency(node_id, &Binding::state(vec!["email".to_string()]),
        None,
);

    // THEN: Node tracked for all paths
    assert!(graph
        .get_dependent_nodes("firstName")
        .unwrap()
        .contains(&node_id));
    assert!(graph
        .get_dependent_nodes("lastName")
        .unwrap()
        .contains(&node_id));
    assert!(graph
        .get_dependent_nodes("email")
        .unwrap()
        .contains(&node_id));
}

#[test]
fn test_get_dependent_nodes_nonexistent_path() {
    // GIVEN: Graph with some dependencies
    let mut graph = DependencyGraph::new();
    let node_id = node(1);
    graph.add_dependency(node_id, &Binding::state(vec!["user".to_string()]),
        None,
);

    // WHEN: Query nonexistent path
    let nodes = graph.get_dependent_nodes("nonexistent");

    // THEN: Returns None
    assert!(nodes.is_none());
}

#[test]
fn test_remove_node_clears_all_dependencies() {
    // GIVEN: Node with multiple dependencies
    let mut graph = DependencyGraph::new();
    let node_id = node(1);

    graph.add_dependency(
        node_id,
        &Binding::state(vec!["user".to_string(), "name".to_string()]), None,
    );
    graph.add_dependency(
        node_id,
        &Binding::state(vec!["user".to_string(), "email".to_string()]), None,
    );
    graph.add_dependency(node_id, &Binding::state(vec!["settings".to_string()]),
        None,
);

    // Verify dependencies exist
    assert!(graph
        .get_dependent_nodes("user.name")
        .unwrap()
        .contains(&node_id));
    assert!(graph
        .get_dependent_nodes("user.email")
        .unwrap()
        .contains(&node_id));
    assert!(graph
        .get_dependent_nodes("settings")
        .unwrap()
        .contains(&node_id));

    // WHEN: Remove node
    graph.remove_node(node_id);

    // THEN: All dependencies cleared
    assert!(graph
        .get_dependent_nodes("user.name")
        .map_or(true, |n| !n.contains(&node_id)));
    assert!(graph
        .get_dependent_nodes("user.email")
        .map_or(true, |n| !n.contains(&node_id)));
    assert!(graph
        .get_dependent_nodes("settings")
        .map_or(true, |n| !n.contains(&node_id)));
}

#[test]
fn test_remove_node_preserves_other_nodes() {
    // GIVEN: Multiple nodes with same dependency
    let mut graph = DependencyGraph::new();
    let node1 = node(1);
    let node2 = node(2);
    let binding = Binding::state(vec!["shared".to_string()]);

    graph.add_dependency(node1, &binding,
        None,
);
    graph.add_dependency(node2, &binding,
        None,
);

    // WHEN: Remove one node
    graph.remove_node(node1);

    // THEN: Other node preserved
    let nodes = graph.get_dependent_nodes("shared").unwrap();
    assert!(!nodes.contains(&node1));
    assert!(nodes.contains(&node2));
    assert_eq!(nodes.len(), 1);
}

// ============================================================================
// Path Affectedness: Parent/Child Changes (5 tests)
// ============================================================================

#[test]
fn test_get_affected_nodes_exact_match() {
    // GIVEN: Node depends on "user.name"
    let mut graph = DependencyGraph::new();
    let node_id = node(1);
    graph.add_dependency(
        node_id,
        &Binding::state(vec!["user".to_string(), "name".to_string()]), None,
    );

    // WHEN: "user.name" changes
    let affected = graph.get_affected_nodes("user.name");

    // THEN: Node is affected
    assert_eq!(affected.len(), 1);
    assert!(affected.contains(&node_id));
}

#[test]
fn test_get_affected_nodes_parent_changed() {
    // GIVEN: Node depends on "user.profile.name"
    let mut graph = DependencyGraph::new();
    let node_id = node(1);
    graph.add_dependency(
        node_id,
        &Binding::state(vec![
            "user".to_string(),
            "profile".to_string(),
            "name".to_string(),
        ]), None,
    );

    // WHEN: Parent "user" changes
    let affected = graph.get_affected_nodes("user");

    // THEN: Child node is affected
    assert_eq!(affected.len(), 1);
    assert!(affected.contains(&node_id));
}

#[test]
fn test_get_affected_nodes_child_changed() {
    // GIVEN: Node depends on parent "user"
    let mut graph = DependencyGraph::new();
    let node_id = node(1);
    graph.add_dependency(node_id, &Binding::state(vec!["user".to_string()]),
        None,
);

    // WHEN: Child "user.email" changes
    let affected = graph.get_affected_nodes("user.email");

    // THEN: Parent node is affected
    assert_eq!(affected.len(), 1);
    assert!(affected.contains(&node_id));
}

#[test]
fn test_get_affected_nodes_unrelated_path() {
    // GIVEN: Node depends on "user.name"
    let mut graph = DependencyGraph::new();
    let node_id = node(1);
    graph.add_dependency(
        node_id,
        &Binding::state(vec!["user".to_string(), "name".to_string()]), None,
    );

    // WHEN: Unrelated "settings" changes
    let affected = graph.get_affected_nodes("settings");

    // THEN: No nodes affected
    assert_eq!(affected.len(), 0);
}

#[test]
fn test_get_affected_nodes_similar_but_different_paths() {
    // GIVEN: Node depends on "user"
    let mut graph = DependencyGraph::new();
    let node_id = node(1);
    graph.add_dependency(node_id, &Binding::state(vec!["user".to_string()]),
        None,
);

    // WHEN: Similar but different "username" changes
    let affected = graph.get_affected_nodes("username");

    // THEN: Node NOT affected (not a child path)
    assert_eq!(affected.len(), 0);
}

// ============================================================================
// Clear and Bulk Operations (4 tests)
// ============================================================================

#[test]
fn test_clear_removes_all_dependencies() {
    // GIVEN: Graph with many dependencies
    let mut graph = DependencyGraph::new();

    for i in 0..10 {
        let node_id = node(i);
        graph.add_dependency(node_id, &Binding::state(vec![format!("path{}", i)]),
        None,
);
    }

    // Verify dependencies exist
    assert!(graph.get_dependent_nodes("path0").is_some());
    assert!(graph.get_dependent_nodes("path5").is_some());

    // WHEN: Clear graph
    graph.clear();

    // THEN: All dependencies removed
    assert!(graph.get_dependent_nodes("path0").is_none());
    assert!(graph.get_dependent_nodes("path5").is_none());
    assert!(graph.get_dependent_nodes("path9").is_none());
}

#[test]
fn test_affected_nodes_with_multiple_dependencies() {
    // GIVEN: Multiple nodes with nested dependencies
    let mut graph = DependencyGraph::new();
    let node1 = node(1); // Depends on "user"
    let node2 = node(2); // Depends on "user.name"
    let node3 = node(3); // Depends on "user.profile.avatar"

    graph.add_dependency(node1, &Binding::state(vec!["user".to_string()]),
        None,
);
    graph.add_dependency(
        node2,
        &Binding::state(vec!["user".to_string(), "name".to_string()]), None,
    );
    graph.add_dependency(
        node3,
        &Binding::state(vec![
            "user".to_string(),
            "profile".to_string(),
            "avatar".to_string(),
        ]), None,
    );

    // WHEN: "user.profile" changes
    let affected = graph.get_affected_nodes("user.profile");

    // THEN: Both parent (user) and child (user.profile.avatar) affected
    assert_eq!(affected.len(), 2); // node1 (user) and node3 (user.profile.avatar)
    assert!(affected.contains(&node1));
    assert!(!affected.contains(&node2)); // user.name is sibling, not affected
    assert!(affected.contains(&node3));
}

#[test]
fn test_dependency_graph_default() {
    // GIVEN: Default constructor
    let graph = DependencyGraph::default();

    // WHEN: Query any path
    let nodes = graph.get_dependent_nodes("anything");

    // THEN: Empty graph
    assert!(nodes.is_none());
}

#[test]
fn test_add_same_dependency_twice_idempotent() {
    // GIVEN: Dependency graph
    let mut graph = DependencyGraph::new();
    let node_id = node(1);
    let binding = Binding::state(vec!["count".to_string()]);

    // WHEN: Add same dependency twice
    graph.add_dependency(node_id, &binding,
        None,
);
    graph.add_dependency(node_id, &binding,
        None,
);

    // THEN: Only stored once (IndexSet deduplicates)
    let nodes = graph.get_dependent_nodes("count");
    assert!(nodes.is_some());
    assert_eq!(nodes.unwrap().len(), 1);
}

// ============================================================================
// Additional Edge Cases
// ============================================================================

#[test]
fn test_deeply_nested_path_tracking() {
    // GIVEN: Very deeply nested binding
    let mut graph = DependencyGraph::new();
    let node_id = node(1);
    let binding = Binding::state(vec![
        "a".to_string(),
        "b".to_string(),
        "c".to_string(),
        "d".to_string(),
        "e".to_string(),
    ]);

    graph.add_dependency(node_id, &binding,
        None,
);

    // WHEN: Query affected nodes at various levels
    let affected_root = graph.get_affected_nodes("a");
    let affected_mid = graph.get_affected_nodes("a.b.c");
    let affected_leaf = graph.get_affected_nodes("a.b.c.d.e");
    let affected_deeper = graph.get_affected_nodes("a.b.c.d.e.f");

    // THEN: All parent and child changes affect the node
    assert!(affected_root.contains(&node_id)); // Parent change
    assert!(affected_mid.contains(&node_id)); // Mid-parent change
    assert!(affected_leaf.contains(&node_id)); // Exact match
    assert!(affected_deeper.contains(&node_id)); // Child change
}