Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7,383 changes: 7,371 additions & 12 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "## Background",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest packages/**/__tests__/**/*.js"
},
"repository": {
"type": "git",
Expand All @@ -18,5 +18,9 @@
"homepage": "https://github.com/PredictiveMovement/digital-twin#readme",
"dependencies": {
"dotenv": "^16.0.2"
},
"devDependencies": {
"eslint": "^8.26.0",
"jest": "^29.1.2"
}
}
54 changes: 54 additions & 0 deletions packages/simulator/__tests__/unit/booking.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const Booking = require('../../lib/models/booking')

describe('A booking', () => {
const arjeplog = { lon: 17.886855, lat: 66.041054 }
const ljusdal = { lon: 14.44681991219, lat: 61.59465992477 }
const ljusdalToArjeplog = {
pickup: {
position: ljusdal,
},
destination: {
position: arjeplog,
},
}
let subject

describe('should initialize correctly', function () {
beforeEach(() => {
subject = new Booking({id: 'b-1', ...ljusdalToArjeplog})
})

it('and have a correct id', () => {
expect(subject.id).toBe('b-1')
})

it('and have a correct status', () => {
expect(subject.status).toBe('New')
})

it('and have a correct position', () => {
expect(subject.position).toBe(ljusdal)
})

it('and have a correct pickup', () => {
expect(subject.pickup).toEqual(ljusdalToArjeplog.pickup)
})

it('and have a correct destination', () => {
expect(subject.destination).toEqual(ljusdalToArjeplog.destination)
})
})
describe('should validate initialization parameters', () => {
it('and throw an error if pickup is missing', () => {
expect(() => {
new Booking({ id: 'b-1', destination: ljusdalToArjeplog.destination })
}).toThrowError('Invalid booking - Missing pickup position')
})

it('and throw an error if destination is missing', () => {
expect(() => {
new Booking({ id: 'b-1', pickup: ljusdalToArjeplog.pickup })
}).toThrowError('Invalid booking - Missing destination position')
})
})
})
48 changes: 29 additions & 19 deletions packages/simulator/__tests__/unit/bus.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
const Bus = require('../../lib/vehicles/bus')
const moment = require('moment')
const { Subject } = require('rxjs')
const Booking = require('../../lib/models/booking')

const Bus = require('../../lib/vehicles/bus')

const range = (length) => Array.from({ length }).map((_, i) => i)
const moment = require('moment')

describe('A bus', () => {
const arjeplog = { lon: 17.886855, lat: 66.041054 }
const ljusdal = { lon: 14.44681991219, lat: 61.59465992477 }
let bus
const arjeplog = { position: { lon: 17.886855, lat: 66.041054 } }
const ljusdal = { position: { lon: 14.44681991219, lat: 61.59465992477 } }

it.only('should be able to pickup multiple bookings and queue the all except the first', () => {
it('should be able to pickup multiple bookings and queue the all except the first', (done) => {
const stops = new Subject()
bus = new Bus({ id: 1, position: arjeplog, stops })
const bus = new Bus({ id: 1, position: arjeplog.position, stops })

range(10).map((i) =>
stops.next({
pickup: ljusdal,
destination: arjeplog,
departureTime: moment('2021-04-20:00:00:00')
.add(i, 'minutes')
.format('HH:mm:ss'),
})
stops.next(
new Booking({
pickup: i % 2 === 0 ? ljusdal : arjeplog,
destination: i % 2 === 0 ? arjeplog : ljusdal,
stopName: i % 2 === 0 ? 'ljusdal' : 'arjeplog',
departureTime: moment('2021-04-20 00:00:00')
.add(i, 'minutes')
.format('HH:mm:ss'),
})
)
)

const queue = bus.queue
console.log(bus.queue.map((e) => e.pickup.departureTime))
expect(queue.length).toBe(8)
expect(queue[0].pickup).toEqual(ljusdal)
expect(queue[0].departureTime).toBe('00:00:00')
expect(queue[0].arrivalTime).toBe('00:00:00')
expect(queue[0].status).toBe('queued')
expect(queue[0].pickup.position).toEqual(arjeplog.position)
expect(queue[0].pickup.stopName).toBe('arjeplog')
expect(queue[0].pickup.departureTime).toBe('00:01:00')
expect(queue[0].destination.position).toEqual(ljusdal.position)
expect(queue[0].destination.stopName).toBe('ljusdal')
expect(queue[0].destination.departureTime).toBe('00:02:00')
expect(queue[0].status).toBe('Queued')
bus.unsubscribe() // TODO: This is a code smell
done()
})
})
Loading