dampen-core 0.3.2

Core parser, IR, and traits for Dampen UI framework
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
//! Contract tests for the AppState struct.
//!
//! These tests verify that AppState works correctly with different
//! generic parameter configurations.

use dampen_core::{AppState, HandlerRegistry, UiBindable};
use dampen_macros::UiModel;
use serde::{Deserialize, Serialize};
use std::any::TypeId;

#[derive(Default, UiModel, Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct TestModel {
    pub count: i32,
    pub name: String,
}

#[test]
fn test_appstate_default_unit() {
    let xml = r#"
        <dampen version="1.1" encoding="utf-8">
            <column>
                <text value="Hello" />
            </column>
        </dampen>
    "#;

    let document = dampen_core::parse(xml).expect("Failed to parse XML");
    let state = AppState::<()>::new(document.clone());

    assert_eq!(state.document.version.major, 1);
    assert_eq!(TypeId::of::<()>(), TypeId::of::<()>());
}

#[test]
fn test_appstate_with_model() {
    let xml = r#"
        <dampen version="1.1" encoding="utf-8">
            <column>
                <text value="Hello" />
            </column>
        </dampen>
    "#;

    let document = dampen_core::parse(xml).expect("Failed to parse XML");
    let model = TestModel {
        count: 42,
        name: "Test".to_string(),
    };
    let state = AppState::with_model(document.clone(), model.clone());

    assert_eq!(state.document.version.major, 1);
    assert_eq!(state.model.count, 42);
    assert_eq!(state.model.name, "Test");
}

#[test]
fn test_appstate_with_handlers() {
    let xml = r#"
        <dampen version="1.1" encoding="utf-8">
            <column>
                <text value="Hello" />
            </column>
        </dampen>
    "#;

    let document = dampen_core::parse(xml).expect("Failed to parse XML");

    let registry = HandlerRegistry::new();
    let state: AppState<()> = AppState::with_handlers(document.clone(), registry);

    assert_eq!(state.document.version.major, 1);
}

#[test]
fn test_appstate_model_implements_uibindable() {
    let model = TestModel {
        count: 100,
        name: "Binding Test".to_string(),
    };

    let _ = model.get_field(&["count"]);
    let _ = model.get_field(&["name"]);

    let fields = TestModel::available_fields();
    assert!(fields.contains(&"count".to_string()));
    assert!(fields.contains(&"name".to_string()));
}

#[test]
fn test_appstate_clonable() {
    let xml = r#"
        <dampen version="1.1" encoding="utf-8">
            <column>
                <text value="Clone test" />
            </column>
        </dampen>
    "#;

    let document = dampen_core::parse(xml).expect("Failed to parse XML");

    let registry = HandlerRegistry::new();
    let state: AppState<TestModel> = AppState::with_handlers(document.clone(), registry);

    let cloned = state.clone();

    assert_eq!(cloned.document.version.major, 1);
    assert_eq!(cloned.model.count, 0);
    assert_eq!(cloned.model.name, "");
}

#[test]
fn test_appstate_model_serialization() {
    let model = TestModel {
        count: 123,
        name: "Serialize".to_string(),
    };

    let serialized = serde_json::to_string(&model).expect("Failed to serialize");
    let deserialized: TestModel = serde_json::from_str(&serialized).expect("Failed to deserialize");

    assert_eq!(model.count, deserialized.count);
    assert_eq!(model.name, deserialized.name);
}

#[test]
fn test_handler_registry_simple_handler() {
    let registry = HandlerRegistry::new();
    let called = std::sync::Arc::new(std::sync::Mutex::new(false));
    let called_clone = called.clone();

    registry.register_simple("test", move |_model: &mut dyn std::any::Any| {
        *called_clone.lock().unwrap() = true;
    });

    if let Some(dampen_core::HandlerEntry::Simple(handler)) = registry.get("test") {
        let mut any_model: Box<dyn std::any::Any> = Box::new(());
        handler(&mut *any_model);
        assert!(*called.lock().unwrap());
    } else {
        panic!("Handler not found");
    }
}

// ===== Hot-Reload Tests =====

#[test]
fn test_hot_reload_preserves_model() {
    // Create initial state with a model
    let xml_v1 = r#"
        <dampen version="1.1" encoding="utf-8">
            <column>
                <text value="Old UI" />
            </column>
        </dampen>
    "#;

    let document_v1 = dampen_core::parse(xml_v1).expect("Failed to parse XML v1");
    let model = TestModel {
        count: 42,
        name: "Preserved".to_string(),
    };
    let mut state = AppState::with_model(document_v1, model);

    // Verify initial state
    assert_eq!(state.model.count, 42);
    assert_eq!(state.model.name, "Preserved");

    // Parse new UI definition
    let xml_v2 = r#"
        <dampen version="1.1" encoding="utf-8">
            <column>
                <text value="New UI" />
                <button label="Click me" />
            </column>
        </dampen>
    "#;

    let document_v2 = dampen_core::parse(xml_v2).expect("Failed to parse XML v2");

    // Hot-reload with new document
    state.hot_reload(document_v2.clone());

    // Model should be preserved
    assert_eq!(state.model.count, 42);
    assert_eq!(state.model.name, "Preserved");

    // Document should be updated
    assert_eq!(
        state.document.root.children.len(),
        document_v2.root.children.len()
    );
}

#[test]
fn test_hot_reload_updates_document() {
    let xml_v1 = r#"
        <dampen version="1.1" encoding="utf-8">
            <column>
                <text value="Version 1" />
            </column>
        </dampen>
    "#;

    let document_v1 = dampen_core::parse(xml_v1).expect("Failed to parse XML v1");
    let mut state = AppState::<TestModel>::new(document_v1);

    // Count children in original document
    let original_widget_count = state.document.root.children.len();

    // Parse new UI with more widgets
    let xml_v2 = r#"
        <dampen version="1.1" encoding="utf-8">
            <column>
                <text value="Version 2" />
                <button label="Button 1" />
                <button label="Button 2" />
            </column>
        </dampen>
    "#;

    let document_v2 = dampen_core::parse(xml_v2).expect("Failed to parse XML v2");
    let new_widget_count = document_v2.root.children.len();

    // Hot-reload
    state.hot_reload(document_v2);

    // Document should be updated
    assert_eq!(state.document.root.children.len(), new_widget_count);
    assert_ne!(original_widget_count, new_widget_count);
}

#[test]
fn test_hot_reload_preserves_handlers() {
    let xml = r#"
        <dampen version="1.1" encoding="utf-8">
            <column>
                <text value="Test" />
            </column>
        </dampen>
    "#;

    let document_v1 = dampen_core::parse(xml).expect("Failed to parse XML");

    // Create handler registry
    let called = std::sync::Arc::new(std::sync::Mutex::new(false));
    let called_clone = called.clone();

    let registry = HandlerRegistry::new();
    registry.register_simple("test_handler", move |_model: &mut dyn std::any::Any| {
        *called_clone.lock().unwrap() = true;
    });

    let mut state = AppState::<TestModel>::with_handlers(document_v1, registry);

    // Verify handler works before hot-reload
    if let Some(dampen_core::HandlerEntry::Simple(handler)) =
        state.handler_registry.get("test_handler")
    {
        let mut any_model: Box<dyn std::any::Any> = Box::new(());
        handler(&mut *any_model);
        assert!(*called.lock().unwrap());
    } else {
        panic!("Handler not found before hot-reload");
    }

    // Reset the flag
    *called.lock().unwrap() = false;

    // Hot-reload with new document
    let xml_v2 = r#"
        <dampen version="1.1" encoding="utf-8">
            <column>
                <text value="Updated" />
            </column>
        </dampen>
    "#;

    let document_v2 = dampen_core::parse(xml_v2).expect("Failed to parse XML v2");
    state.hot_reload(document_v2);

    // Verify handler still works after hot-reload
    if let Some(dampen_core::HandlerEntry::Simple(handler)) =
        state.handler_registry.get("test_handler")
    {
        let mut any_model: Box<dyn std::any::Any> = Box::new(());
        handler(&mut *any_model);
        assert!(*called.lock().unwrap());
    } else {
        panic!("Handler not found after hot-reload");
    }
}

#[test]
fn test_hot_reload_multiple_times() {
    let xml_v1 =
        r#"<dampen version="1.1" encoding="utf-8"><column><text value="V1" /></column></dampen>"#;
    let xml_v2 =
        r#"<dampen version="1.1" encoding="utf-8"><column><text value="V2" /></column></dampen>"#;
    let xml_v3 =
        r#"<dampen version="1.1" encoding="utf-8"><column><text value="V3" /></column></dampen>"#;

    let document_v1 = dampen_core::parse(xml_v1).unwrap();
    let model = TestModel {
        count: 100,
        name: "Stable".to_string(),
    };
    let mut state = AppState::with_model(document_v1, model);

    // First reload
    let document_v2 = dampen_core::parse(xml_v2).unwrap();
    state.hot_reload(document_v2);
    assert_eq!(state.model.count, 100);
    assert_eq!(state.model.name, "Stable");

    // Second reload
    let document_v3 = dampen_core::parse(xml_v3).unwrap();
    state.hot_reload(document_v3);
    assert_eq!(state.model.count, 100);
    assert_eq!(state.model.name, "Stable");

    // Model remains unchanged through multiple reloads
}

// ===== dispatch_with_command Tests =====

#[test]
fn test_dispatch_with_command_simple_handler() {
    let registry = HandlerRegistry::new();
    let called = std::sync::Arc::new(std::sync::Mutex::new(false));
    let called_clone = called.clone();

    registry.register_simple("test", move |_model: &mut dyn std::any::Any| {
        *called_clone.lock().unwrap() = true;
    });

    let mut any_model: Box<dyn std::any::Any> = Box::new(());
    let result = registry.dispatch_with_command("test", &mut *any_model, None);

    assert!(*called.lock().unwrap(), "Handler should have been called");
    assert!(result.is_none(), "Simple handler should return None");
}

#[test]
fn test_dispatch_with_command_value_handler() {
    let registry = HandlerRegistry::new();
    let received = std::sync::Arc::new(std::sync::Mutex::new(String::new()));
    let received_clone = received.clone();

    registry.register_with_value(
        "test",
        move |_model: &mut dyn std::any::Any, value: Box<dyn std::any::Any>| {
            if let Some(s) = value.downcast_ref::<String>() {
                *received_clone.lock().unwrap() = s.clone();
            }
        },
    );

    let mut any_model: Box<dyn std::any::Any> = Box::new(());
    let result = registry.dispatch_with_command("test", &mut *any_model, Some("hello".to_string()));

    assert_eq!(
        *received.lock().unwrap(),
        "hello",
        "Handler should receive the value"
    );
    assert!(result.is_none(), "WithValue handler should return None");
}

#[test]
fn test_dispatch_with_command_returns_command() {
    let registry = HandlerRegistry::new();

    registry.register_with_command("test", |_model: &mut dyn std::any::Any| {
        Box::new(42i32) // Return a boxed value representing a command
    });

    let mut any_model: Box<dyn std::any::Any> = Box::new(());
    let result = registry.dispatch_with_command("test", &mut *any_model, None);

    assert!(result.is_some(), "WithCommand handler should return Some");

    if let Some(boxed_value) = result {
        if let Ok(value) = boxed_value.downcast::<i32>() {
            assert_eq!(*value, 42, "Should return the command value");
        } else {
            panic!("Failed to downcast command value");
        }
    }
}

#[test]
fn test_dispatch_with_command_nonexistent_handler() {
    let registry = HandlerRegistry::new();
    let mut any_model: Box<dyn std::any::Any> = Box::new(());

    let result = registry.dispatch_with_command("nonexistent", &mut *any_model, None);

    assert!(result.is_none(), "Nonexistent handler should return None");
}

#[test]
fn test_dispatch_with_command_modifies_model() {
    #[derive(Default)]
    struct Counter {
        count: i32,
    }

    let registry = HandlerRegistry::new();
    registry.register_with_command("increment", |model: &mut dyn std::any::Any| {
        if let Some(counter) = model.downcast_mut::<Counter>() {
            counter.count += 1;
        }
        Box::new("task".to_string()) // Return a task
    });

    let mut counter = Counter { count: 0 };
    let any_model: &mut dyn std::any::Any = &mut counter;

    let result = registry.dispatch_with_command("increment", any_model, None);

    assert_eq!(counter.count, 1, "Model should be modified");
    assert!(result.is_some(), "Should return the task");

    if let Some(boxed_task) = result {
        if let Ok(task) = boxed_task.downcast::<String>() {
            assert_eq!(*task, "task");
        }
    }
}

#[test]
fn test_dispatch_vs_dispatch_with_command() {
    let registry = HandlerRegistry::new();
    let call_count = std::sync::Arc::new(std::sync::Mutex::new(0));
    let call_count_clone = call_count.clone();

    registry.register_with_command("test", move |_model: &mut dyn std::any::Any| {
        *call_count_clone.lock().unwrap() += 1;
        Box::new("command".to_string())
    });

    let mut any_model: Box<dyn std::any::Any> = Box::new(());

    // Test dispatch() - should call handler but ignore return value
    registry.dispatch("test", &mut *any_model, None);
    assert_eq!(*call_count.lock().unwrap(), 1);

    // Test dispatch_with_command() - should call handler and return value
    let result = registry.dispatch_with_command("test", &mut *any_model, None);
    assert_eq!(*call_count.lock().unwrap(), 2);
    assert!(
        result.is_some(),
        "dispatch_with_command should return the command"
    );
}