|
| 1 | +import { test, expect } from 'vitest'; |
| 2 | +import { Path, PathType } from '../path'; |
| 3 | +import { ExtrusionGeometry } from '../extrusion-geometry'; |
| 4 | +import { BufferGeometry } from 'three'; |
| 5 | +import exp from 'constants'; |
| 6 | + |
| 7 | +test('.addPoint adds a point to the vertices', () => { |
| 8 | + const path = new Path(PathType.Travel, undefined, undefined, undefined); |
| 9 | + |
| 10 | + path.addPoint(1, 2, 3); |
| 11 | + |
| 12 | + expect(path.vertices).not.toBeNull(); |
| 13 | + expect(path.vertices.length).toEqual(3); |
| 14 | +}); |
| 15 | + |
| 16 | +test('.addPoint adds points at the end of vertices', () => { |
| 17 | + const path = new Path(PathType.Travel, undefined, undefined, undefined); |
| 18 | + |
| 19 | + path.addPoint(0, 0, 0); |
| 20 | + path.addPoint(1, 2, 3); |
| 21 | + path.addPoint(5, 6, 7); |
| 22 | + |
| 23 | + expect(path.vertices).not.toBeNull(); |
| 24 | + expect(path.vertices.length).toEqual(9); |
| 25 | + expect(path.vertices[6]).toEqual(5); |
| 26 | + expect(path.vertices[7]).toEqual(6); |
| 27 | + expect(path.vertices[8]).toEqual(7); |
| 28 | +}); |
| 29 | + |
| 30 | +test('.checkLineContinuity returns false if there are less than 3 vertices', () => { |
| 31 | + const path = new Path(PathType.Travel, undefined, undefined, undefined); |
| 32 | + |
| 33 | + expect(path.checkLineContinuity(0, 0, 0)).toBeFalsy(); |
| 34 | +}); |
| 35 | + |
| 36 | +test('.checkLineContinuity returns false if the last point is different', () => { |
| 37 | + const path = new Path(PathType.Travel, undefined, undefined, undefined); |
| 38 | + |
| 39 | + path.addPoint(0, 0, 0); |
| 40 | + path.addPoint(1, 2, 3); |
| 41 | + |
| 42 | + expect(path.checkLineContinuity(1, 2, 4)).toBeFalsy(); |
| 43 | +}); |
| 44 | + |
| 45 | +test('.checkLineContinuity returns true if the last point is the same', () => { |
| 46 | + const path = new Path(PathType.Travel, undefined, undefined, undefined); |
| 47 | + |
| 48 | + path.addPoint(0, 0, 0); |
| 49 | + path.addPoint(1, 2, 3); |
| 50 | + |
| 51 | + expect(path.checkLineContinuity(1, 2, 3)).toBeTruthy(); |
| 52 | +}); |
| 53 | + |
| 54 | +test('.path returns an array of Vector3', () => { |
| 55 | + const path = new Path(PathType.Travel, undefined, undefined, undefined); |
| 56 | + |
| 57 | + path.addPoint(0, 0, 0); |
| 58 | + path.addPoint(1, 2, 3); |
| 59 | + |
| 60 | + const result = path.path(); |
| 61 | + |
| 62 | + expect(result).not.toBeNull(); |
| 63 | + expect(result.length).toEqual(2); |
| 64 | + expect(result[0]).toEqual({ x: 0, y: 0, z: 0 }); |
| 65 | + expect(result[1]).toEqual({ x: 1, y: 2, z: 3 }); |
| 66 | +}); |
| 67 | + |
| 68 | +test('.geometry returns an ExtrusionGeometry from the path', () => { |
| 69 | + const path = new Path(PathType.Travel, undefined, undefined, undefined); |
| 70 | + |
| 71 | + path.vertices = [0, 0, 0, 1, 2, 3]; |
| 72 | + |
| 73 | + const result = path.geometry() as ExtrusionGeometry; |
| 74 | + |
| 75 | + expect(result).not.toBeNull(); |
| 76 | + expect(result).toBeInstanceOf(ExtrusionGeometry); |
| 77 | + expect(result.parameters.points.length).toEqual(2); |
| 78 | + expect(result.parameters.closed).toEqual(false); |
| 79 | +}); |
| 80 | + |
| 81 | +test('.geometry returns an ExtrusionGeometry with the path extrusion width', () => { |
| 82 | + const path = new Path(PathType.Travel, 9, undefined, undefined); |
| 83 | + |
| 84 | + path.vertices = [0, 0, 0, 1, 2, 3]; |
| 85 | + |
| 86 | + const result = path.geometry() as ExtrusionGeometry; |
| 87 | + |
| 88 | + expect(result.parameters.lineWidth).toEqual(9); |
| 89 | +}); |
| 90 | + |
| 91 | +test('.geometry returns an ExtrusionGeometry with the path line height', () => { |
| 92 | + const path = new Path(PathType.Travel, undefined, 5, undefined); |
| 93 | + |
| 94 | + path.vertices = [0, 0, 0, 1, 2, 3]; |
| 95 | + |
| 96 | + const result = path.geometry() as ExtrusionGeometry; |
| 97 | + |
| 98 | + expect(result.parameters.lineHeight).toEqual(5); |
| 99 | +}); |
| 100 | + |
| 101 | +test('.geometry returns an empty BufferGeometry if there are less than 3 vertices', () => { |
| 102 | + const path = new Path(PathType.Travel, undefined, undefined, undefined); |
| 103 | + |
| 104 | + const result = path.geometry(); |
| 105 | + |
| 106 | + expect(result).not.toBeNull(); |
| 107 | + expect(result).toBeInstanceOf(BufferGeometry); |
| 108 | +}); |
| 109 | + |
| 110 | +test('.line returns a BufferGeometry from the path', () => { |
| 111 | + const path = new Path(PathType.Travel, undefined, undefined, undefined); |
| 112 | + |
| 113 | + path.vertices = [0, 0, 0, 1, 2, 3]; |
| 114 | + |
| 115 | + const result = path.line(); |
| 116 | + |
| 117 | + expect(result).not.toBeNull(); |
| 118 | + expect(result).toBeInstanceOf(BufferGeometry); |
| 119 | + expect(result.getAttribute('position').count).toEqual(2); |
| 120 | +}); |
| 121 | + |
| 122 | +test('.line returns a BufferGeometry when there are no vertices', () => { |
| 123 | + const path = new Path(PathType.Travel, undefined, undefined, undefined); |
| 124 | + |
| 125 | + const result = path.line(); |
| 126 | + |
| 127 | + expect(result).not.toBeNull(); |
| 128 | + expect(result).toBeInstanceOf(BufferGeometry); |
| 129 | + expect(result.getAttribute('position').count).toEqual(0); |
| 130 | +}); |
0 commit comments