@wandelbots/wandelbots-js-react-components 2.26.0-pr.feature-update-to-react-19.361.fad9b97 → 2.26.0-pr.feature-update-to-react-19.361.ec4e097

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.
@@ -0,0 +1,300 @@
1
+ import type {
2
+ DHParameter,
3
+ MotionGroupStateResponse,
4
+ } from "@wandelbots/nova-api/v1"
5
+ import { Object3D } from "three"
6
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
7
+
8
+ // Import the component for testing
9
+ import RobotAnimator from "./RobotAnimator"
10
+
11
+ // Mock the dependencies
12
+ vi.mock("../utils/hooks", () => ({
13
+ useAutorun: vi.fn(),
14
+ }))
15
+
16
+ vi.mock("./robotModelLogic", () => ({
17
+ collectJoints: vi.fn(),
18
+ }))
19
+
20
+ vi.mock("@react-spring/three", () => ({
21
+ Globals: {
22
+ assign: vi.fn(),
23
+ },
24
+ useSpring: vi.fn(),
25
+ }))
26
+
27
+ vi.mock("@react-three/fiber", () => ({
28
+ useThree: () => ({
29
+ invalidate: vi.fn(),
30
+ }),
31
+ }))
32
+
33
+ import { useSpring } from "@react-spring/three"
34
+ import { useAutorun } from "../utils/hooks"
35
+ import { collectJoints } from "./robotModelLogic"
36
+
37
+ const mockUseAutorun = useAutorun as any
38
+ const mockCollectJoints = collectJoints as any
39
+ const mockUseSpring = useSpring as any
40
+
41
+ describe("RobotAnimator Logic Tests", () => {
42
+ const mockMotionState: MotionGroupStateResponse = {
43
+ state: {
44
+ joint_position: {
45
+ joints: [0, 0.5, 1, 1.5, 2, 2.5],
46
+ },
47
+ },
48
+ } as any
49
+
50
+ const mockDhParameters: DHParameter[] = [
51
+ { theta: 0, reverse_rotation_direction: false },
52
+ { theta: 0.1, reverse_rotation_direction: true },
53
+ { theta: 0.2, reverse_rotation_direction: false },
54
+ { theta: 0.3, reverse_rotation_direction: true },
55
+ { theta: 0.4, reverse_rotation_direction: false },
56
+ { theta: 0.5, reverse_rotation_direction: true },
57
+ ]
58
+
59
+ let mockJointObjects: Object3D[]
60
+ let mockAxisValues: any
61
+ let mockSetSpring: any
62
+ let mockSpringConfig: any
63
+ let mockAutorunCallback: any
64
+
65
+ beforeEach(() => {
66
+ // Create mock joint objects
67
+ mockJointObjects = Array.from({ length: 6 }, (_, i) => {
68
+ const obj = new Object3D()
69
+ obj.name = `Joint_J${i + 1}`
70
+ obj.rotation.y = 0
71
+ return obj
72
+ })
73
+
74
+ // Mock axis values object that acts like spring values
75
+ mockAxisValues = {}
76
+ for (let i = 0; i < 6; i++) {
77
+ mockAxisValues[i] = {
78
+ get: vi
79
+ .fn()
80
+ .mockReturnValue(mockMotionState.state.joint_position.joints[i]),
81
+ }
82
+ }
83
+
84
+ mockSetSpring = {
85
+ start: vi.fn(),
86
+ }
87
+
88
+ // Mock spring config that will be returned by useSpring
89
+ mockSpringConfig = {
90
+ onChange: vi.fn(),
91
+ onResolve: vi.fn(),
92
+ }
93
+
94
+ // Mock collectJoints to return our mock joint objects
95
+ mockCollectJoints.mockReturnValue(mockJointObjects)
96
+
97
+ // Mock useSpring to return our mock values and accept a factory function
98
+ mockUseSpring.mockImplementation((factory: any) => {
99
+ const config = factory()
100
+ mockSpringConfig.onChange = config.onChange
101
+ mockSpringConfig.onResolve = config.onResolve
102
+ return [mockAxisValues, mockSetSpring]
103
+ })
104
+
105
+ // Mock useAutorun to store the callback
106
+ mockUseAutorun.mockImplementation((callback: any) => {
107
+ mockAutorunCallback = callback
108
+ })
109
+ })
110
+
111
+ afterEach(() => {
112
+ vi.clearAllMocks()
113
+ })
114
+
115
+ it("should have correct component structure", () => {
116
+ expect(RobotAnimator).toBeDefined()
117
+ expect(typeof RobotAnimator).toBe("function")
118
+ })
119
+
120
+ it("should test joint rotation calculation logic", () => {
121
+ // Test the joint rotation calculation logic separately
122
+ const joints = mockJointObjects
123
+ const jointValues = [0, 0.5, 1, 1.5, 2, 2.5]
124
+ const dhParameters = mockDhParameters
125
+
126
+ // Simulate the rotation calculation that would happen in the component
127
+ joints.forEach((joint, index) => {
128
+ const dhParam = dhParameters[index]
129
+ const rotationOffset = dhParam.theta || 0
130
+ const rotationSign = dhParam.reverse_rotation_direction ? -1 : 1
131
+
132
+ joint.rotation.y = rotationSign * jointValues[index]! + rotationOffset
133
+ })
134
+
135
+ // Verify rotations are set correctly
136
+ expect(joints[0].rotation.y).toBe(0 * 1 + 0) // 0
137
+ expect(joints[1].rotation.y).toBe(0.5 * -1 + 0.1) // -0.4
138
+ expect(joints[2].rotation.y).toBe(1 * 1 + 0.2) // 1.2
139
+ expect(joints[3].rotation.y).toBe(1.5 * -1 + 0.3) // -1.2
140
+ expect(joints[4].rotation.y).toBe(2 * 1 + 0.4) // 2.4
141
+ expect(joints[5].rotation.y).toBe(2.5 * -1 + 0.5) // -2.0
142
+ })
143
+
144
+ it("should test joint value filtering logic", () => {
145
+ // Test the filtering logic that removes undefined values
146
+ const inputJoints = [1, undefined, 3, undefined, 5, 6]
147
+ const filteredJoints = inputJoints.filter((item) => item !== undefined)
148
+
149
+ expect(filteredJoints).toEqual([1, 3, 5, 6])
150
+ expect(filteredJoints.length).toBe(4)
151
+ })
152
+
153
+ it("should test DH parameter application", () => {
154
+ // Test various DH parameter combinations
155
+ const testCases = [
156
+ {
157
+ jointValue: 1.0,
158
+ dhParam: { theta: 0, reverse_rotation_direction: false },
159
+ expected: 1.0,
160
+ },
161
+ {
162
+ jointValue: 1.0,
163
+ dhParam: { theta: 0.5, reverse_rotation_direction: false },
164
+ expected: 1.5,
165
+ },
166
+ {
167
+ jointValue: 1.0,
168
+ dhParam: { theta: 0, reverse_rotation_direction: true },
169
+ expected: -1.0,
170
+ },
171
+ {
172
+ jointValue: 1.0,
173
+ dhParam: { theta: 0.5, reverse_rotation_direction: true },
174
+ expected: -0.5,
175
+ },
176
+ ]
177
+
178
+ testCases.forEach(({ jointValue, dhParam, expected }) => {
179
+ const joint = new Object3D()
180
+ const rotationOffset = dhParam.theta || 0
181
+ const rotationSign = dhParam.reverse_rotation_direction ? -1 : 1
182
+
183
+ joint.rotation.y = rotationSign * jointValue + rotationOffset
184
+
185
+ expect(joint.rotation.y).toBeCloseTo(expected, 5)
186
+ })
187
+ })
188
+
189
+ it("should test component props validation", () => {
190
+ // Test that the component has the correct prop structure
191
+ // We can't call the component directly without React context,
192
+ // so we'll validate the prop types instead
193
+ const validProps = {
194
+ rapidlyChangingMotionState: mockMotionState,
195
+ dhParameters: mockDhParameters,
196
+ onRotationChanged: vi.fn(),
197
+ children: null,
198
+ }
199
+
200
+ // Verify the prop structure matches expected types
201
+ expect(validProps.rapidlyChangingMotionState).toBeDefined()
202
+ expect(validProps.dhParameters).toBeDefined()
203
+ expect(Array.isArray(validProps.dhParameters)).toBe(true)
204
+ expect(typeof validProps.onRotationChanged).toBe("function")
205
+ expect(validProps.children).toBeDefined()
206
+ })
207
+
208
+ it("should test empty joint array handling", () => {
209
+ // Test behavior when no joints are available
210
+ const emptyJoints: Object3D[] = []
211
+
212
+ // This simulates the isInitialized check in the component
213
+ const isInitialized =
214
+ emptyJoints.length > 0 && emptyJoints.every((obj) => obj != null)
215
+
216
+ expect(isInitialized).toBe(false)
217
+ })
218
+
219
+ it("should test joint object validation", () => {
220
+ // Test the joint object validation logic
221
+ const validJoints = [new Object3D(), new Object3D(), new Object3D()]
222
+ const invalidJoints = [new Object3D(), null, new Object3D()]
223
+
224
+ const isValidJointsInitialized =
225
+ validJoints.length > 0 && validJoints.every((obj) => obj != null)
226
+ const isInvalidJointsInitialized =
227
+ invalidJoints.length > 0 && invalidJoints.every((obj) => obj != null)
228
+
229
+ expect(isValidJointsInitialized).toBe(true)
230
+ expect(isInvalidJointsInitialized).toBe(false)
231
+ })
232
+
233
+ it("should test motion state data structure", () => {
234
+ // Test that motion state has the expected structure
235
+ expect(mockMotionState.state).toBeDefined()
236
+ expect(mockMotionState.state.joint_position).toBeDefined()
237
+ expect(mockMotionState.state.joint_position.joints).toBeDefined()
238
+ expect(Array.isArray(mockMotionState.state.joint_position.joints)).toBe(
239
+ true,
240
+ )
241
+ expect(mockMotionState.state.joint_position.joints.length).toBe(6)
242
+ })
243
+
244
+ it("should test DH parameters structure", () => {
245
+ // Test that DH parameters have the expected structure
246
+ mockDhParameters.forEach((param, index) => {
247
+ expect(param).toBeDefined()
248
+ expect(typeof param.theta).toBe("number")
249
+ expect(typeof param.reverse_rotation_direction).toBe("boolean")
250
+ })
251
+ })
252
+
253
+ it("should test onRotationChanged callback structure", () => {
254
+ // Test callback function signature
255
+ const mockCallback = vi.fn()
256
+ const testJoints = [new Object3D(), new Object3D()]
257
+ const testValues = [1.0, 2.0]
258
+
259
+ // Simulate calling the callback
260
+ mockCallback(testJoints, testValues)
261
+
262
+ expect(mockCallback).toHaveBeenCalledWith(testJoints, testValues)
263
+ expect(mockCallback).toHaveBeenCalledTimes(1)
264
+ })
265
+
266
+ it("should test race condition prevention logic", () => {
267
+ // Test the initialization state logic that prevents race conditions
268
+ let isInitialized = false
269
+ const joints: Object3D[] = []
270
+
271
+ // Simulate setGroupRef logic
272
+ function simulateSetGroupRef(group: any) {
273
+ if (!group) {
274
+ isInitialized = false
275
+ return
276
+ }
277
+
278
+ // Simulate collectJoints
279
+ const collectedJoints = group ? [new Object3D(), new Object3D()] : []
280
+
281
+ // Only mark as initialized if we have valid joint objects
282
+ if (
283
+ collectedJoints.length > 0 &&
284
+ collectedJoints.every((obj) => obj != null)
285
+ ) {
286
+ isInitialized = true
287
+ } else {
288
+ isInitialized = false
289
+ }
290
+ }
291
+
292
+ // Test null group
293
+ simulateSetGroupRef(null)
294
+ expect(isInitialized).toBe(false)
295
+
296
+ // Test valid group
297
+ simulateSetGroupRef({ type: "Group" })
298
+ expect(isInitialized).toBe(true)
299
+ })
300
+ })
@@ -0,0 +1,111 @@
1
+ import "@testing-library/jest-dom"
2
+ import { beforeAll, vi } from "vitest"
3
+
4
+ // Mock ResizeObserver
5
+ global.ResizeObserver = vi.fn().mockImplementation(() => ({
6
+ observe: vi.fn(),
7
+ unobserve: vi.fn(),
8
+ disconnect: vi.fn(),
9
+ }))
10
+
11
+ // Mock IntersectionObserver
12
+ global.IntersectionObserver = vi.fn().mockImplementation(() => ({
13
+ observe: vi.fn(),
14
+ unobserve: vi.fn(),
15
+ disconnect: vi.fn(),
16
+ }))
17
+
18
+ // Mock matchMedia
19
+ Object.defineProperty(window, "matchMedia", {
20
+ writable: true,
21
+ value: vi.fn().mockImplementation((query) => ({
22
+ matches: false,
23
+ media: query,
24
+ onchange: null,
25
+ addListener: vi.fn(),
26
+ removeListener: vi.fn(),
27
+ addEventListener: vi.fn(),
28
+ removeEventListener: vi.fn(),
29
+ dispatchEvent: vi.fn(),
30
+ })),
31
+ })
32
+
33
+ // Mock WebGL context for Three.js
34
+ const mockWebGLContext = {
35
+ getExtension: vi.fn(),
36
+ getParameter: vi.fn(),
37
+ createShader: vi.fn(),
38
+ shaderSource: vi.fn(),
39
+ compileShader: vi.fn(),
40
+ createProgram: vi.fn(),
41
+ attachShader: vi.fn(),
42
+ linkProgram: vi.fn(),
43
+ useProgram: vi.fn(),
44
+ createBuffer: vi.fn(),
45
+ bindBuffer: vi.fn(),
46
+ bufferData: vi.fn(),
47
+ createTexture: vi.fn(),
48
+ bindTexture: vi.fn(),
49
+ texImage2D: vi.fn(),
50
+ texParameteri: vi.fn(),
51
+ generateMipmap: vi.fn(),
52
+ viewport: vi.fn(),
53
+ clearColor: vi.fn(),
54
+ clear: vi.fn(),
55
+ drawArrays: vi.fn(),
56
+ drawElements: vi.fn(),
57
+ enableVertexAttribArray: vi.fn(),
58
+ vertexAttribPointer: vi.fn(),
59
+ uniform1f: vi.fn(),
60
+ uniform2f: vi.fn(),
61
+ uniform3f: vi.fn(),
62
+ uniform4f: vi.fn(),
63
+ uniformMatrix4fv: vi.fn(),
64
+ getUniformLocation: vi.fn(),
65
+ getAttribLocation: vi.fn(),
66
+ enable: vi.fn(),
67
+ disable: vi.fn(),
68
+ depthFunc: vi.fn(),
69
+ blendFunc: vi.fn(),
70
+ cullFace: vi.fn(),
71
+ frontFace: vi.fn(),
72
+ VERTEX_SHADER: 35633,
73
+ FRAGMENT_SHADER: 35632,
74
+ ARRAY_BUFFER: 34962,
75
+ ELEMENT_ARRAY_BUFFER: 34963,
76
+ STATIC_DRAW: 35044,
77
+ DYNAMIC_DRAW: 35048,
78
+ TEXTURE_2D: 3553,
79
+ RGBA: 6408,
80
+ UNSIGNED_BYTE: 5121,
81
+ TEXTURE_MAG_FILTER: 10240,
82
+ TEXTURE_MIN_FILTER: 10241,
83
+ LINEAR: 9729,
84
+ COLOR_BUFFER_BIT: 16384,
85
+ DEPTH_BUFFER_BIT: 256,
86
+ DEPTH_TEST: 2929,
87
+ BLEND: 3042,
88
+ SRC_ALPHA: 770,
89
+ ONE_MINUS_SRC_ALPHA: 771,
90
+ CULL_FACE: 2884,
91
+ BACK: 1029,
92
+ CCW: 2305,
93
+ FLOAT: 5126,
94
+ FALSE: 0,
95
+ TRUE: 1,
96
+ }
97
+
98
+ HTMLCanvasElement.prototype.getContext = vi
99
+ .fn()
100
+ .mockImplementation((contextType) => {
101
+ if (contextType === "webgl" || contextType === "experimental-webgl") {
102
+ return mockWebGLContext
103
+ }
104
+ return null
105
+ })
106
+
107
+ beforeAll(() => {
108
+ // Suppress console errors in tests
109
+ vi.spyOn(console, "error").mockImplementation(() => {})
110
+ vi.spyOn(console, "warn").mockImplementation(() => {})
111
+ })