hypen-engine 0.4.955

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
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
//! Tests for template string binding and expression evaluation functionality
//!
//! These tests verify that template strings like "Counter: @{state.counter}"
//! are properly tracked as dependencies and generate correct patches when state changes.
//!
//! Also tests expression evaluation with ternary operators, comparisons, etc.

use hypen_engine::{
    ir::{ast_to_ir_node, IRNode, Value},
    lifecycle::{Module, ModuleInstance},
    reactive::{DependencyGraph, Scheduler},
    reconcile::{reconcile_ir, InstanceTree, Patch},
};
use serde_json::json;

fn parse_to_element(input: &str) -> hypen_engine::Element {
    let component = hypen_parser::parse_component(input).unwrap();
    match ast_to_ir_node(&component) {
        IRNode::Element(e) => e,
        other => panic!("Expected Element, got {:?}", other),
    }
}

/// Test that template strings are correctly parsed into TemplateString values
#[test]
fn test_template_string_parsing() {
    let source = r#"Text("Counter: @{state.counter}")"#;
    let element = parse_to_element(source);

    // The first prop (prop "0") should be a TemplateString
    let prop = element.props.get("0").expect("Should have prop 0");
    match prop {
        Value::TemplateString { template, bindings } => {
            assert_eq!(template, "Counter: @{state.counter}");
            assert_eq!(bindings.len(), 1);
            assert_eq!(bindings[0].full_path(), "counter");
        }
        other => panic!("Expected TemplateString, got {:?}", other),
    }
}

/// Test that template string bindings are registered in the dependency graph
#[test]
fn test_template_string_dependency_registration() {
    let source = r#"Text("Counter: @{state.counter}")"#;
    let element = parse_to_element(source);

    let mut tree = InstanceTree::new();
    let mut dependencies = DependencyGraph::new();
    let state = json!({"counter": 0});

    let _patches = reconcile_ir(&mut tree, &IRNode::Element(element.clone()), None, &state, &mut dependencies);
    let node_id = tree.root().expect("Should have root");

    // The "counter" path should have the node_id as a dependent
    let affected = dependencies.get_affected_nodes("counter");
    assert!(
        affected.contains(&node_id),
        "Node should be registered as dependent on 'counter' path"
    );
}

/// Test that initial render correctly interpolates template strings
#[test]
fn test_template_string_initial_render() {
    let source = r#"Text("Counter: @{state.counter}")"#;
    let element = parse_to_element(source);

    let mut tree = InstanceTree::new();
    let mut dependencies = DependencyGraph::new();
    let state = json!({"counter": 42});

    let patches = reconcile_ir(&mut tree, &IRNode::Element(element.clone()), None, &state, &mut dependencies);
    let _node_id = tree.root().expect("Should have root");

    // Check that the Create patch has the interpolated value
    let create_patch = patches.iter().find(|p| matches!(p, Patch::Create { .. }));
    assert!(create_patch.is_some(), "Should have a Create patch");

    if let Some(Patch::Create { props, .. }) = create_patch {
        let prop_value = props.get("0").expect("Should have prop 0");
        assert_eq!(
            prop_value.as_str().unwrap(),
            "Counter: 42",
            "Template string should be interpolated with state value"
        );
    }
}

/// Test that state changes trigger SetProp patches for template strings
#[test]
fn test_template_string_state_update() {
    let source = r#"Text("Counter: @{state.counter}")"#;
    let element = parse_to_element(source);

    // Create module with initial state
    let module = Module::new("TestModule");
    let initial_state = json!({"counter": 0});
    let mut instance = ModuleInstance::new(module, initial_state);

    let mut tree = InstanceTree::new();
    let mut dependencies = DependencyGraph::new();
    let mut scheduler = Scheduler::new();

    // Initial render
    let _patches = reconcile_ir(&mut tree, &IRNode::Element(element.clone()), None, instance.get_state(), &mut dependencies);
    let node_id = tree.root().expect("Should have root");
    tree.set_root(node_id);

    // Verify initial render has correct value
    let node = tree.get(node_id).unwrap();
    assert_eq!(
        node.props.get("0").and_then(|v| v.as_str()),
        Some("Counter: 0"),
        "Initial render should show Counter: 0"
    );

    // Update state
    instance.update_state(json!({"counter": 1}));

    // Mark affected nodes dirty
    let affected = dependencies.get_affected_nodes("counter");
    for &id in &affected {
        scheduler.mark_dirty(id);
    }

    // Render dirty nodes
    let update_patches =
        hypen_engine::render::render_dirty_nodes(&mut scheduler, &mut tree, Some(&instance));

    // Should have a SetProp patch with the new interpolated value
    assert!(!update_patches.is_empty(), "Should have update patches");

    let set_prop_patch = update_patches
        .iter()
        .find(|p| matches!(p, Patch::SetProp { name, .. } if name == "0"));

    assert!(
        set_prop_patch.is_some(),
        "Should have a SetProp patch for prop 0"
    );

    if let Some(Patch::SetProp { value, .. }) = set_prop_patch {
        assert_eq!(
            value.as_str().unwrap(),
            "Counter: 1",
            "SetProp should have interpolated value 'Counter: 1'"
        );
    }
}

/// Test template string with multiple bindings
#[test]
fn test_template_string_multiple_bindings() {
    let source = r#"Text("@{state.greeting}, @{state.name}!")"#;
    let element = parse_to_element(source);

    let mut tree = InstanceTree::new();
    let mut dependencies = DependencyGraph::new();
    let state = json!({"greeting": "Hello", "name": "World"});

    let patches = reconcile_ir(&mut tree, &IRNode::Element(element.clone()), None, &state, &mut dependencies);
    let node_id = tree.root().expect("Should have root");

    // Both paths should have the node as a dependent
    assert!(
        dependencies
            .get_affected_nodes("greeting")
            .contains(&node_id),
        "Node should depend on 'greeting'"
    );
    assert!(
        dependencies.get_affected_nodes("name").contains(&node_id),
        "Node should depend on 'name'"
    );

    // Check interpolated value
    if let Some(Patch::Create { props, .. }) =
        patches.iter().find(|p| matches!(p, Patch::Create { .. }))
    {
        let prop_value = props.get("0").expect("Should have prop 0");
        assert_eq!(
            prop_value.as_str().unwrap(),
            "Hello, World!",
            "Multiple bindings should all be interpolated"
        );
    }
}

/// Test that non-template strings (static strings) work correctly
#[test]
fn test_static_string_not_registered_as_dependency() {
    let source = r#"Text("Hello World")"#;
    let element = parse_to_element(source);

    let mut tree = InstanceTree::new();
    let mut dependencies = DependencyGraph::new();
    let state = json!({});

    let _ = reconcile_ir(&mut tree, &IRNode::Element(element.clone()), None, &state, &mut dependencies);
    let node_id = tree.root().expect("Should have root");

    // Static strings should not register any dependencies
    // (The dependency graph should be empty or not have this node)
    let all_affected = dependencies.get_affected_nodes("anything");
    assert!(
        !all_affected.contains(&node_id),
        "Static string should not be registered as dependency"
    );
}

/// Test render_into scenario (lazy routes) with template strings
#[test]
fn test_template_string_in_child_tree() {
    // Build a tree that includes a template-string child
    // This tests that template strings in child subtrees track dependencies correctly
    let source = r#"Column { Text("Counter: @{state.counter}") }"#;
    let element = parse_to_element(source);

    let mut tree = InstanceTree::new();
    let mut dependencies = DependencyGraph::new();
    let state = json!({"counter": 0});

    // Create tree
    let _patches = reconcile_ir(&mut tree, &IRNode::Element(element.clone()), None, &state, &mut dependencies);
    let root_id = tree.root().expect("Should have root");

    // Find the child Text node (Column's child)
    let root_node = tree.get(root_id).expect("Root should exist");
    assert!(!root_node.children.is_empty(), "Column should have children");
    let child_id = root_node.children[0];

    // Child should be registered as dependent on "counter"
    let affected = dependencies.get_affected_nodes("counter");
    assert!(
        affected.contains(&child_id),
        "Child node should be registered as dependent on 'counter'"
    );

    // Create module with the state
    let module = Module::new("TestModule");
    let mut instance = ModuleInstance::new(module, state);

    // Update state
    instance.update_state(json!({"counter": 5}));

    // Mark dirty and render
    let mut scheduler = Scheduler::new();
    for &id in &affected {
        scheduler.mark_dirty(id);
    }

    let update_patches =
        hypen_engine::render::render_dirty_nodes(&mut scheduler, &mut tree, Some(&instance));

    // Should have SetProp patch with interpolated value
    let set_prop = update_patches
        .iter()
        .find(|p| matches!(p, Patch::SetProp { name, .. } if name == "0"));

    assert!(set_prop.is_some(), "Should have SetProp patch for child");

    if let Some(Patch::SetProp { value, .. }) = set_prop {
        assert_eq!(
            value.as_str().unwrap(),
            "Counter: 5",
            "Child template string should be interpolated with new state"
        );
    }
}

// ============================================================================
// Expression Evaluation Tests
// ============================================================================

/// Test ternary expression in template string
#[test]
fn test_ternary_expression_evaluation() {
    let source = r#"Text("@{state.active ? 'Active' : 'Inactive'}")"#;
    let element = parse_to_element(source);

    let mut tree = InstanceTree::new();
    let mut dependencies = DependencyGraph::new();
    let state = json!({"active": true});

    let patches = reconcile_ir(&mut tree, &IRNode::Element(element.clone()), None, &state, &mut dependencies);

    // Check that the ternary expression is evaluated
    if let Some(Patch::Create { props, .. }) =
        patches.iter().find(|p| matches!(p, Patch::Create { .. }))
    {
        let prop_value = props.get("0").expect("Should have prop 0");
        assert_eq!(
            prop_value.as_str().unwrap(),
            "Active",
            "Ternary should evaluate to 'Active' when state.active is true"
        );
    } else {
        panic!("No Create patch found");
    }
}

/// Test ternary expression with false condition
#[test]
fn test_ternary_expression_false_condition() {
    let source = r#"Text("@{state.active ? 'Active' : 'Inactive'}")"#;
    let element = parse_to_element(source);

    let mut tree = InstanceTree::new();
    let mut dependencies = DependencyGraph::new();
    let state = json!({"active": false});

    let patches = reconcile_ir(&mut tree, &IRNode::Element(element.clone()), None, &state, &mut dependencies);

    if let Some(Patch::Create { props, .. }) =
        patches.iter().find(|p| matches!(p, Patch::Create { .. }))
    {
        let prop_value = props.get("0").expect("Should have prop 0");
        assert_eq!(
            prop_value.as_str().unwrap(),
            "Inactive",
            "Ternary should evaluate to 'Inactive' when state.active is false"
        );
    }
}

/// Test ternary expression with color values (common UI pattern)
#[test]
fn test_ternary_expression_with_colors() {
    let source = r#"Column { }.backgroundColor("@{state.selected ? '#FFA7E1' : '#374151'}")"#;
    let element = parse_to_element(source);

    let mut tree = InstanceTree::new();
    let mut dependencies = DependencyGraph::new();
    let state = json!({"selected": true});

    let patches = reconcile_ir(&mut tree, &IRNode::Element(element.clone()), None, &state, &mut dependencies);

    if let Some(Patch::Create { props, .. }) =
        patches.iter().find(|p| matches!(p, Patch::Create { .. }))
    {
        // Applicator props are named "applicatorName.argIndex" (e.g., "backgroundColor.0")
        let bg_color = props
            .get("backgroundColor.0")
            .expect("Should have backgroundColor.0");
        assert_eq!(
            bg_color.as_str().unwrap(),
            "#FFA7E1",
            "Color should be #FFA7E1 when selected is true"
        );
    }
}

/// Test comparison expression in template
#[test]
fn test_comparison_expression() {
    let source = r#"Text("@{state.count > 10 ? 'Many' : 'Few'}")"#;
    let element = parse_to_element(source);

    let mut tree = InstanceTree::new();
    let mut dependencies = DependencyGraph::new();
    let state = json!({"count": 15});

    let patches = reconcile_ir(&mut tree, &IRNode::Element(element.clone()), None, &state, &mut dependencies);

    if let Some(Patch::Create { props, .. }) =
        patches.iter().find(|p| matches!(p, Patch::Create { .. }))
    {
        let prop_value = props.get("0").expect("Should have prop 0");
        assert_eq!(
            prop_value.as_str().unwrap(),
            "Many",
            "Should evaluate to 'Many' when count > 10"
        );
    }
}

/// Test logical AND expression
#[test]
fn test_logical_and_expression() {
    let source = r#"Text("@{state.a && state.b ? 'Both true' : 'Not both'}")"#;
    let element = parse_to_element(source);

    let mut tree = InstanceTree::new();
    let mut dependencies = DependencyGraph::new();
    let state = json!({"a": true, "b": true});

    let patches = reconcile_ir(&mut tree, &IRNode::Element(element.clone()), None, &state, &mut dependencies);

    if let Some(Patch::Create { props, .. }) =
        patches.iter().find(|p| matches!(p, Patch::Create { .. }))
    {
        let prop_value = props.get("0").expect("Should have prop 0");
        assert_eq!(
            prop_value.as_str().unwrap(),
            "Both true",
            "Should evaluate to 'Both true' when both a and b are true"
        );
    }
}

/// Test mixed expression with text
#[test]
fn test_mixed_expression_with_text() {
    let source = r#"Text("Status: @{state.loading ? 'Loading...' : 'Ready'}")"#;
    let element = parse_to_element(source);

    let mut tree = InstanceTree::new();
    let mut dependencies = DependencyGraph::new();
    let state = json!({"loading": true});

    let patches = reconcile_ir(&mut tree, &IRNode::Element(element.clone()), None, &state, &mut dependencies);

    if let Some(Patch::Create { props, .. }) =
        patches.iter().find(|p| matches!(p, Patch::Create { .. }))
    {
        let prop_value = props.get("0").expect("Should have prop 0");
        assert_eq!(
            prop_value.as_str().unwrap(),
            "Status: Loading...",
            "Should combine static text with expression result"
        );
    }
}

/// Test string concatenation in expression
#[test]
fn test_string_concatenation_expression() {
    let source = r#"Text("@{state.first + ' ' + state.last}")"#;
    let element = parse_to_element(source);

    let mut tree = InstanceTree::new();
    let mut dependencies = DependencyGraph::new();
    let state = json!({"first": "John", "last": "Doe"});

    let patches = reconcile_ir(&mut tree, &IRNode::Element(element.clone()), None, &state, &mut dependencies);

    if let Some(Patch::Create { props, .. }) =
        patches.iter().find(|p| matches!(p, Patch::Create { .. }))
    {
        let prop_value = props.get("0").expect("Should have prop 0");
        assert_eq!(
            prop_value.as_str().unwrap(),
            "John Doe",
            "Should concatenate strings"
        );
    }
}

/// Test expression state update triggers re-evaluation
#[test]
fn test_expression_state_update() {
    let source = r#"Text("@{state.selected ? 'Selected' : 'Not selected'}")"#;
    let element = parse_to_element(source);

    // Create module with initial state
    let module = Module::new("TestModule");
    let initial_state = json!({"selected": false});
    let mut instance = ModuleInstance::new(module, initial_state);

    let mut tree = InstanceTree::new();
    let mut dependencies = DependencyGraph::new();
    let mut scheduler = Scheduler::new();

    // Initial render - should show "Not selected"
    let patches = reconcile_ir(&mut tree, &IRNode::Element(element.clone()), None, instance.get_state(), &mut dependencies);
    let node_id = tree.root().expect("Should have root");
    tree.set_root(node_id);

    // Verify initial render
    if let Some(Patch::Create { props, .. }) =
        patches.iter().find(|p| matches!(p, Patch::Create { .. }))
    {
        assert_eq!(
            props.get("0").unwrap().as_str().unwrap(),
            "Not selected",
            "Initial render should show 'Not selected'"
        );
    }

    // Update state to selected = true
    instance.update_state(json!({"selected": true}));

    // Mark affected nodes dirty
    let affected = dependencies.get_affected_nodes("selected");
    for &id in &affected {
        scheduler.mark_dirty(id);
    }

    // Render dirty nodes
    let update_patches =
        hypen_engine::render::render_dirty_nodes(&mut scheduler, &mut tree, Some(&instance));

    // Should have SetProp patch with re-evaluated expression
    let set_prop = update_patches
        .iter()
        .find(|p| matches!(p, Patch::SetProp { name, .. } if name == "0"));

    assert!(
        set_prop.is_some(),
        "Should have SetProp patch after state change"
    );

    if let Some(Patch::SetProp { value, .. }) = set_prop {
        assert_eq!(
            value.as_str().unwrap(),
            "Selected",
            "Expression should re-evaluate to 'Selected' after state change"
        );
    }
}

/// Test complex nested expression
#[test]
fn test_complex_nested_expression() {
    let source =
        r#"Text("@{state.user.premium && state.user.age >= 18 ? 'VIP Adult' : 'Standard'}")"#;
    let element = parse_to_element(source);

    let mut tree = InstanceTree::new();
    let mut dependencies = DependencyGraph::new();
    let state = json!({
        "user": {
            "premium": true,
            "age": 25
        }
    });

    let patches = reconcile_ir(&mut tree, &IRNode::Element(element.clone()), None, &state, &mut dependencies);

    if let Some(Patch::Create { props, .. }) =
        patches.iter().find(|p| matches!(p, Patch::Create { .. }))
    {
        let prop_value = props.get("0").expect("Should have prop 0");
        assert_eq!(
            prop_value.as_str().unwrap(),
            "VIP Adult",
            "Complex expression should evaluate correctly"
        );
    }
}