-
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathForwarder.test.js
More file actions
213 lines (160 loc) · 5.1 KB
/
Copy pathForwarder.test.js
File metadata and controls
213 lines (160 loc) · 5.1 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
const expect = require("chai").expect;
const {Message, WebMidi, Note, Forwarder} = require("../dist/cjs/webmidi.cjs.js");
const WMT = require("web-midi-test");
// The virtual port is an "external" device so an input is seen as an output by WebMidi. To avoid
// confusion, the naming scheme adopts WebMidi's perspective.
let VIRTUAL_OUTPUT_NAME = "Virtual Output";
let VIRTUAL_OUTPUT = new WMT.MidiDst(VIRTUAL_OUTPUT_NAME);
/** @type {import("../dist/cjs/webmidi.cjs.js").Output} */
let WEBMIDI_OUTPUT;
function noop() {}
describe("Forwarder Object", function() {
before(function () {
VIRTUAL_OUTPUT.connect();
// VIRTUAL_OUTPUT.ignoreTypes(false, false, false); // enable sysex, timing & active sensing
});
after(function () {
VIRTUAL_OUTPUT.disconnect();
});
beforeEach("Check support and enable WebMidi.js", async function () {
await WebMidi.enable({sysex: true, requestMIDIAccessFunction: WMT.requestMIDIAccess});
WEBMIDI_OUTPUT = WebMidi.getOutputByName(VIRTUAL_OUTPUT_NAME);
});
afterEach("Disable WebMidi.js", async function () {
await WebMidi.disable();
});
describe("constructor()", function() {
it("should throw when an invalid destination is specified", function() {
// Arrange
const items = [
"abc",
123,
new Note(64),
];
// Act
// Assert
items.forEach(item => {
expect(() => new Forwarder(item)).to.throw(TypeError);
});
});
it("should throw when an invalid type is specified", function() {
// Arrange
const types = [
"abc",
123,
];
// Act
// Assert
types.forEach(type => {
expect(() => {
new Forwarder(WEBMIDI_OUTPUT, {types: type});
}).to.throw(TypeError);
});
});
it("should throw when an invalid channel is specified", function() {
// Arrange
const items = [
0,
-1,
17,
];
// Act
// Assert
items.forEach(item => {
expect(() => {
new Forwarder(WEBMIDI_OUTPUT, {channels: item});
}).to.throw(TypeError);
});
});
});
describe("forward()", function() {
it("should forward the message to the forwarder's destination(s)", function(done) {
// Arrange
const data = [0x90, 64, 127];
const msg = new Message(data);
const forwarder = new Forwarder(WEBMIDI_OUTPUT);
VIRTUAL_OUTPUT.receive = assert;
// Act
forwarder.forward(msg);
// Assert
function assert(message) {
expect(message).to.have.ordered.members(msg.data);
VIRTUAL_OUTPUT.receive = noop;
done();
}
});
it("should not forward messages when 'suspended' is true", function(done) {
// Arrange
const data = [0x90, 64, 127];
const msg = new Message(data);
const forwarder = new Forwarder(WEBMIDI_OUTPUT);
forwarder.suspended = true;
VIRTUAL_OUTPUT.receive = assert;
// Act
forwarder.forward(msg);
const id = setTimeout(() => {
VIRTUAL_OUTPUT.receive = noop;
done();
}, 250);
// Assert
function assert(deltaTime, message) {
VIRTUAL_OUTPUT.receive = noop;
clearTimeout(id);
done(new Error("Callback should not have been triggered. " + message));
}
});
it("should not forward message if it does not match channel", function(done) {
// Arrange
const msg1 = new Message([0x90, 64, 127]); // note on on ch. 1
const msg2 = new Message([0x94, 64, 127]); // note on on ch. 5
const msg3 = new Message([0x9A, 64, 127]); // note on on ch. 11
const msg4 = new Message([0x9F, 64, 127]); // note on on ch. 16
const channel = 10;
const forwarder = new Forwarder(WEBMIDI_OUTPUT, {channels: channel});
VIRTUAL_OUTPUT.receive = assert;
// Act
const id = setTimeout(() => {
VIRTUAL_OUTPUT.receive = noop;
done();
}, 250);
forwarder.forward(msg1);
forwarder.forward(msg2);
forwarder.forward(msg3);
forwarder.forward(msg4);
// Assert
function assert(deltaTime, message) {
VIRTUAL_OUTPUT.receive = noop;
clearTimeout(id);
done(new Error("Callback should not have been triggered. " + message));
}
});
it("should not forward message if it does not match type", function(done) {
// Arrange
const target = {name: "pitchbend", number: 0xE0}; // pitchbend
const messages = [
0x80,
0x90,
0xA0,
0xB0,
0xC0,
0xD0
];
const forwarder = new Forwarder(WEBMIDI_OUTPUT, {types: target.name});
VIRTUAL_OUTPUT.receive = assert;
// Act
const id = setTimeout(() => {
VIRTUAL_OUTPUT.receive = noop;
done();
}, 250);
messages.forEach(i => {
forwarder.forward(new Message([i, 64, 127]));
});
// Assert
function assert(deltaTime, message) {
VIRTUAL_OUTPUT.receive = noop;
clearTimeout(id);
done(new Error("Callback should not have been triggered. " + message));
}
});
});
});