Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Akctarus committed Jul 17, 2024
1 parent 7a7d407 commit 6259641
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 170 deletions.
108 changes: 0 additions & 108 deletions front/src/applications/stdcm/utils/__tests__/sampleData.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ import {
formatCreationDate,
getStopDurationTime,
extractSpeedLimit,
getOperationalPointsWithTimes,
computeStopDepartureTime,
addMinutesToTime,
getStopDurationBetweenTwoPositions,
} from 'applications/stdcm/utils/formatSimulationReportSheet';

import { suggestedOperationalPoints, simulation, departure_time } from './sampleData';

// TODO: When the code will be generated based on stdcm inputs hash, we will need to adapt this test
describe('Utils simulation report sheet functions', () => {
test('should return a formatted string', () => {
const codeNumber = generateCodeNumber();
expect(codeNumber).toMatch(/^\d{2}\d{2}-\d{3}-\d{3}$/);
});

// TODO DROP STDCM V1
test('should format the date correctly', () => {
const departure_time = '2024-07-08T10:20:30Z';
const formattedDate = formatCreationDate(departure_time);
expect(formattedDate).toEqual({
day: '08',
Expand All @@ -31,50 +34,28 @@ describe('Utils simulation report sheet functions', () => {
});

test('should extract the speed limit correctly', () => {
const speedLimitByTag = 'Some tag - 50';
expect(extractSpeedLimit(speedLimitByTag)).toBe('50');
const speedLimitByTag1 = 'Voyageurs 100 - MA100';
const speedLimitByTag2 = 'Voyageurs';
expect(extractSpeedLimit(speedLimitByTag1)).toBe('MA100');
expect(extractSpeedLimit(speedLimitByTag2)).toBe('Voyageurs');
});

test('should compute stop departure time correctly', () => {
expect(computeStopDepartureTime('10:30', '15:00')).toBe('10:45');
expect(computeStopDepartureTime('23:59', '01:00')).toBe('00:00');
expect(computeStopDepartureTime('23:59', '02:00')).toBe('00:01');
});

test('should add minutes to time correctly', () => {
expect(addMinutesToTime(10, 30, 15)).toBe('10:45');
expect(addMinutesToTime(23, 59, 1)).toBe('00:00');
expect(addMinutesToTime(23, 45, 30)).toBe('00:15');
});

test('should return formatted operational points with times', () => {
const result = getOperationalPointsWithTimes(
suggestedOperationalPoints,
simulation,
departure_time
);
expect(result).toEqual([
{
opId: 'op1',
positionOnPath: 50,
time: '10:20',
name: 'Point 1',
ch: 'Ch1',
duration: 0,
departureTime: '2024-07-08T10:20:30Z',
stopEndTime: '10:20',
trackName: 'Track 1',
},
{
opId: 'op2',
positionOnPath: 100,
time: '10:21',
name: 'Point 2',
ch: 'Ch2',
duration: 120,
departureTime: '2024-07-08T10:20:30Z',
stopEndTime: '10:23',
trackName: 'Track 2',
},
{
opId: 'op3',
positionOnPath: 150,
time: '10:24',
name: 'Point 3',
ch: 'Ch3',
duration: 0,
departureTime: '2024-07-08T10:20:30Z',
stopEndTime: '10:24',
trackName: 'Track 3',
},
]);
test('should return stop duration correctly', () => {
const trainPositions = [1, 2, 2, 3];
const trainTimes = [10000, 120000, 180000];
expect(getStopDurationBetweenTwoPositions(2, trainPositions, trainTimes)).toBe(60000);
expect(getStopDurationBetweenTwoPositions(1, trainPositions, trainTimes)).toBeNull();
});
});
42 changes: 26 additions & 16 deletions front/src/applications/stdcm/utils/formatSimulationReportSheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function generateCodeNumber(): string {
return `${month}${year}-${randomPart1}-${randomPart2}`;
}

// TODO: This function is only used for V1, so it must be deleted when V1 is abandoned.
// TODO DROP STDCM V1
export function formatCreationDate(date: string) {
const creationDate = new Date(date);
const day = creationDate.getDate();
Expand Down Expand Up @@ -69,51 +69,61 @@ function timeStringToSeconds(time: string): number {
/**
* Based on a stop arrival time and a stop duration, calculate the departure time on this stop
*/
function computeStopDepartureTime(hhmm: string, mmss: string): string {
export function computeStopDepartureTime(hhmm: string, mmss: string): string {
const [hh, mm] = hhmm.split(':').map(Number);
const totalSeconds1 = hh * 3600 + mm * 60;
const totalSeconds2 = timeStringToSeconds(mmss);

const totalSeconds = totalSeconds1 + totalSeconds2;
const hours = Math.floor(totalSeconds / 3600);
const hours = Math.floor(totalSeconds / 3600) % 24;
const minutes = Math.floor((totalSeconds % 3600) / 60);

return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}`;
}

// Function to add minutes to the departure time
const addMinutesToTime = (baseHour: number, baseMinute: number, minutesToAdd: number): string => {
export function addMinutesToTime(
baseHour: number,
baseMinute: number,
minutesToAdd: number
): string {
const totalMinutes = baseHour * 60 + baseMinute + minutesToAdd;
const finalHour = Math.floor(totalMinutes / 60) % 24;
const finalMinutes = totalMinutes % 60;
return `${String(finalHour).padStart(2, '0')}:${String(finalMinutes).padStart(2, '0')}`;
};
}

const getTimeAtPosition = (
function getTimeAtPosition(
trainPosition: number,
trainPositions: number[],
trainTimes: number[],
trainDepartureHour: number,
trainDepartureMinute: number
): string => {
): string {
const index = trainPositions.findIndex((pos) => pos >= trainPosition);
const timeInMillis = trainTimes[index];
const timeInMinutes = Math.floor(timeInMillis / 60000);
return addMinutesToTime(trainDepartureHour, trainDepartureMinute, timeInMinutes);
};
}

const getStopDurationBetweenTwoPositions = (
/**
* @param position format: Distance from the beginning of the path in mm
* @param trainPositions format: List of positions of a train in mm.
* @param trainTimes format: List of times in milliseconds corresponding to the positions in trainPositions.
* @returns The duration in milliseconds between the first and last occurrence of the position in the trainPositions array
*/
export function getStopDurationBetweenTwoPositions(
position: number,
trainPositions: number[],
trainTimes: number[]
): number | null => {
const firstIndex = trainPositions.indexOf(position);
const lastIndex = trainPositions.lastIndexOf(position);
positionsList: number[],
timesList: number[]
): number | null {
const firstIndex = positionsList.indexOf(position);
const lastIndex = positionsList.lastIndexOf(position);
if (firstIndex !== -1 && lastIndex !== -1 && firstIndex !== lastIndex) {
return trainTimes[lastIndex] - trainTimes[firstIndex];
return timesList[lastIndex] - timesList[firstIndex];
}
return null;
};
}

export function getOperationalPointsWithTimes(
operationalPoints: SuggestedOP[],
Expand Down

0 comments on commit 6259641

Please sign in to comment.