Skip to content

Commit 2c6a9f9

Browse files
committed
Job tests (and fixes!)
1 parent 5028693 commit 2c6a9f9

2 files changed

Lines changed: 170 additions & 1 deletion

File tree

src/__tests__/job.ts

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import { test, expect, describe } from 'vitest';
2+
import { Job } from '../job';
3+
import { PathType, Path } from '../path';
4+
5+
test('it has an initial state', () => {
6+
const job = new Job();
7+
8+
expect(job.state.x).toEqual(0);
9+
expect(job.state.y).toEqual(0);
10+
expect(job.state.z).toEqual(0);
11+
expect(job.state.e).toEqual(0);
12+
expect(job.state.tool).toEqual(0);
13+
expect(job.state.units).toEqual('mm');
14+
});
15+
16+
describe('.isPlanar', () => {
17+
test('returns true if all extrusions are on the same plane', () => {
18+
const job = new Job();
19+
20+
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
21+
append_path(job, PathType.Extrusion, [1, 2, 0, 5, 6, 0]);
22+
23+
expect(job.isPlanar()).toEqual(true);
24+
});
25+
26+
test('returns false if any extrusions are on a different plane', () => {
27+
const job = new Job();
28+
29+
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
30+
append_path(job, PathType.Extrusion, [1, 2, 0, 5, 6, 1]);
31+
32+
expect(job.isPlanar()).toEqual(false);
33+
});
34+
35+
test('ignores travel paths', () => {
36+
const job = new Job();
37+
38+
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
39+
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 1, 1, 2, 0]);
40+
append_path(job, PathType.Extrusion, [1, 2, 0, 5, 6, 0]);
41+
42+
expect(job.isPlanar()).toEqual(true);
43+
});
44+
});
45+
46+
describe('.layers', () => {
47+
test('returns null if the job is not planar', () => {
48+
const job = new Job();
49+
50+
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
51+
append_path(job, PathType.Extrusion, [5, 6, 0, 5, 6, 1]);
52+
53+
expect(job.layers()).toEqual(null);
54+
});
55+
56+
test('paths without z changes are on the same layer', () => {
57+
const job = new Job();
58+
59+
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
60+
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 0]);
61+
62+
const layers = job.layers();
63+
64+
expect(layers).not.toBeNull();
65+
expect(layers).toBeInstanceOf(Array);
66+
expect(layers?.length).toEqual(1);
67+
expect(layers?.[0].length).toEqual(2);
68+
});
69+
70+
test('travel paths moving z create a new layer', () => {
71+
const job = new Job();
72+
73+
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
74+
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 1]);
75+
76+
const layers = job.layers();
77+
78+
expect(layers).not.toBeNull();
79+
expect(layers).toBeInstanceOf(Array);
80+
expect(layers?.length).toEqual(2);
81+
expect(layers?.[0].length).toEqual(1);
82+
expect(layers?.[1].length).toEqual(1);
83+
});
84+
85+
test('multiple travels in a row are on the same layer', () => {
86+
const job = new Job();
87+
88+
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
89+
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 2]);
90+
append_path(job, PathType.Travel, [5, 6, 2, 5, 6, 0]);
91+
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 2]);
92+
93+
const layers = job.layers();
94+
95+
expect(layers).not.toBeNull();
96+
expect(layers).toBeInstanceOf(Array);
97+
expect(layers?.length).toEqual(2);
98+
expect(layers?.[0].length).toEqual(1);
99+
expect(layers?.[1].length).toEqual(3);
100+
});
101+
102+
test('extrusions after travels are on the same layer', () => {
103+
const job = new Job();
104+
105+
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
106+
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 2]);
107+
append_path(job, PathType.Travel, [5, 6, 2, 5, 6, 0]);
108+
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 2]);
109+
append_path(job, PathType.Extrusion, [5, 6, 2, 5, 6, 2]);
110+
111+
const layers = job.layers();
112+
113+
expect(layers).not.toBeNull();
114+
expect(layers).toBeInstanceOf(Array);
115+
expect(layers?.length).toEqual(2);
116+
expect(layers?.[0].length).toEqual(1);
117+
expect(layers?.[1].length).toEqual(4);
118+
});
119+
});
120+
121+
describe('.extrusions', () => {
122+
test('returns all extrusion paths', () => {
123+
const job = new Job();
124+
125+
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
126+
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 0]);
127+
append_path(job, PathType.Extrusion, [1, 2, 0, 5, 6, 0]);
128+
129+
const extrusions = job.extrusions();
130+
131+
expect(extrusions).not.toBeNull();
132+
expect(extrusions).toBeInstanceOf(Array);
133+
expect(extrusions.length).toEqual(2);
134+
extrusions.forEach((path) => {
135+
expect(path.travelType).toEqual(PathType.Extrusion);
136+
});
137+
});
138+
});
139+
140+
describe('.travels', () => {
141+
test('returns all travel paths', () => {
142+
const job = new Job();
143+
144+
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
145+
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 0]);
146+
append_path(job, PathType.Extrusion, [1, 2, 0, 5, 6, 0]);
147+
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 0]);
148+
149+
const travels = job.travels();
150+
151+
expect(travels).not.toBeNull();
152+
expect(travels).toBeInstanceOf(Array);
153+
expect(travels.length).toEqual(2);
154+
travels.forEach((path) => {
155+
expect(path.travelType).toEqual(PathType.Travel);
156+
});
157+
});
158+
});
159+
160+
function append_path(job, travelType, vertices) {
161+
const path = new Path(travelType, 0.6, 0.2, job.state.tool);
162+
path.vertices = vertices;
163+
job.paths.push(path);
164+
}

src/job.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,19 @@ export class Job {
4545
if (path.travelType === PathType.Extrusion) {
4646
currentLayer.push(path);
4747
} else {
48-
if (path.vertices.some((_, i, arr) => i % 3 === 2 && arr[i] !== arr[2])) {
48+
if (
49+
path.vertices.some((_, i, arr) => i % 3 === 2 && arr[i] !== arr[2]) &&
50+
currentLayer.find((p) => p.travelType === PathType.Extrusion)
51+
) {
4952
layers.push(currentLayer);
5053
currentLayer = [];
5154
}
5255
currentLayer.push(path);
5356
}
5457
});
5558

59+
layers.push(currentLayer);
60+
5661
return layers;
5762
}
5863

0 commit comments

Comments
 (0)