Skip to content

Commit 99debd9

Browse files
committed
Fix bus test, somehow it has OpenHandles still, need to debug
1 parent b49a05e commit 99debd9

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed
Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
1-
const Bus = require('../../lib/vehicles/bus')
1+
const moment = require('moment')
22
const { Subject } = require('rxjs')
3+
4+
const Bus = require('../../lib/vehicles/bus')
5+
36
const range = (length) => Array.from({ length }).map((_, i) => i)
4-
const moment = require('moment')
7+
58
describe('A bus', () => {
69
const arjeplog = { lon: 17.886855, lat: 66.041054 }
710
const ljusdal = { lon: 14.44681991219, lat: 61.59465992477 }
8-
let bus
911

10-
it('should be able to pickup multiple bookings and queue the all except the first', () => {
12+
it('should be able to pickup multiple bookings and queue the all except the first', (done) => {
1113
const stops = new Subject()
12-
bus = new Bus({ id: 1, position: arjeplog, stops })
14+
const bus = new Bus({ id: 1, position: arjeplog, stops })
1315

1416
range(10).map((i) =>
1517
stops.next({
16-
pickup: ljusdal,
17-
destination: arjeplog,
18+
position: i % 2 === 0 ? ljusdal : arjeplog,
19+
stopName: i % 2 === 0 ? 'ljusdal' : 'arjeplog',
1820
departureTime: moment('2021-04-20 00:00:00')
1921
.add(i, 'minutes')
2022
.format('HH:mm:ss'),
2123
})
2224
)
2325

2426
const queue = bus.queue
25-
console.log(bus.queue.map((e) => e.pickup.departureTime))
2627
expect(queue.length).toBe(8)
27-
expect(queue[0].pickup).toEqual(ljusdal)
28-
expect(queue[0].departureTime).toBe('00:00:00')
29-
expect(queue[0].arrivalTime).toBe('00:00:00')
30-
expect(queue[0].status).toBe('queued')
28+
expect(queue[0].pickup.position).toEqual(arjeplog)
29+
expect(queue[0].pickup.stopName).toBe('arjeplog')
30+
expect(queue[0].pickup.departureTime).toBe('00:01:00')
31+
expect(queue[0].destination.position).toEqual(ljusdal)
32+
expect(queue[0].destination.stopName).toBe('ljusdal')
33+
expect(queue[0].destination.departureTime).toBe('00:02:00')
34+
expect(queue[0].status).toBe('Queued')
35+
bus.unsubscribe()
36+
done()
3137
})
3238
})

packages/simulator/lib/vehicles/bus.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
const { pairwise, map } = require('rxjs/operators')
2+
13
const Booking = require('../models/booking')
24
const Vehicle = require('./vehicle')
3-
const { pairwise, map } = require('rxjs/operators')
4-
const moment = require('moment')
5-
const { virtualTime } = require('../virtualTime')
65

76
// TODO: create this somewhere else as real fleet
87
const lanstrafiken = {
@@ -33,9 +32,27 @@ class Bus extends Vehicle {
3332
this.vehicleType = 'bus'
3433
this.heading = heading
3534
this.kommun = kommun
36-
this.startPosition = startPosition
35+
this.startPosition = startPosition || position
3736
this.capacity = 50 // TODO: fill this from the workshop poll
3837
this.co2PerKmKg = 1.3 // NOTE: From a quick google. Needs to be verified.
38+
this.stopsSubscription = stops
39+
.pipe(
40+
pairwise(),
41+
map(([pickup, destination]) => {
42+
this.handleBooking(
43+
new Booking({
44+
// pickup and destination contains both position and arrival and departure time
45+
pickup,
46+
destination,
47+
})
48+
)
49+
})
50+
)
51+
.subscribe(() => {})
52+
}
53+
54+
unsubscribe() {
55+
this.stopsSubscription.unsubscribe()
3956
}
4057

4158
async handleBooking(booking) {

0 commit comments

Comments
 (0)