283 lines
6.7 KiB
TypeScript
283 lines
6.7 KiB
TypeScript
import { Person } from '../../../../domain/entities/ecosystem/person';
|
|
import {
|
|
Geography,
|
|
RouteKey,
|
|
} from '../../../../domain/entities/ecosystem/geography';
|
|
import { Role } from '../../../../domain/types/role.enum';
|
|
import { NamedRoute } from '../../../../domain/entities/ecosystem/named-route';
|
|
import { Route } from '../../../../domain/entities/ecosystem/route';
|
|
import { IGeodesic } from '../../../../domain/interfaces/geodesic.interface';
|
|
import { PointType } from '../../../../domain/types/geography.enum';
|
|
|
|
const person: Person = new Person(
|
|
{
|
|
identifier: 1,
|
|
},
|
|
0,
|
|
900,
|
|
);
|
|
|
|
const mockGeodesic: IGeodesic = {
|
|
inverse: jest.fn().mockImplementation(() => ({
|
|
azimuth: 45,
|
|
distance: 50000,
|
|
})),
|
|
};
|
|
|
|
const mockGeorouter = {
|
|
route: jest
|
|
.fn()
|
|
.mockImplementationOnce(() => {
|
|
return [
|
|
<NamedRoute>{
|
|
key: RouteKey.COMMON,
|
|
route: new Route(mockGeodesic),
|
|
},
|
|
];
|
|
})
|
|
.mockImplementationOnce(() => {
|
|
return [
|
|
<NamedRoute>{
|
|
key: RouteKey.DRIVER,
|
|
route: new Route(mockGeodesic),
|
|
},
|
|
<NamedRoute>{
|
|
key: RouteKey.PASSENGER,
|
|
route: new Route(mockGeodesic),
|
|
},
|
|
];
|
|
})
|
|
.mockImplementationOnce(() => {
|
|
return [
|
|
<NamedRoute>{
|
|
key: RouteKey.DRIVER,
|
|
route: new Route(mockGeodesic),
|
|
},
|
|
];
|
|
})
|
|
.mockImplementationOnce(() => {
|
|
return [
|
|
<NamedRoute>{
|
|
key: RouteKey.PASSENGER,
|
|
route: new Route(mockGeodesic),
|
|
},
|
|
];
|
|
}),
|
|
};
|
|
|
|
describe('Geography entity', () => {
|
|
it('should be defined', () => {
|
|
const geography = new Geography(
|
|
{
|
|
waypoints: [
|
|
{
|
|
lat: 49.440041,
|
|
lon: 1.093912,
|
|
},
|
|
{
|
|
lat: 50.630992,
|
|
lon: 3.045432,
|
|
},
|
|
],
|
|
},
|
|
'Europe/Paris',
|
|
person,
|
|
);
|
|
expect(geography).toBeDefined();
|
|
});
|
|
|
|
describe('init', () => {
|
|
it('should initialize a geography request with point types', () => {
|
|
const geography = new Geography(
|
|
{
|
|
waypoints: [
|
|
{
|
|
lat: 49.440041,
|
|
lon: 1.093912,
|
|
type: PointType.LOCALITY,
|
|
},
|
|
{
|
|
lat: 50.630992,
|
|
lon: 3.045432,
|
|
type: PointType.LOCALITY,
|
|
},
|
|
],
|
|
},
|
|
'Europe/Paris',
|
|
person,
|
|
);
|
|
geography.init();
|
|
expect(geography._points.length).toBe(2);
|
|
expect(geography.originType).toBe(PointType.LOCALITY);
|
|
expect(geography.destinationType).toBe(PointType.LOCALITY);
|
|
});
|
|
it('should throw an exception if waypoints are empty', () => {
|
|
const geography = new Geography(
|
|
{
|
|
waypoints: [],
|
|
},
|
|
'Europe/Paris',
|
|
person,
|
|
);
|
|
expect(() => geography.init()).toThrow();
|
|
});
|
|
it('should throw an exception if only one waypoint is provided', () => {
|
|
const geography = new Geography(
|
|
{
|
|
waypoints: [
|
|
{
|
|
lat: 49.440041,
|
|
lon: 1.093912,
|
|
},
|
|
],
|
|
},
|
|
'Europe/Paris',
|
|
person,
|
|
);
|
|
expect(() => geography.init()).toThrow();
|
|
});
|
|
it('should throw an exception if a waypoint has invalid longitude', () => {
|
|
const geography = new Geography(
|
|
{
|
|
waypoints: [
|
|
{
|
|
lat: 49.440041,
|
|
lon: 201.093912,
|
|
},
|
|
{
|
|
lat: 50.630992,
|
|
lon: 3.045432,
|
|
},
|
|
],
|
|
},
|
|
'Europe/Paris',
|
|
person,
|
|
);
|
|
expect(() => geography.init()).toThrow();
|
|
});
|
|
it('should throw an exception if a waypoint has invalid latitude', () => {
|
|
const geography = new Geography(
|
|
{
|
|
waypoints: [
|
|
{
|
|
lat: 49.440041,
|
|
lon: 1.093912,
|
|
},
|
|
{
|
|
lat: 250.630992,
|
|
lon: 3.045432,
|
|
},
|
|
],
|
|
},
|
|
'Europe/Paris',
|
|
person,
|
|
);
|
|
expect(() => geography.init()).toThrow();
|
|
});
|
|
});
|
|
|
|
describe('create route', () => {
|
|
it('should create routes as driver and passenger', async () => {
|
|
const geography = new Geography(
|
|
{
|
|
waypoints: [
|
|
{
|
|
lat: 49.440041,
|
|
lon: 1.093912,
|
|
},
|
|
{
|
|
lat: 50.630992,
|
|
lon: 3.045432,
|
|
},
|
|
],
|
|
},
|
|
'Europe/Paris',
|
|
person,
|
|
);
|
|
geography.init();
|
|
await geography.createRoutes(
|
|
[Role.DRIVER, Role.PASSENGER],
|
|
mockGeorouter,
|
|
);
|
|
expect(geography.driverRoute.waypoints.length).toBe(2);
|
|
expect(geography.passengerRoute.waypoints.length).toBe(2);
|
|
});
|
|
|
|
it('should create routes as driver and passenger with 3 waypoints', async () => {
|
|
const geography = new Geography(
|
|
{
|
|
waypoints: [
|
|
{
|
|
lat: 49.440041,
|
|
lon: 1.093912,
|
|
},
|
|
{
|
|
lat: 49.781215,
|
|
lon: 2.198475,
|
|
},
|
|
{
|
|
lat: 50.630992,
|
|
lon: 3.045432,
|
|
},
|
|
],
|
|
},
|
|
'Europe/Paris',
|
|
person,
|
|
);
|
|
geography.init();
|
|
await geography.createRoutes(
|
|
[Role.DRIVER, Role.PASSENGER],
|
|
mockGeorouter,
|
|
);
|
|
expect(geography.driverRoute.waypoints.length).toBe(3);
|
|
expect(geography.passengerRoute.waypoints.length).toBe(2);
|
|
});
|
|
|
|
it('should create routes as driver', async () => {
|
|
const geography = new Geography(
|
|
{
|
|
waypoints: [
|
|
{
|
|
lat: 49.440041,
|
|
lon: 1.093912,
|
|
},
|
|
{
|
|
lat: 50.630992,
|
|
lon: 3.045432,
|
|
},
|
|
],
|
|
},
|
|
'Europe/Paris',
|
|
person,
|
|
);
|
|
geography.init();
|
|
await geography.createRoutes([Role.DRIVER], mockGeorouter);
|
|
expect(geography.driverRoute.waypoints.length).toBe(2);
|
|
expect(geography.passengerRoute).toBeUndefined();
|
|
});
|
|
|
|
it('should create routes as passenger', async () => {
|
|
const geography = new Geography(
|
|
{
|
|
waypoints: [
|
|
{
|
|
lat: 49.440041,
|
|
lon: 1.093912,
|
|
},
|
|
{
|
|
lat: 50.630992,
|
|
lon: 3.045432,
|
|
},
|
|
],
|
|
},
|
|
'Europe/Paris',
|
|
person,
|
|
);
|
|
geography.init();
|
|
await geography.createRoutes([Role.PASSENGER], mockGeorouter);
|
|
expect(geography.passengerRoute.waypoints.length).toBe(2);
|
|
expect(geography.driverRoute).toBeUndefined();
|
|
});
|
|
});
|
|
});
|