Skip to content

Commit

Permalink
Make validator deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
Faboslav committed Nov 6, 2024
1 parent d0c42ac commit 71d54ff
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
14 changes: 14 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"pino-pretty": "10.2.3",
"pkg-dir": "7.0.0",
"pprof": "3.2.1",
"prando": "6.0.1",
"query-string": "8.1.0",
"ramda": "0.28.0",
"reflect-metadata": "0.2.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Scenario, ScenarioResult } from '../Scenario';
import { AvailabilityScenarioHelper } from '../../helpers/AvailabilityScenarioHelper';
import descriptions from '../../consts/descriptions';
import { Context } from '../../context/Context';
import Prando from 'prando';

export class AvailabilityChecIntervalScenario implements Scenario {
private readonly product: Product;
Expand All @@ -26,7 +27,8 @@ export class AvailabilityChecIntervalScenario implements Scenario {
);

const availabilities = result.data ?? [];
const randomAvailability = availabilities[Math.floor(Math.random() * availabilities.length)] ?? null;
const randomAvailability =
availabilities[new Prando(availabilities.length).nextInt(0, availabilities.length - 1)] ?? null;

if (randomAvailability === null) {
context.terminateValidation = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { BookingUnitItemSchema, Option, Product, UnitType } from '@octocloud/types';

const randomInteger = (min: number, max: number): number => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
import Prando from 'prando';

interface GetUnitItemsData {
quantity: number;
Expand Down Expand Up @@ -59,7 +56,7 @@ export class ProductBookable {
};

private readonly pickRandomAvailabilityID = (array: string[]): string => {
return array[Math.floor(Math.random() * array.length)];
return array[new Prando(array.length).nextInt(0, array.length - 1)];
};

public getValidUnitItems = (data?: GetUnitItemsData): BookingUnitItemSchema[] => {
Expand All @@ -68,7 +65,8 @@ export class ProductBookable {
const unitId = unit.id;

const quantity =
data?.quantity ?? randomInteger(option.restrictions.minUnits || 1, option.restrictions.maxUnits ?? 5);
data?.quantity ??
new Prando(option.id).nextInt(option.restrictions.minUnits || 1, option.restrictions.maxUnits ?? 5);
return Array(quantity).fill({ unitId });
};

Expand Down

0 comments on commit 71d54ff

Please sign in to comment.