Skip to content

Commit

Permalink
fall back to romeRepository when appellationGateway (diagoriente) tak…
Browse files Browse the repository at this point in the history
…es more than 700ms
  • Loading branch information
JeromeBu committed Jan 27, 2025
1 parent e86fffd commit 32f6275
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { AppellationDto, toLowerCaseWithoutDiacritics } from "shared";
import { AppellationDto, sleep, toLowerCaseWithoutDiacritics } from "shared";
import { AppellationsGateway } from "../ports/AppellationsGateway";

export class InMemoryAppellationsGateway implements AppellationsGateway {
public async searchAppellations(query: string): Promise<AppellationDto[]> {
if (this.#delayInMs > 0) await sleep(this.#delayInMs);

return this.#nextResults.filter((appellation) =>
toLowerCaseWithoutDiacritics(appellation.appellationLabel).includes(
toLowerCaseWithoutDiacritics(query),
Expand All @@ -14,5 +16,11 @@ export class InMemoryAppellationsGateway implements AppellationsGateway {
this.#nextResults = nextResult;
}

public setDelayInMs(delayInMs: number) {
this.#delayInMs = delayInMs;
}

#delayInMs = 0;

#nextResults: AppellationDto[] = [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { AppellationAndRomeDto, expectToEqual } from "shared";
import { InMemoryUowPerformer } from "../../unit-of-work/adapters/InMemoryUowPerformer";
import { createInMemoryUow } from "../../unit-of-work/adapters/createInMemoryUow";
import { InMemoryAppellationsGateway } from "../adapters/InMemoryAppellationsGateway";
import { InMemoryRomeRepository } from "../adapters/InMemoryRomeRepository";
import { AppellationSearch } from "./AppellationSearch";

describe("AppellationSearch Manual test, which take to long to run in CI", () => {
let romeRepo: InMemoryRomeRepository;
let appellationsGateway: InMemoryAppellationsGateway;
let appellationSearch: AppellationSearch;

beforeEach(() => {
romeRepo = new InMemoryRomeRepository();
appellationsGateway = new InMemoryAppellationsGateway();
appellationSearch = new AppellationSearch(
new InMemoryUowPerformer({
...createInMemoryUow(),
romeRepository: romeRepo,
}),
appellationsGateway,
);
});

it("has a fallback when diagoriente searchAppellations takes too long", async () => {
appellationsGateway.setNextSearchAppellationsResult([
{ appellationCode: "19364", appellationLabel: "Secrétaire" },
]);

const appellation: AppellationAndRomeDto = {
romeCode: "M1607",
appellationCode: "19364",
appellationLabel: "Secrétaire",
romeLabel: "Secrétariat",
};

const appellationFallback: AppellationAndRomeDto = {
romeCode: "M1607",
appellationCode: "19367",
appellationLabel:
"Secrétaire bureautique spécialisé / spécialisée - FALLBACK",
romeLabel: "Secrétariat",
};

romeRepo.appellations = [appellation, appellationFallback];

appellationsGateway.setDelayInMs(680);

expectToEqual(
await appellationSearch.execute({
searchText: "secretaire",
fetchAppellationsFromNaturalLanguage: true,
}),
[
{
appellation: appellation,
matchRanges: [{ startIndexInclusive: 0, endIndexExclusive: 10 }],
},
],
);

appellationsGateway.setDelayInMs(720);

expectToEqual(
await appellationSearch.execute({
searchText: "secretaire",
fetchAppellationsFromNaturalLanguage: true,
}),
[
{
appellation: appellation,
matchRanges: [{ startIndexInclusive: 0, endIndexExclusive: 10 }],
},
{
appellation: appellationFallback,
matchRanges: [{ startIndexInclusive: 0, endIndexExclusive: 10 }],
},
],
);
});
});
10 changes: 8 additions & 2 deletions back/src/domains/core/rome/use-cases/AppellationSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
AppellationAndRomeDto,
AppellationMatchDto,
ROME_AND_APPELLATION_MIN_SEARCH_TEXT_LENGTH,
sleep,
zStringMinLength1,
} from "shared";
import { z } from "zod";
Expand Down Expand Up @@ -77,8 +78,13 @@ export class AppellationSearch extends TransactionalUseCase<
uow: UnitOfWork,
searchText: string,
): Promise<AppellationAndRomeDto[]> {
const appellations =
await this.#appellationsGateway.searchAppellations(searchText);
const apiCallPromise =
this.#appellationsGateway.searchAppellations(searchText);

const appellations = await Promise.race([
apiCallPromise,
sleep(700).then(() => []),
]);

if (appellations.length === 0) return [];

Expand Down
24 changes: 12 additions & 12 deletions back/src/domains/core/rome/use-cases/AppellationSearch.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { AppellationSearch } from "./AppellationSearch";
describe("AppellationSearch", () => {
let romeRepo: InMemoryRomeRepository;
let appellationsGateway: InMemoryAppellationsGateway;
let useCase: AppellationSearch;
let appellationSearch: AppellationSearch;

beforeEach(() => {
romeRepo = new InMemoryRomeRepository();
appellationsGateway = new InMemoryAppellationsGateway();
useCase = new AppellationSearch(
appellationSearch = new AppellationSearch(
new InMemoryUowPerformer({
...createInMemoryUow(),
romeRepository: romeRepo,
Expand All @@ -24,7 +24,7 @@ describe("AppellationSearch", () => {

it("returns the list of found matches with ranges", async () => {
expectToEqual(
await useCase.execute({
await appellationSearch.execute({
searchText: "lapins",
fetchAppellationsFromNaturalLanguage: false,
}),
Expand All @@ -42,9 +42,9 @@ describe("AppellationSearch", () => {
);
});

it("returns the list of found matches with ranges from minimum search caracters", async () => {
it("returns the list of found matches with ranges from minimum search characters", async () => {
expectToEqual(
await useCase.execute({
await appellationSearch.execute({
searchText: "lap",
fetchAppellationsFromNaturalLanguage: false,
}),
Expand All @@ -64,7 +64,7 @@ describe("AppellationSearch", () => {

it("issues no queries for short search texts", async () => {
expectToEqual(
await useCase.execute({
await appellationSearch.execute({
searchText: "l",
fetchAppellationsFromNaturalLanguage: false,
}),
Expand All @@ -74,7 +74,7 @@ describe("AppellationSearch", () => {

it("returns empty list when no match is found", async () => {
expectToEqual(
await useCase.execute({
await appellationSearch.execute({
searchText: "unknown_search_term",
fetchAppellationsFromNaturalLanguage: false,
}),
Expand Down Expand Up @@ -107,7 +107,7 @@ describe("AppellationSearch", () => {
];

expectToEqual(
await useCase.execute({
await appellationSearch.execute({
searchText: "secret",
fetchAppellationsFromNaturalLanguage: true,
}),
Expand Down Expand Up @@ -154,7 +154,7 @@ describe("AppellationSearch", () => {
];

expectToEqual(
await useCase.execute({
await appellationSearch.execute({
searchText: "secret",
fetchAppellationsFromNaturalLanguage: true,
}),
Expand All @@ -174,17 +174,17 @@ describe("AppellationSearch", () => {

it("returns empty array if no match", async () => {
expectToEqual(
await useCase.execute({
await appellationSearch.execute({
searchText: "unknown_search_term",
fetchAppellationsFromNaturalLanguage: true,
}),
[],
);
});

it("have a fallback when diagoriente appellations search returns empty array", async () => {
it("has a fallback when diagoriente appellations search returns empty array", async () => {
expectToEqual(
await useCase.execute({
await appellationSearch.execute({
searchText: "lapins",
fetchAppellationsFromNaturalLanguage: true,
}),
Expand Down

0 comments on commit 32f6275

Please sign in to comment.