Skip to content

Commit

Permalink
front: fix schedule table midnight stop input
Browse files Browse the repository at this point in the history
Signed-off-by: Theo Macron <theo.macron0315@gmail.com>
  • Loading branch information
Akctarus committed Oct 2, 2024
1 parent a09a974 commit 6754f98
Show file tree
Hide file tree
Showing 2 changed files with 252 additions and 7 deletions.
241 changes: 241 additions & 0 deletions front/src/modules/timesStops/helpers/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,144 @@ describe('updateDaySinceDeparture', () => {
];
expect(result).toEqual(expected);
});
it('should handle exactly midnight', () => {
const pathWaypointRows = [
{
opId: 'd9c92cb4',
name: 'Ge',
uic: 86,
ch: 'BV',
arrival: { time: '23:50:00' },
},
{
opId: 'd9b38600',
name: 'Ge',
uic: 86,
ch: 'BX',
arrival: { time: '00:00:00' },
},
] as TimesStopsInputRow[];
const startTime = '2024-08-13T23:50:00';
const result = updateDaySinceDeparture(pathWaypointRows, startTime, {
keepFirstIndexArrival: true,
});
const expected = [
{
opId: 'd9c92cb4',
name: 'Ge',
uic: 86,
ch: 'BV',
arrival: { time: '23:50:00', daySinceDeparture: 0 },
departure: undefined,
},
{
opId: 'd9b38600',
name: 'Ge',
uic: 86,
ch: 'BX',
arrival: { time: '00:00:00', daySinceDeparture: 1, dayDisplayed: true },
departure: undefined,
},
];
expect(result).toEqual(expected);
});
it('should handle departure exactly at midnight', () => {
const pathWaypointRows = [
{
opId: 'd9a382bc',
name: 'St',
uic: 75,
ch: 'BV',
arrival: { time: '00:00:00' },
},
{
opId: 'd9b38600',
name: 'Ge',
uic: 86,
ch: 'BX',
arrival: { time: '01:00:00' },
},
] as TimesStopsInputRow[];
const startTime = '2024-08-13T00:00:00';
const result = updateDaySinceDeparture(pathWaypointRows, startTime, {
keepFirstIndexArrival: true,
});
const expected = [
{
opId: 'd9a382bc',
name: 'St',
uic: 75,
ch: 'BV',
arrival: { time: '00:00:00', daySinceDeparture: 1, dayDisplayed: true },
departure: undefined,
},
{
opId: 'd9b38600',
name: 'Ge',
uic: 86,
ch: 'BX',
arrival: { time: '01:00:00', daySinceDeparture: 1 },
departure: undefined,
},
];
expect(result).toEqual(expected);
});
it('should handle a waypoint exactly at midnight in the middle', () => {
const pathWaypointRows = [
{
opId: 'd9a382bc',
name: 'St',
uic: 75,
ch: 'BV',
arrival: { time: '23:45:00' },
},
{
opId: 'd9b38600',
name: 'Ge',
uic: 86,
ch: 'BX',
arrival: { time: '00:00:00' },
},
{
opId: 'd9a382bd',
name: 'Fr',
uic: 87,
ch: 'BY',
arrival: { time: '01:30:00' },
},
] as TimesStopsInputRow[];
const startTime = '2024-08-13T23:45:00';
const result = updateDaySinceDeparture(pathWaypointRows, startTime, {
keepFirstIndexArrival: true,
});
const expected = [
{
opId: 'd9a382bc',
name: 'St',
uic: 75,
ch: 'BV',
arrival: { time: '23:45:00', daySinceDeparture: 0 },
departure: undefined,
},
{
opId: 'd9b38600',
name: 'Ge',
uic: 86,
ch: 'BX',
arrival: { time: '00:00:00', daySinceDeparture: 1, dayDisplayed: true },
departure: undefined,
},
{
opId: 'd9a382bd',
name: 'Fr',
uic: 87,
ch: 'BY',
arrival: { time: '01:30:00', daySinceDeparture: 1 },
departure: undefined,
},
];
expect(result).toEqual(expected);
});
it('should add display flag for the first time in the new day', () => {
const TimesStopsInputRows = [
{
Expand Down Expand Up @@ -566,6 +704,109 @@ describe('updateDaySinceDeparture', () => {
];
expect(result).toEqual(expected);
});
it('should handle a three-day span with two midnights', () => {
const pathWaypointRows = [
{
opId: 'd9a382bc',
name: 'St',
uic: 75,
ch: 'BV',
arrival: { time: '23:45:00' },
},
{
opId: 'd9b38600',
name: 'Ge',
uic: 86,
ch: 'BX',
arrival: { time: '00:00:00' },
},
{
opId: 'd9a382bd',
name: 'Fr',
uic: 87,
ch: 'BY',
arrival: { time: '01:30:00' },
},
{
opId: 'd9a382be',
name: 'Lm',
uic: 88,
ch: 'BZ',
arrival: { time: '23:50:00' },
},
{
opId: 'd9a382bf',
name: 'Nc',
uic: 89,
ch: 'CA',
arrival: { time: '00:00:00' },
},
{
opId: 'd9a382c0',
name: 'Po',
uic: 90,
ch: 'CB',
arrival: { time: '03:00:00' },
},
] as TimesStopsInputRow[];

const startTime = '2024-08-13T23:45:00';
const result = updateDaySinceDeparture(pathWaypointRows, startTime, {
keepFirstIndexArrival: true,
});

const expected = [
{
opId: 'd9a382bc',
name: 'St',
uic: 75,
ch: 'BV',
arrival: { time: '23:45:00', daySinceDeparture: 0 },
departure: undefined,
},
{
opId: 'd9b38600',
name: 'Ge',
uic: 86,
ch: 'BX',
arrival: { time: '00:00:00', daySinceDeparture: 1, dayDisplayed: true },
departure: undefined,
},
{
opId: 'd9a382bd',
name: 'Fr',
uic: 87,
ch: 'BY',
arrival: { time: '01:30:00', daySinceDeparture: 1 },
departure: undefined,
},
{
opId: 'd9a382be',
name: 'Lm',
uic: 88,
ch: 'BZ',
arrival: { time: '23:50:00', daySinceDeparture: 1 },
departure: undefined,
},
{
opId: 'd9a382bf',
name: 'Nc',
uic: 89,
ch: 'CA',
arrival: { time: '00:00:00', daySinceDeparture: 2, dayDisplayed: true },
departure: undefined,
},
{
opId: 'd9a382c0',
name: 'Po',
uic: 90,
ch: 'CB',
arrival: { time: '03:00:00', daySinceDeparture: 2 },
departure: undefined,
},
];
expect(result).toEqual(expected);
});
});
});

Expand Down
18 changes: 11 additions & 7 deletions front/src/modules/timesStops/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,13 @@ export function updateDaySinceDeparture(

return pathWaypointRows.map((pathWaypoint, index) => {
const { arrival, stopFor } = pathWaypoint;

const arrivalInSeconds = arrival?.time ? time2sec(arrival.time) : null;
let formattedArrival: TimeExtraDays | undefined;
if (arrivalInSeconds) {
if (arrivalInSeconds < previousTime) {

if (arrivalInSeconds !== null) {
const isMidnight = arrival?.time === '00:00:00';

if (arrivalInSeconds < previousTime || isMidnight) {
currentDaySinceDeparture += 1;
formattedArrival = {
time: arrival!.time,
Expand All @@ -228,14 +230,17 @@ export function updateDaySinceDeparture(
daySinceDeparture: currentDaySinceDeparture,
};
}
previousTime = arrivalInSeconds;
previousTime = isMidnight ? 0 : arrivalInSeconds;
}

let formattedDeparture: TimeExtraDays | undefined;
if (stopFor && arrivalInSeconds) {

if (stopFor && arrivalInSeconds !== null) {
const departureInSeconds = (arrivalInSeconds + Number(stopFor)) % SECONDS_IN_A_DAY;
const isAfterMidnight = departureInSeconds < previousTime;
if (isAfterMidnight) {
const isDepartureMidnight = departureInSeconds === 0;

if (isAfterMidnight || isDepartureMidnight) {
currentDaySinceDeparture += 1;
formattedDeparture = {
time: secToHoursString(departureInSeconds, { withSeconds: true }),
Expand All @@ -250,7 +255,6 @@ export function updateDaySinceDeparture(
}
previousTime = departureInSeconds;
}

return {
...pathWaypoint,
arrival: keepFirstIndexArrival || index > 0 ? formattedArrival : undefined,
Expand Down

0 comments on commit 6754f98

Please sign in to comment.