fix: change GAME_ROUNDS from strings to numbers

This commit is contained in:
lila 2026-04-28 12:29:46 +02:00
parent 7d3c456efe
commit 02ccc88d24
8 changed files with 181 additions and 14 deletions

View file

@ -60,7 +60,7 @@ const validBody = {
target_language: "it",
pos: "noun",
difficulty: "easy",
rounds: "3",
rounds: 3,
};
const fakeTerms = [
@ -177,4 +177,22 @@ describe("POST /api/v1/game/answer", () => {
expect(body.success).toBe(false);
expect(body.error).toContain("Question not found");
});
it("returns 400 when a field has an invalid value", async () => {
const res = await request(app)
.post("/api/v1/game/start")
.send({ ...validBody, difficulty: "impossible" });
const body = res.body as ErrorResponse;
expect(res.status).toBe(400);
expect(body.success).toBe(false);
});
it("returns 400 when rounds has an invalid value", async () => {
const res = await request(app)
.post("/api/v1/game/start")
.send({ ...validBody, rounds: "invalid" });
const body = res.body as ErrorResponse;
expect(res.status).toBe(400);
expect(body.success).toBe(false);
});
});

View file

@ -14,7 +14,7 @@ const validRequest: GameRequest = {
target_language: "it",
pos: "noun",
difficulty: "easy",
rounds: "3",
rounds: 3,
};
const fakeTerms = [

View file

@ -21,7 +21,7 @@ export const createGameSession = async (
request.target_language,
request.pos,
request.difficulty,
Number(request.rounds),
request.rounds,
);
const answerKey = new Map<string, number>();

View file

@ -24,19 +24,19 @@ const LABELS: Record<string, string> = {
type GameSetupProps = { onStart: (settings: GameRequest) => void };
type SettingGroupProps = {
type SettingGroupProps<T extends string | number> = {
label: string;
options: readonly string[];
selected: string;
onSelect: (value: string) => void;
options: readonly T[];
selected: T;
onSelect: (value: T) => void;
};
const SettingGroup = ({
const SettingGroup = <T extends string | number>({
label,
options,
selected,
onSelect,
}: SettingGroupProps) => (
}: SettingGroupProps<T>) => (
<div className="w-full">
<p className="text-xs font-bold tracking-widest uppercase text-(--color-primary) mb-2">
{label}
@ -52,7 +52,7 @@ const SettingGroup = ({
: "bg-white text-(--color-primary-dark) border-(--color-primary-light) hover:bg-(--color-surface) hover:-translate-y-0.5 active:translate-y-0"
}`}
>
{LABELS[option] ?? option}
{LABELS[String(option)] ?? option}
</button>
))}
</div>
@ -68,7 +68,7 @@ export const GameSetup = ({ onStart }: GameSetupProps) => {
);
const [pos, setPos] = useState<string>(SUPPORTED_POS[0]);
const [difficulty, setDifficulty] = useState<string>(DIFFICULTY_LEVELS[0]);
const [rounds, setRounds] = useState<string>(GAME_ROUNDS[0]);
const [rounds, setRounds] = useState<number>(GAME_ROUNDS[0]);
const handleSourceLanguage = (value: string) => {
if (value === targetLanguage) {