Skip to content

Commit

Permalink
[FEATURE] Prendre en compte la langue depuis la payload pour le simua…
Browse files Browse the repository at this point in the history
…ltor flash (PIX-15218).

 #10650
  • Loading branch information
pix-service-auto-merge authored Nov 26, 2024
2 parents 241562a + cdde2e7 commit 2104ad0
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import _ from 'lodash';

import { pickChallengeService } from '../../../evaluation/domain/services/pick-challenge-service.js';
import { random } from '../../../shared/infrastructure/utils/random.js';
import { extractLocaleFromRequest } from '../../../shared/infrastructure/utils/request-response-utils.js';
import { pickAnswerStatusService } from '../../shared/domain/services/pick-answer-status-service.js';
import { usecases } from '../domain/usecases/index.js';
import { scenarioSimulatorBatchSerializer } from '../infrastructure/serializers/scenario-simulator-batch-serializer.js';
Expand All @@ -17,7 +16,6 @@ async function simulateFlashAssessmentScenario(
random,
pickAnswerStatusService,
pickChallengeService,
extractLocaleFromRequest,
},
) {
const {
Expand All @@ -27,12 +25,11 @@ async function simulateFlashAssessmentScenario(
variationPercent,
capacity,
accessibilityAdjustmentNeeded,
locale,
} = request.payload;

const pickAnswerStatus = dependencies.pickAnswerStatusService.pickAnswerStatusForCapacity(capacity);

const locale = dependencies.extractLocaleFromRequest(request);

async function* generate() {
const iterations = _.range(0, numberOfIterations);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Joi from 'joi';

import { securityPreHandlers } from '../../../shared/application/security-pre-handlers.js';
import { LOCALE } from '../../../shared/domain/constants.js';
import { scenarioSimulatorController } from './scenario-simulator-controller.js';

const register = async (server) => {
Expand All @@ -27,6 +28,10 @@ const register = async (server) => {
variationPercent: Joi.number().min(0).max(1),
capacity: Joi.number().min(-8).max(8).required(),
accessibilityAdjustmentNeeded: Joi.boolean(),
locale: Joi.string()
.valid(...Object.values(LOCALE))
.lowercase()
.required(),
})
.required(),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('Acceptance | Controller | scenario-simulator-controller', function ()

validPayload = {
capacity: 4.5,
locale: 'fr-fr',
};

const learningContent = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ describe('Integration | Application | scenario-simulator-controller', function (
initialCapacity,
capacity: 6,
challengePickProbability,
locale: 'en',
},
null,
{ 'accept-language': 'en' },
);

// then
Expand Down Expand Up @@ -133,9 +133,9 @@ describe('Integration | Application | scenario-simulator-controller', function (
'/api/scenario-simulator',
{
capacity,
locale: 'en',
},
null,
{ 'accept-language': 'en' },
);

// then
Expand Down Expand Up @@ -191,9 +191,9 @@ describe('Integration | Application | scenario-simulator-controller', function (
{
capacity,
initialCapacity,
locale: 'en',
},
null,
{ 'accept-language': 'en' },
);

// then
Expand All @@ -219,6 +219,85 @@ describe('Integration | Application | scenario-simulator-controller', function (
]);
});
});
context('When the route is called with uppercased supported locale', function () {
it('should call simulateFlashAssessmentScenario usecase with correct arguments', async function () {
// given
const capacity = -3.1;

const pickChallengeImplementation = sinon.stub();
pickChallengeService.chooseNextChallenge.returns(pickChallengeImplementation);
const pickAnswerStatusFromCapacityImplementation = sinon.stub();
pickAnswerStatusService.pickAnswerStatusForCapacity
.withArgs(capacity)
.returns(pickAnswerStatusFromCapacityImplementation);

usecases.simulateFlashAssessmentScenario
.withArgs({
pickChallenge: pickChallengeImplementation,
locale: 'en',
pickAnswerStatus: pickAnswerStatusFromCapacityImplementation,
initialCapacity,
})
.resolves(simulationResults);
securityPreHandlers.checkAdminMemberHasRoleSuperAdmin.returns(() => true);

// when
const response = await httpTestServer.request(
'POST',
'/api/scenario-simulator',
{
capacity,
initialCapacity,
locale: 'EN',
},
null,
);

// then
expect(response.statusCode).to.equal(200);
const parsedResult = parseJsonStream(response);
expect(parsedResult).to.deep.equal([
{
index: 0,
simulationReport: [
{
challengeId: challenge1.id,
errorRate: errorRate1,
capacity: capacity1,
minimumCapability: 0.6190392084062237,
answerStatus: 'ok',
reward: reward1,
difficulty: challenge1.difficulty,
discriminant: challenge1.discriminant,
numberOfAvailableChallenges: 1,
},
],
},
]);
});
});
context('When the route is called with non supported locale', function () {
it('should returns 400', async function () {
// given
const capacity = -3.1;

// when
const response = await httpTestServer.request(
'POST',
'/api/scenario-simulator',
{
capacity,
initialCapacity,
locale: 'jp',
},
null,
);

// then
expect(response.statusCode).to.equal(400);
expect(response.result.errors[0].detail).to.have.string('"locale" must be one of');
});
});
});
});
});
Expand Down

0 comments on commit 2104ad0

Please sign in to comment.