@wandelbots/wandelbots-js-react-components 2.26.0-pr.feature-update-to-react-19.361.d11d634 → 2.26.0-pr.feature-update-to-react-19.361.e3316bb

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wandelbots/wandelbots-js-react-components",
3
- "version": "2.26.0-pr.feature-update-to-react-19.361.d11d634",
3
+ "version": "2.26.0-pr.feature-update-to-react-19.361.e3316bb",
4
4
  "description": "React UI toolkit for building applications on top of the Wandelbots platform",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -0,0 +1,133 @@
1
+ import type {
2
+ DHParameter,
3
+ MotionGroupStateResponse,
4
+ } from "@wandelbots/nova-api/v1"
5
+ import { describe, expect, it } from "vitest"
6
+ import RobotAnimator from "./RobotAnimator"
7
+
8
+ describe("RobotAnimator", () => {
9
+ it("should export the component correctly", () => {
10
+ expect(RobotAnimator).toBeDefined()
11
+ expect(typeof RobotAnimator).toBe("function")
12
+ })
13
+
14
+ it("should handle props with different numbers of joints", () => {
15
+ // Test that the component accepts different numbers of DH parameters
16
+ const mockMotionState4Joints: MotionGroupStateResponse = {
17
+ state: {
18
+ joint_position: {
19
+ joints: [0.1, 0.2, 0.3, 0.4],
20
+ },
21
+ },
22
+ } as any
23
+
24
+ const mockDHParameters4Joints: DHParameter[] = [
25
+ { theta: 0, reverse_rotation_direction: false },
26
+ { theta: 0, reverse_rotation_direction: false },
27
+ { theta: 0, reverse_rotation_direction: false },
28
+ { theta: 0, reverse_rotation_direction: false },
29
+ ]
30
+
31
+ const mockMotionState7Joints: MotionGroupStateResponse = {
32
+ state: {
33
+ joint_position: {
34
+ joints: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7],
35
+ },
36
+ },
37
+ } as any
38
+
39
+ const mockDHParameters7Joints: DHParameter[] = Array(7).fill({
40
+ theta: 0,
41
+ reverse_rotation_direction: false,
42
+ })
43
+
44
+ // These should not throw TypeScript errors
45
+ expect(() => {
46
+ const props4 = {
47
+ rapidlyChangingMotionState: mockMotionState4Joints,
48
+ dhParameters: mockDHParameters4Joints,
49
+ children: null,
50
+ }
51
+ // Verify props are correctly typed
52
+ expect(props4.dhParameters).toHaveLength(4)
53
+ expect(
54
+ props4.rapidlyChangingMotionState.state.joint_position.joints,
55
+ ).toHaveLength(4)
56
+ }).not.toThrow()
57
+
58
+ expect(() => {
59
+ const props7 = {
60
+ rapidlyChangingMotionState: mockMotionState7Joints,
61
+ dhParameters: mockDHParameters7Joints,
62
+ children: null,
63
+ }
64
+ expect(props7.dhParameters).toHaveLength(7)
65
+ expect(
66
+ props7.rapidlyChangingMotionState.state.joint_position.joints,
67
+ ).toHaveLength(7)
68
+ }).not.toThrow()
69
+ })
70
+
71
+ it("should verify dynamic joint configuration logic", () => {
72
+ // Test the logic that would create dynamic spring configs
73
+ const testConfigs = [
74
+ { jointCount: 3, expectedJoints: ["joint0", "joint1", "joint2"] },
75
+ {
76
+ jointCount: 6,
77
+ expectedJoints: [
78
+ "joint0",
79
+ "joint1",
80
+ "joint2",
81
+ "joint3",
82
+ "joint4",
83
+ "joint5",
84
+ ],
85
+ },
86
+ {
87
+ jointCount: 8,
88
+ expectedJoints: [
89
+ "joint0",
90
+ "joint1",
91
+ "joint2",
92
+ "joint3",
93
+ "joint4",
94
+ "joint5",
95
+ "joint6",
96
+ "joint7",
97
+ ],
98
+ },
99
+ ]
100
+
101
+ testConfigs.forEach(({ jointCount, expectedJoints }) => {
102
+ // Simulate the logic from the component
103
+ const dhParameters = Array(jointCount).fill({
104
+ theta: 0,
105
+ reverse_rotation_direction: false,
106
+ })
107
+ const jointValues = Array(jointCount)
108
+ .fill(0)
109
+ .map((_, i) => i * 0.1)
110
+
111
+ const config: any = {
112
+ config: { tension: 120, friction: 20 },
113
+ onChange: () => {},
114
+ }
115
+
116
+ dhParameters.forEach((_, index) => {
117
+ config[`joint${index}`] = jointValues[index] || 0
118
+ })
119
+
120
+ // Verify the configuration has the expected joint properties
121
+ expectedJoints.forEach((jointKey) => {
122
+ expect(config[jointKey]).toBeDefined()
123
+ expect(typeof config[jointKey]).toBe("number")
124
+ })
125
+
126
+ // Verify no extra joints were added
127
+ const actualJointKeys = Object.keys(config).filter((key) =>
128
+ key.startsWith("joint"),
129
+ )
130
+ expect(actualJointKeys).toHaveLength(jointCount)
131
+ })
132
+ })
133
+ })
@@ -35,10 +35,45 @@ export default function RobotAnimator({
35
35
  invalidate()
36
36
  }
37
37
 
38
+ // Extract joint values
39
+ const jointValues = useMemo(
40
+ () =>
41
+ rapidlyChangingMotionState.state.joint_position.joints.filter(
42
+ (item) => item !== undefined,
43
+ ),
44
+ [rapidlyChangingMotionState.state.joint_position.joints],
45
+ )
46
+
47
+ // Create dynamic spring configuration based on actual number of joints
48
+ const springConfig = useMemo(() => {
49
+ const config: any = {
50
+ config: {
51
+ tension: 120,
52
+ friction: 20,
53
+ },
54
+ onChange: () => {
55
+ // This is critical: trigger setRotation when spring values change
56
+ setRotation()
57
+ invalidate()
58
+ },
59
+ }
60
+
61
+ // Add joint values dynamically based on dhParameters length
62
+ dhParameters.forEach((_, index) => {
63
+ config[`joint${index}`] = jointValues[index] || 0
64
+ })
65
+
66
+ return config
67
+ }, [jointValues, dhParameters.length])
68
+
69
+ const axisValues = useSpring(springConfig)
70
+
71
+ // Update setRotation to use the dynamic spring values
38
72
  function setRotation() {
39
- const updatedJointValues = jointObjects.current.map((_, objectIndex) =>
40
- (axisValues as any)[objectIndex].get(),
41
- )
73
+ const updatedJointValues = dhParameters.map((_, index) => {
74
+ const springValue = (axisValues as any)[`joint${index}`]
75
+ return springValue ? springValue.get() : 0
76
+ })
42
77
 
43
78
  if (onRotationChanged) {
44
79
  onRotationChanged(jointObjects.current, updatedJointValues)
@@ -54,30 +89,5 @@ export default function RobotAnimator({
54
89
  }
55
90
  }
56
91
 
57
- const jointValues = useMemo(
58
- () =>
59
- rapidlyChangingMotionState.state.joint_position.joints.filter(
60
- (item) => item !== undefined,
61
- ),
62
- [rapidlyChangingMotionState.state.joint_position.joints],
63
- )
64
-
65
- const targetValues = useMemo(
66
- () => Object.fromEntries(jointValues.map((value, index) => [index, value])),
67
- [jointValues],
68
- )
69
-
70
- const axisValues = useSpring({
71
- to: targetValues,
72
- config: {
73
- tension: 120,
74
- friction: 20,
75
- },
76
- onChange: () => {
77
- setRotation()
78
- invalidate()
79
- },
80
- })
81
-
82
92
  return <group ref={setGroupRef}>{children}</group>
83
93
  }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=RobotAnimator.unit.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"RobotAnimator.unit.test.d.ts","sourceRoot":"","sources":["../../../src/components/robots/RobotAnimator.unit.test.tsx"],"names":[],"mappings":""}
@@ -1,404 +0,0 @@
1
- import type { MotionGroupStateResponse } from "@wandelbots/nova-api/v1"
2
- import { Object3D } from "three"
3
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
4
-
5
- // Import the component for testing
6
- import RobotAnimator from "./RobotAnimator"
7
-
8
- // Mock the dependencies
9
- vi.mock("./robotModelLogic", () => ({
10
- collectJoints: vi.fn(),
11
- }))
12
-
13
- vi.mock("@react-spring/three", () => ({
14
- Globals: {
15
- assign: vi.fn(),
16
- },
17
- useSpring: vi.fn(),
18
- }))
19
-
20
- vi.mock("@react-three/fiber", () => ({
21
- useThree: () => ({
22
- invalidate: vi.fn(),
23
- }),
24
- }))
25
-
26
- import { useSpring } from "@react-spring/three"
27
- import { collectJoints } from "./robotModelLogic"
28
-
29
- const mockCollectJoints = collectJoints as any
30
- const mockUseSpring = useSpring as any
31
-
32
- describe("RobotAnimator Spring Animation Tests", () => {
33
- const mockMotionState: MotionGroupStateResponse = {
34
- state: {
35
- joint_position: {
36
- joints: [0, 0.5, 1, 1.5, 2, 2.5],
37
- },
38
- },
39
- } as any
40
-
41
- let mockAxisValues: any
42
- let mockApi: any
43
-
44
- beforeEach(() => {
45
- // Mock axis values object that acts like spring values
46
- mockAxisValues = {}
47
- for (let i = 0; i < 6; i++) {
48
- mockAxisValues[i] = {
49
- get: vi.fn().mockReturnValue(i * 0.5),
50
- }
51
- }
52
-
53
- mockApi = {
54
- start: vi.fn(),
55
- stop: vi.fn(),
56
- }
57
-
58
- // Mock collectJoints
59
- mockCollectJoints.mockReturnValue([])
60
-
61
- // Mock useSpring
62
- mockUseSpring.mockImplementation((factory: any) => {
63
- return [mockAxisValues, mockApi]
64
- })
65
- })
66
-
67
- afterEach(() => {
68
- vi.clearAllMocks()
69
- })
70
-
71
- it("should have correct component structure", () => {
72
- expect(RobotAnimator).toBeDefined()
73
- expect(typeof RobotAnimator).toBe("function")
74
- })
75
-
76
- it("should test spring will animate smoothly", () => {
77
- // Test that spring provides interpolated values during animation
78
- const interpolatedValues = [0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
79
-
80
- // Update mock to return interpolated values
81
- interpolatedValues.forEach((value, index) => {
82
- mockAxisValues[index] = {
83
- get: vi.fn().mockReturnValue(value),
84
- }
85
- })
86
-
87
- // Test reading animated values (what component does in setRotation)
88
- const animatedValues = Object.keys(mockAxisValues).map((key) => {
89
- const index = parseInt(key)
90
- const springValue = mockAxisValues[index]
91
- return typeof springValue === "object" &&
92
- springValue &&
93
- "get" in springValue
94
- ? springValue.get()
95
- : 0
96
- })
97
-
98
- // Verify we get interpolated values
99
- expect(animatedValues).toEqual(interpolatedValues)
100
-
101
- // Verify spring values were accessed
102
- interpolatedValues.forEach((_, index) => {
103
- expect(mockAxisValues[index].get).toHaveBeenCalled()
104
- })
105
- })
106
-
107
- it("should verify spring configuration enables animation", () => {
108
- // Test spring animation configuration
109
- const config = { tension: 120, friction: 20 }
110
-
111
- // Tension provides spring force (higher = faster animation)
112
- expect(config.tension).toBeGreaterThan(0)
113
-
114
- // Friction provides damping (prevents oscillation)
115
- expect(config.friction).toBeGreaterThan(0)
116
-
117
- // These values provide smooth, responsive animation
118
- expect(config.tension).toBe(120)
119
- expect(config.friction).toBe(20)
120
- })
121
-
122
- it("should confirm api.start will trigger animations", () => {
123
- // Test that spring API can trigger new animations
124
- const newValues = { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6 }
125
-
126
- // Call api.start (what useEffect does in component)
127
- mockApi.start(newValues)
128
-
129
- // Verify animation was triggered
130
- expect(mockApi.start).toHaveBeenCalledWith(newValues)
131
- expect(mockApi.start).toHaveBeenCalledTimes(1)
132
- })
133
-
134
- it("should verify smooth animation will work end-to-end", () => {
135
- // Comprehensive test showing animation will work
136
-
137
- // 1. Spring provides smooth interpolation
138
- let currentValue = 0
139
- const targetValue = 5
140
- const mockSpring = {
141
- get: vi.fn(() => currentValue),
142
- }
143
-
144
- // Simulate animation frames
145
- const animationFrames = [0, 1.25, 2.5, 3.75, 5] // Smooth progression
146
-
147
- animationFrames.forEach((value) => {
148
- currentValue = value
149
- const interpolatedValue = mockSpring.get()
150
- expect(typeof interpolatedValue).toBe("number")
151
- })
152
-
153
- // 2. onChange enables real-time updates
154
- let updateCount = 0
155
- const onChange = () => {
156
- updateCount++
157
- // In real component: setRotation() and invalidate()
158
- }
159
-
160
- // Simulate spring calling onChange during animation
161
- animationFrames.forEach(() => onChange())
162
- expect(updateCount).toBe(animationFrames.length)
163
-
164
- // 3. Joint rotation uses interpolated values
165
- const dhParam = { theta: 0.1, reverse_rotation_direction: false }
166
- const joint = new Object3D()
167
-
168
- animationFrames.forEach((animatedValue) => {
169
- const rotationOffset = dhParam.theta || 0
170
- const rotationSign = dhParam.reverse_rotation_direction ? -1 : 1
171
- joint.rotation.y = rotationSign * animatedValue + rotationOffset
172
- })
173
-
174
- // Final rotation uses final animated value
175
- expect(joint.rotation.y).toBe(1 * 5 + 0.1) // 5.1
176
-
177
- // 4. This confirms smooth animation: 0 → 1.25 → 2.5 → 3.75 → 5
178
- expect(animationFrames[0]).toBe(0)
179
- expect(animationFrames[animationFrames.length - 1]).toBe(5)
180
- expect(animationFrames.length).toBeGreaterThan(2) // Multiple frames = smooth
181
- })
182
-
183
- it("should use declarative useSpring without imperative API", () => {
184
- // This test verifies that we're using useSpring declaratively
185
- // Clear previous calls
186
- mockUseSpring.mockClear()
187
-
188
- // Mock useSpring to return spring values
189
- mockUseSpring.mockReturnValue({
190
- 0: { get: () => 0.1 },
191
- 1: { get: () => 0.2 },
192
- 2: { get: () => 0.3 },
193
- 3: { get: () => 0.4 },
194
- 4: { get: () => 0.5 },
195
- 5: { get: () => 0.6 },
196
- })
197
-
198
- // Simulate component instantiation (which would call useSpring)
199
- mockUseSpring({
200
- from: { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0 },
201
- to: { 0: 0, 1: 0.5, 2: 1, 3: 1.5, 4: 2, 5: 2.5 },
202
- config: { tension: 120, friction: 20 },
203
- onChange: () => {},
204
- onResolve: () => {},
205
- })
206
-
207
- // Verify useSpring was called with declarative configuration
208
- expect(mockUseSpring).toHaveBeenCalledWith({
209
- from: { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0 },
210
- to: { 0: 0, 1: 0.5, 2: 1, 3: 1.5, 4: 2, 5: 2.5 },
211
- config: { tension: 120, friction: 20 },
212
- onChange: expect.any(Function),
213
- onResolve: expect.any(Function),
214
- })
215
-
216
- // Verify we have spring values available
217
- const springValues = mockUseSpring.mock.results[0].value
218
- expect(springValues[0].get()).toBe(0.1)
219
- expect(springValues[3].get()).toBe(0.4)
220
- })
221
-
222
- it("should verify declarative pattern works with changing motion state", () => {
223
- // Test that the declarative pattern responds to motion state changes
224
- mockUseSpring.mockClear()
225
-
226
- // First motion state
227
- const motionState1 = {
228
- state: { joint_position: { joints: [0, 0.5, 1, 1.5, 2, 2.5] } },
229
- }
230
-
231
- // Second motion state (different values)
232
- const motionState2 = {
233
- state: { joint_position: { joints: [1, 1.5, 2, 2.5, 3, 3.5] } },
234
- }
235
-
236
- // Mock spring to track the target values
237
- let lastTargetValues: any = null
238
- mockUseSpring.mockImplementation((config: any) => {
239
- if (config && typeof config === "object" && "to" in config) {
240
- lastTargetValues = config.to
241
- }
242
- return {
243
- 0: { get: () => config?.to?.[0] || 0 },
244
- 1: { get: () => config?.to?.[1] || 0 },
245
- 2: { get: () => config?.to?.[2] || 0 },
246
- 3: { get: () => config?.to?.[3] || 0 },
247
- 4: { get: () => config?.to?.[4] || 0 },
248
- 5: { get: () => config?.to?.[5] || 0 },
249
- }
250
- })
251
-
252
- // Simulate first render with motionState1
253
- const filteredJoints1 = motionState1.state.joint_position.joints.filter(
254
- (item) => item !== undefined,
255
- )
256
- const targetValues1 = Object.fromEntries(
257
- filteredJoints1.map((value, index) => [index, value]),
258
- )
259
-
260
- mockUseSpring({
261
- from: Object.fromEntries(filteredJoints1.map((_, index) => [index, 0])),
262
- to: targetValues1,
263
- config: { tension: 120, friction: 20 },
264
- onChange: () => {},
265
- onResolve: () => {},
266
- })
267
-
268
- // Verify the target values match the motion state
269
- expect(lastTargetValues).toEqual({
270
- 0: 0,
271
- 1: 0.5,
272
- 2: 1,
273
- 3: 1.5,
274
- 4: 2,
275
- 5: 2.5,
276
- })
277
-
278
- // This confirms the declarative pattern: motion state changes -> target values change
279
- expect(mockUseSpring).toHaveBeenCalledWith(
280
- expect.objectContaining({
281
- to: { 0: 0, 1: 0.5, 2: 1, 3: 1.5, 4: 2, 5: 2.5 },
282
- }),
283
- )
284
- })
285
-
286
- it("should handle rapid motion state updates without freezing", () => {
287
- // This test verifies the fix for the "robot doesn't move with constant updates" issue
288
-
289
- let springCallCount = 0
290
- let lastConfig: any = null
291
-
292
- mockUseSpring.mockImplementation((config: any) => {
293
- springCallCount++
294
- lastConfig = config
295
-
296
- // Return mock spring values that simulate continuous animation
297
- return {
298
- 0: { get: () => config?.to?.[0] || 0 },
299
- 1: { get: () => config?.to?.[1] || 0 },
300
- 2: { get: () => config?.to?.[2] || 0 },
301
- 3: { get: () => config?.to?.[3] || 0 },
302
- 4: { get: () => config?.to?.[4] || 0 },
303
- 5: { get: () => config?.to?.[5] || 0 },
304
- }
305
- })
306
-
307
- // Simulate rapid motion state updates (like what happens in real usage)
308
- const motionStates = [
309
- { state: { joint_position: { joints: [0, 0.5, 1, 1.5, 2, 2.5] } } },
310
- { state: { joint_position: { joints: [0.1, 0.6, 1.1, 1.6, 2.1, 2.6] } } },
311
- { state: { joint_position: { joints: [0.2, 0.7, 1.2, 1.7, 2.2, 2.7] } } },
312
- { state: { joint_position: { joints: [0.3, 0.8, 1.3, 1.8, 2.3, 2.8] } } },
313
- ]
314
-
315
- motionStates.forEach((motionState, index) => {
316
- const filteredJoints = motionState.state.joint_position.joints.filter(
317
- (item) => item !== undefined,
318
- )
319
- const targetValues = Object.fromEntries(
320
- filteredJoints.map((value, index) => [index, value]),
321
- )
322
-
323
- // This simulates what the CURRENT implementation does
324
- // NEW implementation should NOT have 'from' property and should use same config as CartesianJoggingAxisVisualization
325
- mockUseSpring({
326
- to: targetValues,
327
- config: {
328
- tension: 120, // Same as CartesianJoggingAxisVisualization
329
- friction: 20, // Same as CartesianJoggingAxisVisualization
330
- },
331
- onChange: () => {},
332
- // Note: no 'onResolve' in new implementation, no 'from' property
333
- })
334
- })
335
-
336
- // TEST FOR NEW IMPLEMENTATION (should pass with our fix):
337
- // The spring configuration should NOT include 'from' (which would cause restart from zero)
338
- expect(lastConfig).not.toHaveProperty("from")
339
- expect(lastConfig).toHaveProperty("to")
340
-
341
- // TEST FOR CONSISTENT CONFIGURATION:
342
- // Should use same config as CartesianJoggingAxisVisualization
343
- expect(lastConfig.config).toEqual({
344
- tension: 120, // Same as CartesianJoggingAxisVisualization
345
- friction: 20, // Same as CartesianJoggingAxisVisualization
346
- })
347
-
348
- // Verify we can handle multiple rapid updates
349
- expect(springCallCount).toBeGreaterThan(1)
350
-
351
- // The final target should be the last motion state
352
- expect(lastConfig.to).toEqual({
353
- 0: 0.3,
354
- 1: 0.8,
355
- 2: 1.3,
356
- 3: 1.8,
357
- 4: 2.3,
358
- 5: 2.8,
359
- })
360
- })
361
-
362
- it("should prove old implementation had 'from' property causing restart issues", () => {
363
- // This test simulates the old problematic implementation exactly
364
-
365
- let lastConfig: any = null
366
- mockUseSpring.mockImplementation((config: any) => {
367
- lastConfig = config
368
- return {
369
- 0: { get: () => 0 },
370
- 1: { get: () => 0 },
371
- 2: { get: () => 0 },
372
- 3: { get: () => 0 },
373
- 4: { get: () => 0 },
374
- 5: { get: () => 0 },
375
- }
376
- })
377
-
378
- // Simulate what the OLD implementation would do
379
- const jointValues = [0, 0.5, 1, 1.5, 2, 2.5]
380
- const targetValues = Object.fromEntries(
381
- jointValues.map((value, index) => [index, value]),
382
- )
383
-
384
- // OLD IMPLEMENTATION - this is the problematic pattern
385
- mockUseSpring({
386
- from: Object.fromEntries(jointValues.map((_, index) => [index, 0])), // ❌ ALWAYS starts from zero!
387
- to: targetValues,
388
- config: { tension: 120, friction: 20 },
389
- onChange: () => {},
390
- onResolve: () => {},
391
- })
392
-
393
- // Verify the old implementation has the problematic 'from' property
394
- expect(lastConfig).toHaveProperty("from")
395
- expect(lastConfig.from).toEqual({ 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0 })
396
-
397
- // This is WHY the robot froze with rapid updates:
398
- // Every new motion state would restart the animation from zero!
399
- console.log(
400
- "❌ OLD IMPLEMENTATION PROBLEM: Always restarts from zero:",
401
- lastConfig.from,
402
- )
403
- })
404
- })