forked from googlemaps/fleet-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTripLogs.test.js
More file actions
310 lines (280 loc) · 8.79 KB
/
TripLogs.test.js
File metadata and controls
310 lines (280 loc) · 8.79 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// src/TripLogs.test.js
import TripLogs from "./TripLogs";
import fs from "fs";
import _ from "lodash";
async function loadTripLogs(dataset) {
const parsedData = JSON.parse(fs.readFileSync(dataset));
return new TripLogs(parsedData.rawLogs, parsedData.solutionType);
}
test("basic odrd trip log loading", async () => {
const tripLogs = await loadTripLogs("./datasets/jump-demo.json");
expect(tripLogs.getTripIDs()).toStrictEqual([
"non-trip-segment-0",
"a51c8f15-47c0-4bae-8262-8421b0a3e058",
"non-trip-segment-1",
"2f03ff03-1d0e-49e2-91d4-f3c674d98073",
"non-trip-segment-2",
"d9d7686f-0dd2-4114-a1e2-ae11201d4c0f",
"non-trip-segment-3",
"73a320f8-0e1f-44cb-ba99-cdad010cdaf4",
"non-trip-segment-4",
]);
});
test("basic lmfs trip log loading", async () => {
const tripLogs = await loadTripLogs("./datasets/lmfs.json");
expect(tripLogs.getTripIDs()).toStrictEqual([
"Stops Left 11",
"Stops Left 10",
"Stops Left 9",
"Stops Left 8",
"Stops Left 7",
"Stops Left 6",
"Stops Left 5",
"Stops Left 4",
"Stops Left 3",
]);
});
describe("Location Data Processing", () => {
test("LMFS log populates .location from .rawlocation as a fallback", () => {
const rawLocation = { latitude: 37.422, longitude: -122.084 };
const mockLogs = [
{
timestamp: "2023-01-01T12:00:00Z",
jsonpayload: {
"@type": "updateDeliveryVehicle",
request: {
deliveryvehicle: {
lastlocation: {
rawlocation: rawLocation,
},
},
},
response: {},
},
},
];
const tripLogs = new TripLogs(mockLogs, "LMFS");
expect(tripLogs.rawLogs[0].lastlocation.location).toEqual(rawLocation);
});
test("ODRD log with both location and rawlocation prefers location", () => {
const snappedLocation = { latitude: 37.7749, longitude: -122.4194 };
const rawLocation = { latitude: 37.775, longitude: -122.4195 };
const mockLogs = [
{
timestamp: "2023-01-01T10:00:00Z",
jsonpayload: {
"@type": "updateVehicle",
request: {
vehicle: {
lastlocation: {
location: snappedLocation,
rawlocation: rawLocation,
},
},
},
response: {},
},
},
];
const tripLogs = new TripLogs(mockLogs, "ODRD");
expect(tripLogs.rawLogs[0].lastlocation.location).toEqual(snappedLocation);
expect(tripLogs.rawLogs[0].lastlocation.location).not.toEqual(rawLocation);
});
test("lastKnownState propagates location from an LMFS rawlocation", () => {
const rawLocation = { latitude: 37.422, longitude: -122.084 };
const mockLogs = [
{
timestamp: "2023-01-01T12:00:00Z",
jsonpayload: {
"@type": "updateDeliveryVehicle",
request: {
deliveryvehicle: {
lastlocation: {
rawlocation: rawLocation,
heading: 180,
},
},
},
response: {},
},
},
{
timestamp: "2023-01-01T12:01:00Z",
jsonpayload: {
"@type": "updateDeliveryVehicle",
request: {
deliveryvehicle: {},
},
response: {},
},
},
];
const tripLogs = new TripLogs(mockLogs, "LMFS");
expect(tripLogs.rawLogs[1].lastlocation.location).toEqual(rawLocation);
expect(tripLogs.rawLogs[1].lastlocation.heading).toBe(180);
});
});
test("lastKnownState location is correctly applied to subsequent logs", () => {
// Create mock data with two log entries
const mockLogs = [
{
timestamp: "2023-01-01T10:00:00Z",
jsonpayload: {
"@type": "updateVehicle",
request: {
vehicle: {
lastlocation: {
location: { latitude: 37.7749, longitude: -122.4194 },
heading: 90,
},
},
},
response: {},
},
},
{
timestamp: "2023-01-01T10:01:00Z",
jsonpayload: {
"@type": "updateVehicle",
request: {
vehicle: {},
},
response: {},
},
},
];
const tripLogs = new TripLogs(mockLogs, "ODRD");
// Check that the second log entry received the lastKnownState from the first
expect(tripLogs.rawLogs[1].lastlocation.location).toEqual({ latitude: 37.7749, longitude: -122.4194 });
expect(tripLogs.rawLogs[1].lastlocation.heading).toBe(90);
});
test("request and response objects are not mutated", () => {
// Create a log with location data
const originalLocation = { latitude: 37.7749, longitude: -122.4194 };
const mockLog = {
timestamp: "2023-01-01T10:00:00Z",
jsonpayload: {
"@type": "updateVehicle",
request: {
vehicle: {
lastlocation: {
location: originalLocation,
heading: 90,
},
},
},
response: {},
},
};
const tripLogs = new TripLogs([mockLog], "ODRD");
// Verify the original request object was not modified
expect(tripLogs.rawLogs[0].request.vehicle.lastlocation.location).not.toBe(originalLocation);
// But the data is still equal
expect(tripLogs.rawLogs[0].request.vehicle.lastlocation.location).toEqual(originalLocation);
// Modify the synthetic field
tripLogs.rawLogs[0].lastlocation.location.latitude = 38.0;
// Verify the original was not affected
expect(tripLogs.rawLogs[0].request.vehicle.lastlocation.location).toEqual(originalLocation);
});
test("lastKnownState route segments are reset with NO_GUIDANCE", () => {
// Create mock data with three log entries
const mockLogs = [
{
timestamp: "2023-01-01T10:00:00Z",
jsonpayload: {
"@type": "updateVehicle",
request: {
vehicle: {
currentroutesegment: { id: "segment1", path: [{ latitude: 37.7, longitude: -122.4 }] },
currentroutesegmenttraffic: { speed: 30 },
},
},
response: {},
},
},
{
timestamp: "2023-01-01T10:01:00Z",
jsonpayload: {
"@type": "updateVehicle",
request: {
vehicle: {
navstatus: "NAVIGATION_STATUS_NO_GUIDANCE",
},
},
response: {},
},
},
{
timestamp: "2023-01-01T10:02:00Z",
jsonpayload: {
"@type": "updateVehicle",
request: {
vehicle: {},
},
response: {},
},
},
];
const tripLogs = new TripLogs(mockLogs, "ODRD");
// First log should have the route segment
expect(tripLogs.rawLogs[0].lastlocation.currentroutesegment).toBeDefined();
// Second log sets navstatus to NO_GUIDANCE, so lastKnownState should be reset
expect(tripLogs.rawLogs[1].lastlocation.currentroutesegment).not.toBeDefined();
// Third log should not have route segment since it was reset
expect(tripLogs.rawLogs[2].lastlocation.currentroutesegment).not.toBeDefined();
});
test("lastKnownState is properly propagated through a sequence of logs", () => {
// Create a sequence of logs with varying data
const mockLogs = [
{
// Log 1: Has location but no route
timestamp: "2023-01-01T10:00:00Z",
jsonpayload: {
"@type": "updateVehicle",
request: {
vehicle: {
lastlocation: {
location: { latitude: 37.7749, longitude: -122.4194 },
heading: 90,
},
},
},
response: {},
},
},
{
// Log 2: Has route but no location
timestamp: "2023-01-01T10:01:00Z",
jsonpayload: {
"@type": "updateVehicle",
request: {
vehicle: {
currentroutesegment: { id: "segment1", path: [{ latitude: 37.7, longitude: -122.4 }] },
},
},
response: {},
},
},
{
// Log 3: Has neither location nor route
timestamp: "2023-01-01T10:02:00Z",
jsonpayload: {
"@type": "updateVehicle",
request: {
vehicle: {},
},
response: {},
},
},
];
const tripLogs = new TripLogs(mockLogs, "ODRD");
// Log 1: Should have location but no route
expect(tripLogs.rawLogs[0].lastlocation.location).toEqual({ latitude: 37.7749, longitude: -122.4194 });
expect(tripLogs.rawLogs[0].lastlocation.currentroutesegment).not.toBeDefined();
// Log 2: Should have location from Log 1 and its own route
expect(tripLogs.rawLogs[1].lastlocation.location).toEqual({ latitude: 37.7749, longitude: -122.4194 });
expect(tripLogs.rawLogs[1].lastlocation.currentroutesegment).toBeDefined();
// Log 3: Should have both location from Log 1 and route from Log 2
expect(tripLogs.rawLogs[2].lastlocation.location).toEqual({ latitude: 37.7749, longitude: -122.4194 });
expect(tripLogs.rawLogs[2].lastlocation.currentroutesegment).toBeDefined();
});