@mun-ui/ui 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +104 -2
  3. package/bin/compile.mjs +38 -0
  4. package/bin/dev-protocol.mjs +94 -0
  5. package/bin/formatter.mjs +109 -0
  6. package/bin/hot-reload.mjs +38 -0
  7. package/bin/mun.mjs +521 -0
  8. package/bin/native.mjs +133 -0
  9. package/bin/pnpm-workspace.mjs +58 -0
  10. package/bin/program-patch.mjs +104 -0
  11. package/bin/project.mjs +322 -0
  12. package/bin/watch.mjs +316 -0
  13. package/bin/workflow.mjs +261 -0
  14. package/dist/astro.d.ts +3 -0
  15. package/dist/astro.d.ts.map +1 -0
  16. package/dist/astro.js +3 -0
  17. package/dist/astro.js.map +1 -0
  18. package/dist/closures.d.ts +2 -0
  19. package/dist/closures.d.ts.map +1 -0
  20. package/dist/closures.js +2 -0
  21. package/dist/closures.js.map +1 -0
  22. package/dist/compiler/index.d.ts +2 -0
  23. package/dist/compiler/index.d.ts.map +1 -0
  24. package/dist/compiler/index.js +2 -0
  25. package/dist/compiler/index.js.map +1 -0
  26. package/dist/core.d.ts +3 -0
  27. package/dist/core.d.ts.map +1 -0
  28. package/dist/core.js +3 -0
  29. package/dist/core.js.map +1 -0
  30. package/dist/experimental.d.ts +2 -0
  31. package/dist/experimental.d.ts.map +1 -0
  32. package/dist/experimental.js +2 -0
  33. package/dist/experimental.js.map +1 -0
  34. package/dist/index.d.ts +3 -0
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/index.js +3 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/jsx-dev-runtime.d.ts +2 -0
  39. package/dist/jsx-dev-runtime.d.ts.map +1 -0
  40. package/dist/jsx-dev-runtime.js +2 -0
  41. package/dist/jsx-dev-runtime.js.map +1 -0
  42. package/dist/jsx-runtime.d.ts +2 -0
  43. package/dist/jsx-runtime.d.ts.map +1 -0
  44. package/dist/jsx-runtime.js +2 -0
  45. package/dist/jsx-runtime.js.map +1 -0
  46. package/dist/legacy.d.ts +3 -0
  47. package/dist/legacy.d.ts.map +1 -0
  48. package/dist/legacy.js +3 -0
  49. package/dist/legacy.js.map +1 -0
  50. package/dist/mun.d.ts +5 -0
  51. package/dist/mun.d.ts.map +1 -0
  52. package/dist/mun.js +4 -0
  53. package/dist/mun.js.map +1 -0
  54. package/dist/progressive-blur/index.d.ts +8 -0
  55. package/dist/progressive-blur/index.d.ts.map +1 -0
  56. package/dist/progressive-blur/index.js +8 -0
  57. package/dist/progressive-blur/index.js.map +1 -0
  58. package/dist/progressive-blur/plan.d.ts +3 -0
  59. package/dist/progressive-blur/plan.d.ts.map +1 -0
  60. package/dist/progressive-blur/plan.js +3 -0
  61. package/dist/progressive-blur/plan.js.map +1 -0
  62. package/dist/react-types.d.ts +2 -0
  63. package/dist/react-types.d.ts.map +1 -0
  64. package/dist/react-types.js +2 -0
  65. package/dist/react-types.js.map +1 -0
  66. package/dist/rendering-quality.d.ts +4 -0
  67. package/dist/rendering-quality.d.ts.map +1 -0
  68. package/dist/rendering-quality.js +4 -0
  69. package/dist/rendering-quality.js.map +1 -0
  70. package/dist/state.d.ts +2 -0
  71. package/dist/state.d.ts.map +1 -0
  72. package/dist/state.js +2 -0
  73. package/dist/state.js.map +1 -0
  74. package/dist/types.d.ts +2 -0
  75. package/dist/types.d.ts.map +1 -0
  76. package/dist/types.js +2 -0
  77. package/dist/types.js.map +1 -0
  78. package/dist/vite.d.ts +2 -0
  79. package/dist/vite.d.ts.map +1 -0
  80. package/dist/vite.js +2 -0
  81. package/dist/vite.js.map +1 -0
  82. package/editors/install.mjs +102 -0
  83. package/editors/lsp/mun-lsp.mjs +123 -0
  84. package/editors/lsp/service.mjs +306 -0
  85. package/editors/lsp/source.mjs +58 -0
  86. package/editors/vscode/client.mjs +40 -0
  87. package/editors/vscode/dev-client.mjs +83 -0
  88. package/editors/vscode/discovery.mjs +46 -0
  89. package/editors/vscode/export.mjs +42 -0
  90. package/editors/vscode/extension.cjs +226 -0
  91. package/editors/vscode/language-configuration.json +15 -0
  92. package/editors/vscode/package.json +119 -0
  93. package/editors/vscode/protocol.mjs +29 -0
  94. package/editors/vscode/syntaxes/mun.tmLanguage.json +130 -0
  95. package/native/Cargo.lock +3581 -0
  96. package/native/Cargo.toml +21 -0
  97. package/native/README.md +21 -0
  98. package/native/mun-native/Cargo.toml +23 -0
  99. package/native/mun-native/src/accessibility.rs +738 -0
  100. package/native/mun-native/src/dev.rs +604 -0
  101. package/native/mun-native/src/lib.rs +2676 -0
  102. package/native/mun-native/src/main.rs +204 -0
  103. package/native/mun-native/src/offscreen.rs +393 -0
  104. package/native/mun-native/src/rect.wgsl +63 -0
  105. package/native/mun-native/src/text.rs +310 -0
  106. package/native/mun-native/tests/offscreen_rendering.rs +161 -0
  107. package/native/mun-native/tests/stress.rs +408 -0
  108. package/native/mun-native/tests/window_lifecycle.rs +183 -0
  109. package/native/mun-runtime/Cargo.toml +13 -0
  110. package/native/mun-runtime/src/accessibility.rs +193 -0
  111. package/native/mun-runtime/src/collection.rs +1073 -0
  112. package/native/mun-runtime/src/gesture.rs +1070 -0
  113. package/native/mun-runtime/src/input.rs +325 -0
  114. package/native/mun-runtime/src/ir.rs +782 -0
  115. package/native/mun-runtime/src/kinetics.rs +500 -0
  116. package/native/mun-runtime/src/layout.rs +173 -0
  117. package/native/mun-runtime/src/layout_cache.rs +209 -0
  118. package/native/mun-runtime/src/lib.rs +49 -0
  119. package/native/mun-runtime/src/motion.rs +1162 -0
  120. package/native/mun-runtime/src/retained.rs +574 -0
  121. package/native/mun-runtime/src/runtime.rs +7078 -0
  122. package/native/mun-runtime/src/scene.rs +530 -0
  123. package/native/mun-runtime/src/scroll.rs +521 -0
  124. package/native/mun-runtime/src/scroll_view.rs +220 -0
  125. package/native/mun-runtime/src/text_edit.rs +670 -0
  126. package/native/mun-runtime/src/timeline.rs +1534 -0
  127. package/native/mun-runtime/src/validate.rs +677 -0
  128. package/native/mun-runtime/tests/accessibility_semantics.rs +168 -0
  129. package/native/mun-runtime/tests/compiler_contract.rs +110 -0
  130. package/native/mun-runtime/tests/flexible_frames.rs +47 -0
  131. package/native/mun-runtime/tests/hot_update.rs +367 -0
  132. package/native/mun-runtime/tests/incremental_runtime.rs +323 -0
  133. package/native/mun-runtime/tests/ir_validation.rs +167 -0
  134. package/native/mun-runtime/tests/keyed_collections.rs +251 -0
  135. package/native/mun-runtime/tests/native_controls_style.rs +491 -0
  136. package/native/mun-runtime/tests/native_demo.rs +285 -0
  137. package/native/mun-runtime/tests/native_layout_style.rs +175 -0
  138. package/native/mun-runtime/tests/native_settings_pane.rs +152 -0
  139. package/native/mun-runtime/tests/scroll_view.rs +403 -0
  140. package/native/mun-runtime/tests/text_editing.rs +491 -0
  141. package/package.json +210 -3
  142. package/schemas/README.md +19 -0
  143. package/schemas/semantic-ui-ir-v1.schema.json +1972 -0
  144. package/templates/native/App.mun +15 -0
  145. package/templates/native/gitignore +3 -0
  146. package/templates/native/mun.toml +8 -0
  147. package/templates/project/gitignore +5 -0
  148. package/templates/project/index.html +13 -0
  149. package/templates/project/package.json +21 -0
  150. package/templates/project/src/App.mun +13 -0
  151. package/templates/project/src/index.css +21 -0
  152. package/templates/project/src/main.ts +5 -0
  153. package/templates/project/tsconfig.json +20 -0
  154. package/templates/project/vite.config.ts +6 -0
  155. package/templates/vite/src/App.css +134 -0
  156. package/templates/vite/src/App.tsx +91 -0
  157. package/templates/vite/src/index.css +23 -0
  158. package/templates/vite/vite.config.ts +10 -0
@@ -0,0 +1,574 @@
1
+ use rustc_hash::{FxHashMap, FxHashSet};
2
+
3
+ use thiserror::Error;
4
+
5
+ /// Framework-owned semantic node kinds. These identities intentionally do not
6
+ /// expose Taffy node handles, renderer primitives, or platform widgets.
7
+ #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
8
+ pub enum RetainedNodeKind {
9
+ Window,
10
+ Column,
11
+ Row,
12
+ Overlay,
13
+ Scroll,
14
+ Conditional,
15
+ Text,
16
+ Panel,
17
+ TextField,
18
+ RadioGroup,
19
+ Action,
20
+ Toggle,
21
+ Progress,
22
+ Spacer,
23
+ Divider,
24
+ }
25
+
26
+ impl RetainedNodeKind {
27
+ /// Conditional and window nodes participate in the semantic tree without
28
+ /// manufacturing visual/layout boxes of their own.
29
+ pub fn has_layout_box(self) -> bool {
30
+ !matches!(self, Self::Window | Self::Conditional)
31
+ }
32
+ }
33
+
34
+ /// Finite Mün number normalized for semantic identity comparison.
35
+ #[derive(Clone, Copy, Debug)]
36
+ pub struct RetainedNumberKey(f64);
37
+
38
+ impl RetainedNumberKey {
39
+ pub fn new(value: f64) -> Option<Self> {
40
+ if !value.is_finite() {
41
+ return None;
42
+ }
43
+ Some(Self(if value == 0.0 { 0.0 } else { value }))
44
+ }
45
+
46
+ pub fn get(self) -> f64 {
47
+ self.0
48
+ }
49
+ }
50
+
51
+ impl PartialEq for RetainedNumberKey {
52
+ fn eq(&self, other: &Self) -> bool {
53
+ self.0 == other.0
54
+ }
55
+ }
56
+
57
+ impl Eq for RetainedNumberKey {}
58
+
59
+ /// Runtime value of Mün's semantic `.id(_:)` boundary.
60
+ ///
61
+ /// Structural node IDs remain the stable location anchor. A changed semantic
62
+ /// identity key at that anchor replaces the retained instance without changing
63
+ /// the structural ID used by layout, focus, accessibility, or scene lookup.
64
+ #[derive(Clone, Debug, Eq, PartialEq)]
65
+ pub enum RetainedIdentityKey {
66
+ String(String),
67
+ Number(RetainedNumberKey),
68
+ }
69
+
70
+ impl RetainedIdentityKey {
71
+ pub fn from_value(value: &serde_json::Value) -> Option<Self> {
72
+ match value {
73
+ serde_json::Value::String(value) => Some(Self::String(value.clone())),
74
+ serde_json::Value::Number(value) => value
75
+ .as_f64()
76
+ .and_then(RetainedNumberKey::new)
77
+ .map(Self::Number),
78
+ _ => None,
79
+ }
80
+ }
81
+ }
82
+
83
+ /// A stable runtime instance attached to one semantic UI IR identity.
84
+ #[derive(Clone, Debug, Eq, PartialEq)]
85
+ pub struct RetainedNode {
86
+ pub id: String,
87
+ pub kind: RetainedNodeKind,
88
+ pub identity_key: Option<RetainedIdentityKey>,
89
+ pub instance_id: u64,
90
+ pub parent: Option<String>,
91
+ pub children: Vec<String>,
92
+ }
93
+
94
+ impl RetainedNode {
95
+ pub fn has_layout_box(&self) -> bool {
96
+ self.kind.has_layout_box()
97
+ }
98
+ }
99
+
100
+ #[derive(Clone, Debug, Default, Eq, PartialEq)]
101
+ pub struct RetainedReconciliation {
102
+ pub inserted: Vec<String>,
103
+ pub removed: Vec<String>,
104
+ pub retained: Vec<String>,
105
+ pub replaced: Vec<String>,
106
+ pub reparented: Vec<String>,
107
+ pub children_changed: Vec<String>,
108
+ }
109
+
110
+ impl RetainedReconciliation {
111
+ pub fn is_empty(&self) -> bool {
112
+ self.inserted.is_empty()
113
+ && self.removed.is_empty()
114
+ && self.replaced.is_empty()
115
+ && self.reparented.is_empty()
116
+ && self.children_changed.is_empty()
117
+ }
118
+ }
119
+
120
+ #[derive(Debug, Error, Eq, PartialEq)]
121
+ pub enum RetainedTreeError {
122
+ #[error("duplicate active semantic node identity '{0}'")]
123
+ DuplicateIdentity(String),
124
+ }
125
+
126
+ #[derive(Clone, Debug)]
127
+ pub(crate) struct RetainedNodeSpec {
128
+ pub id: String,
129
+ pub kind: RetainedNodeKind,
130
+ pub identity_key: Option<RetainedIdentityKey>,
131
+ pub parent: Option<String>,
132
+ pub children: Vec<String>,
133
+ }
134
+
135
+ impl RetainedNodeSpec {
136
+ pub(crate) fn new(
137
+ id: impl Into<String>,
138
+ kind: RetainedNodeKind,
139
+ parent: Option<String>,
140
+ children: Vec<String>,
141
+ ) -> Self {
142
+ Self {
143
+ id: id.into(),
144
+ kind,
145
+ identity_key: None,
146
+ parent,
147
+ children,
148
+ }
149
+ }
150
+ }
151
+
152
+ /// Active framework-owned semantic tree.
153
+ ///
154
+ /// Structural Semantic UI IR node IDs are the canonical location anchors.
155
+ /// Runtime instance IDs survive reconciliation only while the node kind and
156
+ /// optional semantic identity key remain stable. Changing `.id(_:)` therefore
157
+ /// replaces the runtime instance at the same structural anchor. Taffy IDs and
158
+ /// scene primitive indices never participate in reconciliation.
159
+ #[derive(Clone, Debug, Default)]
160
+ pub struct RetainedTree {
161
+ /// Long node-id keys hashed for every node on every reconciliation, so a
162
+ /// fast non-DoS hasher is used.
163
+ nodes: FxHashMap<String, RetainedNode>,
164
+ order: Vec<String>,
165
+ next_instance_id: u64,
166
+ revision: u64,
167
+ }
168
+
169
+ impl RetainedTree {
170
+ pub fn node(&self, id: &str) -> Option<&RetainedNode> {
171
+ self.nodes.get(id)
172
+ }
173
+
174
+ pub fn root(&self) -> Option<&RetainedNode> {
175
+ self.order.first().and_then(|id| self.nodes.get(id))
176
+ }
177
+
178
+ pub fn len(&self) -> usize {
179
+ self.nodes.len()
180
+ }
181
+
182
+ pub fn is_empty(&self) -> bool {
183
+ self.nodes.is_empty()
184
+ }
185
+
186
+ pub fn revision(&self) -> u64 {
187
+ self.revision
188
+ }
189
+
190
+ pub fn iter(&self) -> impl Iterator<Item = &RetainedNode> {
191
+ self.order.iter().filter_map(|id| self.nodes.get(id))
192
+ }
193
+
194
+ fn allocate_instance_id(&mut self) -> u64 {
195
+ let next = self.next_instance_id.max(1);
196
+ self.next_instance_id = next.saturating_add(1);
197
+ next
198
+ }
199
+
200
+ pub(crate) fn reconcile(
201
+ &mut self,
202
+ specs: Vec<RetainedNodeSpec>,
203
+ ) -> Result<RetainedReconciliation, RetainedTreeError> {
204
+ {
205
+ let mut seen = FxHashSet::with_capacity_and_hasher(specs.len(), Default::default());
206
+ for spec in &specs {
207
+ if !seen.insert(spec.id.as_str()) {
208
+ return Err(RetainedTreeError::DuplicateIdentity(spec.id.clone()));
209
+ }
210
+ }
211
+ }
212
+
213
+ // Retained nodes are moved out of the previous map and updated in
214
+ // place, so a reconciliation clones each id only for the order and diff.
215
+ let mut previous_nodes = std::mem::take(&mut self.nodes);
216
+ let previous_order = std::mem::take(&mut self.order);
217
+ let mut next_nodes = FxHashMap::with_capacity_and_hasher(specs.len(), Default::default());
218
+ let mut next_order = Vec::with_capacity(specs.len());
219
+ let mut diff = RetainedReconciliation::default();
220
+ let mut identity_resets = FxHashSet::default();
221
+
222
+ for spec in specs {
223
+ let RetainedNodeSpec {
224
+ id,
225
+ kind,
226
+ identity_key,
227
+ parent,
228
+ children,
229
+ } = spec;
230
+ next_order.push(id.clone());
231
+ let ancestor_reset = parent
232
+ .as_ref()
233
+ .is_some_and(|parent| identity_resets.contains(parent));
234
+
235
+ let node = match previous_nodes.remove(&id) {
236
+ Some(mut previous)
237
+ if !ancestor_reset
238
+ && previous.kind == kind
239
+ && previous.identity_key == identity_key =>
240
+ {
241
+ diff.retained.push(id.clone());
242
+ if previous.parent != parent {
243
+ diff.reparented.push(id.clone());
244
+ previous.parent = parent;
245
+ }
246
+ if previous.children != children {
247
+ diff.children_changed.push(id.clone());
248
+ previous.children = children;
249
+ }
250
+ previous
251
+ }
252
+ existing => {
253
+ if existing.is_some() {
254
+ diff.replaced.push(id.clone());
255
+ } else {
256
+ diff.inserted.push(id.clone());
257
+ }
258
+ identity_resets.insert(id.clone());
259
+ RetainedNode {
260
+ id: id.clone(),
261
+ kind,
262
+ identity_key,
263
+ instance_id: self.allocate_instance_id(),
264
+ parent,
265
+ children,
266
+ }
267
+ }
268
+ };
269
+ next_nodes.insert(id, node);
270
+ }
271
+
272
+ diff.removed = previous_order
273
+ .into_iter()
274
+ .filter(|id| previous_nodes.contains_key(id))
275
+ .collect();
276
+
277
+ self.nodes = next_nodes;
278
+ self.order = next_order;
279
+ if !diff.is_empty() {
280
+ self.revision = self.revision.saturating_add(1);
281
+ }
282
+ Ok(diff)
283
+ }
284
+ }
285
+
286
+ #[cfg(test)]
287
+ mod tests {
288
+ use super::*;
289
+
290
+ fn spec(
291
+ id: &str,
292
+ kind: RetainedNodeKind,
293
+ parent: Option<&str>,
294
+ children: &[&str],
295
+ ) -> RetainedNodeSpec {
296
+ RetainedNodeSpec::new(
297
+ id,
298
+ kind,
299
+ parent.map(str::to_owned),
300
+ children.iter().map(|child| (*child).to_owned()).collect(),
301
+ )
302
+ }
303
+
304
+ fn keyed_text_spec(id: &str, key: RetainedIdentityKey) -> RetainedNodeSpec {
305
+ let mut spec = spec(id, RetainedNodeKind::Text, None, &[]);
306
+ spec.identity_key = Some(key);
307
+ spec
308
+ }
309
+
310
+ #[test]
311
+ fn stable_identity_survives_sibling_insertion() {
312
+ let mut tree = RetainedTree::default();
313
+ tree.reconcile(vec![
314
+ spec("window", RetainedNodeKind::Window, None, &["stack"]),
315
+ spec("stack", RetainedNodeKind::Column, Some("window"), &["a"]),
316
+ spec("a", RetainedNodeKind::Text, Some("stack"), &[]),
317
+ ])
318
+ .expect("initial reconciliation");
319
+ let original = tree.node("a").expect("a").instance_id;
320
+
321
+ let diff = tree
322
+ .reconcile(vec![
323
+ spec("window", RetainedNodeKind::Window, None, &["stack"]),
324
+ spec(
325
+ "stack",
326
+ RetainedNodeKind::Column,
327
+ Some("window"),
328
+ &["b", "a"],
329
+ ),
330
+ spec("b", RetainedNodeKind::Text, Some("stack"), &[]),
331
+ spec("a", RetainedNodeKind::Text, Some("stack"), &[]),
332
+ ])
333
+ .expect("insert sibling");
334
+
335
+ assert_eq!(tree.node("a").expect("a").instance_id, original);
336
+ assert_eq!(diff.inserted, vec!["b"]);
337
+ assert!(diff.retained.iter().any(|id| id == "a"));
338
+ assert_eq!(diff.children_changed, vec!["stack"]);
339
+ }
340
+
341
+ #[test]
342
+ fn conditional_is_retained_without_creating_a_layout_box() {
343
+ let mut tree = RetainedTree::default();
344
+ tree.reconcile(vec![
345
+ spec("window", RetainedNodeKind::Window, None, &["branch"]),
346
+ spec(
347
+ "branch",
348
+ RetainedNodeKind::Conditional,
349
+ Some("window"),
350
+ &["collapsed"],
351
+ ),
352
+ spec("collapsed", RetainedNodeKind::Action, Some("branch"), &[]),
353
+ ])
354
+ .expect("initial branch");
355
+ let branch_instance = tree.node("branch").expect("branch").instance_id;
356
+
357
+ let diff = tree
358
+ .reconcile(vec![
359
+ spec("window", RetainedNodeKind::Window, None, &["branch"]),
360
+ spec(
361
+ "branch",
362
+ RetainedNodeKind::Conditional,
363
+ Some("window"),
364
+ &["expanded"],
365
+ ),
366
+ spec("expanded", RetainedNodeKind::Action, Some("branch"), &[]),
367
+ ])
368
+ .expect("switch branch");
369
+
370
+ let branch = tree.node("branch").expect("branch");
371
+ assert_eq!(branch.instance_id, branch_instance);
372
+ assert!(!branch.has_layout_box());
373
+ assert_eq!(diff.inserted, vec!["expanded"]);
374
+ assert_eq!(diff.removed, vec!["collapsed"]);
375
+ assert_eq!(diff.children_changed, vec!["branch"]);
376
+ }
377
+
378
+ #[test]
379
+ fn semantic_kind_change_is_an_explicit_replacement() {
380
+ let mut tree = RetainedTree::default();
381
+ tree.reconcile(vec![spec("node", RetainedNodeKind::Text, None, &[])])
382
+ .expect("text");
383
+ let first = tree.node("node").expect("node").instance_id;
384
+
385
+ let diff = tree
386
+ .reconcile(vec![spec("node", RetainedNodeKind::Action, None, &[])])
387
+ .expect("replacement");
388
+
389
+ assert_eq!(diff.replaced, vec!["node"]);
390
+ assert_ne!(tree.node("node").expect("node").instance_id, first);
391
+ }
392
+
393
+ #[test]
394
+ fn stable_semantic_identity_key_preserves_instance_and_revision() {
395
+ let mut tree = RetainedTree::default();
396
+ let key = RetainedIdentityKey::String("hero".to_owned());
397
+ tree.reconcile(vec![keyed_text_spec("node", key.clone())])
398
+ .expect("initial keyed identity");
399
+ let instance = tree.node("node").expect("node").instance_id;
400
+ let revision = tree.revision();
401
+
402
+ let diff = tree
403
+ .reconcile(vec![keyed_text_spec("node", key.clone())])
404
+ .expect("same keyed identity");
405
+
406
+ let node = tree.node("node").expect("node");
407
+ assert_eq!(node.instance_id, instance);
408
+ assert_eq!(node.identity_key, Some(key));
409
+ assert_eq!(diff.retained, vec!["node"]);
410
+ assert!(diff.is_empty());
411
+ assert_eq!(tree.revision(), revision);
412
+ }
413
+
414
+ #[test]
415
+ fn changed_semantic_identity_key_replaces_instance_at_same_anchor() {
416
+ let mut tree = RetainedTree::default();
417
+ tree.reconcile(vec![keyed_text_spec(
418
+ "node",
419
+ RetainedIdentityKey::String("alpha".to_owned()),
420
+ )])
421
+ .expect("initial keyed identity");
422
+ let instance = tree.node("node").expect("node").instance_id;
423
+ let revision = tree.revision();
424
+
425
+ let diff = tree
426
+ .reconcile(vec![keyed_text_spec(
427
+ "node",
428
+ RetainedIdentityKey::String("beta".to_owned()),
429
+ )])
430
+ .expect("changed keyed identity");
431
+
432
+ let node = tree.node("node").expect("node");
433
+ assert_ne!(node.instance_id, instance);
434
+ assert_eq!(
435
+ node.identity_key,
436
+ Some(RetainedIdentityKey::String("beta".to_owned()))
437
+ );
438
+ assert_eq!(diff.replaced, vec!["node"]);
439
+ assert_eq!(tree.revision(), revision + 1);
440
+ }
441
+
442
+ #[test]
443
+ fn changed_parent_identity_key_replaces_descendants_but_not_siblings() {
444
+ let mut tree = RetainedTree::default();
445
+ let mut keyed_parent = spec(
446
+ "keyed-parent",
447
+ RetainedNodeKind::Column,
448
+ Some("root"),
449
+ &["child"],
450
+ );
451
+ keyed_parent.identity_key = Some(RetainedIdentityKey::String("alpha".to_owned()));
452
+ tree.reconcile(vec![
453
+ spec(
454
+ "root",
455
+ RetainedNodeKind::Window,
456
+ None,
457
+ &["keyed-parent", "stable-sibling"],
458
+ ),
459
+ keyed_parent,
460
+ spec("child", RetainedNodeKind::Text, Some("keyed-parent"), &[]),
461
+ spec("stable-sibling", RetainedNodeKind::Text, Some("root"), &[]),
462
+ ])
463
+ .expect("initial keyed subtree");
464
+
465
+ let parent_instance = tree.node("keyed-parent").expect("keyed parent").instance_id;
466
+ let child_instance = tree.node("child").expect("child").instance_id;
467
+ let sibling_instance = tree
468
+ .node("stable-sibling")
469
+ .expect("stable sibling")
470
+ .instance_id;
471
+
472
+ let mut changed_parent = spec(
473
+ "keyed-parent",
474
+ RetainedNodeKind::Column,
475
+ Some("root"),
476
+ &["child"],
477
+ );
478
+ changed_parent.identity_key = Some(RetainedIdentityKey::String("beta".to_owned()));
479
+ let diff = tree
480
+ .reconcile(vec![
481
+ spec(
482
+ "root",
483
+ RetainedNodeKind::Window,
484
+ None,
485
+ &["keyed-parent", "stable-sibling"],
486
+ ),
487
+ changed_parent,
488
+ spec("child", RetainedNodeKind::Text, Some("keyed-parent"), &[]),
489
+ spec("stable-sibling", RetainedNodeKind::Text, Some("root"), &[]),
490
+ ])
491
+ .expect("changed keyed subtree");
492
+
493
+ assert_ne!(
494
+ tree.node("keyed-parent").expect("keyed parent").instance_id,
495
+ parent_instance
496
+ );
497
+ assert_ne!(
498
+ tree.node("child").expect("child").instance_id,
499
+ child_instance
500
+ );
501
+ assert_eq!(
502
+ tree.node("stable-sibling")
503
+ .expect("stable sibling")
504
+ .instance_id,
505
+ sibling_instance
506
+ );
507
+ assert_eq!(diff.replaced, vec!["keyed-parent", "child"]);
508
+ assert_eq!(diff.retained, vec!["root", "stable-sibling"]);
509
+ assert!(diff.inserted.is_empty());
510
+ assert!(diff.removed.is_empty());
511
+ }
512
+
513
+ #[test]
514
+ fn string_and_number_identity_keys_are_distinct() {
515
+ let string =
516
+ RetainedIdentityKey::from_value(&serde_json::json!("1")).expect("string identity key");
517
+ let integer =
518
+ RetainedIdentityKey::from_value(&serde_json::json!(1)).expect("integer identity key");
519
+ let float =
520
+ RetainedIdentityKey::from_value(&serde_json::json!(1.0)).expect("float identity key");
521
+ let zero =
522
+ RetainedIdentityKey::from_value(&serde_json::json!(0)).expect("zero identity key");
523
+ let negative_zero = RetainedIdentityKey::from_value(&serde_json::json!(-0.0))
524
+ .expect("negative zero identity key");
525
+
526
+ assert_ne!(string, integer);
527
+ assert_eq!(integer, float);
528
+ assert_eq!(zero, negative_zero);
529
+ assert!(RetainedIdentityKey::from_value(&serde_json::json!(true)).is_none());
530
+ }
531
+
532
+ #[test]
533
+ fn child_reordering_preserves_instances() {
534
+ let mut tree = RetainedTree::default();
535
+ tree.reconcile(vec![
536
+ spec("row", RetainedNodeKind::Row, None, &["a", "b"]),
537
+ spec("a", RetainedNodeKind::Text, Some("row"), &[]),
538
+ spec("b", RetainedNodeKind::Text, Some("row"), &[]),
539
+ ])
540
+ .expect("initial row");
541
+ let a = tree.node("a").expect("a").instance_id;
542
+ let b = tree.node("b").expect("b").instance_id;
543
+
544
+ let diff = tree
545
+ .reconcile(vec![
546
+ spec("row", RetainedNodeKind::Row, None, &["b", "a"]),
547
+ spec("b", RetainedNodeKind::Text, Some("row"), &[]),
548
+ spec("a", RetainedNodeKind::Text, Some("row"), &[]),
549
+ ])
550
+ .expect("reordered row");
551
+
552
+ assert_eq!(tree.node("a").expect("a").instance_id, a);
553
+ assert_eq!(tree.node("b").expect("b").instance_id, b);
554
+ assert_eq!(diff.children_changed, vec!["row"]);
555
+ assert!(diff.inserted.is_empty());
556
+ assert!(diff.removed.is_empty());
557
+ }
558
+
559
+ #[test]
560
+ fn duplicate_active_semantic_identity_is_rejected() {
561
+ let mut tree = RetainedTree::default();
562
+ let error = tree
563
+ .reconcile(vec![
564
+ spec("same", RetainedNodeKind::Text, None, &[]),
565
+ spec("same", RetainedNodeKind::Text, None, &[]),
566
+ ])
567
+ .expect_err("duplicate identity must fail");
568
+
569
+ assert_eq!(
570
+ error,
571
+ RetainedTreeError::DuplicateIdentity("same".to_owned())
572
+ );
573
+ }
574
+ }