From c6e2fe553aa882dc49adfcc8dd420fc9b51ae62d Mon Sep 17 00:00:00 2001 From: SharglutDev Date: Thu, 16 Jan 2025 17:09:13 +0100 Subject: [PATCH] front: fix stdcm consist length and mass minimum values The sum of the rolling stock and towed rolling stock mass was previously rounded down (instead of up) resulting to some invalid values send to the form. If a total mass was 80.7t, the user was allowed to send 80t. Signed-off-by: SharglutDev --- .../applications/stdcm/components/StdcmForm/StdcmConsist.tsx | 4 ++-- front/src/applications/stdcm/hooks/useStdcmConsist.ts | 4 ++-- front/src/applications/stdcm/utils/consistValidation.ts | 4 ++-- front/tests/006-stdcm.spec.ts | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/front/src/applications/stdcm/components/StdcmForm/StdcmConsist.tsx b/front/src/applications/stdcm/components/StdcmForm/StdcmConsist.tsx index a2fc13ddb1c..6d90b6a33b7 100644 --- a/front/src/applications/stdcm/components/StdcmForm/StdcmConsist.tsx +++ b/front/src/applications/stdcm/components/StdcmForm/StdcmConsist.tsx @@ -144,7 +144,7 @@ const StdcmConsist = ({ isDebugMode, consistErrors = {}, disabled = false }: Std status: 'error', tooltip: 'left', message: t(consistErrors.totalMass, { - low: Math.floor( + low: Math.ceil( kgToT((rollingStock?.mass ?? 0) + (towedRollingStock?.mass ?? 0)) ), high: CONSIST_TOTAL_MASS_MAX, @@ -168,7 +168,7 @@ const StdcmConsist = ({ isDebugMode, consistErrors = {}, disabled = false }: Std status: 'error', tooltip: 'left', message: t(consistErrors.totalLength, { - low: Math.floor((rollingStock?.length ?? 0) + (towedRollingStock?.length ?? 0)), + low: Math.ceil((rollingStock?.length ?? 0) + (towedRollingStock?.length ?? 0)), high: CONSIST_TOTAL_LENGTH_MAX, }), } diff --git a/front/src/applications/stdcm/hooks/useStdcmConsist.ts b/front/src/applications/stdcm/hooks/useStdcmConsist.ts index 5d35f211f3a..f0c01ea8b62 100644 --- a/front/src/applications/stdcm/hooks/useStdcmConsist.ts +++ b/front/src/applications/stdcm/hooks/useStdcmConsist.ts @@ -51,12 +51,12 @@ const useStdcmConsist = () => { maxSpeedTag?: string | null ) => { if (!totalMassChanged) { - const consistMass = Math.floor(kgToT((rollingStock?.mass ?? 0) + (towed?.mass ?? 0))); + const consistMass = Math.ceil(kgToT((rollingStock?.mass ?? 0) + (towed?.mass ?? 0))); dispatch(updateTotalMass(consistMass > 0 ? consistMass : undefined)); } if (!totalLengthChanged) { - const consistLength = Math.floor((rollingStock?.length ?? 0) + (towed?.length ?? 0)); + const consistLength = Math.ceil((rollingStock?.length ?? 0) + (towed?.length ?? 0)); dispatch(updateTotalLength(consistLength > 0 ? consistLength : undefined)); } diff --git a/front/src/applications/stdcm/utils/consistValidation.ts b/front/src/applications/stdcm/utils/consistValidation.ts index 711986698d7..3926f99d7f2 100644 --- a/front/src/applications/stdcm/utils/consistValidation.ts +++ b/front/src/applications/stdcm/utils/consistValidation.ts @@ -22,7 +22,7 @@ export const validateTotalMass = ({ } const tractionMassInTons = kgToT(tractionEngineMass); - const consistMassInTons = Math.floor(kgToT(tractionEngineMass + towedMass)); + const consistMassInTons = Math.ceil(kgToT(tractionEngineMass + towedMass)); const massLimit = towedMass ? consistMassInTons : tractionMassInTons; if (totalMass < massLimit || totalMass > CONSIST_TOTAL_MASS_MAX) { @@ -49,7 +49,7 @@ export const validateTotalLength = ({ return 'consist.errors.totalLength.negative'; } - const consistLength = Math.floor(tractionEngineLength + towedLength); + const consistLength = Math.ceil(tractionEngineLength + towedLength); if (totalLength < consistLength || totalLength > CONSIST_TOTAL_LENGTH_MAX) { return 'consist.errors.totalLength.range'; diff --git a/front/tests/006-stdcm.spec.ts b/front/tests/006-stdcm.spec.ts index a1f60dff63e..10f69f0e1a0 100644 --- a/front/tests/006-stdcm.spec.ts +++ b/front/tests/006-stdcm.spec.ts @@ -35,7 +35,7 @@ test.describe('Verify train schedule elements and filters', () => { }; const fastRollingStockPrefilledValues = { tonnage: '190', - length: '45', + length: '46', maxSpeed: '220', }; const towedRollingStockPrefilledValues = {