forked from vkondepati/fleet-route-optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-tests.js
More file actions
416 lines (342 loc) · 11.9 KB
/
example-tests.js
File metadata and controls
416 lines (342 loc) · 11.9 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Example test files for the OpenRoute Optimizer platform
// packages/core/tests/astar.test.js
import { AStarPathfinder } from '../src/algorithms/AStar'
describe('A* Pathfinding Algorithm', () => {
let astar
let mockRoadNetwork
beforeEach(() => {
mockRoadNetwork = {
getNeighbors: jest.fn((nodeId) => [
{
id: 'neighbor1',
position: [37.7749, -122.4194],
distance: 1.0,
roadType: 'arterial'
}
])
}
astar = new AStarPathfinder(mockRoadNetwork)
})
test('should find path between two points', () => {
const start = [37.7749, -122.4194]
const goal = [37.7849, -122.4094]
const result = astar.findPath(start, goal)
expect(result).toHaveLength(greaterThan(0))
expect(result[0].start).toEqual(start)
expect(result[result.length - 1].end).toEqual(goal)
})
test('should respect vehicle constraints', () => {
const vehicle = {
id: 'truck1',
restrictions: { maxWeight: 5000 }
}
const start = [37.7749, -122.4194]
const goal = [37.7849, -122.4094]
const result = astar.findPath(start, goal, vehicle)
expect(mockRoadNetwork.getNeighbors).toHaveBeenCalledWith(
expect.any(String),
vehicle
)
})
test('should throw error when no path exists', () => {
mockRoadNetwork.getNeighbors.mockReturnValue([])
const start = [37.7749, -122.4194]
const goal = [37.7849, -122.4094]
expect(() => {
astar.findPath(start, goal)
}).toThrow('No path found between start and goal')
})
})
// packages/core/tests/vrp-solver.test.js
import { VRPSolver } from '../src/algorithms/VRP'
describe('VRP Solver', () => {
let vrpSolver
let testInstance
beforeEach(() => {
vrpSolver = new VRPSolver()
testInstance = {
depot: [37.7749, -122.4194],
vehicles: [
{ id: 'v1', capacity: 1000, status: 'active' },
{ id: 'v2', capacity: 1500, status: 'active' }
],
deliveries: [
{ id: 'd1', position: [37.7849, -122.4094], weight: 300 },
{ id: 'd2', position: [37.7949, -122.3994], weight: 500 },
{ id: 'd3', position: [37.8049, -122.3894], weight: 200 }
],
distanceMatrix: [
[0, 1.0, 2.0, 3.0],
[1.0, 0, 1.0, 2.0],
[2.0, 1.0, 0, 1.0],
[3.0, 2.0, 1.0, 0]
],
timeMatrix: [
[0, 2, 4, 6],
[2, 0, 2, 4],
[4, 2, 0, 2],
[6, 4, 2, 0]
],
constraints: {
capacityConstraints: true,
timeWindowConstraints: false
}
}
})
test('Clarke-Wright should solve basic VRP', () => {
const solution = vrpSolver.solveClarkeWright(testInstance)
expect(solution.feasible).toBe(true)
expect(solution.routes).toHaveLength(greaterThan(0))
expect(solution.computationTime).toBeGreaterThan(0)
// Verify all deliveries are assigned
const assignedDeliveries = solution.routes.flatMap(r => r.sequence)
expect(assignedDeliveries).toHaveLength(3)
expect(assignedDeliveries).toContain('d1')
expect(assignedDeliveries).toContain('d2')
expect(assignedDeliveries).toContain('d3')
})
test('should respect capacity constraints', () => {
// Create instance where one vehicle can't handle all deliveries
testInstance.vehicles = [{ id: 'v1', capacity: 600, status: 'active' }]
const solution = vrpSolver.solveClarkeWright(testInstance)
solution.routes.forEach(route => {
expect(route.load).toBeLessThanOrEqual(600)
})
})
test('Genetic algorithm should improve solution quality', () => {
const clarkeWrightSolution = vrpSolver.solveClarkeWright(testInstance)
const geneticSolution = vrpSolver.solveGenetic(testInstance, {
populationSize: 20,
generations: 50
})
expect(geneticSolution.feasible).toBe(true)
// Genetic should be equal or better
expect(geneticSolution.objectiveValue).toBeLessThanOrEqual(
clarkeWrightSolution.objectiveValue * 1.1 // Allow 10% tolerance
)
})
})
// packages/web/tests/MapContainer.test.tsx
import React from 'react'
import { render, screen, fireEvent } from '@testing-library/react'
import { MapContainer } from '../src/components/MapContainer'
describe('MapContainer Component', () => {
const mockVehicles = [
{
id: '1',
name: 'Vehicle 001',
position: [37.7749, -122.4194],
status: 'active'
},
{
id: '2',
name: 'Vehicle 002',
position: [37.7849, -122.4094],
status: 'idle'
}
]
const mockRoutes = [
{
id: '1',
name: 'Downtown Route',
coordinates: [
[37.7749, -122.4194],
[37.7849, -122.4094]
],
distance: 1.5,
duration: 3,
status: 'active'
}
]
beforeEach(() => {
// Reset Leaflet mocks
jest.clearAllMocks()
})
test('renders map container', () => {
render(<MapContainer />)
expect(L.map).toHaveBeenCalled()
expect(L.tileLayer).toHaveBeenCalledWith(
'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
expect.objectContaining({
attribution: '© OpenStreetMap contributors'
})
)
})
test('displays vehicles on map', () => {
render(<MapContainer vehicles={mockVehicles} />)
// Should create markers for each vehicle
expect(L.marker).toHaveBeenCalledTimes(2)
expect(L.marker).toHaveBeenCalledWith([37.7749, -122.4194], expect.any(Object))
expect(L.marker).toHaveBeenCalledWith([37.7849, -122.4094], expect.any(Object))
})
test('displays routes on map', () => {
render(<MapContainer routes={mockRoutes} />)
expect(L.polyline).toHaveBeenCalledWith(
[[37.7749, -122.4194], [37.7849, -122.4094]],
expect.objectContaining({
color: '#4caf50', // Active route color
weight: 4
})
)
})
test('shows legend with correct information', () => {
render(<MapContainer vehicles={mockVehicles} />)
expect(screen.getByText('Legend')).toBeInTheDocument()
expect(screen.getByText('Vehicles')).toBeInTheDocument()
expect(screen.getByText('Active')).toBeInTheDocument()
expect(screen.getByText('Idle')).toBeInTheDocument()
})
test('updates markers when vehicles change', () => {
const { rerender } = render(<MapContainer vehicles={mockVehicles} />)
const updatedVehicles = [
...mockVehicles,
{
id: '3',
name: 'Vehicle 003',
position: [37.7949, -122.3994],
status: 'maintenance'
}
]
rerender(<MapContainer vehicles={updatedVehicles} />)
// Should have created 3 markers total
expect(L.marker).toHaveBeenCalledTimes(5) // 2 initial + 3 updated
})
})
// packages/web/tests/Dashboard.test.tsx
import React from 'react'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import { Dashboard } from '../src/components/Dashboard'
describe('Dashboard Component', () => {
test('renders navigation menu', () => {
render(<Dashboard />)
expect(screen.getByText('Dashboard')).toBeInTheDocument()
expect(screen.getByText('Vehicles')).toBeInTheDocument()
expect(screen.getByText('Routes')).toBeInTheDocument()
expect(screen.getByText('Analytics')).toBeInTheDocument()
expect(screen.getByText('Settings')).toBeInTheDocument()
})
test('shows fleet status cards', () => {
render(<Dashboard />)
expect(screen.getByText('Active')).toBeInTheDocument()
expect(screen.getByText('Idle')).toBeInTheDocument()
expect(screen.getByText('1')).toBeInTheDocument() // Active count
})
test('optimize button triggers optimization', async () => {
const mockOptimize = jest.fn()
render(<Dashboard onOptimize={mockOptimize} />)
const optimizeButton = screen.getByText('Optimize Routes')
fireEvent.click(optimizeButton)
expect(optimizeButton).toBeDisabled()
expect(screen.getByText('Optimizing...')).toBeInTheDocument()
await waitFor(() => {
expect(screen.getByRole('progressbar')).toBeInTheDocument()
})
})
test('displays vehicle list with status chips', () => {
render(<Dashboard />)
expect(screen.getByText('Vehicle 001')).toBeInTheDocument()
expect(screen.getByText('Vehicle 002')).toBeInTheDocument()
expect(screen.getByText('Vehicle 003')).toBeInTheDocument()
// Check status chips
const activeChip = screen.getByText('active')
const idleChip = screen.getByText('idle')
const maintenanceChip = screen.getByText('maintenance')
expect(activeChip).toHaveClass('MuiChip-colorSuccess')
expect(idleChip).toHaveClass('MuiChip-colorWarning')
expect(maintenanceChip).toHaveClass('MuiChip-colorError')
})
test('shows route progress bars', () => {
render(<Dashboard />)
expect(screen.getByText('Downtown Delivery')).toBeInTheDocument()
expect(screen.getByText('75%')).toBeInTheDocument()
expect(screen.getByText('15 min')).toBeInTheDocument()
const progressBars = screen.getAllByRole('progressbar')
expect(progressBars).toHaveLength(greaterThan(0))
})
})
// Integration test example
// tests/integration/route-optimization.test.js
import request from 'supertest'
import { app } from '../../src/app'
import { setupTestDatabase, cleanupTestDatabase } from '../helpers/database'
describe('Route Optimization Integration', () => {
beforeAll(async () => {
await setupTestDatabase()
})
afterAll(async () => {
await cleanupTestDatabase()
})
test('complete optimization workflow', async () => {
// 1. Create vehicles
const vehicleResponse = await request(app)
.post('/api/vehicles')
.send({
name: 'Test Truck',
capacity: 1000,
vehicleType: 'truck'
})
.expect(201)
const vehicleId = vehicleResponse.body.id
// 2. Create deliveries
const deliveryResponse = await request(app)
.post('/api/deliveries')
.send({
address: '123 Test Street',
position: [37.7749, -122.4194],
weight: 500
})
.expect(201)
const deliveryId = deliveryResponse.body.id
// 3. Request route optimization
const optimizationResponse = await request(app)
.post('/api/optimize')
.send({
vehicleIds: [vehicleId],
deliveryIds: [deliveryId],
depot: [37.7749, -122.4194]
})
.expect(200)
const { routes, statistics } = optimizationResponse.body
// 4. Verify optimization results
expect(routes).toHaveLength(1)
expect(routes[0].vehicleId).toBe(vehicleId)
expect(routes[0].deliveries).toContain(deliveryId)
expect(routes[0].totalDistance).toBeGreaterThan(0)
expect(routes[0].totalDuration).toBeGreaterThan(0)
expect(statistics.totalDistance).toBeGreaterThan(0)
expect(statistics.deliverySuccess).toBe(1.0)
expect(statistics.constraintViolations).toHaveLength(0)
})
test('real-time position updates', async () => {
// Start WebSocket server
const wsServer = new WebSocketServer({ port: 8081 })
// Connect client
const client = new WebSocket('ws://localhost:8081')
await new Promise(resolve => client.on('open', resolve))
// Subscribe to vehicle updates
client.send(JSON.stringify({
type: 'subscribe',
channel: 'vehicle_positions',
vehicleId: 'test-vehicle-1'
}))
// Send position update via API
await request(app)
.post('/api/vehicles/test-vehicle-1/position')
.send({
position: [37.7749, -122.4194],
timestamp: new Date().toISOString()
})
.expect(200)
// Verify WebSocket receives update
const message = await new Promise(resolve => {
client.on('message', data => {
resolve(JSON.parse(data))
})
})
expect(message.type).toBe('position_update')
expect(message.vehicleId).toBe('test-vehicle-1')
expect(message.position).toEqual([37.7749, -122.4194])
client.close()
wsServer.close()
})
})