|
| 1 | +import * as THREEJS from "three"; |
| 2 | +import { BitByBitBase } from "./bitbybit-base"; |
| 3 | + |
| 4 | +describe("BitByBitBase unit tests", () => { |
| 5 | + let bitByBit: BitByBitBase; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + bitByBit = new BitByBitBase(); |
| 9 | + }); |
| 10 | + |
| 11 | + describe("Constructor initialization", () => { |
| 12 | + it("should create a BitByBitBase instance", () => { |
| 13 | + expect(bitByBit).toBeDefined(); |
| 14 | + expect(bitByBit).toBeInstanceOf(BitByBitBase); |
| 15 | + }); |
| 16 | + |
| 17 | + it("should initialize context", () => { |
| 18 | + expect(bitByBit.context).toBeDefined(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("should initialize worker managers", () => { |
| 22 | + expect(bitByBit.jscadWorkerManager).toBeDefined(); |
| 23 | + expect(bitByBit.manifoldWorkerManager).toBeDefined(); |
| 24 | + expect(bitByBit.occtWorkerManager).toBeDefined(); |
| 25 | + }); |
| 26 | + |
| 27 | + it("should initialize math service", () => { |
| 28 | + expect(bitByBit.math).toBeDefined(); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should initialize logic service", () => { |
| 32 | + expect(bitByBit.logic).toBeDefined(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should initialize lists service", () => { |
| 36 | + expect(bitByBit.lists).toBeDefined(); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should initialize json service", () => { |
| 40 | + expect(bitByBit.json).toBeDefined(); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should initialize vector service", () => { |
| 44 | + expect(bitByBit.vector).toBeDefined(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should initialize three service", () => { |
| 48 | + expect(bitByBit.three).toBeDefined(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should initialize point service", () => { |
| 52 | + expect(bitByBit.point).toBeDefined(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should initialize line service", () => { |
| 56 | + expect(bitByBit.line).toBeDefined(); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should initialize transforms service", () => { |
| 60 | + expect(bitByBit.transforms).toBeDefined(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should initialize polyline service", () => { |
| 64 | + expect(bitByBit.polyline).toBeDefined(); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should initialize draw service", () => { |
| 68 | + expect(bitByBit.draw).toBeDefined(); |
| 69 | + }); |
| 70 | + |
| 71 | + it("should initialize verb service", () => { |
| 72 | + expect(bitByBit.verb).toBeDefined(); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should initialize jscad service", () => { |
| 76 | + expect(bitByBit.jscad).toBeDefined(); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should initialize manifold service", () => { |
| 80 | + expect(bitByBit.manifold).toBeDefined(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should initialize text service", () => { |
| 84 | + expect(bitByBit.text).toBeDefined(); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should initialize dates service", () => { |
| 88 | + expect(bitByBit.dates).toBeDefined(); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should initialize tag service", () => { |
| 92 | + expect(bitByBit.tag).toBeDefined(); |
| 93 | + }); |
| 94 | + |
| 95 | + it("should initialize time service", () => { |
| 96 | + expect(bitByBit.time).toBeDefined(); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should initialize occt service", () => { |
| 100 | + expect(bitByBit.occt).toBeDefined(); |
| 101 | + }); |
| 102 | + |
| 103 | + it("should initialize mesh service", () => { |
| 104 | + expect(bitByBit.mesh).toBeDefined(); |
| 105 | + }); |
| 106 | + |
| 107 | + it("should initialize asset service", () => { |
| 108 | + expect(bitByBit.asset).toBeDefined(); |
| 109 | + }); |
| 110 | + |
| 111 | + it("should initialize color service", () => { |
| 112 | + expect(bitByBit.color).toBeDefined(); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe("init method", () => { |
| 117 | + it("should initialize with scene only", () => { |
| 118 | + const scene = new THREEJS.Scene(); |
| 119 | + bitByBit.init(scene); |
| 120 | + expect(bitByBit.context.scene).toBe(scene); |
| 121 | + }); |
| 122 | + |
| 123 | + it("should set verb context", () => { |
| 124 | + const scene = new THREEJS.Scene(); |
| 125 | + bitByBit.init(scene); |
| 126 | + expect(bitByBit.context.verb).toBeDefined(); |
| 127 | + expect(bitByBit.context.verb.geom).toBeDefined(); |
| 128 | + expect(bitByBit.context.verb.core).toBeDefined(); |
| 129 | + }); |
| 130 | + |
| 131 | + it("should set jsonpath context", () => { |
| 132 | + const scene = new THREEJS.Scene(); |
| 133 | + bitByBit.init(scene); |
| 134 | + expect(bitByBit.context.jsonpath).toBeDefined(); |
| 135 | + }); |
| 136 | + |
| 137 | + it("should initialize with scene and occt worker", () => { |
| 138 | + const scene = new THREEJS.Scene(); |
| 139 | + const mockOcctWorker = { |
| 140 | + postMessage: jest.fn(), |
| 141 | + addEventListener: jest.fn(), |
| 142 | + removeEventListener: jest.fn(), |
| 143 | + terminate: jest.fn(), |
| 144 | + onmessage: null, |
| 145 | + onmessageerror: null, |
| 146 | + onerror: null, |
| 147 | + dispatchEvent: jest.fn(), |
| 148 | + } as unknown as Worker; |
| 149 | + |
| 150 | + bitByBit.init(scene, mockOcctWorker); |
| 151 | + expect(bitByBit.context.scene).toBe(scene); |
| 152 | + }); |
| 153 | + |
| 154 | + it("should initialize with scene and jscad worker", () => { |
| 155 | + const scene = new THREEJS.Scene(); |
| 156 | + const mockJscadWorker = { |
| 157 | + postMessage: jest.fn(), |
| 158 | + addEventListener: jest.fn(), |
| 159 | + removeEventListener: jest.fn(), |
| 160 | + terminate: jest.fn(), |
| 161 | + onmessage: null, |
| 162 | + onmessageerror: null, |
| 163 | + onerror: null, |
| 164 | + dispatchEvent: jest.fn(), |
| 165 | + } as unknown as Worker; |
| 166 | + |
| 167 | + bitByBit.init(scene, undefined, mockJscadWorker); |
| 168 | + expect(bitByBit.context.scene).toBe(scene); |
| 169 | + }); |
| 170 | + |
| 171 | + it("should initialize with scene and manifold worker", () => { |
| 172 | + const scene = new THREEJS.Scene(); |
| 173 | + const mockManifoldWorker = { |
| 174 | + postMessage: jest.fn(), |
| 175 | + addEventListener: jest.fn(), |
| 176 | + removeEventListener: jest.fn(), |
| 177 | + terminate: jest.fn(), |
| 178 | + onmessage: null, |
| 179 | + onmessageerror: null, |
| 180 | + onerror: null, |
| 181 | + dispatchEvent: jest.fn(), |
| 182 | + } as unknown as Worker; |
| 183 | + |
| 184 | + bitByBit.init(scene, undefined, undefined, mockManifoldWorker); |
| 185 | + expect(bitByBit.context.scene).toBe(scene); |
| 186 | + }); |
| 187 | + |
| 188 | + it("should initialize with all workers", () => { |
| 189 | + const scene = new THREEJS.Scene(); |
| 190 | + const createMockWorker = () => ({ |
| 191 | + postMessage: jest.fn(), |
| 192 | + addEventListener: jest.fn(), |
| 193 | + removeEventListener: jest.fn(), |
| 194 | + terminate: jest.fn(), |
| 195 | + onmessage: null, |
| 196 | + onmessageerror: null, |
| 197 | + onerror: null, |
| 198 | + dispatchEvent: jest.fn(), |
| 199 | + } as unknown as Worker); |
| 200 | + |
| 201 | + const mockOcctWorker = createMockWorker(); |
| 202 | + const mockJscadWorker = createMockWorker(); |
| 203 | + const mockManifoldWorker = createMockWorker(); |
| 204 | + |
| 205 | + bitByBit.init(scene, mockOcctWorker, mockJscadWorker, mockManifoldWorker); |
| 206 | + expect(bitByBit.context.scene).toBe(scene); |
| 207 | + }); |
| 208 | + |
| 209 | + it("should handle undefined workers gracefully", () => { |
| 210 | + const scene = new THREEJS.Scene(); |
| 211 | + expect(() => bitByBit.init(scene, undefined, undefined, undefined)).not.toThrow(); |
| 212 | + expect(bitByBit.context.scene).toBe(scene); |
| 213 | + }); |
| 214 | + }); |
| 215 | + |
| 216 | + describe("Service integration", () => { |
| 217 | + it("should have functional math operations", () => { |
| 218 | + // Test basic number creation instead of twoNrOperation which requires proper enum |
| 219 | + const result = bitByBit.math.number({ number: 42 }); |
| 220 | + expect(result).toBe(42); |
| 221 | + }); |
| 222 | + |
| 223 | + it("should have functional math rounding", () => { |
| 224 | + const result = bitByBit.math.roundToDecimals({ number: 3.14159, decimalPlaces: 2 }); |
| 225 | + expect(result).toBeCloseTo(3.14, 2); |
| 226 | + }); |
| 227 | + |
| 228 | + it("should have functional vector operations", () => { |
| 229 | + const result = bitByBit.vector.add({ first: [1, 2, 3], second: [4, 5, 6] }); |
| 230 | + expect(result).toEqual([5, 7, 9]); |
| 231 | + }); |
| 232 | + |
| 233 | + it("should have functional point operations", () => { |
| 234 | + const result = bitByBit.point.distance({ startPoint: [0, 0, 0], endPoint: [3, 4, 0] }); |
| 235 | + expect(result).toBeCloseTo(5, 5); |
| 236 | + }); |
| 237 | + |
| 238 | + it("should have functional line creation", () => { |
| 239 | + const result = bitByBit.line.create({ start: [0, 0, 0], end: [1, 1, 1] }); |
| 240 | + expect(result).toEqual({ start: [0, 0, 0], end: [1, 1, 1] }); |
| 241 | + }); |
| 242 | + |
| 243 | + it("should have functional polyline creation", () => { |
| 244 | + const result = bitByBit.polyline.create({ points: [[0, 0, 0], [1, 0, 0], [1, 1, 0]] }); |
| 245 | + expect(result.points).toEqual([[0, 0, 0], [1, 0, 0], [1, 1, 0]]); |
| 246 | + expect(result.isClosed).toBe(false); |
| 247 | + }); |
| 248 | + |
| 249 | + it("should have functional list operations", () => { |
| 250 | + const result = bitByBit.lists.getItem({ list: [10, 20, 30], index: 1 }); |
| 251 | + expect(result).toBe(20); |
| 252 | + }); |
| 253 | + |
| 254 | + it("should have functional transform operations", () => { |
| 255 | + const result = bitByBit.transforms.translationXYZ({ translation: [1, 2, 3] }); |
| 256 | + expect(result).toBeDefined(); |
| 257 | + expect(Array.isArray(result)).toBe(true); |
| 258 | + }); |
| 259 | + |
| 260 | + it("should have functional color operations", () => { |
| 261 | + const result = bitByBit.color.hexToRgb({ color: "#ff0000" }); |
| 262 | + expect(result).toEqual({ r: 255, g: 0, b: 0 }); |
| 263 | + }); |
| 264 | + |
| 265 | + it("should have functional mesh operations", () => { |
| 266 | + const plane = bitByBit.mesh.calculateTrianglePlane({ |
| 267 | + triangle: [[0, 0, 0], [1, 0, 0], [0, 1, 0]] |
| 268 | + }); |
| 269 | + expect(plane).toBeDefined(); |
| 270 | + expect(plane.normal).toBeDefined(); |
| 271 | + }); |
| 272 | + |
| 273 | + it("should have functional logic operations", () => { |
| 274 | + const result = bitByBit.logic.firstDefinedValueGate({ value1: undefined, value2: 5 }); |
| 275 | + expect(result).toBe(5); |
| 276 | + }); |
| 277 | + |
| 278 | + it("should have functional boolean operations", () => { |
| 279 | + const result = bitByBit.logic.boolean({ boolean: true }); |
| 280 | + expect(result).toBe(true); |
| 281 | + }); |
| 282 | + }); |
| 283 | + |
| 284 | + describe("Multiple instances", () => { |
| 285 | + it("should create independent instances", () => { |
| 286 | + const instance1 = new BitByBitBase(); |
| 287 | + const instance2 = new BitByBitBase(); |
| 288 | + |
| 289 | + expect(instance1).not.toBe(instance2); |
| 290 | + expect(instance1.context).not.toBe(instance2.context); |
| 291 | + }); |
| 292 | + |
| 293 | + it("should have independent scenes after init", () => { |
| 294 | + const instance1 = new BitByBitBase(); |
| 295 | + const instance2 = new BitByBitBase(); |
| 296 | + |
| 297 | + const scene1 = new THREEJS.Scene(); |
| 298 | + const scene2 = new THREEJS.Scene(); |
| 299 | + scene1.name = "scene1"; |
| 300 | + scene2.name = "scene2"; |
| 301 | + |
| 302 | + instance1.init(scene1); |
| 303 | + instance2.init(scene2); |
| 304 | + |
| 305 | + expect(instance1.context.scene).toBe(scene1); |
| 306 | + expect(instance2.context.scene).toBe(scene2); |
| 307 | + expect(instance1.context.scene).not.toBe(instance2.context.scene); |
| 308 | + }); |
| 309 | + }); |
| 310 | + |
| 311 | + describe("Scene manipulation after init", () => { |
| 312 | + it("should allow adding objects to the scene via draw", () => { |
| 313 | + const scene = new THREEJS.Scene(); |
| 314 | + bitByBit.init(scene); |
| 315 | + |
| 316 | + // Draw a point |
| 317 | + const result = bitByBit.draw.drawAny({ entity: [1, 2, 3] }); |
| 318 | + expect(result).toBeDefined(); |
| 319 | + expect(scene.children.length).toBeGreaterThan(0); |
| 320 | + }); |
| 321 | + |
| 322 | + it("should allow multiple draws to the same scene", () => { |
| 323 | + const scene = new THREEJS.Scene(); |
| 324 | + bitByBit.init(scene); |
| 325 | + |
| 326 | + bitByBit.draw.drawAny({ entity: [1, 2, 3] }); |
| 327 | + bitByBit.draw.drawAny({ entity: [4, 5, 6] }); |
| 328 | + bitByBit.draw.drawAny({ entity: [[0, 0, 0], [1, 1, 1]] }); // line |
| 329 | + |
| 330 | + expect(scene.children.length).toBeGreaterThanOrEqual(3); |
| 331 | + }); |
| 332 | + }); |
| 333 | +}); |
0 commit comments