Skip to content

Commit 0b8c26f

Browse files
authored
Merge pull request #24 from REVrobotics/fix-tests
2 parents 82a75f7 + c1e3888 commit 0b8c26f

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

test/test_binding.js

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const assert = require("assert").strict;
44

55
let devices = [];
66

7+
const canBridge = new addon.CanBridge();
8+
79
async function testGetDevices() {
8-
assert(addon.getDevices, "getDevices is undefined");
10+
assert(canBridge.getDevices, "getDevices is undefined");
911
try {
10-
devices = await addon.getDevices();
12+
devices = await canBridge.getDevices();
1113
console.log(devices);
1214
console.log(`Found ${devices.length} device(s)`);
1315
devices.forEach((device) => {
@@ -23,19 +25,19 @@ async function testGetDevices() {
2325

2426
async function testConcurrentGetDevices() {
2527
try {
26-
await Promise.all([addon.getDevices(), addon.getDevices(), addon.getDevices(),
27-
addon.getDevices(), addon.getDevices(), addon.getDevices()]);
28+
await Promise.all([canBridge.getDevices(), canBridge.getDevices(), canBridge.getDevices(),
29+
canBridge.getDevices(), canBridge.getDevices(), canBridge.getDevices()]);
2830
} catch(error) {
2931
assert.fail(error.toString());
3032
}
3133
}
3234

3335
async function testRegisterDeviceToHAL() {
34-
assert(addon.registerDeviceToHAL, "registerDeviceToHAL is undefined");
36+
assert(canBridge.registerDeviceToHAL, "registerDeviceToHAL is undefined");
3537
try {
3638
if (devices.length > 0) {
3739
console.log(`Registering device ${devices[0].descriptor} to HAL`);
38-
const status = addon.registerDeviceToHAL(devices[0].descriptor, 0, 0);
40+
const status = canBridge.registerDeviceToHAL(devices[0].descriptor, 0, 0);
3941
console.log(`Device registered with status code ${status}`);
4042
assert.equal(status, 0, "Registering device failed");
4143
}
@@ -46,12 +48,12 @@ async function testRegisterDeviceToHAL() {
4648
}
4749

4850
async function testUnregisterDeviceFromHAL() {
49-
assert(addon.unregisterDeviceFromHAL, "unregisterDeviceFromHAL is undefined");
51+
assert(canBridge.unregisterDeviceFromHAL, "unregisterDeviceFromHAL is undefined");
5052

5153
try {
5254
if (devices.length > 0) {
5355
console.log(`Unregistering device ${devices[0].descriptor} from HAL`);
54-
const status = await addon.unregisterDeviceFromHAL(devices[0].descriptor);
56+
const status = await canBridge.unregisterDeviceFromHAL(devices[0].descriptor);
5557
console.log(`Device unregistered with status code ${status}`);
5658
assert.equal(status, 0, "unregisterDeviceFromHAL device failed");
5759
}
@@ -61,22 +63,22 @@ async function testUnregisterDeviceFromHAL() {
6163
}
6264

6365
async function testReceiveMessage() {
64-
assert(addon.receiveMessage, "receiveMessage is undefined");
66+
assert(canBridge.receiveMessage, "receiveMessage is undefined");
6567
try {
6668
if (devices.length === 0) return;
6769
await new Promise(resolve=>{setTimeout(resolve, 200)});
68-
const message = addon.receiveMessage(devices[0].descriptor, 0, 0);
70+
const message = canBridge.receiveMessage(devices[0].descriptor, 0, 0);
6971
console.log("Got message", message);
7072
} catch(error) {
7173
assert.fail(error);
7274
}
7375
}
7476

7577
async function testOpenStreamSession() {
76-
assert(addon.openStreamSession, "openStreamSession is undefined");
78+
assert(canBridge.openStreamSession, "openStreamSession is undefined");
7779
try {
7880
if (devices.length === 0) return;
79-
const sessionHandle = addon.openStreamSession(devices[0].descriptor, 0, 0, 4);
81+
const sessionHandle = canBridge.openStreamSession(devices[0].descriptor, 0, 0, 4);
8082
console.log("Started stream session with handle", sessionHandle);
8183
return sessionHandle;
8284
} catch(error) {
@@ -85,11 +87,11 @@ async function testOpenStreamSession() {
8587
}
8688

8789
async function testReadStreamSession(sessionHandle) {
88-
assert(addon.readStreamSession, "readStreamSession is undefined");
90+
assert(canBridge.readStreamSession, "readStreamSession is undefined");
8991
if (devices.length === 0) return;
9092
try {
9193
await new Promise(resolve => {setTimeout(resolve, 200)});
92-
const data = addon.readStreamSession(devices[0].descriptor, sessionHandle, 4);
94+
const data = canBridge.readStreamSession(devices[0].descriptor, sessionHandle, 4);
9395
console.log("Got stream:", data);
9496
return sessionHandle;
9597
} catch (error) {
@@ -98,45 +100,45 @@ async function testReadStreamSession(sessionHandle) {
98100
}
99101

100102
async function testCloseStreamSession(sessionHandle) {
101-
assert(addon.closeStreamSession, "closeStreamSession is undefined");
103+
assert(canBridge.closeStreamSession, "closeStreamSession is undefined");
102104
try {
103105
if (devices.length === 0) return;
104-
const status = addon.closeStreamSession(devices[0].descriptor, sessionHandle);
106+
const status = canBridge.closeStreamSession(devices[0].descriptor, sessionHandle);
105107
assert.equal(status, 0, "Closing stream failed");
106108
} catch(error) {
107109
assert.fail(error);
108110
}
109111
}
110112

111113
async function testGetCANDetailStatus() {
112-
assert(addon.getCANDetailStatus, "getCANDetailStatus is undefined");
114+
assert(canBridge.getCANDetailStatus, "getCANDetailStatus is undefined");
113115
try {
114116
if (devices.length === 0) return;
115-
const status = addon.getCANDetailStatus(devices[0].descriptor);
117+
const status = canBridge.getCANDetailStatus(devices[0].descriptor);
116118
console.log("CAN Status:", status);
117119
} catch(error) {
118120
assert.fail(error);
119121
}
120122
}
121123

122124
async function testSendCANMessage() {
123-
assert(addon.sendCANMessage, "sendCANMessage is undefined");
125+
assert(canBridge.sendCANMessage, "sendCANMessage is undefined");
124126
try {
125127
if (devices.length === 0) return;
126128
// Send identify to SparkMax #1
127-
const status = addon.sendCANMessage(devices[0].descriptor, 0x2051D81, [], 0);
129+
const status = canBridge.sendCANMessage(devices[0].descriptor, 0x2051D81, [], 0);
128130
console.log("CAN Status:", status);
129131
} catch(error) {
130132
assert.fail(error);
131133
}
132134
}
133135

134136
async function testSendHALMessage() {
135-
assert(addon.sendCANMessage, "sendCANMessage is undefined");
137+
assert(canBridge.sendCANMessage, "sendCANMessage is undefined");
136138
try {
137139
if (devices.length === 0) return;
138140
// Send identify to SparkMax #1
139-
const status = addon.sendHALMessage(0x2051D81, [], 500);
141+
const status = canBridge.sendHALMessage(0x2051D81, [], 500);
140142
await new Promise(resolve => {setTimeout(resolve, 2000)});
141143
console.log("Status:", status);
142144
} catch(error) {
@@ -145,18 +147,18 @@ async function testSendHALMessage() {
145147
}
146148

147149
async function testSetThreadPriority() {
148-
assert(addon.setThreadPriority, "setThreadPriority is undefined");
150+
assert(canBridge.setThreadPriority, "setThreadPriority is undefined");
149151
try {
150152
if (devices.length === 0) return;
151-
addon.setThreadPriority(devices[0].descriptor, 4);
153+
canBridge.setThreadPriority(devices[0].descriptor, 4);
152154
} catch(error) {
153155
assert.fail(error);
154156
}
155157
}
156158

157159
function testInitializeNotifier() {
158160
try {
159-
addon.initializeNotifier();
161+
canBridge.initializeNotifier();
160162
} catch(error) {
161163
assert.fail(error);
162164
}
@@ -165,7 +167,7 @@ function testInitializeNotifier() {
165167
async function testWaitForNotifierAlarm() {
166168
try {
167169
const start = Date.now();
168-
await addon.waitForNotifierAlarm(1000);
170+
await canBridge.waitForNotifierAlarm(1000);
169171
console.log("Time passed:", Date.now() - start);
170172
} catch(error) {
171173
assert.fail(error);
@@ -174,15 +176,15 @@ async function testWaitForNotifierAlarm() {
174176

175177
function testStopNotifier() {
176178
try {
177-
addon.stopNotifier();
179+
canBridge.stopNotifier();
178180
} catch(error) {
179181
assert.fail(error);
180182
}
181183
}
182184

183185
async function testOpenHALStreamSession() {
184186
try {
185-
const handle = addon.openHALStreamSession(0, 0, 8);
187+
const handle = canBridge.openHALStreamSession(0, 0, 8);
186188
return handle;
187189
} catch(error) {
188190
assert.fail(error);
@@ -192,7 +194,7 @@ async function testOpenHALStreamSession() {
192194
async function testReadHALStreamSession(handle) {
193195
try {
194196
await new Promise(resolve => {setTimeout(resolve, 200)});
195-
const data = addon.readHALStreamSession(handle, 8);
197+
const data = canBridge.readHALStreamSession(handle, 8);
196198
console.log("Got stream from HAL:", data);
197199
return handle;
198200
} catch(error) {
@@ -202,7 +204,7 @@ async function testReadHALStreamSession(handle) {
202204

203205
async function testCloseHALStreamSession(handle) {
204206
try {
205-
addon.closeHALStreamSession(handle);
207+
canBridge.closeHALStreamSession(handle);
206208
console.log("Closed HAL stream");
207209
} catch(error) {
208210
assert.fail(error);
@@ -212,13 +214,13 @@ async function testCloseHALStreamSession(handle) {
212214
async function testHeartbeat() {
213215
try {
214216
if (devices.length === 0) return;
215-
await addon.sendCANMessage(devices[0].descriptor, 33882241, [10, 215, 163, 60, 0, 0, 0, 0], 0);
216-
await addon.setSparkMaxHeartbeatData(devices[0].descriptor, Array(8).fill(0xFF));
217-
const interval = setInterval(addon.ackSparkMaxHeartbeat, 900);
217+
await canBridge.sendCANMessage(devices[0].descriptor, 33882241, [10, 215, 163, 60, 0, 0, 0, 0], 0);
218+
await canBridge.setSparkMaxHeartbeatData(devices[0].descriptor, Array(8).fill(0xFF));
219+
const interval = setInterval(canBridge.ackSparkMaxHeartbeat, 900);
218220
await new Promise(resolve => {setTimeout(resolve, 6000)});
219221
clearInterval(interval);
220222
console.log("STOPPING");
221-
await addon.setSparkMaxHeartbeatData(devices[0].descriptor, Array(8).fill(0x00));
223+
await canBridge.setSparkMaxHeartbeatData(devices[0].descriptor, Array(8).fill(0x00));
222224
await new Promise(resolve => {setTimeout(resolve, 2000)});
223225
} catch(error) {
224226
assert.fail(error);

0 commit comments

Comments
 (0)