Skip to content

Commit

Permalink
Fixed broken profile comparison tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkapok committed Apr 2, 2024
1 parent 3c8bf5c commit 123a90d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 44 deletions.
2 changes: 1 addition & 1 deletion projects/planner/src/app/shared/diff/ComparedWaypoint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WayPoint } from "../models";
import { WayPoint } from '../models';

export class ComparedWaypoint {
public selected = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ import { ReloadDispatcher } from '../reloadDispatcher';
import { WayPointsService } from '../waypoints.service';
import { Segment, StandardGases } from 'scuba-physics';
import { ComparedWaypoint } from './ComparedWaypoint';
import _ from 'lodash';

interface AssertedWayPoint {
runTime: number;
depthA: number | undefined;
durationA: number | undefined;
depthB: number | undefined;
durationB: number | undefined;
}

describe('WayPoints Difference Service', () => {
let sut: ProfileComparatorService;
let schedules: DiveSchedules;
let wayPoints: WayPointsService;
let dispatcher: ReloadDispatcher;

beforeEach(async () => {
await TestBed.configureTestingModule({
Expand All @@ -29,6 +39,7 @@ describe('WayPoints Difference Service', () => {
schedules.add();
sut = TestBed.inject(ProfileComparatorService);
wayPoints = TestBed.inject(WayPointsService);
dispatcher = TestBed.inject(ReloadDispatcher);
setDiveCalculated(0);
setDiveCalculated(1);
});
Expand Down Expand Up @@ -73,10 +84,24 @@ describe('WayPoints Difference Service', () => {
new Segment(10, 0, StandardGases.air, 180)
];

const assertDivesWayPointsCompare = (profileA: Segment[], profileB: Segment[], expected: ComparedWaypoint[])=> {
function toAssert(current: ComparedWaypoint[]): AssertedWayPoint[] {
return _(current).map<AssertedWayPoint>(e => ({
runTime: e.runTime,
depthA: e.depthA,
durationA: e.durationA,
depthB: e.depthB,
durationB: e.durationB
})).toArray().value();
}

const assertDivesWayPointsCompare = (profileA: Segment[], profileB: Segment[], expected: AssertedWayPoint[])=> {
schedules.dives[0].diveResult.wayPoints = wayPoints.calculateWayPoints(profileA);
schedules.dives[1].diveResult.wayPoints = wayPoints.calculateWayPoints(profileB);
expect(sut.difference).toEqual(expected);
// simulate calculation behavior to enforce cache of waypoints
dispatcher.sendInfoCalculated(1);
dispatcher.sendInfoCalculated(2);
const current = toAssert(sut.difference);
expect(current).toEqual(expected);
};

it('Not calculated profile A', () => {
Expand All @@ -90,7 +115,7 @@ describe('WayPoints Difference Service', () => {
});

it('Failed profile A with valid profile B', () => {
schedules.dives[0].diveResult .endFailed();
schedules.dives[0].diveResult.endFailed();
schedules.dives[1].diveResult.wayPoints = wayPoints.calculateWayPoints(segments3_minutes6);
expect(sut.difference).toEqual([]);
});
Expand All @@ -102,76 +127,76 @@ describe('WayPoints Difference Service', () => {
});

it('Identical profiles', () => {
const expected: ComparedWaypoint[] = [
new ComparedWaypoint(60, 20, 60, 20, 60),
new ComparedWaypoint(180, 20, 120, 20, 120),
new ComparedWaypoint(360, 0, 180, 0, 180),
const expected: AssertedWayPoint[] = [
{ runTime: 60, depthA: 20, durationA: 60, depthB: 20, durationB: 60 },
{ runTime: 180, depthA: 20, durationA: 120, depthB: 20, durationB: 120 },
{ runTime: 360, depthA: 0, durationA: 180, depthB: 0, durationB: 180 },
];
assertDivesWayPointsCompare(segments3_minutes6, segments3_minutes6, expected);
});

it('Profile B takes longer', () => {
const expected: ComparedWaypoint[] = [
new ComparedWaypoint(60, 20, 60, 20, 60),
new ComparedWaypoint(180, 20, 120, undefined, undefined),
new ComparedWaypoint( 360, 0, 180, 20, 300),
new ComparedWaypoint( 540, undefined, undefined, 0, 180),
const expected: AssertedWayPoint[] = [
{ runTime: 60, depthA: 20, durationA: 60, depthB: 20, durationB: 60 },
{ runTime: 180, depthA: 20, durationA: 120, depthB: undefined, durationB: undefined },
{ runTime: 360, depthA: 0, durationA: 180, depthB: 20, durationB: 300 },
{ runTime: 540, depthA: undefined, durationA: undefined, depthB: 0, durationB: 180 },
];
assertDivesWayPointsCompare(segments3_minutes6, segments3_minutes9, expected);
});

it('Profile A takes longer', () => {
const expected: ComparedWaypoint[] = [
new ComparedWaypoint(60, 20, 60, 20, 60),
new ComparedWaypoint(180, undefined, undefined, 20, 120),
new ComparedWaypoint(360, 20, 300, 0, 180),
new ComparedWaypoint(540, 0, 180, undefined, undefined),
const expected: AssertedWayPoint[] = [
{ runTime: 60, depthA: 20, durationA: 60, depthB: 20, durationB: 60 },
{ runTime: 180, depthA: undefined, durationA: undefined, depthB: 20, durationB: 120 },
{ runTime: 360, depthA: 20, durationA: 300, depthB: 0, durationB: 180 },
{ runTime: 540, depthA: 0, durationA: 180, depthB: undefined, durationB: undefined },
];
assertDivesWayPointsCompare(segments3_minutes9, segments3_minutes6, expected);
});

it('Profile B has more segments', () => {
const expected: ComparedWaypoint[] = [
new ComparedWaypoint(60, 20, 60, 20, 60),
new ComparedWaypoint(180, 20, 120, 20, 120),
new ComparedWaypoint(360, 0, 180, undefined, undefined),
new ComparedWaypoint(480, undefined, undefined, 10, 300),
new ComparedWaypoint(660, undefined, undefined, 0, 180),
const expected: AssertedWayPoint[] = [
{ runTime: 60, depthA: 20, durationA: 60, depthB: 20, durationB: 60 },
{ runTime: 180, depthA: 20, durationA: 120, depthB: 20, durationB: 120 },
{ runTime: 360, depthA: 0, durationA: 180, depthB: undefined, durationB: undefined },
{ runTime: 480, depthA: undefined, durationA: undefined, depthB: 10, durationB: 300 },
{ runTime: 660, depthA: undefined, durationA: undefined, depthB: 0, durationB: 180 },
];
assertDivesWayPointsCompare(segments3_minutes6, segments4_minutes11, expected);
});

it('Profile A has more segments', () => {
const expected: ComparedWaypoint[] = [
new ComparedWaypoint(60, 20, 60, 20, 60),
new ComparedWaypoint(180, 20, 120, 20, 120),
new ComparedWaypoint(360, undefined, undefined, 0, 180),
new ComparedWaypoint(480, 10, 300, undefined, undefined),
new ComparedWaypoint(660, 0, 180, undefined, undefined),
const expected: AssertedWayPoint[] = [
{ runTime: 60, depthA: 20, durationA: 60, depthB: 20, durationB: 60 },
{ runTime: 180, depthA: 20, durationA: 120, depthB: 20, durationB: 120 },
{ runTime: 360, depthA: undefined, durationA: undefined, depthB: 0, durationB: 180 },
{ runTime: 480, depthA: 10, durationA: 300, depthB: undefined, durationB: undefined },
{ runTime: 660, depthA: 0, durationA: 180, depthB: undefined, durationB: undefined },
];
assertDivesWayPointsCompare(segments4_minutes11, segments3_minutes6, expected);
});

it('Profile B has levels missing in profile A', () => {
const expected: ComparedWaypoint[] = [
new ComparedWaypoint(60, 20, 60, 20, 60),
new ComparedWaypoint(90, undefined, undefined, 15, 30),
new ComparedWaypoint(150, undefined, undefined, 15, 60),
new ComparedWaypoint(180, 20, 120, 20, 30),
new ComparedWaypoint(480, 10, 300, 10, 300),
new ComparedWaypoint(660, 0, 180, 0, 180),
const expected: AssertedWayPoint[] = [
{ runTime: 60, depthA: 20, durationA: 60, depthB: 20, durationB: 60 },
{ runTime: 90, depthA: undefined, durationA: undefined, depthB: 15, durationB: 30 },
{ runTime: 150, depthA: undefined, durationA: undefined, depthB: 15, durationB: 60 },
{ runTime: 180, depthA: 20, durationA: 120, depthB: 20, durationB: 30 },
{ runTime: 480, depthA: 10, durationA: 300, depthB: 10, durationB: 300 },
{ runTime: 660, depthA: 0, durationA: 180, depthB: 0, durationB: 180 },
];
assertDivesWayPointsCompare(segments4_minutes11, segments6_minutes11, expected);
});

it('Profile A has levels missing in profile B', () => {
const expected: ComparedWaypoint[] = [
new ComparedWaypoint(60, 20, 60, 20, 60 ),
new ComparedWaypoint(90, 15, 30, undefined, undefined),
new ComparedWaypoint(150, 15, 60, undefined, undefined),
new ComparedWaypoint(180, 20, 30, 20, 120),
new ComparedWaypoint(480, 10, 300, 10, 300),
new ComparedWaypoint(660, 0, 180, 0, 180),
const expected: AssertedWayPoint[] = [
{ runTime: 60, depthA: 20, durationA: 60, depthB: 20, durationB: 60 },
{ runTime: 90, depthA: 15, durationA: 30, depthB: undefined, durationB: undefined },
{ runTime: 150, depthA: 15, durationA: 60, depthB: undefined, durationB: undefined },
{ runTime: 180, depthA: 20, durationA: 30, depthB: 20, durationB: 120 },
{ runTime: 480, depthA: 10, durationA: 300, depthB: 10, durationB: 300 },
{ runTime: 660, depthA: 0, durationA: 180, depthB: 0, durationB: 180 },
];
assertDivesWayPointsCompare(segments6_minutes11, segments4_minutes11, expected);
});
Expand Down

0 comments on commit 123a90d

Please sign in to comment.