-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.js
195 lines (168 loc) · 4.67 KB
/
test.js
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
import {
SystemEvents,
MSFS_API,
MSFS_NOT_CONNECTED,
loadAirportDB,
} from "../msfs-api.js";
import { SimVars } from "../simvars/index.js";
const api = new MSFS_API();
/**
* ...docs go here...
*/
(async function tryConnect() {
console.log(`Testing call prevention prior to connection`);
await testAPriori();
console.log(`Awaiting connection`);
api.connect({
autoReconnect: true,
retries: Infinity,
retryInterval: 5,
onConnect: connect,
onRetry: (_retries, interval) =>
console.log(`Connection failed: retrying in ${interval} seconds.`),
});
})();
/**
* ...docs go here...
*/
async function testAPriori() {
try {
await Promise.all(
[`on`, `trigger`, `get`, `set`, `schedule`].map(async (fname) => {
try {
await api[fname](`the function input should not matter`);
throw new Error(
`"${fname}" was allowed through, despite there not being a connection yet.`
);
} catch (e) {
if (e.message !== MSFS_NOT_CONNECTED) throw e;
}
})
);
} catch (e) {
throw e;
}
}
/**
* ...docs go here...
* @param {*} handle
*/
async function connect(handle) {
console.log(`MSFS connected`);
if (true) {
console.log(`testing direct airport access`);
const airports = loadAirportDB();
console.log(`${airports.length} airports loaded directly`);
// find all airports near Denver
const [lat, long] = [39.7642219, -105.0202604];
const start = performance.now();
const denver = airports.filter((a) => {
const { latitude: y, longitude: x } = a;
return lat - 0.5 < y && y < lat + 0.5 && long - 0.5 < x && x < long + 0.5;
});
const end = performance.now();
console.log(
`found ${
denver.length
} airports found in a 1 arc degree rect around Denver in ${end - start}ms`
);
}
const { CAMERA_STATE: camera } = await api.get(`CAMERA_STATE`);
if (camera > 10) {
throw new Error(`MSFS needs to be "in game" for the tests to run\n`);
}
const pauseOff = api.on(SystemEvents.PAUSED, () => {
pauseOff();
console.log(`sim paused`);
});
const unpauseOff = api.on(SystemEvents.UNPAUSED, () => {
unpauseOff();
console.log(`sim unpaused`);
});
const { ALL_AIRPORTS } = await api.get(`ALL_AIRPORTS`);
console.log(`${ALL_AIRPORTS.length} total airports on the planet`);
const { NEARBY_AIRPORTS } = await api.get(`NEARBY_AIRPORTS`);
console.log(`${NEARBY_AIRPORTS.length} airports in local reality bubble`);
const radius = 10;
const near = await api.get(`NEARBY_AIRPORTS:${radius}`);
const nearList = near[`NEARBY_AIRPORTS:${radius}`];
console.log(
`${nearList.length} airports found within a ${radius}NM radius around the plane`
);
console.log(`getting ${NEARBY_AIRPORTS[0].icao}`);
const airportData = await api.get(`AIRPORT:${NEARBY_AIRPORTS[0].icao}`);
console.log(`result:`, JSON.stringify(airportData, null, 2));
api.on(SystemEvents.AIRPORTS_IN_RANGE, (data) => {
console.log(`New in range:`, data);
});
api.on(SystemEvents.AIRPORTS_OUT_OF_RANGE, (data) => {
console.log(`Out of range:`, data);
});
try {
console.log(`Quick "unknown var" test`);
await api.get(`PLANE_LONGITUDE`, `NO_THANKS`);
} catch (e) {
if (e.message !== `Cannot get SimVar: "NO THANKS" unknown.`) throw e;
}
runTests(api);
}
/**
* ...docs go here...
* @param {*} api
*/
async function runTests(api) {
console.log(`Running sim variables tests`);
await testSimVars(api);
console.log(`Running sim events tests`);
await testSimEvents(api);
console.log(`Running interval test`);
testInterval(api, () => {
process.exit(0);
});
}
/**
* ...docs go here...
* @param {*} api
*/
async function testSimVars(api) {
const varKeys = Object.keys(SimVars);
for (let i = 0; i < varKeys.length; i++) {
const key = varKeys[i];
console.log(`[${i}] ${key}`);
console.log(await api.get(key.replace(`:index`, `:1`)), `\n`);
}
console.log(`Tested ${varKeys.length} sim vars.`);
}
/**
* ...docs go here...
* @param {*} api
*/
async function testSimEvents(api) {
let lock = await api.get(`TAILWHEEL_LOCK_ON`);
await api.trigger(`TOGGLE_TAILWHEEL_LOCK`);
let newlock = await api.get(`TAILWHEEL_LOCK_ON`);
if (lock === newlock) {
throw new Error(`TAILWHEEL_LOCK_ON did not change state!`);
}
console.log(`\nSim event trigger passed.\n`);
}
/**
* ...docs go here...
* @param {*} api
* @param {*} done
*/
function testInterval(api, done) {
const stop = api.schedule(
(data) => {
console.log(data);
},
500,
"RUDDER POSITION",
"PLANE LONGITUDE",
"PLANE LATITUDE"
);
setTimeout(() => {
stop();
done();
}, 5000);
}