-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.test.ts
More file actions
140 lines (123 loc) · 4.32 KB
/
controller.test.ts
File metadata and controls
140 lines (123 loc) · 4.32 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
import { Controller, IController } from './controller';
import { EventEmitter } from 'events';
class MockHID extends EventEmitter {
write() {}
}
jest.mock('node-hid', () => {
return {
HID: jest.fn().mockImplementation(() => new MockHID())
};
});
describe('Controller', () => {
let controller: Controller;
let mockDevice: MockHID;
beforeEach(() => {
controller = new Controller('/dev/hidraw2');
mockDevice = (controller as any).device;
});
it('should initialize with default state', () => {
expect(controller.state).toMatchObject({
name: 'Steam Controller (Neptune)',
index: 0,
connected: true,
lastUpdate: null,
joysticks: {
left: { x: 0, y: 0 },
right: { x: 0, y: 0 }
},
dpad: {
up: false,
down: false,
left: false,
right: false
},
buttons: {
a: false, b: false, x: false, y: false,
lb: false, rb: false, lt: false, rt: false,
select: false, start: false, steam: false,
leftJoystick: false, rightJoystick: false,
}
});
});
it('should update button state on data event', (done) => {
controller.on('button.a', (state) => {
try {
expect(state).toBe(true);
done();
} catch (error) {
done(error);
}
});
// Simulate data from the HID device
const data = buildBuffer({ button: 'a' });
mockDevice.emit('data', data);
});
it('should update dpad state on data event', (done) => {
controller.on('dpad.up', (state) => {
try {
expect(state).toBe(true);
done();
} catch (error) {
done(error);
}
});
// Simulate data from the HID device
const data = buildBuffer({ dpad: 'up' });
mockDevice.emit('data', data);
});
it('should update joystick state on data event', (done) => {
controller.on('joystick.left.x', (state) => {
try {
expect(state).toBe(0.25196850393700787);
done();
} catch (error) {
done(error);
}
});
// Simulate data from the HID device
const data = buildBuffer({ joystick: { side: 'left', axis: 'x', value: 32 } });
mockDevice.emit('data', data);
});
// Additional tests for other buttons, dpad, and joystick states
});
function buildBuffer(options: { button?: keyof IController['buttons'], dpad?: keyof IController['dpad'], joystick?: { side: keyof IController['joysticks'], axis: keyof IController['joysticks']['left'], value: number } }): Buffer {
const buffer = Buffer.alloc(64); // Create a buffer of 64 bytes initialized with zeros
if (options.button) {
switch (options.button) {
case 'a': buffer[8] = 128; break;
case 'b': buffer[8] = 32; break;
case 'x': buffer[8] = 64; break;
case 'y': buffer[8] = 16; break;
case 'lb': buffer[8] = 8; break;
case 'rb': buffer[8] = 4; break;
case 'lt': buffer[8] = 2; break;
case 'rt': buffer[8] = 1; break;
case 'select': buffer[9] = 16; break;
case 'start': buffer[9] = 64; break;
case 'steam': buffer[9] = 32; break;
case 'leftJoystick': buffer[10] = 64; break;
case 'rightJoystick': buffer[11] = 4; break;
default: break;
}
}
if (options.dpad) {
switch (options.dpad) {
case 'up': buffer[9] = 1; break;
case 'down': buffer[9] = 8; break;
case 'left': buffer[9] = 4; break;
case 'right': buffer[9] = 2; break;
default: break;
}
}
if (options.joystick) {
const { side, axis, value } = options.joystick;
if (side === 'left') {
if (axis === 'x') buffer[49] = value;
if (axis === 'y') buffer[51] = value;
} else if (side === 'right') {
if (axis === 'x') buffer[53] = value;
if (axis === 'y') buffer[55] = value;
}
}
return buffer;
}