-
-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathboards-service.test.ts
193 lines (184 loc) · 6.28 KB
/
boards-service.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
import { Deferred } from '@theia/core/lib/common/promise-util';
import { Mutable } from '@theia/core/lib/common/types';
import { expect } from 'chai';
import {
AttachedBoardsChangeEvent,
BoardInfo,
getBoardInfo,
noNativeSerialPort,
nonSerialPort,
Port,
selectPortForInfo,
unknownBoard,
} from '../../common/protocol';
import { firstToUpperCase } from '../../common/utils';
describe('boards-service', () => {
describe('AttachedBoardsChangeEvent', () => {
it('should detect one attached port', () => {
const event = <AttachedBoardsChangeEvent & any>{
oldState: {
boards: [
{
name: 'Arduino MKR1000',
fqbn: 'arduino:samd:mkr1000',
port: '/dev/cu.usbmodem14601',
},
{
name: 'Arduino Uno',
fqbn: 'arduino:avr:uno',
port: '/dev/cu.usbmodem14501',
},
],
ports: [
{
protocol: 'serial',
address: '/dev/cu.usbmodem14501',
},
{
protocol: 'serial',
address: '/dev/cu.usbmodem14601',
},
{
protocol: 'serial',
address: '/dev/cu.Bluetooth-Incoming-Port',
},
{ protocol: 'serial', address: '/dev/cu.MALS' },
{ protocol: 'serial', address: '/dev/cu.SOC' },
],
},
newState: {
boards: [
{
name: 'Arduino MKR1000',
fqbn: 'arduino:samd:mkr1000',
port: '/dev/cu.usbmodem1460',
},
{
name: 'Arduino Uno',
fqbn: 'arduino:avr:uno',
port: '/dev/cu.usbmodem14501',
},
],
ports: [
{
protocol: 'serial',
address: '/dev/cu.SLAB_USBtoUART',
},
{
protocol: 'serial',
address: '/dev/cu.usbmodem14501',
},
{
protocol: 'serial',
address: '/dev/cu.usbmodem14601',
},
{
protocol: 'serial',
address: '/dev/cu.Bluetooth-Incoming-Port',
},
{ protocol: 'serial', address: '/dev/cu.MALS' },
{ protocol: 'serial', address: '/dev/cu.SOC' },
],
},
};
const diff = AttachedBoardsChangeEvent.diff(event);
expect(diff.attached.boards).to.be.empty; // tslint:disable-line:no-unused-expression
expect(diff.detached.boards).to.be.empty; // tslint:disable-line:no-unused-expression
expect(diff.detached.ports).to.be.empty; // tslint:disable-line:no-unused-expression
expect(diff.attached.ports.length).to.be.equal(1);
expect(diff.attached.ports[0].address).to.be.equal(
'/dev/cu.SLAB_USBtoUART'
);
});
});
describe('getBoardInfo', () => {
const vid = '0x0';
const pid = '0x1';
const serialNumber = '1730323';
const name = 'The Board';
const fqbn = 'alma:korte:szolo';
const selectedBoard = { name, fqbn };
const selectedPort = (
properties: Record<string, string> = {},
protocol = 'serial'
): Mutable<Port> => ({
address: 'address',
addressLabel: 'addressLabel',
protocol,
protocolLabel: firstToUpperCase(protocol),
properties,
});
it('should handle when no port is selected', async () => {
const info = await getBoardInfo(undefined, never());
expect(info).to.be.equal(selectPortForInfo);
});
it("should handle when port protocol is not 'serial'", async () => {
await Promise.allSettled(
['network', 'teensy'].map(async (protocol) => {
const selectedPort: Port = {
address: 'address',
addressLabel: 'addressLabel',
protocolLabel: firstToUpperCase(protocol),
protocol,
};
const info = await getBoardInfo(selectedPort, never());
expect(info).to.be.equal(nonSerialPort);
})
);
});
it("should not detect a port as non-native serial, if protocol is 'serial' but VID or PID is missing", async () => {
const insufficientProperties: Record<string, string>[] = [
{},
{ vid },
{ pid },
{ VID: vid, pid: pid }, // case sensitive
];
for (const properties of insufficientProperties) {
const port = selectedPort(properties);
const info = await getBoardInfo(port, {
[Port.keyOf(port)]: [port, []],
});
expect(info).to.be.equal(noNativeSerialPort);
}
});
it("should detect a port as non-native serial, if protocol is 'serial' and VID/PID are available", async () => {
const port = selectedPort({ vid, pid });
const info = await getBoardInfo(port, {
[Port.keyOf(port)]: [port, []],
});
expect(typeof info).to.be.equal('object');
const boardInfo = <BoardInfo>info;
expect(boardInfo.VID).to.be.equal(vid);
expect(boardInfo.PID).to.be.equal(pid);
expect(boardInfo.SN).to.be.equal('(null)');
expect(boardInfo.BN).to.be.equal(unknownBoard);
});
it("should show the 'SN' even if no matching board was detected for the port", async () => {
const port = selectedPort({ vid, pid, serialNumber });
const info = await getBoardInfo(port, {
[Port.keyOf(port)]: [port, []],
});
expect(typeof info).to.be.equal('object');
const boardInfo = <BoardInfo>info;
expect(boardInfo.VID).to.be.equal(vid);
expect(boardInfo.PID).to.be.equal(pid);
expect(boardInfo.SN).to.be.equal(serialNumber);
expect(boardInfo.BN).to.be.equal(unknownBoard);
});
it("should use the name of the matching board as 'BN' if available", async () => {
const port = selectedPort({ vid, pid });
const info = await getBoardInfo(port, {
[Port.keyOf(port)]: [port, [selectedBoard]],
});
expect(typeof info).to.be.equal('object');
const boardInfo = <BoardInfo>info;
expect(boardInfo.VID).to.be.equal(vid);
expect(boardInfo.PID).to.be.equal(pid);
expect(boardInfo.SN).to.be.equal('(null)');
expect(boardInfo.BN).to.be.equal(selectedBoard.name);
});
});
});
function never<T>(): Promise<T> {
return new Deferred<T>().promise;
}