- Add AppError base class, ValidationError (400), NotFoundError (404) - Add central error middleware in app.ts - Remove inline safeParse error handling from controllers - Replace plain Error throws with NotFoundError in gameService
58 lines
1.9 KiB
TypeScript
58 lines
1.9 KiB
TypeScript
async function main() {
|
|
// Step 1: start a game
|
|
const startResponse = await fetch("http://localhost:3000/api/v1/game/start", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
source_language: "en",
|
|
target_language: "it",
|
|
pos: "noun",
|
|
difficulty: "easy",
|
|
rounds: "3",
|
|
}),
|
|
});
|
|
const game = await startResponse.json();
|
|
console.log("Game started:", JSON.stringify(game, null, 2));
|
|
|
|
// Step 2: answer each question (always pick option 0)
|
|
for (const question of game.data.questions) {
|
|
const answerResponse = await fetch(
|
|
"http://localhost:3000/api/v1/game/answer",
|
|
{
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
sessionId: game.data.sessionId,
|
|
questionId: question.questionId,
|
|
selectedOptionId: 0,
|
|
}),
|
|
},
|
|
);
|
|
const result = await answerResponse.json();
|
|
console.log("Raw result:", JSON.stringify(result, null, 2));
|
|
console.log(
|
|
`${question.prompt}: ${result.data.isCorrect ? "✓" : "✗"} (picked ${0}, correct was ${result.data.correctOptionId})`,
|
|
);
|
|
}
|
|
|
|
const badRequest = await fetch("http://localhost:3000/api/v1/game/start", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ source_language: "en" }),
|
|
});
|
|
console.log("400 test:", badRequest.status, await badRequest.json());
|
|
|
|
// Send a valid shape but a session that doesn't exist
|
|
const notFound = await fetch("http://localhost:3000/api/v1/game/answer", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
sessionId: "00000000-0000-0000-0000-000000000000",
|
|
questionId: "00000000-0000-0000-0000-000000000000",
|
|
selectedOptionId: 0,
|
|
}),
|
|
});
|
|
console.log("404 test:", notFound.status, await notFound.json());
|
|
}
|
|
|
|
main();
|