-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathsagas.test.ts
More file actions
288 lines (265 loc) · 8.74 KB
/
Copy pathsagas.test.ts
File metadata and controls
288 lines (265 loc) · 8.74 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// SPDX-License-Identifier: MIT
// Copyright (c) 2021-2025 The Pybricks Authors
import { AsyncSaga } from '../../test';
import {
didFailToSendCommand,
didFailToWriteCommand,
didNotifyEvent,
didReceiveStatusReport,
didReceiveWriteAppData,
didReceiveWriteStdout,
didSendCommand,
didWriteCommand,
eventProtocolError,
sendLegacyStartReplCommand,
sendLegacyStartUserProgramCommand,
sendStartUserProgramCommand,
sendStopUserProgramCommand,
sendWriteAppDataCommand,
sendWriteStdinCommand,
sendWriteUserProgramMetaCommand,
sendWriteUserRamCommand,
writeCommand,
} from './actions';
import { BuiltinProgramId, CommandType, ProtocolError } from './protocol';
import blePybricksService from './sagas';
describe('command encoder', () => {
test.each([
[
'stop user program',
sendStopUserProgramCommand(0),
[
0x00, // stop user program command
],
],
[
'start user program with program id',
sendStartUserProgramCommand(0, 0x2a),
[
0x01, // start user program command
0x2a, // program ID
],
],
[
'start user program',
sendLegacyStartUserProgramCommand(0),
[
0x01, // start user program command
],
],
[
'start repl',
sendLegacyStartReplCommand(0),
[
0x02, // start repl command
],
],
[
'write user program meta',
sendWriteUserProgramMetaCommand(0, 100),
[
0x03, // write user program meta command
0x64, // program size LSB
0x00,
0x00,
0x00, // program size MSB
],
],
[
'write user ram',
sendWriteUserRamCommand(0, 100, new Uint8Array([1, 2, 3, 4]).buffer),
[
0x04, // write user ram command
0x64, // offset size LSB
0x00,
0x00,
0x00, // offset size MSB
0x01, // payload start
0x02,
0x03,
0x04, // payload end
],
],
[
'write stdin',
sendWriteStdinCommand(0, new Uint8Array([1, 2, 3, 4]).buffer),
[
0x06, // write stdin command
0x01, // payload start
0x02,
0x03,
0x04, // payload end
],
],
[
'write AppData',
sendWriteAppDataCommand(0, 0x2a, new Uint8Array([1, 2, 3, 4]).buffer),
[
0x07, // write AppData command
0x2a, // offset LSB 16bit
0x00, // offset MSB 16bit
0x01, // payload start
0x02,
0x03,
0x04, // payload end
],
],
])('encode %s request', async (_n, request, expected) => {
const saga = new AsyncSaga(blePybricksService);
saga.put(request);
const message = new Uint8Array(expected);
const action = await saga.take();
expect(action).toEqual(writeCommand(0, message));
await saga.end();
});
test('commands are serialized', async () => {
const saga = new AsyncSaga(blePybricksService);
// we send 4 commands
saga.put(sendStopUserProgramCommand(0));
saga.put(sendStopUserProgramCommand(1));
saga.put(sendStopUserProgramCommand(2));
saga.put(sendStopUserProgramCommand(3));
// but only two didSendCommand actions meaning only the first two completed
saga.put(didWriteCommand(0));
saga.put(didWriteCommand(1));
// So only 3 commands were actually sent and two didSendCommand were
// dispatched (making 5 total dispatches). The last request is still
// buffered and has not been dispatched.
const numPending = saga.numPending();
expect(numPending).toEqual(5);
const message = new Uint8Array([CommandType.StopUserProgram]);
// every other action is the "write command" action
// and the interleaving actions are "did send command" actions
const action0 = await saga.take();
expect(action0).toEqual(writeCommand(0, message));
const action1 = await saga.take();
expect(action1).toEqual(didSendCommand(0));
const action2 = await saga.take();
expect(action2).toEqual(writeCommand(1, message));
const action3 = await saga.take();
expect(action3).toEqual(didSendCommand(1));
const action4 = await saga.take();
expect(action4).toEqual(writeCommand(2, message));
await saga.end();
});
test('fail to send triggers fail to write', async () => {
const saga = new AsyncSaga(blePybricksService);
saga.put(sendStopUserProgramCommand(0));
const message = new Uint8Array([0x00]);
const action1 = await saga.take();
expect(action1).toEqual(writeCommand(0, message));
const err = new Error('test error');
saga.put(didFailToWriteCommand(0, err));
const action2 = await saga.take();
expect(action2).toEqual(didFailToSendCommand(0, err));
await saga.end();
});
});
describe('event decoder', () => {
test.each([
[
'v1.3 status report',
[
0x00, // status report event
0x01, // flags count LSB
0x00, // .
0x00, // .
0x00, // flags count MSB
],
didReceiveStatusReport(0x00000001, 0, 0),
],
[
'v1.4 status report',
[
0x00, // status report event
0x01, // flags count LSB
0x00, // .
0x00, // .
0x00, // flags count MSB
0x80, // program ID
],
didReceiveStatusReport(0x00000001, BuiltinProgramId.REPL, 0),
],
[
'status report',
[
0x00, // status report event
0x01, // flags count LSB
0x00, // .
0x00, // .
0x00, // flags count MSB
0x80, // program ID
0x02, // selected slot
],
didReceiveStatusReport(0x00000001, BuiltinProgramId.REPL, 2),
],
[
'write stdout',
[
0x01, // write stdout event
't'.charCodeAt(0), //payload
'e'.charCodeAt(0),
't'.charCodeAt(0),
't'.charCodeAt(0),
],
didReceiveWriteStdout(
new Uint8Array([
't'.charCodeAt(0),
'e'.charCodeAt(0),
't'.charCodeAt(0),
't'.charCodeAt(0),
]).buffer,
),
],
[
'write AppData',
[
0x02, // write AppData event
't'.charCodeAt(0), //payload
'e'.charCodeAt(0),
't'.charCodeAt(0),
't'.charCodeAt(0),
],
didReceiveWriteAppData(
new Uint8Array([
't'.charCodeAt(0),
'e'.charCodeAt(0),
't'.charCodeAt(0),
't'.charCodeAt(0),
]).buffer,
),
],
])('decode %s event', async (_n, message, expected) => {
const saga = new AsyncSaga(blePybricksService);
const notification = new Uint8Array(message);
saga.put(didNotifyEvent(new DataView(notification.buffer)));
const action = await saga.take();
expect(action).toEqual(expected);
await saga.end();
});
test.each([
[
'unknown event',
[
0xff, // **bad event**
0x01, // **junk**
0x02, // **junk**
0x03, // **junk**
0x04, // **junk**
],
eventProtocolError(
new ProtocolError(
'unknown pybricks event type: 0xff',
new DataView(new Uint8Array().buffer),
),
),
],
])('protocol error', async (_n, message, expected) => {
const saga = new AsyncSaga(blePybricksService);
const notification = new Uint8Array(message);
saga.put(didNotifyEvent(new DataView(notification.buffer)));
const action = await saga.take();
expect(action).toEqual(expected);
await saga.end();
});
});