Skip to content

Commit 615f035

Browse files
committed
front: update train count check
1 parent 19912ba commit 615f035

File tree

8 files changed

+56
-55
lines changed

8 files changed

+56
-55
lines changed

front/public/locales/en/operationalStudies/scenario.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"toggleTimetable": "Toggle the timetable",
7272
"trainCount_one": "1/{{ totalCount }} trains",
7373
"trainCount_other": "{{ count }}/{{ totalCount }} trains",
74-
"trainCount_zero": "0/{{ totalCount }} train",
74+
"trainCount_zero": "0/{{ totalCount }} trains",
7575
"conflictsCount_one": "1 conflict",
7676
"conflictsCount_other": "{{count}} conflicts",
7777
"conflictsCount_zero": "No conflict",

front/public/locales/fr/operationalStudies/scenario.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"toggleTimetable": "Basculer l'affichage de la grille horaires",
7171
"trainCount_one": "1/{{ totalCount }} trains",
7272
"trainCount_other": "{{ count }}/{{ totalCount }} trains",
73-
"trainCount_zero": "0/{{ totalCount }} train",
73+
"trainCount_zero": "0/{{ totalCount }} trains",
7474
"conflictsCount_one": "1 conflit",
7575
"conflictsCount_other": "{{count}} conflits",
7676
"conflictsCount_zero": "Aucun conflit",

front/tests/008-train-schedule.spec.ts

+20-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { test } from '@playwright/test';
22

3-
import type { Scenario, Study, Project } from 'common/api/osrdEditoastApi';
3+
import type { ScenarioV2, Study, Project } from 'common/api/osrdEditoastApi';
44

55
import HomePage from './pages/home-page-model';
66
import OperationalStudiesTimetablePage from './pages/op-timetable-page-model';
@@ -12,7 +12,7 @@ import { postSimulation, sendTrainSchedules } from './utils/trainSchedule';
1212
let selectedLanguage: string;
1313
let project: Project;
1414
let study: Study;
15-
let scenario: Scenario;
15+
let scenario: ScenarioV2;
1616

1717
const trainSchedulesJson = readJsonFile('./tests/assets/trainSchedule/train_schedules.json');
1818

@@ -47,10 +47,10 @@ test.describe('Verifying that all elements in the train schedule are loaded corr
4747
await scenarioPage.checkInfraLoaded();
4848

4949
// Verify the train count and various elements on the timetable page
50-
await opTimetablePage.verifyTrainCount(20, selectedLanguage);
50+
await opTimetablePage.verifyTrainCount(20, 20);
5151
await opTimetablePage.verifyInvalidTrainsMessageVisibility(selectedLanguage);
5252
await opTimetablePage.checkSelectedTimetableTrain();
53-
await opTimetablePage.filterValidityAndVerifyTrainCount(selectedLanguage, 'Valid', 16);
53+
await opTimetablePage.filterValidityAndVerifyTrainCount(selectedLanguage, 'Valid', 16, 20);
5454
await opTimetablePage.verifyEachTrainSimulation();
5555
});
5656

@@ -63,15 +63,15 @@ test.describe('Verifying that all elements in the train schedule are loaded corr
6363
await scenarioPage.checkInfraLoaded();
6464

6565
// Verify the train count and apply various filters
66-
await opTimetablePage.verifyTrainCount(20, selectedLanguage);
67-
await opTimetablePage.filterValidityAndVerifyTrainCount(selectedLanguage, 'Invalid', 4);
68-
await opTimetablePage.filterValidityAndVerifyTrainCount(selectedLanguage, 'All', 20);
69-
await opTimetablePage.filterHonoredAndVerifyTrainCount(selectedLanguage, 'Honored', 12);
70-
await opTimetablePage.filterValidityAndVerifyTrainCount(selectedLanguage, 'Valid', 12);
71-
await opTimetablePage.filterHonoredAndVerifyTrainCount(selectedLanguage, 'Not honored', 4);
72-
await opTimetablePage.filterValidityAndVerifyTrainCount(selectedLanguage, 'Invalid', 0);
73-
await opTimetablePage.filterHonoredAndVerifyTrainCount(selectedLanguage, 'All', 4);
74-
await opTimetablePage.filterValidityAndVerifyTrainCount(selectedLanguage, 'All', 20);
66+
await opTimetablePage.verifyTrainCount(20, 20);
67+
await opTimetablePage.filterValidityAndVerifyTrainCount(selectedLanguage, 'Invalid', 4, 20);
68+
await opTimetablePage.filterValidityAndVerifyTrainCount(selectedLanguage, 'All', 20, 20);
69+
await opTimetablePage.filterHonoredAndVerifyTrainCount(selectedLanguage, 'Honored', 12, 20);
70+
await opTimetablePage.filterValidityAndVerifyTrainCount(selectedLanguage, 'Valid', 12, 20);
71+
await opTimetablePage.filterHonoredAndVerifyTrainCount(selectedLanguage, 'Not honored', 4, 20);
72+
await opTimetablePage.filterValidityAndVerifyTrainCount(selectedLanguage, 'Invalid', 0, 20);
73+
await opTimetablePage.filterHonoredAndVerifyTrainCount(selectedLanguage, 'All', 4, 20);
74+
await opTimetablePage.filterValidityAndVerifyTrainCount(selectedLanguage, 'All', 20, 20);
7575

7676
// Define the composition filters and verify each filter
7777
const compositionFilters = [
@@ -81,13 +81,15 @@ test.describe('Verifying that all elements in the train schedule are loaded corr
8181
{ code: 'Without code', count: 9 },
8282
];
8383

84-
await compositionFilters.reduce(async (promise, filter) => {
85-
await promise;
86-
return opTimetablePage.clickCodeCompoTrainFilterButton(
84+
/* eslint-disable no-restricted-syntax, no-await-in-loop */
85+
for (const filter of compositionFilters) {
86+
await opTimetablePage.clickCodeCompoTrainFilterButton(
8787
selectedLanguage,
8888
filter.code,
89-
filter.count
89+
filter.count,
90+
20
9091
);
91-
}, Promise.resolve());
92+
}
93+
/* eslint-enable no-restricted-syntax, no-await-in-loop */
9294
});
9395
});

front/tests/pages/op-timetable-page-model.ts

+12-19
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ class OperationalStudiesTimetablePage {
115115
async clickCodeCompoTrainFilterButton(
116116
selectedLanguage: string,
117117
filterTranslation: string,
118-
expectedTrainCount: number
118+
expectedTrainCount: number,
119+
totalTrainCount: number
119120
): Promise<void> {
120121
await this.timetableFilterButton.click();
121122
const translations = selectedLanguage === 'English' ? enTranslations : frTranslations;
@@ -129,31 +130,24 @@ class OperationalStudiesTimetablePage {
129130
: this.page.getByRole('button', { name: filterTranslation });
130131

131132
await filterButtonLocator.click();
132-
await this.verifyTrainCount(expectedTrainCount, selectedLanguage);
133+
await this.verifyTrainCount(expectedTrainCount, totalTrainCount);
133134
await filterButtonLocator.click();
134135
await this.timetableFilterButton.click();
135136
}
136137

137138
// Verifies that the imported train number is correct
138-
async verifyTrainCount(trainCount: number, selectedLanguage: string): Promise<void> {
139+
async verifyTrainCount(trainCount: number, totalTrainCount: number): Promise<void> {
139140
await this.page.waitForLoadState('networkidle');
140-
const translations = selectedLanguage === 'English' ? enTranslations : frTranslations;
141141
const trainCountText = await this.trainCountText.innerText();
142-
143-
const translationMapping: Record<number, string> = {
144-
0: translations.trainCount_zero,
145-
1: translations.trainCount_one,
146-
};
147-
const expectedText = translationMapping[trainCount] || `${trainCount} trains`;
148-
149-
expect(trainCountText).toEqual(expectedText);
142+
expect(trainCountText).toEqual(`${trainCount}/${totalTrainCount} trains`);
150143
}
151144

152145
// Filter trains validity and verify their count
153146
async filterValidityAndVerifyTrainCount(
154147
selectedLanguage: string,
155148
validityFilter: 'Valid' | 'Invalid' | 'All',
156-
expectedTrainCount: number
149+
expectedTrainCount: number,
150+
totalTrainCount: number
157151
): Promise<void> {
158152
const translations = selectedLanguage === 'English' ? enTranslations : frTranslations;
159153
await this.timetableFilterButton.click();
@@ -166,14 +160,15 @@ class OperationalStudiesTimetablePage {
166160

167161
await this.clickValidityTrainFilterButton(validityFilters[validityFilter]);
168162
await this.timetableFilterButton.click();
169-
await this.verifyTrainCount(expectedTrainCount, selectedLanguage);
163+
await this.verifyTrainCount(expectedTrainCount, totalTrainCount);
170164
}
171165

172166
// Filter the honored trains and verify their count
173167
async filterHonoredAndVerifyTrainCount(
174168
selectedLanguage: string,
175169
honoredFilter: 'Honored' | 'Not honored' | 'All',
176-
expectedTrainCount: number
170+
expectedTrainCount: number,
171+
totalTrainCount: number
177172
): Promise<void> {
178173
const translations = selectedLanguage === 'English' ? enTranslations : frTranslations;
179174
await this.timetableFilterButton.click();
@@ -186,24 +181,22 @@ class OperationalStudiesTimetablePage {
186181

187182
await this.clickHonoredTrainFilterButton(honoredFilters[honoredFilter]);
188183
await this.timetableFilterButton.click();
189-
await this.verifyTrainCount(expectedTrainCount, selectedLanguage);
184+
await this.verifyTrainCount(expectedTrainCount, totalTrainCount);
190185
}
191186

192187
// Iterate over each train element and verify the visibility of simulation results
193188
async verifyEachTrainSimulation(): Promise<void> {
194189
const trainCount = await this.timetableTrains.count();
195-
let currentTrainIndex = 0;
196190

197191
/* eslint-disable no-await-in-loop */
198-
while (currentTrainIndex < trainCount) {
192+
for (let currentTrainIndex = 0; currentTrainIndex < trainCount; currentTrainIndex += 1) {
199193
await this.page.waitForLoadState('networkidle');
200194
await this.waitForSimulationResults();
201195
const trainButton = OperationalStudiesTimetablePage.getTrainButton(
202196
this.timetableTrains.nth(currentTrainIndex)
203197
);
204198
await trainButton.click();
205199
await this.verifySimulationResultsVisibility();
206-
currentTrainIndex += 1;
207200
}
208201
/* eslint-enable no-await-in-loop */
209202
}

front/tests/utils/api-setup.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { request } from '@playwright/test';
22

3-
import type { Project, Study, RollingStock, Infra, Scenario } from 'common/api/osrdEditoastApi';
3+
import type { Project, Study, RollingStock, Infra, ScenarioV2 } from 'common/api/osrdEditoastApi';
44

55
import { handleApiResponse } from './index';
66

@@ -29,7 +29,7 @@ export const postApiRequest = async <T>(
2929
const apiContext = await getApiContext();
3030
const response = await apiContext.post(url, { data, params });
3131
if (errorMessage) {
32-
await handleApiResponse(response, errorMessage);
32+
handleApiResponse(response, errorMessage);
3333
}
3434
return response.json();
3535
};
@@ -66,7 +66,7 @@ export const getScenario = async (projectId: number, studyId: number) => {
6666
const { results } = await getApiRequest(
6767
`/api/v2/projects/${projectId}/studies/${studyId}/scenarios/`
6868
);
69-
const scenario = findOneInResults(results, 'scenario_test_e2e') as Scenario;
69+
const scenario = findOneInResults(results, 'scenario_test_e2e') as ScenarioV2;
7070
return scenario;
7171
};
7272
export const getRollingStock = async () => {

front/tests/utils/index.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'fs';
22

3-
import { type APIResponse, type Page, expect } from '@playwright/test';
3+
import { type APIResponse, type Page, expect, request } from '@playwright/test';
44
import { v4 as uuidv4 } from 'uuid';
55

66
import type { Project, ScenarioV2, Study, RollingStock, Infra } from 'common/api/osrdEditoastApi';
@@ -141,10 +141,7 @@ export async function extractNumberFromString(input: string): Promise<number> {
141141
export const readJsonFile = (path: string) => JSON.parse(fs.readFileSync(path, 'utf8'));
142142

143143
// Helper function to handle API error responses
144-
export async function handleApiResponse(
145-
response: APIResponse,
146-
errorMessage: string
147-
): Promise<void> {
144+
export function handleApiResponse(response: APIResponse, errorMessage: string) {
148145
if (!response.ok()) {
149146
throw new Error(`${errorMessage}: ${response.status()} ${response.statusText()}`);
150147
}

front/tests/utils/scenario.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { v4 as uuidv4 } from 'uuid';
22

3-
import type { Infra, Project, Scenario, Study, TimetableResult } from 'common/api/osrdEditoastApi';
3+
import type {
4+
Infra,
5+
Project,
6+
ScenarioV2,
7+
Study,
8+
TimetableResult,
9+
} from 'common/api/osrdEditoastApi';
410

511
import { getInfra, getProject, getStudy, postApiRequest } from './api-setup';
612
import scenarioData from '../assets/operationStudies/scenario.json';
@@ -10,7 +16,7 @@ interface SetupResult {
1016
smallInfra: Infra;
1117
project: Project;
1218
study: Study;
13-
scenario: Scenario;
19+
scenario: ScenarioV2;
1420
timetableResult: TimetableResult;
1521
}
1622

front/tests/utils/trainSchedule.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { APIResponse, APIRequestContext } from '@playwright/test';
22

3-
import type { TrainSchedule } from 'common/api/osrdEditoastApi';
3+
import type { TrainScheduleResult } from 'common/api/osrdEditoastApi';
44

55
import { getApiContext, postApiRequest } from './api-setup';
66
import { handleApiResponse } from './index';
@@ -9,7 +9,7 @@ import { handleApiResponse } from './index';
99
export async function sendTrainSchedules(
1010
timetableId: number,
1111
body: object
12-
): Promise<TrainSchedule[]> {
12+
): Promise<TrainScheduleResult[]> {
1313
const apiContext: APIRequestContext = await getApiContext();
1414
const trainSchedulesResponse: APIResponse = await apiContext.post(
1515
`/api/v2/timetable/${timetableId}/train_schedule/`,
@@ -20,16 +20,19 @@ export async function sendTrainSchedules(
2020
},
2121
}
2222
);
23-
await handleApiResponse(trainSchedulesResponse, 'Failed to send train schedule');
23+
handleApiResponse(trainSchedulesResponse, 'Failed to send train schedule');
2424
return trainSchedulesResponse.json();
2525
}
2626

2727
// Function to extract train IDs from the train schedules
28-
export function getTrainIds(trainSchedules: TrainSchedule[]) {
29-
return trainSchedules.map((item: TrainSchedule) => item.id);
28+
export function getTrainIds(trainSchedules: TrainScheduleResult[]) {
29+
return trainSchedules.map((item: TrainScheduleResult) => item.id);
3030
}
3131
// Function to post simulation using extracted train IDs
32-
export async function postSimulation(response: TrainSchedule[], infraId: number): Promise<void> {
32+
export async function postSimulation(
33+
response: TrainScheduleResult[],
34+
infraId: number
35+
): Promise<void> {
3336
const trainIds = getTrainIds(response);
3437
if (trainIds.length > 0) {
3538
await postApiRequest(

0 commit comments

Comments
 (0)