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') }) })