Skip to content

Commit 9b1b7ed

Browse files
committed
Test sending a button
1 parent 7bd8c77 commit 9b1b7ed

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

tests/button.test.tsx

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import Mitm from 'mitm';
2+
import { InSim } from 'node-insim';
3+
import { IS_BTN, IS_ISI, IS_VER } from 'node-insim/packets';
4+
5+
import type { CreateRootOptions } from '../src';
6+
import { Button, createRoot } from '../src';
7+
8+
function stringToBytes(string: string) {
9+
return string.split('').map((char) => char.charCodeAt(0));
10+
}
11+
12+
describe('Buttons', () => {
13+
let mitm: ReturnType<typeof Mitm>;
14+
15+
beforeEach(() => {
16+
mitm = Mitm();
17+
});
18+
19+
afterEach(() => {
20+
mitm.disable();
21+
});
22+
23+
it('should connect to InSim and send a button', (done) => {
24+
let root: ReturnType<typeof createRoot>;
25+
let receivedPackets: Buffer[] = [];
26+
27+
const createRootOptions: CreateRootOptions = {
28+
name: 'Test App',
29+
host: '127.0.0.1',
30+
port: 29999,
31+
adminPassword: 'adminPassword',
32+
prefix: '!',
33+
};
34+
35+
process.nextTick(() => {
36+
root = createRoot(createRootOptions);
37+
root.render(
38+
<Button width={20} height={5}>
39+
Hello world
40+
</Button>,
41+
);
42+
});
43+
44+
mitm.on('connection', (socket, opts) => {
45+
expect(opts.host).toEqual('127.0.0.1');
46+
expect(opts.port).toEqual(29999);
47+
48+
socket.on('data', (data) => {
49+
receivedPackets.push(data);
50+
const combined = Buffer.concat(receivedPackets);
51+
const isi = new IS_ISI({
52+
ReqI: 255,
53+
InSimVer: 9,
54+
Prefix: '!',
55+
IName: 'Test App',
56+
Admin: 'adminPassword',
57+
});
58+
59+
if (combined.length >= isi.Size && receivedPackets.length === 1) {
60+
expect(combined).toEqual(Buffer.from(isi.pack()));
61+
62+
const ver = new IS_VER();
63+
const versionPacket = Buffer.from([
64+
ver.Size / ver.SIZE_MULTIPLIER,
65+
ver.Type,
66+
255, // ReqI
67+
0, // Zero
68+
...stringToBytes('0.7F\0\0\0\0'), // Version
69+
...stringToBytes('S3\0\0\0\0'), // Product
70+
InSim.INSIM_VERSION,
71+
0, // Spare
72+
]);
73+
setTimeout(() => socket.write(versionPacket));
74+
75+
receivedPackets = [];
76+
return;
77+
}
78+
79+
const btn = new IS_BTN({
80+
ReqI: 1,
81+
W: 20,
82+
H: 5,
83+
L: 0,
84+
T: 0,
85+
ClickID: 0,
86+
Text: 'Hello world',
87+
});
88+
89+
if (combined.length >= btn.Size) {
90+
expect(combined).toEqual(Buffer.from(btn.pack()));
91+
92+
root.disconnect();
93+
done();
94+
}
95+
});
96+
});
97+
});
98+
});

0 commit comments

Comments
 (0)