From 5e0bc1dc001065f1abbcb89fcc81427fd17c44c3 Mon Sep 17 00:00:00 2001 From: Dorian Date: Thu, 12 Mar 2026 23:17:16 +0000 Subject: [PATCH] test: verify correct/incorrect feedback in respond endpoint (BUG-1) Tests confirm checkAnswer integration: correct answer returns correct: true, wrong answer returns correct: false. Co-Authored-By: Claude Opus 4.6 --- server/src/routes/fights-respond.test.ts | 78 ++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 server/src/routes/fights-respond.test.ts diff --git a/server/src/routes/fights-respond.test.ts b/server/src/routes/fights-respond.test.ts new file mode 100644 index 0000000..86d7bbf --- /dev/null +++ b/server/src/routes/fights-respond.test.ts @@ -0,0 +1,78 @@ +import { describe, it, expect, beforeEach, afterEach } from 'vitest' +import { Hono } from 'hono' +import { fightsRouter } from './fights.js' +import { waitForHumanResponse, submitHumanResponse, clearAllPending } from '../engine/human-responses.js' +import type { Challenge } from '../engine/challenges.js' + +const app = new Hono() +app.route('/api/fights', fightsRouter) + +const testChallenge: Challenge = { + type: 'speed_blitz', + label: 'Speed Blitz', + prompt: 'What is 2+2?', + answers: ['4'], + scoring: 'factual', + timeout_ms: 8000, + baseDamage: 20, + choices: ['4', '3', '5'], +} + +afterEach(() => { + clearAllPending() +}) + +describe('POST /fights/:fightId/respond/:botId', () => { + it('returns correct: true for correct answer', async () => { + const fightId = `fight-respond-correct-${Date.now()}` + const botId = 'bot-respond-test' + + // Set up pending challenge + const { promise } = waitForHumanResponse(fightId, botId, testChallenge, 1) + + const res = await app.request(`/api/fights/${fightId}/respond/${botId}`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ answer: '4' }), + }) + + expect(res.status).toBe(200) + const json = await res.json() as { accepted: boolean; correct: boolean } + expect(json.accepted).toBe(true) + expect(json.correct).toBe(true) + + await promise // consume + }) + + it('returns correct: false for wrong answer', async () => { + const fightId = `fight-respond-wrong-${Date.now()}` + const botId = 'bot-respond-wrong' + + const { promise } = waitForHumanResponse(fightId, botId, testChallenge, 1) + + const res = await app.request(`/api/fights/${fightId}/respond/${botId}`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ answer: 'banana' }), + }) + + expect(res.status).toBe(200) + const json = await res.json() as { accepted: boolean; correct: boolean } + expect(json.accepted).toBe(true) + expect(json.correct).toBe(false) + + await promise + }) + + it('returns 404 for no pending challenge', async () => { + const res = await app.request('/api/fights/nonexistent/respond/nonexistent', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ answer: 'test' }), + }) + + expect(res.status).toBe(404) + const json = await res.json() as { error: string } + expect(json.error).toContain('No pending challenge') + }) +})