-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
test.js
153 lines (138 loc) · 4.28 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
import test from 'tape'
import {fetchFromRadar} from './lib/radar.js'
import {fetchFlight} from './lib/flight.js'
const validateAirport = (t, a) => {
t.equal(typeof a.id, 'string') // todo: validate IATA code
t.equal(typeof a.name, 'string')
t.ok(a.coordinates)
t.equal(typeof a.coordinates.latitude, 'number')
t.equal(typeof a.coordinates.longitude, 'number')
t.equal(typeof a.coordinates.altitude, 'number')
t.ok(a.coordinates.altitude >= 0)
t.ok(a.coordinates.altitude < 100000)
t.equal(typeof a.timezone, 'string') // todo: validate
if (a.country) t.equal(typeof a.country, 'string') // todo: validate
}
const isValidISODate = (d) => !Number.isNaN(+new Date(d))
test('fetchFromRadar', async (t) => {
const flights = await fetchFromRadar(50.5, 8.5, 50, 9)
{
t.ok(Array.isArray(flights))
if (!flights[0]) throw new Error('no flights found')
for (let flight of flights) {
t.ok(flight)
if (flight.id !== null) {
t.equal(typeof flight.id, 'string') // todo: validate IATA code
}
t.equal(typeof flight.registration, 'string')
if (flight.flight !== null) {
t.equal(typeof flight.flight, 'string')
}
t.equal(typeof flight.callsign, 'string')
if (flight.origin !== null) {
t.equal(typeof flight.origin, 'string')
}
if (flight.destination !== null) {
t.equal(typeof flight.destination, 'string')
}
t.equal(typeof flight.latitude, 'number')
t.equal(typeof flight.longitude, 'number')
t.equal(typeof flight.altitude, 'number')
t.equal(typeof flight.bearing, 'number')
if (flight.speed !== null) {
t.equal(typeof flight.speed, 'number')
}
if (flight.rateOfClimb !== null) {
t.equal(typeof flight.rateOfClimb, 'number')
}
t.equal(typeof flight.isOnGround, 'boolean')
t.equal(typeof flight.squawkCode, 'string')
if (flight.model !== null) {
t.equal(typeof flight.model, 'string')
}
t.equal(typeof flight.modeSCode, 'string')
t.equal(typeof flight.radar, 'string')
t.equal(typeof flight.isGlider, 'boolean')
t.equal(typeof flight.timestamp, 'number')
}
}
t.end()
})
test('fetchFlight', async (t) => {
const flights = await fetchFromRadar(50.5, 8.5, 50, 9)
{
t.ok(Array.isArray(flights))
if (!flights[0]) throw new Error('no flights found')
t.ok(flights[0])
t.ok(flights[0].id)
}
const flight = await fetchFlight(flights[0].id)
{
t.ok(flight)
t.equal(typeof flight.liveData, 'boolean')
t.equal(typeof flight.id, 'string') // todo: validate IATA code
t.equal(typeof flight.callsign, 'string')
if (flight.model) t.equal(typeof flight.model, 'string')
t.equal(typeof flight.registration, 'string')
if (flight.airline) {
t.equal(typeof flight.airline, 'string')
// todo: validate IATA code
}
if (flight.origin) validateAirport(t, flight.origin)
if (flight.destination) validateAirport(t, flight.destination)
if (flight.departure) {
t.ok(isValidISODate(flight.departure))
// todo: validate timezone
}
if (flight.scheduledDeparture) {
t.ok(isValidISODate(flight.scheduledDeparture))
// todo: validate timezone
}
if (flight.departureTerminal) {
t.equal(typeof flight.departureTerminal, 'string')
}
if (flight.departureGate) {
t.equal(typeof flight.departureGate, 'string')
}
if (flight.arrival) {
t.ok(isValidISODate(flight.arrival))
// todo: validate timezone
}
if (flight.scheduledArrival) {
t.ok(isValidISODate(flight.scheduledArrival))
// todo: validate timezone
}
if (flight.arrivalTerminal) {
t.equal(typeof flight.arrivalTerminal, 'string')
}
if (flight.arrivalGate) {
t.equal(typeof flight.arrivalGate, 'string')
}
if (flight.delay !== null) t.equal(typeof flight.delay, 'number')
if (flight.trail !== null) {
t.ok(Array.isArray(flight.trail))
for (let trail of flight.trail) {
t.ok(trail)
if (trail.latitude !== null) {
t.equal(typeof trail.latitude, 'number')
}
if (trail.longitude !== null) {
t.equal(typeof trail.longitude, 'number')
}
if (trail.altitude !== null) {
t.equal(typeof trail.altitude, 'number')
}
if (trail.bearing !== null) {
t.equal(typeof trail.bearing, 'number')
}
if (trail.speed !== null) {
t.equal(typeof trail.speed, 'number')
}
if (trail.timestamp !== null) {
t.equal(typeof trail.timestamp, 'number')
}
}
}
}
t.end()
})