hypen-engine 0.5.1

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
//! Tests for src/serialize/remote.rs - Remote UI protocol
//!
//! Tests serialization/deserialization of messages for remote UI streaming

use hypen_engine::ir::NodeId;
use hypen_engine::reconcile::Patch;
use hypen_engine::serialize::remote::{
    deserialize_message, serialize_message, InitialTree, PatchStream, RemoteMessage,
};
use serde_json::json;

// Helper to create test NodeId
fn test_node_id() -> NodeId {
    NodeId::default()
}

// ============================================================================
// InitialTree Serialization/Deserialization (5 tests)
// ============================================================================

#[test]
fn test_initial_tree_new() {
    // GIVEN: Module, state, and patches
    let state = json!({"count": 0, "user": "Alice"});
    let patches = vec![Patch::create(
        test_node_id(),
        "Column".to_string(),
        std::sync::Arc::new(indexmap::indexmap! {}),
    )];

    // WHEN: Create InitialTree
    let tree = InitialTree::new("Counter".to_string(), state.clone(), patches.clone());

    // THEN: Fields set correctly
    assert_eq!(tree.module, "Counter");
    assert_eq!(tree.state, state);
    assert_eq!(tree.patches.len(), 1);
    assert_eq!(tree.revision, 0);
    assert_eq!(tree.hash, None);
}

#[test]
fn test_initial_tree_with_hash() {
    // GIVEN: InitialTree
    let tree = InitialTree::new("Test".to_string(), json!({}), vec![]);

    // WHEN: Add hash
    let tree_with_hash = tree.with_hash("abc123".to_string());

    // THEN: Hash added
    assert_eq!(tree_with_hash.hash, Some("abc123".to_string()));
}

#[test]
fn test_initial_tree_serialization() {
    // GIVEN: InitialTree with state and patches
    let tree = InitialTree::new(
        "UserModule".to_string(),
        json!({"name": "Alice", "email": "alice@example.com"}),
        vec![Patch::create(
            test_node_id(),
            "Text".to_string(),
            std::sync::Arc::new(indexmap::indexmap! {}),
        )],
    );

    // WHEN: Serialize to JSON
    let json_str = serde_json::to_string(&tree).unwrap();

    // THEN: Contains all fields
    assert!(json_str.contains("UserModule"));
    assert!(json_str.contains("Alice"));
    assert!(json_str.contains("alice@example.com"));
    assert!(json_str.contains("revision"));
}

#[test]
fn test_initial_tree_deserialization() {
    // GIVEN: JSON representation
    let json_str = r#"{
        "module": "Counter",
        "state": {"count": 5},
        "patches": [],
        "revision": 0
    }"#;

    // WHEN: Deserialize from JSON
    let tree: InitialTree = serde_json::from_str(json_str).unwrap();

    // THEN: Correctly parsed
    assert_eq!(tree.module, "Counter");
    assert_eq!(tree.state["count"], 5);
    assert_eq!(tree.patches.len(), 0);
    assert_eq!(tree.revision, 0);
    assert_eq!(tree.hash, None);
}

#[test]
fn test_initial_tree_hash_omitted_when_none() {
    // GIVEN: InitialTree without hash
    let tree = InitialTree::new("Test".to_string(), json!({}), vec![]);

    // WHEN: Serialize
    let json_str = serde_json::to_string(&tree).unwrap();

    // THEN: Hash field not included (skip_serializing_if)
    assert!(!json_str.contains("hash"));
}

// ============================================================================
// PatchStream with Revision Tracking (5 tests)
// ============================================================================

#[test]
fn test_patch_stream_new() {
    // GIVEN: Module, patches, and revision
    let patches = vec![Patch::set_prop(
        test_node_id(),
        "count".to_string(),
        json!(10),
    )];

    // WHEN: Create PatchStream
    let stream = PatchStream::new("Counter".to_string(), patches.clone(), 5);

    // THEN: Fields set correctly
    assert_eq!(stream.module, "Counter");
    assert_eq!(stream.patches.len(), 1);
    assert_eq!(stream.revision, 5);
    assert_eq!(stream.hash, None);
}

#[test]
fn test_patch_stream_with_hash() {
    // GIVEN: PatchStream
    let stream = PatchStream::new("Test".to_string(), vec![], 1);

    // WHEN: Add hash
    let stream_with_hash = stream.with_hash("xyz789".to_string());

    // THEN: Hash added
    assert_eq!(stream_with_hash.hash, Some("xyz789".to_string()));
}

#[test]
fn test_patch_stream_serialization() {
    // GIVEN: PatchStream with patches
    let stream = PatchStream::new(
        "Counter".to_string(),
        vec![Patch::set_prop(
            test_node_id(),
            "count".to_string(),
            json!(15),
        )],
        3,
    );

    // WHEN: Serialize to JSON
    let json_str = serde_json::to_string(&stream).unwrap();

    // THEN: Contains all fields
    assert!(json_str.contains("Counter"));
    assert!(json_str.contains("revision"));
    assert!(json_str.contains("3"));
}

#[test]
fn test_patch_stream_deserialization() {
    // GIVEN: JSON representation
    let json_str = r#"{
        "module": "UserProfile",
        "patches": [],
        "revision": 10
    }"#;

    // WHEN: Deserialize from JSON
    let stream: PatchStream = serde_json::from_str(json_str).unwrap();

    // THEN: Correctly parsed
    assert_eq!(stream.module, "UserProfile");
    assert_eq!(stream.patches.len(), 0);
    assert_eq!(stream.revision, 10);
    assert_eq!(stream.hash, None);
}

#[test]
fn test_patch_stream_revision_monotonically_increasing() {
    // GIVEN: Multiple patch streams
    let stream1 = PatchStream::new("Test".to_string(), vec![], 1);
    let stream2 = PatchStream::new("Test".to_string(), vec![], 2);
    let stream3 = PatchStream::new("Test".to_string(), vec![], 3);

    // THEN: Revisions increase monotonically
    assert!(stream2.revision > stream1.revision);
    assert!(stream3.revision > stream2.revision);
}

// ============================================================================
// RemoteMessage Variants (5 tests)
// ============================================================================

#[test]
fn test_remote_message_initial_tree() {
    // GIVEN: InitialTree message
    let tree = InitialTree::new("Test".to_string(), json!({"count": 0}), vec![]);
    let message = RemoteMessage::InitialTree(tree);

    // WHEN: Serialize and deserialize
    let json = serialize_message(&message).unwrap();
    let deserialized = deserialize_message(&json).unwrap();

    // THEN: Correct type and data preserved
    match deserialized {
        RemoteMessage::InitialTree(t) => {
            assert_eq!(t.module, "Test");
            assert_eq!(t.state["count"], 0);
        }
        _ => panic!("Expected InitialTree variant"),
    }
}

#[test]
fn test_remote_message_patch() {
    // GIVEN: Patch message
    let stream = PatchStream::new("Counter".to_string(), vec![], 5);
    let message = RemoteMessage::Patch(stream);

    // WHEN: Serialize and deserialize
    let json = serialize_message(&message).unwrap();
    let deserialized = deserialize_message(&json).unwrap();

    // THEN: Correct type and data preserved
    match deserialized {
        RemoteMessage::Patch(s) => {
            assert_eq!(s.module, "Counter");
            assert_eq!(s.revision, 5);
        }
        _ => panic!("Expected Patch variant"),
    }
}

#[test]
fn test_remote_message_dispatch_action() {
    // GIVEN: DispatchAction message
    let message = RemoteMessage::DispatchAction {
        module: "Counter".to_string(),
        action: "increment".to_string(),
        payload: Some(json!({"amount": 5})),
    };

    // WHEN: Serialize and deserialize
    let json = serialize_message(&message).unwrap();
    let deserialized = deserialize_message(&json).unwrap();

    // THEN: Correct type and data preserved
    match deserialized {
        RemoteMessage::DispatchAction {
            module,
            action,
            payload,
        } => {
            assert_eq!(module, "Counter");
            assert_eq!(action, "increment");
            assert_eq!(payload.unwrap()["amount"], 5);
        }
        _ => panic!("Expected DispatchAction variant"),
    }
}

#[test]
fn test_remote_message_state_update() {
    // GIVEN: StateUpdate message
    let message = RemoteMessage::StateUpdate {
        module: "User".to_string(),
        state: json!({"name": "Bob", "age": 25}),
    };

    // WHEN: Serialize and deserialize
    let json = serialize_message(&message).unwrap();
    let deserialized = deserialize_message(&json).unwrap();

    // THEN: Correct type and data preserved
    match deserialized {
        RemoteMessage::StateUpdate { module, state } => {
            assert_eq!(module, "User");
            assert_eq!(state["name"], "Bob");
            assert_eq!(state["age"], 25);
        }
        _ => panic!("Expected StateUpdate variant"),
    }
}

#[test]
fn test_remote_message_dispatch_action_without_payload() {
    // GIVEN: DispatchAction without payload
    let message = RemoteMessage::DispatchAction {
        module: "Auth".to_string(),
        action: "logout".to_string(),
        payload: None,
    };

    // WHEN: Serialize and deserialize
    let json = serialize_message(&message).unwrap();
    let deserialized = deserialize_message(&json).unwrap();

    // THEN: Payload is None
    match deserialized {
        RemoteMessage::DispatchAction {
            module,
            action,
            payload,
        } => {
            assert_eq!(module, "Auth");
            assert_eq!(action, "logout");
            assert!(payload.is_none());
        }
        _ => panic!("Expected DispatchAction variant"),
    }
}

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

#[test]
fn test_message_type_field_camelcase() {
    // GIVEN: Any RemoteMessage
    let message = RemoteMessage::DispatchAction {
        module: "Test".to_string(),
        action: "test".to_string(),
        payload: None,
    };

    // WHEN: Serialize
    let json = serialize_message(&message).unwrap();

    // THEN: Type field is camelCase (dispatchAction not dispatch_action)
    assert!(json.contains("\"type\""));
    assert!(json.contains("dispatchAction"));
}

#[test]
fn test_serialize_deserialize_roundtrip_all_variants() {
    // GIVEN: All RemoteMessage variants
    let messages = vec![
        RemoteMessage::InitialTree(InitialTree::new("M1".to_string(), json!({}), vec![])),
        RemoteMessage::Patch(PatchStream::new("M2".to_string(), vec![], 1)),
        RemoteMessage::DispatchAction {
            module: "M3".to_string(),
            action: "act".to_string(),
            payload: Some(json!({"x": 1})),
        },
        RemoteMessage::StateUpdate {
            module: "M4".to_string(),
            state: json!({"y": 2}),
        },
    ];

    // WHEN: Serialize and deserialize each
    for message in messages {
        let json = serialize_message(&message).unwrap();
        let deserialized = deserialize_message(&json).unwrap();

        // THEN: Variants match
        match (&message, &deserialized) {
            (RemoteMessage::InitialTree(_), RemoteMessage::InitialTree(_)) => {}
            (RemoteMessage::Patch(_), RemoteMessage::Patch(_)) => {}
            (RemoteMessage::DispatchAction { .. }, RemoteMessage::DispatchAction { .. }) => {}
            (RemoteMessage::StateUpdate { .. }, RemoteMessage::StateUpdate { .. }) => {}
            _ => panic!("Variant mismatch after roundtrip"),
        }
    }
}

#[test]
fn test_deserialize_invalid_json_returns_error() {
    // GIVEN: Invalid JSON
    let invalid_json = "{ not valid json }";

    // WHEN: Try to deserialize
    let result = deserialize_message(invalid_json);

    // THEN: Returns error
    assert!(result.is_err());
}

#[test]
fn test_initial_tree_with_multiple_patches() {
    // GIVEN: InitialTree with multiple patches
    let patches = vec![
        Patch::create(
            test_node_id(),
            "Column".to_string(),
            std::sync::Arc::new(indexmap::indexmap! {}),
        ),
        Patch::insert(test_node_id(), test_node_id(), None),
        Patch::set_text(test_node_id(), "Hello".to_string()),
    ];
    let tree = InitialTree::new("App".to_string(), json!({}), patches);

    // WHEN: Serialize and deserialize
    let json = serde_json::to_string(&tree).unwrap();
    let deserialized: InitialTree = serde_json::from_str(&json).unwrap();

    // THEN: All patches preserved
    assert_eq!(deserialized.patches.len(), 3);
}