1
#[allow(unused_imports)]
2
pub use super::*;
3
#[cfg(test)]
4
mod tests {
5
    use super::*;
6
    use crate::geom::{LogicalPosition, LogicalRect, LogicalSize};
7

            
8
    /// `Spring` is a real `AnimationInterpolationFunction` variant now, so it
9
    /// must survive the C-ABI enum's own accessors rather than panicking or
10
    /// silently answering as some other curve.
11
    #[test]
12
1
    fn spring_is_a_first_class_interpolation_function() {
13
1
        let f = AnimationInterpolationFunction::Spring(Spring::SNAPPY);
14
1
        assert!(f.is_spring());
15
1
        assert!(!AnimationInterpolationFunction::EaseInOut.is_spring());
16
        // No duration to evaluate against: it answers as the documented
17
        // ease-in-out stand-in, and `get_curve`/`ease` must not disagree.
18
1
        assert_eq!(
19
1
            f.get_curve(),
20
1
            AnimationInterpolationFunction::EaseInOut.get_curve()
21
        );
22
6
        for t in [0.0_f32, 0.25, 0.5, 0.75, 1.0] {
23
5
            assert!((ease(f, t) - ease(AnimationInterpolationFunction::EaseInOut, t)).abs() < 1e-6);
24
        }
25
1
    }
26

            
27
23
    fn rect(x: f32, y: f32, w: f32, h: f32) -> LogicalRect {
28
23
        LogicalRect {
29
23
            origin: LogicalPosition::new(x, y),
30
23
            size: LogicalSize::new(w, h),
31
23
        }
32
23
    }
33

            
34
    #[test]
35
1
    fn flip_inverts_a_pure_translation() {
36
        // Moved right 100 and down 50, same size: the inversion must put it back.
37
1
        let f = flip(rect(0.0, 0.0, 10.0, 10.0), rect(100.0, 50.0, 10.0, 10.0));
38
1
        assert_eq!(f.translate_x, -100.0);
39
1
        assert_eq!(f.translate_y, -50.0);
40
1
        assert_eq!(f.scale_x, 1.0);
41
1
        assert_eq!(f.scale_y, 1.0);
42
1
    }
43

            
44
    #[test]
45
1
    fn flip_never_scales_a_size_change() {
46
        // USER ruling 2026-08-17 (was `flip_inverts_a_pure_scale`, asserting
47
        // 0.5): a resized node has already RELAYOUTED at its final size, and
48
        // drawing it at half scale for the flight squashes freshly laid-out
49
        // content — a card growing from half-width to full-width rendered its
50
        // text visibly compressed for the whole transition. Size is layout's
51
        // job; the animation only travels.
52
1
        let f = flip(rect(0.0, 0.0, 50.0, 20.0), rect(0.0, 0.0, 100.0, 40.0));
53
1
        assert_eq!(f.scale_x, 1.0);
54
1
        assert_eq!(f.scale_y, 1.0);
55
1
        assert!(
56
1
            f.is_identity(),
57
            "same origin, changed size: nothing to animate"
58
        );
59
1
    }
60

            
61
    #[test]
62
1
    fn flip_of_an_unchanged_rect_is_identity() {
63
1
        let r = rect(12.0, 34.0, 56.0, 78.0);
64
1
        assert!(flip(r, r).is_identity());
65
1
    }
66

            
67
    #[test]
68
1
    fn flip_never_produces_a_non_finite_scale() {
69
        // A collapsed target would divide by zero; the display list must never
70
        // see a NaN transform.
71
1
        let f = flip(rect(0.0, 0.0, 10.0, 10.0), rect(0.0, 0.0, 0.0, 0.0));
72
1
        assert!(f.scale_x.is_finite() && f.scale_y.is_finite());
73
1
        assert_eq!(f.scale_x, 1.0);
74
1
        assert_eq!(f.scale_y, 1.0);
75
1
    }
76

            
77
    #[test]
78
1
    fn a_spring_settles_at_its_target() {
79
1
        let mut c = AnimChannel::spring(0.0, 100.0, Spring::SMOOTH);
80
68
        for _ in 0..600 {
81
68
            c.tick(1.0 / 60.0);
82
68
            if c.is_finished() {
83
1
                break;
84
67
            }
85
        }
86
1
        assert!(c.is_finished(), "spring did not settle within 10s");
87
1
        assert_eq!(c.current, 100.0);
88
1
        assert_eq!(c.velocity, 0.0);
89
1
    }
90

            
91
    #[test]
92
1
    fn a_curve_reaches_its_target_at_the_duration() {
93
        // NOTE the frame budget: 60 ticks of 1/60 sum to 0.99999994, not 1.0,
94
        // so a curve legitimately lands on the frame AFTER its nominal
95
        // duration. Asserting exact arrival at tick 60 would be asserting that
96
        // f32 addition is exact.
97
1
        let mut c = AnimChannel::curve(0.0, 10.0, AnimationInterpolationFunction::Linear, 1.0);
98
61
        for _ in 0..60 {
99
60
            c.tick(1.0 / 60.0);
100
60
        }
101
1
        assert!(
102
1
            (c.current - 10.0).abs() < 0.01,
103
            "should be at the target within a frame, got {}",
104
            c.current
105
        );
106
1
        c.tick(1.0 / 60.0);
107
1
        assert!(
108
1
            c.is_finished(),
109
            "curve did not finish one frame past its duration"
110
        );
111
1
        assert_eq!(
112
            c.current, 10.0,
113
            "a finished curve must land exactly on `to`"
114
        );
115
1
    }
116

            
117
    #[test]
118
1
    fn retarget_preserves_position_and_velocity() {
119
        // THE differentiator: mid-flight redirect must not snap back to a new
120
        // `from`, and must keep the momentum it had.
121
1
        let mut c = AnimChannel::spring(0.0, 100.0, Spring::SMOOTH);
122
11
        for _ in 0..10 {
123
10
            c.tick(1.0 / 60.0);
124
10
        }
125
1
        let value_before = c.current;
126
1
        let velocity_before = c.velocity;
127
1
        assert!(
128
1
            value_before > 0.0 && velocity_before > 0.0,
129
            "should be mid-flight"
130
        );
131

            
132
1
        c.retarget(-50.0);
133

            
134
1
        assert_eq!(c.current, value_before, "retarget must not move the value");
135
1
        assert_eq!(
136
            c.velocity, velocity_before,
137
            "retarget must not discard velocity"
138
        );
139
1
        assert_eq!(c.from, value_before);
140
1
        assert_eq!(c.to, -50.0);
141
1
        assert!(!c.is_finished());
142
1
    }
143

            
144
    #[test]
145
1
    fn retargeting_to_the_same_target_does_not_restart_the_clock() {
146
1
        let mut c = AnimChannel::curve(0.0, 10.0, AnimationInterpolationFunction::Linear, 1.0);
147
1
        c.tick(0.5);
148
1
        let elapsed = c.elapsed_secs;
149
1
        c.retarget(10.0);
150
1
        assert_eq!(
151
            c.elapsed_secs, elapsed,
152
            "a no-op retarget restarted the animation"
153
        );
154
1
    }
155

            
156
    #[test]
157
1
    fn a_settled_spring_can_be_woken_by_a_retarget() {
158
1
        let mut c = AnimChannel::spring(0.0, 1.0, Spring::SNAPPY);
159
27
        for _ in 0..600 {
160
27
            c.tick(1.0 / 60.0);
161
27
            if c.is_finished() {
162
1
                break;
163
26
            }
164
        }
165
1
        assert!(c.is_finished());
166
1
        c.retarget(0.0);
167
1
        assert!(
168
1
            !c.is_finished(),
169
            "retarget must un-finish a settled channel"
170
        );
171
1
        c.tick(1.0 / 60.0);
172
1
        assert!(
173
1
            c.current < 1.0,
174
            "woken channel did not move toward the new target"
175
        );
176
1
    }
177

            
178
    #[test]
179
1
    fn a_huge_frame_gap_cannot_fling_a_spring() {
180
        // A stalled frame must be clamped, not integrated verbatim.
181
1
        let mut c = AnimChannel::spring(0.0, 1.0, Spring::SNAPPY);
182
1
        c.tick(10.0);
183
1
        assert!(c.current.is_finite());
184
1
        assert!(c.current.abs() < 100.0, "clamping failed: {}", c.current);
185
1
    }
186

            
187
    #[test]
188
1
    fn zero_duration_curves_apply_instantly() {
189
1
        let mut c = AnimChannel::curve(0.0, 42.0, AnimationInterpolationFunction::Ease, 0.0);
190
1
        c.tick(0.0);
191
1
        assert!(c.is_finished());
192
1
        assert_eq!(c.current, 42.0);
193
1
    }
194

            
195
    #[test]
196
1
    fn easing_curves_are_pinned_at_both_ends() {
197
5
        for f in [
198
1
            AnimationInterpolationFunction::Linear,
199
1
            AnimationInterpolationFunction::Ease,
200
1
            AnimationInterpolationFunction::EaseIn,
201
1
            AnimationInterpolationFunction::EaseOut,
202
1
            AnimationInterpolationFunction::EaseInOut,
203
        ] {
204
5
            assert_eq!(ease(f, 0.0), 0.0, "{f:?} did not start at 0");
205
5
            assert_eq!(ease(f, 1.0), 1.0, "{f:?} did not end at 1");
206
            // Out of range must clamp, not extrapolate.
207
5
            assert_eq!(ease(f, -1.0), 0.0);
208
5
            assert_eq!(ease(f, 2.0), 1.0);
209
        }
210
1
    }
211

            
212
    #[test]
213
1
    fn ease_in_starts_slower_than_linear_and_ease_out_starts_faster() {
214
1
        let t = 0.25;
215
1
        let linear = ease(AnimationInterpolationFunction::Linear, t);
216
1
        assert!(ease(AnimationInterpolationFunction::EaseIn, t) < linear);
217
1
        assert!(ease(AnimationInterpolationFunction::EaseOut, t) > linear);
218
1
    }
219

            
220
    #[test]
221
1
    fn damping_ratio_identifies_the_regime() {
222
        // Critically damped: damping = 2*sqrt(k*m).
223
1
        let critical = Spring {
224
1
            stiffness: 100.0,
225
1
            damping: 20.0,
226
1
            mass: 1.0,
227
1
        };
228
1
        assert!((critical.damping_ratio() - 1.0).abs() < 1e-5);
229
1
        assert!(
230
1
            Spring {
231
1
                stiffness: 100.0,
232
1
                damping: 5.0,
233
1
                mass: 1.0
234
1
            }
235
1
            .damping_ratio()
236
1
                < 1.0
237
        );
238
1
        assert!(
239
1
            Spring {
240
1
                stiffness: 100.0,
241
1
                damping: 40.0,
242
1
                mass: 1.0
243
1
            }
244
1
            .damping_ratio()
245
1
                > 1.0
246
        );
247
1
    }
248

            
249
    #[test]
250
1
    fn a_degenerate_spring_snaps_instead_of_dividing_by_zero() {
251
1
        let s = Spring {
252
1
            stiffness: 100.0,
253
1
            damping: 10.0,
254
1
            mass: 0.0,
255
1
        };
256
1
        let (value, velocity) = s.step(0.0, 5.0, 0.0, 1.0 / 60.0);
257
1
        assert_eq!(value, 5.0);
258
1
        assert_eq!(velocity, 0.0);
259
1
    }
260

            
261
    #[test]
262
1
    fn the_manager_retargets_instead_of_stacking() {
263
1
        let mut m = AnimationManager::new();
264
1
        let key = AnimKey(7);
265
1
        let mode = InterpolationMode::Spring(Spring::SMOOTH);
266

            
267
1
        m.start_or_retarget_move(
268
1
            key,
269
1
            flip(rect(0.0, 0.0, 10.0, 10.0), rect(100.0, 0.0, 10.0, 10.0)),
270
1
            mode,
271
        );
272
1
        assert_eq!(m.len(), 1);
273
11
        for _ in 0..10 {
274
10
            m.tick(1.0 / 60.0);
275
10
        }
276
1
        let mid = m.get(key).expect("still animating").current_transform();
277

            
278
        // A second move for the SAME key must not create a second animation.
279
1
        m.start_or_retarget_move(
280
1
            key,
281
1
            flip(rect(0.0, 0.0, 10.0, 10.0), rect(200.0, 0.0, 10.0, 10.0)),
282
1
            mode,
283
        );
284
1
        assert_eq!(m.len(), 1, "retarget created a second animation");
285
1
        let after = m.get(key).expect("still animating").current_transform();
286
1
        assert_ne!(
287
            after.translate_x, mid.translate_x,
288
            "retarget did not fold in the new offset"
289
        );
290
1
    }
291

            
292
    #[test]
293
1
    fn the_manager_reports_and_drops_finished_animations() {
294
1
        let mut m = AnimationManager::new();
295
1
        m.start_enter(
296
1
            AnimKey(1),
297
1
            (-120.0, 0.0),
298
1
            InterpolationMode::Curve {
299
1
                function: AnimationInterpolationFunction::Linear,
300
1
                duration_secs: 0.1,
301
1
            },
302
        );
303
1
        assert_eq!(m.len(), 1);
304
1
        let mut finished = Vec::new();
305
6
        for _ in 0..20 {
306
6
            finished = m.tick(1.0 / 60.0);
307
6
            if !finished.is_empty() {
308
1
                break;
309
5
            }
310
        }
311
1
        assert_eq!(finished, alloc::vec![AnimKey(1)]);
312
1
        assert!(m.is_empty(), "finished animation was not dropped");
313
1
    }
314

            
315
    #[test]
316
1
    fn an_exit_replaces_an_in_flight_move() {
317
        // The node is leaving; continuing toward a layout slot it will never
318
        // occupy would be wrong.
319
1
        let mut m = AnimationManager::new();
320
1
        let key = AnimKey(3);
321
1
        let mode = InterpolationMode::Spring(Spring::SMOOTH);
322
1
        m.start_or_retarget_move(
323
1
            key,
324
1
            flip(rect(0.0, 0.0, 10.0, 10.0), rect(50.0, 0.0, 10.0, 10.0)),
325
1
            mode,
326
        );
327
1
        assert_eq!(m.get(key).map(|a| a.class), Some(AnimClass::Move));
328
1
        m.start_exit(key, (-120.0, 0.0), mode);
329
1
        assert_eq!(m.get(key).map(|a| a.class), Some(AnimClass::Exit));
330
1
        assert_eq!(m.len(), 1);
331
1
    }
332

            
333
    #[test]
334
1
    fn the_anim_key_survives_a_node_id_change() {
335
        // THE property the whole store depends on. A keyed node that shifts
336
        // position in the array (a sibling was prepended) must keep its
337
        // identity — otherwise the second produce looks like a brand-new
338
        // animation and retargeting never fires.
339
        use crate::dom::NodeData;
340

            
341
1
        let tree_a = [NodeData::create_div().with_key("hero")];
342
1
        let tree_b = [
343
1
            NodeData::create_div().with_key("spacer"),
344
1
            NodeData::create_div().with_key("hero"),
345
1
        ];
346

            
347
1
        let key_a = AnimKey(calculate_reconciliation_key(&tree_a, &[], NodeId::ZERO));
348
1
        let key_b = AnimKey(calculate_reconciliation_key(&tree_b, &[], NodeId::new(1)));
349
1
        assert_eq!(
350
            key_a, key_b,
351
            "the same keyed node got two different AnimKeys"
352
        );
353

            
354
        // And a DIFFERENT key must not collide with it.
355
1
        let other = AnimKey(calculate_reconciliation_key(&tree_b, &[], NodeId::ZERO));
356
1
        assert_ne!(key_a, other);
357
1
    }
358

            
359
    #[test]
360
1
    fn correspondences_drop_pairs_with_no_geometry() {
361
        use crate::dom::NodeData;
362
1
        let new_data = [
363
1
            NodeData::create_div().with_key("a"),
364
1
            NodeData::create_div().with_key("b"),
365
1
        ];
366
1
        let moves = [
367
1
            NodeMove {
368
1
                old_node_id: NodeId::ZERO,
369
1
                new_node_id: NodeId::ZERO,
370
1
            },
371
1
            NodeMove {
372
1
                old_node_id: NodeId::new(1),
373
1
                new_node_id: NodeId::new(1),
374
1
            },
375
1
        ];
376
1
        let r = rect(0.0, 0.0, 10.0, 10.0);
377
1
        let out = correspondences_from_moves(
378
1
            &moves,
379
1
            &new_data,
380
1
            &[],
381
2
            |id| (id == NodeId::ZERO).then_some(r), // only node 0 existed before
382
2
            |_| Some(rect(5.0, 0.0, 10.0, 10.0)),
383
        );
384
1
        assert_eq!(
385
1
            out.len(),
386
            1,
387
            "a node with no previous geometry has nothing to fly from"
388
        );
389
1
    }
390

            
391
    #[test]
392
1
    fn seed_moves_skips_nodes_that_did_not_move() {
393
        // An identity FLIP would take a GPU key and animate nothing.
394
1
        let mut m = AnimationManager::new();
395
1
        let stayed = rect(0.0, 0.0, 10.0, 10.0);
396
1
        let moved_first = rect(0.0, 0.0, 10.0, 10.0);
397
1
        let moved_last = rect(40.0, 0.0, 10.0, 10.0);
398
1
        let seeded = seed_moves(
399
1
            &mut m,
400
1
            [
401
1
                (AnimKey(1), stayed, stayed),
402
1
                (AnimKey(2), moved_first, moved_last),
403
1
            ],
404
1
            InterpolationMode::Spring(Spring::SMOOTH),
405
        );
406
1
        assert_eq!(seeded, 1, "only the node that moved should animate");
407
1
        assert!(m.get(AnimKey(1)).is_none());
408
1
        assert!(m.get(AnimKey(2)).is_some());
409
1
    }
410

            
411
    #[test]
412
1
    fn seed_moves_retargets_a_key_that_is_already_animating() {
413
        // Two produces in quick succession must not stack two animations on
414
        // one node — that is the visible "fighting" artefact.
415
1
        let mut m = AnimationManager::new();
416
1
        let mode = InterpolationMode::Spring(Spring::SMOOTH);
417
1
        seed_moves(
418
1
            &mut m,
419
1
            [(
420
1
                AnimKey(9),
421
1
                rect(0.0, 0.0, 10.0, 10.0),
422
1
                rect(50.0, 0.0, 10.0, 10.0),
423
1
            )],
424
1
            mode,
425
        );
426
6
        for _ in 0..5 {
427
5
            m.tick(1.0 / 60.0);
428
5
        }
429
1
        let seeded = seed_moves(
430
1
            &mut m,
431
1
            [(
432
1
                AnimKey(9),
433
1
                rect(0.0, 0.0, 10.0, 10.0),
434
1
                rect(90.0, 0.0, 10.0, 10.0),
435
1
            )],
436
1
            mode,
437
        );
438
1
        assert_eq!(seeded, 1);
439
1
        assert_eq!(m.len(), 1, "a second produce stacked a second animation");
440
1
    }
441

            
442
    #[test]
443
1
    fn an_enter_does_not_clobber_an_animation_already_in_flight() {
444
1
        let mut m = AnimationManager::new();
445
1
        let key = AnimKey(5);
446
1
        let mode = InterpolationMode::Spring(Spring::SMOOTH);
447
1
        m.start_exit(key, (-120.0, 0.0), mode);
448
1
        m.start_enter(key, (-120.0, 0.0), mode);
449
1
        assert_eq!(m.get(key).map(|a| a.class), Some(AnimClass::Exit));
450
1
    }
451
}