This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
test.ts
243 lines (211 loc) · 6.84 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//#!tsc --target ES5 --module commonjs test.ts && node test.js
import {
STATE_IDLE,
Grbl,
GrblLineParser,
GrblLineParserResult,
GrblLineParserResultStartup,
GrblLineParserResultOk,
GrblLineParserResultError,
GrblLineParserResultAlarm,
GrblLineParserResultFeedback,
GrblLineParserResultDollar,
GrblLineParserResultStatus,
SerialPort
} from "./grbl";
import assert = require('assert');
var parser = new GrblLineParser();
var result: any;
result = parser.parse("Grbl 0.9j ['$' for help]");
assert(result instanceof GrblLineParserResultStartup);
assert(result.version.major == '0.9');
assert(result.version.minor == 'j');
result = parser.parse("Grbl 0.9 ['$' for help]");
assert(result instanceof GrblLineParserResultStartup);
assert(result.version.major == '0.9');
assert(result.version.minor == ' ');
result = parser.parse("Grbl 0.51 ['$' for help]");
assert(result instanceof GrblLineParserResultStartup);
assert(result.version.major == '0.5');
assert(result.version.minor == '1');
result = parser.parse("<Idle,MPos:0.000,0.000,0.000,WPos:0.000,0.000,0.000>");
assert(result instanceof GrblLineParserResultStatus);
assert(result.state === 'Idle');
assert(result.machinePosition.x === 0);
assert(result.machinePosition.y === 0);
assert(result.machinePosition.z === 0);
assert(result.workingPosition.x === 0);
assert(result.workingPosition.y === 0);
assert(result.workingPosition.z === 0);
assert(result.plannerBufferCount === undefined);
assert(result.rxBufferCount === undefined);
result = parser.parse("<Idle,MPos:0.000,0.000,0.000,WPos:0.000,0.000,0.000,Buf:0>");
assert(result instanceof GrblLineParserResultStatus);
assert(result.state === 'Idle');
assert(result.machinePosition.x === 0);
assert(result.machinePosition.y === 0);
assert(result.machinePosition.z === 0);
assert(result.workingPosition.x === 0);
assert(result.workingPosition.y === 0);
assert(result.workingPosition.z === 0);
assert(result.plannerBufferCount === 0);
assert(result.rxBufferCount === undefined);
result = parser.parse("<Idle,MPos:0.000,0.000,0.000,WPos:0.000,0.000,0.000,Buf:0,RX:0>");
assert(result instanceof GrblLineParserResultStatus);
assert(result.state === 'Idle');
assert(result.machinePosition.x === 0);
assert(result.machinePosition.y === 0);
assert(result.machinePosition.z === 0);
assert(result.workingPosition.x === 0);
assert(result.workingPosition.y === 0);
assert(result.workingPosition.z === 0);
assert(result.plannerBufferCount === 0);
assert(result.rxBufferCount === 0);
result = parser.parse("[Reset to continue]");
assert(result instanceof GrblLineParserResultFeedback);
assert(result.message == 'Reset to continue');
result = parser.parse("['$H'|'$X' to unlock]");
assert(result instanceof GrblLineParserResultFeedback);
result = parser.parse("[Caution: Unlocked]");
assert(result instanceof GrblLineParserResultFeedback);
result = parser.parse("[Enabled]");
assert(result instanceof GrblLineParserResultFeedback);
result = parser.parse("[Disabled]");
assert(result instanceof GrblLineParserResultFeedback);
result = parser.parse("ALARM:Hard/soft limit");
assert(result instanceof GrblLineParserResultAlarm);
assert(result.message == 'Hard/soft limit');
result = parser.parse("ALARM:Abort during cycle");
assert(result instanceof GrblLineParserResultAlarm);
result = parser.parse("ALARM:Probe fail");
assert(result instanceof GrblLineParserResultAlarm);
result = parser.parse("error:Invalid gcode ID:XX");
assert(result instanceof GrblLineParserResultError);
assert(result.message == 'Invalid gcode ID:XX');
result = parser.parse("$20=1");
assert(result instanceof GrblLineParserResultDollar);
assert(result.raw == '$20=1');
var s1 = new GrblLineParserResultStatus(null);
s1.state = 'Idle';
s1.machinePosition = {x : 0, y : 0, z : 0};
s1.workingPosition = {x : 0, y : 0, z : 0};
var s2 = new GrblLineParserResultStatus("");
s2.state = 'Idle';
s2.machinePosition = {x : 0, y : 0, z : 0};
s2.workingPosition = {x : 0, y : 0, z : 0};
assert(s1.equals(s2));
function test ( block: (next:Function) => void ) {
var timer = setTimeout( () => {
console.log('test timeout');
assert(false);
process.exit(1);
}, 1000);
block( () => {
clearTimeout(timer);
});
}
import events = require("events");
class MockSerialPort extends events.EventEmitter implements SerialPort {
written: Array<string>;
constructor() {
super();
this.written = [];
}
open(cb: (err: any) => void) {
setTimeout(cb, 0);
}
write(d: string, cb?: (err: any, results: any) => void) {
if (d == "\x18") {
this.mockResponse("Grbl 0.9j ['$' for help]\n");
} else
if (d == "?") {
this.mockResponse("<Idle,MPos:0.000,0.000,0.000,WPos:0.000,0.000,0.000>\n");
}
this.written.unshift(d);
}
close(cb: (err: any) => void) {
setTimeout(cb, 0);
}
mockResponse(d: string) {
setTimeout( () => {
this.emit("data", d);
}, 0);
}
}
var mock = new MockSerialPort();
var grbl = new Grbl(mock);
grbl.once('startup', (r: GrblLineParserResultStartup) => {
assert(r.version.major == 0.9);
assert(r.version.minor == 'j');
});
Promise.resolve().
then( ()=> {
return grbl.open().then( () => {
assert(grbl.isOpened);
console.log('open');
grbl.status.state = STATE_IDLE;
});
}).
then( ()=> {
var promise = grbl.getConfig().then( (config) => {
assert.deepEqual(config, { '$0': 'a', '$1': 'b' });
});
mock.mockResponse('$0=a\n');
mock.mockResponse('$1=b\n');
mock.mockResponse('ok\n');
return promise;
}).
then( () => {
return grbl.getStatus().then( (status) => {
assert.deepEqual(status, {
raw: '<Idle,MPos:0.000,0.000,0.000,WPos:0.000,0.000,0.000>',
state: 'Idle',
machinePosition: { x: 0, y: 0, z: 0 },
workingPosition: { x: 0, y: 0, z: 0 }
});
});
}).
then( () => {
var promise = grbl.command("G01 X0.000 Y0.000 F500").then( () => {
assert(true);
});
mock.mockResponse('ok\n');
return promise;
}).
then( () => {
var promise = grbl.command("G01 X0.000 Y0.000 F500").then( () => {
assert(false);
}, (e) => {
assert.deepEqual(e, { raw: 'error:test error', message: 'test error' });
});
mock.mockResponse('error:test error\n');
return promise;
}).
then( ()=> {
return new Promise( (resolve, reject) => {
grbl.once('alarm', (r : GrblLineParserResultAlarm) => {
assert.deepEqual(r, {raw: 'ALARM:TEST ALARM', message: 'TEST ALARM' });
assert.deepEqual(grbl.lastAlarm, {raw: 'ALARM:TEST ALARM', message: 'TEST ALARM' });
resolve();
});
mock.mockResponse('ALARM:TEST ALARM\n');
});
}).
then( ()=> {
return new Promise( (resolve, reject) => {
grbl.once('feedback', (r: GrblLineParserResultFeedback) => {
assert.deepEqual(r, { raw: '[Enabled]', message: 'Enabled' });
assert.deepEqual(grbl.lastFeedback, { raw: '[Enabled]', message: 'Enabled' });
resolve();
});
mock.mockResponse('[Enabled]\n');
});
}).
then( ()=> {
console.log('close');
return grbl.close();
}).
catch( (e) => {
console.log(e);
process.exit(1);
});